JAVA GUI GAME help Object Oriented Board Game with GUI The objective of this lab
ID: 3555300 • Letter: J
Question
JAVA GUI GAME help
Object Oriented Board Game with GUI
The objective of this lab is to expand on the Graphical User Interface. You will get the opportunity to work with interfaces, listeners, Inner classes and components which are modified during run time.
Enjoy - this should be a fun exercise!
(upload the source code and the .class files!)
Build the frame with the components, listeners and inner classes needed for the GUI. Name this new frame class: BoardGameWindow.
You will be using the GUI you build to manage all input and output instead of external files. Connect the BoardGameWindow class to your BoardGame class. Add any methods that are needed to BoardGame and the other classes to provide the text needed to display the information you need on your GUI. Your GUI should look as close to this as possible:
Notes:
Explanation / Answer
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
public class BoardGame {
static ArrayList<String> players;
static String[] names = { "Blank", "Blank", "Star", "Blank", "Blank",
"Blank", "Star", "Lightining", "Blank", "X", "Blank", "Blank",
"Blank", "Lightning", "Blank", "Blank", "Blank", "Blank", "Blank",
"Smiley", "Star", "Finish" };
static int[] scores = { 0, 0, 5, 0, 0, 0, 5, 15, 0, 8, 0, 0, 0, 15, 0, 0,
0, 0, 0, 10, 5, 0 };
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame = new JFrame("Board Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.setSize(500, 600);
frame.setResizable(false);
JPanel topPanel = new JPanel();
topPanel.setLayout(new BorderLayout());
JPanel topInTopPanel = new JPanel();
final JButton newGame = new JButton("New Game");
topInTopPanel.add(newGame);
JLabel label = new JLabel("Player Name");
topInTopPanel.add(label);
final JTextField playerName = new JTextField(20);
playerName.setEnabled(false);
topInTopPanel.add(playerName);
JPanel bottomInTopPanel = new JPanel();
final JButton addPlayer = new JButton("Add Player");
addPlayer.setEnabled(false);
bottomInTopPanel.add(addPlayer);
topPanel.add(bottomInTopPanel, BorderLayout.PAGE_END);
topPanel.add(topInTopPanel, BorderLayout.PAGE_START);
frame.add(topPanel, BorderLayout.PAGE_START);
final JTextArea messages = new JTextArea();
Font font1 = new Font("MonoSpaced", Font.PLAIN, 12);
messages.setFont(font1);
messages.setEditable(false);
JScrollPane scroll = new JScrollPane(messages);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
frame.add(scroll, BorderLayout.CENTER);
JPanel bottomPanel = new JPanel();
final JButton play = new JButton("Play");
play.setEnabled(false);
bottomPanel.add(play);
frame.add(bottomPanel, BorderLayout.PAGE_END);
newGame.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
players = new ArrayList<>();
messages.append("New Game ");
messages.append("-------- ");
playerName.setEnabled(true);
addPlayer.setEnabled(true);
newGame.setEnabled(false);
play.setEnabled(true);
}
});
frame.setVisible(true);
addPlayer.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
String name = playerName.getText().trim();
if (name != null && !name.equalsIgnoreCase("")) {
players.add(name);
messages.append("Player aded: " + name + " ( Player-"
+ players.size() + " ) ");
}
}
});
play.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
messages.append(" Playing ");
messages.append("-------- ");
playerName.setEnabled(false);
addPlayer.setEnabled(false);
play.setEnabled(false);
ArrayList<Integer> playerScores = new ArrayList<>();
int currPosition, die, currPoints;
Random rand = new Random();
int maxScore = 0, maxScorePos = 0;
if (players.size() < 2) {
messages.append("Min number of players is two ");
} else {
for (int counter = 0; counter < players.size(); counter++) {
currPosition = 1;
currPoints = 0;
messages.append(" Player-" + (counter + 1) + " "
+ players.get(counter) + " playing ");
while (currPosition < 22) {
die = rand.nextInt(6 < (22 - currPosition) ? 6
: (22 - currPosition)) + 1;
currPosition += die;
currPoints += scores[currPosition - 1];
messages.append("Player-" + (counter + 1) + ": "
+ players.get(counter) + " Rolled " + die
+ " and landed on "
+ names[currPosition - 1] + " ( #"
+ currPosition + " ) ");
messages.append(" Player-" + (counter + 1) + ": "
+ players.get(counter) + " has "
+ currPoints + " points ");
}
if (currPoints > maxScore) {
maxScore = currPoints;
maxScorePos = counter;
}
}
}
messages.append(" Player-" + (maxScorePos + 1) + ": "
+ players.get(maxScorePos) + " wins with " + maxScore
+ " points ");
messages.append(" ");
newGame.setEnabled(true);
}
});
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.