spotsandlines.java

来自「Java the UML Way 书中所有源码」· Java 代码 · 共 42 行

JAVA
42
字号
/*
 * SpotsAndLines.java   E.L. 2001-08-12
 *
 * An applet with filled circles and lines between the circles.
 * The applet window is 400 pixels in the horizontal direction,
 * and 300 pixels in the vertical direction.
 */
import java.awt.*;
import javax.swing.*;

public class SpotsAndLines extends JApplet {
  public void init() {
    Container content = getContentPane();
    Drawing theDrawing = new Drawing();
    content.add(theDrawing);
  }
}

class Drawing extends JPanel {
private static final int windowWidth = 400;  // have to agree
private static final int windowHeight = 300;   // with the html file
private static final int diameter = 20;
private static final int center = diameter / 2;
private static final int noOfLines = 10;

  public void paintComponent(Graphics window) {
    super.paintComponent(window);
    int x = (int) ((windowWidth - diameter) * Math.random() + 1);
    int y = (int) ((windowHeight - diameter) * Math.random() + 1);
    window.fillOval(x, y, diameter, diameter);
    int counter = 0;
    while (counter < noOfLines) {
      int lastX = x;
      int lastY = y;
      x = (int) ((windowWidth - diameter) * Math.random() + 1);
      y = (int) ((windowHeight - diameter) * Math.random() + 1);
      window.drawLine(lastX + center, lastY + center, x + center, y + center);
      window.fillOval(x, y, diameter, diameter);
      counter++;
    }
  }
}

⌨️ 快捷键说明

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