1

您好,我在 switch 语句的帮助下创建了一个骰子以显示骰子的正确面,现在我打算将其重写为一个数组,但我不知道这应该是什么样子..(我是新手Java,它只是用于学习目的)有人可以给我看一个它必须是什么样子的小例子吗?

这是包含 switch 语句的类:

package h05Dobbelsteen;

import java.awt.*;

import javax.swing.JPanel;

public class DobbelSteen extends JPanel {

private final static int SPOT_DIAMETER = 40; // diameter dobbelsteen rondjes
private int faceValue; // getoonde waarde op scherm

public DobbelSteen() {

}

/*
 * roll de dobbelsteen
 */
public int roll() {

    int val = (int) (6*Math.random() + 1); // bepaal getal tussen 1 - 6
    setValue(val);
    System.out.println(val);
    return val;

}

/*
 * set de waarde van de roll
 */
public void setValue(int spots) {
    faceValue = spots;
    repaint();
}

/*
 * get de waarde van de roll
 */
public int getValue() {
    return faceValue;
}

/*
 * teken de view van de dobbelsteen
 */
public void paintComponent(Graphics g) {

    int w = getWidth();  // Get height and width
    int h = getHeight();

    // Graphics naar 2d
    Graphics2D g2 = (Graphics2D)g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);

    //... Paint background
    g2.setColor(Color.WHITE);
    g2.fillRect(0, 0, w, h);
    g2.setColor(Color.BLACK);

    g2.drawRect(0, 0, w-1, h-1);  // Draw border

    switch (faceValue) {
        case 1:
            drawSpot(g2, w/2, h/2);
            break;
        case 3:
            drawSpot(g2, w/2, h/2);

        case 2:
            drawSpot(g2, w/4, h/4);
            drawSpot(g2, 3*w/4, 3*h/4);
            break;
        case 5:
            drawSpot(g2, w/2, h/2);
        case 4:
            drawSpot(g2, w/4, h/4);
            drawSpot(g2, 3*w/4, 3*h/4);
            drawSpot(g2, 3*w/4, h/4);
            drawSpot(g2, w/4, 3*h/4);
            break;
        case 6:
            drawSpot(g2, w/4, h/4);
            drawSpot(g2, 3*w/4, 3*h/4);
            drawSpot(g2, 3*w/4, h/4);
            drawSpot(g2, w/4, 3*h/4);
            drawSpot(g2, w/4, h/2);
            drawSpot(g2, 3*w/4, h/2);
            break;
    }
}

/*
 * Teken de spots
 */
private void drawSpot(Graphics2D g2, int x, int y) {
    g2.fillOval(x-SPOT_DIAMETER/2, y-SPOT_DIAMETER/2, SPOT_DIAMETER, SPOT_DIAMETER);
}

}
4

1 回答 1

2

就像是:

private class Point {
  int x;
  int y;
  Point(int x, int y) {
    this.x = x;
    this.y = y;
}

Point[][] pointSpecs = new Point[][] { {new Point( .5, .5) }, 
                                       {new Point(.25, .25), new Point(.75, .75)},
                                  ...};

这是数组数组的声明。第一个索引是骰子值 1(因为 java 数组是零索引的)。每个位置都有一个需要绘制的点数组。只有乘数在那里,你需要乘以你的宽度和高度。

要使用:

public void paintComponent(Graphics g) {

int w = getWidth();  // Get height and width
int h = getHeight();

// Graphics naar 2d
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON);

//... Paint background
g2.setColor(Color.WHITE);
g2.fillRect(0, 0, w, h);
g2.setColor(Color.BLACK);

g2.drawRect(0, 0, w-1, h-1);  // Draw border

Point[] points = pointSpecs[faceValue-1];
for (Point point : points) {
  drawSpot(g2, w*point.x, h*point.y);
}

您需要填写其余的点值...

于 2012-10-30T21:03:16.663 回答