的Polygon
類可以被視爲一個傳統類,一直以來存在的Java 1.0,但應該很難在新的代碼中使用了。指定x的奇數方式/ y座標在單獨的陣列,而且,更重要的是,事實上,它僅支持int[]
陣列限制了其應用領域。雖然它實現了接口,但是這個接口的更多現代實現可以用來表示多邊形。在大多數情況下,將多邊形描述爲Path2D
更簡單且更靈活。可以創建一個Path2D p = new Path2D.Double();
,然後執行一系列moveTo
和lineTo
調用來生成所需的形狀。
下面的程序示出了Path2D
類可以如何被用來生成星形形狀。最重要的方法是createStar
方法。這是非常通用的。它接收
- 爲星形
- 星形
- 的內,外半徑的中心座標的光線的數量,恆星應具有
- 的角度,其中所述第一射線應(即星號的旋轉角度)
如果需要,可以在這一個方法上包一個更簡單的方法 - 就像下面代碼中的createDefaultStar
例子一樣。
該程序示出了不同分,畫成線和填充有不同的顏色和徑向漸變油漆,作爲例子:
![Stars](https://i.stack.imgur.com/PILdr.png)
完整的程序作爲MCVE:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RadialGradientPaint;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.Path2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class DrawStarShape
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
createAndShowGUI();
}
});
}
private static void createAndShowGUI()
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new DrawStarShapePanel());
f.setSize(600, 600);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
class DrawStarShapePanel extends JPanel
{
@Override
protected void paintComponent(Graphics gr)
{
super.paintComponent(gr);
Graphics2D g = (Graphics2D) gr;
g.setColor(Color.WHITE);
g.fillRect(0, 0, getWidth(), getHeight());
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.BLACK);
g.draw(createDefaultStar(50, 200, 200));
g.setPaint(Color.RED);
g.fill(createStar(400, 400, 40, 60, 10, 0));
g.setPaint(new RadialGradientPaint(
new Point(400, 200), 60, new float[] { 0, 1 },
new Color[] { Color.RED, Color.YELLOW }));
g.fill(createStar(400, 200, 20, 60, 8, 0));
g.setPaint(new RadialGradientPaint(
new Point(200, 400), 50, new float[] { 0, 0.3f, 1 },
new Color[] { Color.RED, Color.YELLOW, Color.ORANGE }));
g.fill(createStar(200, 400, 40, 50, 20, 0));
}
private static Shape createDefaultStar(double radius, double centerX,
double centerY)
{
return createStar(centerX, centerY, radius, radius * 2.63, 5,
Math.toRadians(-18));
}
private static Shape createStar(double centerX, double centerY,
double innerRadius, double outerRadius, int numRays,
double startAngleRad)
{
Path2D path = new Path2D.Double();
double deltaAngleRad = Math.PI/numRays;
for (int i = 0; i < numRays * 2; i++)
{
double angleRad = startAngleRad + i * deltaAngleRad;
double ca = Math.cos(angleRad);
double sa = Math.sin(angleRad);
double relX = ca;
double relY = sa;
if ((i & 1) == 0)
{
relX *= outerRadius;
relY *= outerRadius;
}
else
{
relX *= innerRadius;
relY *= innerRadius;
}
if (i == 0)
{
path.moveTo(centerX + relX, centerY + relY);
}
else
{
path.lineTo(centerX + relX, centerY + relY);
}
}
path.closePath();
return path;
}
}
也參見該相關[示例](http://stackoverflow.com/q/8614972/230513)。 – trashgod 2013-05-02 01:06:49
或者[更一般的方法](http://java-sl.com/shapes.html)。 – trashgod 2013-05-02 01:26:54