0

I am working on creating a game for fun that basically is a simplistic representation for evolution.

Essentially, when I click on my moving ball it changes color. The goal is to continuously change until it matches the background color meaning the ball is successfully hidden. Eventually I will add more balls but I am trying to figure out how to change its color upon a mouse click. I have created the moving ball animation so far.

How can I change the ball color when I click on the ball?

Code:

public class EvolutionColor
{
   public static void main( String args[] )
   {
       JFrame frame = new JFrame( "Bouncing Ball" );
       frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

       BallPanel bp = new BallPanel(); 
       frame.add( bp );

       frame.setSize( 1800, 1100 ); // set frame size
       frame.setVisible( true ); // display frame
       bp.setBackground(Color.YELLOW);
   } // end main
}

class BallPanel extends JPanel implements ActionListener
{       
    private int delay = 10;
    protected Timer timer;

    private int x = 0;      // x position
    private int y = 0;      // y position
    private int radius = 15;    // ball radius

    private int dx = 2;     // increment amount (x coord)
    private int dy = 2;     // increment amount (y coord)

    public BallPanel() 
    {
        timer = new Timer(delay, this);
        timer.start();      // start the timer
    }

    public void actionPerformed(ActionEvent e)
    // will run when the timer fires
    {
        repaint();
    }

    public void mouseClicked(MouseEvent arg0) 
    {
       System.out.println("here was a click ! ");
    }

    // draw rectangles and arcs
    public void paintComponent( Graphics g )
    {
        super.paintComponent( g ); // call superclass's paintComponent 
        g.setColor(Color.red);

        // check for boundaries
        if (x < radius)  dx = Math.abs(dx);
        if (x > getWidth() - radius)  dx = -Math.abs(dx);
        if (y < radius)  dy = Math.abs(dy);
        if (y > getHeight() - radius)  dy = -Math.abs(dy);

        // adjust ball position
        x += dx;
        y += dy;
        g.fillOval(x - radius, y - radius, radius*2, radius*2);
    }    

}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
user3474775
  • 199
  • 1
  • 2
  • 12

2 Answers2

6

Take a look at How to Write a Mouse Listener.

Don't make decisions about the state of the view in the paintComponent, painting can occur for any number of reasons, many you don't control. Instead, make theses decisions within the actionPerformed method of your Timer

You may also wish to consider changing your design slightly. Rather then having the balls as JPanels, you create a virtual concept of a ball, which contains all the properties and logic it needs and use the JPanel to paint them. You could then store them in some kind of List, each time you register a mouse click you could iterate the List and check to see if any of the balls were clicked

Have look at Java Bouncing Ball for an example

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

Instead of hard-coding the color (g.setColor(Color.red);), why not create an attribute:

g.setColor(currentColor);

Then when you click in the circle area, change currentColor.

Edwin Torres
  • 2,774
  • 1
  • 13
  • 15
  • Thank you! I added my mouse listener and changed the color. That way when it repaints the canvas it changes the color. – user3474775 May 31 '14 at 02:49