java GUI painting starry sky

Source: Internet
Author: User

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

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.