Meaningless Drivel is fun!*
The moose likes Swing / AWT / SWT and the fly likes Drawing circles using the coordinate values that are stored in an ArrayList. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Drawing circles using the coordinate values that are stored in an ArrayList." Watch "Drawing circles using the coordinate values that are stored in an ArrayList." New topic
Author

Drawing circles using the coordinate values that are stored in an ArrayList.

Rose Jac
Ranch Hand

Joined: May 20, 2011
Posts: 33
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() ??? ???

Do Help!!!

Any help would be appreciated!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
Why are you using the paint() method in the first place? You should be using paintComponent().

Moving thread to our GUIs forum.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
Why are you using a List<Integer> to store those coordinates, rather than creating a Point class to store them? Then you would use a List<Point> instead.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

And after you make the change suggested by Campbell, to get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem.


luck, db
There are no new questions, but there may be new answers.
Rose Jac
Ranch Hand

Joined: May 20, 2011
Posts: 33
I have incorporated the changes asked: Though, i still dont knwo how to finish it



Help please
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

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.
Rose Jac
Ranch Hand

Joined: May 20, 2011
Posts: 33
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.
Help!



Any help would be appreciated!
please help!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
Good grief!
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
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

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:
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
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.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2152
    
    7
Custom Painting Approaches has an example that is similiar to what your want (it draws coloured rectangles).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Drawing circles using the coordinate values that are stored in an ArrayList.
 
Similar Threads
Bouncing Circles
How to draw circles whose center coordinates are stored in an array?
writeObject writes rubbish
calling a method of a java class from jsp
passing stack elements to paint method