JAVA动画一直闪烁,求解,球沿着十字路口来回走,但是十字架总是闪烁
packagexiancheng;importjava.awt.BasicStroke;importjava.awt.Color;importjava.awt.Graph...
package xiancheng;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.Stroke;
import javax.swing.JFrame;
public class JoinDemo extends JFrame {
public static void main(String[] args) {
new JoinDemo();
}
private int x = 200, y = 300, r = 100;
public JoinDemo() {
this.setTitle("球车");
this.setSize(800, 600);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
new UpdateThread().start();
}
public void paint(Graphics g) {
Graphics2D g2d=(Graphics2D)g;
Stroke stroke=new BasicStroke(12.0f);//设置线宽为12.0
g2d.setStroke(stroke);
g2d.setColor(Color.green);
g.drawLine(0, 300, 800, 300);
g.drawLine(400, 0, 400, 600);
Image image = createImage(getWidth(), getHeight());
Graphics gg = image.getGraphics();
bufferedPaint(gg);
gg.dispose();
g.drawImage(image, 0, 0, null);
}
private void bufferedPaint(Graphics gg) {
gg.fillOval(x, y, r, r);
}
class UpdateThread extends Thread {
public void run() {
for (int j = 0; j <= 200; j++) {
x++;
repaint();
sleep(10);
}
for (int j = 0; j <= 300; j++) {
y--;
repaint();
sleep(10);
}
for (int j = 0; j <= 300; j++) {
y++;
repaint();
sleep(10);
}
for (int j = 0; j <= 200; j++) {
x--;
repaint();
sleep(10);
}
}
private void sleep(int millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
} 展开
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.Stroke;
import javax.swing.JFrame;
public class JoinDemo extends JFrame {
public static void main(String[] args) {
new JoinDemo();
}
private int x = 200, y = 300, r = 100;
public JoinDemo() {
this.setTitle("球车");
this.setSize(800, 600);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
new UpdateThread().start();
}
public void paint(Graphics g) {
Graphics2D g2d=(Graphics2D)g;
Stroke stroke=new BasicStroke(12.0f);//设置线宽为12.0
g2d.setStroke(stroke);
g2d.setColor(Color.green);
g.drawLine(0, 300, 800, 300);
g.drawLine(400, 0, 400, 600);
Image image = createImage(getWidth(), getHeight());
Graphics gg = image.getGraphics();
bufferedPaint(gg);
gg.dispose();
g.drawImage(image, 0, 0, null);
}
private void bufferedPaint(Graphics gg) {
gg.fillOval(x, y, r, r);
}
class UpdateThread extends Thread {
public void run() {
for (int j = 0; j <= 200; j++) {
x++;
repaint();
sleep(10);
}
for (int j = 0; j <= 300; j++) {
y--;
repaint();
sleep(10);
}
for (int j = 0; j <= 300; j++) {
y++;
repaint();
sleep(10);
}
for (int j = 0; j <= 200; j++) {
x--;
repaint();
sleep(10);
}
}
private void sleep(int millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
} 展开
1个回答
展开全部
闪烁的原因请百度 swing 双缓冲
这里我只是给你一种简单的解决办法:
public class JoinDemo extends JFrame {
public static void main(String[] args) {
new JoinDemo();
}
private int x = 200, y = 300, r = 100;
private Graphics2D g2d;
private BufferStrategy bs;
public JoinDemo() {
this.setTitle("球车");
this.setSize(800, 600);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.createBufferStrategy(2);
bs =this.getBufferStrategy();
g2d =(Graphics2D)bs.getDrawGraphics();
new UpdateThread().start();
}
public void draw() {
Image image = createImage(getWidth(), getHeight());
Graphics gg = image.getGraphics();
bufferedPaint(gg);
gg.dispose();
g2d.drawImage(image, 0, 0, null);
Stroke stroke=new BasicStroke(12.0f);
g2d.setStroke(stroke);
g2d.setColor(Color.green);
g2d.drawLine(0, 300, 800, 300);
g2d.drawLine(400, 0, 400, 600);
bs.show();
}
private void bufferedPaint(Graphics gg) {
gg.fillOval(x, y, r, r);
}
class UpdateThread extends Thread {
public void run() {
for (int j = 0; j <= 200; j++) {
x++;
draw();
sleep(10);
}
for (int j = 0; j <= 300; j++) {
y--;
draw();
sleep(10);
}
for (int j = 0; j <= 300; j++) {
y++;
draw();
sleep(10);
}
for (int j = 0; j <= 200; j++) {
x--;
draw();
sleep(10);
}
}
private void sleep(int millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
更多追问追答
追问
确实不闪了。但是有个问题是小车被绿线挡住了。
怎么让车子在绿线的上层显示。
追答
因为我调试的时候换了绘制顺序,你可以自己把它换回来
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询