我制作了一个Hangman游戏并且它正在运行,但我在整合游戏图形方面遇到了麻烦。我试图在每次收到错误的字母时添加图形,有人可以告诉我如何做到这一点吗?我认为它不起作用的原因是因为图形的方法是无效的,但我不知道如何解决这个问题。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Hangman extends JFrame {
private String[] wordList = {"computer", "sport", "denver", "university", "education",
"action", "competiion", "mysterious", "program", "complete"};
private JTextField inputField;
private JLabel lettersUsed;
static String letter;
static int[] wordLength = new int[10];
static int randomWord = (int) (Math.random() * 10);
private JLabel lines;
private int wrong = 0;
private int right = 0;
private Graphics2D g2d;
private JPanel center = new JPanel();
public Hangman() {
setSize(500, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Hangman Game");
JPanel bottomRight = new JPanel();
JPanel bottomLeft = new JPanel();
JPanel top = new JPanel();
JPanel bottom = new JPanel();
JPanel panel = new JPanel();
add(panel);
bottom.setLayout(new BoxLayout(bottom, BoxLayout.X_AXIS));
panel.setLayout(new BorderLayout());
panel.setOpaque(false);
top.setBorder(BorderFactory.createTitledBorder(""));
bottom.setBorder(BorderFactory.createTitledBorder(""));
inputField = new JTextField(1);
JLabel enterLetter = new JLabel("Enter a letter", JLabel.CENTER);
lettersUsed = new JLabel("Letters used: ", JLabel.CENTER);
lines = new JLabel("__ ", JLabel.CENTER);
top.add(enterLetter);
top.add(inputField);
bottomLeft.add(lettersUsed);
bottomRight.add(lines);
bottom.add(bottomLeft);
bottom.add(bottomRight);
panel.add(center, BorderLayout.CENTER);
panel.add(top, BorderLayout.NORTH);
panel.add(bottom, BorderLayout.SOUTH);
HangmanListener hl = new HangmanListener();
inputField.addActionListener(hl);
String line = "";
for (int i = 0; i < 10; i++) {
wordLength[i] = wordList[i].length();
}
int j = 0;
while (j < wordLength[randomWord]) {
line += "__ ";
j++;
}
lines.setText(line);
}
private class HangmanListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JTextField inputField = (JTextField) e.getSource();
letter = inputField.getText();
inputField.setText("");
lettersUsed.setText(lettersUsed.getText() + letter + " ");
char[] labelText = lines.getText().toCharArray();
char enteredChar = letter.charAt(0);
if (!wordList[randomWord].contains(letter)) {
wrong++;
if (wrong == 1) {
System.out.println("1st mistake, 5 more tries");
}
if (wrong == 2) {
System.out.println("2st mistake, 4 more tries");
}
if (wrong == 3) {
System.out.println("3st mistake, 3 more tries");
}
if (wrong == 4) {
System.out.println("4st mistake, 2 more tries");
}
if (wrong == 5) {
System.out.println("5st mistake, 1 more tries");
}
if (wrong == 6) {
System.out.println("You Lose");
System.out.println("Your word was: " + wordList[randomWord]);
}
if (wrong > 6) {
System.out.println("You already lose!");
}
return;
}
for (int i = 0; i < wordList[randomWord].length(); i++) {
if (wordList[randomWord].charAt(i) == enteredChar) {
labelText[3 * i] = ' ';
labelText[3 * i + 1] = enteredChar;
right++;
}
lines.setText(String.valueOf(labelText));
if (labelText.length / 3 == right) {
System.out.println("You Win");
}
}
}
}
public void paint(Graphics g) {
super.paint(g);
g2d = (Graphics2D) g;
g2d.setColor(Color.BLACK);
g2d.drawLine(50, 450, 50, 100);
g2d.drawLine(50, 100, 300, 100);
g2d.drawLine(300, 100, 300, 150);
/*
* g.drawOval(275, 150, 50, 50);
* g.drawLine(300, 200, 300, 300);
*
* g.drawLine(300, 225, 250, 300);
* g.drawLine(300, 225, 350, 300);
*
* g.drawLine(300, 300, 250, 400);
* g.drawLine(300, 300, 350, 400);
* g.setFont(new Font("", Font.PLAIN, 20));
* g.drawString("You Lose!", 250, 450);
*/
if (!wordList[randomWord].contains(letter)) {
if (wrong == 1) {
g2d.drawOval(275, 150, 50, 50);
}
if (wrong == 2) {
g2d.drawLine(300, 200, 300, 300);
}
if (wrong == 3) {
g2d.drawLine(300, 225, 250, 300);
}
if (wrong == 4) {
g2d.drawLine(300, 225, 350, 300);
}
if (wrong == 5) {
g2d.drawLine(300, 300, 250, 400);
}
if (wrong == 6) {
g2d.drawLine(300, 300, 350, 400);
g2d.setFont(new Font("", Font.PLAIN, 20));
g2d.drawString("You Lose!", 250, 450);
}
}
}
}
答案 0 :(得分:0)
您的代码中有两个问题:首先是您有刷新问题,添加
Hangman.this.repaint();
在第117行的return-Statement之前的。现在第二个问题变得可见:例如第3个错误你不仅需要绘制一条线,而是绘制椭圆形和两条线。但我想你可以自己解决这个问题。 ; - )