1 Replies - 6316 Views - Last Post: 12 July 2009 - 06:02 AM Rate Topic: -----

#1 Tyler1986  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 29-November 08

Drawing Rectangles with Mouse Events

Post icon  Posted 11 July 2009 - 10:34 PM

My code does most of what I want it to do: It draws rectangles corresponding to coordinates returned from mouse pressed and mouse released events. The problem is after I draw the rectangle it won't stay. The only code I have that clears the canvas in under paint, repaint is only called on mouse drag. Why when I mouse release or mouse move does my canvas clear. One other thing, when either the height or width of my rectangle is negative the rectangle is filled in black, not sure why, not a big deal though, really.

package pracpapp2;


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MouseTracker4July extends JFrame
   implements MouseListener, MouseMotionListener {


  private static final long serialVersionUID = 1L;
  private JLabel mousePosition;
  int x, y;
  int x1, x2, y1, y2;
  int w, h;
  private JLabel recStart;
  private JLabel recStop;
  private JLabel cords;
   // set up GUI and register mouse event handlers
   public MouseTracker4July()
   {
	  super( "Rectangle Drawer" );

	  mousePosition = new JLabel();
	  mousePosition.setHorizontalAlignment(SwingConstants.CENTER);
	  getContentPane().add( mousePosition, BorderLayout.CENTER );

	
	  JLabel text1 = new JLabel();
	  text1.setText( "At the center the mouse pointer's coordinates will be displayed." );
	  getContentPane().add( text1, BorderLayout.SOUTH );

	  recStart = new JLabel();
	  getContentPane().add(recStart, BorderLayout.WEST);

	  recStop = new JLabel();
	  getContentPane().add(recStop, BorderLayout.EAST);

	  cords = new JLabel();
	  getContentPane().add(cords, BorderLayout.NORTH);
	  
	
	  addMouseListener( this );		// listens for own mouse and
	  addMouseMotionListener( this );  // mouse-motion events

	  setSize( 800, 600 );
	  setVisible( true );
   }

   // MouseListener event handlers
   // handle event when mouse released immediately after press
   public void mouseClicked( MouseEvent event )
   {
	  mousePosition.setText( "Clicked at [" + event.getX() +
		 ", " + event.getY() + "]" );
   }

   // handle event when mouse pressed
   public void mousePressed( MouseEvent event )
   {

	  mousePosition.setText( "Pressed at [" +(x1 = event.getX()) +
		 ", " + (y1 = event.getY()) + "]" );

	  recStart.setText( "Start:  [" + x1 +
		 ", " + y1 + "]" );
   }

   // handle event when mouse released after dragging
   public void mouseReleased( MouseEvent event )
   {
	 mousePosition.setText( "Released at [" +(x2 = event.getX()) +
		 ", " + (y2 = event.getY()) + "]" );

	 recStop.setText( "End:  [" + x2 +
		 ", " + y2 + "]" );

   }

   // handle event when mouse enters area
   public void mouseEntered( MouseEvent event )
   {
	  mousePosition.setText( "Mouse entered at [" + event.getX() +
		 ", " + event.getY() + "]" );
   }

   // handle event when mouse exits area
   public void mouseExited( MouseEvent event )
   {
	  mousePosition.setText( "Mouse outside window" );
   }

   // MouseMotionListener event handlers
   // handle event when user drags mouse with button pressed
   public void mouseDragged( MouseEvent event )
   {
	  mousePosition.setText( "Dragged at [" + (x = event.getX()) + 
		 ", " + (y = event.getY()) + "]" );
	  // call repaint which calls paint
	  repaint();

   }

   // handle event when user moves mouse
   public void mouseMoved( MouseEvent event )
   {
	  mousePosition.setText( "Moved at [" + event.getX() +
		 ", " + event.getY() + "]" );
   }
   
   public void paint(Graphics g)
   {
   	  super.paint(g); // clear the frame surface
   	  g.drawString("Start Rec Here", x1, y1);
	  g.drawString("End Rec Here", x, y);

	  w = x1 - x;
	  h = y1 - y;
	  w = w * -1;
	  h = h * -1;

	  g.drawRect(x1, y1, w, h);

	  cords.setText( "w = " + w + ", h = " + h);
   }

   public static void main( String args[] )
   { 
	  MouseTracker4July application = new MouseTracker4July();
	  application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
   }

} // end class MouseTracker



This post has been edited by Tyler1986: 11 July 2009 - 10:35 PM


Is This A Good Question/Topic? 1

Replies To: Drawing Rectangles with Mouse Events

#2 scrat  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 18
  • Joined: 30-June 09

Re: Drawing Rectangles with Mouse Events

Posted 12 July 2009 - 06:02 AM

Your mouseDragged method repaints the scene, displaying your rectangle. When you release the mouse button, you are setting text in a couple of JLabels and this is causing your content pane to repaint. If you add repaint methods to mouseMoved and mouseReleased, it works.

The problem I think is that you are overriding paint() on the JFrame, but is the Container for getContentPane() that is doing the work. Perhaps the next step might be to add a JPanel to your content pane and override its paint method.

HTH Scrat
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1