【发布时间】:2014-08-12 09:48:42
【问题描述】:
在我的游戏主类中,我有以下代码:
// dimensions
public static final int WIDTH = 266;
public static final int HEIGHT = 200;
public static final int SCALE = 3;
// game loop
private Thread thread;
private boolean running = true;
public static int count = 0;
// rendering
private BufferedImage image;
public Panel() {
setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
}
public void run() {
long start = System.nanoTime();
final double numUpdates = 30.0;
double ns = 1000000000 / numUpdates;
double delta = 0;
while(running) {
long current = System.nanoTime();
delta += (current - start) / ns;
start = current;
if(delta >= 1) {
update();
delta--;
}
repaint();
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(new Color(230, 100, 100));
g.fillRect(0, 0, 200, 100);
g.drawImage(image, 0, 0, WIDTH * SCALE, HEIGHT * SCALE, null);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Rage Mage");
frame.add(new Panel());
frame.setResizable(false);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
image 并没有被缩放,它是按照实际尺寸绘制的,而不是让它的尺寸乘以 3 (SCALE)。
此外,如果我删除 super.paintComponent(g); 行和 g.drawImage(...); 行,矩形仍在渲染中。
谢谢!
【问题讨论】:
-
你认为
drawImage()为什么会缩放图像?您正在调用的方法的 Javadoc 的第一行声明:“绘制尽可能多的指定图像已经缩放以适合指定的矩形。”。您需要事先进行实际缩放,例如,使用图像的getScaledInstance方法。 -
@Ordous 我现在读了它,是的,这就是解决方案! - 但不要使用 Image.getScaledInstance!
标签: java bufferedimage scaling