Hey, I'm trying to create a small program which displays several circles across the screen. However, I intend for these circles to be clickable and change color, size... on different mouse events on each object. So in order for the Class circle to be added to a JPanel and to register events, it should extend the JComponent class. Of course not JComponent since it is abstract. So which one? JPanel, JLabel... Thank you.
Creating a new JComponent
Collapse
X
-
-
Originally posted by rpm27Hey, I'm trying to create a small program which displays several circles across the screen. However, I intend for these circles to be clickable and change color, size... on different mouse events on each object. So in order for the Class circle to be added to a JPanel and to register events, it should extend the JComponent class. Of course not JComponent since it is abstract. So which one? JPanel, JLabel... Thank you.
When you click those circles, how would you choose to set it's color? by a ColorChooser?
or a patterned color... ( Set )....
Are those circles are changing it's position in every event?
instead of several circles across the screen, have you tried 1 circle across the screen? (Program)
Please be more specific....
Our experts here are happy to help you if your problem have the supported complete details....
sukatoa -
Originally posted by rpm27Hey, I'm trying to create a small program which displays several circles across the screen. However, I intend for these circles to be clickable and change color, size... on different mouse events on each object. So in order for the Class circle to be added to a JPanel and to register events, it should extend the JComponent class. Of course not JComponent since it is abstract. So which one? JPanel, JLabel... Thank you.
CircleHandler, than can contain and manage Circles? That handler can extend
from a JPanel class and store Circles. The JPanel can handle mouse events and
decide which Circle the mouse was on. Deal with it from there: dragging, resizing
and whatever you have in mind with those Circles. At every paintComponent( )
call for the extended JPanel all you have to do is redraw those Circles.
kind regards,
JosComment
-
I didn't realize that my post wasn't clear enough. Here's what I had in mind.
As a test before I stumbled upon this problem, I already created a class Circle with the following methods:
Code:void setColor(Color c) void setSize(int x) void setPosition(int x, int y) void setActive(boolean b) void draw(Graphics g)
In addition, I wanted to include a Button, which when clicked, will activate the next circle (which means make it able to respond to events), while disactivating the current circle, by iterating through the array.
I thought about implementing JosAH solution, but I had some problems. For example, I want the circle to be highlighted (ie change its color) when you put the mouse above it (using mouseEntered() method). I can't imagine how the program will know which circle was on focus if the circle isn't itself a component. In addition, event handling should take in consideration the active status of each circle, which made me realize that with this additional complexity, circle events should be handled by the Circle class and not the JPanel.
I hope this time I was clear enough. I appreciate your input.Comment
-
Originally posted by rpm27I thought about implementing JosAH solution, but I had some problems. For example, I want the circle to be highlighted (ie change its color) when you put the mouse above it (using mouseEntered() method). I can't imagine how the program will know which circle was on focus if the circle isn't itself a component. In addition, event handling should take in consideration the active status of each circle, which made me realize that with this additional complexity, circle events should be handled by the Circle class and not the JPanel.
receive all these events through its listeners, determine which circle was pointed
at and delegate all the 'business logic' i.e. 'what needs to be done' to the appropriate
circle. That moves all the 'circle functionality' to the Circle class just as you wanted.
The JPanel just acts as a 'messenger', it delegates everything to the circles
and offers its own functionality being a JComponent for the circles.
kind regards,
JosComment
-
Originally posted by JosAHFailing to implement an idea doesn't mean the idea is bad. The JPanel could
receive all these events through its listeners, determine which circle was pointed
at and delegate all the 'business logic' i.e. 'what needs to be done' to the appropriate
circle. That moves all the 'circle functionality' to the Circle class just as you wanted.
The JPanel just acts as a 'messenger', it delegates everything to the circles
and offers its own functionality being a JComponent for the circles.
kind regards,
JosComment
-
Originally posted by rpm27I understood that (I've already written something similar), but when I create an event handler class which implements MouseListener, and I write a method mouseEntered(), how the program will know that the mouse entered this particular circle so I can change its state to Active?
of the circle and r_c is the radius of the circle. You have to figure out what to
do with two or more overlapping circles, but that's a geometry problem, not a
classes/components problem. Maybe all the circles for which the test succeeds
get active? It's up to you ...
kind regards,
JosComment
-
Originally posted by JosAHSomething like |m-c| <= r_c where m is the mouse coordinates, c is the center
of the circle and r_c is the radius of the circle. You have to figure out what to
do with two or more overlapping circles, but that's a geometry problem, not a
classes/components problem. Maybe all the circles for which the test succeeds
get active? It's up to you ...Comment
-
Originally posted by JosAHYou're changing the problem statement. Those non-circles didn't exist in your
original post.Comment
Comment