我做了一个简单的骰子游戏,我有一个 DiceFace 类,它定义了一个 diceFace
package panel;
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
public class DiceFace
{
// Holds the seven possible dot positions on a standard die
private Ellipse2D.Double[] dots = new Ellipse2D.Double[7];
private Rectangle box;
private int xLeft;
private int yTop;
private int side;
private int diceValue;
public DiceFace(int s, int x, int y, int v)
{
side = s; // dimension of dice face
xLeft = x; // position
yTop = y; // position
diceValue = v; // pip value
}
public void draw(Graphics2D g2)
{
box = new Rectangle(xLeft, yTop, side, side);
makeDots();
// Black background
g2.setColor(Color.BLACK);
g2.fill(box);
// White dots on black
g2.setColor(Color.WHITE);
// Draw dots
if (diceValue == 1)
g2.fill(dots[0]);
else if (diceValue == 2)
{
g2.fill(dots[1]); g2.fill(dots[2]);
}
else if (diceValue == 3)
{
g2.fill(dots[0]); g2.fill(dots[1]); g2.fill(dots[2]);
}
else if (diceValue == 4)
{
g2.fill(dots[1]); g2.fill(dots[2]);
g2.fill(dots[3]); g2.fill(dots[4]);
}
else if (diceValue == 5)
{
g2.fill(dots[0]); g2.fill(dots[1]);
g2.fill(dots[2]); g2.fill(dots[3]); g2.fill(dots[4]);
}
else if (diceValue == 6)
{
g2.fill(dots[1]); g2.fill(dots[2]); g2.fill(dots[3]);
g2.fill(dots[4]); g2.fill(dots[5]); g2.fill(dots[6]);
}
}
public void makeDots()
{
int w = side/6; // dot width
int h = side/6; // dot height
dots[0] = new Ellipse2D.Double(xLeft + (2.5 * side)/6,
yTop + (2.5 * side)/6, h, w);
dots[1] = new Ellipse2D.Double(xLeft + (3.75 * side)/6,
yTop + (3.75 * side)/6, h, w);
dots[2] = new Ellipse2D.Double(xLeft + (1.25 * side)/6,
yTop + (1.25 * side)/6, h, w);
dots[3] = new Ellipse2D.Double(xLeft + (1.25 * side)/6,
yTop + (3.75 * side)/6, h, w);
dots[4] = new Ellipse2D.Double(xLeft + (3.75 * side)/6,
yTop + (1.25 * side)/6, h, w);
dots[5] = new Ellipse2D.Double(xLeft + (1.25 * side)/6,
yTop + (2.5 * side)/6, h, w);
dots[6] = new Ellipse2D.Double(xLeft + (3.75 * side)/6,
yTop + (2.5 * side)/6, h, w);
}
}
我还有一个扩展 JComponent 的类 DiceFaceConstructor
package panel;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
public class DiceFaceComponent extends JComponent {
public void paintComponent(Graphics g) //method returns void, takes object of type Graphics which it refers to internally as g
{
Graphics2D g2 = (Graphics2D)g; //casting
DiceFace dice1 = new DiceFace(60,0,0,6);
dice1.draw(g2); //draw the andgate
}
}
最后我有一个查看器类,它有一个主要方法和一个框架。直接在框架上绘制一个新的骰子面对象就可以了(显示骰子),如下封装面板;
import java.awt.Component;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class PanelViewer
{
/**
* @param args
*/
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setSize(600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(dice1)
DiceFaceComponent dice1 = new DiceFaceComponent();
frame.add(dice1)
//JPanel panel1 = new JPanel();
//panel1.add(dice1);
//frame.add(panel1);
//frame.pack();
frame.setVisible(true);
}
}
问题是当我尝试将我的骰子组件添加到面板然后将面板添加到框架时,没有显示任何内容,如下所示。请帮忙,已经五个小时了。
package panel;
import java.awt.Component;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class PanelViewer
{
/**
* @param args
*/
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setSize(600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DiceFaceComponent dice1 = new DiceFaceComponent();
JPanel panel1 = new JPanel();
panel1.add(dice1);
frame.add(panel1);
frame.pack();
frame.setVisible(true);
}
}