hello, i am currently working on a pong game and i wish to input a JButton on a separate Gui running on a different thread to restart the program. The problem which is occurring is that when i press the restart button the key listener fails to respond therefore the 'rackets' do not move, while other processes such as the ball and point incrementation do. Therefore in conclusion, please may you tell me what the problem is, and the steps and the steps necessary for its fixing.
my code is composed of two main classes:
main class:
components class:
my code is composed of two main classes:
main class:
import java.awt.*; import java.awt.event.*; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.SwingUtilities; public class Main extends JFrame { static Thread hilo=new Thread(new setup()); static int pointLimit; private JPanel jContentPane = null; private compoments panel = null; private compoments getPanel() { if (panel == null) { panel = new compoments(); } return panel; } public static int getPointlimit(){ return pointLimit; } public Main() { super(); initialize(); this.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent evt) { formKeyPressed(evt); } public void keyReleased(KeyEvent evt) { formKeyReleased(evt); } }); } private void formKeyPressed(KeyEvent evt) { panel.keyPressed(evt); } private void formKeyReleased(KeyEvent evt) { panel.keyReleased(evt); } private void initialize() { this.setResizable(false); this.setFocusable(true); this.setBounds(new Rectangle(312, 184, 250, 250)); this.setMinimumSize(new Dimension(250, 250)); this.setMaximumSize(new Dimension(250, 250)); this.setContentPane(getJContentPane()); this.setTitle("Pong Made by ryan butler"); } private JPanel getJContentPane() { if (jContentPane == null) { jContentPane = new JPanel(); jContentPane.setLayout(new BorderLayout()); jContentPane.add(getPanel(), BorderLayout.CENTER); } return jContentPane; } @SuppressWarnings("deprecation") public static void main(String[] args) { while(true){ //stop infinite pong games boolean h=hilo.isAlive(); if(h==true){ hilo.stop(); SwingUtilities.invokeLater(new Runnable() { public void run() { Main thisClass = new Main(); thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); thisClass.setVisible(true); } }); break; } } } public static class setup extends JFrame implements ActionListener, Runnable{ private JLabel intro; private JLabel points2; private JTextField points; private JButton finnish; private JLabel a; public setup(){ intro= new JLabel("settings",JLabel.CENTER); points2= new JLabel("enter point limit",JLabel.LEFT); points = new JTextField(30); a= new JLabel("choose difficulty",JLabel.LEFT); finnish=new JButton("finnish"); finnish.addActionListener(this); setVisible(true); setSize(200,200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new GridLayout(6,8,10,10)); setTitle("setup"); add(intro); add(points2); add(points); add(finnish); } public void actionPerformed(ActionEvent e) { System.out.println("test"); Integer point=new Integer(points.getText()); pointLimit=point; setVisible(false); hilo.start(); } @Override public void run() { setup n = new setup(); } } }
components class:
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import javax.swing.*; public class compoments extends JPanel implements Runnable { private static int x; static Thread a =new Thread(new buttonPanel()); static JPanel buttonMenue; private static boolean Restart=false; private int pelotaX = 100, pelotaY = 100, jug1X=10, jug1Y=100, jug2X=230, jug2Y=100; Thread hilo; static int left=-5; static int right=5; static int up=5; static int down=-5; int up2=5; int down2= -5; int width, height; int contPlay1=-1, contPlay2=0; boolean player1FlagArr,player1FlagAba, player2FlagArr, player2FlagAba; boolean juego, gameOver; public compoments(){ juego=true; hilo=new Thread(this); hilo.start(); } public void paintComponent(Graphics gc){ setOpaque(false); super.paintComponent(gc); gc.setColor(Color.black); gc.fillOval(pelotaX, pelotaY, 8,8); gc.fillRect(jug1X, jug1Y, 10, 25); gc.fillRect(jug2X, jug2Y, 10, 25); gc.drawString("PLAYER1: "+contPlay1, 25, 10); gc.drawString("PLAYER2: "+contPlay2, 150, 10); int pointlimit=Main.getPointlimit(); if(gameOver){ if(contPlay1==pointlimit){ gc.drawString("Game Over, player 1 wins", 50, 100); gc.clearRect(jug1X, jug1Y, 10, 25); gc.clearRect(jug2X, jug2Y, 10, 25); } else{ if(contPlay2==pointlimit){ gc.drawString("Game Over, player 2 wins", 50, 100); gc.clearRect(jug1X, jug1Y, 10, 25); gc.clearRect(jug2X, jug2Y, 10, 25); } } } } public void dibujarPelota (int nx, int ny) { pelotaX= nx; pelotaY= ny; this.width=this.getWidth(); this.height=this.getHeight(); repaint(); } public void keyPressed(KeyEvent evt) { switch(evt.getKeyCode()) { case KeyEvent.VK_W : player1FlagArr = true; break; case KeyEvent.VK_S : player1FlagAba = true; break; case KeyEvent.VK_UP: player2FlagArr=true; break; case KeyEvent.VK_DOWN: player2FlagAba=true; break; } } public void keyReleased(KeyEvent evt) { switch(evt.getKeyCode()) { case KeyEvent.VK_W : player1FlagArr = false; break; case KeyEvent.VK_S : player1FlagAba = false; break; // Mover nave 2 case KeyEvent.VK_UP: player2FlagArr=false; break; case KeyEvent.VK_DOWN: player2FlagAba=false; break; } } public void moverPlayer1() { if (player1FlagArr == true && jug1Y >= 0) jug1Y += down2; if (player1FlagAba == true && jug1Y <= (this.getHeight()-25)) jug1Y += up2; dibujarPlayer1(jug1X, jug1Y); } public void moverPlayer2() { if (player2FlagArr == true && jug2Y >= 0) jug2Y += down2; if (player2FlagAba == true && jug2Y <= (this.getHeight()-25)) jug2Y += up2; dibujarPlayer2(jug2X, jug2Y); } public void dibujarPlayer1(int x, int y){ this.jug1X=x; this.jug1Y=y; repaint(); } public void dibujarPlayer2(int x, int y){ this.jug2X=x; this.jug2Y=y; repaint(); } public void run() { // TODO Auto-generated method stub boolean izqDer=false; boolean arrAba=false; while(true){ if(Restart==true){ contPlay1=0; contPlay2=0; pelotaX = 100; pelotaY = 100; jug1X=10; jug1Y=100; jug2X=230; jug2Y=100; left=-5; right=5; up=5; down=-5; juego=true; gameOver=false; Restart=false; } if(juego){ if (izqDer) { pelotaX += right; if (pelotaX >= (width - 8)) izqDer= false; } else { pelotaX += left; if ( pelotaX <= 0) izqDer = true; } if (arrAba) { pelotaY += up; if (pelotaY >= (height - 8)) arrAba= false; } else { pelotaY += down; if ( pelotaY <= 0) arrAba = true; } dibujarPelota(pelotaX, pelotaY); try { Thread.sleep(50); } catch(InterruptedException ex) { } moverPlayer1(); moverPlayer2(); if (pelotaX >= (width - 8)) contPlay1++; if ( pelotaX == 0) contPlay2++; int pointlimit=Main.getPointlimit(); if(contPlay1==pointlimit || contPlay2==pointlimit){ juego=false; gameOver=true; } if(pelotaX==jug1X+10 && pelotaY>=jug1Y && pelotaY<=(jug1Y+25)) izqDer=true; if(pelotaX==(jug2X-5) && pelotaY>=jug2Y && pelotaY<=(jug2Y+25)) izqDer=false; } } } public static class buttonPanel extends JFrame implements ActionListener,Runnable{ private JButton restart; public buttonPanel(){ restart= new JButton("restart"); restart.addActionListener(this); setVisible(true); setSize(200,200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new GridLayout(6,8,10,10)); setTitle("buttonMenue"); add(restart); } @Override public void actionPerformed(ActionEvent arg0) { if(arg0.getSource()==restart){ Restart=true; } } @Override public void run() { buttonPanel g =new buttonPanel(); } } }