⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 associatetool.java

📁 这个是一个java绘图方面的源码
💻 JAVA
字号:
package com.sunking.tp.tool;

import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.*;
import java.awt.geom.*;
import com.sunking.tp.framework.*;
import com.sunking.tp.util.*;

/**
 *
 * <p>Title: </p>
 * <p>Description:连线工具 </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author <a href="mailto:sunkingxie@hotmail.com">SunKing</a>
 * @version 1.0
 */
public class AssociateTool extends AbstractTool {
    /**
     *画虚线用的Stroke
     */
    BasicStroke stroke = new BasicStroke(1.0f, BasicStroke.CAP_BUTT,
        BasicStroke.JOIN_ROUND, 140.0f, new float[] {1.0f}
        , 150.0f);
    Line2D oldLine;
    /**
     *连线源
     */
    JTPComponent source = null;
    /**
     *连线类型
     */
    int assoicateType;
    boolean hasRepaint = false;
    /**
     * @param desk 所属绘图桌面
     * @param assoicateType 连线类型
     * @param icon 图标
     */
    public AssociateTool(Desktop desk, int assoicateType, Icon icon) {
        super(desk);
        this.assoicateType = assoicateType;
        setToolTip(JTPUtil.getString("AssociateTool_TIP"));
        setIcon(icon);
    }

    /**
     *启动工具
     */
    public void enabledTool() {
        super.enabledTool();
        source = null;
        oldLine = null;
    }

    /**
     *弃用工具
     */
    public void disabledTool() {
        super.disabledTool();
        source = null;
        oldLine = null;
    }

    public void mouseDragged(MouseEvent e) {}

    public void mouseClicked(MouseEvent e) {}

    public void mouseReleased(MouseEvent e) {}

    public void mouseEntered(MouseEvent e) {}

    public void mouseExited(MouseEvent e) {}

    public void mousePressed(MouseEvent e) {
        //设置起点和终点
        JTPComponent dest = desk.getJTPComponent(e.getPoint());
        if (dest != null) {
            if (source == null) {
                source = dest;
                source.setSelect(true);
                Component cSource = (Component) source;
                oldLine = new Line2D.Float();
                int x = cSource.getX() + (cSource.getWidth() / 2);
                int y = cSource.getY() + (cSource.getHeight() / 2);
                oldLine.setLine(x, y, x, y);
            } else {
                if (source != e.getSource()
                    && !source.hasAssociator(dest)
                    && !dest.hasAssociator(source)) {
                    int type = UndoableEditFactory.CREATE_ASSOICATORLINE_0;
                    if (assoicateType == Assoicator.ASSOICATETYPE_2) {
                        type = UndoableEditFactory.CREATE_ASSOICATORLINE_1;
                    } else if (assoicateType == Assoicator.ASSOICATETYPE_3) {
                        type = UndoableEditFactory.CREATE_ASSOICATORLINE_2;
                    }
                    UndoableEditFactory.getDefault().startEdit(type, desk,
                        new AssoicatorLine(source,new Assoicator(dest,assoicateType)));
                    UndoableEditFactory.getDefault().stopEdit(null);
                    source.addAssociator(dest, assoicateType);
                }
                source.setSelect(false);
                source = null;
                oldLine = null;
                desk.fireAssoicatorChanged();
            }
            desk.mouseSelect(e.getPoint());
        } else if (e.getSource()instanceof Desktop) {
            if (source != null) {
                source.setSelect(false);
                source = null;
            }
            desk.mouseSelect(e.getPoint());
            ( (Container) desk).repaint();
            hasRepaint = true;
        }
    }

    Component getDesktopPane(Component c) {
        if (c instanceof Desktop)
            return c;
        c = c.getParent();
        if (c == null)
            return null;
        return getDesktopPane(c);
    }

    public void mouseMoved(MouseEvent e) {
        if (oldLine == null)
            return;
        int x = e.getX(), y = e.getY();
        //画虚线
        Component desktop = getDesktopPane(e.getComponent());
        Graphics2D g2 = (Graphics2D) (desktop.getGraphics());
        g2.setStroke(stroke);
        g2.setXORMode(desktop.getBackground());
        if (oldLine != null && !hasRepaint) {
            g2.drawLine( (int) oldLine.getX1(), (int) oldLine.getY1(),
                (int) oldLine.getX2(), (int) oldLine.getY2());
        }
        hasRepaint = false;
        oldLine.setLine(oldLine.getX1(), oldLine.getY1(), e.getX(), e.getY());
        g2.drawLine( (int) oldLine.getX1(), (int) oldLine.getY1(),
            (int) oldLine.getX2(), (int) oldLine.getY2());
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -