gpt4 book ai didi

java - 使用 Graphics2D 绘制图像

转载 作者:行者123 更新时间:2023-11-29 08:02:45 25 4
gpt4 key购买 nike

我正在尝试使用 java 的 Graphics2D 在屏幕上绘制图像。这是我正在使用的代码。我想看到图像在屏幕上稳定移动。目前我可以看到图像,但它不会移动,除非我调整窗口大小,在这种情况下它会移动。我已经勾勒出下面的类。

 public class Tester extends JFrame {

private static final long serialVersionUID = -3179467003801103750L;
private Component myComponent;
public static final int ONE_SECOND = 1000;
public static final int FRAMES_PER_SECOND = 20;

private Timer myTimer;

public Tester (Component component, String title) {
super(title);
myComponent = component;
}

public void start () {

myTimer = new Timer(ONE_SECOND / FRAMES_PER_SECOND, new ActionListener() {
@Override
public void actionPerformed (ActionEvent e) {
repaint();
}
});
myTimer.start();
}

@Override
public void paint (Graphics pen) {
if (myComponent != null) {
myComponent.paint(pen);
}
}

}

传递给 Tester 的 Component 对象是以下类:

public class LevelBoard extends Canvas implements ISavable {

private static final long serialVersionUID = -3528519211577278934L;

@Override
public void paint (Graphics pen) {
for (Sprite s : mySprites) {
s.paint((Graphics2D) pen);
}
}

protected void add (Sprite sprite) {
mySprites.add(sprite);
}

我已经确保这个类只有一个我添加的 Sprite 。 Sprite 类大致如下:

public class Sprite {

private Image myImage;
private int myX, myY;

public Sprite () {
URL path = getClass().getResource("/images/Bowser.png");
ImageIcon img = new ImageIcon(path);
myImage = img.getImage();

}

public void update () {
myX += 5;
myY += 5;
}

public void paint (Graphics2D pen) {
update();
pen.drawImage(myImage, myX, myY,null);
}

但是,我在屏幕上只看到 bowser 的静止图像。除非调整窗口大小,否则他不会移动。我知道 Sprite 类中的 paint(Graphics2D pen) 方法以特定的时间间隔被调用(因为 Tester 类中的 Timer)。但是,即使 x 和 y 位置每次都增加 5。 Sprite 不会移动。为什么不?我如何解决它?我现在只是想测试我的程序的一些其他功能,所以我真的只需要启动并运行它。我真的不在乎如何。

最佳答案

你的代码充满了问题:

  1. 不要覆盖 JFrame.paint(),尤其是在不调用 super 的情况下。设置一个 ContentPane 并覆盖它的 paintComponent()。虽然这看起来很方便,但通常是一个糟糕的设计并且没有必要。
  2. 不要覆盖 JComponent.paint(),而是覆盖 JComponent.paintComponent()(并调用 super)
  3. 使用 JLabel 显示图像。这要简单得多。
  4. 不要混用 AWT(Canvas) 和 Swing (JFrame)。坚持 Swing 。

这是一个简单的示例,展示了 Bowser 在框架中移动。 (当您缩小框架尺寸并用框架边框击中图像时,这很有趣 ;-))

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UnsupportedLookAndFeelException;

public class TestAnimation2 {
private static final int NB_OF_IMAGES_PER_SECOND = 50;
private static final int WIDTH = 800;
private static final int HEIGHT = 600;
private Random random = new Random();

private double dx;
private double dy;

private double x = WIDTH / 2;
private double y = HEIGHT / 2;

protected void initUI() throws MalformedURLException {
final JFrame frame = new JFrame(TestAnimation2.class.getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
final JLabel label = new JLabel(new ImageIcon(new URL("http://www.lemondedemario.fr/images/dossier/bowser/bowser.png")));
label.setSize(label.getPreferredSize());
frame.setMinimumSize(label.getPreferredSize());
frame.add(label);
frame.setSize(WIDTH, HEIGHT);
dx = getNextSpeed();
dy = getNextSpeed();
Timer t = new Timer(1000 / NB_OF_IMAGES_PER_SECOND, new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
x += dx;
y += dy;
if (x + label.getWidth() > frame.getContentPane().getWidth()) {
x = frame.getContentPane().getWidth() - label.getWidth();
dx = -getNextSpeed();
} else if (x < 0) {
x = 0;
dx = getNextSpeed();
}
if (y + label.getHeight() > frame.getContentPane().getHeight()) {
y = frame.getContentPane().getHeight() - label.getHeight();
dy = -getNextSpeed();
} else if (y < 0) {
y = 0;
dy = getNextSpeed();
}
label.setLocation((int) x, (int) y);

}
});
frame.setVisible(true);
t.start();
}

private double getNextSpeed() {
return 2 * Math.PI * (0.5 + random.nextDouble());
}

public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException,
UnsupportedLookAndFeelException {
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
try {
new TestAnimation2().initUI();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}

关于java - 使用 Graphics2D 绘制图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13425220/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com