gpt4 book ai didi

java - 将图形添加到 JPanel 然后再添加到 JFrame 时不显示

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:52:58 26 4
gpt4 key购买 nike

我正在为我的计算机科学类(class)创建一个过桥游戏,我们需要创建问题的图形表示。图形表示由 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 像素,因此在创建、添加和显示时,不会显示任何有值(value)的内容。

事实上,这可以很容易地通过(暂时)在 BridgeCanvas 的 paintComponent(...) 方法中加入一个 getSize() 方法来测试,该方法只会如果显示对象则调用:

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 对象的代码可以更改此设置。

关于java - 将图形添加到 JPanel 然后再添加到 JFrame 时不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15725166/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com