基本的Java骰子表示

时间:2015-01-25 20:19:15

标签: java dice

我试图用给定的类(Square,Circle和Canvas)来表示Java中的骰子,我用方块表示骰子的盒子,但在代表n个圆圈的时候我制作了一个Circle矩阵,但它没有出现,这是我的骰子构造函数:

public Dice(){

    dice = new Rectangle();

    Circle matrix[][] = new Circle[3][3];
    matrix[0][0] = new Circle();
    matrix[0][1] = null;
    matrix[0][2] = new Circle();
    matrix[1][0] = new Circle();
    matrix[1][1] = new Circle();
    matrix[1][2] = new Circle();
    matrix[2][0] = new Circle();
    matrix[2][1] = null;
    matrix[2][2] = new Circle();

// Circle 
    diameter = 20;
    xPositionDot = 20;
    yPositionDot = 15;
    colorDot = "blue";
    isVisibleDot = false;

// Box
    height = 100;
    width = 100;
    colorBox = "red";
    xPosition = 70;
    yPosition = 15;
    isVisible = false;
}   

所以我做错了什么想法?

1 个答案:

答案 0 :(得分:1)

对于我最后一次骰子游戏,我创建了一个类来绘制模具面。我将类实例化了6次,通过调用draw方法绘制了6个面,并将这些面保存在Image数组中。

package com.ggl.dice.game.view;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;

public class DieImage {

    /** Side of a die in pixels */
    private static final int    SIDE            = 64;

    private static final int    SPOT_DIAMETER   = 10;

    private Image               image;

    public DieImage() {
        image = new BufferedImage(SIDE, SIDE, 
                BufferedImage.TYPE_INT_RGB);
    }

    public Image draw(int count) {
        int w = image.getWidth(null);
        int h = image.getHeight(null);

        Graphics g = image.getGraphics();

        drawBorder(g, w, h);
        drawBackground(g, w, h);
        drawSpots(g, w, h, count);

        g.dispose();
        return image;
    }

    private void drawBorder(Graphics g, int w, int h) {
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, w, h);
    }

    private void drawBackground(Graphics g, int w, int h) {
        g.setColor(Color.WHITE);
        g.fillRect(3, 3, w - 6, h - 6);
    }

    private void drawSpots(Graphics g, int w, int h, int count) {
        g.setColor(Color.BLACK);

        switch (count) {
        case 1:
            drawSpot(g, w / 2, h / 2);
            break;
        case 3:
            drawSpot(g, w / 2, h / 2);
            // Fall thru to next case
        case 2:
            drawSpot(g, w / 4, h / 4);
            drawSpot(g, 3 * w / 4, 3 * h / 4);
            break;
        case 5:
            drawSpot(g, w / 2, h / 2);
            // Fall thru to next case
        case 4:
            drawSpot(g, w / 4, h / 4);
            drawSpot(g, 3 * w / 4, 3 * h / 4);
            drawSpot(g, 3 * w / 4, h / 4);
            drawSpot(g, w / 4, 3 * h / 4);
            break;
        case 6:
            drawSpot(g, w / 4, h / 4);
            drawSpot(g, 3 * w / 4, 3 * h / 4);
            drawSpot(g, 3 * w / 4, h / 4);
            drawSpot(g, w / 4, 3 * h / 4);
            drawSpot(g, w / 4, h / 2);
            drawSpot(g, 3 * w / 4, h / 2);
            break;
        }
    }

    private void drawSpot(Graphics g, int x, int y) {
        g.fillOval(x - SPOT_DIAMETER / 2, y - SPOT_DIAMETER / 2, 
                SPOT_DIAMETER, SPOT_DIAMETER);
    }

}