-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHangman.java
More file actions
96 lines (65 loc) · 2.26 KB
/
Copy pathHangman.java
File metadata and controls
96 lines (65 loc) · 2.26 KB
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
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
public class Hangman extends Applet implements ActionListener
{
Button letterBtns[] = new Button[26];
Font font20 = new Font ("Ariel", 1, 20);
String output = "";
String input = "";
char secretLetters[] = {'L', 'U', 'C', 'I', 'A', 'N', 'O'};
char dashLetters[] = {'-', '-', '-', '-', '-', '-', '-'};
char letter = 'A';
char guess = '?';
int numWrong = 0;
Image hangedMan;
public void init()
{
this.setLayout(null);
for (int num=0; num <= 25; num++)
{
letterBtns[num] = new Button("" + letter);
letterBtns[num].setBounds(50 + 50 * num, 600, 40, 40);
letterBtns[num].addActionListener(this);
this.add(letterBtns[num]);
letter++;
letterBtns[num].setBackground(Color.white);
letterBtns[num].setForeground(Color.red);
}
hangedMan = this.getImage(this.getCodeBase(), "imgs/gallow-" + numWrong + ".png");
repaint();
}
public void actionPerformed(ActionEvent e)
{
for (int place=0; place < 26; place++) {
if (e.getSource() == letterBtns[place])
{
guess = letterBtns[place].getLabel().charAt(0);
letterBtns[place].setVisible(false);
boolean letterFound = false;
for (int letNum = 0; letNum <7; letNum++)
{
if (secretLetters[letNum] == guess)
{
letterFound = true;
dashLetters[letNum] = guess;
}
}
if (!letterFound)
{
numWrong++;
hangedMan = this.getImage(this.getCodeBase(), "imgs/gallow-" + numWrong + ".png");
}
}
}
repaint();
}
public void paint(Graphics g)
{
g.setFont(font20);
g.drawImage(hangedMan, 100, 100, 350, 350, this);
g.drawString("You have gotten " + numWrong + " Letters wrong", 550, 250);
for (int place = 0; place< 7; place++)
g.drawString("" + dashLetters[place], 550 + 40 * place, 400);
}
}