I want to make circles onto a JPanel using the x and y coordinates that are generated by another method in the same class. I have stored these coordinate values in an ArrayList<Integer>.
How do i access these values or the ArrayList inside the paint(Graphics g) method?
I tried making the ArrayList as final and declared it as a class variable. But, i still couldnt access the list in paint().
Is there some way to pass parameters into the paint() ??? ???
Line 11: your list doesn't need to be final, although it does no harm. But you need ArrayList<Point>() on the right hand side (at least until Java 7, when it will be ArrayList<>() ).
Line 21: You don't need the cast since you've already declared its generic type.
Line 30: don't call paintComponent() yourself: call repaint()
Line 34: paintComponent only takes 1 argument (Graphics g). It wouldn't make any sense to pass arrays in as an argument, since this method is called automatically any time the panel is repainted, so where would these extra arguments come from? The method can, however, use the panel's instance variables.
Line 39: it won't display on your panel because it's outputting to the System.out
Line 42: coordinates.get(i).x is x co-ord
You probably want to override getPreferredSize() as well. Using an IDE will help catch a lot of your mistakes and do automatic formatting etc.
Joined: May 20, 2011
I have tried to simplify the code , though it still doesnt work. Do help!
I want to display n number of circles whose center coordinates are mentioned in x and y. I am not able to access x and y in the paintComponent() method even though i have declared it as class variable.
Any help would be appreciated!
Joined: Oct 13, 2005
That's not an SSCCE because I can't execute it. to be simple, it ought to have things like the translate call removed.
I can suggest a few things to get rid of. Get rid of your two arrays; that is bad design. Get rid of the List<String>, because it is bad practice to change things to and from Strings if you can avoid it. You need a List<Point> or a Point. the java.awt.Point class shows some examples of bad design, so beware of it. Maybe you ought to create your own Point class.
Set up a List<Point> and populate it with a few instances. Then iterate the List and draw some circles
If this is the AWT Point you're using, I don't think that will work: getX() returns a double, while drawOval takes integers. You would have to put an (int) cast in there. Or just use:
Joined: Oct 13, 2005
Luigi Plinge wrote:If this is the AWT Point you're using, I don't think that will work: getX() returns a double, while drawOval takes integers. You would have to put an (int) cast in there. Or just use:
As I said, the java.awt.Point class shows some design errors. I had actually created my own Point class.