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

📄 transformview.java

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

import java.awt.*;
import javax.swing.*;
import java.util.LinkedList;
import java.util.Iterator;
import java.awt.geom.Ellipse2D;

public class TransformView extends JPanel implements Constants {
  TransformModel transModel;
  Graph graph;

  public TransformView(TransformModel transModel) {
    this.transModel = transModel;
    this.setPreferredSize(new Dimension(400, 300));
    this.setBackground(Color.cyan);
  }

  public void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D)g;
    super.paintComponent(g2d);
    BasicStroke bs = new BasicStroke(5);

    Rectangle graphBounds = transModel.getBounds();
    Dimension size = this.getPreferredSize();
    Point location = new Point((size.width - graphBounds.width)/2,
        (size.height - graphBounds.height)/2);

    Element arrowLink;
    int x = 50 - graphBounds.x;
    int y = 50 - graphBounds.y;

    graph = transModel.getGraph();
    graph.setCurrent(0);

    for(int i = 0; i < transModel.node.length; i++) {
      //画结点
      Point curPoint = transModel.node[i].getPosition();
      Element node = new Element.Circle(new Point(curPoint.x + x, curPoint.y + y),
                                        RADIUS, bs, Color.black);
      Ellipse2D ellipse = new Ellipse2D.Double(curPoint.x + x - RADIUS,
                                               curPoint.y + y - RADIUS,
                                               2*RADIUS, 2*RADIUS);
      g2d.setPaint(Color.white);
      g2d.fill(ellipse);
      node.draw(g2d);
    }

    while(graph.hasNext()) {
      EdgeLink next = graph.getNext();
      if (next != null) {
        Point pa = next.n1.getPosition();
        Point pb = next.n2.getPosition();

        Rectangle interPoint = intersectPoint(pa, pb);
        Point p1 = new Point(interPoint.x, interPoint.y);
        Point p2 = new Point(interPoint.width, interPoint.height);

        String label = next.getLabel();

        /*if(Math.abs(p2.x - p1.x) > 4*RADIUS && p1.x < p2.x &&p1.y == p2.y)
          arrowLink = new Element.ArrowLine(new Point(p1.x + x, p1.y + y),
                                             new Point(p1.x + x, p1.y + y + 2*RADIUS),
                                             new Point(p2.x + x, p2.y + y + 2*RADIUS),
                                             new Point(p2.x + x, p2.y + y),
                                             new BasicStroke(5), Color.green, label);
                 else if(p2.x < p1.x )       //*运算中的向前循环
                   arrowLink = new Element.ArrowLine(new Point(p1.x + x, p1.y + y),
                                                       new Point(p1.x + x, p1.y + y - 2*RADIUS),
                                                       new Point(p2.x + x, p2.y + y - 2*RADIUS),
                                                       new Point(p2.x + x, p2.y + y),
                                                       new BasicStroke(5), Color.green, label);
*/
        //else
        if((Math.abs(p2.x - p1.x) <= 4*RADIUS && p1.x < p2.x) || p1.y != p2.y) {
          //普通的线, 由两点X距离和Y距离决定
          arrowLink = new Element.ArrowLine(new Point(p1.x + x, p1.y + y),
                                            new Point(p2.x + x, p2.y + y),
                                            new BasicStroke(5), Color.green,
                                            label);
          arrowLink.draw(g2d);
        }
      }
    }

    Point pointa = graph.getStart().getPosition();
    Point pointb = new Point(pointa.x - 4*RADIUS, pointa.y);
    Rectangle interPoint = intersectPoint(pointa, pointb);
    Point point2 = new Point(interPoint.x, interPoint.y);
    Point point1 = new Point(interPoint.width, interPoint.height);

    arrowLink = new Element.ArrowLine(new Point(point1.x + x, point1.y + y),
                                            new Point(point2.x + x, point2.y + y),
                                            new BasicStroke(5), Color.green,
                                            "ε");
    arrowLink.draw(g2d);

    LinkedList list = transModel.getSpecialEdge();
    Iterator iter = list.iterator();
    while(iter.hasNext()) {
      SpecialEdge special = (SpecialEdge)iter.next();
      EdgeLink edge = special.getEdge();
      Point pb = edge.n1.getPosition();
      Point pa = edge.n2.getPosition();

      String label = edge.getLabel();
      Rectangle bounds = special.getBounds();
      if(pa.x < pb.x )  {     //*运算中的向前循环
        Point p2 = new Point(pa.x, pa.y - RADIUS);
        Point p1 = new Point(pb.x, pb.y - RADIUS);
        arrowLink = new Element.ArrowLine(new Point(p1.x + x, p1.y + y),
                                          new Point(p1.x + x,
            p1.y + y + bounds.y + RADIUS),
                                          new Point(p2.x + x,
            p2.y + y + bounds.y + RADIUS),
                                          new Point(p2.x + x, p2.y + y),
                                          new BasicStroke(5), Color.green,
                                          label);
      }
      else {         //隔着几个结点的线
        Point p2 = new Point(pa.x, pa.y + RADIUS);
        Point p1 = new Point(pb.x, pb.y + RADIUS);
        arrowLink = new Element.ArrowLine(new Point(p1.x + x, p1.y + y),
                                          new Point(p1.x + x,
            p1.y + y + bounds.height + bounds.y - RADIUS),
                                          new Point(p2.x + x,
            p2.y + y + bounds.height + bounds.y - RADIUS),
                                          new Point(p2.x + x, p2.y + y),
                                          new BasicStroke(5), Color.green,
                                          label);
      }

      arrowLink.draw(g2d);
    }

    g2d.dispose();
  }

  public Rectangle intersectPoint(Point center1, Point center2) {
    double l = Math.sqrt((center2.x - center1.x)*(center2.x - center1.x) +
                         (center2.y - center1.y)*(center2.y - center1.y));

    int x1 = (int)(RADIUS/l*(center2.x - center1.x)) + center1.x;
    int y1 = (int)(RADIUS/l*(center2.y - center1.y)) + center1.y;
    int x2 = (int)((l - RADIUS)/l*(center2.x - center1.x)) + center1.x;
    int y2 = (int)((l - RADIUS)/l*(center2.y - center1.y)) + center1.y;

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

⌨️ 快捷键说明

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