如何添加指令阶段和主阶段以及通向我的游戏窗格的按钮?

时间:2018-06-07 05:14:54

标签: java button graphics

这是我为熟悉编码GUI而创建的演示,但我不知道如何使用按钮创建启动阶段和指令阶段。我的开始阶段应该包含两个按钮:一个用于进入指令阶段,另一个用于进入动画演示阶段。

在这个演示中,我创建了一个名为“box”的玩家,它位于一个300乘200的游戏阶段。玩家是JLabel。我在游戏中使用了Swing组件。

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.HashSet;
import java.util.Set;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

/**
 * This class demonstrates basic animation using a timer, an action listener,
 * and a key listener
 * 
 * @author mrprowse
 *
 */
public class AnimationDemo extends JPanel implements ActionListener, KeyListener {

 private static final long serialVersionUID = 1L;
 JLabel box = new JLabel();
 int playerSpeed = 1;
 int FPS = 30;
 // The keys set holds the keys being pressed
 private final Set<Integer> keys = new HashSet<>();

public static void main(String[] args) {
 // Open the GUI window
 SwingUtilities.invokeLater(new Runnable() {
  @Override
  public void run() {
  // Create a new object and
  // run its go() method
  new AnimationDemo().go();
  }
 });
}

AnimationDemo() {
 // Run the parent class constructor
 super();
 // Allow the panel to get focus
 setFocusable(true);
 // Don't let keys change the focus
 setFocusTraversalKeysEnabled(false);
}

protected void go() {
 // Setup the window
 JFrame jf = new JFrame();
 // Add this panel to the window
 jf.setContentPane(this);
 // Set the window properties
 jf.setTitle("Animation Demo");
 jf.setSize(300, 200);
 jf.setLocationRelativeTo(null);
 jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 jf.setVisible(true);

 // Setup the movable box
 box.setBounds(10, 10, 10, 10);
 box.setVisible(true);
 box.setBackground(Color.BLUE);
 // Opaque makes the background visible
 box.setOpaque(true);

 // Setup the key listener
 addKeyListener(this);
 // Null layout allows moving objects!!!
 setLayout(null);
 add(box);

 // Set the timer
 Timer tm = new Timer(1000 / FPS, this);
 tm.start();
}

@Override
public void actionPerformed(ActionEvent arg0) {
 // Move up if W is pressed
 if (keys.contains(KeyEvent.VK_W)) {
  box.setLocation(box.getX(), box.getY() - playerSpeed);
 }
 // Move right if D is pressed
 if (keys.contains(KeyEvent.VK_D)) {
  box.setLocation(box.getX() + playerSpeed, box.getY());
 }
 // Move down if S is pressed
 if (keys.contains(KeyEvent.VK_S)) {
  box.setLocation(box.getX(), box.getY() + playerSpeed);
 }
 // Move left if A is pressed
 if (keys.contains(KeyEvent.VK_A)) {
  box.setLocation(box.getX() - playerSpeed, box.getY());
 }
}

@Override
public void keyPressed(KeyEvent e) {
 // Add the key to the list
 // of pressed keys
 if (!keys.contains(e.getKeyCode())) {
  keys.add(e.getKeyCode());
 }
}

@Override
public void keyReleased(KeyEvent e) {
 // Remove the key from the
 // list of pressed keys
 keys.remove((Integer) e.getKeyCode());
}

@Override
public void keyTyped(KeyEvent e) {
}

}

0 个答案:

没有答案