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

JPanel实现Circle Packing时圆形圆心偏移问题求助

Java Swing: Circle Packing Animation - Circles Shift Position When Growing Instead of Staying Centered

我正在用JPanel和JFrame实现**Circle Packing(圆形填充)**的动画效果,目标是让持续生长的不同大小圆形填充整个区域。我的预期是每个圆形固定圆心点P,仅通过增大半径R来实现生长,但实际绘制时,JPanel里的圆形会同时改变x、y坐标,这让我完全摸不着头脑。

以下是我的repaint方法代码:

public void repaint(Graphics g) { 
    g.setColor(new Color(125, 0, 100)); 
    g.fillRect(0, 0, WIDTH, HEIGHT); 
    g.setColor(new Color(255, 165, 0)); 
    Circle myCircle = new Circle(rand.nextInt(500), rand.nextInt(500), 1); 
    circles.add(myCircle); 
    for (Circle c : circles) { 
        int newx = (int) c.getX(); 
        int newy = (int) c.getY(); 
        int newsize = (int) c.getSize(); 
        c.grow(); 
        c.growth(); // These are the growing functions 
        c.Edges(); 
        g.drawOval(newx, newy, newsize, newsize); 
    } 
}

其中grow()、growth()和Edges()方法是用来检测圆形是否触达屏幕边界的逻辑。

我是Stack Overflow的新手,麻烦大家帮忙排查问题,也可以帮我调整这个提问的标题,让它更准确清晰!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:27:03