帮助KeyEvent

时间:2015-05-06 00:32:47

标签: java swing key coordinates keyevent

我正在为一个课程项目制作一个平台游戏,到目前为止我所能做的就是在游戏中加入鸡角色。我需要能够让他继续推进" D"或右箭头。我的代码是:

public class Main extends JFrame {
    public Main(){

        //Creates Title Image 
        JLabel title = new JLabel(" ");
        ImageIcon tl = new ImageIcon("title.gif");
        title.setIcon(tl);

        //Creates Start Image
        final JButton start = new JButton("");
        ImageIcon st = new ImageIcon("start.gif");
        start.setIcon(st);

        //Creates Options Image
        JButton options = new JButton("");
        ImageIcon opt = new ImageIcon("options.gif");
        options.setIcon(opt);
        options.setBackground(Color.BLACK);

        //Create first frame for "Start" button
        final JPanel p1 = new JPanel();
        p1.setLayout(new GridLayout(1, 1));
        p1.add(start, BorderLayout.CENTER);

        //Create second panel for title label
        final JPanel p2 = new JPanel(new BorderLayout());
        p2.setLayout(new GridLayout(1, 3));
        p2.add(title, BorderLayout.WEST);

        //Create third panel for "Options" button
        final JPanel p3 = new JPanel(new BorderLayout());
        p3.setLayout(new GridLayout(1, 1));
        p3.add(options, BorderLayout.SOUTH);

        //Creates fourth panel to organize all other primary
        final JPanel p4 = new JPanel(new BorderLayout());
        p4.setLayout(new GridLayout(1, 3));
        p4.add(p1, BorderLayout.WEST);
        p4.add(p2, BorderLayout.CENTER);
        p4.add(p3, BorderLayout.EAST);


        //When button is clicked, it changes the level
        start.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(start.isEnabled()) {
                    remove(p4);
                    setSize(1440, 500);
                    add(new ContentPanel1());
                    validate();
                }
                else {
                    return;
                }
            }
        });

        //Adds fourth panel to frame
        add(p4, BorderLayout.CENTER);
    }

    public static void main(String arg[]) {
        Main frame = new Main();

        //Finds screen size of monitor
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

        //Creates the frame
        frame.setTitle("Cockadoodle Duty: Awakening");
        frame.setSize(screenSize);
        frame.setLocale(null); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        String background = "#000000";
        frame.setBackground(Color.decode(background));
    }
}

class coordinate {
    public static int x;
    public static int y;
}

class ContentPanel1 extends JPanel{
    Image back = Toolkit.getDefaultToolkit().getImage("level0.gif");
    Image chick = Toolkit.getDefaultToolkit().getImage("chicken.gif");

    ContentPanel1() {
        MediaTracker mt = new MediaTracker(this);

        mt.addImage(back, 0);
        try {
            mt.waitForAll();
        } catch (InterruptedException e){
            e.printStackTrace();
        }
    }

    public void paintComponent(Graphics g){
        coordinate.x = 20;
        coordinate.y = 321;

        super.paintComponent(g);
        int imwidth = back.getWidth(null);
        int imheight = back.getHeight(null);
        g.drawImage(back, 1, 1, null);
        g.drawImage(chick, coordinate.x, coordinate.y, null);
    }

    public void MoveDirection(KeyEvent e, Graphics g) {
          coordinate.x = 20;
          coordinate.y = 321;

          super.paintComponent(g);
          int key = e.getKeyCode();

          if(key == 68) {
              coordinate.x += 1;
              g.drawImage(chick, coordinate.x, coordinate.y, null);
          }
      } 
}

我使用我的代码遇到的主要问题是MoveDirection方法的最后一点。我实现它的方法是在框架中添加一个新的鸡(这主要是因为我只是在测试以查看代码是否有效)。还有更好的方法吗?

1 个答案:

答案 0 :(得分:2)

首先看一下How to Use Key Bindings

  • 永远不要直接从super.paintComponent(g);方法的上下文中调用paintComponent(g);(或paintComponent),还有很多要绘画然后只绘制组件背景。有关详细信息,请参阅Painting in AWT and SwingPerforming Custom Painting。相反,只需在需要时调用repaint,然后重新绘制组件。
  • MediaTracker的使用已过期,您应该使用ImageIO API,它会在阅读图片时自动阻止。有关详细信息,请参阅Reading/Loading an Image
  • 不要将Toolkit.getDefaultToolkit().getScreenSize()JFrame#setSize结合使用,getScreenSize方法不会考虑某些操作系统的任务栏或停靠栏等内容,而是使用{{1并传递JFrame#setExtendedState
  • JFrame.MAXIMIZED_BOTH没有做你认为的事情