Function: Draw the tank on the coordinate system
1 /*2 * Function: Tank Game of 1.03 * 1. Draw a tank4 * */5 PackageCom.tank;6 7 Importjavax.swing.*;8 9 Importjava.awt.*;Ten Public classDemo2extendsjframe{ One A /** - * @paramargs - */ theMypanel MP =NULL; - Public Static voidMain (string[] args) { - //TODO auto-generated Method Stub -Demo2 demo2=NewDemo2 (); + } - + //constructor Function A PublicDemo2 () at { -MP =NewMypanel (); - - This. Add (MP); - This. SetSize (400,300); - This. Setdefaultcloseoperation (jframe.exit_on_close); in This. setvisible (true); - } to + } - the //my panel * classMypanelextendsJPanel $ {Panax Notoginseng //define a tank of mine -Hero Hero =NULL; the + //constructor Function A PublicMypanel () the { +Hero =NewHero (10,10); - } $ $ //overriding the Paint function - Public voidPaint (Graphics g) - { the Super. Paint (g); -G.fillrect (0,0,400, 300);Wuyi This. Drawtank (Hero.getx (), Hero.gety (), g, 0, 1); the } - //draw the function of a tank Wu Public voidDrawtank (intXintY,graphics G,intDirectinttype) - { About //determine what type of tank (enemy or own) $ Switch(type) - { - Case0: - G.setcolor (Color.cyan); A Break; + Case1: the G.setcolor (color.yellow); - Break; $ } the the //Judging Direction the Switch(direct) the { - //Upward in Case0: the the //draw my tank (then encapsulate it into a function) About //1. Draw the left rectangle theG.fill3drect (x, Y, 5, 30,false); the //2. Draw the right rectangle theG.fill3drect (x+15, Y, 5, 30,false); + //3. Draw the middle rectangle -G.fill3drect (x+5, Y+5, 10, 20,false); the //4. Draw a circleBayiG.filloval (x+5, y+10, 10, 10); the //5. Draw the outlet theG.drawline (x+10, y+15, x+10, y); - Break; - } the } the } the the //Tank Class - classTank the { the //indicates the horizontal axis of the tank the intX=0;94 the //from the ordinate of the tank the intY=0; the Public intGetX () {98 returnx; About } - Public voidSetX (intx) {101 This. x =x;102 }103 Public intGetY () {104 returny; the }106 Public voidSety (inty) {107 This. y =y;108 }109 PublicTank (intXinty) the {111 This. x=x; the This. y=y;113 } the } the the //my tank.117 classHeroextendsTank118 {119 PublicHero (intXinty) - {121 Super(x, y);122 }123}
Java basic drawing technology. The Java drawing coordinate system of tank wars (II.)