在java中应用paint和repaint方法的一段小程序如下! 问:为什么运行起来红色圆重画了之后之前的圆还显示着
importjava.awt.*;importjavax.swing.*;publicclassTankWarClinetextendsJFrame{publicstat...
import java.awt.*;
import javax.swing.*;
public class TankWarClinet extends JFrame {
public static final int GAME_WIDTH = 800;
public static final int GAME_HEIGTH = 600;
int x = 50,y = 50;
Image offScreenImage = null;
public void updata(Graphics g) {
if(offScreenImage == null) {
offScreenImage = this.createImage(GAME_WIDTH,GAME_HEIGTH);
Graphics offGraphics = offScreenImage.getGraphics();
paint(offGraphics);
g.drawImage(offScreenImage,GAME_WIDTH,GAME_HEIGTH,null);
}
}
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.red);
g.fillOval(x,y,30,30);
g.setColor(c);
y+=50;
}
public void LaunchFrame() {
this.setBounds(100,50,GAME_WIDTH,GAME_HEIGTH);
this.setResizable(false);
this.setBackground(Color.green);
setVisible(true);
new Thread(new PaintThread()).start();
}
public static void main(String[] args) {
TankWarClinet tWc = new TankWarClinet();
tWc.LaunchFrame();
tWc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private class PaintThread implements Runnable {
public void run() {
while(true) {
repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
} 展开
import javax.swing.*;
public class TankWarClinet extends JFrame {
public static final int GAME_WIDTH = 800;
public static final int GAME_HEIGTH = 600;
int x = 50,y = 50;
Image offScreenImage = null;
public void updata(Graphics g) {
if(offScreenImage == null) {
offScreenImage = this.createImage(GAME_WIDTH,GAME_HEIGTH);
Graphics offGraphics = offScreenImage.getGraphics();
paint(offGraphics);
g.drawImage(offScreenImage,GAME_WIDTH,GAME_HEIGTH,null);
}
}
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.red);
g.fillOval(x,y,30,30);
g.setColor(c);
y+=50;
}
public void LaunchFrame() {
this.setBounds(100,50,GAME_WIDTH,GAME_HEIGTH);
this.setResizable(false);
this.setBackground(Color.green);
setVisible(true);
new Thread(new PaintThread()).start();
}
public static void main(String[] args) {
TankWarClinet tWc = new TankWarClinet();
tWc.LaunchFrame();
tWc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private class PaintThread implements Runnable {
public void run() {
while(true) {
repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
} 展开
1个回答
展开全部
import java.awt.*;
import javax.swing.*;
public class TankWarClinet extends JFrame {
public static final int GAME_WIDTH = 800;
public static final int GAME_HEIGTH = 600;
int x = 50, y = 50;
Image offScreenImage = null;
public void updata(Graphics g) {
if (offScreenImage == null) {
offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGTH);
Graphics offGraphics = offScreenImage.getGraphics();
paint(offGraphics);
g.drawImage(offScreenImage, GAME_WIDTH, GAME_HEIGTH, null);
}
}
@Override
public void paint(Graphics g) {
super.paint(g);
Color c = g.getColor();
g.setColor(Color.red);
g.fillOval(x, y, 30, 30);
g.setColor(c);
y += 50;
}
public void LaunchFrame() {
this.setBounds(100, 50, GAME_WIDTH, GAME_HEIGTH);
this.setResizable(false);
this.setBackground(Color.green);
setVisible(true);
new Thread(new PaintThread()).start();
}
public static void main(String[] args) {
TankWarClinet tWc = new TankWarClinet();
tWc.LaunchFrame();
tWc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private class PaintThread implements Runnable {
@Override
public void run() {
while (true) {
repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
}
}
加一句super.paint(g);就可以了
import javax.swing.*;
public class TankWarClinet extends JFrame {
public static final int GAME_WIDTH = 800;
public static final int GAME_HEIGTH = 600;
int x = 50, y = 50;
Image offScreenImage = null;
public void updata(Graphics g) {
if (offScreenImage == null) {
offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGTH);
Graphics offGraphics = offScreenImage.getGraphics();
paint(offGraphics);
g.drawImage(offScreenImage, GAME_WIDTH, GAME_HEIGTH, null);
}
}
@Override
public void paint(Graphics g) {
super.paint(g);
Color c = g.getColor();
g.setColor(Color.red);
g.fillOval(x, y, 30, 30);
g.setColor(c);
y += 50;
}
public void LaunchFrame() {
this.setBounds(100, 50, GAME_WIDTH, GAME_HEIGTH);
this.setResizable(false);
this.setBackground(Color.green);
setVisible(true);
new Thread(new PaintThread()).start();
}
public static void main(String[] args) {
TankWarClinet tWc = new TankWarClinet();
tWc.LaunchFrame();
tWc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private class PaintThread implements Runnable {
@Override
public void run() {
while (true) {
repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
}
}
加一句super.paint(g);就可以了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询