0

I want to run the snake class after the start button its pressed the programg works until i press the start button and try to move the rectangle around the window, there is the problem the rectangle wont move at all. Please help. Thanks.

public class Main {
public static void main(String [] args) {
MenuScreen MenuScreen = new MenuScreen();
MenuScreen.menuscreen();
}
    }

The Screen Menu class

import java.awt.Color;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

    public class MenuScreen {

        Snake Snake = new Snake();

    public  void menuscreen () {

            //Window
        final JFrame window = new JFrame();
        window.setVisible(true);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setSize(800, 600);
        window.setLocationRelativeTo(null);

            //Panel
        JPanel panel = new JPanel();
        window.getContentPane().add(panel);
        panel.setBackground(Color.BLACK);
        panel.setVisible(true);
        panel.setLayout(null);

            //Title 
        JLabel label = new JLabel("SNAKE");
        panel.add(label);
        label.setBounds(290, 5, 204, 73);
        label.setFont(new Font("Tahoma", Font.BOLD, 60));
        label.setHorizontalAlignment(SwingConstants.CENTER);
        label.setForeground(Color.RED);
        label.setBackground(Color.red);

            //Buttons
            //start button
        JButton bstart = new JButton ("Start");
        panel.add(bstart);
        bstart.setBounds(424, 200, 70, 60);
        bstart.setVisible(true);

        //start button - run snake on click
        bstart.addActionListener(new ActionListener() 
              {
                public void actionPerformed(ActionEvent e)
            {

            window.getContentPane().removeAll();
            Snake Snake = new Snake();
            window.add(Snake);
            window.repaint();
            window.revalidate();

       }
              });

            //exit button
        JButton bexit = new JButton ("Exit");
        panel.add(bexit);
        bexit.setBounds(290, 200, 70, 60);
        bexit.setVisible(true);
            //exit button - close on click
        bexit.addActionListener(new ActionListener() 
              {
                public void actionPerformed(ActionEvent e)
            {
                System.exit(0);
            }
              });

    }
}

The Snake class, when I click start the frame gets cleared and shows the rect but when I try to move the rect aroud the screen it wont move.

    public class Snake extends JPanel implements ActionListener, KeyListener {
     public void thicks () {}

    public Snake () {

     addKeyListener(this);
     setFocusable(true);
     setFocusTraversalKeysEnabled(false);

    }

    private static final long serialVersionUID = 1L;


    Timer FPS = new Timer( 5, this);
    double x =0 , y = 0, SpeedX = 0, SpeedY = 0;



    public void paint(Graphics g) {
        FPS.start();
        super.paintComponent(g);
        g.setColor(Color.blue);
        Graphics2D graphics  = (Graphics2D) g;

        Rectangle2D rect = new Rectangle2D.Double(x, y, 40, 40);
        graphics.fill(rect);


        }

    public void actionPerformed(ActionEvent e) {    
    if(x < 0 ||  x > 740 ) {
        SpeedX= -SpeedX;
    }

    if(y < 0 || y > 520) {
        SpeedY = -SpeedY;
            }

    x += SpeedX;
    y += SpeedY;
    repaint();
}
public void down() {
        SpeedY = 2;
        SpeedX =  0;

    }

    public void up() {
        SpeedY = -2;
        SpeedX = 0;     
    }

    public void right() {
        SpeedY = 0;
        SpeedX = 2; 

    }

    public void left() {
        SpeedY =  0;
        SpeedX = -2;    
    }

    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();

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

        if(keyCode == KeyEvent.VK_UP) {
            up();
        }

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

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

    }

    public void keyReleased(KeyEvent e) {}
    public void keyTyped(KeyEvent e) {}

   }
Frakcool
  • 10,915
  • 9
  • 50
  • 89
  • 1) Don't use `null` layouts. 2) Don't use KeyListeners, use KeyBindings instead. 3) Follow Java Naming Conventions (variable names should start with lower case letters). 4) Please indent your code correctly. Right now I can't compile your program (on the phone) but if you indent your code correctly I might give it a try later – Frakcool Mar 19 '19 at 21:24
  • Also `public void paint(Graphics g)` should be `public void paintComponenet(Graphics g)`. For more help post [mcve] – c0der Mar 20 '19 at 09:16
  • Hi, thanks for the help, but could you show me an example of keybinding i have been looking for examples but they dont work on this program. Sry am new to programming. The thing is that if I run only the snake class without the screenmenu class the program works fine but when i add the screenmenu and press start then the rect wont move – Melkree Mar 20 '19 at 13:06
  • Here's a [question](https://stackoverflow.com/q/44550511/2180785) with an accepted answer, that answer leads to an example. But I recommend reading both posts with their answers – Frakcool Mar 20 '19 at 16:33

0 Answers0