-5

I'm trying to make a representation of a dice in Java with a given classes(Square, Circle and Canvas), I have represent the box of the dice with the square but at the time of represent the n circles I have made a Circle matrix, but it doesn't appear, this is my dice constructor:

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;
}   

So any ideas in what I'm doing wrong?

Alex K
  • 8,269
  • 9
  • 39
  • 57

1 Answers1

1

For my last dice game, I created a class just to draw the die faces. I instantiated the class 6 times, drew the 6 faces by calling the draw method, and saved the faces in an Image array.

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);
    }

} 
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
  • Thank you, sorry for bother you, i'm starting with java but do you know is there any other way of representing the face of the dice, only using 3 classes: the canvas, the rectangle and the circle, i was told to use a Circle Matrix of 3x3, but i do't know how to make it appear in the same time with the Rectangle. – user3880826 Jan 25 '15 at 23:03
  • @user3880826: The Rectangle is the die face, with dimensions 0, 0, width, width. The Circle is a pip (dot) on the die face. The Canvas is a JPanel. You draw on a JPanel by overriding the paintComponent method. The actual Canvas class is obsolete. – Gilbert Le Blanc Jan 26 '15 at 16:14