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

📄 ishape.java

📁 基于MPEG 7 标准,符合未来语义网架构,很值得参考
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        }

        // draw connecting lines
        Point p1 = pStart;
        while (it.hasNext()) {
            IDot dot = (IDot) it.next();
            Point p2 = dot.getComponentPoint();
            g2.drawLine(p1.x, p1.y, p2.x, p2.y);
            p1 = p2;
        }

        // draw the last dot depending in the mode. Close shape or draw to mousepointer
        if (doBuildShape) {
            if (mousePos != null) {
                if (p1 != null) {
                    // draw a line from the last dot to the currend mouse position
                    g2.setPaint(backgroundColor);
                    g2.drawLine(p1.x, p1.y, mousePos.x, mousePos.y);
                }
                // draw a dot to indicate the user that something can be done
                int size = IDot.DEFAULT_DOT_SIZE;
                g2.setPaint(drawColor);
                g2.fill3DRect(mousePos.x - size / 2, mousePos.y - size / 2, size, size, true);
            }
        } else {
            // close shape
            if (p1 != null) {
                g2.drawLine(pStart.x, pStart.y, p1.x, p1.y);
            }

            if (!isTransparent) {
                // draw shapeColor
                if (shapeColor != null) {
                    Shape shape = java2dTools.pointListToShape(dotListToPointList());
                    g2.setPaint(shapeColor);
                    g2.fill(shape);
                }
            }

            // draw border;
            if (doDrawBorder) {
                g2.setPaint(Color.RED);
                g2.draw3DRect(boundary.x, boundary.y, boundary.width, boundary.height, true);
            }
        } // end if doBuildShape

        //super.paintComponent(g); // draw extra visual and so on
    } // end method paint


    // override the orginal one. the dots have to be set too
    public void setComponentBounds(int x, int y, int width, int heigh) {
        super.setComponentBounds(x, y, width, heigh);
        resizeShape();
    }

    public void setComponentBounds(Rectangle rect1) {
        super.setComponentBounds(rect1);
        resizeShape();
    }

    // override the orginal one. the dots have to be set too
    public void setComponentLocation(Point point1) {
        super.setComponentLocation(point1);
        resizeShape();
    }

    private Rectangle calculateBorder() {
        Point left = new Point(0, 0);
        Point right = new Point(0, 0);
        Point up = new Point(0, 0);
        Point down = new Point(0, 0);

        boolean isFirst = true;
        List pointList = new Vector();
        for (Iterator it = dotList.iterator(); it.hasNext();) {
            IDot dot = (IDot) it.next();
            Point p = dot.getComponentPoint();
            pointList.add(p);
        }

        int size = IDot.DEFAULT_DOT_SIZE;
        Rectangle returnValue;
        returnValue = (Rectangle) java2dTools.calculateBorder(pointList, size, new Rectangle(0, 0, 0, 0));

        return returnValue;
    }

    // fit the the dots to the border whithout changing the aspect ratio
    private void resizeShape() {
        Rectangle dotBorder = calculateBorder();
        Rectangle compBorder = this.getComponentBounds();

        Rectangle newBorder;
        newBorder = (Rectangle) java2dTools.fitToWindow(compBorder, dotBorder);

        cat.debug("dotBorder: " + dotBorder);
        cat.debug("compBorder: " + compBorder);
        cat.debug("newBorder: " + newBorder);

        double aspectRatio = newBorder.getWidth() / newBorder.getHeight();
        double scaleFactor = newBorder.width / dotBorder.getWidth();

        for (Iterator it = dotList.iterator(); it.hasNext();) {
            IDot dot = (IDot) it.next();
            Point p = dot.getComponentPoint();

            int offsetX = p.x - dotBorder.x;
            int offsetY = p.y - dotBorder.y;

            offsetX *= scaleFactor;
            offsetY *= (scaleFactor * aspectRatio);

            int newX = newBorder.x + offsetX;
            int newY = newBorder.y + offsetY;
            dot.setComponentLocation(new Point(newX, newY));

            //cat.debug("Border: " + newBorder.toString() + ", dot: " + newX + ", " + newY);

        } // end for

    }

    /**
     * add some special menu entries
     * *7
     */
    protected void setPopupMenuEntries() {
        super.setPopupMenuEntries();

        menuTools.setParentClass(this);
        //menuTools.addPopupMenuEntry("&Extract Metadata", "Use this image to extract data", "dominant_color.gif", "actionColorAndShape");
        menuTools.addPopupMenuEntry("&Toggle transparency", "Switch to fill the shape or not", "", "actionToggleTransparency");
    }


    //--------------------- Mouse Events ----------------------------------------
    // set a new dot
    public void mouseClicked(MouseEvent e) {
        //cat.debug("mouseClicked: " + e.getX() + ", " + e.getY());
        if (!doBuildShape) {
            super.mouseClicked(e);
        } else {

            if (SwingUtilities.isLeftMouseButton(e)) {
                IDot dot = new IDot(drawPanel, this, e.getX(), e.getY(),
                                drawColor, backgroundColor);

                //cat.debug("set a new dot at: " + e.getPoint());
                this.addNewDot(dot);
            } else if (SwingUtilities.isRightMouseButton(e)) {
                /*if (path.getCurrentPoint() != null) { // close path if there are at least one point available
                   path.closePath();*/
                endDoBuildShape();
            }
            repaint();
        } // end if doBuildShape


    } // end method

    /**
     * invoked when the shape is build ready.
     * use "null" if shape should be transparent
     */
    public void endDoBuildShape() {
        if (!doBuildShape) {
            return;
        }

        doBuildShape = false;
        setMoveable(true);
        setResizeable(true);
        boundary = calculateBorder();

        // set the shape color automatically
        if (image != null) {
            java.util.List pointList = dotListToPointList();

            Rectangle orginalBorder = imageSize;
            Rectangle newBorder = new Rectangle(0, 0, image.getWidth(), image.getHeight());
            java.util.List fitPointList = java2dTools.fitPointToOtherBorder(pointList, orginalBorder, newBorder);
            /*cat.debug("old: " + orginalBorder.toString() + ", new: " + newBorder.toString());
            cat.debug("old Points: " + pointList.toString());
            cat.debug("new Points: " + fitPointList.toString());*/

            Color color = java2dTools.getDominantColor(image, fitPointList);
            setShapeColor(color);
        }
    }

    // convert the IDot() list to a list of Point()
    private java.util.List dotListToPointList() {
        java.util.List pointList = new Vector(dotList.size());
        for (ListIterator dotIterator = dotList.listIterator(); dotIterator.hasNext();) {
            IDot dot = (IDot) dotIterator.next();
            pointList.add(dot.getComponentPoint());
        }
        return pointList;
    }

    public void mouseMoved(MouseEvent e) {
        //cat.debug("mouseDragged: " + e.getX() + ", " + e.getY());
        if (!doBuildShape) {
            super.mouseMoved(e);
        } else {
            mousePos = e.getPoint();
            repaint();
        }
    }

    public void mouseDragged(MouseEvent e) {
        //cat.debug("mouse dragged");
        Rectangle oldRect = new Rectangle(this.boundary);
        super.mouseDragged(e);

        //cat.debug("check rectangle: " + oldRect.x + ", " + this.boundary.x);
        // move the complete shape to a new positions
        if ((oldRect.x != this.boundary.x) || (oldRect.y != this.boundary.y)) {
            int offsetX = this.boundary.x - oldRect.x;
            int offsetY = this.boundary.y - oldRect.y;

            Iterator it = dotList.iterator();
            while (it.hasNext()) {
                IDot dot = (IDot) it.next();
                Rectangle oldDot = dot.getComponentBounds();
                dot.setComponentBounds(oldDot.x + offsetX, oldDot.y + offsetY, oldDot.width, oldDot.height);
            }

            //cat.debug("Border moved: " + offsetX + ", " + offsetY);
            this.repaint();
        }

    }

    public void actionToggleTransparency(ActionEvent e) {
        isTransparent = !isTransparent;
        repaint();
    }

    //--------------- Drop stuff from Dnd ------------------------------
    public void dragEnter(DropTargetDragEvent event) {
    }

    public void dragOver(DropTargetDragEvent event) {
    }

    public void dropActionChanged(DropTargetDragEvent event) {
    }

    public void dragExit(DropTargetEvent event) {
    }


    public void drop(DropTargetDropEvent dtde) {
        try {
            if (!acceptDnd) {
                dtde.rejectDrop();
                return;
            }

            //cat.debug("dnd dropped, included Flavors:");
            Transferable t = dtde.getTransferable();
            DataFlavor[] flavorList = dtde.getCurrentDataFlavors();

            IComponent component = null; //this object will receive the dropped data

            //------------- ACCEPT own IComponent Data ----------------------------
            if (t.isDataFlavorSupported(IComponentTransferable.localIComponentFlavor)) {
                //cat.info("IMAGE is from Objectpalette");
                dtde.acceptDrop(DnDConstants.ACTION_COPY);
                component = (IComponent) t.getTransferData(IComponentTransferable.localIComponentFlavor);
                dtde.getDropTargetContext().dropComplete(true);

                shapeColor = component.getDrawColor();
                isTransparent = false;
                repaint();
            } else {
                cat.debug("Flavor rejected");
                dtde.rejectDrop();
            }

            dtde.getDropTargetContext().dropComplete(true);
        } catch (Exception e) {
            cat.error(e);
            e.printStackTrace();
        }
    }


    public String toString() {
        String returnValue = "Size: " + boundary.toString();
        returnValue += "\nDots: " + dotList.size();
        return returnValue;
    }

    protected void finalize() throws Throwable {
        super.finalize();
    }

} // end class IRectangle

⌨️ 快捷键说明

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