import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;public class Client extends Frame {
int x = 50; int y = 50; @Override
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.RED);
g.fillOval(x, y, 30, 30);
y += 5;
g.setColor(c); } public void launchFrame() {
this.setLocation(400, 300);
this.setSize(800, 600);
this.setTitle("TankWar");
this.setResizable(false);
this.setVisible(true);
this.setBackground(Color.GREEN);
this.addWindowListener(new WindowAdapter() { @Override
public void windowClosing(WindowEvent e) {
System.exit(0);
} });
new Thread(new ThreadM()).start();
} public static void main(String[] args) {
new Client().launchFrame(); } private class ThreadM implements Runnable { public void run() { while (true) {
new Client().repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
} } }}
为啥这样写不能重画出来那个圆阿?谢谢!
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;public class Client extends Frame {
int x = 50; int y = 50; @Override
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.RED);
g.fillOval(x, y, 30, 30);
y += 5;
g.setColor(c); } public void launchFrame() {
this.setLocation(400, 300);
this.setSize(800, 600);
this.setTitle("TankWar");
this.setResizable(false);
this.setVisible(true);
this.setBackground(Color.GREEN);
this.addWindowListener(new WindowAdapter() { @Override
public void windowClosing(WindowEvent e) {
System.exit(0);
} });
new Thread(new ThreadM()).start();
} public static void main(String[] args) {
new Client().launchFrame(); } private class ThreadM implements Runnable { public void run() { while (true) {
new Client().repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
} } }}
为啥这样写不能重画出来那个圆阿?谢谢!
改成repaint()
就可以了
为什么阿?
new Client().repaint(); // 这个改变的是你实例化后的对象,并不是当前对象.而 repaint(); // 在类的内部,默认是this.repaint(); 是指当前这个对象本身.