?? tankwar.java
字號:
import javax.swing.*;
import java.awt.*;
public class TankWar extends JFrame
{
Tank t=new Tank();
public void showFrame()
{
this.setTitle("坦克大戰");
this.setLocation(100,100);
this.setSize(800,600);
this.setBackground(Color.green);
this.setResizable(false);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
new Thread(new RepaintThread()).start();
}
public void paint(Graphics g)
{
try
{
t.drawTank(g);
}
catch(Exception e)
{
e.printStackTrace();
}
}
//用于實現重畫線程
private class RepaintThread implements Runnable
{
public void run()
{
while(true)
{
repaint();//這個地方的重畫不知道為什么沒有把從前繪過的圖給擦掉
// System.out.println("=====");
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
public static void main(String[]args)
{
TankWar tk=new TankWar();
Container c=tk.getContentPane();
c.setBackground(Color.green);
tk.showFrame();
}
}
class Tank
{
private final int WIDTH=30,HEIGHT=20;
int intX,intY;
int x,y;
Tank()
{
}
Tank(int x,int y)
{
intX=x;
intY=y;
}
public void drawTank(Graphics g)
{
try
{
Color c=g.getColor();
g.setColor(Color.RED);
g.fillRect(x+40,y+40,WIDTH,HEIGHT);
g.setColor(c);
x+=50;
y+=5;
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -