Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Arraylist with circleobjects, how to paint them in a JPanel inside a JFrame?  RSS feed

 
Nick Tickolainen
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! I've tried googling and looking up an answer to this question but all the information i could find doesn't really cover my problem specifically. Im studying java and this is was an optional exercise in our study book that i've been trying to solve.
I've made circleobjects that holds data for width, height and color. I want to paint these to the jpanel, and add buttons that allows me to cycle through the arraylist of objects.

I've found information how to do this directly into a JPanel, but i'm making doing an applikation in Java swing where i have drag-n-dropped a JPanel inside a JFrame like this:



The marked area is my jPanel. I have a circleclass that looks like this:



I also have a drawclass:



And this is the first lines of code in my GUI-class:



The variable name for my JPanel is jPanel.

Now, how do i make the first circleobject appear in my jpanel?

i've tried everything from jPanel.getGraphics and jPanel.paintComponent to call the methods in the drawclass and the circleclass to display the objects but i can't seem to find the right code for this.

Any help is appreciated!

//Nick
 
Les Morgan
Rancher
Posts: 767
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nick,

You basically have things right, as far as I can understand from your post, but you have a major disconnect between your paintComponent in your JPanel and your circle collection.

In your paintComponent override your need to have a repository that will hold all the circles you want to paint at any point in time. So basically you need another ArrayList in the scope of your paintComponent that the paintComponent override will render all object contained in it. Something along the line of this:

So when you want to add a circle to your JComponent you need to have a setter to load your ArrayList, myCircles, and if you are going to drop it in, then you may want to include location information in each circle.
 
Les Morgan
Rancher
Posts: 767
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nick,

Just a secondary thought: if you are going to want to do anything with your Circle objects, then you might want to look at implementing the Shape Interface, java.awt.Shape, so you can have some interaction in your universe.

Les
 
Nick Tickolainen
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean move my arraylist to the drawclass, something like this?



That kind of makes sense and there no error. You talked about a setter for the arraylist but i'm not quite sure what you meant.



Am i totally off?

 
Carey Brown
Saloon Keeper
Posts: 2840
43
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would add an X, Y location to your Circle class.
 
Carey Brown
Saloon Keeper
Posts: 2840
43
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nick Tickolainen wrote:
First off, I'd change the name of draw, which is a verb, to something like DrawPanel, which is a noun.
I think every "shape" should have its own draw(Graphics2D g) method and know how to paint itself, then you can have

And while whe're talking about "Shape", I mean create a super-class called Shape that Circle extends, not necessarily java.awt.Shape, which I think is overkill at this stage of development.
Don't override paintComponent(), override paint() instead.

 
Campbell Ritchie
Sheriff
Posts: 55329
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nick Tickolainen wrote:You mean move my arraylist to the drawclass, something like this?. . .
No. Don't add data to a display class. That List needs to live somewhere but a display class is the wrong place for it. You need another class to model your app, and that is where the List should be.
You will have to pass a reference to your model to the display classes, and if you do it right you can do something like
myModel.drawCircles(g);
inside the paintComponent method. Which of course you can do by giving all the Circles a method to draw themselves, as I see you have already worked out. There is no need to copy the List.

Why do you have separate width and height fields for a Circle? Surely they will both be the same.

And welcome to the Ranch
 
Campbell Ritchie
Sheriff
Posts: 55329
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few minutes ago, I wrote:. . .
myModel.drawCircles(g);
. . .
And welcome to the Ranch
myModel.drawShapes(g);
would be so much better
 
Les Morgan
Rancher
Posts: 767
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nick,

You need the ArrayList to be visible from your paintComponent in your JPanel, but not actually in your JPanel's paintComponent. That paintComponent override could get called multiple times per second, so in that light adding the entire code you showed is definitely not a good idea. Look at the code I posted last time, it is what you need to do. Where it says to put your code to render your circle objects is the only thing you need add to the paintComponent.

Les

Nick Tickolainen wrote:You mean move my arraylist to the drawclass, something like this?



That kind of makes sense and there no error. You talked about a setter for the arraylist but i'm not quite sure what you meant.



Am i totally off?

 
Les Morgan
Rancher
Posts: 767
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW: the call, "super.paintComponent(g)" has to be the first line in the paintComponent method, if you use it; it is a call to the super class of your object to allow the original paintComponent of the parent class to be run--one thing it will do is clear your screen for you, so the new rendering will not overlay onto a dirty screen.
 
Les Morgan
Rancher
Posts: 767
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using your code, here is what I mean:



Then you just "add" a Circle object any time you want a new one to be rendered, and it will remain in the rendering queue until you remove it from the JPanel's ArrayList.
 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've tried googling and looking up an answer to this question but all the information i could find doesn't really cover my problem specifically.


That is annoying.

First of all rarely will you find the code to copy directly into your application. You will find code that addresses the "concept" of what you are trying to do. It is up to you to understand the basic concept and then make the necessary changes.

You were given working code yesterday when you asked this same question: http://stackoverflow.com/questions/34614888/java-swing-app-arraylist-with-circleobjects-how-to-draw-them-in-a-jpanel-insi/34615861#34615861

Of course you might have to modify the code a little but all the basics are there:

1. How to implement an add(...) method to add object to paint.
2. How to create the ArrayList
3. How to create the Object containing all the information need to paint the object
4. How to override the paintComponent() method to paint everything in the ArrayList.

Out time is valuable. Its annoying when you just ignore the suggestions and then take time of other people as well.
 
Campbell Ritchie
Sheriff
Posts: 55329
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:. . .
. . .


Single responsibility principle. The circle object should take care of itself. Any painting should be done inside the Circle object. Don't make the display class try to work out the details of the circles. If you have your shape interface or similar, you can make that a List of Shapes, and this will work nicely:-Because the Square class overrides the paint() method that code will work nicely and a square will appear in the appropriate location. To go into pedantic mode, the List#add method isn't a setter.

Don't put the List in the display class; it belongs elsewhere. I think you want setColor before you call fillOval.
 
Fred Kleinschmidt
Bartender
Posts: 555
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with what Campbell Ritchie says. The Circle class should have a method


Since Circle is not a subclass of JComponent, I would not call this method "paint" or "paintComponent".
Then in your paintComponent() method of draw class (Note: you should get into the habit of always beginning class names with an upper case letter, so it should be Draw):


 
Campbell Ritchie
Sheriff
Posts: 55329
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest you need a Model class too, which will incorporate the List. That Model class can have a paintEverything(Graphics g) or similar method.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!