基本 Java 骰子表示
Basic Java dice representation
我正在尝试用给定的 类(正方形、圆形和 Canvas)来表示 Java 中的骰子,我已经表示了骰子的盒子用正方形但在代表n个圆圈时我做了一个圆圈矩阵,但它没有出现,这是我的骰子构造函数:
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;
}
所以我做错了什么?
在我的最后一个骰子游戏中,我创建了一个 class 只是为了绘制骰子面。我实例化了6次class,通过调用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);
}
}
我正在尝试用给定的 类(正方形、圆形和 Canvas)来表示 Java 中的骰子,我已经表示了骰子的盒子用正方形但在代表n个圆圈时我做了一个圆圈矩阵,但它没有出现,这是我的骰子构造函数:
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;
}
所以我做错了什么?
在我的最后一个骰子游戏中,我创建了一个 class 只是为了绘制骰子面。我实例化了6次class,通过调用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);
}
}