1 Replies - 3649 Views - Last Post: 20 August 2012 - 04:56 PM Rate Topic: -----

#1 willphill02  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 24-July 12

Draw circles to screen using mouse listener java applet

Posted 20 August 2012 - 04:42 PM

Hi I am trying to create an applet that draws a christmas tree then using buttons the user can draw decorations (just circles at the moment) to the tree by clicking on a location on the tree. So far i've got it so that when i select a button and click on the tree it draws the desired circle however when i click somewhere else it removes the first circle, and the same when i click on another button to draw a different colored circle. My question is how to I get the circles to remain on the screen when I click to draw another one? I think the circles need to be defined as objects but i'm not sure how to do this. Any help would be much appreciated. Here is my code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class christmasTree extends Applet implements ActionListener, MouseListener, MouseMotionListener
{
	int[] xPoints = {200,50,350};
	int[] yPoints = {35,400,400};
	Button lights;
	Button decorations;
	Button stars;
	int mx;
	int my;
	Object source;


	public void init()
	{
		lights = new Button("Add Lights");
		decorations = new Button("Add Decorations");
		stars = new Button("Add Stars");
		add(lights);
		add(decorations);
		add(stars);
		lights.setLocation(50, 450);
		addMouseListener( this );
        addMouseMotionListener( this );
        lights.addActionListener(this);
        decorations.addActionListener(this);
	}

	public void paint (Graphics g)
	{
        g.setColor(Color.green);
        g.fillPolygon(xPoints, yPoints, 3);
        g.setColor(Color.black);
        g.fillRect(175, 400, 50, 50);
        g.drawString(Integer.toString(mx), 25, 85);
        g.drawString(Integer.toString(my), 25, 100);
        if (source == lights){
			g.setColor(Color.yellow);
		    g.fillOval(mx, my, 20, 20);
		}
		if (source == decorations){
			g.setColor(Color.blue);
		    g.fillOval(mx, my, 50, 50);
		}

	}

	public void actionPerformed(ActionEvent ev)
	{
		if (ev.getSource() == lights){
			source = lights;
		}
		if (ev.getSource() == decorations){
		    source = decorations;
		}
		repaint();

	}

	public void mousePressed(MouseEvent e)
	{
		mx = e.getX();
		my = e.getY();
		repaint();


	}

	public void mouseReleased(MouseEvent e)
	{

	}
	public void mouseEntered(MouseEvent e)
	{}
	public void mouseExited(MouseEvent e)
	{}
	public void mouseMoved(MouseEvent e)
	{}
	public void mouseClicked(MouseEvent e)
	{

	}
	public void mouseDragged(MouseEvent e)
	{}

}

Thanks



Is This A Good Question/Topic? 0
  • +

Replies To: Draw circles to screen using mouse listener java applet

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Draw circles to screen using mouse listener java applet

Posted 20 August 2012 - 04:56 PM

You should start your paint() method with

super.paint( g );

but that won't solve the question you've asked.

The paint() method redraws the graphic with each call or repaint(), so the ball drawn on the tree is determined by the position of the mouse when it was clicked. The paint() method 'forgets' about the previous ball drawn. So, your program needs a way to store the previous ball locations or to 'remember' them.

As you suggest, you could make a Ball object that has mx and my characteristics (variables), and you could store instances of Ball objects in a collection, maybe an ArrayList. Then, each mouse click adds a new Ball instance to the collection, the paint() method is called, and all objects in the collection are drawn.

Make sense?
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1