Have a look at code!!

I want to draw a rectangle in an applet and trace

the mouse pressed action only inside that rectangle

To achive this i have created 2 classes

1.Class Rect

This class extends Component and used to draw the Rectangle

using drawRect() method and used to trace the action performed

in side the Rect using a Thread

2.Class Display extends applet,implements ActionListener

and adds the textarea and the Rect class

this class is used to display the rectangle in the applet.

ActionListener is added to this class to trace any

action performed in the Rectangle.So when ever mouse key is

pressed in side the rectangle action event is generated and

that is appended into the textarea

My problem

Action event is fired in the entire area of the applet

how to get the action event(the mouse key press)only in side

the rectangle

I am drawing images dynamicaly at run time , i want to make the code

simplified,so i draw the rect in a class which extends the

component(actually i want to make the rect as a component like

button or label ) so that the addActionListener can be implemented

in that rect to trace any action

what i am doing is right ?

Here is the Runnable code

//////**class Rect***//////////////////////////

import java.awt.*;

import java.awt.event.*;

public class Rect extends Component implements Runnable,MouseListener

{

public Rect(){

addMouseListener (this);

}

public void paint(Graphics g)

{

g.setColor(Color.red);

g.fillRect(10,10,30,30);

}

public synchronized void mousePressed (MouseEvent me) {

processAction ();}

public synchronized void mouseDragged (MouseEvent me) {}

public synchronized void mouseClicked (MouseEvent me) {}

public synchronized void mouseReleased (MouseEvent me) {}

public synchronized void mouseEntered (MouseEvent event){}

public synchronized void mouseExited(MouseEvent event){}

private ActionListener actionListener = null;

public synchronized void addActionListener (ActionListener al) {

actionListener = AWTEventMulticaster.add (actionListener, al);

}

public synchronized void removeActionListener (ActionListener al) {

actionListener = AWTEventMulticaster.remove (actionListener, al);

}

public void processAction () {

if (null != actionListener)

new Thread (this).start ();

}

public void run () {

try { actionListener.actionPerformed

(new ActionEvent(this,ActionEvent.ACTION_PERFORMED,toString ()));

} catch (Exception exc) {

System.out.println ("failed processAction:"+exc);

exc.printStackTrace ();

}

}

}

/////////////********Display***********/////////////////

import java.awt.*;

import java.applet.*;

import java.awt.event.*;

public class Display extends Applet implements ActionListener{

Rect1 rect;

TextArea textarea;

public Display(){

rect = new Rect1();

setLayout(new BorderLayout());

add(rect,BorderLayout.CENTER);

rect.addActionListener(this);

add (textarea = new TextArea (12,60),BorderLayout.SOUTH);

}

public void actionPerformed (ActionEvent evt) {

textarea.append ("\n"+evt);

}

}

[3471 byte] By [ramya4java] at [2007-9-26 3:49:18]
# 1
Hi, the mouseEvent (in your program me) has a method or public variable (not sure but i tought it was a variable) that conains the coordinate sof the mouse click you can use that to determen if the click is inside your rect.HTHRobert
borg-javadrone at 2007-6-29 12:33:46 > top of Java-index,Desktop,Core GUI APIs...
# 2

I know i can get the x and y coordenation of the

Mouse click to compare the rect area, but I

do not want to do in that way, I want to make the

rect as component like button or label to trace

the mouse click in that rect , is it possible to

do this way , will any one help me

-Ramya

ramya4java at 2007-6-29 12:33:46 > top of Java-index,Desktop,Core GUI APIs...
# 3
hi,a question: you are initialising an object rec of class Rect1, but where is this class? I cannot find it.
swingfreak at 2007-6-29 12:33:46 > top of Java-index,Desktop,Core GUI APIs...
# 4
I am very Sorry Both Rect1 and Rect are the same-vijay
ramya4java at 2007-6-29 12:33:46 > top of Java-index,Desktop,Core GUI APIs...
# 5

Hi, the way to do what you want is the following:

Make your component(Rect1) the same size as your Rect. This can be done by eighter setting its bounds (no Layout) or overriding the getPreferredSize(), getMinimumSize() and getMaximumSize() of your component.

HTH,

Robert

borg-javadrone at 2007-6-29 12:33:46 > top of Java-index,Desktop,Core GUI APIs...