This program is run against a server. Here is the server program. If the server is down, start it on the computer you're working on.
In lecture 4 a server program was written which plays "rock, paper, scissors" with its clients. The code is available here. Your task is to write a graphical client program using only Swing components, namely JFrame, JTextField, JLabel, JButton, ImageIcon and Box.
The game is widely known, and you can find useful material at Rock, paper, scissors, e.g. three gif images.
Note: the server speaks Swedish, so either change the server code (and run the server locally), or translate the replies from the server inside the client program ("sten"->rock, "sax"->scissors, "påse"->paper).
try {
Socket socket=new Socket("my.nada.kth.se",4712) ;
BufferedReader in=new BufferedReader(new InputStreamReader(
socket.getInputStream())) ;
PrintWriter ut=new PrintWriter(socket.getOutputStream()) ;
ut.println("Charlotta"); ut.flush() ;
System.out.println(in.readLine()) ;}
If the server is down, try the one at rungner by replacing "my.nada.kth.se"
with "rungner.nada.kth.se". The client should send your name to the server
listening on port 4712 and display the answer you receive. Remember to do
ut.flush(), or the name won't be sent. Once the client works
you can focus completely on the graphical interface.
public class Client extends JFrame implements ActionListener{
where the left half is your game area and the right half is that of the
computer. Use BoxLayout since this often gives the best results.
Each game area is a vertical Box which contains, in order from
top to bottom,
JLabel with either Me: or Computer:.
JTextField where whatever is being sent from you or from the server is shown (first the greeting, then "ROCK", "PAPER" or "SCISSORS").
new JButton(rockimg)...)
ImageIcon...)
JLabel which after one click displays ONE...,
after a second click TWO... and after a third click WINS,
DRAW or LOSES.
JTextField which displays Score: 7
GameArea be an inner class, to make it easy to
create two instances of it beside each other in a horizontal box.
The advantage of a box is the ability to put fixed spaces between the
components inside. With the declaration Box box=Box.createHorizontalBox(); one can do this:
box.add(me); //GameArea me=new GameArea("Me:"); has already been declared
box.add(Box.createHorizontalStrut(20)); //Spacer
box.add(you); //GameArea you=new GameArea("Computer:"); has already been declared
Extra assignment: Add sound effects! (May require headphones on some computers).
GameArea inherits from Box you can start
the constructor with super(BoxLayout.Y_AXIS) to make it vertical.
ImageIcon rock=new ImageIcon("rock.gif") and collected in an array
Icon[] imgs={rock,scissors,paper}.
String[]text={"ROCK","PAPER","SCISSORS"}.
button[i].setActionCommand(text[i]). This can then be
retrieved in actionPerformed by e.getActionCommand().
button[i].addActionListener(Client.this).
public void select(String text) is suitable for the GameArea
class. It might go through the buttons and set a grey background for
all buttons except the one with the specified text, which instead
is turned yellow. At the same time, write a new message to the
message field.
select method in the computer area.
A beautiful client by (student)................................................................
admired by (teacher)........................................... on (date) .....................