在第一次猜测时,应该显示的图形不显示。在第八个猜测中,根本没有显示任何图形。 (用户在失败前可以进行10次尝试)。有关为什么会发生这种情况的任何想法?感谢帮助!
HangmanFigure.java
import java.awt.*;
import javax.swing.*;
public class HangmanFigure extends JPanel {
private int guesses;
private Image background;
public HangmanFigure() {
super();
guesses = 0;
setPreferredSize(new Dimension(400, 400));
//setting background image to JPEG
background = Toolkit.getDefaultToolkit().createImage("drac0.jpg");
//set to true because we want to see the background
setOpaque(true);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(background, 0, 0, null);
//Testing number of guesses
System.out.println(guesses);
//right line
if(guesses >= 1) {
background = Toolkit.getDefaultToolkit().createImage("drac1.jpg");
g.drawImage(background, 0, 0, null);
}
//top and slant line
if(guesses >= 2) {
background = Toolkit.getDefaultToolkit().createImage("drac2.jpg");
g.drawImage(background, 0, 0, null);
}
//string
if(guesses >= 3) {
background = Toolkit.getDefaultToolkit().createImage("drac3.jpg");
g.drawImage(background, 0, 0, null);
}
//head
if(guesses >= 4) {
background = Toolkit.getDefaultToolkit().createImage("drac4.jpg");
g.drawImage(background, 0, 0, null);
}
//body
if(guesses >= 5) {
background = Toolkit.getDefaultToolkit().createImage("drac5.jpg");
g.drawImage(background, 0, 0, null);
}
//left arm
if(guesses >= 6) {
background = Toolkit.getDefaultToolkit().createImage("drac6.jpg");
g.drawImage(background, 0, 0, null);
}
//right hand
if(guesses >= 7) {
background = Toolkit.getDefaultToolkit().createImage("drac7.jpg");
g.drawImage(background, 0, 0, null);
}
//left leg
if(guesses >= 8) {
background = Toolkit.getDefaultToolkit().createImage("drac8.jpg");
g.drawImage(background, 0, 0, null);
}
//right leg
if(guesses >= 9) {
background = Toolkit.getDefaultToolkit().createImage("drac9.jpg");
g.drawImage(background, 0, 0, null);
}
//death face and stake
if(guesses == 10) {
background = Toolkit.getDefaultToolkit().createImage("drac10.jpg");
g.drawImage(background, 0, 0, null);
}
}
public void set() {
guesses++;
paintComponent(this.getGraphics());
}
这是我的MainWindow.java,我在窗口中显示其他所有内容:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MainWindow extends JFrame {
private int remainingGuesses;
private String wrongGuesses;
private String word;
private String visible;
public MainWindow(String toGuess) {
remainingGuesses = 10;
wrongGuesses = "";
word = toGuess;
visible = "";
for(int i = 0; i < word.length(); ++i) {
visible += "_ ";
}
JPanel corePanel = new JPanel();
corePanel.setLayout(new BorderLayout());
final JLabel status = new JLabel("You have "+remainingGuesses+" remaining", SwingConstants.CENTER);
final JLabel wrong = new JLabel("Wrong guesses so far: "+wrongGuesses);
final JLabel visibleLabel = new JLabel(visible, SwingConstants.CENTER);
final JTextField input = new JTextField();
JPanel southPanel = new JPanel(new GridLayout(4, 1));
southPanel.add(status);
southPanel.add(visibleLabel);
southPanel.add(input);
southPanel.add(wrong);
corePanel.add(southPanel, BorderLayout.SOUTH);
final HangmanFigure hf = new HangmanFigure();
corePanel.add(hf, BorderLayout.CENTER);
this.add(corePanel, BorderLayout.CENTER);
input.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String text = input.getText();
if(text.length() == 1 && text.matches("[a-z]")) {
boolean guessFound = false;
for(int i = 0; i < word.length(); ++i) {
if(text.charAt(0) == word.charAt(i)) {
guessFound = true;
String newVisible = "";
for(int j = 0; j < visible.length(); ++j) {
if(j == (i*2)) {
newVisible += word.charAt(i);
}
else {
newVisible += visible.charAt(j);
}
}
visible = newVisible;
visibleLabel.setText(visible);
}
}
if(!guessFound) {
if(--remainingGuesses > 0) {
status.setText("You have "+remainingGuesses+" guesses remaining");
wrongGuesses += text+" ";
wrong.setText("Wrong guesses so far: "+wrongGuesses);
hf.set();
}
else {
status.setText("You lost: the word was "+word);
input.setEnabled(false);
}
}
else {
String actualVisible = "";
for(int i = 0; i < visible.length(); i+=2) {
actualVisible += visible.charAt(i);
}
if(actualVisible.equals(word)) {
status.setText("Congratulations, you have won!");
input.setEnabled(false);
}
}
}
else {
//displays a dialogue box if user inputs anything other than lowercase letter.
JFrame frame = new JFrame();
JOptionPane.showMessageDialog(frame, "Please enter a lowercase letter!", "Inane warning", JOptionPane.WARNING_MESSAGE);
}
input.setText("");
}
});
this.pack();
this.setLocationRelativeTo(null);
this.setTitle("Hangman");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new MainWindow("cat");
}
}
答案 0 :(得分:0)
可能不是处理此问题的正确/最佳方式,我对Java Graphics没有任何经验。但是,我查看了drawImage
函数,它在图像准备好后返回true。所以我尝试在while循环中包装调用,现在它绘制所有图像。
while(!g.drawImage(background, 0, 0, null));
使用您的IF语句示例:
try {
.....
if(guesses >= 1) {
background = Toolkit.getDefaultToolkit().createImage("drac1.jpg");
// old code - g.drawImage(background, 0, 0, null);
while(!g.drawImage(background, 0, 0, null)) Thread.sleep(100); // - new code
}
if(guesses >= 2) {
background = Toolkit.getDefaultToolkit().createImage("drac2.jpg");
// old code - g.drawImage(background, 0, 0, null);
while(!g.drawImage(background, 0, 0, null)) Thread.sleep(100); // - new code
}
.....
} catch(InterruptedException ex) {
ex.printStackTrace();
}