我一直试图移动,并显示图像(例如心率图像)。这是我到目前为止所拥有的。图像一直向左移动;到现在为止还挺好。但是我需要将这个运动图像嵌入到另一个帧中。我知道我的问题似乎非常
import java.awt.EventQueue;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class HeartBeat extends JPanel{
public static void main(String[] args) throws Exception {
new HeartBeat();
}
public HeartBeat(){
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel j = new JPanel();
j.add(new HeartBeat2());
frame.add(j);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
class HeartBeat2 extends JPanel{
BufferedImage bi;
public HeartBeat2(){
try {
bi = ImageIO.read(new URL("http://i.stack.imgur.com/i8UJD.jpg"));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Runnable r = new Runnable() {
@Override
public void run() {
final BufferedImage canvas = new BufferedImage(
bi.getWidth(), bi.getHeight(),
BufferedImage.TYPE_INT_RGB);
final JLabel animationLabel = new JLabel(new ImageIcon(canvas));
ActionListener animator = new ActionListener() {
int x = 0;
@Override
public void actionPerformed(ActionEvent e) {
Graphics2D g = canvas.createGraphics();
// paint last part of image in left of canvas
g.drawImage(bi, x, 0, null);
// paint first part of image immediately to the right
g.drawImage(bi, x + bi.getWidth(), 0, null);
// reset x to prevent hitting integer overflow
if (x%bi.getWidth()==0) x = 0;
g.dispose();
animationLabel.repaint();
x--;
}
};
Timer timer = new Timer(40, animator);
timer.start();
JPanel j = new JPanel();
JOptionPane.showMessageDialog(null, animationLabel);
timer.stop();
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency
SwingUtilities.invokeLater(r);
}
}}
答案 0 :(得分:1)
代码的问题是
animationLabel
已添加到JOptionPane
,但您从未将其添加到HeartBeat2
JOptionPane.showMessageDialog
是一个阻塞(模态)调用,因此它会阻止在它之后发生的任何代码(即timer.stop()
)。但是如果删除JOptionPane
(尝试将标签添加到面板中),将自动调用timer.stop()
(计时器正在控制图像/动画)。如果你只是离开那里的JOptionPane
,那么将标签添加到面板将不起作用,因为每个组件只能有一个父
所以你需要
首先,完全删除Runnable
。你不需要它。
取出JOptionPane
,然后add(animationLabel)
取出HeartBeat2
取出timer.stop()
public HeartBeat2() {
try {
bi = ImageIO.read(new URL("http://i.stack.imgur.com/i8UJD.jpg"));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
final BufferedImage canvas = new BufferedImage(
bi.getWidth(), bi.getHeight(),
BufferedImage.TYPE_INT_RGB);
final JLabel animationLabel = new JLabel(new ImageIcon(canvas));
add(animationLabel);
ActionListener animator = new ActionListener() {
int x = 0;
@Override
public void actionPerformed(ActionEvent e) {
Graphics2D g = canvas.createGraphics();
// paint last part of image in left of canvas
g.drawImage(bi, x, 0, null);
// paint first part of image immediately to the right
g.drawImage(bi, x + bi.getWidth(), 0, null);
// reset x to prevent hitting integer overflow
if (x % bi.getWidth() == 0) {
x = 0;
}
g.dispose();
animationLabel.repaint();
x--;
}
};
Timer timer = new Timer(40, animator);
timer.start();
}