Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何根据计时器移动图像?_Java_Image_Swing_Timer_Paint - Fatal编程技术网

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)
,这将完成几乎相同的任务:-)@nIcEcOw
java.awt.Frame
没有
setDefaultCloseOperation
。这将假定OP正在使用Swing;)@MadProgrammer:但是在main方法中,我看到
JFrame
正在被使用……很抱歉浪费了你的时间。今天我的思维一定不正常。非常感谢。欢迎来到我的世界:P@moonbeamer2234:与其在
JFrame
中添加
WindowListener
,不如编写
f.setDefaultCloseOperation(JFrame.DISPOSE\u ON\u CLOSE)
,这将完成几乎相同的任务:-)@nIcEcOw
java.awt.Frame
没有
setDefaultCloseOperation
。这将假定OP正在使用Swing;)@MadProgrammer:但是在主方法中,我正在观看
JFrame
被使用。。。