0

所以我在我的计算机科学课上为我的期末项目用 Java 做一个 Hang man 游戏。我已经完成了 Hang man 的模板(即按钮并添加了所需的文字)。但我不知道如何绘制刽子手本身以及如何将单词(意思是他们将开始猜测单词)随机添加到程序中。我要问的是,如果一个字母被猜错(或正确,写在文本字段中),我该如何画刽子手。对于字母我做了 JButtons,因为我认为它会更容易,并添加了动作侦听器。我知道代码很长,但不要注意模板,方法 Start 是我有我的话和提示的地方。先感谢您。别介意我没有 main 方法,我有一个扩展这个类的驱动程序类。(这是要求之一)

解决了!

    package hangman;
import javax.swing.*;
import java.util.Random;
import java.util.Scanner;
import java.awt.event.*;
import java.awt.*;
public class Hangman extends JFrame
{
    JPanel panel = new JPanel();
    JButton restart = new JButton("Restart");
    JButton a = new JButton("A"), b = new JButton("B"), c = new JButton("C"), d = new JButton("D"), ee = new JButton("E"), f = new JButton("F"), 
            g = new JButton("G"), h = new JButton("H"), i = new JButton("I"), j = new JButton("J"), k = new JButton("K"), l = new JButton("L"), 
            m = new JButton("M"), n = new JButton("N"), o = new JButton("O"), p = new JButton("P"), q = new JButton("Q"), r = new JButton("R"), 
            s = new JButton("S"), t = new JButton("T"), u = new JButton("U"), v = new JButton("V"), w = new JButton("W"), x = new JButton("X"), 
            y = new JButton("Y"), z = new JButton("Z");
    JButton exit = new JButton("Exit");
    JButton hint = new JButton("Hint");
    JLabel title = new JLabel("Welcome to Hang Man!  ");
    JLabel hintWord = new JLabel("Click the Button to show a hint!");
    Font font = new Font("Comic Sans MS", Font.ITALIC, 24);
    JTextField theWord = new JTextField();
    Font font1 = new Font("Comic Sans MS", Font.BOLD, 34);

    public Hangman()
    {

        //Panel adding
        panel.add(restart);
        panel.add(theWord);
        panel.add(hint);
        panel.add(exit);
        panel.add(title);
        panel.add(hintWord);
        panel.add(a);
        panel.add(b);
        panel.add(c);
        panel.add(d);
        panel.add(ee);
        panel.add(f);
        panel.add(g);
        panel.add(h);
        panel.add(j);
        panel.add(k);
        panel.add(l);
        panel.add(m);
        panel.add(n);
        panel.add(o);
        panel.add(p);
        panel.add(q);
        panel.add(r);
        panel.add(s);
        panel.add(t);
        panel.add(u);
        panel.add(v);
        panel.add(w);
        panel.add(x);
        panel.add(y);
        panel.add(z);
        add(panel);

        title.setFont(font);

         // JFrame properties 

         setSize(1024, 700);
         setTitle("Hangman");
         setLocationRelativeTo(null);
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         setVisible(true);

         this.setLayout(null);
         panel.setLayout(null);

         //Adding to the panel
         panel.add(restart);
         panel.add(theWord);
         panel.add(hint);
         panel.add(exit);
         panel.add(title);
         panel.add(hintWord);
         panel.add(a);
         panel.add(b);
         panel.add(c);
         panel.add(d);
         panel.add(ee);
         panel.add(f);
         panel.add(g);
         panel.add(h);
         panel.add(j);
         panel.add(k);
         panel.add(l);
         panel.add(m);
         panel.add(n);
         panel.add(o);
         panel.add(p);
         panel.add(q);
         panel.add(r);
         panel.add(s);
         panel.add(t);
         panel.add(u);
         panel.add(v);
         panel.add(w);
         panel.add(x);
         panel.add(y);
         panel.add(z);
         add(panel);

         // Positioning
         restart.setLocation(25,25);
         theWord.setLocation(100, 530);
         theWord.setEnabled(false);
         theWord.setSize(800, 100);
         theWord.setHorizontalAlignment(theWord.CENTER);
         theWord.setFont(font1);
         theWord.setForeground(Color.RED);
         hint.setLocation(600, 150);
         exit.setLocation(900, 25);
         title.setLocation(350, 25);
         a.setLocation(600, 250);
         b.setLocation(650, 250);
         c.setLocation(700, 250);
         d.setLocation(750, 250);
         ee.setLocation(800, 250);
         f.setLocation(850, 250);
         g.setLocation(900, 250);
         h.setLocation(950, 250);
         j.setLocation(600, 280);
         k.setLocation(650, 280);
         l.setLocation(700, 280);
         m.setLocation(750, 280);
         n.setLocation(800, 280);
         o.setLocation(850, 280);
         p.setLocation(900, 280);
         q.setLocation(950, 280);
         r.setLocation(600, 310);
         s.setLocation(650, 310);
         t.setLocation(700, 310);
         u.setLocation(750, 310);
         v.setLocation(800, 310);
         w.setLocation(850, 310);
         x.setLocation(900, 310);
         y.setLocation(950, 310);
         z.setLocation(750, 340);
         hintWord.setLocation(670, 150);

         // Action Listeners
        restart.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    restartActionPerformed(e);
                }
            });
        hint.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    hintActionPerformed(e);
                }
            });
        exit.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    exitActionPerformed(e);
                }
            });
        a.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    aActionPerformed(e);
                }
            });
        b.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    bActionPerformed(e);
                }
            });
        c.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    cActionPerformed(e);
                }
            });
        d.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    dActionPerformed(e);
                }
            });
        ee.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    eeActionPerformed(e);
                }
            });
        f.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    fActionPerformed(e);
                }
            });
        g.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    gActionPerformed(e);
                }
            });
        h.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    hActionPerformed(e);
                }
            });
        j.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    jActionPerformed(e);
                }
            });
        k.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    kActionPerformed(e);
                }
            });
        l.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    lActionPerformed(e);
                }
            });
        m.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    mActionPerformed(e);
                }
            });
        n.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    nActionPerformed(e);
                }
            });
        o.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    oActionPerformed(e);
                }
            });
        p.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    pActionPerformed(e);
                }
            });
        q.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    qActionPerformed(e);
                }
            });
        r.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    rActionPerformed(e);
                }
            });
        s.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    sActionPerformed(e);
                }
            });
        t.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    tActionPerformed(e);
                }
            });
        u.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    uActionPerformed(e);
                }
            });
        v.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    vActionPerformed(e);
                }
            });
        w.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    wActionPerformed(e);
                }
            });
        x.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    xActionPerformed(e);
                }
            });
        y.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    yActionPerformed(e);
                }
            });
        z.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    zActionPerformed(e);
                }
            });


    }
    public void Start()
    {
        String words[] = new String[26];
        String hints[] = new String[26];
        // Words along with hints
        words[0] = "president";
        hints[0] = "Leader.";
        words[1] = "exclamation";
        hints[1] = "Shout out.";
        words[2] = "statement";
        hints[2] = "To say.";
        words[3] = "television";
        hints[3] = "You watch it.";
        words[4] = "physics";
        hints[4] = "Form of Science.";
        words[5] = "algebra";
        hints[5] = "Form of math.";
        words[6] = "geometry";
        hints[5] = "Form of math.";
        words[7] = "difficult";
        hints[7] = "Hard.";
        words[8] = "extreme";
        hints[8] = "Intense.";
        words[9] = "procedure";
        hints[9] = "Steps.";
        words[10] = "ship";
        hints[10] = "Big Boat.";
        words[11] = "soldier";
        hints[11] = "Army.";
        words[12] = "lunch";
        hints[12] = "Meal.";
        words[13] = "hockey";
        hints[13] = "Sports.";
        words[14] = "tennis";
        hints[14] = "Sports.";
        words[15] = "soccer";
        hints[15] = "Sports.";
        words[16] = "football";
        hints[16] = "Sports.";
        words[17] = "basketball";
        hints[17] = "Sports.";
        words[18] = "bias";
        hints[18] = "One sided.";
        words[19] = "magazine";
        hints[19] = "Form of book.";
        words[20] = "computer";
        hints[20] = "Microsoft.";
        words[21] = "internet";
        hints[21] = "World Wide Web.";
        words[22] = "allegedly";
        hints[22] = "Supposedly.";
        words[23] = "system";
        hints[23] = "Network.";
        words[24] = "unison";
        hints[24] = "As one.";
        words[25] = "excited";
        hints[25] = "Upbeat.";

    }
    // Action Listeners
    public void restartActionPerformed(ActionEvent e)
    {   
        a.setEnabled(true);
        b.setEnabled(true);
        c.setEnabled(true);
        d.setEnabled(true);
        ee.setEnabled(true);
        f.setEnabled(true);
        g.setEnabled(true);
        h.setEnabled(true);
        j.setEnabled(true);
        k.setEnabled(true);
        l.setEnabled(true);
        m.setEnabled(true);
        n.setEnabled(true);
        o.setEnabled(true);
        p.setEnabled(true);
        q.setEnabled(true);
        r.setEnabled(true);
        s.setEnabled(true);
        t.setEnabled(true);
        u.setEnabled(true);
        v.setEnabled(true);
        w.setEnabled(true);
        x.setEnabled(true);
        y.setEnabled(true);
        z.setEnabled(true);
        hintWord.setText("Click the Button to show a hint!");
    }
    public void exitActionPerformed(ActionEvent e)
    {   
        System.exit(0);
    }
    public void hintActionPerformed(ActionEvent e)
    {   
        hintWord.setText("");
    }
    public void aActionPerformed(ActionEvent e)
    {   
        a.setEnabled(false);
    }
    public void bActionPerformed(ActionEvent e)
    {   
        b.setEnabled(false);
    }
    public void cActionPerformed(ActionEvent e)
    {   
        c.setEnabled(false);
    }
    public void dActionPerformed(ActionEvent e)
    {   
        d.setEnabled(false);
    }
    public void eeActionPerformed(ActionEvent e)
    {   
        ee.setEnabled(false);
    }
    public void fActionPerformed(ActionEvent e)
    {   
        f.setEnabled(false);
    }
    public void gActionPerformed(ActionEvent e)
    {   
        g.setEnabled(false);
    }
    public void hActionPerformed(ActionEvent e)
    {   
        h.setEnabled(false);
    }
    public void jActionPerformed(ActionEvent e)
    {   
        j.setEnabled(false);
    }
    public void kActionPerformed(ActionEvent e)
    {   
        k.setEnabled(false);
    }
    public void lActionPerformed(ActionEvent e)
    {   
        l.setEnabled(false);
    }
    public void mActionPerformed(ActionEvent e)
    {   
        m.setEnabled(false);
    }
    public void nActionPerformed(ActionEvent e)
    {   
        n.setEnabled(false);
    }
    public void oActionPerformed(ActionEvent e)
    {   
        o.setEnabled(false);
    }
    public void pActionPerformed(ActionEvent e)
    {   
        p.setEnabled(false);
    }
    public void qActionPerformed(ActionEvent e)
    {   
        q.setEnabled(false);
    }
    public void rActionPerformed(ActionEvent e)
    {   
        r.setEnabled(false);
    }
    public void sActionPerformed(ActionEvent e)
    {   
        s.setEnabled(false);
    }
    public void tActionPerformed(ActionEvent e)
    {   
        t.setEnabled(false);
    }
    public void uActionPerformed(ActionEvent e)
    {   
        u.setEnabled(false);
    }
    public void vActionPerformed(ActionEvent e)
    {   
        v.setEnabled(false);
    }
    public void wActionPerformed(ActionEvent e)
    {   
        w.setEnabled(false);
    }
    public void xActionPerformed(ActionEvent e)
    {   
        x.setEnabled(false);
    }
    public void yActionPerformed(ActionEvent e)
    {   
        y.setEnabled(false);
    }
    public void zActionPerformed(ActionEvent e)
    {   
        z.setEnabled(false);
    }

}
4

1 回答 1

0

画出你可以drawlineGraphics课堂上使用的线条。

 g.drawLine(int x1, int y1, int x2, int y2);

文件:

使用当前颜色在点 (x1, y1) 和 (x2, y2) 之间绘制一条线。

将坐标写到一个int数组中,然后循环认为所有绳子的数组。

int[] line1 = new int[]{x1,y1,x2,y2};
int[] line2 = new int[]{x3,y3,x4,y4};

如果您想知道如何绘制头部,您可以使用drawPolygon

int[] head = new int[]{x,y};

final int headSize = 50; //width and height in px
g.drawPolygon(head[0], head[1], headSize, headSize);

希望它有所帮助。问候-马克斯

于 2013-06-12T16:36:02.633 回答