Goals The main goal of this assignment is to learn about Java Swing user interfaces. You will build a UI and read documentation on different features you may want to use. Getting Started The structure...


Goals


The main goal of this assignment is to learn about Java Swing user interfaces. You will build a UI and read documentation on different features you may want to use.


Getting Started


The structure of your assignment solution will closely follow that of the SimpleAppListener application from lecture 31 and the lab that extended it. You are encouraged to examine that code carefully and build upon it.


Assignment Requirements


Your task is to make a player versus computer battle game based on the classic game of rock-paper-scissors. If you are unfamiliar with this game, please read about it here - https://en.wikipedia.org/wiki/Rock_paper_scissors (Links to an external site.)


To play on the computer, a person will make a choice from rock, paper, or scissors, the computer makes a choice without knowing the player move, and then a winner is determined (or a tie).


You should implement a Java Swing user interface that implements this.



  1. There should be a collection of buttons representing the player choices of rock, paper, or scissors. Pressing one of the buttons will indicate the player's move and trigger the computer to respond and a winner to be determined.

  2. There should be an area showing the results of one game. Following a player move, the player's choice should be printed, the computer's choice should be printed, and then the winner shown (or a tie).

  3. There should be a way of choosing between two different computer opponents. There should be a random computer player, that randomly chooses from the three possible choices, and a last player choice computer opponent, that always chooses what you played the previous game. This opponent should play rock for its first move. Changing a computer opponent does not count as playing a game - that only happens when a person picks one of the rock, paper, or scissors buttons.


You need to spend some effort laying out the components of the program so that they make sense and look reasonably nice. You are free to learn about other useful parts of Java Swing. Some useful references are



Examples of using button types(Links to an external site.)



How to use Labels(Links to an external site.)



Laying Out Components Within a Container(Links to an external site.)



Modifying the Look and Feel(Links to an external site.)


As a word of caution, many of the button examples in those references use a Button action, which is basically a String, to communicate to the ActionListener method when a button is pressed. In our example code, we have been comparing the pressed object reference with button instance variable references to decide what was pressed.


The basic requirement for the UI is that is looks roughly as good or better than this example, which is not a particularly nice design.


I used a titled border (you can read about it in the links above, or search for "Java swing titled border") to split off each area. The left area is essentially the same panel of JButtons from the lecture code. The middle text is mostly JLabels. Using a GridLayout was useful for arranging the text neatly in its own area. The right are uses radio buttons to only allow one selection at a time. You do not need to use those particular things, but you are welcome to search around for examples if you want to incorporate them in your own project. When reading about these things, look for small code examples that do something close to what you want. It is harder to read a general description of the class and figure out exactly what code you need. Using small 1 or 2 line examples from tutorials and adapting them for this assignment is not academic misconduct. Copying 1 or 2 line examples from another student in the class is - you did not do the work to investigate how to use these components.


Finally, make sure the program quits properly when the window is closed.


Program Structure


This project should be one class called Battle (although a better future design would probably involve classes for the computer opponent variations, and one for the game mechanics).


You should expect to have a number of instance variables, mostly user interface components that need to be accessible in the listener method to respond to events. Do not declare all UI components as instance variables - for example, JPanels that help with layouts are not needed once the UI is constructed, and text labels that never change are also not needed in the listener.


The constructor needs to build up the user interface similar to how it was done in the lab code. There are more parts to this interface. I found it quite daunting. One approach that helped me to make progress was having the constructor call other methods, like makePlayerChoicePanel. The job of that method is to make a JPanel, fill it with the buttons for rock, paper, or scissors, add a layout, add a border, set sizes, and then return the panel to the constructor. This panel is then added to the main UI panel in the constructor. This breaking apart of the problem made it much easier to work on one part at a time, to track what button or panel I was working on, and to make incremental, testable progress. The constructor also needs to connect the buttons to the listener, and to initialize any other needed parts of the program.


The ActionPerformed method required by the ActionListener is what gets called whenever a button on the UI is pressed. Its job is to figure out what was pressed and to respond appropriately. In the ActionPerformed method you will need to store what choice the player made. Think about how to represent this - is it a number, is it a String, is it a bunch of booleans? You will need to represent the computer player choice as well.


You must have a method called pickWinner that has parameters for the player and computer choices of rock, paper, or scissors and returns whether the winner was the player, the computer, or a tie. Call this pickWinner method from the ActionPerformed method to get the winner and display the winner on the UI.










SimpleListener Application


import java.awt.event.ActionEvent; import java.awt.event.ActionListener;  import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel;  public class SimpleAppListenerCompleted extends JFrame implements ActionListener {         private JButton sayHi;         private JButton quit;          public SimpleAppListenerCompleted() {                 super("Simple Button Test");                 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                  sayHi = new JButton("Say Hi!");                 quit = new JButton("Quit");                  JPanel panel = new JPanel();                                  // Try a non-default layout                 BoxLayout box = new BoxLayout(panel, BoxLayout.PAGE_AXIS);                  panel.setLayout(box);                  panel.add(sayHi);                 panel.add(quit);                 this.setContentPane(panel);                 sayHi.addActionListener(this);                 quit.addActionListener(this);                 this.pack();         }          public static void main(String[] args) {                                  SimpleAppListenerCompleted app = new SimpleAppListenerCompleted();                                 app.setVisible(true);          }          @Override         public void actionPerformed(ActionEvent e) {                 if (e.getSource() == sayHi)                         System.out.println("Hi!");                 if (e.getSource() == quit)                         this.dispose();          } }
Nov 18, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here