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

📄 selectiontool.java

📁 用java实现的画图程序
💻 JAVA
字号:
package com.sunking.tp.tool;

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

/**
 *
 * <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 SelectionTool
    extends AbstractTool {
    Component oldC;
    Point oldP = new Point(0, 0);
    int handleIndex = -1;
    Rectangle oldRec;
    /**
     *
     * @param desk
     */
    public SelectionTool(Desktop desk) {
        super(desk);
        setToolTip("Selection and drag.");
    }

    /**
     *激活工具
     */
    public void enabledTool() {
        super.enabledTool();
        oldC = null;
        oldP = new Point(0, 0);
        desk.setCursor(Cursor.getDefaultCursor());
    }

    /**
     *灰化工具
     */
    public void disabledTool() {
        super.disabledTool();
        oldC = null;
        oldP = new Point(0, 0);
    }

    public void mouseMoved(MouseEvent e) {}

    public void mouseDragged(MouseEvent e) {
        if (oldP == null || !SwingUtilities.isLeftMouseButton(e)) {//非左键不处理
            return;
        }
        if (oldC != null) {
            Point newP = e.getPoint();
            if (handleIndex == -1) {
				//移动全部组件
                JTPComponent selection[] = desk.getSelectionJTPComponent();
                for (int index = 0; index < selection.length; index++) {
                    Component c = ( (Component) selection[index]);
                    c.setLocation(c.getX() + (newP.x - oldP.x),
                                  c.getY() + (newP.y - oldP.y));
                }
//                oldC.setLocation(oldC.getX() + (newP.x - oldP.x),
//                                 oldC.getY() + (newP.y - oldP.y));
                oldP = newP;
            }
            else {
				//拖动图柄改变组件大小
                ( (JTPComponent) oldC).moveHandle(handleIndex, oldRec,
                                                  newP.x - oldP.x,
                                                  newP.y - oldP.y);
            }
            desk.fireAssoicatorChanged(); //repaint assoicator line;
        }
        else {
			//拉框多选组件
            int x = e.getX(), y = e.getY(), m = (int) oldP.getX(),
                n = (int) oldP.getY();
            int tmp = 0;
            if (x < m) {
                tmp = x;
                x = m;
                m = tmp;
            }
            if (y < n) {
                tmp = y;
                y = n;
                n = tmp;
            }
            desk.mouseSelect(new Rectangle(m, n, x - m, y - n));
        }
    }

    public void mouseReleased(MouseEvent e) {
        oldP = new Point(0, 0);
        oldC = null;
        desk.setCursor(Cursor.getDefaultCursor());
        desk.mouseSelect( (Rectangle)null);
    }

    public void mouseEntered(MouseEvent e) {}

    public void mouseExited(MouseEvent e) {}

    public void mouseClicked(MouseEvent e) {}

    public void mousePressed(MouseEvent e) {
        if ( (e.getModifiers() & InputEvent.CTRL_MASK) == 0) {//按CTRL键多选
            desk.clearSelection();
        }
        oldC = (Component) desk.getJTPComponent(e.getPoint());
        if (oldC != null) {
            Point p = SwingUtilities.convertPoint( (Component) desk,
                                                  e.getPoint(), oldC);
            handleIndex = ( (JTPComponent) oldC).getHandle(p);
            if (handleIndex != -1) {
                desk.setCursor( ( (JTPComponent) oldC).getAdjustCursor(
                    handleIndex));
            }
            else {
                desk.setCursor(new Cursor(Cursor.HAND_CURSOR));
            }
            oldRec = oldC.getBounds();
        }
        else {
            desk.setCursor(Cursor.getDefaultCursor());
        }
        oldP = e.getPoint();
        desk.mouseSelect(e.getPoint());
        //右键弹出菜单
        if (SwingUtilities.isRightMouseButton(e)) {
            JTPComponent selection[] = desk.getSelectionJTPComponent();
            if (selection == null || selection.length == 0) {
                return;
            }
            JPopupMenu popup = desk.getJTPMenuFactory().getPopup(selection);
            if (popup != null) {
                popup.show( ( (Component) desk), e.getX(), e.getY());
            }
        }
    }
}

⌨️ 快捷键说明

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