我正在创建一个骰子程序,并完成了。我唯一要做的就是能够点击复选框到相应的骰子上,这样骰子就不会被卷起来,所以我可以将那些未经检查的骰子卷起来。我会如何去做这件事?这里是我的代码:如何“冻结”滚动的某些骰子?
主模具类:
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