PackageDay2;ImportJava.awt.Color;ImportJava.awt.Font;ImportJava.awt.Graphics;ImportJava.awt.Panel;ImportJava.awt.event.MouseAdapter;ImportJava.util.Random;Importjava.awt.event.MouseEvent;ImportJavax.swing.JFrame;ImportJavax.swing.JPanel;//Inheritance Public classDay2_cextendsjpanel{/** * */ Private Static Final LongSerialversionuid = 1L; Public int[] Rednumber = {0,0,0,0,0,0,0}; Public int[] Checknum =New int[7]; Random Mrrandom=NewRandom (); BooleanIschange =false; BooleanState =false; Public intCounterrun =-1; Public Static voidMain (String args[]) {JFrame MyFrame=NewJFrame ("NEF"); Myframe.setsize (1200, 650); Myframe.setdefaultcloseoperation (2); Myframe.setlocationrelativeto (NULL); Myframe.setresizable (false); Day2_c Mypanel=NewDay2_c (); Myframe.add (Mypanel); Myframe.setvisible (true); Mypanel.changen (); } Public voidPaint (Graphics myg) {//Draw the Click of the mouthMyg.setcolor (Color.orange); Myg.fillrect (550, 50, 100, 50); Myg.setfont (NewFont ("italics", 1,25)); Myg.setcolor (Color.Black); Myg.drawstring ("Click to start", 547, 80); //draw a red ball intX_axis = 100; intY_axis = 250; Myg.setcolor (color.red); for(inti = 0; I < 6; i++) {myg.filloval (X_axis, Y_axis,100, 100); X_axis+ = 150; } myg.setcolor (Color.Blue); Myg.filloval (1000, 250, 100, 100); //whether the change if(ischange) {//Draw Numbers intT_AXISX = 125; intT_axisy = 320; intNud = 0; for(inti = Counterrun; I < 6; i++) {Nud= Mrrandom.nextint (33) + 1; for(inth = 0; h < i; h++) { if(Nud = =Rednumber[i]) {i--; Break; }} Rednumber[i]=Nud; } rednumber[6] = Mrrandom.nextint (16) + 1; Myg.setfont (NewFont ("", 1, 45)); Myg.setcolor (Color.green); for(inti = 0;i < 7;i++) { if(Rednumber[i]% 10 = = 0) {myg.drawstring (" " +Rednumber[i],t_axisx, T_axisy); } Else{myg.drawstring ("" +Rednumber[i],t_axisx, T_axisy); } T_AXISX+ = 150; } //myg.drawstring ("+rednumber[6", T_axisx, T_axisy); } } Public voidChangen () {mouseadapter MA=NewMouseadapter () { Public voidmouseclicked (MouseEvent e) {intx =E.getx (); inty =e.gety (); if(X > 550 & X < 650 & y > &, y < 100) {Ischange=true; //repaint (); } } }; Addmouselistener (MA); //the mouse time monitoring is actually separate from the main function main thread.//Listening is a simple event, unable to perform a loop for(inti = 0; i < 140; i++) { Try{Thread.Sleep (50); } Catch(Exception e3) {e3.printstacktrace (); } if(i% 20 = = 0) {Counterrun++; } repaint (); } Counterrun= 0; }}
The program has a bug, has not found a good solution, interested can leave a message, I send the teacher's code, the implementation of the process is not the same
There would have been a sort of small function, but it was too simple to write,,, to fight bugs, just a bubble sort.
Someone is willing to help me to solve the bug of this program not ...
Java day2 A code that simulates color ball