• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Marshal
Posts: 80212
423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 80212
423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have incorporated the changes asked: Though, i still dont knwo how to finish it



Help please
 
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 80212
423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 80212
423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Custom Painting Approaches has an example that is similiar to what your want (it draws coloured rectangles).
 
reply
    Bookmark Topic Watch Topic
  • New Topic