2013-06-12 82 views
0

所以我在我的計算機科學課上爲我的最終項目做了一個Java人的Hang人遊戲。我爲Hang人完成了模板(即按鈕並添加了所需的字)。但我不知道如何繪製Hang子手本身,以及如何隨機添加這些單詞(意思是讓他們開始猜詞)。我問的是,如果一封信被錯誤地猜出(或者正確地寫在文本框中),我該如何畫Hang子手。對於我已經完成JButtons的信件,因爲我認爲它會更容易一些,並增加了動作偵聽器。我知道代碼很長,但不注意模板,方法Start是我的話和提示。先謝謝你。別介意我沒有主要方法,我有一個驅動類來擴展這個類。 (這是要求之一)如何在JFrame中隨機分配Hangman遊戲中的單詞?

解決!

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

} 
+0

'setLayout(null);'Java GUI可能需要在多種平臺上工作,使用不同的屏幕分辨率和使用不同的PLAF。因此,它們不利於組件的準確放置。要爲可靠的GUI組織組件,請使用佈局管理器或它們的組合,以及用於空白區域的佈局填充和邊框。 –

+0

我有一個問題。我嘗試過使用不同的佈局管理器,但他們都沒有幫助我組織我的GUI,這是我老師的想法,實際上使用絕對位置。感謝您的洞察力!我實際上遇到的問題是繪製hang子手的線條(使用絕對位置以及[x1,y1,x2,y2]) –

+1

*「這是我老師的想法,實際使用絕對位置」*將它們拍打在頭上並告訴他們醒來。如果他們起訴你從字面上接受這個建議,不要牽涉我。 –

回答

0

吸引你可以在Graphics類使用drawline線。

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

希望它幫助。 Regards -Max

+0

Graphics類的問題在於它從字面上刪除了其他所有內容,剩下的唯一東西就是該行。使用公共空白顏料(圖形g) \t { \t g.drawLine(25,0,25,50); \t g.setColor(Color.black); \t} –

+0

我想我會用一張實際的照片。 –