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.

Results 1 to 4 of 4

Thread: Drawing a line and repaint()

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Drawing a line and repaint()

    OK, bear with me because I'm new to Java and may get some terminology wrong in explaining this...

    I have a class based on JPanel filled with some components. I want to have a vertical line drawn when the mouse is dragged. This is to help users line up some of the moveable components.
    I have implemented a mouse motion listener for mouseDragged and it is working but I'm having some issues with the line being drawn. It is drawing the line correctly at the mouse cursor location but then it immediately draws another line a bit behind the cursor (and removes the first line). What I THINK is happening is that the draw method for the JPanel is drawing the line in the right place but then the JPanel's components are redrawn the drawline happens again based off of the component position.
    I'll post what I think are the relevant parts of the ReportEditBean class.
    public void paint(Graphics g) {
    	    initializeLayout();
    		super.paint(g);
    		// Draw vertical line here
    		if (mousePoint_ != null && mousePoint_.x > 0 && mousePoint_.y > 0){
    			drawLine(g);
    		}
     
    	}
     
    	public void drawLine(Graphics g){
    		g.setColor(Color.LIGHT_GRAY);
    		g.drawLine(mousePoint_.x, mousePoint_.y +500, mousePoint_.x, mousePoint_.y -400); 
    		repaint();
    	}

    In the constructor for the class I have the mouse motion event, which seems to be working fine, but I'll include it here as well
    long eventMask = AWTEvent.MOUSE_MOTION_EVENT_MASK + AWTEvent.MOUSE_EVENT_MASK; Toolkit.getDefaultToolkit().addAWTEventListener( 
    					new AWTEventListener() 
    					{     public void eventDispatched(AWTEvent e)     
    							{    
    								if (e instanceof MouseEvent) {
    									Point widgetpt = ReportEditBean.this.getLocationOnScreen();
     
    									MouseEvent evt = ((MouseEvent)e);
    									if (evt.getID() == MouseEvent.MOUSE_DRAGGED) {
    										int newX = evt.getX(); // evt.getXOnScreen();
    										int newY = evt.getY(); // evt.getYOnScreen();
    										//int newX = evt.getXOnScreen();
    										//int newY = evt.getYOnScreen();
    										mousePoint_ = new Point((int) newX, (int)newY); //((MouseEvent)e).getPoint();
    										Graphics g = reportPanel_.getGraphics();
    										drawLine(g);
    									} else {
    										mousePoint_ = null;
    									}
    								}   
    							} 
    					}, eventMask);

    Any pointers on what I might have wrong would be appreciated. I'll provide more information if needed.
    Last edited by rogerbacon; October 18th, 2011 at 11:24 AM. Reason: typo


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,099
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: Drawing a line and repaint()

    A few tips:
    a) Override paintComponent, not paint
    b) Use a MouseListener, and don't add a listener via the Toolkit (see How to Write a Mouse Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners) )
    c) You have to know where the initial mouse down event occurred. Capture the point at this location using the mousePressed event, then use this as your first point - the mouse location in the mouseDragged event the second point.

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Drawing a line and repaint()

    Thank you very much. The tip on paintComponent was very helpful. I still have a few other issues but at least I am making progress now.

  4. #4
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,099
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: Drawing a line and repaint()

    Quote Originally Posted by rogerbacon View Post
    Thank you very much. The tip on paintComponent was very helpful. I still have a few other issues but at least I am making progress now.
    I'm glad to here it. If have any questions along the way let us know.

Similar Threads

  1. Replies: 10
    Last Post: September 16th, 2011, 07:49 PM
  2. Java Frame Buffer Line drawing
    By tcmei in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 17th, 2011, 06:55 AM
  3. Line Drawing Program
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 13th, 2010, 02:54 PM
  4. Reading a file line by line using the Scanner class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 07:34 AM
  5. How to Read a file line by line using BufferedReader?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:32 AM