My assignment is to create a hangman game through eclipse java. We started by us
ID: 638555 • Letter: M
Question
My assignment is to create a hangman game through eclipse java. We started by using the windows builder to create a basic layout of the game and we hardcoded it to just work for the word "light." Now I need to alter this given program to:
1. Allow for the user to select easy, medium or hard word to guess. (I have already created 3 text files for this called "EasyWords.txt, MediumWords.txt and HardWords.txt")
2. Accept words from therse 3 text files (easy, medium and hard) to randomly choose a word for the user to guess during the game
3. Create the graphics to create the picture of the hangman
This is the program that I already created, I just need the alterations that I explained above:
import java.awt.Color;
public class Game extends JPanel {
private DashPanel panDashes = new DashPanel();
private String wtg;
private StringBuilder dashes = new StringBuilder();
private JFrame frmJavaHangman;
private final ButtonGroup buttonGroup = new ButtonGroup();
private JRadioButton rdbtnEasy = new JRadioButton("Easy");
private JRadioButton rdbtnMedium = new JRadioButton("Medium");
private JRadioButton rdbtnHard = new JRadioButton("Hard");
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Game window = new Game();
window.frmJavaHangman.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Game() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmJavaHangman = new JFrame();
frmJavaHangman.setResizable(false);
frmJavaHangman.setTitle("CSE 271 - Hangman");
frmJavaHangman.setAlwaysOnTop(true);
frmJavaHangman.setBounds(100, 100, 672, 643);
frmJavaHangman.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmJavaHangman.getContentPane().setLayout(null);
ButtonClick bc = new ButtonClick();
setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
setBounds(8, 13, 537, 425);
frmJavaHangman.getContentPane().add(this);
panDashes.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
panDashes.setBounds(8, 444, 537, 48);
frmJavaHangman.getContentPane().add(panDashes);
JButton btnExit = new JButton("Exit");
btnExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
btnExit.setBounds(557, 467, 97, 25);
frmJavaHangman.getContentPane().add(btnExit);
JButton btnNewGame = new JButton("New Game");
btnNewGame.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
JButton btn;
for(Object o : frmJavaHangman.getContentPane().getComponents()) {
if(o instanceof JButton) {
btn = (JButton)o;
if(btn.getText().length() == 1){
btn.setEnabled(true);
btn.setBackground(Color.yellow);
}
}
}
}
});
btnNewGame.setBounds(557, 413, 97, 25);
frmJavaHangman.getContentPane().add(btnNewGame);
buttonGroup.add(rdbtnEasy);
rdbtnEasy.setSelected(true);
rdbtnEasy.setBounds(561, 319, 105, 25);
frmJavaHangman.getContentPane().add(rdbtnEasy);
buttonGroup.add(rdbtnMedium);
rdbtnMedium.setBounds(561, 349, 105, 25);
frmJavaHangman.getContentPane().add(rdbtnMedium);
buttonGroup.add(rdbtnHard);
rdbtnHard.setBounds(561, 379, 101, 25);
frmJavaHangman.getContentPane().add(rdbtnHard);
// Create all A-Z buttons
int y = 501, x = 8;
for(int i = 0; i < 26; i++) {
if(i == 13) { x = 8; y = 550; }
JButton btn = new JButton("" + (char)(65 + i));
btn.setBounds(x, y, 46, 46);
btn.setBackground(Color.yellow);
btn.addActionListener(bc);
frmJavaHangman.getContentPane().add(btn);
x+=50;
}
setNewWord();
}
public void paint(Graphics g) {
super.paint(g);
// draw your gallows
// Now repaint the dashes JPanel
panDashes.repaint();
}
private void setNewWord() {
// This is just an example... You should open one of the text files
// (depending on which radio button is selected) and choose a random
// word from it.
wtg = "light".toUpperCase();
dashes = new StringBuilder(new String(new char[wtg.length()]).replace("", "-"));
}
private void showDashes() {
System.out.println(dashes.toString());
// You are to draw dashes on the panel
}
// Method that updates the dashes string with a guessed letter
private boolean guessLetter(char letter) {
boolean ret = false;
for(int i = 0; i < wtg.length(); i++)
if(wtg.charAt(i) == letter) {
dashes.replace(i, i+1, ""+letter);
ret = true;
}
showDashes();
return ret;
}
private boolean checkForWin() {
// If we search the dashes string and don't find a dash then game over
return dashes.indexOf("-") == -1;
}
private void gameOver() {
JButton btn;
// Look at each object on the frame
for(Object o : frmJavaHangman.getContentPane().getComponents())
// Found a JButton
if(o instanceof JButton) {
btn = (JButton)o;
// If the button has only 1 character then it's a letter so disable it.
if(btn.getText().length() == 1) btn.setEnabled(false);
}
}
// A simple class to handle the dashes panel
private class DashPanel extends JPanel {
public void paint(Graphics g) {
super.paint(g);
Font f = new Font("Courier New", 1, getHeight());
g.setFont(f);
FontMetrics metrics = getFontMetrics(f);
int h = metrics.getHeight();
int w = metrics.stringWidth(dashes.toString());
g.drawString(dashes.toString(), getWidth()/2 - w/2, getHeight() - h/4);
}
}
// A class to handle the clicks of the letters
private class ButtonClick implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
Random rnd = new Random();
JButton btn = (JButton)e.getSource();
btn.setBackground( (guessLetter(btn.getText().charAt(0)) ? Color.green : Color.red) );
btn.setEnabled(false);
repaint();
if (checkForWin())
gameOver();
}
}
}
Explanation / Answer
Program Code:
import java.awt.Color;
import java.util.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.io.*;
public class Game extends JPanel
{
HangManPanel1 hngman=new HangManPanel1();
File file;
String easy[];
Random randomGenerator = new Random();
int count=0;
private DashPanel panDashes = new DashPanel();
private String wtg;
private StringBuilder dashes = new StringBuilder();
private JFrame frmJavaHangman;
private final ButtonGroup buttonGroup = new ButtonGroup();
private JRadioButton rdbtnEasy = new JRadioButton("Easy");
private JRadioButton rdbtnMedium = new JRadioButton("Medium");
private JRadioButton rdbtnHard = new JRadioButton("Hard");
/**
* Launch the application.
*/
public static void main(String[] args) throws Exception
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
Game window = new Game();
window.frmJavaHangman.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
public Game() throws Exception
{
initialize();
}
private void initialize() throws Exception
{
frmJavaHangman = new JFrame();
frmJavaHangman.setResizable(false);
frmJavaHangman.setTitle("CSE 271 - Hangman");
frmJavaHangman.setAlwaysOnTop(true);
frmJavaHangman.setBounds(100, 100, 672, 643);
frmJavaHangman.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmJavaHangman.getContentPane().setLayout(null);
ButtonClick bc = new ButtonClick();
hngman.setVisible(true);
hngman.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
hngman.setBounds(18, 13, 537, 425);
add(hngman);
setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
setBounds(8, 13, 537, 425);
frmJavaHangman.getContentPane().setVisible(true);
frmJavaHangman.getContentPane().add(this);
panDashes.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
panDashes.setBounds(8, 444, 537, 48);
frmJavaHangman.getContentPane().add(panDashes);
JButton btnExit = new JButton("Exit");
btnExit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
btnExit.setBounds(557, 467, 97, 25);
frmJavaHangman.getContentPane().add(btnExit);
JButton btnNewGame = new JButton("New Game");
btnNewGame.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0)
{
JButton btn;
for(Object o : frmJavaHangman.getContentPane().getComponents())
{
if(o instanceof JButton)
{
btn = (JButton)o;
if(btn.getText().length() == 1)
{
btn.setEnabled(true);
btn.setBackground(Color.yellow);
try
{
count=0;
hngman.setCount(0);
hngman.repaint();
panDashes.repaint();
setNewWord();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
}
});
btnNewGame.setBounds(557, 413, 97, 25);
frmJavaHangman.getContentPane().add(btnNewGame);
buttonGroup.add(rdbtnEasy);
rdbtnEasy.setSelected(true);
rdbtnEasy.setBounds(561, 319, 105, 25);
frmJavaHangman.getContentPane().add(rdbtnEasy);
buttonGroup.add(rdbtnMedium);
rdbtnMedium.setBounds(561, 349, 105, 25);
frmJavaHangman.getContentPane().add(rdbtnMedium);
buttonGroup.add(rdbtnHard);
rdbtnHard.setBounds(561, 379, 101, 25);
frmJavaHangman.getContentPane().add(rdbtnHard);
// Create all A-Z buttons
int y = 501, x = 8;
for(int i = 0; i < 26; i++)
{
if(i == 13)
{
x = 8;
y = 550;
}
JButton btn = new JButton("" + (char)(65 + i));
btn.setBounds(x, y, 46, 46);
btn.setBackground(Color.yellow);
btn.addActionListener(bc);
frmJavaHangman.getContentPane().add(btn);
x+=50;
}
setNewWord();
}
public void paint(Graphics g)
{
super.paint(g);
hngman.draw(g);
hngman.setCount(count);
add(hngman);
// Now repaint the dashes JPanel
panDashes.repaint();
}
private void setNewWord() throws FileNotFoundException
{
wtg=getWord();
wtg = wtg.toUpperCase();
dashes = new StringBuilder(new String(new char[wtg.length()]).replace("", "-"));
}
private String getWord() throws FileNotFoundException
{
int i=0;
int num=0;
if(rdbtnEasy.isSelected())
file=new File("words.txt");
else if(rdbtnMedium.isSelected())
file=new File("MediumWords.txt");
else if(rdbtnHard.isSelected())
file=new File("HardWords.txt");
Scanner in=new Scanner(file);
if(file.exists())
{
num=in.nextInt();
easy=new String[num];
while(in.hasNextLine())
{
easy[i]=in.next();
i++;
}
}
return easy[randomGenerator.nextInt(num)];
}
private void showDashes()
{
System.out.println(dashes.toString());
// You are to draw dashes on the panel
}
// Method that updates the dashes string with a guessed letter
private boolean guessLetter(char letter)
{
boolean ret = false;
for(int i = 0; i < wtg.length(); i++)
if(wtg.charAt(i) == letter)
{
dashes.replace(i, i+1, ""+letter);
ret = true;
}
showDashes();
return ret;
}
private boolean checkForWin()
{
// If we search the dashes string and don't find a dash then game over
return dashes.indexOf("-") == -1;
}
private void gameOver()
{
JButton btn;
// Look at each object on the frame
for(Object o : frmJavaHangman.getContentPane().getComponents())
// Found a JButton
if(o instanceof JButton)
{
btn = (JButton)o;
// If the button has only 1 character then it's a letter so disable it.
if(btn.getText().length() == 1) btn.setEnabled(false);
if(count==10)
btn.setEnabled(false);
}
}
// A simple class to handle the dashes panel
private class DashPanel extends JPanel
{
public void paint(Graphics g)
{
int x = getWidth();
int y = getHeight();
super.paint(g);
Font f = new Font("Courier New", 1, getHeight());
g.setFont(f);
FontMetrics metrics = getFontMetrics(f);
int h = metrics.getHeight();
int w = metrics.stringWidth(dashes.toString());
g.drawString(dashes.toString(), getWidth()/2 - w/2, getHeight() - h/4);
}
}
// A class to handle the clicks of the letters
private class ButtonClick implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Random rnd = new Random();
JButton btn = (JButton)e.getSource();
boolean flag=guessLetter(btn.getText().charAt(0));
if (flag)
btn.setBackground(Color.green );
else
{
btn.setBackground(Color.red );
++count;
hngman.setCount(count);
}
btn.setEnabled(false);
repaint();
if (checkForWin())
gameOver();
if(count>10)
gameOver();
}
}
}
import java.awt.Graphics;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;
public class HangManPanel1 extends JPanel
{
JPanel p=new JPanel();
static int count=0;
HangManPanel1()
{
p.repaint();
add(p);
//add(this);
}
public void setCount(int n)
{
count=n;
}
public void draw(Graphics g)
{
int x = getWidth();
int y = getHeight();
x=x*10+50;
y=y*10+30;
super.paint(g);
if(count==1)
g.drawArc((int)(x * 0.10),(int)(y * 0.9),
(int)(x * 0.35),(int)(y*0.16), 0, 180);
if(count==2)
{g.drawArc((int)(x * 0.10),(int)(y * 0.9),
(int)(x * 0.35),(int)(y*0.16), 0, 180);
g.drawLine((int)(x * 0.275),(int)(y * 0.1),
(int)(x * 0.275), (int)(y * 0.9));
}
if(count==3)
{
g.drawArc((int)(x * 0.10),(int)(y * 0.9),
(int)(x * 0.35),(int)(y*0.16), 0, 180);
g.drawLine((int)(x * 0.275),(int)(y * 0.1),
(int)(x * 0.275), (int)(y * 0.9));
g.drawLine((int)(x * 0.275), (int)(y * 0.1),
(int)(x * 0.75), (int)(y * 0.1));
}
if(count==4)
{
g.drawArc((int)(x * 0.10),(int)(y * 0.9),
(int)(x * 0.35),(int)(y*0.16), 0, 180);
g.drawLine((int)(x * 0.275),(int)(y * 0.1),
(int)(x * 0.275), (int)(y * 0.9));
g.drawLine((int)(x * 0.275), (int)(y * 0.1),
(int)(x * 0.75), (int)(y * 0.1));
g.drawLine((int)(x * 0.75), (int)(y * 0.1),
(int)(x * 0.75), (int)(y * 0.18));
}
if(count==5)
{
g.drawArc((int)(x * 0.10),(int)(y * 0.9),
(int)(x * 0.35),(int)(y*0.16), 0, 180);
g.drawLine((int)(x * 0.275),(int)(y * 0.1),
(int)(x * 0.275), (int)(y * 0.9));
g.drawLine((int)(x * 0.275), (int)(y * 0.1),
(int)(x * 0.75), (int)(y * 0.1));
g.drawLine((int)(x * 0.75), (int)(y * 0.1),
(int)(x * 0.75), (int)(y * 0.18));
g.drawOval((int)(x * 0.67), (int)(y * 0.18),
(int)(x * 0.16), (int)(y * 0.18));
}
if(count==6)
{
g.drawArc((int)(x * 0.10),(int)(y * 0.9),
(int)(x * 0.35),(int)(y*0.16), 0, 180);
g.drawLine((int)(x * 0.275),(int)(y * 0.1),
(int)(x * 0.275), (int)(y * 0.9));
g.drawLine((int)(x * 0.275), (int)(y * 0.1),
(int)(x * 0.75), (int)(y * 0.1));
g.drawLine((int)(x * 0.75), (int)(y * 0.1),
(int)(x * 0.75), (int)(y * 0.18));
g.drawOval((int)(x * 0.67), (int)(y * 0.18),
(int)(x * 0.16), (int)(y * 0.18));
g.drawLine((int)(x * 0.70), (int)(y * 0.34),
(int)(x * 0.53), (int)(y * 0.55));
}
if(count==7)
{
g.drawArc((int)(x * 0.10),(int)(y * 0.9),
(int)(x * 0.35),(int)(y*0.16), 0, 180);
g.drawLine((int)(x * 0.275),(int)(y * 0.1),
(int)(x * 0.275), (int)(y * 0.9));
g.drawLine((int)(x * 0.275), (int)(y * 0.1),
(int)(x * 0.75), (int)(y * 0.1));
g.drawLine((int)(x * 0.75), (int)(y * 0.1),
(int)(x * 0.75), (int)(y * 0.18));
g.drawOval((int)(x * 0.67), (int)(y * 0.18),
(int)(x * 0.16), (int)(y * 0.18));
g.drawLine((int)(x * 0.70), (int)(y * 0.34),
(int)(x * 0.53), (int)(y * 0.55));
g.drawLine((int)(x * 0.80), (int)(y * 0.34),
(int)(x * 0.97), (int)(y * 0.55));
}
if(count==8)
{
g.drawArc((int)(x * 0.10),(int)(y * 0.9),
(int)(x * 0.35),(int)(y*0.16), 0, 180);
g.drawLine((int)(x * 0.275),(int)(y * 0.1),
(int)(x * 0.275), (int)(y * 0.9));
g.drawLine((int)(x * 0.275), (int)(y * 0.1),
(int)(x * 0.75), (int)(y * 0.1));
g.drawLine((int)(x * 0.75), (int)(y * 0.1),
(int)(x * 0.75), (int)(y * 0.18));
g.drawOval((int)(x * 0.67), (int)(y * 0.18),
(int)(x * 0.16), (int)(y * 0.18));
g.drawLine((int)(x * 0.70), (int)(y * 0.34),
(int)(x * 0.53), (int)(y * 0.55));
g.drawLine((int)(x * 0.80), (int)(y * 0.34),
(int)(x * 0.97), (int)(y * 0.55));
g.drawLine((int)(x * 0.75), (int)(y * 0.36),
(int)(x * 0.75), (int)(y * 0.62));
}
if(count==9)
{
g.drawArc((int)(x * 0.10),(int)(y * 0.9),
(int)(x * 0.35),(int)(y*0.16), 0, 180);
g.drawLine((int)(x * 0.275),(int)(y * 0.1),
(int)(x * 0.275), (int)(y * 0.9));
g.drawLine((int)(x * 0.275), (int)(y * 0.1),
(int)(x * 0.75), (int)(y * 0.1));
g.drawLine((int)(x * 0.75), (int)(y * 0.1),
(int)(x * 0.75), (int)(y * 0.18));
g.drawOval((int)(x * 0.67), (int)(y * 0.18),
(int)(x * 0.16), (int)(y * 0.18));
g.drawLine((int)(x * 0.70), (int)(y * 0.34),
(int)(x * 0.53), (int)(y * 0.55));
g.drawLine((int)(x * 0.80), (int)(y * 0.34),
(int)(x * 0.97), (int)(y * 0.55));
g.drawLine((int)(x * 0.75), (int)(y * 0.36),
(int)(x * 0.75), (int)(y * 0.62));
g.drawLine((int)(x * 0.75), (int)(y * 0.62),
(int)(x * 0.62), (int)(y * 0.76));
}
if(count==10)
{
g.drawArc((int)(x * 0.10),(int)(y * 0.9),
(int)(x * 0.35),(int)(y*0.16), 0, 180);
g.drawLine((int)(x * 0.275),(int)(y * 0.1),
(int)(x * 0.275), (int)(y * 0.9));
g.drawLine((int)(x * 0.275), (int)(y * 0.1),
(int)(x * 0.75), (int)(y * 0.1));
g.drawLine((int)(x * 0.75), (int)(y * 0.1),
(int)(x * 0.75), (int)(y * 0.18));
g.drawOval((int)(x * 0.67), (int)(y * 0.18),
(int)(x * 0.16), (int)(y * 0.18));
g.drawLine((int)(x * 0.70), (int)(y * 0.34),
(int)(x * 0.53), (int)(y * 0.55));
g.drawLine((int)(x * 0.80), (int)(y * 0.34),
(int)(x * 0.97), (int)(y * 0.55));
g.drawLine((int)(x * 0.75), (int)(y * 0.36),
(int)(x * 0.75), (int)(y * 0.62));
g.drawLine((int)(x * 0.75), (int)(y * 0.62),
(int)(x * 0.62), (int)(y * 0.76));
g.drawLine((int)(x * 0.75), (int)(y * 0.62),
(int)(x * 0.88), (int)(y * 0.76));
}
}
}
Note:
The changes made are highlighted with bold letters. Now the program is 100% working.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.