1 Replies - 1537 Views - Last Post: 11 May 2012 - 04:43 PM Rate Topic: -----

#1 badgetfleet   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-May 12

How do I move a Rectangle Shape in an Applet by clicking?

Posted 11 May 2012 - 04:12 PM

Create a Java application that paints a square. The user specifies the position and size of the square by positioning and clicking the mouse twice, once for the upper left corner of the square and once for the lower right corner.


I got the square and I got the mouse click input working but I'm not sure how to move the square.


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



  public class JavaSquare extends JApplet implements MouseListener  
  {		
  int height=20;
  int width=20;
  int x=10;
  int y=10;

	  public void paint(Graphics g )
  	  {
	     g.setColor(Color.red);
  	     g.drawRect(10,10,height,width);
  	     g.setColor(Color.blue);
  	     g.fillRect(x,y,height,width); 
  	  }
  public void Frame()
    {	  
	      JFrame frame = new JFrame("Java Square Assignment");
     	  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	  frame.setSize(400,400);
    	  frame.setVisible(true);
    	  frame.setLocationRelativeTo(null);
    	  frame.setLayout(new BorderLayout());
    }
 public JavaSquare()
 {
   addMouseListener(new MouseAdapter()
   {
   public void mouseClicked(MouseEvent e) 
   {
	   System.out.println("Mouse clicked; # of clicks: " + e.getClickCount() );
     if (e.getClickCount() == 2) 
    		{
    	 
    		}
   }
   
    }); 
 }
  	
	}
  


Is This A Good Question/Topic? 0
  • +

Replies To: How do I move a Rectangle Shape in an Applet by clicking?

#2 CasiOo   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1578
  • View blog
  • Posts: 3,551
  • Joined: 05-April 11

Re: How do I move a Rectangle Shape in an Applet by clicking?

Posted 11 May 2012 - 04:43 PM

You wont be able to use getClickCount for what you want.

You could have a boolean value indicating which corner to change
Like so:
public void mouseClicked(MouseEvent e) {
	if (positionTopLeftCorner) {
		topLeft = new Point(..., ...);
	}
	else {
		...
	}
	
	positionTopLeftCornor = !positionTopLeftCornor;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1