【问题标题】:Updating JFrame to show animation. For game更新 JFrame 以显示动画。游戏用
【发布时间】:2013-12-30 03:10:18
【问题描述】:

我正在尝试为我的计算机学习课程制作一个平台游戏,而现在我只是让角色的运动下降,也就是跳跃和左右移动。我得到了跳转,从构造函数调用时它工作正常,但是,当从事件侦听器调用它时,帧不会更新,角色只是从一个地方跳到另一个地方,没有任何动画。我不知道为什么会发生这种情况,任何帮助将不胜感激,如果您对制作此类游戏有任何建议,我将非常乐意接受。

提前致谢。

import java.awt.event.*;
import javax.swing.*;

public class LooperGui extends JFrame implements ActionListener{

//setting up all of the variables and components of the JFrame
private JLabel stick = new JLabel();
JButton g = new JButton("jump");
ImageIcon h = new ImageIcon("src//stickGuy.jpg");

int x = 100, y = 120, maxY = y, minY = 168;
double time = 5;
int fps = 25, frames = (int) (time*fps); 
double timePerFrame = (time/frames);

public LooperGui(){

    setSize(500, 500);
    //setUndecorated(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
    setLayout(null);
    setResizable(false);
    stick.setIcon(h);
    g.setBounds(10, 10, 100, 30);
    g.addActionListener(this);
    add(g);
    stick.setBounds(x, y, h.getIconWidth(), h.getIconHeight());
    add(stick);
    jump();//call jump from the constructor and it will be perfectly animated, the exact way that I intended it to be
}

public void jump(){

    //I attempted to make the jump as close to reality as possible so I used 
    //kinematic equations to set the characters height in the air at any given time
    //from here it is easy to change the characters side to side movement, as it is simply changing the x value

    //the first for loop if for the ascent, and the second one is for the descent
    for(double t = time; t>0; t-=timePerFrame){
        y = (int) ((9.81*(t*t))/2);
        stick.setBounds(x, y, h.getIconWidth(), h.getIconHeight());
        x+=1;
        //there may be a problem with using thread.sleep(), not really sure
        try {
            Thread.sleep(4);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }

    for(double t = 0; t<time; t+=timePerFrame){
        y = (int) ((9.81*(t*t))/2);
        stick.setBounds(x, y, h.getIconWidth(), h.getIconHeight());
        x+=1;
        try {
            Thread.sleep(4);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }   
}

public void actionPerformed(ActionEvent e) {
    if(e.getSource() == g){
        jump();//calling jump from the action performed method makes the character jump positions
    }

}

}

我目前正在为角色使用火柴人,无法链接它,因为我没有足够高的代表。但是在 Photoshop 或 Paint 中制作一个看起来很糟糕的照片很容易。

【问题讨论】:

    标签: java swing jframe event-listener


    【解决方案1】:
    1. 永远不要将ActionListener 应用于任何没有此类侦听器的组件
    2. 要注册监听器,要么使用匿名类的方式使用内联方法,要么实现一个反映监听器的新命名类并将其实例化以分配。 Check out the tutorial page
    3. 切勿将null 布局和设置大小提示与setBounds(x, y, width, height) 一起使用。正确学习layout managers
    4. 大多数时候,不要将大小设置为JFrame。而是在布局组件并将它们添加到内容窗格之后调用 pack()
    5. 始终将 GUI 渲染任务放在 EDT using SwingUtilities.invokeLater(Runnable) 中。
    6. 永远不要在 Swing GUI 或事件任务中调用 Thread.sleep(milSecond),因此它可能有机会阻止 EDT。

    【讨论】:

    • 所以我应该实现一个 keyListener 来“坚持”?关于设置 JFrame 的大小并将布局设置为 null,这就是我被教导设置 JFrame 的方式。但我会查看您提供的链接。最后一个链接,EDT 对我来说毫无意义,4 个月前才开始编程,还没有真正进入线程。所以一个对菜鸟更友好的解释会很棒。再次感谢。
    • @user3093022,如果没有意义,没关系。您可以稍后了解它。但首先开始学习布局管理器。然后转到使用侦听器。但此时不要试图实现动画。一件一件的往前走。及时做gui渲染任务前,先学习Threading和Swing线程模型。
    • 是的,这有点问题,我正在为课程的最后一个项目做这个,我可能有四个多星期的时间来完成这个编程。不过,谢谢你,我一定会努力学习所有这些。
    • 四个星期是很多时间。至少了解FlowLayoutBorderLayout。从How to Make frame开始。你不会后悔的
    • 现在,一个月后,我必须非常感谢你。我的程序运行良好,这一切都归功于你!你的好男人!!!保持良好的工作!再次感谢。
    【解决方案2】:

    尝试调用

    repaint();
    

    刷新JFrame的显示

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-27
      • 2016-04-17
      相关资源
      最近更新 更多