博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UMLet
阅读量:4605 次
发布时间:2019-06-09

本文共 9846 字,大约阅读时间需要 32 分钟。

轻量级UML建模工具,

//Umlet\sourcefiles\src\com\baselet\diagram\draw\BaseDrawHandler.javapackage com.baselet.diagram.draw;import java.awt.AlphaComposite;import java.awt.Color;import java.awt.Dimension;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Polygon;import java.awt.Rectangle;import java.awt.Shape;import java.awt.geom.Arc2D;import java.awt.geom.CubicCurve2D;import java.awt.geom.Ellipse2D;import java.awt.geom.Line2D;import java.awt.geom.QuadCurve2D;import java.awt.geom.RoundRectangle2D;import com.baselet.control.Constants;import com.baselet.control.Constants.AlignHorizontal;import com.baselet.control.Utils;import com.baselet.diagram.DiagramHandler;public class BaseDrawHandler {    private DiagramHandler handler;    // Save some objects needed to draw the shapes    private Graphics2D g2;    private Style style;    private Color bgColor;    private Color fgColor;    private int width;    private int height;    private boolean isSelected;    public BaseDrawHandler(Graphics g, DiagramHandler handler, Color fgColor, Color bgColor, Dimension size, boolean isSelected) {        this.g2 = (Graphics2D) g;        this.handler = handler;        this.fgColor  = fgColor;        this.bgColor = bgColor;        g2.setFont(handler.getFontHandler().getFont());        g2.setColor(fgColor);        this.style = new Style(fgColor, bgColor, (int) handler.getFontHandler().getFontSize(false));        this.width = size.width;        this.height = size.height;        this.isSelected = isSelected;    }    private float getZoom() {        return handler.getZoomFactor();    }    private void drawShape(Shape s) {        // Shapes Background        g2.setColor(style.getBgColor());        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, style.getBgAlpha()));        this.g2.fill(s);        // Shapes Foreground        g2.setColor(style.getFgColor());        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, style.getFgAlpha()));        g2.setStroke(Utils.getStroke(style.getLineType(), style.getLineThickness()));        this.g2.draw(s);    }    private void drawFilledShape(Shape s) {        // Shapes Background        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, style.getFgAlpha()));        g2.setColor(style.getFgColor());        g2.fillRoundRect(s.getBounds().x, s.getBounds().y, s.getBounds().width, s.getBounds().height, s.getBounds().width, s.getBounds().height);        // Shapes Foreground        g2.setColor(style.getFgColor());        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, style.getFgAlpha()));        this.g2.draw(s);    }    private void drawText(Text t) {        g2.setColor(style.getFgColor());        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, style.getFgAlpha()));        handler.getFontHandler().setFontSize(style.getFontSize());        g2.setFont(handler.getFontHandler().getFont());        handler.getFontHandler().writeText(this.g2, t.getText(), t.getX(), t.getY(), t.getHorizontalAlignment());        handler.getFontHandler().resetFontSize();    }    /*     * TEXT METHODS     */    public final void print(String text, int x, int y) {        drawText(new Text(text, (int) (x * getZoom()), (int) (y * getZoom()), AlignHorizontal.LEFT));    }    public final void print(String text, int x, int y, AlignHorizontal align) {        drawText(new Text(text, (int) (x * getZoom()), (int) (y * getZoom()), align));    }    public final void printLeft(String text, int y) {        drawText(new Text(text, (int) handler.getFontHandler().getDistanceBetweenTexts(), (int) (y * getZoom()), AlignHorizontal.LEFT));    }    public final void printRight(String text, int y) {        drawText(new Text(text, (int) (width - handler.getFontHandler().getDistanceBetweenTexts()), (int) (y * getZoom()) + 20, AlignHorizontal.RIGHT));    }    public final void printCenter(String text, int y) { drawText(new Text(text, width / 2, (int) (y * getZoom()), AlignHorizontal.CENTER));    }    public final int textHeight() {        return textDimension("dummy").height;    }    public final int textWidth(String text) {        return textDimension(text).width;    }    private final Dimension textDimension(String text) {        boolean specialFontSize = (style.getFontSize() != (int) handler.getFontHandler().getFontSize(false));        if (specialFontSize) {            handler.getFontHandler().setFontSize(style.getFontSize());        }        Dimension returnVal = handler.getFontHandler().getTextSize(text, false);        if (specialFontSize) {            handler.getFontHandler().resetFontSize();        }        return returnVal;    }    /*     * DRAW METHODS     */    public final void drawArcOpen(float x, float y, float width, float height, float start, float extent) {        drawShape(new Arc2D.Float(x * getZoom(), y * getZoom(), width * getZoom(), height * getZoom(), start, extent, Arc2D.OPEN));    }    public final void drawArcChord(float x, float y, float width, float height, float start, float extent) {        drawShape(new Arc2D.Float(x * getZoom(), y * getZoom(), width * getZoom(), height * getZoom(), start, extent, Arc2D.CHORD));    }    public final void drawArcPie(float x, float y, float width, float height, float start, float extent) {        drawShape(new Arc2D.Float(x * getZoom(), y * getZoom(), width * getZoom(), height * getZoom(), start, extent, Arc2D.PIE));    }    public final void drawCircle(int x, int y, int radius) {        drawFilledShape(new Ellipse2D.Float((int) ((x - radius) * getZoom()), (int) ((y - radius) * getZoom()), (int) (radius * 2 * getZoom()), (int) (radius * 2 * getZoom())));    }    public final void drawCurveCubic(float x1, float y1, float ctrlx1, float ctrly1, float ctrlx2, float ctrly2, float x2, float y2) {        drawShape(new CubicCurve2D.Float(x1 * getZoom(), y1 * getZoom(), ctrlx1 * getZoom(), ctrly1 * getZoom(), ctrlx2 * getZoom(), ctrly2 * getZoom(), x2 * getZoom(), y2 * getZoom()));    }    public final void drawCurveQuad(float x1, float y1, float ctrlx, float ctrly, float x2, float y2) {        drawShape(new QuadCurve2D.Float(x1 * getZoom(), y1 * getZoom(), ctrlx * getZoom(), ctrly * getZoom(), x2 * getZoom(), y2 * getZoom()));    }    public final void drawEllipse(int x, int y, int radiusX, int radiusY) {        drawShape(new Ellipse2D.Float((int) ((x - radiusX) * getZoom()), (int) ((y - radiusY) * getZoom()), (int) (radiusX * 2 * getZoom()), (int) (radiusY * 2 * getZoom())));    }    public final void drawLine(int x1, int y1, int x2, int y2) {        drawShape(new Line2D.Float((int) (x1 * getZoom()), (int) (y1 * getZoom()), (int) (x2 * getZoom()), (int) (y2 * getZoom())));    }    public final void drawLineHorizontal(int y) {        drawShape(new Line2D.Float((int) (0 * getZoom()), (int) (y * getZoom()), handler.realignToGrid(false, width, true), (int) (y * getZoom())));    }    public final void drawLineVertical(int x) {        drawShape(new Line2D.Float((int) (x * getZoom()), (int) (0 * getZoom()), (int) (x * getZoom()), handler.realignToGrid(false, height, true)));    }    public final void drawPolygon(Polygon polygon) {        for (int i = 0; i < polygon.xpoints.length; i++) {            polygon.xpoints[i] *= getZoom();        }        for (int i = 0; i < polygon.ypoints.length; i++) {            polygon.ypoints[i] *= getZoom();        }        drawShape(polygon);    }    public final void drawRectangle(int x, int y, int width, int height) {        drawShape(new Rectangle((int) (x * getZoom()), (int) (y * getZoom()), (int) (width * getZoom()), (int) (height * getZoom())));    }    public final void drawRectangleRound(int x, int y, int width, int height, float arcw, float arch) {        drawShape(new RoundRectangle2D.Float((int) (x * getZoom()), (int) (y * getZoom()), (int) (width * getZoom()), (int) (height * getZoom()), arcw * getZoom(), arch * getZoom()));    }    /*     * STYLING METHODS     */    public final void setForeground(String color, float alpha) {        setForegroundColor(color);        setForegroundAlpha(alpha);    }    public final void setForeground(Color color, float alpha) {        setForegroundColor(color);        setForegroundAlpha(alpha);    }    public final void setBackground(String color, float alpha) {        setBackgroundColor(color);        setBackgroundAlpha(alpha);    }    public final void setBackground(Color color, float alpha) {        setBackgroundColor(color);        setBackgroundAlpha(alpha);    }    public final void setForegroundColor(String color) {        if (color.equals("fg")) setForegroundColor(fgColor);        else setForegroundColor(Utils.getColor(color)); // if fgColor is not a valid string null will be set    }    public final void setForegroundColor(Color color) {        if (isSelected) style.setFgColor(Constants.DEFAULT_SELECTED_COLOR);        else if (color == null) style.setFgColor(Constants.DEFAULT_FOREGROUND_COLOR);        else style.setFgColor(color);    }    public final void setBackgroundColor(String color) {        if (color.equals("bg")) setBackgroundColor(bgColor);        else setBackgroundColor(Utils.getColor(color));    }    public final void setBackgroundColor(Color color) {        if (color == null) style.setBgColor(Constants.DEFAULT_BACKGROUND_COLOR);        else style.setBgColor(color);    }    public final void setForegroundAlpha(float alpha) {        style.setFgAlpha(alpha);    }    public final void setBackgroundAlpha(float alpha) {        style.setBgAlpha(alpha);    }    public void resetColorSettings() {        setForeground("fg", Constants.ALPHA_NO_TRANSPARENCY);        setBackground("bg", Constants.ALPHA_FULL_TRANSPARENCY);    }    public final void setFontSize(int fontSize) {        style.setFontSize(fontSize);    }}

 

转载于:https://www.cnblogs.com/mark-huang/archive/2013/06/06/3122836.html

你可能感兴趣的文章
如何在移动设备上搭建服务器承载自己的全景作品?
查看>>
iOS SQLite3数据库操作
查看>>
除了 iOS 和 Android,世界第三大移动系统是什么?
查看>>
35.7. FAQ
查看>>
深搜算法实例:老鼠走迷宫(一)
查看>>
VMWare网络设置的3中方式(转)
查看>>
支付这条线上 谁在赚钱谁在哭?
查看>>
机器学习之朴素贝叶斯分类
查看>>
亚信安全参加第六届全国等保技术大会 态势感知助力“等保2.0”落地
查看>>
【设计模式系列】--抽象工厂
查看>>
JqueryValidate 动态添加验证
查看>>
双活数据中心的架构
查看>>
大数据公司Palantir融得7亿美元 曾追踪拉登
查看>>
先行者长虹佳华超融合市场沙龙在京举行
查看>>
建立备份策略的重要性
查看>>
小白用户如何轻松上云 -我的轻量应用服务器探索记
查看>>
BCG与阿里研究院等联合揭秘中国互联网经济:成功的关键是什么?
查看>>
发力IoT领域 Marvell注重生态系统发展
查看>>
数据中心网络布线工程必备七大件
查看>>
20个问题揭穿冒牌数据科学家
查看>>