ImportJava.awt.Color;ImportJava.awt.Graphics;ImportJava.awt.Image;ImportJavax.swing.ImageIcon;ImportJavax.swing.JFrame;ImportJavax.swing.JPanel; Public classTeststar {/** * @paramargs * Starry Star chart*/ Public Static voidMain (string[] args) {//TODO auto-generated Method StubJFrame jf=NewJFrame (); Jf.setsize (800, 800); Jf.setbackground (Color.Black); //Jf.add (New Teststar ());jf.setdefaultcloseoperation (jframe.exit_on_close); Jf.setvisible (true); Jf.add (NewShapespanel ()); Jf.settitle ("Starry Star"); /*Public void Paint (Graphics g) {Graphicsdemod mygrapics=new graphicsdemod (); Mygrapics.paintcomponent (g); }*/}}classShapespanelextendsjpanel{ Public voidpaintcomponent (Graphics g) {intPlacex; intPlacey; intSize//Store font size, the star is directly output *, with font control size//Draw the Stars for(inti=0;i<300;i++){ //Math is a math class, random () is a method in the math class, and the random itself produces only decimals (0~1),//random () *10 means to produce a decimal between 0~10,//int (random () *10) means to force rounding, to remove the fractional part from only the integer portion, so it becomes an integer between the 0~9. Placex= (int) (800*math.random ()); Placey=(int) (800*math.random ()); Size=(int) (20*math.random ()); G.setcolor (Color.White); g.DrawString ("*", Placex, Placey);//draw a specific symbol at x, y position * } //Painting the MoonPlacex= (int) (750*math.random ()); G.filloval (Placex,100, 100, 100);//the placex,100 is the x, Y value of the center coordinate (exactly the central symmetry point of the Ellipse), and the last two are the lengths of the two half-length axes. //Filloval is an ellipse, and the back two equals, of course, round.G.setcolor (Color.Black); G.filloval (Placex-20, 80, 100, 100); //Draw the groundG.setcolor (Color.White); G.filloval (-400, 710, 1000, 100); Image Mimage=NewImageIcon ("./images/gril.png"). GetImage (); G.drawimage (Mimage,80, 500, 300, 300,NULL); }}
The paint method must be in the JPanel
Java GUI picture stars