Skip to content
Advertisement

How to make use of key events USing Intellij IDE and StdDraw as GUI?

I need to make space invaders in java. I am however struggling to implement key events into my program. Currently I have 2 classes, MainMenu.java and FrameListener.java. Noting happens when I press keys. What am I doing wrong?

MAINMENU.JAVA

import java.awt.*;
import java.io.File;
import java.io.IOException;


public class MainMenu {
    public static void main(String[] args) {

        //////////////////////////////////////////////////////https://stackoverflow.com/questions/5652344/how-can-i-use-a-custom-font-in-java
        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());
    }
}

Advertisement

Answer

StdDraw.onKeyPress(--keyCode--);
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement