Events: mouseevent/keyevent.
Addmouselistener is required for a frame (the listener event you defined)
For example:
Class Monitor extends mouseadapter...
In the corresponding frame class,
The function of defining paint... painting is to draw automatically. When the window is overwritten, and so on... when it appears again, it will be re-drawn.
Paint will always be called, or even click the mouse.
You can regard graphics as a paint brush, where the paint brush is your current object of this ..
The role of repaint, forced window re-painting. First Call repaint-> Update [implement dual buffering]-> paint ..
Event source Windows/frame/panel ect ...,
Listener listener,
Penalty event * event.
Paint Brush: Graphics
Import java. AWT .*;
Import java. AWT. event .*;
Import java. util. collection .*;
Import java. util .*;
Public class test {
Public static void main (string ARGs []) {
New paintframe ("My paintboader ");
}
}
Class paintframe extends frame {
Arraylist points = NULL;
Paintframe (string s ){
Super (s );
Points = new arraylist ();
Setlayout (null );
Setbounds (300,300,400,300 );
This. setbackground (new color (204,204,255 ));
Setvisible (true );
This. addmouselistener (New Monitor ());
}
Public void paint (Graphics g ){
Iterator I = points. iterator ();
While (I. hasnext ()){
Point P = (point) I. Next ();
G. setcolor (color. Blue );
G. filloval (P. X, p. Y, 10, 10 );
}
}
Public void addpoint (point P ){
Points. Add (P );
}
}
Class Monitor extends mouseadapter {
Public void mousepressed (mouseevent me ){
Paintframe pframe = (paintframe) me. getsource ();
Pframe. addpoint (new point (Me. getx (), me. Gety ()));
Pframe. repaint ();
}
}