wood burning stoves
The moose likes Swing / AWT / SWT and the fly likes Self controlling component Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Self controlling component" Watch "Self controlling component" New topic

Self controlling component

con nor

Joined: Jun 10, 2004
Posts: 2
Hi there

I'm making a painfully slow adjustment from flash programming to java.
I am currently trying to generate a scatterplot where the points draw themselves and detect if the mouse is over them.

I have created a scatterplot class, a pointsArrayList class and a custom Point object(code below). I've currently got the points to draw themselves but cannot get the mouseEvent to work. I've extended the point class from the component class so I though mouseActions would be inherited.
Have I got this all wrong?

package connor;
import java.awt.*;
import java.awt.event.*;

public class Point extends Component implements MouseListener{
private int _x;
private int _y;

/** Creates a new instance of Point */
public Point(int x,int y) {

public int getX() {
return _x;

public int getY() {
return _y;

public void paint(Graphics g){

public void mouseClicked(MouseEvent e) {

public void mouseEntered(MouseEvent e) {

public void mouseExited(MouseEvent e) {

public void mousePressed(MouseEvent e) {

public void mouseReleased(MouseEvent e) {
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Did you calculate, how many MouseListeners you get, for a 1024x768 pixel area?

The pixels are drawn on a Component. Give the Component the MouseListener.

There is a AWT/Swing-Forum waiting for this question to be moved to.
(Wait for the sheriffs, their job is moving.)

Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Moving this to the Swing / JFC / AWT forum...

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Eddie Vanda
Ranch Hand

Joined: Mar 18, 2003
Posts: 281
Hi Con,

If you can see the circles drawn by the paint routine then I guess your point must have a size big enough to accept a mouse listener. Maybe you could also implement MouseMotionListener and see if that can detect your mouse.

I would not have chosen Point as the class name since there is already a Point class in java.awt.* which you import. Better ranchers than me can say whether that is a problem or not.

I guess individual mouse listener points are ok if you only have a couple of hundred or so. That would certainly simplify your mouse event point identification. I have had up to 500 small JLabel widgets each with a mouse listener with no discernable slowdown effect.

Tell us how you go.


The nice thing about Standards is that there are so many to choose from!
con nor

Joined: Jun 10, 2004
Posts: 2
I've tried the motionListener and still no joy.
I did try a different method where I declared a private Reactangle object within the point. This allowed me to trigger a mousePoint by passing it the mouseCo-ords from the applet (code below). This gave me the result I wanted but not in the way I wanted.

I think what I'm trying to do is create a JComponent from scratch?
The bigger picture is to create interective graphs and to do this the final elements will need to have some degree of self-knowledge. I want them to draw themselves and detect mouse actions relating to themselves.
I then need to extend them into having different shapes & colours. (which is why I didn't want to rely on the Rectangle object). I can't seem to find much information on this anywhere? Any Ideas?

//A rectangular data node object
package connor;
import java.awt.*;

//public class RectPoint extends Rectangle{
public class RectPoint{
private int _x,_y;
private String _n;
private boolean inside=false;
private Rectangle box;
Graphics g;

// Creates a new instance of RectPoint
public RectPoint(String name, int x, int y, int width, int height) {
box=new Rectangle(x,y,width,height);

//if mouse is moved inside our rectangle, the flag "inside" is set
public void SubmitMouseCoord(int x, int y)

public String getN(){
return _n;

public void paint(Graphics g){
g.fillRect(box.x, box.y, box.width, box.height);
g.drawString(this.getN(), this._x+20, this._y+10);
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Welcome to the Ranch con!

Performing Custom Painting from the Java Tutorial will provide you useful compulsary information.
I will quote:

Before you implement a component that performs custom painting, first make sure that you really need to do so. You might be able to use the text and image capabilities of labels, buttons

SCJP2. Please Indent your code using UBB Code
I agree. Here's the link: http://aspose.com/file-tools
subject: Self controlling component
It's not a secret anymore!