如何使用 Intellij IDE 和 StdDraw 作为 GUI 使用按键事件?
How to make use of key events USing Intellij IDE and StdDraw as GUI?
我需要 space 入侵 java。然而,我正在努力将关键事件实施到我的程序中。目前我有 2 个 类、MainMenu.java 和 FrameListener.java。当我按下按键时会发生注意。我做错了什么?
MAINMENU.JAVA
import java.awt.*;
import java.io.File;
import java.io.IOException;
public class MainMenu {
public static void main(String[] args) {
//////////////////////////////////////////////////////
Font fNewFont = null;
try {
fNewFont = Font.createFont(Font.TRUETYPE_FONT, new File("StarJedi-DGRW.ttf")).deriveFont(50f);
} catch (FontFormatException | IOException e) {
e.printStackTrace();
}
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
//register the font
ge.registerFont(fNewFont);
////////////////////////////////////////////////////////////////////////
StdAudio.loop("Disconnected.wav");
StdDraw.setCanvasSize(800, 800);
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.filledSquare(0, 0, 1);
StdDraw.setPenColor(StdDraw.WHITE);
StdDraw.setPenRadius(0.002);
for (int i = 0; i < 1000; i++) {
StdDraw.point(Math.random(), Math.random());
}
// Font font = new Font("Arial", Font.BOLD, 60);
StdDraw.setPenColor(StdDraw.RED);
StdDraw.setFont(fNewFont);
StdDraw.text(0.5, 0.8, "SPACE invaders");
fNewFont = new Font("Arial", Font.PLAIN, 20);
StdDraw.setFont(fNewFont);
StdDraw.text(0.5, 0.7, "PRESS [SPACE] TO PLAY!");
fNewFont = new Font("Arial", Font.PLAIN, 12);
StdDraw.setFont(fNewFont);
StdDraw.text(0.5, 0.6, "Shoot - [W]");
StdDraw.text(0.5, 0.55, "Move - Left [A]; Right [D]");
StdDraw.text(0.5, 0.5, "Rotate Turret- Left [Q]; Right [E]");
StdDraw.text(0.5, 0.45, "Quit - [ESC]");
//StdDraw.show();
new FrameListener();
}
}
FRAMELISTENER.JAVA
import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class FrameListener extends JFrame implements KeyListener {
JLabel label;
ImageIcon icon;
FrameListener() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(0, 0);
this.addKeyListener(this);
this.setVisible(false);
}
public void keyTyped(KeyEvent e) {
System.out.println("Key Pressed-" + e.getKeyCode());
}
public void keyPressed(KeyEvent e) {
System.out.println("Key Pressed-" + e.getKeyCode());
}
public void keyReleased(KeyEvent e) {
System.out.println("Key Pressed-" + e.getKeyCode());
}
}
StdDraw.onKeyPress(--keyCode--);
我需要 space 入侵 java。然而,我正在努力将关键事件实施到我的程序中。目前我有 2 个 类、MainMenu.java 和 FrameListener.java。当我按下按键时会发生注意。我做错了什么?
MAINMENU.JAVA
import java.awt.*;
import java.io.File;
import java.io.IOException;
public class MainMenu {
public static void main(String[] args) {
//////////////////////////////////////////////////////
Font fNewFont = null;
try {
fNewFont = Font.createFont(Font.TRUETYPE_FONT, new File("StarJedi-DGRW.ttf")).deriveFont(50f);
} catch (FontFormatException | IOException e) {
e.printStackTrace();
}
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
//register the font
ge.registerFont(fNewFont);
////////////////////////////////////////////////////////////////////////
StdAudio.loop("Disconnected.wav");
StdDraw.setCanvasSize(800, 800);
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.filledSquare(0, 0, 1);
StdDraw.setPenColor(StdDraw.WHITE);
StdDraw.setPenRadius(0.002);
for (int i = 0; i < 1000; i++) {
StdDraw.point(Math.random(), Math.random());
}
// Font font = new Font("Arial", Font.BOLD, 60);
StdDraw.setPenColor(StdDraw.RED);
StdDraw.setFont(fNewFont);
StdDraw.text(0.5, 0.8, "SPACE invaders");
fNewFont = new Font("Arial", Font.PLAIN, 20);
StdDraw.setFont(fNewFont);
StdDraw.text(0.5, 0.7, "PRESS [SPACE] TO PLAY!");
fNewFont = new Font("Arial", Font.PLAIN, 12);
StdDraw.setFont(fNewFont);
StdDraw.text(0.5, 0.6, "Shoot - [W]");
StdDraw.text(0.5, 0.55, "Move - Left [A]; Right [D]");
StdDraw.text(0.5, 0.5, "Rotate Turret- Left [Q]; Right [E]");
StdDraw.text(0.5, 0.45, "Quit - [ESC]");
//StdDraw.show();
new FrameListener();
}
}
FRAMELISTENER.JAVA
import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class FrameListener extends JFrame implements KeyListener {
JLabel label;
ImageIcon icon;
FrameListener() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(0, 0);
this.addKeyListener(this);
this.setVisible(false);
}
public void keyTyped(KeyEvent e) {
System.out.println("Key Pressed-" + e.getKeyCode());
}
public void keyPressed(KeyEvent e) {
System.out.println("Key Pressed-" + e.getKeyCode());
}
public void keyReleased(KeyEvent e) {
System.out.println("Key Pressed-" + e.getKeyCode());
}
}
StdDraw.onKeyPress(--keyCode--);