很难将这些圆圈放在彼此之内。我需要它们在彼此之间均匀间隔。你看到我做错了吗?
import javax.swing.*;
import java.awt.*;
public class JNestedCircles extends JFrame
{
public void paint(Graphics c)
{
super.paint(c);
setTitle("JNestedCircles");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final int TOTAL_CIRCLES = 15;
final int GAP = 17;
int arcSize, x = 40, y = 80;
int x1 = 500;
int y1 = 500;
for(arcSize = 0; arcSize < TOTAL_CIRCLES; arcSize++)
{
c.drawOval(x, y, x1, y1);
x += GAP;
y += GAP;
x1 -= GAP ;
y1 -= GAP ;
}
}
public static void main(String[] args)
{
JNestedCircles aFrame = new JNestedCircles();
final int WIDTH = 585;
final int HEIGHT = 640;
aFrame.setSize(WIDTH, HEIGHT);
aFrame.setVisible(true);
aFrame.setLocationRelativeTo(null);
}
}
答案 0 :(得分:1)
看起来你有正确的想法,你只需要这样做:
x1 -= 2 * GAP ;
y1 -= 2 * GAP ;
这是因为后两个参数是宽度和高度,而不是结束位置。您通过GAP使用增量正确地抵消了x,y,该增量指定了圆圈的左上角。现在你只需要宽度/高度每次减少2 * GAP。