Java event listener/event

Source: Internet
Author: User

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 ();
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.