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

📄 dsline.java

📁 用JAVA编写的绘图程序 功能简介: 支持存储
💻 JAVA
字号:
package drawsmart.itsv.basic;

import java.awt.geom.*;
import java.awt.*;
import drawsmart.itsv.framework.JDSComponentface;
import drawsmart.itsv.framework.JDSLineface;

/**
 * <p>Title: 直线基类</p>
 * <p>Description: 实现基本的重绘方法,直线基础接口</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author 崔江
 * @version 2.0
 */

public abstract class DSLine extends Line2D.Double implements JDSLineface
{
  /**
   *无箭头连接
   */
  public static final int ASSOICATETYPE_1 = 1;
  /**
   *空心箭头连接
   */
  public static final int ASSOICATETYPE_2 = 2;
  /**
   *实心箭头连接
   */
  public static final int ASSOICATETYPE_3 = 3;

  private int ASSOICATETYPE=0;

  //开始组件
  private JDSComponentface startComponentface;

  //结束组件
  private JDSComponentface endComponentface;

  //选择状态
  boolean selectBoolean=false;

  public DSLine()
  {
    //初始化默认是无箭头的直线
    ASSOICATETYPE=ASSOICATETYPE_1;
  }

  /**
   * 设置直线类型,实现接口方法
   * @param i
   */
  public void setModeLine(int i)
  {
    ASSOICATETYPE=i;
  }

  /**
   * 重绘线条,实现接口方法
   * @param g
   */
  public void drawLine(Graphics2D g)
  {
    Graphics2D g2D = (Graphics2D) g;

    Point2D pStart = this.getP1();
    Point2D pEnd = getP2();

    //绘制直线
    g2D.drawLine(new java.lang.Double(pStart.getX()).intValue(),
                 new java.lang.Double(pStart.getY()).intValue(),
                 new java.lang.Double(pEnd.getX()).intValue(),
                 new java.lang.Double(pEnd.getY()).intValue());

  }

  /** 获得这个直线的开始组件 */
  public JDSComponentface getJDSComponentStart()
  {
    return startComponentface;
  }

  /** 获得这个直线的结束组件 */
  public JDSComponentface getJDSComponentEnd() {
    return endComponentface;
  }

  /** 设置这个直线的开始组件
   *
   * @param tpc 该直线的开始组件 */
  public void setJDSComponentStart(JDSComponentface tpc) {
    startComponentface=tpc;
  }

  /** 设置这个直线的结束组件
   *
   * @param tpc 设置这个直线的结束组件 */
  public void setJDSComponentEnd(JDSComponentface tpc) {
    endComponentface=tpc;
  }

  /** 判断是否包含一个点,主要用于点击时的选取动作
   *
   * @param p 鼠标点击的位置点 */
  public boolean containPoint(Point2D p) {
    selectBoolean=contains((int)p.getX(),(int)p.getY());
    return selectBoolean;
  }

  /**
   * 判断是否包含点(1)
   * 暂时没用到
   * @param x
   * @param y
   * @return
   */
  public boolean contains(int x, int y) {
    return insideSegment(x, y, (int) getX1(), (int) getY1(), (int) getX2(),
                         (int) getY2());
  }
  /**
   * 判断是否包含点(2)
   * @param x
   * @param y
   * @param x0
   * @param y0
   * @param x1
   * @param y1
   * @return
   */
  private boolean insideSegment(int x, int y, int x0, int y0, int x1, int y1) {
    if (x1 == x0)
      return (x == x0) && (y >= Math.min(y0, y1)) && (y <= Math.max(y0, y1));
    Rectangle bounds = new Rectangle(x0, y0, 1, 1);
    int fudge = (int) Math.round(insideTolerance());
    bounds.add(x1, y1);
    bounds.grow(fudge, fudge);
    if (!bounds.contains(x, y))
      return false;
    double slope = (double) (y1 - y0) / (double) (x1 - x0);
    double fx = (slope * (double) (x - x0)) + (double) y0;
    return Math.abs(fx - (double) y) < insideTolerance(slope);
  }
  /**
   * 判断是否包含点(3)
   * @param slope
   * @return
   */
  private double insideTolerance(double slope) {
    return Math.max(insideTolerance(), Math.abs(slope));
  }
  /**
   * 判断是否包含点(4)
   * @return
   */
  private double insideTolerance() {
    return 2.5;
  }

  /**
   * 获得开始点
   * @return
   */
  public Point2D getStartPoint()
  {
    return getP1();
  }

  /**
   * 获得结束点
   * @return
   */
  public Point2D getEndPoint()
  {
    return getP2();
  }

  public boolean getIsSelect()
  {
    return selectBoolean;
  }

  /**
   * 绘制直线
   * @param x1 int
   * @param y1 int
   * @param x2 int
   * @param y2 int
   */
  public void setLine(int x1,int y1,int x2,int y2)
  {
    super.setLine(x1,y1,x2,y2);
  }
}

⌨️ 快捷键说明

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