-1
我做了一個Hang子手遊戲並且它正在工作,但是我在整合遊戲圖形時遇到了麻煩。每次當你收到一封錯誤信息時,我都會試着添加一張圖片,有人能告訴我如何做到這一點嗎?我認爲它不工作的原因是因爲圖形所在的方法是無效的,但我不知道如何解決這個問題。需要幫助讓圖形在Hang子手遊戲上工作
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);
}
}
}
}