1 packagecom.law.frame;2 3 Importjava.awt.Color;4 Importjava.awt.Font;5 Importjava.awt.Frame;6 Importjava.awt.Graphics;7 Importjava.awt.Image;8 Importjava.awt.event.WindowAdapter;9 Importjava.awt.event.WindowEvent;Ten Importjava.net.URL; one a public classMywindowextendsframe{ - - /** the * - */ - Private Static Final LongSerialversionuid = 2490169038285678148L; - + Private intx; - Private inty; + Private Booleanflag; a at - publicMywindow () { - - } - publicMywindow (String Title) { - Super(title); in } - to public voidOpenmywindow () { +SetBackground (NewColor (0, 0, 0)); -SetSize (500, 500); theSetLocation (250, 250); *SetVisible (true); $ this. Addwindowlistener (Newwindowadapter () {Panax Notoginseng public voidwindowclosing (windowevent E) { -System.exit (0); the } + }); a Newrepaintthread (). start (); the } + - $ $ @Override - public voidpaint (Graphics G) { - G.setcolor (color.white); theFont font =NewFont ("candara", font.bold, 30); - G.setfont (font);Wuyig.DrawString ("Java Concurrency", 130, 150); theURL U = this. getclass (). getclassloader (). getresource ("images/plane_32px_1194817_easyicon.net.png"); -Image img =common.loadpic (u); wuG.drawimage (img, x, 350,NULL); - } about $ classRepaintthreadextendsthread{ - - @Override - public voidRun () { a while(true){ + Changepositionx (); the Repaint (); - Try { $Thread.Sleep (60); the}Catch(interruptedexception E) { the e.printstacktrace (); the } the } - } in the } the public voidChangepositionx () { about if(x>=500-32){ theFlag =false; the } the if(x<=0){ +Flag =true; - } the if(flag) {BayiX + = 5; the}Else{ thex-= 5; - } - } the public voidchangepositiony () { the if(y>=500-32){ theFlag =false; the } - if(y<=0){ theFlag =true; the } the if(flag) {94Y + = 5; the}Else{ they-= 5; the }98 } about public Static voidmain (string[] Args) { -Mywindow window =NewMywindow ("open open");101 Window.openmywindow ();102 }103 104 public intGetX () { the returnx;106 }107 public voidSetX (intX) {108 this. x =x;109 } the public intGetY () {111 returny; the }113 public voidSety (intY) { the this. y =y; the } the public BooleanIsflag () {117 returnflag;118 }119 public voidSetflag (BooleanFlag) { - this. Flag =flag;121 }122 123}
Tool classes for loading pictures:
1 packagecom.law.frame;2 3 Importjava.awt.Image;4 Importjava.io.IOException;5 Importjava.net.URL;6 7 Importjavax.imageio.ImageIO;8 9 Ten public classCommon { one a - public StaticImage loadpic (URL u) { -Image img =NULL; the Try { -IMG =Imageio.read (u); -}Catch(ioexception E) { - e.printstacktrace (); + } - returnimg; + } a}
AWT Frame-java window