Java 如何根据计时器移动图像?
如何根据计时器移动图像?我试图让图像在没有任何用户输入的情况下移动,但我做了一些错误的事情,因为我无法让图像移动 我是否错误地调用了为实际移动图像而构建的方法Java 如何根据计时器移动图像?,java,image,swing,timer,paint,Java,Image,Swing,Timer,Paint,如何根据计时器移动图像?我试图让图像在没有任何用户输入的情况下移动,但我做了一些错误的事情,因为我无法让图像移动 我是否错误地调用了为实际移动图像而构建的方法AnimationPanel?我有一种感觉,这与此有关 到目前为止,我已经尝试了以下内容:重载构造函数是这样的:我可以从这个特定类轻松调用AnimationPanel public class ClassC extends Component { int x; int y; BufferedImage img; p
AnimationPanel
?我有一种感觉,这与此有关
到目前为止,我已经尝试了以下内容:重载构造函数是这样的:我可以从这个特定类轻松调用AnimationPanel
public class ClassC extends Component {
int x; int y;
BufferedImage img;
public void paint(Graphics g) {
g.drawImage(img, x, y, null);
}
public ClassC() {
try {
img = ImageIO.read(new File("RobotFrame1.png"));
} catch (IOException e) {
}
}
public ClassC(int x)
{}
public Dimension getPreferredSize() {
if (img == null) {
return new Dimension(100,100);
} else {
return new Dimension(img.getWidth(null)+900, img.getHeight(null)+900);
}
}
public void AnimationPanel() {
javax.swing.Timer timer = new javax.swing.Timer(20, new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
x++;
y++;
repaint();
}
});
timer.start();
}
public static void main(String[] args) {
JFrame f = new JFrame("Load Image Sample");
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.add(new ClassC());
f.pack();
f.setVisible(true);
ClassC callanimation = new ClassC(1);
callanimation.AnimationPanel();
}
}
屏幕上的组件不是正在设置动画的组件
// Create first instance here...
f.add(new ClassC());
f.pack();
f.setVisible(true);
// Create second instance here...
ClassC callanimation = new ClassC(1);
callanimation.AnimationPanel();
相反,这会让你开始
ClassC callanimation = new ClassC();
callanimation.AnimationPanel();
f.add(callanimation);
f.pack();
f.setVisible(true);
ps-我也会非常小心,你在混合框架。虽然AWT和Swing共享库的某些部分,但它们并不总是在一起玩得很好;) 屏幕上的组件不是您正在设置动画的组件
// Create first instance here...
f.add(new ClassC());
f.pack();
f.setVisible(true);
// Create second instance here...
ClassC callanimation = new ClassC(1);
callanimation.AnimationPanel();
相反,这会让你开始
ClassC callanimation = new ClassC();
callanimation.AnimationPanel();
f.add(callanimation);
f.pack();
f.setVisible(true);
ps-我也会非常小心,你在混合框架。虽然AWT和Swing共享库的某些部分,但它们并不总是在一起玩得很好;)
为什么这是空的?x代表什么
f.add(new ClassC()); <-- you created a ClassC() here
f.pack();
f.setVisible(true);
ClassC callanimation = new ClassC(1); <-- you created a new ClassC here
为什么这是空的?x代表什么
f.add(new ClassC()); <-- you created a ClassC() here
f.pack();
f.setVisible(true);
ClassC callanimation = new ClassC(1); <-- you created a new ClassC here
我想到的第一个问题是,为什么AWT?相关:-)我想到的第一个问题是,为什么AWT?相关:-)我很抱歉浪费了你的时间。今天我的思维一定不正常。非常感谢。欢迎来到我的世界:P@moonbeamer2234:与其在
JFrame
中添加WindowListener
,不如编写f.setDefaultCloseOperation(JFrame.DISPOSE\u ON\u CLOSE)
,这将完成几乎相同的任务:-)@nIcEcOwjava.awt.Frame
没有setDefaultCloseOperation
。这将假定OP正在使用Swing;)@MadProgrammer:但是在main方法中,我看到JFrame
正在被使用……很抱歉浪费了你的时间。今天我的思维一定不正常。非常感谢。欢迎来到我的世界:P@moonbeamer2234:与其在JFrame
中添加WindowListener
,不如编写f.setDefaultCloseOperation(JFrame.DISPOSE\u ON\u CLOSE)
,这将完成几乎相同的任务:-)@nIcEcOwjava.awt.Frame
没有setDefaultCloseOperation
。这将假定OP正在使用Swing;)@MadProgrammer:但是在主方法中,我正在观看JFrame
被使用。。。