Java-graphic programming

Source: Internet
Author: User

I. Create a framework

frame, No window with other windows

Swing based on awt, the first step in creating a framework requires the import of swing and AWT packages

Importjava.awt.*;Importjavax.swing.*; Swing in the Java expansion pack/*** File Name: Simpleframetest.java
* Function: understand the framework to build the deployment
Steps
* 1. Define subclass simpleframe, The size of the constructor is set to 300 * 200 pixels
* 2. Create a Simpleframe object in the main method in Simpleframetest frame
* 3. Defines the response action when the user closes the frame, which is hidden by default, not closed
* 4. Show frame, default not displayed
*/ public classsimpleframetest{ public Static voidmain (string[] Args) {eventqueue.invokelater (NewRunnable () { public voidRun () {simpleframe frame=NewSimpleframe (); Frame.setdefaultcloseoperation (jframe.exit_on_close); Frame.setvisible (true); } }); }}classSimpleframeextendsjframe{Private Static Final intDefault_width = 300; Private Static Final intDefault_height = 200; publicsimpleframe () {setSize (default_width, default_height); }}

second, Frame Size
Importjava.awt.*;Importjavax.swing.*;/*** File Name: Sizedframetest.java* Features: Added frame properties, add title
*
*
*/ public classsizedframetest{ public Static voidmain (string[] Args) {eventqueue.invokelater (NewRunnable () { public voidRun () {JFrame frame=NewSizedframe (); Frame.settitle ("sizedframe"); Set frame title name Frame.setdefaultcloseoperation (jframe.exit_on_close); Frame.setvisible (true); } }); }}classSizedframeextendsjframe{ publicSizedframe () {
//Get frame size
Steps:
1. Call the static method of the Toolkit class Getdefaulttookit get a Tookit object
2. This method returns the screen size in dimension mode
3. Dimension saving screen width and height with public variable width and hight
Toolkit Kit=Toolkit.getdefaulttoolkit (); Dimension screensize=kit.getscreensize (); intScreenHeight =screensize.height; intScreenWidth =screensize.width; //Set the screen to half with the size set in frame properties
Let the system position the framesetSize (screenwidth/2, SCREENHEIGHT/2); Setlocationbyplatform (true); //Set frame icon      Image img=NewImageIcon ("icon.gif"). getImage (); Seticonimage (img); }}

Iii. displaying information in Components
Importjavax.swing.*;Importjava.awt.*;/*** File name: Nothelloworld * function: Display information in component * Step: * 1. */ public classnothelloworld{ public Static voidmain (string[] Args) {eventqueue.invokelater (NewRunnable () { public voidRun () {JFrame frame=NewNothelloworldframe (); Frame.settitle ("nothelloworld");               Frame.setdefaultcloseoperation (jframe.exit_on_close); Frame.setvisible (true);   }         }); }}/*** Create a new panel in the constructor and add it to the frame*/classNothelloworldframeextendsjframe{ publicnothelloworldframe () {add (Newnothelloworldcomponent ());   Pack (); }}/*** Graphics brushes inherit from Jcompoent and can also inherit from JPanel*/classNothelloworldcomponentextendsjcomponent{ public Static Final intmessage_x = 75;  public Static Final intmessage_y = 100; Private Static Final intDefault_width = 300; Private Static Final intDefault_height = 200;  public voidpaintcomponent (Graphics g) {g.drawstring ("not a Hello, the world program", message_x, message_y); }       publicDimension getpreferredsize () {return NewDimension (default_width, default_height); }}    

Iv. painting a tank with a paintbrush
Importjavax.swing.*;Importjava.awt.*;/*** Function: draw out tank * Step * 1. Create tank class Tank * 2. Create my tank class hero inherit Tank * 3. Create my panel mypanel inherit JPanel * 4. In the TankGame1 class, add Add Mypanel **/ public classTankGame1extendsjframe{//Create a Panel object MPMypanel MP =NULL;  public Static voidmain (string[] Args) {TankGame1 mtg=NewTankGame1 (); }        //constructor Function     publicTankGame1 () {mp=NewMypanel ();  this. Add (mp);//add into the panel         this. setSize (400, 300);//size             this. Setdefaultcloseoperation (jframe.exit_on_close);  this. setvisible (true);//Show    }    }//my panelclassMypanelextendsjpanel{//define a tank of mineHero Hero =NULL; //constructor Function     publicMypanel () {hero=NewHero (100,100); }        //Re-Paint     public voidpaint (Graphics G) {Super. Paint (g); //Set the Active AreaG.fillrect (0, 0, 400, 300); //Call function Drawtank draw out my tank.         this. Drawtank (hero.getx (), hero.gety (), g, 0, 1); }        //draw the function of a tank     public voidDrawtank (intXinty, Graphics g,intDirectintType) {                //judging what type of tank        Switch(type) { case0: G.setcolor (color.cyan);  break;  case1: G.setcolor (color.yellow); }                //Judging Direction        Switch(direct) {//Upward         case0:            //draw out my tank.//1. Draw the left rectangleG.fill3drect (x, y, 5, 30,false); //2. Reveal the right rectangleG.fill3drect (x+15, y, 5, 30,false); //3. Draw the middle rectangleG.fill3drect (x+5, y+5, 10, 20,false); //4. Draw a circleG.filloval (x+5, y+10, 10, 10); //5. Draw the outletG.drawline (x+10, y+15, x+10, y); }    }}//Tank Classclasstank{intx = 0;//indicates the horizontal axis of the tank    inty = 0;//indicates the ordinate of the tankTank (intXintY) { this. x =x;  this. y =y; }         public intGetX () {returnx; }     public voidSetX (intX) { this. x =x; }     public intGetY () {returny; }     public voidSety (intY) { this. y =y; }    }//my Tank.classHeroextendstank{ publicHero (intXintY) {Super(x, y); }}

Java-graphic programming

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.