🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
绘制一个坦克 Test.java ~~~ package tkdz; import javax.swing.*; import java.awt.*; public class Test extends JFrame{ MyPanel mp =null; public static void main(String[] args){ Test t1 =new Test(); } public Test(){ mp =new MyPanel();//面板 this.add(mp); this.setSize(400,300); this.setLocation(300, 280); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } } ~~~ Tank.java ~~~ package tkdz; public class Tank { int x=0,y=0; 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; } public Tank(int x, int y) { super(); this.x = x; this.y = y; } } ~~~ MyTank.java ~~~ package tkdz; public class MyTank extends Tank{ public MyTank(int x, int y) { super(x, y); // TODO Auto-generated constructor stub } } ~~~ MyPanel.java ~~~ package tkdz; import java.awt.*; import javax.swing.*; public class MyPanel extends JPanel{ MyTank mt=null; public MyPanel(){ mt=new MyTank(100,100); } public void paint(Graphics g){ super.paint(g); g.fillRect(0, 0, 400, 300);//背景? g.setColor(Color.yellow); g.fill3DRect(mt.getX(), mt.getY(), 5, 30, false);//false是虚线覆盖 g.fill3DRect(mt.getX()+15, mt.getY(), 5, 30, false); g.fill3DRect(mt.getX()+5, mt.getY()+5, 10, 20, false); g.fillOval(mt.getX()+5, mt.getY()+10, 10, 10);//实心 g.drawLine(mt.getX()+10, mt.getY()+15, mt.getX()+10, mt.getY()-3); } } ~~~ 绘制一个坦克 ![](https://box.kancloud.cn/886483d732a05d516af17e0256e7777a_397x294.png)