• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trying to display and recolor multiple circles

 
Jenn Terry
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

This is my firsts post! I'm trying to tech myself swing and have bumped into a problem.

I have created two classes. The FrameViewer is my driver class, and Circle is my constructor.

The first problem is, the program seems to be creating both circles, but it only displays displaying the last added circle (in this case, circle2).

The second problem is, I want to pass the color I wish to use to the constructor, but I'm having problems figuring out the proper method to do so. In the constructor, I have assigned Color.BLUE as a default. On circle two, I want to override this color choice and assign Color.Orange. I am not having any luck setting this to work either.

I have posted my code below. Would you gents be willing to examine my code and perhaps post the snippet of code that will correct my issues. Oh, and so I can learn, can you also tell me what I did wrong and what you did correctly. I would really appreciate it.



 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By default a JFrame uses a BorderLayout. You are adding both component to the "CENTER" of the BorderLayout, but only one component can be displayed in each area of the BorderLayout, so only the last one added is displayed. Read the section from the Swing tutorial on Layout Managers for more information. Even if you did change the layout manager to something like a FlowLayout it would still not display as you want because each component is displayed based on the rules of the layout manager.

You really need to create one component that will paint all the circles on the same panel. Check out Custom Painting Approaches for examples of the two common ways to do this.
 
Jenn Terry
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. I'll get right on that reading.

Do you have any thoughts on what I am doing incorrectly on color issue I mentioned?
 
Ron McLeod
Saloon Keeper
Posts: 1263
131
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you need to set the color before drawing the circle.
 
Paul Clapham
Sheriff
Posts: 21862
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I must be missing something, because I don't understand how the Circle class you posted there can have a setColor() method.
 
Jenn Terry
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you talking about the following? If so, it seems to work there as I end up with a blue circle.
 
Paul Clapham
Sheriff
Posts: 21862
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I was talking about the Circle's setColor method. This code implies it must have one:



The code you posted calls the setColor method of a Graphics object.
 
Jenn Terry
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was the code I was looking for help on.

I think I've solved that issue with the following:

In the Circle class, I added


In the Frameview class, I reworked a few things as follows.


That removed the errors and seemed to make the system happy. I'm still trying to figure out the getting both circles to display. I'm afraid I'm having no luck with the reading I was assigned. It went over my head by a mile.
 
Paul Clapham
Sheriff
Posts: 21862
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean to say you had compiler errors because of that line? It would have helped if you had said that in the first place.
 
Jenn Terry
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry. I didn't leave details out on purpose. After 14 hours of trying to figure this out, I'm becoming a bit forgetful.
 
Chris Barrett
Bartender
Posts: 317
24
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jenn,

Great to hear you made some progress.

Top Level Containers, such as JFrame, hold other JComponents (such as JPanels). Those JComponents will be displayed based on the Layout Manager assigned to the JFrame. By default, JFrame's will use the BorderLayout - which (as Rob mentioned) will always put the JComponents in the same location (the BorderLayout.CENTER location) if not implicitly specified. Right now, your two circles are being assigned to the same BorderLayout location so you can only see one of them.

The big question is - do you want the circles to display overlapping each other, or beside each other in separate JPanels?
If you want the circles side by side, than your approach is fine. You just need to change the frame object's Layout Manager to FlowLayout before you add the Circles. FlowLayout will by default display multiple JComponents side-by-side.

On the other hand, if you want the circles to overlap, you'll need to break down things a bit. As you have it right now, the Circle is trying to be two things - it's trying to be both a business object and a display object. You should have your circle business object just keep track of the colour and the location of that particular circle and a separate CirclePanel that extends JPanel should be used to draw those circles. That CirclePanel would store the Circle objects in a collection and then iterate through them in the CirclePanel's paintComponent method. This will result in all the circles being drawn in the JPanel extension.

I hope that helps!

Cheers!
Chris
 
Jenn Terry
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the explanation. I'm a little brain sponged right now, so I need to walk away. I will hit it again after a nights sleep and let you know the results.
 
Jenn Terry
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Chris R Barrett
I took you explanation and did a rework and all is running now. I can see it was basically the same thing that Rob Camick was telling me, but for some reason, having both explanations did the trick. I have now created separate constructors for different shapes and call them from the container.

Thanks everyone who made suggestions.

I guess the next step in the evolution of my learning is to figure out how to animate them. Once I figure that out, I'll do collision detection. After that, who knows but there is always more to learn.

Again, thank you.
 
Chris Barrett
Bartender
Posts: 317
24
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jenn,

That's great to hear!

For animation, you will want to look at setting up a Swing Timer. For more information: http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html. Hint - if it's to be random, you would want your Circle object class to have moveLeft, moveRight, moveUp, moveDown methods that the timer could randomly trigger. Those move methods would set the x and y according to the direction moved, so you will want to fill out the missing getters and setters in the Circle.

For collision detection, it's important for you to clarify if these are truly circles (where the ovalHeight and ovalWidth are always the same - therefore not requiring separate height and width variables), or ovals. Once you know that, spend some time on the math sites looking at the algebra formulas to determine if a circle overlaps another circle. You should also think about using the xPosition, yPosition coordinates as the centre point of the circle (drawOval uses xPosition and yPosition for the upper left corner of the bounding box that surrounds the oval), since collision involves measuring out from the centre points by the radius distance.

Good luck!
Chris
 
Jenn Terry
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds fun! I think I will first work with a set of true circles and go from there. It is a walk before you run kind of thing.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!