Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Hangman Game
    text
    copied!<p>Hi I've been making a hangman game in Java over ther holidays. Im almost done, but i'm having a few problems with repainting/updating the window.</p> <p>So when ever a life is lost, I'm removing the panel and then adding another one, but for some reason it keeps shifting to the left or sometimes to the right, which means some of my lines are n't being redrawn.</p> <p>edit: I'm still having trouble, I think I will just leave it now, unless anyone else has some other suggestions.</p> <pre><code>import java.awt.*; import java.awt.event.*; import java.util.ArrayList; import javax.swing.*; /** class to display a JFrame object using border layout */ public class HangmanJFrame extends JFrame implements ActionListener { private JPanel centerPanel; private JPanel southPanel; private JTextField textField; private LinePanel line; private String [] wordList = {"computer","java","activity","alaska","appearance","article", "automobile","basket","birthday","canada","central","character","chicken","chosen", "cutting","daily","darkness","diagram","disappear","driving","effort","establish","exact", "establishment","fifteen","football","foreign","frequently","frighten","function","gradually", "hurried","identity","importance","impossible","invented","italian","journey","lincoln", "london","massage","minerals","outer","paint","particles","personal","physical","progress", "quarter","recognise","replace","rhythm","situation","slightly","steady","stepped", "strike","successful","sudden","terrible","traffic","unusual","volume","yesterday" }; public ArrayList&lt;String&gt; usedLetters = new ArrayList(); // list of used letter by user public ArrayList&lt;String&gt; correctLetters = new ArrayList(); public String userInput = ""; private int numLives = 6; // number of lives public String theWord; // the wrong which is chosen JButton exitButton; JButton playAgain; // no-argument constructor public HangmanJFrame() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); theWord = pickWord(); correctLetters = new ArrayList(theWord.length()); setSize(600,500); setLocation(100, 100); setTitle("Hangman Game"); setLayout(new BorderLayout()); centerPanel = new JPanel(); line = new LinePanel(15,theWord,usedLetters); centerPanel.setSize(500,500); centerPanel.setBackground(Color.WHITE); centerPanel.add(line); add(centerPanel, BorderLayout.CENTER); textField = new JTextField(20); textField.addActionListener(this); playAgain = new JButton("Play Again"); exitButton = new JButton("Exit"); exitButton.addActionListener(this); playAgain.addActionListener(this); southPanel = new JPanel(); southPanel.setBackground(Color.BLACK); southPanel.setLayout(new GridLayout(0,3)); southPanel.add(playAgain); southPanel.add(textField); southPanel.add(exitButton); add(southPanel, BorderLayout.SOUTH); } // Picks a word, latter it will be picked randomly. private String pickWord(){ return wordList[0]; } // This method check wither the input is valid // i.e. its in the alphabet. private boolean checkInput(String s){ String [] alphabet = {"a","b","c","d","e","f", "g","h","i","j","k","l","m","n","o","p", "q","r","s","t","u","v","w","x","y","z"}; for (int i = 0; i &lt; alphabet.length; i++){ if (s.equals(alphabet[i]) &amp;&amp; s.length() &lt;= 1){ return true; } } return false; } /** * */ public void Play(){ if (numLives &gt; 0){ if (userInput.length() == 1 &amp;&amp; usedLetters.contains(userInput) == false &amp;&amp; checkInput(userInput) == true){ usedLetters.add(userInput); if (theWord.contains(userInput) == true){ correctLetters.add(userInput); centerPanel.removeAll(); line = new LinePanel(20,theWord,correctLetters); centerPanel.add(line); centerPanel.revalidate(); } else{ numLives = numLives - 1; centerPanel.removeAll(); line = new LinePanel(numLives,theWord,correctLetters); centerPanel.add(line); centerPanel.revalidate(); } } else if (userInput.length() &gt; 1) System.out.println("Enter a valid letter"); else if (userInput.length() == 1 &amp;&amp; checkInput(userInput) == true &amp;&amp; theWord.contains(userInput)){ correctLetters.add(userInput); } centerPanel.removeAll(); line = new LinePanel(20,theWord,usedLetters); centerPanel.add(line); centerPanel.revalidate(); } } // return true if the word and the correctly used letters list match public boolean checkWord(String s, ArrayList&lt;String&gt; t){ String temp = ""; for (int i = 0; i &lt; s.length(); i++){ if ( t.contains(s.substring(i, i+1)) == true){ temp += s.substring(i, i+1); } } if (s.equals(temp)){ return true; } return false; } public void actionPerformed(ActionEvent evt) { String temp = textField.getText(); if (temp.length() == 1){ userInput = temp; } textField.selectAll(); if (checkWord(theWord, correctLetters) != true){ Play(); } if (evt.getSource() == exitButton){ System.exit(0); } // if (evt.getSource() == playAgain){ // removeAll(); // repaint(); // // } } } import java.awt.BasicStroke; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Line2D; import java.util.ArrayList; import javax.swing.JPanel; public class LinePanel extends JPanel { int x = 5; String theWord = ""; ArrayList&lt;String&gt; letterList; public LinePanel(int num, String t, ArrayList&lt;String&gt; s) { super(); // 300,350 setPreferredSize(new Dimension(400,400)); setBackground(Color.RED); this.x = num; this.theWord = t; letterList = cloneList(s); } private ArrayList&lt;String&gt; cloneList(ArrayList&lt;String&gt; aList) { ArrayList&lt;String&gt; clonedList = new ArrayList&lt;String&gt;(aList.size()); for (String letter : aList) clonedList.add(letter); return clonedList; } public int getX() { return x; } public void setX(int x) { this.x = x; } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; if (x == 15 || x != 15){ Line2D line = new Line2D.Double(0, 250, 80, 250); // Creates base Line2D line2 = new Line2D.Double(40, 50, 40, 250); // Creates vertical line Line2D line3 = new Line2D.Double(40, 50, 150, 50); // Creates horizontal line Line2D line4 = new Line2D.Double(150, 50, 150, 80); // Creates small line to hang the man g2.setStroke(new BasicStroke(5.0f)); // Line thickness g2.setColor(Color.BLACK); // Line colour //draw shape of line g2.draw(line); g2.draw(line2); g2.draw(line3); g2.draw(line4); int x1 = 0; int y = 320; for (int i = 0; i &lt; theWord.length();i++){ g2.drawLine(x1, y, x1 + 20, y); x1 += 50; } } // head if (x == 5 || x &lt; 5){ System.out.println("uheuheuha"); g2.setStroke(new BasicStroke(5.0f)); g2.drawOval(135, 85, 35, 35); } // body if (x == 4 || x &lt; 4){ g2.drawLine(150, 120, 150, 190); } // left arm if (x == 3 || x &lt; 3){ g2.drawLine(150, 140, 125, 155); } // right arm if (x == 2 || x &lt; 2){ g2.drawLine(150, 140, 175, 155); } // left leg and foot if (x == 1 || x &lt; 1){ g2.drawLine(150, 190, 125, 200); // leg g2.drawLine(125, 200, 120, 190); // foot } // right leg and foot if (x == 0){ g2.drawLine(150, 190, 175, 200); // leg g2.drawLine(175, 200, 180, 190); // foot } // Show whole word on screen if (x == 20){ int x1 = 3; int y = 317; String temp = ""; for (int i = 0; i &lt; theWord.length(); i++){ if ( letterList.contains(theWord.substring(i, i+1)) == true){ temp += theWord.substring(i, i+1); } else{ temp += " "; } } for (int i = 0; i &lt; temp.length() ;i++){ g2.setColor(Color.BLACK); Font font = new Font("Arial", Font.PLAIN, 25); g2.setFont(font); g2.drawString(temp.substring(i, i+1), x1, y); x1 += 50; } x1 = 3; y = 370; for (int i = 0; i &lt; letterList.size() ;i++){ if (theWord.contains(letterList.get(i)) == false){ g2.drawString(letterList.get(i), x1, y ); x1 += 50; } } } } } import java.awt.BasicStroke; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Line2D; import java.util.ArrayList; import javax.swing.JPanel; public class LinePanel extends JPanel { int x = 5; String theWord = ""; ArrayList&lt;String&gt; letterList; public LinePanel(int num, String t, ArrayList&lt;String&gt; s) { super(); // 300,350 setPreferredSize(new Dimension(400,400)); setBackground(Color.RED); this.x = num; this.theWord = t; letterList = cloneList(s); } private ArrayList&lt;String&gt; cloneList(ArrayList&lt;String&gt; aList) { ArrayList&lt;String&gt; clonedList = new ArrayList&lt;String&gt;(aList.size()); for (String letter : aList) clonedList.add(letter); return clonedList; } public int getX() { return x; } public void setX(int x) { this.x = x; } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; if (x == 15 || x != 15){ Line2D line = new Line2D.Double(0, 250, 80, 250); // Creates base Line2D line2 = new Line2D.Double(40, 50, 40, 250); // Creates vertical line Line2D line3 = new Line2D.Double(40, 50, 150, 50); // Creates horizontal line Line2D line4 = new Line2D.Double(150, 50, 150, 80); // Creates small line to hang the man g2.setStroke(new BasicStroke(5.0f)); // Line thickness g2.setColor(Color.BLACK); // Line colour //draw shape of line g2.draw(line); g2.draw(line2); g2.draw(line3); g2.draw(line4); int x1 = 0; int y = 320; for (int i = 0; i &lt; theWord.length();i++){ g2.drawLine(x1, y, x1 + 20, y); x1 += 50; } } // head if (x == 5 || x &lt; 5){ g2.setStroke(new BasicStroke(5.0f)); g2.drawOval(135, 85, 35, 35); } // body if (x == 4 || x &lt; 4){ g2.drawLine(150, 120, 150, 190); } // left arm if (x == 3 || x &lt; 3){ g2.drawLine(150, 140, 125, 155); } // right arm if (x == 2 || x &lt; 2){ g2.drawLine(150, 140, 175, 155); } // left leg and foot if (x == 1 || x &lt; 1){ g2.drawLine(150, 190, 125, 200); // leg g2.drawLine(125, 200, 120, 190); // foot } // right leg and foot if (x == 0){ g2.drawLine(150, 190, 175, 200); // leg g2.drawLine(175, 200, 180, 190); // foot } // Show whole word on screen if (x == 20){ int x1 = 3; int y = 317; String temp = ""; for (int i = 0; i &lt; theWord.length(); i++){ if ( letterList.contains(theWord.substring(i, i+1)) == true){ temp += theWord.substring(i, i+1); } else{ temp += " "; } } for (int i = 0; i &lt; temp.length() ;i++){ g2.setColor(Color.BLACK); Font font = new Font("Arial", Font.PLAIN, 25); g2.setFont(font); g2.drawString(temp.substring(i, i+1), x1, y); x1 += 50; } x1 = 3; y = 370; for (int i = 0; i &lt; letterList.size() ;i++){ if (theWord.contains(letterList.get(i)) == false){ g2.drawString(letterList.get(i), x1, y ); x1 += 50; } } } } } import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; public class PlayHangman { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub HangmanJFrame window = new HangmanJFrame(); window.setVisible(true); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload