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

📄 element.java

📁 这是编译原理的一个实验, 是把一个正则表达式转化为不确定有穷自动机NFA的算法程序,朋兴趣的朋友可以下载来看看哦。    一个正则表达式就是由普通字符(例如字符 a 到 z)以及特殊字符(称为元字
💻 JAVA
字号:
 package reg2nfa;

import java.awt.*;
import java.awt.geom.*;

public abstract class Element {
  protected Color color;             //图形颜色
  protected BasicStroke bs;          //画笔的大小

  public Element(BasicStroke bs, Color color) {
    this.color = color;
    this.bs = bs;
  }

  public void setStroke(BasicStroke bs) {
    this.bs = bs;
  }

  public void setColor(Color color) {
    this.color = color;
  }

  public abstract void draw(Graphics2D g2D);     //绘画图形
  public abstract Rectangle getBounds();         //图形的大小

  public static class ArrowLine extends Element implements Constants {
    //箭头形的线
    private Point start, ctrl1, ctrl2, end;
    private String label = "ε";

    public ArrowLine(Point start, Point end,
                     BasicStroke bs, Color color, String label) {
      super(bs, color);
      this.start = this.ctrl2 = start;
      this.end = this.ctrl1 = end;
      this.label = label;
      //System.out.print(this.start);
      //System.out.println("    " + this.end.toString());
    }

    public ArrowLine(Point p1, Point p2, Point p3, Point p4,
                    BasicStroke bs, Color color, String label) {
      super(bs, color);
      this.start = p1;
      this.ctrl1 = p2;
      this.ctrl2 = p3;
      this.end = p4;
      this.label = label;
    }

    public void draw(Graphics2D g2d) {
      g2d.setStroke(bs);
      g2d.setColor(color);
      Line2D  line1 = new Line2D.Double(start.x, start.y,
                                        ctrl1.x, ctrl1.y);
      Line2D  line2 = new Line2D.Double(ctrl1.x, ctrl1.y,
                                        ctrl2.x, ctrl2.y);
      Line2D  line3 = new Line2D.Double(ctrl2.x, ctrl2.y,
                                        end.x, end.y);
      Rectangle arrow = arrowPoint(ctrl2, end);

      GradientPaint gradient = new GradientPaint(start.x, start.y,
                                              Color.magenta,
                                              end.x, end.y,
                                              color);
      g2d.setPaint(gradient);

      g2d.draw(line1);
      g2d.draw(line2);
      g2d.draw(line3);
      //g2d.setPaint(Color.MAGENTA);
      //g2d.fill(new Ellipse2D.Double(end.x - bs.getLineWidth(), end.y - bs.getLineWidth(),
      //                              bs.getLineWidth() + 5, bs.getLineWidth() + 5));
      g2d.draw(new Line2D.Double(new Point(arrow.x, arrow.y), end));
      g2d.draw(new Line2D.Double(new Point(arrow.width, arrow.height), end));

      g2d.setPaint(Color.darkGray);
      g2d.setFont(new Font("Helvetica", Font.BOLD + Font.ITALIC, 20));
      g2d.drawString(label, (ctrl1.x + ctrl2.x)/2 - 5, (ctrl1.y + ctrl2.y)/2 -5);
    }

    private Rectangle arrowPoint(Point p1, Point p2) {
      //计算箭头的两头坐标
      int a = p1.x, b = p1.y;
      int c = p2.x, d = p2.y;
      double x0, y0, k, m, n, q;
      double L = Math.sqrt((a-c)*(a-c) + (b-d)*(b-d));
      double x1, y1, x2, y2;

      if(c != a && b != d) {
        x0 = c - Math.sqrt(2)/2*RADIUS*(c-a)/L;
        y0 = d - Math.sqrt(2)/2*RADIUS*(d-b)/L;
        k = (a-c)/(d-b);
        m = 1 + k*k;
        n = -2*c + 2*k*(y0 - k*x0) - 2*d*k;
        q = (y0 - k*x0)*(y0 - k*x0) - 2*d*(y0 - k*x0) + d*d
            - RADIUS*RADIUS + c*c;
        x1 = (-n + Math.sqrt(n*n - 4*m*q))/(2*m);
        y1 = k*x1 + (y0 - k*x0);
        x2 = (-n - Math.sqrt(n*n - 4*m*q))/(2*m);
        y2 = k*x2 + (y0 - k*x0);
      }
      else if(c == a && b != d) {
        k = 0;
        x0 = c;
        y0 = d - Math.sqrt(2)/2*RADIUS*(d-b)/L;

        x1 = c - Math.sqrt(2)/2*RADIUS;
        y1 = y0;
        x2 = c + Math.sqrt(2)/2*RADIUS;
        y2 = y0;
      }
      else if(c != a && b == d) {
        k = 1;
        x0 = c - Math.sqrt(2)/2*RADIUS*(c-a)/L;
        y0 = b;

        x1 = x0;
        y1 = d - Math.sqrt(2)/2*RADIUS;
        x2 = x0;
        y2 = d + Math.sqrt(2)/2*RADIUS;
      }
      else return null;

      return new Rectangle((int)x1,(int)y1, (int)x2, (int)y2);
    }

    public Rectangle getBounds() {
      return new Rectangle();
    }
  }

  public static class Circle extends Element {
    //圆形
    private Ellipse2D.Double circle;
    public Circle(Point center, double radius, BasicStroke bs, Color color) {
      super(bs, color);

      circle = new Ellipse2D.Double(center.x - radius,
                                    center.y - radius,
                                    2.0*radius, 2.0*radius);
    }

    public Shape getShape() {
      return circle;
    }

    public Rectangle getBounds() {
      return circle.getBounds();
    }

   public void modify(Point center, Point circum) {
      double radius = center.distance(circum);
      circle.x = center.x - (int)radius;
      circle.y = center.y - (int)radius;
      circle.width = circle.height = 2*radius;
    }

    public void draw(Graphics2D g2D) {
      g2D.setPaint(color);
      g2D.draw(circle);
    }
  }
}

⌨️ 快捷键说明

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