添加到JPanel然后添加到JFrame时图形不显示

时间:2013-03-31 00:45:02

标签: java swing graphics jpanel paintcomponent

我正在为我的计算机科学课创建一个过桥游戏,我们需要创建问题的图形表示。图形表示由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);
}

}

1 个答案:

答案 0 :(得分:3)

查看BridgeCanvas对象的大小。如果将其添加到JPanel并且JPanel使用其默认的FlowLayout,则JComponent派生对象的大小最多为1 x 1像素,因此在创建,添加和显示时,不会显示任何有价值的内容。

事实上,这可以通过(临时)在BridgeCanvas的getSize()方法中隐藏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()方法,并让它返回一个尺寸,该尺寸保持您希望它实际存在的尺寸。

注意:您要避免在任何内容上调用setSize(),并且您还希望避免(每notes个Swing专家Jeanette / kleopatra)调用setPreferredSize(),因为此设置可以通过使用BridgeCanvas对象的代码进行更改。