473,401 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Creating a new JComponent

23
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.
Apr 12 '08 #1
9 4067
sukatoa
539 512MB
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.
Are those number of circles are fixed?
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
Apr 12 '08 #2
JosAH
11,448 Expert 8TB
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.
Why would you want a circular JComponent like thing? Why not create a, say,
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,

Jos
Apr 12 '08 #3
rpm27
23
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:
Expand|Select|Wrap|Line Numbers
  1.  void setColor(Color c)
  2. void setSize(int x)
  3. void setPosition(int x, int y)
  4. void setActive(boolean b)
  5. void draw(Graphics g)
  6.  
My original idea was to create a class which extends JPanel, and have as instance variable an array of circles (which will be initialized with 5 circles).
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.
Apr 12 '08 #4
JosAH
11,448 Expert 8TB
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.
Failing 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,

Jos
Apr 13 '08 #5
rpm27
23
Failing 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,

Jos
I 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?
Apr 13 '08 #6
JosAH
11,448 Expert 8TB
I 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?
Something 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 ...

kind regards,

Jos
Apr 13 '08 #7
rpm27
23
Something 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 ...
But, what if it's not a circle? Maybe some irregular shape, or a silhouette? I know I'm testing you patience :-) but I'd like to put my question in another way. I'd like to create a new button, with a look and appearance that I specify. Anyways, I'll try to extend class JComponent, and use paintComponent(Graphic g) to specify how it looks like. Thank you.
Apr 13 '08 #8
JosAH
11,448 Expert 8TB
You're changing the problem statement. Those non-circles didn't exist in your
original post.

kind regards,

Jos
Apr 13 '08 #9
rpm27
23
You're changing the problem statement. Those non-circles didn't exist in your
original post.
This thread seems to get annoying :-) Anyways I don't actually have time to test these solutions. When I'll try them, I'll post more questions . Thanks again
Apr 13 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Schisto | last post by:
I need to develop a graphical component that should come in two flavors: a static one (that could be exported to a image and viewed in a browser, for example) and a dynamic one, which could be...
2
by: rdsteph | last post by:
Python411 is a series of podcasts about Python, aimed at hobbyists and others who are learning Python. Each episode focuses on one aspect of learning Python, or one kind of Python programming, and...
6
by: owen | last post by:
Generally speaking, what does it mean when I see a "button" with red text showing this message instead of the control I've dragged onto the web form in Design View.? (But the page works fine at...
2
by: Pawan | last post by:
Hi Guys, I have this current assignment where I have to develop online forms for local municipal authorities. I have to use adobe acrobat to create online forms from PDFs (which I have never done...
15
by: Carlos Lozano | last post by:
Hi, What is the right way to create an OCX COM component. The component is already registerred, but can't create an instance. I am using the reference to the interop module created. If I use...
12
by: Mats Lycken | last post by:
Hi, I'm creating a CMS that I would like to be plug-in based with different plugins handling different kinds of content. What I really want is to be able to load/unload plugins on the fly without...
9
by: =?Utf-8?B?YmJn?= | last post by:
Hi all, I read somewhere "using kernel stuff in thread is not good.." if ManualResetEvent object is created in thread but not actually used, will it affect performance? Bob
1
by: amitpathak | last post by:
I am working on a project in which I am required to read a file and load the contents in some JComponent to show it to the users. I was reading the file with the help of stringbuffer/filebuffer...
8
by: eddiewould | last post by:
Hi, I want to write a custom widget which will act similarly to a JPanel (i.e it can contain other Components), but semantically it's not a kind of JPanel so it shouldn't extend from it. Here is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.