Name: mc57594 Date: 01/23/97
Compile and run TestApplet class. You'll see two canvases -
"Foo" and "Bar". When you press or release mouse buttons in this
canvases strings "<Canvas name> Mouse pressed" or "<name> Mouse
released" are printed to the console. If you press mouse button in some
canvas, quickly drag mouse into another and release it there, you should
see something like this:
Foo Mouse pressed
Foo Mouse released
But sometimes you can see:
Foo Mouse pressed
Bar Mouse released
I think it's wrong behaviour because all mouse input should be delivered
to the component where mouse was pressed.
--------------------- Source --------------------------------------
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class TestApplet extends Applet {
public void init () {
setLayout (null);
TestCanvas tc1 = new TestCanvas ("Foo");
TestCanvas tc2 = new TestCanvas ("Bar");
add (tc1);
add (tc2);
tc1.reshape (0, 0, 40, 20);
tc2.reshape (42, 0, 40, 20);
show ();
tc1.show ();
tc2.show ();
}
}
public class TestCanvas extends Canvas {
String m_name;
public TestCanvas (String name) {
super ();
m_name = name;
addMouseListener (
new MouseAdapter () {
public void mousePressed (MouseEvent e) {
System.out.println (m_name + " Pressed");
}
public void mouseReleased (MouseEvent e) {
System.out.println (m_name + " Released");
}
}
);
}
public void paint (Graphics g) {
g.setColor (Color.black);
g.drawRect (0, 0, size ().width - 1, size ().height - 1);
g.drawString (m_name, 5, 15);
}
}
----------------------- End of source -----------------------------
======================================================================