You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JFrame弹跳小球程序repaint()残留初始位置痕迹问题求助

问题:透明JFrame中弹跳小球重绘残留痕迹

我正在实现一个JFrame程序,用于在并发环境下模拟小球碰壁及小球间的弹跳效果。目前小球能正常显示和更新,但调用repaint()时会残留初始位置的痕迹,表现为边缘带有渐变灰色效果。

我已完成透明JFrame背景的设置,相关代码如下:

窗口实例化代码

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            JFrame frame = new JFrame("Bouncing Ball");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setContentPane(new BouncingBall(640, 480));
            frame.setUndecorated(true);
            frame.setBackground(new Color(0, 0, 0, 0));
            frame.pack();
            frame.setVisible(true);
        }
    });
}

JPanel重绘代码

class PaintCanvas extends JPanel {
    @Override
    public void paintComponent(Graphics graphics) {
        super.paintComponent(graphics);
        // wall.drawWall(graphics);
        for (int i = 0; i < numBalls; i++) {
            Ball ball = balls.get(i);
            ball.drawBall(graphics);
        }  
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(width, height);
    }
}

小球绘制代码

public void drawBall(Graphics graphics) {
    graphics.setColor(new Color(1, 1, 0, 1f));
    graphics.fillOval((int)(posX - radius), (int)(posY - radius), radius * 2, radius * 2);
}

我在相关技术平台未找到类似问题的解决方案,恳请各位提供解决思路!


内容的提问来源于stack exchange,提问作者Prionantha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 11:17:48