Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Rose Jac
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5116
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rose Jac
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have incorporated the changes asked: Though, i still dont knwo how to finish it



Help please
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 441
IntelliJ IDE Scala Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2475
8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic