我有代码来绘制相等部分的饼图。但是我不希望每个切片彼此连接,而是在每个切片之间有一个距离(空格)。我该怎么做?
import javax.swing.JApplet;
import java.awt.*;
public class PieChart extends JApplet {
public void paint(Graphics page) {
setBackground(Color.lightGray);
page.setColor(Color.blue);
page.fillArc(70, 25, 100, 100, 0, 45);
page.setColor(Color.yellow);
page.fillArc(70, 25, 100, 100, 45, 45);
page.setColor(Color.black);
page.fillArc(70, 25, 100, 100, 90, 45);
page.setColor(Color.green);
page.fillArc(70, 25, 100, 100, 135, 45);
page.setColor(Color.red);
page.fillArc(70, 25, 100, 100, 180, 45);
page.setColor(Color.magenta);
page.fillArc(70, 25, 100, 100, 225, 45);
page.setColor(Color.cyan);
page.fillArc(70, 25, 100, 100, 270, 45);
page.setColor(Color.orange);
page.fillArc(70, 25, 100, 100, 315, 45);
}
}
答案 0 :(得分:0)
正如哈杰罗建议的那样,你可能会想要爆炸"将碎片从中心移出来的馅饼。为此,您需要计算每个楔形(的中心)的方向。然后将楔形的原点向这个方向移动一段距离。这里可能计算那些重新映射的x& y坐标:
public class pie {
public static void main(String[] args) {
int x;
int y;
int d = 5; /* our distance of "explosion" */
// thing.fillArc(70, 25, 100, 100, 0, 45); /* we had (70,25) */
x = offsetX(70, d, 0, 45);
y = offsetY(25, d, 0, 45);
// thing.fillArc(x, y, 100, 100, 0, 45); /* with new x & y */
System.out.println("x = " + x + ", y = " + y); /* showing */
}
public static int offsetX ( int x, int d, int sa, int aa ) {
double ca = Math.toRadians((sa + aa) / 2.0);
return x + (int)(Math.cos(ca) * d);
}
public static int offsetY ( int y, int d, int sa, int aa ) {
double ca = Math.toRadians((sa + aa) / 2.0);
return y + (int)(Math.sin(ca) * d);
}
}