所以我正在为我的高中计算机科学课创建一个涉及图形的简单程序,而且我很难让图形实际绘制和更新。在JPanel上,我希望它能够绘制部分刽子手,因为用户无法猜出正确的字母/单词以及沿途显示和更新mysteryWord(即h-ll-,word是你好)。在终端上,用户将输入他们的猜测。
现在我已将其设置为仅绘制随机线条。唯一的问题是它不会绘制它们。我不知道我做错了什么。
以下是Hangman代码:
import java.util.Scanner;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.util.ArrayList;
public class Hangman extends JPanel
{
private String mysteryWord; //The hidden word "----"
private String word; //The word to be guessed
private ArrayList<String> bank; //List of words to be guessed
private ArrayList<String> incorrect; //List of wrong guesses the user has made
private int guesses; //Number of guesses
private int drawing; //determines what to draw
/**
* Creates a Hangman object. word is the word to be guessed and mysteryWord is
* the word with all characters replaced with a blank "_"
*/
public Hangman()
{
drawing = 0;
guesses = 11;
bank = new ArrayList<String>();
incorrect = new ArrayList<String>();
bank.add("Spain");bank.add("France");bank.add("Germany");bank.add("Vietnam");bank.add("Italy");bank.add("Portugal");bank.add("United States");
bank.add("Argentina");bank.add("Canada");bank.add("Mexico");bank.add("China");bank.add("Japan");bank.add("Somalia");
bank.add("Soccer");bank.add("Football");bank.add("Baseball");bank.add("Tennis");bank.add("Basketball");bank.add("Games");
bank.add("One");
int random = (int)(Math.random() * bank.size());
word = bank.get(random);
mysteryWord = "";
for(int i = 0; i < word.length(); i++)
{
mysteryWord = mysteryWord + "-";
}
}
/**
* Creates a Hangman object. The word to be guessed is a word chosen by the used and the mysteryWord is the chosen
* word with all characters replaced with a "-";
*/
public Hangman(String str)
{
guesses = 11;
incorrect = new ArrayList<String>();
word = str;
mysteryWord = "";
for(int i = 0; i < str.length(); i++)
{
mysteryWord = mysteryWord + "-";
}
}
/**
* Sets the number of guesses
*/
public void setGuesses(int x)
{
guesses = x;
}
/**
* Returns the number of guesses
*/
public int getGuesses()
{
return guesses;
}
/**
* Makes a guess. The guessed word must have a length of 1, guessing one character or
* have a length of the hidden word, guessing the whole word
*/
public boolean makeGuess(String guess)
{
String updatedStr = "";
String str = guess;
boolean status = false;
if(guess.length() == 0 || guess.length() > 1 && guess.length() < word.length() || guess.length() > word.length())
{
System.out.println("You can only guess 1 letter or the whole word");
updatedStr = mysteryWord;
incorrect.add(guess);
}
else if(guess.length() == 1)
{
for(int i = 0; i < word.length(); i++)
{
if(word.substring(i,i+1).equalsIgnoreCase(guess))
{
updatedStr += guess;
status = true;
}
else
{
updatedStr += mysteryWord.substring(i,i+1);
}
}
if(status == false)
{
incorrect.add(guess);
}
}
else if(guess.equalsIgnoreCase(word))
{
status = true;
updatedStr = word;
guesses = 0;
}
else
{
updatedStr = mysteryWord;
incorrect.add(guess);
}
guesses--;
mysteryWord = updatedStr;
return status;
}
/**
* Returns whether or not the user's guessed string is equal to the hidden word
*/
public boolean isEqual()
{
if(mysteryWord.equalsIgnoreCase(word))
{
return true;
}
return false;
}
public String getIncorrectGuesses()
{
String wrong = "";
for(int i = 0; i < incorrect.size(); i++)
{
wrong = wrong + (incorrect.get(i) + ", ");
}
return wrong;
}
/**
* Returns the word the user has been guessing
*/
public String getMWord()
{
return mysteryWord;
}
/**
* Returns the word the user is trying to guess
*/
public String getWord()
{
return word;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
this.setBackground(Color.WHITE);
g.setColor(Color.BLACK);
g.drawString(mysteryWord, 25, 120);
switch(drawing)
{
case 1: g.drawLine(23, 150, 200, 150);break;
case 2: g.drawLine(23, 150, 250, 150);break;
case 3: g.drawLine(23, 150, 300, 150);break;
case 4: g.drawLine(23, 150, 800, 150);break;
case 5: g.drawLine(23, 150, 70, 150);break;
case 6: g.drawLine(23, 150, 40, 150);break;
case 7: g.drawLine(23, 150, 10, 150);break;
case 8: g.drawLine(23, 150, 50, 150);break;
case 9: g.drawLine(23, 150, 20, 150);break;
case 10: g.drawLine(23, 150, 90, 150);break;
case 11: g.drawLine(23, 200, 200, 150);break;
}
}
public void playHangman()
{
Scanner kb = new Scanner(System.in);
System.out.println("Time to play Hangman!");
System.out.println("Would you like to use your own word? Yes or no?");
String play = kb.nextLine();
if(play.equalsIgnoreCase("yes"))
{
System.out.println("What word would you like to use?");
String word = kb.nextLine();
Hangman test = new Hangman(word);
boolean status = false;
while(test.getGuesses() > 0)
{
System.out.println("Progress: " + test.getMWord());
System.out.println("Guesses: " + test.getGuesses());
System.out.println("Incorrect: " + test.getIncorrectGuesses());
System.out.print("Guess a character or word: ");
String guess = kb.nextLine();
if(test.makeGuess(guess) == true)
{
System.out.println("Correct!");
}
else
{
System.out.println("Wrong!");
drawing++;
}
if(test.isEqual() == true)
{
test.setGuesses(0);
status = true;
}
}
if(status == true)
{
System.out.println("Progress: " + test.getMWord());
System.out.println("The hidden word was: " + test.getWord());
System.out.println("Congratulations you win!");
}
else
{
System.out.println("Progress: " + test.getMWord());
System.out.println("You lost... better luck next time!");
System.out.println("The hidden word was: " + test.getWord());
}
}
else
{
Hangman test = new Hangman();
boolean status = false;
while(test.getGuesses() > 0)
{
System.out.println("Progress: " + test.getMWord());
System.out.println("Guesses: " + test.getGuesses());
System.out.println("Incorrect: " + test.getIncorrectGuesses());
System.out.print("Guess a character or word: ");
String guess = kb.nextLine();
if(test.makeGuess(guess) == true)
{
System.out.println("Correct!");
}
else
{
System.out.println("Wrong!");
drawing++;
}
if(test.isEqual() == true)
{
test.setGuesses(0);
status = true;
}
}
if(status == true)
{
System.out.println("Progress: " + test.getMWord());
System.out.println("Congratulations you win!");
}
else
{
System.out.println("Progress: " + test.getMWord());
System.out.println("You lost... better luck next time!");
System.out.println("The hidden word was: " + test.getWord());
}
}
}
}
这是用户为了玩游戏而运行的课程
import javax.swing.*;
import java.util.Scanner;
public class Tester
{
public static void main(String [] args)
{
Hangman player = new Hangman();
JFrame window = new JFrame("Hangman");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(500,300);
window.add(player);
window.setVisible(true);
player.playHangman();
}
}
最终我将修改我的代码,以便终端上显示的唯一内容是要求用户猜测并接收一个的程序,以及通知用户他们的猜测是否正确。但首先我必须让我的线条实际绘制,因为用户猜错了。
答案 0 :(得分:0)
一切都在主(事件调度)线程上,所以图形 从来没有机会做任何事情,因为你总是阻止 等待输入。从概念上讲,使用控制台为a提供输入 图形程序很糟糕。 - John3136
我很确定John3136是对的。你最好通过框架内的摇摆组件或不同的方式接收用户的输入。
如果您仍希望使用控制台进行输入,请在paintComponent()
方法的末尾调用repaint()方法。我试了一下,它对我来说很好。
答案 1 :(得分:0)
如果要更新图形(调用paintComponent方法),请调用repaint();