JComponent大小问题

发布于 2024-12-13 22:49:02 字数 1377 浏览 3 评论 0 原文

我有一个 JComponent 子类,用于在屏幕上绘制形状。在构造函数中,我尝试将 ballXballY 设置为 XY 大小值的一半JComponent,我认为我做错了。我现在查了很多资料,还是找不到解决办法。代码如下。请记住,这是我第一次真正的 Swing/Graphics2D 冒险。

public class PongCanvas extends JComponent {
//Vars to hold XY values and Dimension values.

    private int batXDim, batYDim;
    private int b1X, b1Y;
    private int b2X, b2Y;
    private int ballRad, ballX, ballY;

    public PongCanvas() {//Instantiate vars.
        batXDim = 20;
        batYDim = 100;

        b1X = 0;
        b1Y = 0;

        b2X = 0;
        b2Y = 0;

        ballRad = 20;
        ballX = getWidth() / 2;
        ballY = getHeight() / 2;
    }

    public void paint(Graphics g) {//Main paint Method.
        //Cast Graphics to Graphics2D.
        Graphics2D g2 = (Graphics2D) g;
        //Enable antialiasing.
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        //Draw background.
        g2.setPaint(Color.black);
        g2.fillRect(0, 0, getWidth(), getHeight());
        //Draw ball.
        g2.setPaint(Color.white);
        g2.fillOval(ballX, ballY, ballRad, ballRad);
        //Draw bat 1.
        g2.fillRect(b1X, b1Y, batXDim, batYDim);
        //Draw bat 2.
        g2.fillRect(b2X, b2Y, batXDim, batYDim);
    }
}

I have a JComponent subclass that I am using to draw shapes onto my screen. In the constructor, I am trying to set ballX and ballY to half of the X and Y size values of the JComponent, and I think I am doing it wrong. I have looked this up a lot now, and cannot find a remedy. The code is as follows. Please bear in mind that this is my first real Swing/Graphics2D venture.

public class PongCanvas extends JComponent {
//Vars to hold XY values and Dimension values.

    private int batXDim, batYDim;
    private int b1X, b1Y;
    private int b2X, b2Y;
    private int ballRad, ballX, ballY;

    public PongCanvas() {//Instantiate vars.
        batXDim = 20;
        batYDim = 100;

        b1X = 0;
        b1Y = 0;

        b2X = 0;
        b2Y = 0;

        ballRad = 20;
        ballX = getWidth() / 2;
        ballY = getHeight() / 2;
    }

    public void paint(Graphics g) {//Main paint Method.
        //Cast Graphics to Graphics2D.
        Graphics2D g2 = (Graphics2D) g;
        //Enable antialiasing.
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        //Draw background.
        g2.setPaint(Color.black);
        g2.fillRect(0, 0, getWidth(), getHeight());
        //Draw ball.
        g2.setPaint(Color.white);
        g2.fillOval(ballX, ballY, ballRad, ballRad);
        //Draw bat 1.
        g2.fillRect(b1X, b1Y, batXDim, batYDim);
        //Draw bat 2.
        g2.fillRect(b2X, b2Y, batXDim, batYDim);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云雾 2024-12-20 22:49:03

覆盖 getPreferredSize() 在您的 JComponent 中返回您的首选尺寸,并从该 Dimension 的宽度和高度的一半开始。为了达到同样的目的,此 KineticModel 会调用 setPreferredSize rel="nofollow">显示面板

附录:作为解释,您当前的方法失败,因为 getWidth()getHeight() 的结果在 validate 之前无效 () 已在封闭容器上调用,通常是 pack()

Override getPreferredSize() in your JComponent to return your preferred size, and start with half the width and height of that Dimension. To the same end, this KineticModel invokes setPreferredSize() in DisplayPanel.

Addendum: By way of explanation, your current approach fails because the results from getWidth() and getHeight() are invalid until validate() has been called on the enclosing container, typically as the result of pack().

飞烟轻若梦 2024-12-20 22:49:03

我同意垃圾神的回答。 (+1)

paintComponent(g) 中移动 ballX 和 ballY,如下所示

if (ballX==0 && ballY==0) {
    ballX = getWidth()/2;
    ballY = getHeight()/2;
}

I agree with trashgod's answer. (+1)

Move the ballX and ballY in the paintComponent(g) like this

if (ballX==0 && ballY==0) {
    ballX = getWidth()/2;
    ballY = getHeight()/2;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文