Search code examples
javadice

Basic Java dice representation


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?


Solution

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