[Java]双缓冲无法双缓冲
好吧,正如标题所说,我在双缓冲方面遇到了麻烦。我读过这本 Java 电子书,它没有为您提供他们所教内容的代码 - 至少不完全。所以我必须做很多猜测工作。
目标:在小程序中弹跳球。
球仍然闪烁的方式不起作用。又名双缓冲无法工作。
我使用三个类,球类,双缓冲类和MainApplet类。 MainApplet 扩展了双缓冲,并且 ball 类扩展了 MainApplet
import java.applet.Applet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
@SuppressWarnings("serial")
public class MainApplet extends DoubleBuffering implements Runnable {
public Ball ball;
public Graphics g;
private Thread ticker;
public boolean running = false;
public void init() {
setSize(100,100);
ball = new Ball(getWidth() / 5f, getHeight() / 4f, 1.5f,
2.3f, 12, Color.red);
moveBall();
}
public void run() {
while(running) {
try {
Rectangle bou = new Rectangle(getWidth(), getHeight());
ball.move(bou);
ball.update(getGraphics());
Thread.sleep(1000 / 15);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
repaint();
}
}
public void moveBall() {
start();
}
public synchronized void start() {
running = true;
ticker = new Thread(this);
ticker.setPriority(Thread.MIN_PRIORITY + 1);
ticker.start();
}
public synchronized void stop() {
running = false;
ticker.stop();
}
}
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
public class DoubleBuffering extends Applet
{
Image offScreenBuffer;
@SuppressWarnings("deprecation")
public void update(Graphics g)
{
System.out.println("We are buffing");
Graphics gr;
if (offScreenBuffer==null ||
(! (offScreenBuffer.getWidth(this) == this.size().width
&& offScreenBuffer.getHeight(this) == this.size().height)))
{
offScreenBuffer = this.createImage(size().width, size().height);
}
gr = offScreenBuffer.getGraphics();
System.out.println("Something else");
paint(gr);
g.drawImage(offScreenBuffer, 0, 0, this);
}
}
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
public class Ball extends MainApplet{
int size;
private Color color;
public float x, y, dx, dy;
public Ball ball;
public int width, height;
public Image offscreenImage;
public Graphics offscr;
private MainApplet ma;
Ball (float x, float y, float dx, float dy, int size,
Color color) {
this.x = x;
this.y = y;
this.dy = dx;
this.dy = dy;
this.color = color;
this.size = size;
}
public void draw (Graphics g) {
g.setColor(this.color);
g.fillOval((int) x, (int) y, size, size);
}
public void update(Graphics g) {
g.clearRect(0, 0, getWidth(), getHeight());
draw(g);
}
public void move(Rectangle bounds) {
// Add velocity values dx/dy to position to
// ball s new position
x += dx;
y += dy;
// Check for collision with left edge
if (x < bounds.x && dx < 0) {
dx = -dx;
x -= 2 * (x - bounds.x);
}
// Check for collision with right edge
else if (x + size > bounds.x + bounds.width &&
dx > 0) {
dx = -dx;
x -= 2 * ((x + size) - (bounds.x + bounds.width));
}
// Checks for collision with top edge
else if (y < bounds.y && dy < 0) {
dy = -dy;
y -= 2 * (y - bounds.y);
}
// Checks for collision with bottom edge
else if (y + size > bounds.y + bounds.height && dy >0) {
dy = -dy;
y -= 2 * ((y + size) - (bounds.y + bounds.width));
}
}
}
注意:我不太确定此代码将如何出现>。<看起来好像“code:”函数不稳定。
不管怎样,不要太讨厌我的惯例,我还是个新手。提示和答案将不胜感激。谢谢。
Well, as the title says, I am having trouble with double buffering. I read this Java Ebook and it doesn't give you code for what they are teaching you - at least not completely. So I have to do a lot of guess work.
Objective : Bouncing ball in an applet.
It's not working in the way that the ball is still flashing. Aka double buffering is failing to work.
I use three classes, ball class, double buffering class, and MainApplet class. MainApplet extends double buffering, and ball class extends MainApplet
import java.applet.Applet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
@SuppressWarnings("serial")
public class MainApplet extends DoubleBuffering implements Runnable {
public Ball ball;
public Graphics g;
private Thread ticker;
public boolean running = false;
public void init() {
setSize(100,100);
ball = new Ball(getWidth() / 5f, getHeight() / 4f, 1.5f,
2.3f, 12, Color.red);
moveBall();
}
public void run() {
while(running) {
try {
Rectangle bou = new Rectangle(getWidth(), getHeight());
ball.move(bou);
ball.update(getGraphics());
Thread.sleep(1000 / 15);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
repaint();
}
}
public void moveBall() {
start();
}
public synchronized void start() {
running = true;
ticker = new Thread(this);
ticker.setPriority(Thread.MIN_PRIORITY + 1);
ticker.start();
}
public synchronized void stop() {
running = false;
ticker.stop();
}
}
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
public class DoubleBuffering extends Applet
{
Image offScreenBuffer;
@SuppressWarnings("deprecation")
public void update(Graphics g)
{
System.out.println("We are buffing");
Graphics gr;
if (offScreenBuffer==null ||
(! (offScreenBuffer.getWidth(this) == this.size().width
&& offScreenBuffer.getHeight(this) == this.size().height)))
{
offScreenBuffer = this.createImage(size().width, size().height);
}
gr = offScreenBuffer.getGraphics();
System.out.println("Something else");
paint(gr);
g.drawImage(offScreenBuffer, 0, 0, this);
}
}
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
public class Ball extends MainApplet{
int size;
private Color color;
public float x, y, dx, dy;
public Ball ball;
public int width, height;
public Image offscreenImage;
public Graphics offscr;
private MainApplet ma;
Ball (float x, float y, float dx, float dy, int size,
Color color) {
this.x = x;
this.y = y;
this.dy = dx;
this.dy = dy;
this.color = color;
this.size = size;
}
public void draw (Graphics g) {
g.setColor(this.color);
g.fillOval((int) x, (int) y, size, size);
}
public void update(Graphics g) {
g.clearRect(0, 0, getWidth(), getHeight());
draw(g);
}
public void move(Rectangle bounds) {
// Add velocity values dx/dy to position to
// ball s new position
x += dx;
y += dy;
// Check for collision with left edge
if (x < bounds.x && dx < 0) {
dx = -dx;
x -= 2 * (x - bounds.x);
}
// Check for collision with right edge
else if (x + size > bounds.x + bounds.width &&
dx > 0) {
dx = -dx;
x -= 2 * ((x + size) - (bounds.x + bounds.width));
}
// Checks for collision with top edge
else if (y < bounds.y && dy < 0) {
dy = -dy;
y -= 2 * (y - bounds.y);
}
// Checks for collision with bottom edge
else if (y + size > bounds.y + bounds.height && dy >0) {
dy = -dy;
y -= 2 * ((y + size) - (bounds.y + bounds.width));
}
}
}
Note: I'm not too sure how this code will come out >.< it looks as if it's being choppy with the 'code:' function.
Anyways, don't hate too hard on my conventions, I'm still rather new. Tips and answers would be appreciated. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要调用 Thread.Sleep() ,而是尝试使用像这样的 swing 计时器。
这至少应该有一点帮助。
Instead of calling
Thread.Sleep()
try using the swing Timer like this.that should help at least a little bit.
我假设 DoubleBuffering 类是一些教程类。我猜它源自 Applet 而不是 JApplet。如果您使用 JApplet,默认情况下您将获得双缓冲。
I'm assuming the class DoubleBuffering is some tutorial class. I'm guessing it derives from Applet and not JApplet. If you use JApplet you will get double buffering by default.