Java 带缓冲图像的双缓冲
Java double buffering with buffered image
找了一天,我已经放弃了,决定问这个问题:我似乎无法阻止这个程序不断闪烁:
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Paint {
BufferedImage image0, image1, image2;
double rotation0 = 0.0001;
double rotation1 = 0.0501;
double rotation2 = 3.0001;
double add0 = 0.0001;
double add1 = 0.0016;
double add2 = 0.000001;
private int x() {
return Main.getX() / 2;
}
private int y() {
return Main.getY() / 2;
}
public Paint() {
try {
image0 = ImageIO.read(new File("circle1.jpg"));
image1 = ImageIO.read(new File("circle2.jpg"));
image2 = ImageIO.read(new File("circle3.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void paint(Graphics g) {
// create the transform, note that the transformations happen
// in reversed order (so check them backwards)
AffineTransform at0 = new AffineTransform();
AffineTransform at1 = new AffineTransform();
AffineTransform at2 = new AffineTransform();
// 4. translate it to the center of the component
at0.translate(x(), y() + 10);
at1.translate(x(), y() + 10);
at2.translate(x(), y() + 10);
// 3. do the actual rotation
rotation0 += add0;
rotation1 += add1;
rotation2 += add2;
at0.rotate(rotation0);
at1.rotate(rotation1);
at2.rotate(rotation2);
// 2. just a scale because this image is big
at0.scale(1, 1);
at1.scale(1, 1);
at2.scale(1, 1);
// 1. translate the object so that you rotate it around the
// center (easier :))
at0.translate(-image0.getWidth()/2, -image0.getHeight()/2);
at1.translate(-image1.getWidth()/2, -image1.getHeight()/2);
at2.translate(-image2.getWidth()/2, -image2.getHeight()/2);
// draw the image
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(image0, at0, null);
g2d.drawImage(image1, at1, null);
g2d.drawImage(image2, at2, null);
}
}
重要提示:除双缓冲外一切正常,双缓冲会导致图像闪烁。如果您不介意,请提供代码和答案。
双缓冲包括将您的内容绘制到内部图像,然后将该图像绘制到屏幕上。通过这样做,图像永远不会被部分打印。
因此,不要直接在 Graphics 对象中绘制 3 个图像,而是尝试将它们绘制到一个新的 BufferedImage 中,然后在 Graphics 对象(屏幕)中绘制该图像。
有关详细信息,请参阅此官方页面:https://docs.oracle.com/javase/tutorial/extra/fullscreen/doublebuf.html
我假设用法是在 JPanel 子项中。默认情况下,JPanel 是双缓冲的。
JPanel panel new JPanel() {
Paint paint = new Paint();
@Override
public void paintComponent(Graphics g) {
paint.paint(g);
}
};
.. add the panel to the JFrame.
这应该适用于 java swing。 Java awt 组件是另一个 过时的 问题。
找了一天,我已经放弃了,决定问这个问题:我似乎无法阻止这个程序不断闪烁:
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Paint {
BufferedImage image0, image1, image2;
double rotation0 = 0.0001;
double rotation1 = 0.0501;
double rotation2 = 3.0001;
double add0 = 0.0001;
double add1 = 0.0016;
double add2 = 0.000001;
private int x() {
return Main.getX() / 2;
}
private int y() {
return Main.getY() / 2;
}
public Paint() {
try {
image0 = ImageIO.read(new File("circle1.jpg"));
image1 = ImageIO.read(new File("circle2.jpg"));
image2 = ImageIO.read(new File("circle3.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void paint(Graphics g) {
// create the transform, note that the transformations happen
// in reversed order (so check them backwards)
AffineTransform at0 = new AffineTransform();
AffineTransform at1 = new AffineTransform();
AffineTransform at2 = new AffineTransform();
// 4. translate it to the center of the component
at0.translate(x(), y() + 10);
at1.translate(x(), y() + 10);
at2.translate(x(), y() + 10);
// 3. do the actual rotation
rotation0 += add0;
rotation1 += add1;
rotation2 += add2;
at0.rotate(rotation0);
at1.rotate(rotation1);
at2.rotate(rotation2);
// 2. just a scale because this image is big
at0.scale(1, 1);
at1.scale(1, 1);
at2.scale(1, 1);
// 1. translate the object so that you rotate it around the
// center (easier :))
at0.translate(-image0.getWidth()/2, -image0.getHeight()/2);
at1.translate(-image1.getWidth()/2, -image1.getHeight()/2);
at2.translate(-image2.getWidth()/2, -image2.getHeight()/2);
// draw the image
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(image0, at0, null);
g2d.drawImage(image1, at1, null);
g2d.drawImage(image2, at2, null);
}
}
重要提示:除双缓冲外一切正常,双缓冲会导致图像闪烁。如果您不介意,请提供代码和答案。
双缓冲包括将您的内容绘制到内部图像,然后将该图像绘制到屏幕上。通过这样做,图像永远不会被部分打印。
因此,不要直接在 Graphics 对象中绘制 3 个图像,而是尝试将它们绘制到一个新的 BufferedImage 中,然后在 Graphics 对象(屏幕)中绘制该图像。
有关详细信息,请参阅此官方页面:https://docs.oracle.com/javase/tutorial/extra/fullscreen/doublebuf.html
我假设用法是在 JPanel 子项中。默认情况下,JPanel 是双缓冲的。
JPanel panel new JPanel() {
Paint paint = new Paint();
@Override
public void paintComponent(Graphics g) {
paint.paint(g);
}
};
.. add the panel to the JFrame.
这应该适用于 java swing。 Java awt 组件是另一个 过时的 问题。