⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 erectangle.java

📁 JAVA编程百例书中各章节的所有例子的源代码,包括套接字编程
💻 JAVA
字号:
package ch06.section02;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;

public class ERectangle
    extends JPanel {

  final static Color bg = Color.white;
  final static Color fg = Color.black;
  final static Color red = Color.red;
  final static Color white = Color.white;

  final static BasicStroke stroke = new BasicStroke(2.0f);
  final static BasicStroke wideStroke = new BasicStroke(8.0f);
  final static float dash1[] = {
      10.0f};
  final static BasicStroke dashed = new BasicStroke(1.0f,
      BasicStroke.CAP_BUTT,
      BasicStroke.JOIN_MITER,
      10.0f, dash1, 0.0f);

  public ERectangle() {
    setBackground(bg);
  }

  public void paintComponent(Graphics g) {
    super.paintComponent(g);

    Graphics2D g2 = (Graphics2D) g;
    drawShapes(g2);
  }

  static void drawShapes(Graphics2D g2) {
    int gridWidth = 600 / 6;
    int gridHeight = 250 / 2;

    int rowspacing = 5;
    int columnspacing = 7;
    int rectWidth = gridWidth - columnspacing;
    int rectHeight = gridHeight - rowspacing;

    Color fg3D = Color.lightGray;

    g2.setPaint(fg3D);
    g2.drawRect(50, 50, 505 - 1, 130);
    g2.setPaint(fg);

    int x = 55;
    int y = 57;

    // 使用默认的线条类型一条直线
    g2.draw(new Line2D.Double(x, y + rectHeight - 1, x + rectWidth, y));
    x += gridWidth;

    // 使用宽度为2.0的线条画一个矩形
    g2.setStroke(stroke);
    g2.draw(new Rectangle2D.Double(x, y, rectWidth, rectHeight));
    x += gridWidth;

    // 使用虚线画一个圆角矩形
    g2.setStroke(dashed);
    g2.draw(new RoundRectangle2D.Double(x, y, rectWidth,
                                        rectHeight, 10, 10));
    x += gridWidth;

    // 使用宽度为8.0的线条画一个三角形
    int x1Points[] = {
        x + (int) (rectWidth / 2), x, x + rectWidth};
    int y1Points[] = {
        y, y + rectHeight, y + rectHeight};
    g2.setStroke(wideStroke);
    g2.drawPolygon(x1Points, y1Points, 3);
    x += gridWidth;

    // 画一个多边形
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);

    int x2Points[] = {
        x, x + rectWidth, x, x + rectWidth};
    int y2Points[] = {
        y, y + rectHeight, y + rectHeight, y};
    GeneralPath polygon = new GeneralPath(GeneralPath.WIND_EVEN_ODD,
                                          x1Points.length);
    polygon.moveTo(x2Points[0], y2Points[0]);
    for (int index = 1; index < x2Points.length; index++) {
      polygon.lineTo(x2Points[index], y2Points[index]);
    }

    polygon.closePath();
    g2.setStroke(stroke);
    g2.draw(polygon);

  }

  public static void main(String[] args) {
    WindowListener l = new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }

      public void windowClosed(WindowEvent e) {
        System.exit(0);
      }
    };
    JFrame f = new JFrame();
    f.addWindowListener(l);
    JPanel panel = new JPanel();
    f.getContentPane().add(BorderLayout.SOUTH, panel);
    f.getContentPane().add(BorderLayout.CENTER, new ERectangle());
    f.setSize(610, 350);
    f.show();
  }

}

⌨️ 快捷键说明

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