Java draws images on the screen (run in a single example)

Source: Internet
Author: User

Package COM. CZP; import Java. AWT. color; import Java. AWT. dimension; import Java. AWT. font; import Java. AWT. graphics; import Java. AWT. toolkit; import Java. AWT. event. mouseadapter; import Java. AWT. event. mouseevent; import Java. io. ioexception; import java.net. inet4address; import java.net. serversocket; import javax. swing. joptionpane; import javax. swing. jwindow;/***** draw IP on screen *** @ author caozhongping ***/public class mywindow extends jwindow {Private Static final int font_size = 26; private final string font_name = ""; Private Static final long serialversionuid = 1l; private serversocket sock; Private Static Font font; private int width; Public mywindow (INT width, int height) {super (); font = new font (font_name, Font. plain, font_size); this. width = width; this. setsize (width, height); this. setpostion (); this. setalwaysontop (true); this. setvisible (true); this. bindport (); this. addmouselistener (New mywinlister (this);}/*** run a single instance */private void bindport () {New thread (New runnable () {@ overridepublic void run () {try {sock = new serversocket (6666); sock. accept ();} catch (ioexception e) {joptionpane. showmessagedialog (null, "getip aready running"); system. exit (0 );}}}). start ();} public serversocket getsock () {return sock;} @ overridepublic void paint (Graphics g) {G. setcolor (color. blue); G. setfont (font); G. drawstring ("Your IP is:" + getlocalip () + "", 0, this. gety () + font_size);} private string getlocalip () {try {return inet4address. getlocalhost (). gethostaddress ();} catch (exception e) {Return e. tostring () ;}/ *** center to screen */private void setpostion () {dimension D = toolkit. getdefatooltoolkit (). getscreensize (); this. setlocation (INT) (D. getwidth ()-This. width), 2);}/***** Double Click Quit **/class mywinlister extends mouseadapter {private mywindow window; Public mywinlister (mywindow window) {super (); this. window = Window ;}@ overridepublic void mouseclicked (mouseevent e) {super. mouseclicked (E); If (E. getclickcount () = 2) {try {This. window. getsock (). close (); this. window. dispose ();} catch (ioexception E1) {e1.printstacktrace ();} system. exit (0) ;}} public static void main (string [] ARGs) {New mywindow (320, 30 );}}

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.