📄 linefiguare.java
字号:
package draw.figuare;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.geom.AffineTransform;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
/**
* 直线图形
*
* @author Thihy
*
*/
public class LineFiguare extends AbstractFiguare {
public LineFiguare() {
this(0, 0, null, null);
}
public LineFiguare(int x, int y, Color lineColor, Color fillColor) {
super(x, y, lineColor, fillColor, "draw rect");
figuare = new Line2D.Double(x, y, x, y);
}
@Override
public int getID() {
return STRAIGHT_LINE;
}
@Override
protected Object getFiguareClone() {
return ((Line2D.Double) figuare).clone();
}
@Override
public void drawTo(int x, int y) {
this.x2 = x;
this.y2 = y;
updateFiguare();
}
@Override
public void moveTo(double px, double py) {
if (isDragging) {
rtTheta = Math.atan((y1 - y2) / (x1 - x2));
rx = (x1 + x2) / 2;
ry = (y1 + y2) / 2;
Point2D p = getPointBeforeTransform(px, py);
double x = p.getX();
double y = p.getY();
System.out.println(px == x);
rtTheta = 0;
switch (dragStyle) {
case ONE_POINT:
this.x1 = px;
this.y1 = py;
break;
case ANOTHER_POINT:
this.x2 = px;
this.y2 = py;
break;
case LINE_STYLE_CHANGE:
if (x <= rx) {
lineStyle = (rx - x) / 2;
if (lineStyle > LINE_MAX_STYLE) {
lineStyle = LINE_MAX_STYLE;
}
}
break;
case LINE_WIDTH_CHANGE:
if (y <= ry - 8) {
lineWidth = (ry - y - 8) * 2;
if (lineWidth > LINE_MAX_WIDTH) {
lineWidth = LINE_MAX_WIDTH;
}
}
break;
default:
break;
}
} else {
super.moveTo(px, py);
}
isChanged = true;
updateFiguare();
}
@Override
public void makePermanent() {
perm = true;
isChanged = true;
updateFiguare();
}
@Override
public boolean contains(double x, double y) {
double allowDist = lineWidth / 2 + 3.0;
if (((Line2D.Double) figuare).ptLineDist(x, y) < allowDist) {
if (getExtraBounds().contains(x, y))
return true;
}
return false;
}
@Override
public void deselect() {
}
@Override
public void preSelect(Graphics2D g) {
if (isSelect())
return;
AffineTransform saveAt = g.getTransform();
Color saveColor = g.getColor();
g.setColor(PRESELECT_RECT_COLOR);
g.draw(figuare);
g.setColor(saveColor);
g.setTransform(saveAt);
}
@Override
public void select(Graphics2D g) {
if (!isSelect())
return;
AffineTransform saveAt = g.getTransform();
Color saveColor = g.getColor();
dRects.clear();
Rectangle2D.Double dRect;
for (int i = 0; i < 4; ++i) {
dRect = new Rectangle2D.Double();
dRects.add(dRect);
}
g.setColor(DRAG_RECT_COLOR);
int size = DRAG_RECT_SIZE;
((Rectangle2D.Double) dRects.elementAt(ONE_POINT)).setFrame(x1 - size,
y1 - size, 2 * size, 2 * size);// 西北
((Rectangle2D.Double) dRects.elementAt(ANOTHER_POINT)).setFrame(x2
- size, y2 - size, 2 * size, 2 * size);// 东北
for (int i = 2; i < 4; ++i) {
g.fill(dRects.elementAt(i));
}
double cx = (x1 + x2) / 2, cy = (y1 + y2) / 2;
double length = Math.sqrt(Math.pow(x1 - cx, 2.0)
+ Math.pow(y1 - cy, 2.0));
double px1 = cx - length, py1 = cy, px2 = cx, py2 = cy;
g.rotate(Math.atan((y1 - y2) / (x1 - x2)), cx, cy);
// 边框变化
g.setColor(LINECHANGE_RECT_COLOR);
// 线宽
int[] xPoints = { (int) (px1), (int) (px1 + 4), (int) (px1),
(int) (px1 - 4) };
int[] yPoints = { (int) (py1 - 16 - lineWidth / 2),
(int) (py1 - 12 - lineWidth / 2),
(int) (py1 - 8 - lineWidth / 2),
(int) (py1 - 12 - lineWidth / 2) };
Polygon aRect = new Polygon(xPoints, yPoints, 4);
dRects.setElementAt(aRect, LINE_WIDTH_CHANGE);
g.fill(aRect);
// 线间间距
xPoints = new int[] { (int) (px2 - 4 - 2 * lineStyle),
(int) (px2 - 2 * lineStyle), (int) (px2 + 4 - 2 * lineStyle),
(int) (px2 - 2 * lineStyle) };
yPoints = new int[] { (int) (py2 - 8), (int) (py2 - 12),
(int) (py2 - 8), (int) (py2 - 4) };
aRect = new Polygon(xPoints, yPoints, 4);
dRects.setElementAt(aRect, LINE_STYLE_CHANGE);
g.fill(aRect);
g.setColor(saveColor);
g.setTransform(saveAt);
}
@Override
public int getDirectionRect(double x, double y) {
for (int i = 0; i < 2; ++i) {
rtTheta = Math.atan((y1 - y2) / (x1 - x2));
rx = (x1 + x2) / 2;
ry = (y1 + y2) / 2;
Point2D p = getPointBeforeTransform(x, y);
double px = p.getX();
double py = p.getY();
rtTheta = 0;
if ((dRects.elementAt(i)).contains(px, py)) {
return i;
}
}
for (int i = 2; i < dRects.size(); ++i) {
if ((dRects.elementAt(i)).contains(x, y)) {
return i;
}
}
return -1;
}
@Override
public Cursor getCursor(int Style) {
switch (Style) {
case ONE_POINT:
case ANOTHER_POINT:
return Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR);
case LINE_STYLE_CHANGE:
case LINE_WIDTH_CHANGE:
return Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR);
default:
return null;
}
}
@Override
public void updateFiguare() {
rtTheta = 0;
((Line2D.Double) figuare).setLine(x1, y1, x2, y2);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -