-
Notifications
You must be signed in to change notification settings - Fork 0
/
WelcomeMenu.java
executable file
·86 lines (67 loc) · 2.8 KB
/
WelcomeMenu.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.Graphics;
import javax.imageio.ImageIO;
/**
* Creates a WelcomeMenu for the battleship application
*/
public class WelcomeMenu implements ActionListener {
private JFrame frame;
private JPanel panel;
private JLabel title;
private JButton playGameButton;
private JButton helpButton;
/**
* Constructs a WelcomeMenu with GUI elements
*/
public WelcomeMenu() {
frame = new JFrame();
panel = new JPanel();
title = new JLabel(new ImageIcon(((new ImageIcon("battleship-wallpaper.jpg")).getImage()).getScaledInstance(600, 250, java.awt.Image.SCALE_SMOOTH)));
playGameButton = new JButton("Play Game");
helpButton = new JButton("Help");
playGameButton.addActionListener(this);
helpButton.addActionListener(this);
//JLabel wallpaper = new JLabel(new ImageIcon(getClass().getResource("battleship-wallpaper.jpg")));
//title.add(wallpaper);
panel.setLayout(new GridLayout(3,1));
panel.add(title);
panel.add(playGameButton);
panel.add(helpButton);
frame.setLayout(new BorderLayout());
frame.setSize(500, 500);
frame.setTitle("Battleship");
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
/**
* actionPerformed reads in an actionEvent by the user to determine what happens next
* @param e the action performed
*/
public void actionPerformed(ActionEvent e) //throws InterruptedException
{
JButton source = (JButton) e.getSource();
switch (source.getText())
{
case "Play Game":
{
frame.dispose();
(new Thread() {
public void run() {
Battleship b = new Battleship();
}
}).start();
} break;
case "Help":
JOptionPane.showMessageDialog(frame, "To begin the game, Each player will drag their ships to the grid in locations of their choosing. \nAfter each of the ships have been placed, Player One will start off attacking by clicking on a square in the grid located above the grid containing his ships. \nA message will appear stating whether the attack was a hit or a miss. \nThe upper grid will indicate this by being filled red (when a ship is hit), or white (when the attack is missed). \nThe remaining tiles are filled blue to indicate they have not been targeted. \nOnce a ship has sunk, another message will appear stating which type of ship was sunk. \nOnce all of the ships from one player have sunk, the game is over and the opponent wins." +
"\n\n Key for colors used: \n blue - empty water spaces \n gray - ships \n red - attacked ships \n white - attacked water");
break;
}
}
}