Hangman Graphics

时间:2013-12-05 17:48:11

标签: java if-statement graphics graphics2d

我无法通过我的hangman程序显示图形。

这是我的引擎类的代码:

public static void main(String [] args)     {

    //hangman viewer stuff
    //////////////////////////////////////////////////////////////
    JFrame frame = new JFrame();

    frame.setSize(200,375); //invoked the method setSize on the implicit parameter frame
    frame.setTitle("Hangman"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    HangmanComponent g = new HangmanComponent();
    frame.add(g);

    frame.setVisible(true);

    ///////////////////////////////////////////////////////////////

    String wordd = JOptionPane.showInputDialog("Type in a word.");
    int length = wordd.length();
    String blank = "_ ";
    String word2 = new String("");
    int guesscount = 10;

    ArrayList<String>answers=new ArrayList<String>(); //creates reference to empty structure that will contain references
    char blanks[]=new char[wordd.length()]; //creates an array with the same number of terms as the length of the word
    for (int i=0; i<length; i++)//fills the array with blanks corresponding to the length of the word
    {
        blanks[i] = '_';
    }

    HangmanComponent y = new HangmanComponent();

    while (true)
    {
        String letter = JOptionPane.showInputDialog("Guess a letter! You have "+guesscount+" guesses."+"\n"+answers+"\n"+Arrays.toString(blanks).replace(",", " ").replace("[","").replace("]","")); //Prints a space
        char letterchar = letter.charAt(0); //converts string letter to char letterchar
        int idx = 0;
        boolean found = false;
        answers.add(letter); //adds the string to the arraylist answers

        while (idx >= 0 && idx < length) //idx is greater than or equal to 0 but less than the length of the word
        {
            //System.out.println("idx = " + idx);
            idx = wordd.indexOf(letter, idx); //idx is the index of "letter" in "wordd" and finds all instances of the letter
            //System.out.println("idx = " + idx + ", guesscount = " + guesscount);
            if (idx != -1) //if idx is not -1 (the letter exists in the word)
            {
                found = true;
                blanks[idx] = letterchar; //sets the term in the array equal to the letter 
                idx += 1; //idx=idx+1
            } 
            else 
            {
                guesscount=guesscount-1;
                y.nextStage();
                y.printStage();
                frame.add(y);
                frame.setVisible(true);
                break;
            }
        }

        if (found)
        {
            JOptionPane.showMessageDialog(null, Arrays.toString(blanks).replace(",", " ").replace("[","").replace("]","")+"\n"+"You found a letter!"+"\n"+answers);
        }
        else 
        {
            JOptionPane.showMessageDialog(null, Arrays.toString(blanks).replace(",", " ").replace("[","").replace("]","")+"\n"+"That letter is not in the word! Guess again!"+"\n"+answers);

            if (guesscount == 0)
            {
                JOptionPane.showMessageDialog(null, "Sorry, you're all out of guesses. The answer was '"+wordd+".' Thanks for playing!");
                break;
            }
        }

        char [] lettersArray = wordd.toCharArray(); //converts word to array of chars

        if (Arrays.equals(blanks, lettersArray))//compares array of blanks to array of letters
        {
            JOptionPane.showMessageDialog(null, "You guessed the word! Thanks for playing!");
            break;
        }
    }
}

这是我的HangmanComponent类的代码(带有图形)

int stage=0;
public void nextStage()
{
    stage++;
}

public void printStage()
{
    System.out.println("Your stage number is:"+stage);
}

public void paintComponent(Graphics g)
{
    Graphics2D g2 = (Graphics2D) g; 

    g2.setStroke(new BasicStroke(5,BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
    g2.setColor(Color.BLACK); 
    g.drawLine(50, 30, 50, 10);
    g.drawLine(50, 10, 130, 10);
    g.drawLine(130, 10, 130, 300);
    g.drawLine(20, 300, 150, 300);

    if (stage==1)
    {
        //draws the head
        g2.setStroke(new BasicStroke(3,BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
        Ellipse2D.Double head = new Ellipse2D.Double(25, 30, 50, 50);
        g2.draw(head); 
    }

    else if (stage==2)
    {
        //draws the body
        g.drawLine(50, 80, 50, 180);
    }
    else if (stage==3)
    {
        //draws the left arm
        g.drawLine(10, 150, 50, 100);
    }
    else if (stage==4)
    {
        //draws the right arm
        g.drawLine(50, 100, 90, 150);
    }
    else if (stage==5)
    {
        //draws the left leg
        g.drawLine(30, 250, 50, 180);
    }
    else if (stage==6)
    {
        //draws the right leg
        g.drawLine(50, 180, 70, 250);
    }
    else if (stage==7)
    {
        //draws the left eye
        Ellipse2D.Double lefteye = new Ellipse2D.Double(40, 50, 1, 1);
        g2.draw(lefteye); 
        g2.fill(lefteye);
    }
    else if (stage==8)
    {
        //draws the right eye
        Ellipse2D.Double righteye = new Ellipse2D.Double(58, 50, 1, 1);
        g2.draw(righteye); 
        g2.fill(righteye);
    }
    else if (stage==9)
    {
        //draws the mouth
        Arc2D.Double mouth = new Arc2D.Double(40.00, 50.00, 20.00, 20.00, 180.00, 190.00, Arc2D.OPEN); 
        g2.draw(mouth); 
    }
}

现在,hangman程序运行得非常好,但只有在用户第一次输入错误的字母时才会添加图形。如何输入错误的字母,如何输入图形?

0 个答案:

没有答案