2014-10-27 48 views
0

我正在創建一個骰子程序,並完成了。我唯一要做的就是能夠點擊複選框到相應的骰子上,這樣骰子就不會被捲起來,所以我可以將那些未經檢查的骰子捲起來。我會如何去做這件事?這裏是我的代碼:如何「凍結」滾動的某些骰子?

主模具類:

public class Die { 
 

 
    public static void main(String[] args) {} 
 
    protected int face = 1; 
 

 
    void roll() { 
 
    face = (int)(Math.random() * 6 + 1); 
 
    } 
 

 
    public int getFace() { 
 
    return face; 
 
    } 
 

 
    public int setFace() { 
 
    return face; 
 
    }

Graphics類的,我畫了一切,設置方法:

public class Graphics extends Die { 
 
    private int x, y; 
 
    boolean locked; { 
 

 
    locked = !locked; 
 
    } 
 

 

 
    public Graphics(int x, int y) { 
 
    this.x = x; 
 
    this.y = y; 
 
    } 
 

 
    public void draw(DrawingKit dk) { 
 
    Rectangle2D.Float die1 = new Rectangle2D.Float(x, y, 80, 80); 
 

 
    Ellipse2D.Float topleft = new Ellipse2D.Float(x + 3, y + 3, 18, 18); 
 
    Ellipse2D.Float topright = new Ellipse2D.Float(x + 55, y + 3, 18, 18); 
 
    Ellipse2D.Float middleleft = new Ellipse2D.Float(x + 3, y + 28, 18, 18); 
 
    Ellipse2D.Float middle = new Ellipse2D.Float(x + 28, y + 28, 18, 18); 
 
    Ellipse2D.Float middleright = new Ellipse2D.Float(x + 55, y + 28, 18, 18); 
 
    Ellipse2D.Float bottomleft = new Ellipse2D.Float(x + 3, y + 53, 18, 18); 
 
    Ellipse2D.Float bottomright = new Ellipse2D.Float(x + 55, y + 53, 18, 18); 
 
    dk.setPaint(Color.red); 
 
    dk.fill(die1); 
 
    if (face > 1) { 
 
     dk.draw(topleft); 
 
     dk.setPaint(Color.black); 
 
     dk.fill(topleft); 
 
    } 
 
    if (face > 3) { 
 
     dk.draw(topright); 
 
     dk.setPaint(Color.black); 
 
     dk.fill(topright); 
 
    } 
 
    if (face == 6) { 
 
     dk.draw(middleleft); 
 
     dk.setPaint(Color.black); 
 
     dk.fill(middleleft); 
 
    } 
 
    if (face % 2 == 1) { 
 
     dk.draw(middle); 
 
     dk.setPaint(Color.black); 
 
     dk.fill(middle); 
 
    } 
 
    if (face == 6) { 
 
     dk.draw(middleright); 
 
     dk.setPaint(Color.black); 
 
     dk.fill(middleright); 
 
    } 
 
    if (face > 3) { 
 
     dk.draw(bottomleft); 
 
     dk.setPaint(Color.black); 
 
     dk.fill(bottomleft); 
 
    } 
 
    if (face > 1) { 
 
     dk.draw(bottomright); 
 
     dk.setPaint(Color.black); 
 
     dk.fill(bottomright); 
 
    } 
 
    } 
 

 

 

 
    public static void main(String[] args) { 
 

 
    } 
 
}

輥類,其中的複選框,一切都跑了:

public class Roll { 
 

 

 
    public static void main(String[] args) { 
 

 
    JPanel topPanel = new JPanel(); 
 
    Icon Dice = new ImageIcon("button.png"); 
 
    final JButton button1 = new JButton(Dice); 
 
    JCheckBox Dice1 = new JCheckBox("Dice 1"); 
 
    Dice1.setBounds(100, 100, 100, 100); 
 
    JCheckBox Dice2 = new JCheckBox("Dice 2"); 
 
    JCheckBox Dice3 = new JCheckBox("Dice 3"); 
 
    JCheckBox Dice4 = new JCheckBox("Dice 4"); 
 
    JCheckBox Dice5 = new JCheckBox("Dice 5"); 
 

 
    int cnt1 = 1, cnt2 = 2, cnt3 = 3, cnt4 = 4, cnt5 = 5, cnt6 = 6; 
 

 
    final DrawingKit dk = new DrawingKit("Dice Game"); 
 
    dk.addPanel(topPanel); 
 
    dk.setBackground(Color.blue); 
 
    topPanel.setBackground(Color.red); 
 
    topPanel.setSize(500, 500); 
 

 
    final Graphics die1 = new Graphics(0, 45); 
 
    die1.roll(); 
 
    die1.draw(dk); 
 
    final Graphics die2 = new Graphics(100, 45); 
 
    die2.roll(); 
 
    die2.draw(dk); 
 
    final Graphics die3 = new Graphics(200, 45); 
 
    die3.roll(); 
 
    die3.draw(dk); 
 
    final Graphics die4 = new Graphics(300, 45); 
 
    die4.roll(); 
 
    die4.draw(dk); 
 
    final Graphics die5 = new Graphics(400, 45); 
 
    die5.roll(); 
 
    die5.draw(dk); 
 

 
    topPanel.add(button1); 
 
    button1.setToolTipText("Click this button to roll the dice."); 
 
    button1.setForeground(Color.red); 
 
    button1.setContentAreaFilled(false); 
 
    button1.setFocusPainted(false); 
 
    button1.setBorderPainted(false); 
 
    button1.setFont(new Font("Arial", Font.BOLD, 15)); 
 
    button1.setPreferredSize(new Dimension(40, 25)); 
 
    button1.addActionListener(new ActionListener() { 
 
     public void actionPerformed(ActionEvent event) { 
 
     die1.roll(); 
 
     die1.draw(dk); 
 
     die2.roll(); 
 
     die2.draw(dk); 
 
     die3.roll(); 
 
     die3.draw(dk); 
 
     die4.roll(); 
 
     die4.draw(dk); 
 
     die5.roll(); 
 
     die5.draw(dk); 
 

 

 
     } 
 

 

 
    }); 
 

 
    topPanel.add(Dice1); 
 

 
    Dice1.addActionListener(new ActionListener() { 
 
     public void actionPerformed(ActionEvent event) { 
 

 

 

 

 
     } 
 

 

 
    }); 
 
    topPanel.add(Dice2); 
 
    topPanel.add(Dice3); 
 
    topPanel.add(Dice4); 
 
    topPanel.add(Dice5); 
 

 
    } 
 
}

正如你所看到的,我有動作監聽準備複選框1,我只是不知道里面放什麼括號。我知道它與圖形類中的鎖定布爾方法有關,但我無法弄清楚該怎麼做,我需要一些幫助。

由於

+0

只需將一個新的布爾型字段添加到'Die'類,您可以在列表程序中將其設置爲'true'或'false'。如果它是真的,那麼方法'roll()'不應該做任何事情。順便說一句,從這個類中刪除過時的'main'方法。 – Tom 2014-10-27 19:32:06

+0

老實說,我不知道你想說什麼,通過添加一個布爾字段,然後將其設置爲true或false。 – 2014-10-27 19:37:47

回答

0

一種可能的方式將被增加一個新的字段添加到該類Die將表示鎖定:

public class Die { 
    // omitted existing fields 
    private boolean locked = false; 

    void roll() { 
     if (!locked) { 
      face = (int)(Math.random() * 6 + 1); 
     } 
    } 
    // omitted existing getter and setter 

    public void setLocked(final boolean locked) { 
     this.locked = locked; 
    } 

    public boolean isLocked() { // this getter is maybe not necessary 
     return locked; 
    } 
} 

這種鎖定可防止從改變電流值的roll方法。

和你的聽衆的第一個複選框看起來是這樣的:

Dice1.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent event) { 
     die1.setLocked(Dice1.isSelected()); 
    } 
}); 

此偵聽器設置複選框的當前狀態到locked領域。因此,如果選中複選框,則locked字段將設置爲true,方法調用die1.roll()不會更改其當前值。

這樣你就不必更改當前的roll()draw(dk)來電。

不要忘記申報您的Dice1組件作爲最終,否則將無法使用它的監聽器裏:

final JCheckBox Dice1 = new JCheckBox("Dice 1"); 

它也可能需要聲明die1final,如好。

+0

收到此:多個標誌物在該行 \t - 不能是指在限定的非最終局部變量Dice1一個 \t封閉範圍 \t - 的方法setLocked(布爾值)是未定義的類型圖形 \t - 無法指在包含範圍內定義的非最終局部變量Dice1 – 2014-10-27 19:55:49

+0

@JohnClark您需要將'Dice1'聲明爲'final'(在偵聽器中使用它)。請參閱編輯。 – Tom 2014-10-27 19:59:35

+0

是的!非常感謝。我還有最後一個問題。爲你。我無法從屏幕頂部移動複選框,並且使用設置的位置不會移動它們。我該怎麼辦? – 2014-10-27 21:31:33