0

そのため、コンピューター サイエンス クラスの最終プロジェクトで、Java でハングマン ゲームを行っています。ハングマンのテンプレートを作成しました (つまり、ボタンと必要な単語を追加しました)。しかし、ハングマン自体を描画する方法と、プログラムに単語をランダムに追加する方法 (つまり、単語を推測し始める方法) がわかりません。私が求めているのは、文字が間違っていると推測された場合 (または、テキストフィールドに書き込むのが正しい場合)、ハングマンをどのように描画するかです。文字については、JButtons の方が簡単だと思い、アクション リスナーを追加しました。コードが非常に長いことは知っていますが、テンプレートには注意を払っていません。メソッド Start には、言葉とヒントがあります。前もって感謝します。メイン メソッドがないことを気にしないでください。このクラスを拡張するドライバー クラスがあります。(要件の一つでした)

解決しました!

    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

クラスdrawlineで使用できる線を描く。Graphics

 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 に答える