2

I would need to get the location coordinates of all the drawings I'm creating in the paintComponent method. How can I do this?

Notice that I use the timer to perform some animations so the coordinates change at every tick of the timer.

public class TestPane extends JPanel {

    private int x = 0;
    private int y = 100;
    private int radius = 20;
    private int xDelta = 2;

    public TestPane() {
        Timer timer = new Timer(10, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                x += xDelta;
                if (x + (radius * 2) > getWidth()) {
                    x = getWidth() - (radius * 2);
                    xDelta *= -1;


                } else if (x < 0) {
                    x = 0;
                    xDelta *= -1;
                }
                label.setText(x+" "+y);
                repaint();
            }
        });
        timer.start();
    }

More code...

 protected void paintComponent(Graphics g) {
            Random random = new Random();
            super.paintComponent(g);
            g.setColor(Color.ORANGE);
            g.fillOval(random.nextInt(500), random.nextInt(500) - radius, radius * 2, radius * 2);

            g.setColor(Color.BLUE);
            g.fillOval(y, x - radius, radius * 2, radius * 2);
//            label.setText(label.getText()+ x+" "+y); ;
            g.setColor(Color.RED);
            g.fillOval(x, y - radius, radius * 2, radius * 2);
//            label.setText(label.getText()+ x+" "+y);
        }
mKorbel
  • 109,525
  • 20
  • 134
  • 319
ocram
  • 1,384
  • 6
  • 20
  • 36
  • 1
    *"I would need to get the location coordinates of all the drawings I'm creating.."* Ummm.. for two of them you are *supplying* the x's any y's, and for the other you can store the `random.nextInt(500)` in a class level attribute before using it.. If that is not a solution to this, I don't understand the question! – Andrew Thompson Nov 13 '15 at 11:30
  • @AndrewThompson I apologise, I added the timer part at the top. So every time the timer ticks the positions are changed. – ocram Nov 13 '15 at 11:38
  • 1
    See `List` in the example cited [here](http://stackoverflow.com/a/11944233/230513). – trashgod Nov 13 '15 at 11:38
  • @trashgod I cant see any `List` – ocram Nov 13 '15 at 12:02
  • *"So every time the timer ticks the positions are changed."* I ..don't see how that changes anything I said earlier. (shrug) Voting to close as 'unclear what you are asking'. – Andrew Thompson Nov 13 '15 at 12:04
  • @AndrewThompson Mmmh... so like I said these are moving objects so the coordinates are changing every iteration of the timer. I want to print or store the coordinates for the three drawings. What is it not clear? – ocram Nov 13 '15 at 12:15
  • *"I want to print or store the coordinates for the three drawings. What is it not clear?"* That's the fist time you've mentioned 'print' (note that though we have super powers, they do **not** include mind reading). *"..or store.."* So put the x,y values into a list structure, most easily as a `List` or `List` ...sheesh. – Andrew Thompson Nov 13 '15 at 12:25

1 Answers1

2

Your program should maintain a List<Node> as a class level attribute. Each instance of Node should contain the geometry required to render each element in your program.

class Node {
    private Point p;
    private int r;
    …
}

In your ActionListener, update the fields of each Node in the List. When a repaint() occurs, the new positions will be waiting for paintComponent() to render.

@Override
public void paintComponent(Graphics g) {
    …
    for (Node n : nodes) {
        // draw each node
    }

A complete example, named GraphPanel, is cited here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045