Jump to content

Help with making ddr in java for a school project

WaffleMage
Go to solution Solved by Bananaman1,

All you have to do is implement KeyListener and add it to the frame.

Here's how to do it:

import javax.swing.*;
import java.awt.*;
import java.util.Random;
import javax.swing.*;
import java.awt.Graphics;
import java.awt.*;
import java.awt.event.*;
import javax.swing.ImageIcon;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import javax.swing.JPanel;
import javax.swing.Timer;

//add implements KeyListener
public class App extends JPanel implements KeyListener {

    //changing these values will change the size of the game, while still remaining functional
    //within the size limit specified.
    static int windowWidth = 525;8
    static int windowHeight = 400;
    JLabel label1 = new JLabel("Test");
    int randNumb = 0;
    int squareWidth = 60;
    int squareHeight = 60;
    double x = 0, y = 0, velX = 0, velY = 0;
    int squareYLocation = -squareHeight;
    private ImageIcon lpp;
    private ImageIcon upp;
    private ImageIcon dpp;
    private ImageIcon rpp;
    

    boolean numberCreated = false;
    static boolean gameRunning = false;

    //generates a random Y value inside the window for the square to spawn at
    public void generateRandomNumber() {
        lpp = new ImageIcon ("r.png");
        upp = new ImageIcon ("u.png");
        dpp = new ImageIcon ("d.png");
        rpp = new ImageIcon ("l.png");
        Random rand = new Random();
        randNumb = rand.nextInt(5);
        numberCreated = true;
    }

    //paints a black screen, then paints a rectangle on top of the black screen
    public void paint(Graphics g) {
        g.setColor(Color.white);
        g.fillRect(0, 0, windowWidth, windowHeight);
        g.setColor(Color.black);
        JLabel label1 = new JLabel("Test");
        
        g.drawImage(upp.getImage(), 112, 290, 60, 60, null);
        g.drawImage(dpp.getImage(), 212, 290, 60, 60, null);
        g.drawImage(lpp.getImage(), 312, 290, 60, 60, null);
        g.drawImage(rpp.getImage(), 12, 290, 60, 60, null);
        g.fillRect(425, 20, 75, 329);
        
        if(randNumb == 1){
        g.fillRect(12, squareYLocation, squareWidth, squareHeight);
        }
        if(randNumb == 2){
        g.fillRect(112, squareYLocation, squareWidth, squareHeight);
        }
        if(randNumb == 3){
        g.fillRect(212, squareYLocation, squareWidth, squareHeight);
        }
        if(randNumb == 4){
        g.fillRect(312, squareYLocation, squareWidth, squareHeight);
        }
    }
    public void up(){
        System.out.println("hi");
        
    }

    public void down(){
        System.out.println("hi");
        
    }

    public void left(){
        System.out.println("hi");
        
    }

    public void right(){
      System.out.println("hi");
        
       
    }


    public void update() {

        //calls the generateRandomNumber() method which gives the square a random x value inside the screen
        if (!numberCreated) {
            generateRandomNumber();
        }
        //moves the squares y coordinate towards the bottom of the screen and stops once it hits the bottom
        if (squareYLocation <= windowHeight) {
            squareYLocation++;

            //resets the x and y location to a new position
        } else {
            numberCreated = false;
            squareYLocation = -squareHeight;
        }
    }

    //sets the while loop to true to start the game
    public void start() {
        gameRunning = true;
    }

    public static void main(String[] args) throws InterruptedException {

        App game = new App();
        JFrame frame = new JFrame();
        frame.add(game);
        frame.setVisible(true);
        frame.setSize(windowWidth, windowHeight);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setTitle("Raining Squares");
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
//add this line
        frame.addKeyListener(this);

        game.start();

        //updates square position, repaints square, and slows down update and paint speed.
        while (gameRunning) {
            game.update();
            game.repaint();
            Thread.sleep(1);
            
        }
    }
    public void keyPressed(KeyEvent e){
        int code = e.getKeyCode();

        while(code == KeyEvent.VK_UP){
            up();
        }

        if (code == KeyEvent.VK_DOWN){
            down();
        }

        if (code == KeyEvent.VK_LEFT){
            left();
        }

        if (code == KeyEvent.VK_RIGHT){
            right();
        }
    }

    public void keyTyped(KeyEvent e){}

    public void keyReleased(KeyEvent e){

      velX = 0;
      velY = 0;
        int code = e.getKeyCode();

        if (code == KeyEvent.VK_UP){
            velY = 0;
        }
        if (code == KeyEvent.VK_DOWN){
            velY = 0;
        }
        if (code == KeyEvent.VK_LEFT){
            velX = 0;
        }
        if (code == KeyEvent.VK_RIGHT){
            velX = 0;
        }
    }
}

import javax.swing.*;
import java.awt.*;
import java.util.Random;
import javax.swing.*;
import java.awt.Graphics;
import java.awt.*;
import java.awt.event.*;
import javax.swing.ImageIcon;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import javax.swing.JPanel;
import javax.swing.Timer;


public class App extends JPanel {

    //changing these values will change the size of the game, while still remaining functional
    //within the size limit specified.
    static int windowWidth = 525;8
    static int windowHeight = 400;
    JLabel label1 = new JLabel("Test");
    int randNumb = 0;
    int squareWidth = 60;
    int squareHeight = 60;
    double x = 0, y = 0, velX = 0, velY = 0;
    int squareYLocation = -squareHeight;
    private ImageIcon lpp;
    private ImageIcon upp;
    private ImageIcon dpp;
    private ImageIcon rpp;
    

    boolean numberCreated = false;
    static boolean gameRunning = false;

    //generates a random Y value inside the window for the square to spawn at
    public void generateRandomNumber() {
        lpp = new ImageIcon ("r.png");
        upp = new ImageIcon ("u.png");
        dpp = new ImageIcon ("d.png");
        rpp = new ImageIcon ("l.png");
        Random rand = new Random();
        randNumb = rand.nextInt(5);
        numberCreated = true;
    }

    //paints a black screen, then paints a rectangle on top of the black screen
    public void paint(Graphics g) {
        g.setColor(Color.white);
        g.fillRect(0, 0, windowWidth, windowHeight);
        g.setColor(Color.black);
        JLabel label1 = new JLabel("Test");
        
        g.drawImage(upp.getImage(), 112, 290, 60, 60, null);
        g.drawImage(dpp.getImage(), 212, 290, 60, 60, null);
        g.drawImage(lpp.getImage(), 312, 290, 60, 60, null);
        g.drawImage(rpp.getImage(), 12, 290, 60, 60, null);
        g.fillRect(425, 20, 75, 329);
        
        if(randNumb == 1){
        g.fillRect(12, squareYLocation, squareWidth, squareHeight);
        }
        if(randNumb == 2){
        g.fillRect(112, squareYLocation, squareWidth, squareHeight);
        }
        if(randNumb == 3){
        g.fillRect(212, squareYLocation, squareWidth, squareHeight);
        }
        if(randNumb == 4){
        g.fillRect(312, squareYLocation, squareWidth, squareHeight);
        }
    }
    public void up(){
        System.out.println("hi");
        
    }

    public void down(){
        System.out.println("hi");
        
    }

    public void left(){
        System.out.println("hi");
        
    }

    public void right(){
      System.out.println("hi");
        
       
    }


    public void update() {

        //calls the generateRandomNumber() method which gives the square a random x value inside the screen
        if (!numberCreated) {
            generateRandomNumber();
        }
        //moves the squares y coordinate towards the bottom of the screen and stops once it hits the bottom
        if (squareYLocation <= windowHeight) {
            squareYLocation++;

            //resets the x and y location to a new position
        } else {
            numberCreated = false;
            squareYLocation = -squareHeight;
        }
    }

    //sets the while loop to true to start the game
    public void start() {
        gameRunning = true;
    }

    public static void main(String[] args) throws InterruptedException {

        App game = new App();
        JFrame frame = new JFrame();
        frame.add(game);
        frame.setVisible(true);
        frame.setSize(windowWidth, windowHeight);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setTitle("Raining Squares");
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);

        game.start();

        //updates square position, repaints square, and slows down update and paint speed.
        while (gameRunning) {
            game.update();
            game.repaint();
            Thread.sleep(1);
            
        }
    }
    public void keyPressed(KeyEvent e){
        int code = e.getKeyCode();

        while(code == KeyEvent.VK_UP){
            up();
        }

        if (code == KeyEvent.VK_DOWN){
            down();
        }

        if (code == KeyEvent.VK_LEFT){
            left();
        }

        if (code == KeyEvent.VK_RIGHT){
            right();
        }
    }

    public void keyTyped(KeyEvent e){}

    public void keyReleased(KeyEvent e){

      velX = 0;
      velY = 0;
        int code = e.getKeyCode();

        if (code == KeyEvent.VK_UP){
            velY = 0;
        }
        if (code == KeyEvent.VK_DOWN){
            velY = 0;
        }
        if (code == KeyEvent.VK_LEFT){
            velX = 0;
        }
        if (code == KeyEvent.VK_RIGHT){
            velX = 0;
        }
    }
}

I'm tryinh to make ddr in java as a school assignment. I've made a key listener and everything, but when i press the keys that activate their respective method, nothing happens

 

Thanks in advance

Edited by WaffleMage
Forgot cofe tabs
Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, WaffleMage said:

<Snip>

Thanks in advance

Put it in code tags please, much easier for us to help you when you do that. theres a pinned sticky thread at the top of this subform for more info. either that or it just looks really goofy on my system lol

Main Rig:

| 13900K@6.1/4.7 w/TVB | Corsair h150i Elite LCD | Sapphire NITRO SE+ 6900XT@2710MHz | ASUS Z790 Strix-E | Corsair DDR5 Dominator Platinum 2x16GB@6200MT/s | Lian Li O11 Dynamic EVO |

My Folding Stats

 

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, MysticalRainXIV said:

Put it in code tags please, much easier for us to help you when you do that. theres a pinned sticky thread at the top of this subform for more info

Sorry, forgot to, just did it

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, WaffleMage said:

Sorry, forgot to, just did ot

Much better, Now hopefully someone who knows java can help you because to be honest I cant get a good grasp of it.

Main Rig:

| 13900K@6.1/4.7 w/TVB | Corsair h150i Elite LCD | Sapphire NITRO SE+ 6900XT@2710MHz | ASUS Z790 Strix-E | Corsair DDR5 Dominator Platinum 2x16GB@6200MT/s | Lian Li O11 Dynamic EVO |

My Folding Stats

 

Link to comment
Share on other sites

Link to post
Share on other sites

you've got no listener, you throw the event but there's no one to take care of it.

The fast way to do this is to add this code to main:

KeyListener keyListener=new KeyListener(){
            @Override
            public void keyTyped(KeyEvent ke) {
                
            }

            @Override
            public void keyPressed(KeyEvent ke) {
                
            }

            @Override
            public void keyReleased(KeyEvent ke) {
                
            }
            
};
frame.add(keyListener)

and get rid of the key events you've got below.

 

This I can't remember for sure, but you might need to add the keyListener to the game object instead of the frame. Try one of them and if it does not work try the other one

The best way to measure the quality of a piece of code is "Oh F*** "s per line

Link to comment
Share on other sites

Link to post
Share on other sites

What is ddr?

Running Arch with i3-gaps on a Thinkpad X1 Extreme
Data Science Postgrad

 

Link to comment
Share on other sites

Link to post
Share on other sites

All you have to do is implement KeyListener and add it to the frame.

Here's how to do it:

import javax.swing.*;
import java.awt.*;
import java.util.Random;
import javax.swing.*;
import java.awt.Graphics;
import java.awt.*;
import java.awt.event.*;
import javax.swing.ImageIcon;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import javax.swing.JPanel;
import javax.swing.Timer;

//add implements KeyListener
public class App extends JPanel implements KeyListener {

    //changing these values will change the size of the game, while still remaining functional
    //within the size limit specified.
    static int windowWidth = 525;8
    static int windowHeight = 400;
    JLabel label1 = new JLabel("Test");
    int randNumb = 0;
    int squareWidth = 60;
    int squareHeight = 60;
    double x = 0, y = 0, velX = 0, velY = 0;
    int squareYLocation = -squareHeight;
    private ImageIcon lpp;
    private ImageIcon upp;
    private ImageIcon dpp;
    private ImageIcon rpp;
    

    boolean numberCreated = false;
    static boolean gameRunning = false;

    //generates a random Y value inside the window for the square to spawn at
    public void generateRandomNumber() {
        lpp = new ImageIcon ("r.png");
        upp = new ImageIcon ("u.png");
        dpp = new ImageIcon ("d.png");
        rpp = new ImageIcon ("l.png");
        Random rand = new Random();
        randNumb = rand.nextInt(5);
        numberCreated = true;
    }

    //paints a black screen, then paints a rectangle on top of the black screen
    public void paint(Graphics g) {
        g.setColor(Color.white);
        g.fillRect(0, 0, windowWidth, windowHeight);
        g.setColor(Color.black);
        JLabel label1 = new JLabel("Test");
        
        g.drawImage(upp.getImage(), 112, 290, 60, 60, null);
        g.drawImage(dpp.getImage(), 212, 290, 60, 60, null);
        g.drawImage(lpp.getImage(), 312, 290, 60, 60, null);
        g.drawImage(rpp.getImage(), 12, 290, 60, 60, null);
        g.fillRect(425, 20, 75, 329);
        
        if(randNumb == 1){
        g.fillRect(12, squareYLocation, squareWidth, squareHeight);
        }
        if(randNumb == 2){
        g.fillRect(112, squareYLocation, squareWidth, squareHeight);
        }
        if(randNumb == 3){
        g.fillRect(212, squareYLocation, squareWidth, squareHeight);
        }
        if(randNumb == 4){
        g.fillRect(312, squareYLocation, squareWidth, squareHeight);
        }
    }
    public void up(){
        System.out.println("hi");
        
    }

    public void down(){
        System.out.println("hi");
        
    }

    public void left(){
        System.out.println("hi");
        
    }

    public void right(){
      System.out.println("hi");
        
       
    }


    public void update() {

        //calls the generateRandomNumber() method which gives the square a random x value inside the screen
        if (!numberCreated) {
            generateRandomNumber();
        }
        //moves the squares y coordinate towards the bottom of the screen and stops once it hits the bottom
        if (squareYLocation <= windowHeight) {
            squareYLocation++;

            //resets the x and y location to a new position
        } else {
            numberCreated = false;
            squareYLocation = -squareHeight;
        }
    }

    //sets the while loop to true to start the game
    public void start() {
        gameRunning = true;
    }

    public static void main(String[] args) throws InterruptedException {

        App game = new App();
        JFrame frame = new JFrame();
        frame.add(game);
        frame.setVisible(true);
        frame.setSize(windowWidth, windowHeight);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setTitle("Raining Squares");
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
//add this line
        frame.addKeyListener(this);

        game.start();

        //updates square position, repaints square, and slows down update and paint speed.
        while (gameRunning) {
            game.update();
            game.repaint();
            Thread.sleep(1);
            
        }
    }
    public void keyPressed(KeyEvent e){
        int code = e.getKeyCode();

        while(code == KeyEvent.VK_UP){
            up();
        }

        if (code == KeyEvent.VK_DOWN){
            down();
        }

        if (code == KeyEvent.VK_LEFT){
            left();
        }

        if (code == KeyEvent.VK_RIGHT){
            right();
        }
    }

    public void keyTyped(KeyEvent e){}

    public void keyReleased(KeyEvent e){

      velX = 0;
      velY = 0;
        int code = e.getKeyCode();

        if (code == KeyEvent.VK_UP){
            velY = 0;
        }
        if (code == KeyEvent.VK_DOWN){
            velY = 0;
        }
        if (code == KeyEvent.VK_LEFT){
            velX = 0;
        }
        if (code == KeyEvent.VK_RIGHT){
            velX = 0;
        }
    }
}
Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×