JAVA坦克大战为什么画不出来坦克了 代码如下 目的是画出一个hero坦克 不知道为什么只有一个JFrame 出来
importjavax.swing.*;importjava.awt.*;publicclassmyTankGame1extendsJFrame{myPanelmp=nu...
import javax.swing.*;import java.awt.*;public class myTankGame1 extends JFrame{ myPanel mp=null; public static void main(String[] args) { myTankGame1 mtg=new myTankGame1(); } public myTankGame1(){ mp=new myPanel(); this.add(mp); this.setSize(400,300); this.setVisible(true); this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); }
}class Tank{ int x=0; int y=0; public Tank(int x,int y){ this.x=x; this.y=y; } public int getX() { return x; }
public void setX(int x) { this.x = x; }
public int getY() { return y; }
public void setY(int y) { this.y = y; }
}class myPanel extends JPanel{ Hero hero=null; public myPanel(){ hero=new Hero(10,10); } public void Paint(Graphics g){ super.paint(g); g.fillRect(0, 0, 400, 300); this.drawTank(hero.getX(),hero.getY(),g,0,0); } public void drawTank(int x,int y,Graphics g,int direct,int type){ switch(type) { case 0: g.setColor(Color.CYAN); break; case 1: g.setColor(Color.YELLOW); break; } switch (direct) { case 0: g.fill3DRect(x, y, 5, 30,false); g.fillRect(x+15, y, 5, 30); g.fillRect(x+5, y+5, 10,20); g.fillOval(x+5, y+10, 10, 10); g.drawLine(x+10,y+15,x+10,y); } }}class Hero extends Tank{ public Hero(int x,int y) { super(x,y); }} 展开
}class Tank{ int x=0; int y=0; public Tank(int x,int y){ this.x=x; this.y=y; } public int getX() { return x; }
public void setX(int x) { this.x = x; }
public int getY() { return y; }
public void setY(int y) { this.y = y; }
}class myPanel extends JPanel{ Hero hero=null; public myPanel(){ hero=new Hero(10,10); } public void Paint(Graphics g){ super.paint(g); g.fillRect(0, 0, 400, 300); this.drawTank(hero.getX(),hero.getY(),g,0,0); } public void drawTank(int x,int y,Graphics g,int direct,int type){ switch(type) { case 0: g.setColor(Color.CYAN); break; case 1: g.setColor(Color.YELLOW); break; } switch (direct) { case 0: g.fill3DRect(x, y, 5, 30,false); g.fillRect(x+15, y, 5, 30); g.fillRect(x+5, y+5, 10,20); g.fillOval(x+5, y+10, 10, 10); g.drawLine(x+10,y+15,x+10,y); } }}class Hero extends Tank{ public Hero(int x,int y) { super(x,y); }} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询