I need to expand the following class into two classes. The task it accomplishes
ID: 3716605 • Letter: I
Question
I need to expand the following class into two classes. The task it accomplishes will be the same; the additional class will contain the state of one of the buttons in the array of JButtons "buttons". I am running into problems separating the methods without changing the entrire code block. Any suggestions are appreciated!
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;
@SuppressWarnings("serial")
public class MineFieldPanel extends JPanel implements ActionListener {
private static final int DELAY = 250;
private JButton[][] buttons;
public Boolean[][] mines;
public RandomWalk walk;
public int gridSize;
private int lives, score, index;
private int previousX = 0, previousY = 0;
private Timer animationTimer;
private boolean state;
ArrayList<Point> path;
JLabel livesLabel;
JLabel scoreLabel;
JPanel scorePanel;
final Color DEFAULT_COLOR = new JButton().getBackground();
final Color RED = new Color(230, 25, 75);
final Color ORANGE = new Color(245, 130, 48);
final Color YELLOW = new Color(255, 225, 25);
final Color GREEN = new Color(60, 180, 75);
final Color BLUE = new Color(0, 130, 200);
final Color INDIGO = new Color(145, 30, 180);
final Color VIOLET = new Color(100, 105, 255);
Color color;
Color[] colors = { color, DEFAULT_COLOR };
public MineFieldPanel(int gridSize, Boolean[][] mines, RandomWalk walk, ArrayList<Point> path) {
buttons = new JButton[gridSize][gridSize];
this.path = path;
this.mines = mines;
this.gridSize = gridSize;
this.walk = walk;
this.state = false;
lives = 5;
score = 500;
index = 0;
previousX = gridSize - 1;
previousY = 0;
this.animationTimer = new Timer(DELAY, new TimerActionListener());
JPanel main = new JPanel();
main.setPreferredSize(new Dimension(500, 500));
//grid layout
main.setBackground(Color.BLACK);
main.setLayout(new GridLayout(gridSize, gridSize));
for (int i = 0; i < buttons.length; i++) {
for (int j = 0; j < buttons[i].length; j++) {
buttons[i][j] = new JButton();
buttons[i][j].setEnabled(false);
buttons[i][j].addActionListener(this);
buttons[i][j].setPreferredSize(new Dimension(20, 20));
main.add(buttons[i][j]);
}
}
buttons[gridSize - 1][0].setEnabled(true);
buttons[gridSize - 1][0].setBackground(INDIGO);
buttons[0][gridSize - 1].setBackground(VIOLET);
this.add(main);
JPanel eastPanel = new JPanel();
eastPanel.setPreferredSize(new Dimension(250, 80));
eastPanel.setLayout(new BoxLayout(eastPanel, BoxLayout.PAGE_AXIS));
JPanel scorePanel = new JPanel();
eastPanel.add(scorePanel);
livesLabel = new JLabel("Lives: " + lives);
livesLabel.setHorizontalAlignment(JLabel.CENTER);
livesLabel.setAlignmentX(livesLabel.CENTER_ALIGNMENT);
scoreLabel = new JLabel("Score: " + score);
scoreLabel.setHorizontalAlignment(JLabel.CENTER);
scoreLabel.setAlignmentX(scoreLabel.CENTER_ALIGNMENT);
scorePanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLoweredBevelBorder(), BorderFactory.createEmptyBorder(10, 10, 10, 10)));
scorePanel.add(livesLabel);
scorePanel.add(scoreLabel);
this.add(eastPanel, BorderLayout.EAST);
}
private void setText(JLabel label, final String text) {
if (label != null) {
label.setText(text);
label.paintImmediately(label.getVisibleRect());
revalidate();
repaint();
}
}
public void showMines() {
for (int i = 0; i < mines.length; i++) {
for (int j = 0; j < mines.length; j++) {
if (mines[i][j] == true && buttons[i][j].getBackground() != Color.BLACK) {
buttons[i][j].setBackground(Color.WHITE);
}
}
}
}
public void hideMines() {
for (int i = 0; i < mines.length; i++) {
for (int j = 0; j < mines.length; j++) {
if (buttons[i][j].getBackground() == Color.GRAY && mines[i][j] == true) {
buttons[i][j].setBackground(DEFAULT_COLOR);
}
}
}
}
public void disableAll() {
for (int i = 0; i < mines.length; i++) {
for (int j = 0; j < mines.length; j++) {
buttons[i][j].setEnabled(false);
}
}
}
public void showHidePath() {
for (Point p : path) {
if (buttons[p.x][p.y].getBackground() == BLUE) {
buttons[p.x][p.y].setBackground(DEFAULT_COLOR);
} else if (buttons[p.x][p.y].getBackground() == DEFAULT_COLOR) {
buttons[p.x][p.y].setBackground(BLUE);
}
}
}
private void animateFlash(int x, int y, Color color) {
this.previousX = x;
this.previousY = y;
this.color = color;
buttons[x][y].setBackground(colors[index]);
index = (index + 1) % colors.length;
}
/**
* Performs action when timer event fires.
*/
private class TimerActionListener implements ActionListener {
public void actionPerformed(ActionEvent evt) {
animateFlash(previousX, previousY, color);
revalidate();
repaint();
}
}
/* Public Methods */
/**
* Check whether the button is currently animating
*
* @return true if animation is active, false if not
*/
public boolean isActive() {
return this.animationTimer.isRunning();
}
/**
* Create an animation thread that runs periodically
*/
private void startAnimation() {
if (state == true && !this.animationTimer.isRunning()) {
this.animationTimer.start();
//toggleSliders();
} else if (state == false) {
this.animationTimer.stop();
//toggleSliders();
}
}
public void actionPerformed(ActionEvent ae) {
for (int i = 0; i < buttons.length; i++) {
for (int j = 0; j < buttons.length; j++) {
// button buttons[i][j] was clicked
if (ae.getSource() == buttons[i][j] && buttons[i][j].getBackground() != Color.BLACK) {
buttons[previousX][previousY].setBackground(colors[0]);
index = 0;
state = true;
int up = 0, down = 0, left = 0, right = 0;
startAnimation();
disableAll();
if (i - 1 >= 0) {
buttons[i - 1][j].setEnabled(true);
up = (mines[i - 1][j]) ? 1 : 0;
}
if (i + 1 <= buttons.length - 1) {
buttons[i + 1][j].setEnabled(true);
down = (mines[i + 1][j]) ? 1 : 0;
}
if (j - 1 >= 0) {
buttons[i][j - 1].setEnabled(true);
left = (mines[i][j - 1]) ? 1 : 0;
}
if (j + 1 <= buttons.length - 1) {
buttons[i][j + 1].setEnabled(true);
right = (mines[i][j + 1]) ? 1 : 0;
}
int sum = up + down + left + right;
if (mines[i][j] == true) {
buttons[i][j].setBackground(Color.BLACK);
buttons[previousX][previousY].doClick();
buttons[i][j].setEnabled(true);
score -= 100;
lives -= 1;
} else if (sum >= 3) {
buttons[i][j].setBackground(RED);
score -= 1;
} else if (sum == 2) {
buttons[i][j].setBackground(ORANGE);
score -= 1;
} else if (sum == 1) {
buttons[i][j].setBackground(YELLOW);
score -= 1;
} else {
buttons[i][j].setBackground(GREEN);
score -= 1;
}
if (buttons[i][j].getBackground() != Color.BLACK) {
colors[0] = buttons[i][j].getBackground();
previousX = i;
previousY = j;
}
setText(livesLabel, "Lives: " + lives);
setText(scoreLabel, "Score: " + score);
if (lives == 0 || score <= 0) {
this.animationTimer.stop();
disableAll();
showMines();
Component frame = null;
JOptionPane.showMessageDialog(frame, "You Lose! Your final score is " + score + ".");
}
if (i == 0 && j == gridSize - 1) {
this.animationTimer.stop();
disableAll();
showMines();
Component frame = null;
if (lives > 1) {
JOptionPane.showMessageDialog(frame, "You made it! You had " + lives
+ " lives remaing and your final score is " + score + ".");
} else {
JOptionPane.showMessageDialog(frame,
"You made it! You only had one life remaining and your final score is " + score
+ ".");
}
}
}
}
}
}
}
Explanation / Answer
Code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;
class Buttonss
{
public JButton[][] getButtons() {
return buttons;
}
public Buttonss(int gridSize) {
this.buttons = new JButton[gridSize][gridSize];
}
private JButton[][] buttons;
}
class MineFieldPanel extends JPanel implements ActionListener {
private static final int DELAY = 250;
public Boolean[][] mines;
public RandomWalk walk;
public int gridSize;
private int lives, score, index;
private int previousX = 0, previousY = 0;
private Timer animationTimer;
private boolean state;
ArrayList<Point> path;
JLabel livesLabel;
JLabel scoreLabel;
JPanel scorePanel;
Buttonss b;
final Color DEFAULT_COLOR = new JButton().getBackground();
final Color RED = new Color(230, 25, 75);
final Color ORANGE = new Color(245, 130, 48);
final Color YELLOW = new Color(255, 225, 25);
final Color GREEN = new Color(60, 180, 75);
final Color BLUE = new Color(0, 130, 200);
final Color INDIGO = new Color(145, 30, 180);
final Color VIOLET = new Color(100, 105, 255);
Color color;
Color[] colors = { color, DEFAULT_COLOR };
public MineFieldPanel(int gridSize, Boolean[][] mines, RandomWalk walk, ArrayList<Point> path) {
b=new Buttonss(gridSize);
this.path = path;
this.mines = mines;
this.gridSize = gridSize;
this.walk = walk;
this.state = false;
lives = 5;
score = 500;
index = 0;
previousX = gridSize - 1;
previousY = 0;
this.animationTimer = new Timer(DELAY, new TimerActionListener());
JPanel main = new JPanel();
main.setPreferredSize(new Dimension(500, 500));
//grid layout
main.setBackground(Color.BLACK);
main.setLayout(new GridLayout(gridSize, gridSize));
JButton buttons[][]=b.getButtons();
for (int i = 0; i < buttons.length; i++) {
for (int j = 0; j < buttons[i].length; j++) {
buttons[i][j] = new JButton();
buttons[i][j].setEnabled(false);
buttons[i][j].addActionListener(this);
buttons[i][j].setPreferredSize(new Dimension(20, 20));
main.add(buttons[i][j]);
}
}
buttons[gridSize - 1][0].setEnabled(true);
buttons[gridSize - 1][0].setBackground(INDIGO);
buttons[0][gridSize - 1].setBackground(VIOLET);
this.add(main);
JPanel eastPanel = new JPanel();
eastPanel.setPreferredSize(new Dimension(250, 80));
eastPanel.setLayout(new BoxLayout(eastPanel, BoxLayout.PAGE_AXIS));
JPanel scorePanel = new JPanel();
eastPanel.add(scorePanel);
livesLabel = new JLabel("Lives: " + lives);
livesLabel.setHorizontalAlignment(JLabel.CENTER);
livesLabel.setAlignmentX(livesLabel.CENTER_ALIGNMENT);
scoreLabel = new JLabel("Score: " + score);
scoreLabel.setHorizontalAlignment(JLabel.CENTER);
scoreLabel.setAlignmentX(scoreLabel.CENTER_ALIGNMENT);
scorePanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLoweredBevelBorder(), BorderFactory.createEmptyBorder(10, 10, 10, 10)));
scorePanel.add(livesLabel);
scorePanel.add(scoreLabel);
this.add(eastPanel, BorderLayout.EAST);
}
private void setText(JLabel label, final String text) {
if (label != null) {
label.setText(text);
label.paintImmediately(label.getVisibleRect());
revalidate();
repaint();
}
}
public void showMines() {
JButton[][] buttons=b.getButtons();
for (int i = 0; i < mines.length; i++) {
for (int j = 0; j < mines.length; j++) {
if (mines[i][j] == true && buttons[i][j].getBackground() != Color.BLACK) {
buttons[i][j].setBackground(Color.WHITE);
}
}
}
}
public void hideMines() {
JButton[][] buttons=b.getButtons();
for (int i = 0; i < mines.length; i++) {
for (int j = 0; j < mines.length; j++) {
if (buttons[i][j].getBackground() == Color.GRAY && mines[i][j] == true) {
buttons[i][j].setBackground(DEFAULT_COLOR);
}
}
}
}
public void disableAll() {
JButton[][] buttons=b.getButtons();
for (int i = 0; i < mines.length; i++) {
for (int j = 0; j < mines.length; j++) {
buttons[i][j].setEnabled(false);
}
}
}
public void showHidePath() {
JButton[][] buttons=b.getButtons();
for (Point p : path) {
if (buttons[p.x][p.y].getBackground() == BLUE) {
buttons[p.x][p.y].setBackground(DEFAULT_COLOR);
} else if (buttons[p.x][p.y].getBackground() == DEFAULT_COLOR) {
buttons[p.x][p.y].setBackground(BLUE);
}
}
}
private void animateFlash(int x, int y, Color color) {
this.previousX = x;
this.previousY = y;
JButton[][] buttons=b.getButtons();
this.color = color;
buttons[x][y].setBackground(colors[index]);
index = (index + 1) % colors.length;
}
/**
* Performs action when timer event fires.
*/
private class TimerActionListener implements ActionListener {
public void actionPerformed(ActionEvent evt) {
animateFlash(previousX, previousY, color);
revalidate();
repaint();
}
}
/* Public Methods */
/**
* Check whether the button is currently animating
*
* @return true if animation is active, false if not
*/
public boolean isActive() {
return this.animationTimer.isRunning();
}
/**
* Create an animation thread that runs periodically
*/
private void startAnimation() {
if (state == true && !this.animationTimer.isRunning()) {
this.animationTimer.start();
//toggleSliders();
} else if (state == false) {
this.animationTimer.stop();
//toggleSliders();
}
}
public void actionPerformed(ActionEvent ae) {
JButton[][] buttons=b.getButtons();
for (int i = 0; i < buttons.length; i++) {
for (int j = 0; j < buttons.length; j++) {
// button buttons[i][j] was clicked
if (ae.getSource() == buttons[i][j] && buttons[i][j].getBackground() != Color.BLACK) {
buttons[previousX][previousY].setBackground(colors[0]);
index = 0;
state = true;
int up = 0, down = 0, left = 0, right = 0;
startAnimation();
disableAll();
if (i - 1 >= 0) {
buttons[i - 1][j].setEnabled(true);
up = (mines[i - 1][j]) ? 1 : 0;
}
if (i + 1 <= buttons.length - 1) {
buttons[i + 1][j].setEnabled(true);
down = (mines[i + 1][j]) ? 1 : 0;
}
if (j - 1 >= 0) {
buttons[i][j - 1].setEnabled(true);
left = (mines[i][j - 1]) ? 1 : 0;
}
if (j + 1 <= buttons.length - 1) {
buttons[i][j + 1].setEnabled(true);
right = (mines[i][j + 1]) ? 1 : 0;
}
int sum = up + down + left + right;
if (mines[i][j] == true) {
buttons[i][j].setBackground(Color.BLACK);
buttons[previousX][previousY].doClick();
buttons[i][j].setEnabled(true);
score -= 100;
lives -= 1;
} else if (sum >= 3) {
buttons[i][j].setBackground(RED);
score -= 1;
} else if (sum == 2) {
buttons[i][j].setBackground(ORANGE);
score -= 1;
} else if (sum == 1) {
buttons[i][j].setBackground(YELLOW);
score -= 1;
} else {
buttons[i][j].setBackground(GREEN);
score -= 1;
}
if (buttons[i][j].getBackground() != Color.BLACK) {
colors[0] = buttons[i][j].getBackground();
previousX = i;
previousY = j;
}
setText(livesLabel, "Lives: " + lives);
setText(scoreLabel, "Score: " + score);
if (lives == 0 || score <= 0) {
this.animationTimer.stop();
disableAll();
showMines();
Component frame = null;
JOptionPane.showMessageDialog(frame, "You Lose! Your final score is " + score + ".");
}
if (i == 0 && j == gridSize - 1) {
this.animationTimer.stop();
disableAll();
showMines();
Component frame = null;
if (lives > 1) {
JOptionPane.showMessageDialog(frame, "You made it! You had " + lives
+ " lives remaing and your final score is " + score + ".");
} else {
JOptionPane.showMessageDialog(frame,
"You made it! You only had one life remaining and your final score is " + score
+ ".");
}
}
}
}
}
}
}