-
Notifications
You must be signed in to change notification settings - Fork 0
/
SheepHerder.java
153 lines (129 loc) · 4.5 KB
/
SheepHerder.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* The SheepHerder application displays the welcome message and game instruction.
* It also asks for the player's name, and directs them to playing level options when "Enter" button is clicked.
*
* Note: If messages cannot be seen when you run the application, click minimize or maximize button.
*
* @author Cherry Dominguez
* @version 1.0
* @since 2022-03-30
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SheepHerder extends JFrame implements ActionListener
{
/**
* Instance variables
*/
final int WIDTH = 600;
final int HEIGHT = 450;
String name;
JPanel head, body, start;
JLabel heading, goal, playerName;
JTextField nameField;
JButton button;
/**
* Constructor for SheepHerder ()
*/
public SheepHerder ()
{
//JFrame settings
super ("Sheep Herder Game");
setSize (WIDTH, HEIGHT);
setVisible (true);
setResizable (true);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
//header
head = new JPanel ();
head.setBackground (Color.cyan);
head.setLayout (new FlowLayout (FlowLayout.CENTER, 15, 15));
add (head, BorderLayout.NORTH);
heading = new JLabel ("WELCOME TO SHEEP HERDER GAME");
Font bigFont = new Font ("Arial", Font.BOLD, 30);
heading.setFont (bigFont);
head.add (heading);
//instructions
body = new JPanel ();
body.setLayout (new FlowLayout (FlowLayout.LEFT, 15, 15));
add (body, BorderLayout.CENTER);
goal = new JLabel ("GOAL:");
Font medFont = new Font ("Arial", Font.BOLD, 20);
goal.setFont (medFont);
body.add (goal);
goal = new JLabel ("Find the SHEEP before it gets eaten by the WOLF.");
Font medFont2 = new Font ("Arial", Font.PLAIN, 20);
goal.setFont (medFont2);
body.add (goal);
goal = new JLabel ("If the SHEEP gets eaten, the PLAYER will lose.");
Font medFont3 = new Font ("Arial", Font.PLAIN, 20);
goal.setFont (medFont3);
body.add (goal);
goal = new JLabel ("If the PLAYER and the WOLF meet, the PLAYER will lose ");
Font medFont4 = new Font ("Arial", Font.PLAIN, 20);
goal.setFont (medFont4);
body.add (goal);
goal = new JLabel (" unless the PLAYER finds the valiant DOG first.");
Font medFont5 = new Font ("Arial", Font.PLAIN, 20);
goal.setFont (medFont5);
body.add (goal);
goal = new JLabel ("The fearless hunting DOG fights the WOLF when they meet,");
Font medFont6 = new Font ("Arial", Font.PLAIN, 20);
goal.setFont (medFont6);
body.add (goal);
goal = new JLabel (" and the PLAYER may get an extra turn.");
Font medFont7 = new Font ("Arial", Font.PLAIN, 20);
goal.setFont (medFont7);
body.add (goal);
//start button
start = new JPanel ();
start.setBackground (Color.cyan);
start.setLayout (new FlowLayout (FlowLayout.CENTER, 10, 15));
add (start, BorderLayout.SOUTH);
playerName = new JLabel ("ENTER PLAYER NAME:");
Font font = new Font ("Arial", Font.BOLD, 20);
playerName.setFont (font);
start.add (playerName);
nameField = new JTextField (15);
Font font1 = new Font ("Arial", Font.PLAIN, 15);
nameField.setFont (font1);
start.add (nameField);
button = new JButton ("Enter");
button.setBackground (Color.yellow);
Font font2 = new Font ("Arial", Font.PLAIN, 15);
button.setFont (font2);
start.add (button);
//event listener
button.addActionListener (this);
}
/**
* actionPerformed method ()
* This method executes when "Enter" button is pressed
*/
@Override
public void actionPerformed (ActionEvent event)
{
Object source = event.getSource();
if(source == button)
{
if(nameField.getText().equals(""))
{
JOptionPane.showMessageDialog(null,"Please enter your name","Empty Field",1);
}
else
{
name = nameField.getText().toString();
this.dispose();
new Level(name);
}
}
}// end of actionPerformed method ()
/**
* Main method
* @param args the command line arguments
*/
public static void main (String [] args)
{
new SheepHerder ();
}//end of main method
}//end of SheepHerder Class