我正在創建一個骰子程序,並完成了。我唯一要做的就是能夠點擊複選框到相應的骰子上,這樣骰子就不會被捲起來,所以我可以將那些未經檢查的骰子捲起來。我會如何去做這件事?這裏是我的代碼:如何「凍結」滾動的某些骰子?
主模具類:
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,我只是不知道里面放什麼括號。我知道它與圖形類中的鎖定布爾方法有關,但我無法弄清楚該怎麼做,我需要一些幫助。
由於
只需將一個新的布爾型字段添加到'Die'類,您可以在列表程序中將其設置爲'true'或'false'。如果它是真的,那麼方法'roll()'不應該做任何事情。順便說一句,從這個類中刪除過時的'main'方法。 – Tom 2014-10-27 19:32:06
老實說,我不知道你想說什麼,通過添加一個布爾字段,然後將其設置爲true或false。 – 2014-10-27 19:37:47