在画布上闪烁的白色 [英] Flickering white on canvas
问题描述
我发现一个解决方案分钟后,张贴,但由于低声誉我不能删除的帖子
它我决定开始工作,可能会变成一个游戏在某一点。
我想画一些圆圈,目前在指定的方向移动它们。这导致闪烁。这很可能是我监督一些非常基本的东西,但我不知道为什么它不顺利。
我的板类看起来像(删除我认为不必要):
public class Board extends Canvas implements Runnable {
public static void main ] args){
Board board = new Board();
board.setPreferredSize(new Dimension(WIDTH * SCALE,HEIGHT * SCALE));
JFrame frame = new JFrame(Circles);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(board);
frame.pack();
frame.setVisible(true);
board.start();
}
@Override
public void run(){
while(running){
process();
repaint();
try {
Thread.sleep(15);
} catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
paint方法:
public void paint(Graphics g1){
super.paint(g1);
Graphics2D g =(Graphics2D)g1;
for(Ball ball:Ball.BALLS){
g.drawOval((int)ball.getLocation()。getX(),(int)ball.getLocation()。getY() addRadius(),ball.getRadius());
}
}
我的处理方法:
private void process(){
if(utilities.randInt(1,100)< 10&&Ball.getBallCount ; 40){
Ball.spawnNew(this);
}
(球球:Ball.BALLS){
ball.move(this);
}
Ball.BALLS.removeAll(Ball.TO_REMOVE);
Ball.TO_REMOVE.clear();
}
move方法基本上将球的x值增加给定值每次它的调用(移动它正确)。
像我说的,我不确定为什么它闪烁,如果你有任何指针,请告诉。
谢谢!
为此,您需要创建一个 BufferedImage
并从中获取 Graphics
。
示例:
将所有图像绘制到图像,将图像渲染到屏幕上,然后最终填充图像的背景颜色或图像以重置图像。 > //一次实例化
BufferedImage b = new BufferedImage(width,height,mode);
在 paint(Graphics g)
:
Graphics buffer = b.getGraphics();
//将所有的东西渲染到缓冲区
Graphics2D g =(Graphics2D)buffer;
for(Ball ball:Ball.BALLS){
g.drawOval((int)ball.getLocation()。getX(),(int)ball.getLocation()。getY() getRadius(),ball.getRadius());
}
g1.drawImage(b,0,0,width,height,null);
g.setColor(Color.BLACK);
//复位图像
g.drawRect(0,0,width,height);
g.dispose();
I figured out a solution minutes after posting, but due to low reputation I couldn't delete the post
For the fun of it I decided to start working on something which might turn into a game at some point.
I'm trying to draw some circles and move them in a given direction currently. This causes flickering. It's very likely that I oversee something very basic but I can't figure out why it doesn't render smoothly.
My board class looks something like (removed what I deemed unnecessary):
public class Board extends Canvas implements Runnable {
public static void main(String[] args) {
Board board = new Board();
board.setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
JFrame frame = new JFrame("Circles");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(board);
frame.pack();
frame.setVisible(true);
board.start();
}
@Override
public void run() {
while (running) {
process();
repaint();
try {
Thread.sleep(15);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
The paint method:
public void paint(Graphics g1) {
super.paint(g1);
Graphics2D g = (Graphics2D) g1;
for (Ball ball : Ball.BALLS) {
g.drawOval((int) ball.getLocation().getX(), (int) ball.getLocation().getY(), ball.getRadius(), ball.getRadius());
}
}
My process method:
private void process() {
if (utilities.randInt(1, 100) < 10 && Ball.getBallCount() < 40) {
Ball.spawnNew(this);
}
for (Ball ball : Ball.BALLS) {
ball.move(this);
}
Ball.BALLS.removeAll(Ball.TO_REMOVE);
Ball.TO_REMOVE.clear();
}
The move method basically increments the x-value of the ball by a given value each time its called (moving it right).
Like I said, I'm unsure why it flickers so if you have any pointers please do tell.
Thanks!
You need double buffering. To do this you need to create a BufferedImage
and get the Graphics
from it. Paint everything to the image, render the image on to the screen, then finally fill the image with a the background color or image to reset it.
Sample:
//one time instantiation
BufferedImage b = new BufferedImage(width, height, mode);
In paint(Graphics g)
:
Graphics buffer = b.getGraphics();
//render all of the stuff on to buffer
Graphics2D g = (Graphics2D) buffer;
for (Ball ball : Ball.BALLS) {
g.drawOval((int) ball.getLocation().getX(), (int) ball.getLocation().getY(), ball.getRadius(), ball.getRadius());
}
g1.drawImage(b, 0, 0, width, height, null);
g.setColor(Color.BLACK);
//reset the image
g.drawRect(0, 0, width, height);
g.dispose();
这篇关于在画布上闪烁的白色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!