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...

1 answer below »

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.



An example UI for rock-paper-scissors


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.


Submitting


In case there are issues running your program, please make a short video of your running application. It should show choosing the different player choices and shifting from one computer opponent to the other. On a Mac, you can make a screen recording with Quicktime. Windows can do this through the Game bar (https://www.pcmag.com/how-to/how-to-capture-video-clips-in-windows-10(Links to an external site.)) although there are plenty of web sites that can help with this. You can shoot with your phone as well. Please keep it short and the file small.


Submit your Battle.java file and the video to Gradescope. There is no autograder functionality for this UI assignment.

Answered Same DayNov 12, 2021

Answer To: The main goal of this assignment is to learn about Java Swing user interfaces. You will build a UI...

Mohd answered on Nov 16 2021
143 Votes
solution/RockPaperScissors/.classpath

    
    
    
solution/RockPaperScissors/.project

     RockPaperSciss
ors
    
    
    
    
        
             org.eclipse.jdt.core.javabuilder
            
            
        
    
    
         org.eclipse.jdt.core.javanature
    
solution/RockPaperScissors/bin/Battle.class
public synchronized class Battle extends javax.swing.JFrame implements java.awt.event.ActionListener {
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
public void Battle();
private void initComponents();
public static void main(String[]);
public void...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here