I am almost done with this Hangman game, but I am having problems with the letter buttons and am lost as to what to do to fix the problem...
package hangman;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
public class Hangman extends JFrame implements ActionListener {
private static int WIDTH=1000;
private static int HEIGHT=750;
private JButton exitB;
private Container pane;
private String[] easy={"dog", "cat", "hot", "yes", "run"};
private String[] normal={"door", "calm", "fish", "year", "stun"};
private String[] hard={"truck", "fight", "mouse", "radio", "atone"};
private boolean usd[] = new boolean[26];
private String guessme=easy[0];
private int numguesses=0;
private boolean finished = false;
private boolean won = false;
private JButton a[];
private JRadioButton easyRB, normalRB, hardRB;
public Hangman() {
setTitle("Hangman");
setSize(WIDTH, HEIGHT);
pane=getContentPane();
int i;
StringBuffer buffer;
a = new JButton[26];
// create all 26 buttons
for (i = 0; i <26; i++) {
buffer = new StringBuffer();
buffer.append((char)(i+65));
a[i] = new JButton(buffer.toString());
a[i].addActionListener( this );
a[i].setSize(50, 30);
if(i<13)a[i].setLocation(i*50, 300);
else a[i].setLocation((i-13)*50, 330);
pane.add(a[i]);
}
exitB=new JButton("Exit");
easyRB=new JRadioButton("Easy");
easyRB.setSelected(false);
normalRB=new JRadioButton("Normal");
normalRB.setSelected(true);
hardRB=new JRadioButton("Hard");
hardRB.setSelected(false);
ButtonGroup group = new ButtonGroup();
group.add(easyRB);
group.add(normalRB);
group.add(hardRB);
easyRB.setLocation(800, 10);
normalRB.setLocation(800, 40);
hardRB.setLocation(800, 70);
easyRB.setSize(80, 30);
normalRB.setSize(80, 30);
hardRB.setSize(80, 30);
easyRB.addActionListener(this);
normalRB.addActionListener(this);
hardRB.addActionListener(this);
pane.setLayout(null);
exitB.setSize(200, 30);
exitB.setLocation(800, 600);
exitB.addActionListener(this);
pane.add(easyRB);
pane.add(normalRB);
pane.add(hardRB);
pane.add(exitB);
repaint();
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}//end Hangman
public void paint(Graphics g) {
//draw gallows and rope
setBackground(Color.white);
g.fillRect(10, 250, 150, 20);
g.fillRect(40,70,10,200);
g.fillRect(40,70,60,10);
g.setColor(Color.yellow);
g.fillRect(95,70,5,25);
g.setColor(Color.orange);
if (numguesses >=1 )
g.drawOval(82,95,30,30);
g.setColor(Color.green);
if (numguesses >=2 )
g.drawLine(97,125,97,150);
if (numguesses >=3 )
g.drawLine(97,150,117,183);
if (numguesses >=4 )
g.drawLine(97,150,77,183);
if (numguesses >=5 )
g.drawLine(97,125,117,135);
if (numguesses >=6 )
g.drawLine(97,125,77,135);
StringBuffer st = new StringBuffer();
for (int l=0; l<=25; l++) {
if (usd[l]) st.append((char)(l+65));
else st.append(".");
}
g.setColor(Color.blue);
Font f = new Font("Courier",Font.ITALIC,14);
g.setFont(f);
g.drawString(st.toString(),25,285);
StringBuffer guessed = new StringBuffer();
Font ff = new Font("Courier",Font.BOLD,24);
g.setColor(Color.black);
g.setFont(ff);
for (int mm=0;mm<guessme.length();mm++) {
if (usd[(int)guessme.charAt(mm)-65])
guessed.append(guessme.charAt(mm));
else
guessed.append(".");
}
g.drawString(guessed.toString(),75,230);
if (numguesses >=6) {
g.setColor(Color.white);
g.fillRect(70, 200, 200, 30);
g.setColor(Color.black);
g.drawString(guessme.toString(),75,230);
Font fff = new Font("Helvetica",Font.BOLD,36);
g.setFont(fff);
g.setColor(Color.red);
g.drawString("You lose!",200,100);
finished = true;
}
if (won) {
Font fff = new Font("Helvetica",Font.BOLD,36);
g.setFont(fff);
// Color red=new Color.red
g.setColor(Color.red);
g.drawString("You Win!",200,100);
finished = true;
}
}
public void rer(int lett) {
if (!finished) {
boolean found=false;
boolean www=false;
if (!usd[lett]) {
for (int mm =0;mm<guessme.length();mm++) {
if (guessme.charAt(mm)==((char)(lett+65))) found=true;
}
if (!found) numguesses++;
}
usd[lett] = true;
for (int mm =0;mm<guessme.length();mm++) {
if (!usd[(int)(guessme.charAt(mm))-65]) www=true;
}
if (!www) won=true;
repaint();
}
}
public void actionPerformed( ActionEvent ev) {
if(ev.getActionCommand().equals("Exit")){
System.exit(0);
}
if(easyRB.isSelected()){
guessme=easy[0];
}
if(normalRB.isSelected()){
guessme=normal[0];
}
if(hardRB.isSelected()){
guessme=hard[0];
}
int i;
for (i = 0; i < 26; i++) {
if (ev.getSource() == a[i]) { rer(i); }
}
}
public static void main(String[] args) {
Hangman demo =new Hangman();
}
}