starapplet.java

来自「非常好的java事例以及带源码事例的java2教程」· Java 代码 · 共 46 行

JAVA
46
字号
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;

public class StarApplet extends JApplet
{
  // Initialize the applet
  public void init()
  {
    Container content = getContentPane();

    // Add the panel to the content pane for the applet
    content.add(pane);                 // BorderLayout.CENTER is default position
  }

  // Class defining a pane on which to draw
  class StarPane extends JComponent
  {
    public void paint(Graphics g)
    {
      Graphics2D g2D = (Graphics2D)g;
      Star star = new Star(0,0);                 // Create a star
      float delta = 60;                          // Increment between stars
      float starty = 0;                          // Starting y position

      // Draw 3 rows of 4 stars
      for(int yCount = 0 ; yCount<3; yCount++)
      {
        starty += delta;                        // Increment row position
        float startx = 0;                       // Start x position in a row

        // Draw a row of 4 stars
        for(int xCount = 0 ; xCount<4; xCount++)
        {
          g2D.setPaint(Color.blue);            // Drawing color blue
          g2D.draw(star.atLocation(startx += delta, starty));
          g2D.setPaint(Color.green);           // Color for fill is green
          g2D.fill(star.getShape());           // Fill the star
        }
      }
    }
  }

  StarPane pane = new StarPane();              // Pane containing stars
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?