📄 abstractfiguare.java
字号:
}
/**
* 设置线条颜色
*
* @param strokeColor
* 新的线条颜色
*/
public void setStrokeColor(Color strokeColor) {
this.strokeColor = strokeColor;
isChanged = true;
}
/**
* 获取填充颜色
*
* @return 填充颜色
*/
public Color getFillColor() {
return fillColor;
}
/**
* 设置填充颜色
*
* @param fillColor
* 新的填充颜色
*/
public void setFillColor(Color fillColor) {
this.fillColor = fillColor;
isChanged = true;
}
/**
* 预选这个图形
*/
public abstract void preSelect(Graphics2D g);
/**
* 绘出选择框
*/
public abstract void select(Graphics2D g);
/**
* 取消选择框
*/
public abstract void deselect();
/**
* 获取当前鼠标所在的小方块标号
*/
public int getDirectionRect(double x, double y) {
int index = -1;
for (int i = 0; i < dRects.size(); ++i) {
if ((i >= N_SHEAR_BORDER && i <= W_SHEAR_BORDER)) {
if ((dRects.elementAt(i))
.contains(getPointBeforeTransform(x, y))) {
index = i;
break;
}
} else if ((dRects.elementAt(i)).contains(x, y)) {
index = i;
break;
}
}
// 下面禁用切变的非北边界小块
if (index == E_SHEAR_BORDER || index == S_SHEAR_BORDER
|| index == W_SHEAR_BORDER)
return -1;
return index;
}
/**
* 先还原旋转,再还原切变
*/
public Point2D getPointBeforeTransform(double x, double y) {
Point2D pr = getPointBeforeRotate(x, y);
Point2D ps = getPointBeforeShear(pr.getX(), pr.getY());
return ps;
}
/**
* 先切变再旋转之后的结果。
*/
public Point2D getPointAfterTransform(double x, double y) {
Point2D ps = getPointAfterShear(x, y);
Point2D pr = getPointAfterRotate(ps.getX(), ps.getY());
return pr;
}
/**
* 获取旋转之后的状态
*/
public Point2D getPointAfterRotate(double x, double y) {
double px, py;
x -= rx;
y -= ry;
px = rx + x * Math.cos(rtTheta) - y * Math.sin(rtTheta);
py = ry + y * Math.cos(rtTheta) + x * Math.sin(rtTheta);
return new Point2D.Double(px, py);
}
/**
* 获取旋转之前的状态
*/
public Point2D getPointBeforeRotate(double x, double y) {
double px, py;
x -= rx;
y -= ry;
px = rx + x * Math.cos(rtTheta) + y * Math.sin(rtTheta);
py = ry + y * Math.cos(rtTheta) - x * Math.sin(rtTheta);
return new Point2D.Double(px, py);
}
/**
* 获取切变之后的状态
*/
public Point2D getPointAfterShear(double x, double y) {
transX = -Math.max(y1, y2) * Math.tan(shXTheta);
transY = 0;
double px = x + Math.tan(shXTheta) * y + transX;
double py = y + Math.tan(shYTheta) * x + transY;
return new Point2D.Double(px, py);
}
/**
* 获取切变之前的状态
*/
public Point2D getPointBeforeShear(double px, double py) {
transX = -Math.max(y1, y2) * Math.tan(shXTheta);
transY = 0;
double sx = Math.tan(shXTheta), sy = Math.tan(shYTheta);
double x, y;
x = (sx * (py - transY) - (px - transX)) / (sx * sy - 1);
y = (sy * (px - transX) - (py - transY)) / (sx * sy - 1);
return new Point2D.Double(x, y);
}
/**
* 根据小方块的id获取相应的鼠标
*
* @param Style
* 小方块的id
* @return 鼠标
*/
public abstract Cursor getCursor(int Style);
/**
* 从此点开始移动
*/
public void willMoveFromHere(double x, double y) {
dragStyle = -1;
perm = false;
isDragging = false;
mx = x;
my = y;
// 暂存值
tx1 = x1;
ty1 = y1;
tx2 = x2;
ty2 = y2;
tshXTheta = shXTheta;
tshYTheta = shYTheta;
ttransX = transX;
ttransY = transY;
}
/**
* 从此点开始改变图形外形
*/
public void willDragFromHere(int style, double x, double y) {
willMoveFromHere(x, y);
perm = false;
isDragging = true;
dragStyle = style;
}
/**
* 判断是否正在改变图形外形
*
* @return 如果正在改变图形外形返回真
*/
public boolean isDragging() {
return isDragging;
}
/**
* 改变图形外形完毕
*/
public void dragged() {
isDragging = false;
dragStyle = -1;
makePermanent();
Point2D pp = getPointAfterShear((x1 + x2) / 2, (y1 + y2) / 2);
double trX = pp.getX(), trY = pp.getY();
Point2D p = getPointAfterRotate(trX, trY);
x1 = x1 - trX + p.getX();
y1 = y1 - trY + p.getY();
x2 = x2 - trX + p.getX();
y2 = y2 - trY + p.getY();
rx = p.getX();
ry = p.getY();
isChanged = true;
}
/**
* 重写toString
*/
@Override
public String toString() {
return getClass().getName() + "()" + serialId + "[x1=" + x1 + ",y1="
+ y1 + ",x2=" + x2 + ",y2=" + y2 + "]";
}
/**
* 顺时针旋转一个角度
*
* @param theta
* 旋转的角度
*/
public void rotate(double theta) {
double trX = (x1 + x2) / 2, trY = (y1 + y2) / 2;
Point2D pr = getPointAfterShear(trX, trY);
rx = pr.getX();
ry = pr.getY();
rtTheta += theta / 180 * Math.PI;
isChanged = true;
}
/**
* 重置旋转
*/
public void rotateRedo() {
double trX = (x1 + x2) / 2, trY = (y1 + y2) / 2;
Point2D pr = getPointAfterShear(trX, trY);
rx = pr.getX();
ry = pr.getY();
rtTheta = 0;
isChanged = true;
}
/**
* 向右切变一段距离
*/
public void rightShear() {
shXTheta = Math.atan(Math.tan(shXTheta) - (Math.abs(x1 - x2) + 1)
/ (Math.abs(y1 - y2) + 1));
isChanged = true;
}
/**
* 向左切变一段距离
*/
public void leftShear() {
shXTheta = Math.atan(Math.tan(shXTheta) + (Math.abs(x1 - x2) + 1)
/ (Math.abs(y1 - y2) + 1));
isChanged = true;
}
/**
* 重置切变
*/
public void redoShear() {
shXTheta = 0;
isChanged = true;
}
/**
* 获取线宽
*
* @return 线宽
*/
public double getLineWidth() {
return lineWidth;
}
/**
* 设置线宽
*/
public void setLineWidth(double lw) {
lineWidth = lw;
isChanged = true;
}
/**
* 获取虚线间距
*
* @return 虚线间距
*/
public double getLineDis() {
return lineStyle;
}
/**
* 设置虚线间距
*/
public void setLineDis(double ld) {
lineStyle = ld;
isChanged = true;
}
/**
* 移动一个位移量
*/
public void translate(int dx, int dy) {
willMoveFromHere(0, 0);
moveTo(dx, dy);
dragged();
}
protected int dragStyle;
protected boolean isDragging;
protected Vector<Shape> dRects = new Vector<Shape>();
// 常量
public static final int STRAIGHT_LINE = 0x10;
public static final int RECTANGEL = 0x12;
public static final int ROUND_RECTANGEL = 0x14;
public static final int ELLIPSE = 0x16;
public static final int TEXT = 0x18;
public static final int HAND = 0x1a;
public static final int MAGNIFIER = 0x1c;
public static final int ARROW = 0x20;
public static final int SELECT = 0x22;
// 下面这些常量是显示在图形上的一些小块的样式ID,注意它们必须是连续的。
// 这些是改变线形的
public static final int LINE_WIDTH_CHANGE = 0;
public static final int LINE_STYLE_CHANGE = 1;
// 用于直线的选中
public static final int ONE_POINT = 2;
public static final int ANOTHER_POINT = 3;
// 这些是显示在矩形选框上的八个小块的ID
// 必须是顺时针方向,因为旋转后光标改变,见getCursor
public static final int EAST = 2;
public static final int SOUTH_EAST = 3;
public static final int SOUTH = 4;
public static final int SOUTH_WEST = 5;
public static final int WEST = 6;
public static final int NORTH_WEST = 7;
public static final int NORTH = 8;
public static final int NORTH_EAST = 9;
// 旋转小块的ID
public static final int ROTATE = 10;
// 用于切边,在选择边界附件上的一个隐藏的矩形块,用于切边功能(直线没有这个功能)
// 顺时针方向
public final static int N_SHEAR_BORDER = 11;
public final static int E_SHEAR_BORDER = 12;
public final static int S_SHEAR_BORDER = 13;
public final static int W_SHEAR_BORDER = 14;
// 圆角矩形上用于改变角形状的小块ID
public final static int ARC_WIDTH_CHANGE = 15;
public final static int ARC_HEIGHT_CHANGE = 16;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -