Divided into two categories
1. Main class, is to design the main interface and part of the event monitoring Myball.java (no guide package)
Public classMyballextendsJFrame { Public Static BooleanIsdown; Public Static intNumber_star = 0;//(1) Private Static Final LongSerialversionuid = 1L; Dimension Screen=Toolkit.getdefaulttoolkit (). Getscreensize (); JMenuBar MenuBar=NewJMenuBar (); JLabel menu; Jradiobutton Rdbtnslop, Rdbtndown; JTextField Txtnumber; Buttongroup BTNGP; JLabel label; JButton Btnalert; PublicMyball () {Super("Meteor = snow in the sky"); Setdefaultcloseoperation (Jframe.exit_on_close); Init (); } Private voidinit () { This. Getcontentpane (). SetBackground (Color.Black); This. setSize (screen); Menu=NewJLabel ("Style:"); Rdbtnslop=NewJradiobutton ("Slash meteor type")); Rdbtndown=NewJradiobutton ("Drop type")); Txtnumber=NewJTextField ("Please enter the number of Stars ~ ~ ~ Return"); Label=NewJLabel ("Change the number of stars:"); Rdbtnslop.setselected (true); Btnalert=NewJButton ("Hint"); Btnalert.setbackground (Color.orange); Btnalert.settooltiptext ("Please change within the 1-201 range"); BTNGP=NewButtongroup (); This. Setjmenubar (menubar); Menubar.add (menu); Btngp.add (Rdbtnslop); Btngp.add (Rdbtndown); Menubar.add (Rdbtnslop); Menubar.add (Rdbtndown); Menubar.add (Box.createhorizontalstrut (20)); Menubar.add (label); Menubar.add (Txtnumber); Menubar.add (Btnalert); Btnalert.addactionlistener (NewActionListener () { Public voidactionperformed (ActionEvent e) {Joptionpane.showmessagedialog (NULL, "Only 1-201 of the number can be entered as the number of stars, otherwise it will be wrong", "Tips", Joptionpane.warning_message); } }); Rdbtndown.addmouselistener (NewMouseadapter () {//mouse click Drop Type Public voidmouseclicked (MouseEvent e) {Isdown=true; } }); Rdbtnslop.addmouselistener (NewMouseadapter () {//mouse click Diagonal Streamline Public voidmouseclicked (MouseEvent e) {Isdown=false; } }); Txtnumber.addkeylistener (NewKeyadapter () {//keyboard Enter to get the number of stars Public voidkeypressed (KeyEvent e) {if(E.getkeychar () = =keyevent.vk_enter) { intNumber =Integer.parseint (Txtnumber.gettext (). Trim ());//(2) if(Number > 0 && number < 202) Number_star=Number ; ElseJoptionpane.showmessagedialog (NULL, "Please enter one of the numbers in 1-201 as the number of stars."); } } }); Txtnumber.addfocuslistener (NewFocusadapter () {//Star number text box get and release focus Public voidfocusgained (focusevent e) {Txtnumber.settext (""); } Public voidFocuslost (focusevent e) {Txtnumber.settext ("Please enter the number of stars ~ ~ ~" Return); } }); } Public Static voidMain (string[] args) {Myball ball=NewMyball (); Frommypanel MP; MP=NewFrommypanel (); Ball.add (MP); Thread th=NewThread (MP); Th.start (); Ball.setvisible (true); }}
2. Want the interface to fill the stars and the moon and apply multithreading to the stars Frommypanel.java
classFrommypanelextendsPanelImplementsRunnable {Private Static Final LongSerialversionuid = 1L; Private Static intN = 101;//Number of stars//(3)Dimension screen =Toolkit.getdefaulttoolkit (). Getscreensize (); Random Rand=NewRandom (); intX[] =New int[201];//(4) intY[] =New int[201];//(5) intR, GG, B;//The colors of the stars are red, green, and blue. PublicFrommypanel () { for(inti = 0; I < This. GETN (); i++) {//(6)X[i] = Rand.nextint ((int) Screen.getwidth ()); Y[i]= Rand.nextint ((int) Screen.getheight ()); } } Public voidPaint (Graphics g) {G.setcolor (color.white);//MoonG.filloval (900, 50, 100, 100); G.setcolor (Color.Black); G.filloval (880, 30, 100, 100); G.setfont (NewFont ("", 0, 35)); R= Rand.nextint (255); GG= Rand.nextint (255); b= Rand.nextint (255); G.setcolor (NewColor (R, GG, b));//the color of the stars//G.setcolor (color.white); Try { for(inti = 0; I < This. GETN (); i++) {//(7)g.DrawString ("*", X[i], y[i]); } } Catch(Exception e) {Joptionpane.showmessagedialog (NULL, E.getmessage ()); } } Public voidrun () { while(true) { for(inti = 0; I < This. GETN (); i++) {//(8)//slide diagonally, if you want to see the downward direction of the slide, please comment out the following code if(!myball.isdown) {if(i% 20 = = 0) {//only the stars in multiples of 20 in the following table are moving, i.e. 0,20,40,60,80x[i]++;//bottom Right Slidey[i]++; if(Y[i] > (int) Screen.getheight ()) {Y[i]= 0; } if(X[i] > (int) Screen.getwidth ()) {X[i]= 0; } } } //slide vertically, if you want to see the diagonal slide, please comment out the code above if(myball.isdown) {if(i% 3 = = 0) Y[i]+ = 10; Y[i]+ = 10;//slipped if(Y[i] > (int) Screen.getheight ()) {Y[i]= 40; } if(X[i] > (int) Screen.getwidth ()) {X[i]= 10; } } } Try{Thread.Sleep (30); Repaint (); } Catch(interruptedexception e) {Joptionpane.showmessagedialog (NULL, E.getmessage ()); } } } Public intGetn () {//get the number of stars entered, but one thing, the number of inputs greater than 201 will go wrong, the Association code is (1)-(8) returnMyball.number_star! = 0?myball.number_star:n; }}
Stars that can drop and slide