0

I'am trying to make a little game for academic stuff. What I want to do is to make a random number of balls move inside a JPanel, with random directions, but when I click inside of the ball it should sysout the actual coords of the ball. Here is my class without the move methods (I already made it but don't need to post it for this question):

public class Ball implements Runnable {

    int x = UserInterface.BALL_START; 
    int y = 0;
    int size  = 10;     
    Color color;

    public Ball() {
        Random random = new Random();    
        int r, g, b;
        r = random.nextInt(256);
        g = random.nextInt(256);
        b = random.nextInt(256);
        color = new Color(r,g,b);
        y = random.nextInt(500);
    }

    public void run() {
        int vx = (new Random()).nextInt(10) + 1;                
        while (x < UserInterface.BALL_END) {
            x += vx;            
            try {
                Thread.sleep(15);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        x = UserInterface.BALL_END;
    }

    public int getX() {
        return x;
    }

    public void paint(Graphics g)  {
        g.setColor(color);
        g.fillOval(x - size / 2, y - size / 2, size, size);     
    }
}

How can I add a mouselistener to the ball class without make it as a JComponent or JLabel or JPanel?

cyberpirate92
  • 3,076
  • 4
  • 28
  • 46
  • 2
    What ever is calling `paint`, should be registered as the `MouseListener`, it should then talk with the ball to figure out where it is and do what ever else you need it to – MadProgrammer Oct 23 '14 at 11:17
  • `public void paint(Graphics g) {` missing `super.paint` – mKorbel Oct 23 '14 at 11:20
  • @mKorbel don't miss the super cause I don't extended nothing –  Oct 23 '14 at 11:23
  • The documentation of the class [`AWTEventMulticaster`](http://docs.oracle.com/javase/7/docs/api/java/awt/AWTEventMulticaster.html) has an example for implementing an event source. Of course, you need a mechanism to forward the component’s mouse events to the ball instance… – Holger Oct 23 '14 at 11:24
  • See MadProgrammers's comment! In addition, you need to ensure the mouse coords from your mouse click are in the correct coordinate system, then get the Ball to create a Shape and use "contains" on it to check if your click has "hit" the ball. – csadler Oct 23 '14 at 11:42
  • Are you sure you want each of your balls to be running in their own thread? – Kayaman Oct 23 '14 at 11:46
  • @Kayaman I just want multiple balls moving random, and when I select one of them it should print the actual coords of the ball –  Oct 23 '14 at 11:56
  • 2
    @niarb 1. (you comment about don't extended nothing) nothing will be painted, 2. I just want multiple balls moving random - use Swing Timer, 1-5k good ideas with code examples in SSCCE/MCVE form are here, – mKorbel Oct 23 '14 at 12:42
  • @MadProgrammer, what you are saying is that I should have a class named BallsZone (extends JPanel), and this class should have a mouselistener to check if any ball has been clicked? –  Oct 23 '14 at 13:19
  • Something is painting the, somewhere, there is some kind of component, that should have the MouseListener – MadProgrammer Oct 23 '14 at 20:05

1 Answers1

1

See Collision detection with complex shapes & replace the small moving ball with the mouse position and tweak the collision code for an Area and a Point using Area.contains(Point2D).

That code renders the visible part to an image displayed in a label. Display the label in a container/layout/constraint that preserves the size, and we can add the mouse listener directly to the label.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433