📄 textfiguare.java
字号:
package draw.figuare;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.font.FontRenderContext;
import java.awt.font.LineMetrics;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import app.pane.FontDialog;
/**
* 文字图形
*
* @author Thihy
*
*/
public class TextFiguare extends RectangularFiguare {
protected String text;
protected Font font;
protected boolean shouldChangeBound;
protected Color textColor;// 保留未用
protected double textWidth, textLineHeight, textDesent;
public TextFiguare() {
this(0, 0, null, null);
}
public TextFiguare(double x, double y, Color lineColor, Color fillColor) {
super(x, y, lineColor, fillColor, "draw Text");
shouldChangeBound = false;
font = new Font("微软雅黑", Font.PLAIN, 16);
figuare = new Rectangle2D.Double(x, y, 10.0, 10.0);
textColor = Color.black;
this.strokeColor = null;
this.fillColor = fillColor;
editText();
}
@Override
public int getID() {
return TEXT;
}
@Override
public Object clone() {
Object a = super.clone();
TextFiguare tf = (TextFiguare) a;
tf.text = new String(text);
tf.scaleX = scaleX;
tf.scaleY = scaleY;
tf.font = font;
tf.shouldChangeBound = shouldChangeBound;
tf.textColor = textColor;
tf.textDesent = textDesent;
tf.textLineHeight = textLineHeight;
tf.textWidth = textWidth;
return a;
}
/**
* 编辑文本,会弹出对话框
*/
public void editText() {
FontDialog fd = new FontDialog(font, textColor, text);
fd.setVisible(true);
font = fd.getChoosedFont();
fillColor = textColor = fd.getChoosedColor();
setText(fd.getText());
}
/**
* 获取文本
*/
public String getText() {
return text;
}
/**
* 设置文本
*
* @param str
*/
public void setText(String str) {
text = str;
shouldChangeBound = true;
isChanged = true;
}
@Override
public void draw(Graphics2D g) {
if (text.equals(""))
return;// text为空就没有必要画了
Color saveColor = g.getColor();
AffineTransform saveAt = g.getTransform();
g.rotate(rtTheta, rx, ry);
g.shear(Math.tan(shXTheta), Math.tan(shYTheta));
g.translate(-Math.max(y1, y2) * Math.tan(shXTheta), 0);
g.setFont(font);
if (shouldChangeBound) {
FontRenderContext frc = g.getFontRenderContext();
Rectangle2D rect = font.getStringBounds(text, frc);// 获得文字边框
LineMetrics metrics = font.getLineMetrics(text, frc);
textWidth = rect.getWidth();
textLineHeight = metrics.getHeight();
textDesent = metrics.getDescent();
this.x2 = x1 + offset.left + offset.right + scaleX * textWidth;
this.y2 = y1 + offset.top + offset.bottom + scaleY * textLineHeight;
shouldChangeBound = false;
} else {
scaleX = Math.abs(Math.abs(x2 - x1) - offset.left - offset.right)
/ textWidth;
scaleY = Math.abs(Math.abs(y2 - y1) - offset.top - offset.bottom)
/ textLineHeight;
}
updateFiguare();
double scaleXY = Math.sqrt(scaleX * scaleX + scaleY * scaleY);
g.setStroke(new BasicStroke((float) (lineWidth / scaleXY),
BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER, 10.0f,
new float[] { (float) ((Math.max(2.0, lineStyle / scaleXY))),
(float) (lineStyle / scaleXY) }, 0.0f));
Shape shape = font.createGlyphVector(g.getFontRenderContext(), text)
.getOutline();
g.scale(scaleX, scaleY);
double strX = (Math.min(x1, x2) + offset.left) / scaleX;
double strY = (Math.min(y1, y2) + offset.top) / scaleY + textLineHeight
- textDesent;
g.translate(strX, strY);
if (strokeColor != null) {
g.setColor(strokeColor);
g.draw(shape);
}
if (fillColor != null) {
g.translate(0.1, 0.1);
g.setColor(fillColor);
g.fill(shape);
}
g.setColor(saveColor);
g.setTransform(saveAt);
}
@Override
public boolean contains(double x, double y) {
Point2D p = getPointBeforeTransform(x, y);
boolean returnValue = figuare.contains(p);
return returnValue;
}
@Override
public Rectangle2D getExtraBounds() {
Rectangle2D.Double rect = (Rectangle2D.Double) getBounds();
return (Rectangle2D) rect.clone();
}
@Override
public void drawTo(int x, int y) {
}
@Override
public void deselect() {
}
@Override
public Cursor getCursor(int Style) {
return super.getCursor(Style);
}
@Override
public void select(Graphics2D g) {
super.select(g);
}
@Override
public void updateFiguare() {
((Rectangle2D.Double) figuare).setFrame(getBounds());
}
@Override
protected Object getFiguareClone() {
return ((Rectangle2D.Double) figuare).clone();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -