我正在为我的计算机科学课创建一个过桥游戏,我们需要创建问题的图形表示。图形表示由BridgeCanvas类绘制。该类扩展了从JComponent扩展来的Canvas类。
我知道BridgeCanvas正确绘制了图形,因为当我将BridgeCanvas对象直接添加到JFrame时,它可以正确显示,但是当我先将BridgeCanvas放入JPanel中,然后将其添加到JFrame时,它不会显示(全部我得到的是空白帧)。我的一位同学说这可能是因为我使用的是Mac。我也在netbeans中这样做。
这是BridgeCanvas类(主要方法也包含在此类中):
public class BridgeCanvas extends Canvas {
// Initializes the background, river, bridge, and character graphics
public BridgeCanvas(State state) {
super(state);
background = new RoundRectangle2D.Double(0, 0, 400, 400, 40, 40);
bridge = new GeneralPath();
bridge.moveTo(100, 175);
bridge.curveTo(200, 120, 200, 120, 300, 175);
bridge.lineTo(300, 225);
bridge.curveTo(200, 170, 200, 170, 100, 225);
bridge.closePath();
river = new GeneralPath();
river.moveTo(150, 0);
river.curveTo(50, 100, 150, 200, 150, 200);
river.curveTo(225, 300, 150, 400, 150, 400);
river.lineTo(250, 400);
river.curveTo(325, 300, 250, 200, 250, 200);
river.curveTo(150, 100, 250, 0, 250, 0);
river.closePath();
AffineTransform transform = new AffineTransform();
transform.setToTranslation(25, 25);
// generateShapeFromText is method used to create shapes from numbers.
P1 = (GeneralPath) generateShapeFromText(new Font(Font.MONOSPACED, Font.BOLD, 32), "P1");
P1 = (GeneralPath) P1.createTransformedShape(transform);
P2 = (GeneralPath) generateShapeFromText(new Font(Font.MONOSPACED, Font.BOLD, 32), "P2");
transform.setToTranslation(25, 100);
P2 = (GeneralPath) P2.createTransformedShape(transform);
FL = (GeneralPath) generateShapeFromText(new Font(Font.MONOSPACED, Font.BOLD, 32), "F");
P5 = (GeneralPath) generateShapeFromText(new Font(Font.MONOSPACED, Font.BOLD, 32), "P5");
P10 = (GeneralPath) generateShapeFromText(new Font(Font.SANS_SERIF, Font.BOLD, 32), "P10");
}
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
super.paintComponent(g);
g2.setFont(new Font(Font.MONOSPACED, Font.BOLD, 32));
BridgeState state = (BridgeState) getState();
g2.setColor(new Color(0, 140, 0));
g2.fill(background);
g2.setColor(new Color(0, 0, 200));
g2.fill(river);
g2.setColor(new Color(169, 69, 19));
g2.fill(bridge);
g2.setColor(Color.BLACK);
g2.fill(P1);
g2.fill(P2);
}
private RoundRectangle2D.Double background;
private GeneralPath bridge;
private GeneralPath river;
private GeneralPath P1;
private GeneralPath P2;
private GeneralPath FL;
private GeneralPath P5;
private GeneralPath P10;
public static void main(String[] args) {
JFrame frame = new JFrame();
BridgeState state = new BridgeState(Position.WEST, Position.WEST, Position.WEST, Position.WEST, Position.WEST, 0);
BridgeCanvas canvas = new BridgeCanvas(state);
JComponent panel = new JPanel();
panel.add(canvas);
panel.setLayout(new FlowLayout());
panel.setOpaque(false);
panel.setSize(new Dimension(440, 440));
frame.setPreferredSize(new Dimension(440, 440));
frame.add(panel);
frame.setVisible(true);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
最佳答案
查看您的BridgeCanvas对象的大小。如果将其添加到JPanel且JPanel使用其默认的FlowLayout,则JComponent派生对象的大小最多为1 x 1像素,因此,在创建,添加和显示该对象时,看不到任何值得的东西。
实际上,可以很容易地通过(暂时)将getSize()
方法塞入BridgeCanvas的paintComponent(...)
方法中来进行测试,该方法仅在显示对象时才会被调用:
public void paintComponent(Graphics g) {
// TODO: delete line below!
System.out.println("BridgeCanvas Size: " + getSize());
Graphics2D g2 = (Graphics2D) g;
super.paintComponent(g);
g2.setFont(new Font(Font.MONOSPACED, Font.BOLD, 32));
// .... etc...
}
一种可能的解决方案:覆盖BridgeCanvas的
getPreferredSize()
方法,并使其返回一个Dimension,该Dimension保留您希望的尺寸。注意:您将希望避免在任何地方调用
setSize()
,并且还希望避免(按notes Swing专家Jeanette / kleopatra)调用setPreferredSize()
,因为此设置可以通过使用BridgeCanvas对象的代码来更改。