private int x = 100; public TestPanel() { new Thread(){ public void run() { try { //循环改变x的值 while (true) { Thread.sleep(100); x++; repaint(); } } catch (InterruptedException e) { e.printStackTrace(); }
} }.start(); }
//重写此方法,第一次生成该JPanel以及每次调用repaint()的时候会调用此方法 public void paintComponent(Graphics g) { super.paintComponent(g); Stroke stroke = new BasicStroke(2.0f); Graphics2D g2 = (Graphics2D) g; g2.setStroke(stroke);
// 绘制(x,100)到(x,200)的线段 g2.drawLine(x, 100, x,200); } }public class Test extends JFrame { public static void main(String[] args) { JFrame main = new Test();
下面有视频下载地址:
http://www.verycd.com/topics/93279/
repaint ---> update ---->paint
我重写了paint()方法就可以了
package test;import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;import javax.swing.JFrame;
import javax.swing.JPanel;class TestPanel extends JPanel{
private int x = 100; public TestPanel() {
new Thread(){
public void run() {
try {
//循环改变x的值
while (true) {
Thread.sleep(100);
x++;
repaint();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
}
//重写此方法,第一次生成该JPanel以及每次调用repaint()的时候会调用此方法
public void paintComponent(Graphics g) {
super.paintComponent(g); Stroke stroke = new BasicStroke(2.0f);
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(stroke);
// 绘制(x,100)到(x,200)的线段
g2.drawLine(x, 100, x,200);
}
}public class Test extends JFrame {
public static void main(String[] args) {
JFrame main = new Test();
JPanel panel = new TestPanel();
main.setContentPane(panel);
main.setSize(400,300);
main.setVisible(true);
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Color oldC = g.getColor();
if (this.isGood()) {
blood.draw(g);
g.setColor(Color.BLUE);
} else {
g.setColor(Color.GRAY);
}
g.fillOval(x, y, TANK_WIDTH, TANK_HEIGHT);
g.setColor(oldC);
// 画炮筒
switch (gunDir) {
case U:
g.drawLine(x + TANK_WIDTH / 2, y + TANK_HEIGHT / 2, x + TANK_WIDTH
/ 2, y);
break;
case LU:
g.drawLine(x + TANK_WIDTH / 2, y + TANK_HEIGHT / 2, x, y);
break;
case RU:
g.drawLine(x + TANK_WIDTH / 2, y + TANK_HEIGHT / 2, x + TANK_WIDTH,
y);
break;
case D:
g.drawLine(x + TANK_WIDTH / 2, y + TANK_HEIGHT / 2, x + TANK_WIDTH
/ 2, y + TANK_HEIGHT);
break;
case LD:
g.drawLine(x + TANK_WIDTH / 2, y + TANK_HEIGHT / 2, x, y
+ TANK_HEIGHT);
break;
case RD:
g.drawLine(x + TANK_WIDTH / 2, y + TANK_HEIGHT / 2, x + TANK_WIDTH,
y + TANK_HEIGHT);
break;
case L:
g.drawLine(x + TANK_WIDTH / 2, y + TANK_HEIGHT / 2, x, y
+ TANK_HEIGHT / 2);
break;
case R:
g.drawLine(x + TANK_WIDTH / 2, y + TANK_HEIGHT / 2, x + TANK_WIDTH,
y + TANK_HEIGHT / 2);
break;
case STOP:
g.drawLine(x + TANK_WIDTH / 2, y + TANK_HEIGHT / 2, x + TANK_WIDTH
/ 2, y + TANK_HEIGHT);
break;
default:
break;
}
for (int i = 0; i < missiles.size(); i++) {
Missile m = missiles.get(i);
if (m != null && m.getLive()) {
m.draw(g);
} else {
missiles.remove(i);
} for (int j = 0; j < explodes.size(); j++) {
Explode e = explodes.get(j);
if (e != null && e.isLive()) {
e.draw(g);
} else {
explodes.remove(j);
}
}
}
move();
}