简体   繁体   中英

What's wrong with my graphics?

On the first guess, the graphic that's supposed to show up doesn't show. And on the eighth guess, no graphics show up at all. (The user is allowed 10 tries before they lose). Any ideas on why this is happening? Help is appreciated!

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());
      }

Here is my MainWindow.java, where I display everything else in the window:

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");
   }
}

May not be the right / best way to handle this, I don't have any experience with Java Graphics. But, I looked at the drawImage function, and it returns true when the image is ready. So I tried wrapping the calls in a while loop, and now it draws all images.

while(!g.drawImage(background, 0, 0, null));

See API reference

Sample with your IF statement:

  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();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM