How to customize messages in Java?

Source: Internet
Author: User
Tags gety
Sender: tomhzt (snil), email area: javatutorial

Question: Re: How to customize messages in Java?

Mailing station: BBS shuimu Tsinghua station (Mon Nov 8 19:18:46 2004), within the station

Here is an example from the book:

Import java. util. eventobject;

Import java. AWT. image;

Class signatureevent extends eventobject

{

Protected image signatureimage;



Public signatureevent (Object source, Image Signature)

{

Super (source );

Signatureimage = signature;

}



Public Image getimage ()

{

Return signatureimage;

}

} Public interface signatureeventlistener

{

Public void usersigned (signatureevent signature );



}

Import java. AWT. Canvas;

Import java. AWT .*;

Import java. AWT. event .*;

Import java. AWT. image .*;

Class signingsurface extends canvas

{

Protected int width, height;

Protected int X, Y, lastx, lasty;

Protected image offscreenimage = NULL;

Protected graphics offscreeng;



Public signingsurface ()

{

Enableevents (awtevent. mouse_event_mask |

Awtevent. mouse_motion_event_mask );

}



Public void Update (Graphics g)

{

Paint (g );

}



Public void paint (Graphics g)

{

If (offscreenimage = NULL)

{

Width = getsize (). width;

Height = getsize (). height;

Offscreenimage = createimage (width, height );

If (offscreenimage = NULL)

{

System. Out. println ("error: couldn't create offscreen .");

}

Offscreeng = offscreenimage. getgraphics ();

}

G. drawimage (offscreenimage, 0, 0, this );

}



Public void processmouseevent (mouseevent)

{

X = mouseevent. getx ();

Y = mouseevent. Gety ();



Switch (mouseevent. GETID ())

{

Case mouseevent. mouse_pressed:

{

Lastx = X;

Lasty = y;

Repaint ();

Break;

}

Default:

Break;

}

}



Protected void processmousemotionevent (mouseevent mousemotionevent)

{

X = mousemotionevent. getx ();

Y = mousemotionevent. Gety ();



Switch (mousemotionevent. GETID ())

{

Case mouseevent. mouse_dragged:

{

Offscreeng. drawline (X, Y, lastx, lasty );

Lastx = X;

Lasty = y;

Repaint ();

Break;

}

Default:

Break;

}

}



Public Image getimage ()

{

Return offscreenimage;

}



Public void wipesignature ()

{

Wipeimage (offscreenimage, color. White, this );

Offscreeng. setcolor (color. Black );

Repaint ();

}



Public static void wipeimage (image im, color Col, imageobserver observer)

{

Graphics g;

Int width, height;



G = Im. getgraphics ();

Width = Im. getwidth (observer );

Height = Im. getheight (observer );

G. setcolor (COL );

G. fillrect (0, 0, width, height );

}



} // Endof class signingsurface

Import java. AWT .*;

Import java. AWT. event .*;

Import java. util .*;

Public class signme extends panel implements actionlistener

{

Protected signingsurface scribble;

Protected button erasebutton, acceptbutton;

Protected signatureeventlistener siglistener = NULL;



Public signme ()

{

Panel buttonspanel, but1panel, but2panel;

Setlayout (New borderlayout ());

Scribble = new signingsurface ();

Buttonspanel = new Panel ();

But1panel = new Panel ();

But2panel = new Panel ();



Erasebutton = new button ("Erased ");

Acceptbutton = new button ("OK ");



But1panel. Add (erasebutton );

But2panel. Add (acceptbutton );

Buttonspanel. Add (but1panel );

Buttonspanel. Add (but2panel );

Add (scribble, "center ");

Add (buttonspanel, "South ");



Erasebutton. addactionlistener (this );

Acceptbutton. addactionlistener (this );

}



Public void actionreceivmed (actionevent buttonclick)

{

If (buttonclick. getsource () = erasebutton)

{

Scribble. wipesignature ();

}

If (buttonclick. getsource () = acceptbutton)

{

System. Out. println ("User accepted signature! ");

Firesignedevent ();

}

}



Public void addsignaturelistener (signatureeventlistener listener)

Throws java. util. toomanylistenersexception

{

If (siglistener! = NULL)

{

Throw new toomanylistenersexception ("signme already has an event listener ");

}

Siglistener = listener;

}



Public void removesignaturelistener (signatureeventlistener listener)

{

If (siglistener = NULL)

{

Throw new illegalargumentexception ("can't remove signme event ");

}



If (siglistener! = Listener)

{

Throw new illegalargumentexception ("different from currently set! ");

}

Siglistener = NULL;

}



Protected void firesignedevent ()

{

Signatureevent event;



If (siglistener! = NULL)

{

Event = new signatureevent (this, scribble. getimage ());

Siglistener. usersigned (event );

}

}

} // End of class signme

Import java. AWT. frame;

Import utilities .*;

Import java. AWT .*;

Import java. AWT. event .*;

Import java. util .*;

Class signmeclient extends frame implements signatureeventlistener

{

Protected signme sigwindow;



Public static void main (string [] ARGs)

{

New signmeclient ();

}



Public signmeclient ()

{

Super ("sign me demo window ");

Sigwindow = new signme ();



Try

{

Sigwindow. addsignaturelistener (this );

}

Catch (toomanylistenersexception ex)

{

System. Out. println ("cocould not register" + ex );

}



Add (sigwindow, "center ");

Setsize (new dimension (350,250 ));

Setvisible (true );

}



Public void usersigned (signatureevent signed)

{

Frame receivedsignwindow;

Graphics g;

Image sigimage;

Int width, height;



Receivedsignwindow = new frame ("Received Signature ");

Sigimage = signed. getimage ();

Width = sigimage. getwidth (this );

Height = sigimage. getheight (this );

Receivedsignwindow. setsize (new dimension (width + 20, height + 20 ));

Receivedsignwindow. setvisible (true );

Misckit. Delay (300 );



G = receivedsignwindow. getgraphics ();

Int x = receivedsignwindow. getinsets (). Left;

Int y = receivedsignwindow. getinsets (). Top;

G. drawimage (sigimage, X, Y, receivedsignwindow );

Misckit. Delay (1000 );

Receivedsignwindow. setvisible (false );

}



}

[Mentioned in the masterpiece of liyuexp (Yannan Fei :]

: How to customize a message in Java and accept the message for processing?

--

※Source: · BBS shuimu Tsinghua station smth.org · [from: 202.112.78. *]

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.