Applet程序。
1 import javax.swing.*; 2 import java.awt.*; 3 import java.awt.geom.GeneralPath; 4 5 @SuppressWarnings("serial") 6 public class StarApplet extends JApplet { 7 // Initialize the applet 8 @Override 9 public void init() { 10 StarPane pane = new StarPane(); // Pane containing stars 11 getContentPane().add(pane); // BorderLayout.CENTER is default position 12 } 13 14 // Class defining a pane on which to draw 15 class StarPane extends JComponent { 16 @Override 17 public void paint(Graphics g) { 18 Graphics2D g2D = (Graphics2D)g; 19 float delta = 60; // Increment between stars 20 float starty = 0; // Starting y position 21 22 // Draw 3 rows of 4 stars 23 GeneralPath star = null; 24 for(int yCount = 0 ; yCount < 3 ; yCount++) { 25 starty += delta; // Increment row position 26 float startx = 0; // Start x position in a row 27 28 // Draw a row of 4 stars 29 for(int xCount = 0 ; xCount < 4 ; xCount++) { 30 star = Star.starAt(startx += delta, starty); 31 g2D.setPaint(Color.GREEN); // Color for fill is green 32 g2D.fill(star); // Fill the star 33 g2D.setPaint(Color.BLUE); // Drawing color blue 34 g2D.draw(star); 35 } 36 } 37 } 38 } 39 }
其他与上一例同。
注意:在填充后才绘制边界是很重要的,否则填充就会覆盖边界。如果仅仅调用fill()方法,在填充形状后就不再绘制边界。