Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 3 of 4 FirstFirst 1234 LastLast
Results 51 to 75 of 86

Thread: drawLine problem, lines not connected

  1. #51
    Member
    Join Date
    Sep 2012
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: drawLine problem, lines not connected

    Hm I have jdk 1.7.0_07
    so What do you suggest?

  2. #52
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,434
    Thanks
    49
    Thanked 2,216 Times in 2,189 Posts

    Default Re: drawLine problem, lines not connected

    What do you suggest?
    Calling super.paintComponent().
    If you don't understand my answer, don't ignore it, ask a question.

  3. #53
    Member
    Join Date
    Sep 2012
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: drawLine problem, lines not connected

    I tried and you keep saying to use an arraylist.

    I dont know how, as i've said before. I know the calls and such, but I don't know where I'd put any of it.

  4. #54
    Member
    Join Date
    Sep 2012
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: drawLine problem, lines not connected

    Because the applet would need to redraw every point in the array like, everytime the mouse moved?

  5. #55
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,434
    Thanks
    49
    Thanked 2,216 Times in 2,189 Posts

    Default Re: drawLine problem, lines not connected

    Time for some design work and programming:
    Create the arraylist at the class level
    Add points to it where you currently save x,y for the mouse's new location.
    Get the points out of the list and draw lines between them
    If you don't understand my answer, don't ignore it, ask a question.

  6. #56
    Member
    Join Date
    Sep 2012
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: drawLine problem, lines not connected

    by at class level you mean, do something like:
    public class ArrayList {
    ....
    }

  7. #57
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,434
    Thanks
    49
    Thanked 2,216 Times in 2,189 Posts

    Default Re: drawLine problem, lines not connected

    No that is defining a class.
    This statement in your program is at the class level:
    BlankArea blankArea;

    Look at the definition of scope.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #58
    Member
    Join Date
    Sep 2012
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: drawLine problem, lines not connected

    OH ok , I already have an arraylist defined up there, i've kept it there incase.
    is something like this correct?

        ArrayList al = new ArrayList(50000);
        Integer ia[] = (Integer[]) al.toArray();

    I made it that size because I didnt know if its supposed to save every point, or just the most recent 4

  9. #59
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,434
    Thanks
    49
    Thanked 2,216 Times in 2,189 Posts

    Default Re: drawLine problem, lines not connected

    Giving it a size is not too important, it will expand itself if needed.

    You want to save every point that is used to draw segments of the lines you want drawn.

    An advantage of having the points in a arraylist is that if you draw a number of shapes, the points for each shape could be saved in its own arraylist so that shapes could be manipulated individually.
    They could be different colors, moved, deleted, drawn with wider lines, ...
    If you don't understand my answer, don't ignore it, ask a question.

  10. #60
    Member
    Join Date
    Sep 2012
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: drawLine problem, lines not connected

    Ok now my issue comes in, where exactly am I adding points to the arraylist AND how exactly do i make it so it draws everypoint?

  11. #61
    Member
    Join Date
    Sep 2012
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: drawLine problem, lines not connected

    I guess my question isnt where, its pretty much how.

    I know the calls, Ijust don't know where to implement them

  12. #62
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,434
    Thanks
    49
    Thanked 2,216 Times in 2,189 Posts

    Default Re: drawLine problem, lines not connected

    where to implement them
    That was discussed in post#55
    If you don't understand my answer, don't ignore it, ask a question.

  13. #63
    Member
    Join Date
    Sep 2012
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: drawLine problem, lines not connected

    I guess what I really meant was, do I use al.add(x) and then al.add(y)
    or should I give them specific locations in the array like al.add(1, x) ?

  14. #64
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,434
    Thanks
    49
    Thanked 2,216 Times in 2,189 Posts

    Default Re: drawLine problem, lines not connected

    Look at using the Point class.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #65
    Member
    Join Date
    Sep 2012
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: drawLine problem, lines not connected

    Now I'm just more confused.

    I can't get a feel for it by looking at all the docs and functions of the class, I need to be able to find semi-relevant examples of the code, and I can't find that for Point class. I've been looking for a while as I came across that idea earlier today

  16. #66
    Member
    Join Date
    Sep 2012
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: drawLine problem, lines not connected

    I have

    public class point{
    protected int x, y;
    }

    and that's as far as I got

  17. #67
    Member
    Join Date
    Sep 2012
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: drawLine problem, lines not connected

    Am i creating seperate instances of the class for every new xy coordinate?

    EDIT: this is what I have so far, I'm confused now as to how to get the x,y from each point instance when calling drawLine

    code:
    package events;
     
    /*
     * MouseMotionEventDemo.java
     *
     */
     
    import java.awt.*;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionListener;
    import javax.swing.*;
    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JPanel;
    import java.util.*;
     
     
    public class MouseMotionEventDemo extends JPanel
            implements MouseMotionListener {
        BlankArea blankArea;
        JTextArea textArea;
        point Point;
        int lastX=0, lastY=0, currX=0, currY=0;
        ArrayList al = new ArrayList();
        Object ia[] = al.toArray();
        static final String NEWLINE = System.getProperty("line.separator");
     
        public static void main(String[] args) {
            /* Use an appropriate Look and Feel */
            try {
                //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                //UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
                UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
            } catch (UnsupportedLookAndFeelException ex) {
                ex.printStackTrace();
            } catch (IllegalAccessException ex) {
                ex.printStackTrace();
            } catch (InstantiationException ex) {
     
                ex.printStackTrace();
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
            }
            /* Turn off metal's use of bold fonts */
            UIManager.put("swing.boldMetal", Boolean.FALSE);
     
            //Schedule a job for the event dispatch thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
     
        /**
         * Create the GUI and show it.  For thread safety,
         * this method should be invoked from the
         * event-dispatching thread.
         */
     
        public class BlankArea extends JLabel {
        Dimension minSize = new Dimension(100, 50);
     
        public BlankArea(Color color) {
            setBackground(color);
            setOpaque(false);
            setBorder(BorderFactory.createLineBorder(Color.black));  
        }
      }
     
        public class point{
            protected int x, y;
     
            public point() {
                setPoint(0,0);
            }
     
            public void setPoint(int coordx, int coordy){
                x = coordx;
                y = coordy;
            }
     
            public Integer getX(){
                return x;
            }
            public Integer getY(){
                return y;
            }
        }
     
        private static void createAndShowGUI() {
            //Create and set up the window.
            JFrame frame = new JFrame("MouseMotionEventDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            //Create and set up the content pane.
            JComponent newContentPane = new MouseMotionEventDemo();
            newContentPane.setOpaque(true); //content panes must be opaque
            frame.setContentPane(newContentPane);
     
            //Display the window.
            frame.pack();
            frame.setVisible(true);
     
     
        }
     
        public MouseMotionEventDemo() {
            super(new GridLayout(0,1));
            blankArea = new BlankArea(Color.BLUE);
            add(blankArea);
     
            textArea = new JTextArea();
            textArea.setEditable(false);
            JScrollPane scrollPane = new JScrollPane(textArea,
                    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                    JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
            scrollPane.setPreferredSize(new Dimension(200, 75));
     
            add(scrollPane);
     
            //Register for mouse events on blankArea and panel.
            blankArea.addMouseMotionListener(this);
            addMouseMotionListener(this);
     
     
            setPreferredSize(new Dimension(450, 450));
            /*setBorder(BorderFactory.createEmptyBorder(20,20,20,20)); */
     
        }
     
        void eventOutput(String eventDescription, MouseEvent e) {
            textArea.append(eventDescription
                    + " (" + e.getX() + "," + e.getY() + ")"
                    + " detected on "
                    + e.getComponent().getClass().getName()
                    + NEWLINE);
            textArea.setCaretPosition(textArea.getDocument().getLength());
        }
     
       public void mouseMoved(MouseEvent e) {
           eventOutput("Mouse moved", e);
           record(e.getX(),e.getY());
     
       }
     
        @Override
       public boolean mouseDown(Event e, int x, int y){
           return(record(x,y));
       }
     
        @Override
       public boolean mouseEnter(Event e, int x, int y) {
            return(record(x, y));
        }
     
       public void mouseDragged(MouseEvent e) {
            eventOutput("Mouse dragged", e);    
            currX = e.getX();
            currY = e.getY();
            Point = new point();
            Point.setPoint(currX, currY);
            al.add(Point);
            repaint();
        }
     
        @Override
       public void paintComponent(Graphics g) {
            super.paintComponent(g);
     
            for(int i = 0; i < (al.size()/2); i = i+2){
                g.drawLine( ia[1],ia[1] ,ia[2] ,ia[2] );
            }
            record(currX,currY);
       }
     
       protected boolean record(int x, int y){
            lastX = x;
            lastY = y;
            Point.setPoint(lastX, lastY);
            al.add(Point);
            return(true);
        }
    }
    Last edited by crzybldthrwr; September 3rd, 2012 at 04:06 PM.

  18. #68
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,434
    Thanks
    49
    Thanked 2,216 Times in 2,189 Posts

    Default Re: drawLine problem, lines not connected

    You could use the Java SE Point class instead of making your own.

    To get some experience using the classes, make a test program that creates an array list, adds several Point objects to it and then in a loop goes through the list of points and simulates drawing connected lines by printing out a line that says: drawing a line from x1,y1 to x2,y2
    where x1,y1 etc are the points from the array list.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #69
    Member
    Join Date
    Sep 2012
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: drawLine problem, lines not connected

    Edited post 67 to include my progress

    as in post 67, just not sure how to call getX and getY from the point class while inside the drawline function
    Last edited by crzybldthrwr; September 3rd, 2012 at 04:10 PM.

  20. #70
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,434
    Thanks
    49
    Thanked 2,216 Times in 2,189 Posts

    Default Re: drawLine problem, lines not connected

    Did you write the simple program to learn how to use the ArrayList and Point classes?
    When you get that to work you will know how to use the classes.
    If you don't understand my answer, don't ignore it, ask a question.

  21. #71
    Member
    Join Date
    Sep 2012
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: drawLine problem, lines not connected

    I don't see how that would really help me, as I don't know how to GET the x and y from the instances of the class within the array, so i can't write that simple program?

  22. #72
    Member
    Join Date
    Sep 2012
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: drawLine problem, lines not connected

    As each element in the array is an instance of Point, that means each element has an x and a y in that container; i just don't know how to access them
    because ia[1].getX() doesn't work lol

  23. #73
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,434
    Thanks
    49
    Thanked 2,216 Times in 2,189 Posts

    Default Re: drawLine problem, lines not connected

    how that would really help me,
    You need to learn how to use the classes. Mixing a lot of new code into a larger program makes it very hard to work on fixing the problems. The smaller program will use some of the class's methods in a simpler environment.


    Get rid of the array. The data is in the ArrayList. Writing the test program will show you most of what you need to know.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #74
    Member
    Join Date
    Sep 2012
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: drawLine problem, lines not connected

    For me it won't help because I don't know the syntax?
    all I need to figure out is how to get the x and y from the Point instance in the arraylist.

  25. #75
    Member
    Join Date
    Sep 2012
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: drawLine problem, lines not connected

    No matter how simple the program is, if I don't know the syntax it's not gonna matter.

Page 3 of 4 FirstFirst 1234 LastLast

Similar Threads

  1. help with keeping client connected to server
    By frozen java in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 19th, 2011, 01:29 PM
  2. Problem with printing out collinear lines!
    By rkrajnov in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 25th, 2011, 05:32 PM
  3. Problem with Output # of lines
    By coyboss in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 7th, 2011, 09:21 PM
  4. Why rs...still connected
    By javamula in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 30th, 2011, 03:53 PM
  5. g.drawLine doesn't draw line in for loop
    By shumpi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 12th, 2010, 06:15 PM