Question: Fix JAVA game. Code is written in BlueJ and working, except for following problems. Issues with the code: 1) Play a Game button , should
Fix JAVA game. Code is written in BlueJ and working, except for following problems.
Issues with the code:
1) Play a Game button , should restart the game at any moment and be ready for new round.
2) Win or Lose message at the end of the game should appear on green panel, saying for example "You Win! Your score is 5"
3) Easy Intermediate Difficult buttons should arm red pannels with "bombs" 3, 5 and 10 panels respectively.
_______________________________________________________________________________________
import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.Random;
import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel;
public class PanelA extends JPanel{
private JPanel[][] squares; private int score=0;
public int x=0; public int y=0;
protected int level2; public PanelA() { setLayout(new GridLayout(2, 5)); squares = new JPanel[2][5]; setBackground(Color.WHITE); setPreferredSize(new Dimension(200, 200));
for(int i = 0; i < squares.length; i++) { for(int j = 0; j < squares[i].length; j++) { squares[i][j] = new JPanel(); add(squares[i][j]); final int iCopy = i; final int jCopy = j;
squares[iCopy][jCopy].setBackground(Color.RED); squares[i][j].setBorder(BorderFactory.createLineBorder(Color.WHITE));
squares[i][j].addMouseListener(new MouseAdapter() {
@Override public void mouseClicked(MouseEvent click) { if(squares[iCopy][jCopy].getBackground()!=Color.YELLOW){ squares[iCopy][jCopy].setBackground(Color.YELLOW); score++;
if(score == PanelC.getLevel()){ // if(score == 3){ JOptionPane.showMessageDialog(null, "You Win! Your Score = " + score); System.exit(0); } } }});
} }
Random rand = new Random(); for(int i=0;i<3;i++){ final int x; final int y;
x = rand.nextInt(2); y = rand.nextInt(5);
squares[x][y].addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent click) { squares[x][y].setBackground(Color.BLACK); JOptionPane.showMessageDialog(null, "You lose! Your Score = " + score); System.exit(0); }});
}
}
}
_______________________________________________________________________________________________________________________
import java.util.*; import javax.swing.*; import java.awt.*; import java.awt.event.*;
public class PanelB extends JPanel { private JPanel panelB = new JPanel(); private JButton buttonPlayAgame, buttonExit;
protected static JLabel outputMessage;
public PanelB() { setLayout (new GridLayout()); setBackground(Color.GREEN); setVisible(true); makePanelB();
}
public void makePanelB() { JButton buttonPlayAgame = new JButton("Play a game"); buttonPlayAgame.addActionListener(source -> new MainFrame()); JButton buttonExit = new JButton("Exit"); buttonExit.addActionListener(source -> System.exit(0)); JLabel outputMessage= new JLabel("There should be Win or Lose message");
add(panelB); panelB.add(buttonPlayAgame); panelB.add(buttonExit); panelB.add(outputMessage); panelB.setBackground(Color.GREEN); } }
_________________________________________________________________________________________________________________
import java.util.*; import javax.swing.*; import java.awt.*; import java.awt.event.*;
public class PanelC extends JPanel { private JPanel panelC = new JPanel(); private JButton buttonEasy, buttonIntermediate, buttonDifficult; public static int level = 5;
public PanelC() { setBackground(Color.YELLOW); setLayout (new GridLayout()); setVisible(true); makePanelC();
}
public void makePanelC() { JButton buttonEasy = new JButton("Easy"); buttonEasy.addActionListener(source -> level=7); //this level should arm 3 panels JButton buttonIntermediate = new JButton("Intermediate"); buttonIntermediate.addActionListener(source -> level = 5); //this level should arm 5 panels JButton buttonDifficult = new JButton("Difficult"); buttonDifficult.addActionListener(source -> level = 10); //this level should arm all 10 panels
add(panelC); panelC.add(buttonEasy); panelC.add(buttonIntermediate); panelC.add(buttonDifficult); panelC.setBackground(Color.YELLOW); }
public static int getLevel() { return level; }
}
__________________________________________________________________________________________________________________________
import java.util.*; import javax.swing.*; import java.awt.*; import java.awt.event.*;
public class MainFrame extends JFrame { protected JFrame frame = new JFrame("CHASING BLACK PANELS");
public MainFrame() {
frame.setSize(840, 220); frame.setLayout (new GridLayout()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); makeFrame(); }
public void makeFrame() { frame.add(new PanelA()); frame.add(new PanelB()); frame.add(new PanelC()); } }
__________________________________________________________________________________________________________
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
