📄 roundedrectanglefiguare.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.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
/**
* 圆角矩形
*
* @author Thihy
*
*/
public class RoundedRectangleFiguare extends RectangularFiguare {
public final static double DEFAULT_ARC_WIDTH = 50.0;
public final static double DEFAULT_ARC_HEIGHT = 50.0;
protected double arc_width = DEFAULT_ARC_WIDTH;
protected double arc_height = DEFAULT_ARC_HEIGHT;
public RoundedRectangleFiguare() {
this(0, 0, null, null);
}
public RoundedRectangleFiguare(int x, int y, Color lineColor,
Color fillColor) {
super(x, y, lineColor, fillColor, "draw rect");
figuare = new RoundRectangle2D.Double(x, y, 0.1, 0.1,
DEFAULT_ARC_WIDTH, DEFAULT_ARC_HEIGHT);
}
@Override
protected Object getFiguareClone() {
return ((RoundRectangle2D.Double) figuare).clone();
}
@Override
public int getID() {
return ROUND_RECTANGEL;
}
@Override
public void select(Graphics2D g) {
super.select(g);
if (isPermanent()) {
arc_width = Math.max(Math.min(Math.abs(x2 - x1 + 1), arc_width), 0);
arc_height = Math.max(Math.min(Math.abs(y2 - y1 + 1), arc_height),
0);
}
AffineTransform saveAt = g.getTransform();
Color saveColor = g.getColor();
Rectangle2D rect = getBounds();
int x = (int) rect.getX(), y = (int) rect.getY(), width = (int) rect
.getWidth(), height = (int) rect.getHeight();
int px1 = x, py1 = y;
Point2D arcwPoint = getPointAfterTransform(px1 + arc_width / 2, py1 - 8);
double arcwX = arcwPoint.getX(), arcwY = arcwPoint.getY();
g.setColor(ARCCHANGE_RECT_COLOR);
int[] xPoints = { (int) (arcwX), (int) (arcwX + 4), (int) (arcwX),
(int) (arcwX - 4) };
int[] yPoints = { (int) (arcwY - 4), (int) (arcwY), (int) (arcwY + 4),
(int) (arcwY) };
Polygon aRect = new Polygon(xPoints, yPoints, 4);
dRects.add(aRect);
g.fill(aRect);
Point2D arcsPoint = getPointAfterTransform(px1 - 8, py1 + arc_height
/ 2);
double arcsX = arcsPoint.getX(), arcsY = arcsPoint.getY();
xPoints = new int[] { (int) (arcsX - 4), (int) (arcsX),
(int) (arcsX + 4), (int) (arcsX) };
yPoints = new int[] { (int) (arcsY), (int) (arcsY + 4), (int) (arcsY),
(int) (arcsY - 4) };
aRect = new Polygon(xPoints, yPoints, 4);
dRects.add(aRect);
g.fill(aRect);
g.setColor(saveColor);
g.setTransform(saveAt);
}
@Override
public void deselect() {
}
@Override
public Cursor getCursor(int Style) {
if (Style == ARC_WIDTH_CHANGE || Style == ARC_HEIGHT_CHANGE)
return Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR);
else
return super.getCursor(Style);
}
@Override
public void moveTo(double px, double py) {
Point2D p = getPointBeforeTransform(px, py);
double x = p.getX();
double y = p.getY();
if (dragStyle == ARC_WIDTH_CHANGE) {
if (isDragging) {
arc_width = 2 * (x - x1);
updateFiguare();
}
} else if (dragStyle == ARC_HEIGHT_CHANGE) {
if (isDragging) {
arc_height = 2 * (y - y1);
updateFiguare();
}
} else {
super.moveTo(px, py);
}
isChanged = true;
}
@Override
public void updateFiguare() {
((RoundRectangle2D.Double) figuare).setFrame(getBounds());
arc_width = Math.max(arc_width, 0);
arc_height = Math.max(arc_height, 0);
((RoundRectangle2D.Double) figuare).archeight = arc_height;
((RoundRectangle2D.Double) figuare).arcwidth = arc_width;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -