Results 1 to 4 of 4
  1. #1
    VettesRus is offline Member
    Join Date
    Jun 2012
    Posts
    35
    Rep Power
    0

    Default Moving Shapes Program. Cannot remove first shape when new shape is clicked.

    Hello, I am writing a program that puts either a circle or square on the screen and moves/resizes it each time the button is clicked. My problem is I need to have the previous shape clear when the new shape is clicked. So if I click square, then click circle I would like the square to disappear from the program just leaving the circle. I have been trying to figure out how to clear the previous shape but have no luck. Any help is appreciated.

    Java Code:
    import java.awt.*;							//	AWT = "Abstract Window Toolkit"
    import java.awt.event.*;					//		or "Another Window Toolkit"
    
    public class GettingInShape extends Frame
    							implements MouseListener
    {
    	private final int						//	To define the size of our buttons
    				BUTTON_WIDTH = Abutton.BUTTON_WIDTH,
    				BUTTON_HEIGHT = Abutton.BUTTON_HEIGHT;
    
    //
    //	Constructor
    //	===========
    //
    //	The constructor customizes a new frame to accommodate the drawing window.
    //	It sets the scene with the buttons for the user interaction.
    //
    	public GettingInShape()
    	{
    		setTitle("Getting in shape ...");	//	We set the characteristics of our
    		setLocation(50, 50);				//		drawing window
    		setSize(450, 400);
    		setBackground(Color.orange);
    		setVisible(true);					//		and we make it appear
    
    		addWindowListener(myWindow);		//	To check on the window
    		addMouseListener(this);				//		and on the mouse
    		
    		int x = BUTTON_WIDTH * 1/2;
    		int y = BUTTON_HEIGHT * 3/2;
    		squareButton = new Abutton("Square", Color.green, x, y, BUTTON_WIDTH, BUTTON_HEIGHT);
    		y += BUTTON_HEIGHT * 1.5;
    		circleButton = new Abutton("Circle", Color.cyan, x, y, BUTTON_WIDTH, BUTTON_HEIGHT);
    
    		initializationComplete = true;		//	Everything is initialized
    	}
    
    	public void action()
    	{
    		repaint();
    
    		System.out.println("\nClick on one of the buttons!\n");
    	}
    
    //
    //	The next method checks where the mouse is been clicked.
    //	Each button is associated with its own action.
    //
    	private void check()
    	{
    		if (squareButton.isInside(lastX, lastY)) {
    											//	We generate a square
    			square = new Square();
    		}
    		else if (circleButton.isInside(lastX, lastY)) {
    											//	We generate a circle
    			circle = new Circle();
    		}
    		else  {
    			System.out.println("What?");
    		}
    		repaint();
    	}
    
    //
    //	The only "graphical" method of the class is the paint method.
    //
    	public void paint(Graphics pane)
    	{
    		if (initializationComplete) {
    	
    			squareButton.paint(pane);		//	We display the buttons
    			circleButton.paint(pane);
    
    			square.paint(pane);				//		and we show the shape
    			circle.paint(pane);
    		}
    	}
    
    //
    //	M o u s e L i s t e n e r
    //	=========================
    //
    //	The implementation of the MouseListener requires the implementation of the
    //		following five methods.  Each method deals with a particular event
    //		associated with the mouse.
    //
    //	mouseClicked:	invoked when the mouse button is pressed and released at the
    //					same location.
    //	mousePressed:	invoked when the mouse button is pressed down
    //	mouseReleased:	invoked when the mouse button is released
    //	mouseEntered:	invoked when the mouse pointer moves into a component
    //					(i.e., the Frame)
    //	mouseExited:	invoked when the mouse pointer moves out of a component
    //
    //	Note that we are "listening" to mouse events that occur in conjunction with the
    //		Frame that implements the methods associated with the mouse.  If your
    //		application has several frames, each frame can deal with its own mouse
    //		events in completely independent ways.
    //
    //	Only the click of the mouse is of interest to this demo  ... or is it?
    //
    	public void mouseClicked(MouseEvent event)
    	{
    		//	Note that, if the mouse is clicked, then it must have been pressed (and
    		//		released at the same location).  Therefore, the mouse location has
    		//		already been recorded.  We only have to handle the mouse click.
    		check();								//	To handle the mouse click
    	}
    	public void mousePressed(MouseEvent event)
    	{
    		lastX = event.getX();					//	Update the mouse location
    		lastY = event.getY();
    												//	Highlight the button being hit
    		if (squareButton.isInside(lastX, lastY))
    			squareButton.flip();				//		(if any)
    		else if (circleButton.isInside(lastX, lastY))
    			circleButton.flip();
    		repaint();								//	Refresh the window
    	}
    
    	public void mouseReleased(MouseEvent event)
    	{
    												//	"De-highlight" any button that
    		if (squareButton.isInside(lastX, lastY))
    			squareButton.flip();				//		may be highlighted
    		else if (circleButton.isInside(lastX, lastY))
    			circleButton.flip();
    		repaint();								//	Refresh the window
    	}
    
    	public void mouseEntered(MouseEvent event) {}
    	public void mouseExited(MouseEvent event) {}
    
    //
    //	The instance variables
    //
    	private boolean initializationComplete = false;	//	Nothing is initialized yet
    
    	private Square square;							//	Two shapes: a square
    	private Circle circle;							//		and a circle to play with
    
    	private Abutton squareButton,					//	Two buttons
    					circleButton;					//		to create shapes
    
    	private UneFenetre myWindow = new UneFenetre();	//	A handle on the window
    
    	private int lastX, lastY;						//	To keep the mouse location
    
    }	// end GettinInShape
    Java Code:
    /*
    	Defines and implements a circle.
    */
    
    import java.awt.*;
    import java.awt.event.*;
    
    public class Circle
    {
    	private final int						//	To define the size of our buttons
    				BUTTON_WIDTH = Abutton.BUTTON_WIDTH,
    				BUTTON_HEIGHT = Abutton.BUTTON_HEIGHT;
    
    	//
    	//	In the default constructor we generate a random circle.
    	//
    	public Circle()
    	{
    		randomize();
    	}
    
    	//
    	//	The randomize method defines random characteristics for the circle.
    	//
    	private void randomize()
    	{
    		x = (int) (200 * Math.random()) + BUTTON_WIDTH * 2;
    		y = (int) (200 * Math.random()) + BUTTON_HEIGHT * 2;
    		size = (int) (100 * Math.random());
    	}
    
    	//
    	//	The only graphical method of the class is the paint method.
    	//
    	public void paint(Graphics pane)
    	{
    		pane.setColor(Color.cyan);			//	A circle is always cyan
    		pane.fillOval(x, y, size, size);	//	We draw a circle
    
    		pane.setColor(Color.black);			//		with a border
    		pane.drawOval(x, y, size, size);
    	}
    
    	private int	x, y,						//	The circle's location
    				size;						//		and its size
    
    }	// end Circle
    Java Code:
    /*
    	Defines and implements a square.
    */
    
    import java.awt.*;
    import java.awt.event.*;
    
    public class Square
    {
    	private final int						//	To define the size of our buttons
    				BUTTON_WIDTH = Abutton.BUTTON_WIDTH,
    				BUTTON_HEIGHT = Abutton.BUTTON_HEIGHT;
    
    	//
    	//	In the default constructor we generate a random square.
    	//
    	public Square()
    	{
    		randomize();
    	}
    
    	//
    	//	The randomize method defines random characteristics for the square.
    	//
    	private void randomize()
    	{
    		x = (int) (200 * Math.random()) + BUTTON_WIDTH * 2;
    		y = (int) (200 * Math.random()) + BUTTON_HEIGHT * 2;
    		size = (int) (100 * Math.random());
    	}
    
    	//
    	//	The only graphical method of the class is the paint method.
    	//
    	public void paint(Graphics pane)
    	{
    		pane.setColor(Color.green);			//	A square is always green
    		pane.fillRect(x, y, size, size);	//	We draw a square
    
    		pane.setColor(Color.black);			//		with a border
    		pane.drawRect(x, y, size, size);
    	}
    
    	private int	x, y,						//	The square's location
    				size;						//		and its size
    
    }	// end Square

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default Re: Moving Shapes Program. Cannot remove first shape when new shape is clicked.

    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    VettesRus is offline Member
    Join Date
    Jun 2012
    Posts
    35
    Rep Power
    0

    Default Re: Moving Shapes Program. Cannot remove first shape when new shape is clicked.

    Thanks, unfortunately I am still unsure how to remove the previous shape. I did make an erase method in both the circle and square class, but could not come close to getting it to work properly.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default Re: Moving Shapes Program. Cannot remove first shape when new shape is clicked.

    You don't need an erase method, you need to properly override paintComponent and include a call to the superclass's implementation of it.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Need help moving a shape
    By ll Source ll in forum New To Java
    Replies: 1
    Last Post: 12-04-2011, 02:30 AM
  2. Need help moving a shape!
    By chrishans in forum AWT / Swing
    Replies: 3
    Last Post: 10-16-2011, 07:17 AM
  3. Replies: 1
    Last Post: 06-08-2011, 10:22 PM
  4. Make shape rounding (moving in circle)
    By mneskovic in forum New To Java
    Replies: 8
    Last Post: 08-17-2010, 03:05 PM
  5. Replies: 1
    Last Post: 04-02-2010, 12:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •