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

📄 ecolor.java

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

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

public class EColor
    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 float dash1[] = {
      10.0f};

  public EColor() {
    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.setPaint(red);
    g2.fill(new Rectangle2D.Double(x, y, rectWidth, rectHeight));
    g2.setPaint(fg);
    x += gridWidth;
    // 使用梯度模式填充一块圆角矩形区域
    GradientPaint redtowhite = new GradientPaint(x, y, red, x + rectWidth, y,
                                                 white, false);

    g2.setPaint(redtowhite);
    g2.fill(new RoundRectangle2D.Double(x, y, rectWidth,
                                        rectHeight, 10, 10));
    g2.setPaint(fg);
    x += gridWidth;
    // 使用红色填充弧形区域
    g2.setPaint(red);
    g2.fill(new Arc2D.Double(x, y, rectWidth, rectHeight, 90,
                             135, Arc2D.OPEN));
    g2.setPaint(fg);
    x += gridWidth;
    // 使用梯度模式填充一块椭圆形区域
    redtowhite = new GradientPaint(x, y, red, x + rectWidth, y, white);
    g2.setPaint(redtowhite);
    g2.fill(new Ellipse2D.Double(x, y, rectWidth, rectHeight));
    g2.setPaint(fg);
    x += gridWidth;
    // 使用红色填充一块多边形区域
    int x3Points[] = {
        x, x + rectWidth, x, x + rectWidth};
    int y3Points[] = {
        y, y + rectHeight, y + rectHeight, y};
    g2.setPaint(red);
    g2.fillPolygon(x3Points, y3Points, 4);
    g2.setPaint(fg);
  }

  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 EColor());
    f.setSize(600, 350);
    f.show();
  }

}

⌨️ 快捷键说明

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