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

📄 svgcolorchooser.java

📁 完全基于java开发的svg矢量绘图工具
💻 JAVA
字号:
/* * Created on 22 févr. 2005 * ============================================= GNU LESSER GENERAL PUBLIC LICENSE Version 2.1 ============================================= GLIPS Graffiti Editor, a SVG Editor Copyright (C) 2003 Jordi SUC, Philippe Gil, SARL ITRIS  This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.  You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  Contact : jordi.suc@itris.fr; philippe.gil@itris.fr  ============================================= */package fr.itris.glips.svgeditor.colorchooser;import javax.swing.*;import fr.itris.glips.svgeditor.*;import java.awt.*;import fr.itris.glips.svgeditor.canvas.*;import java.awt.datatransfer.*;import java.awt.event.*;import java.io.*;import java.util.*;import org.w3c.dom.*;/** * the class of the color chooser for this editor *  * @author Jordi SUC */public class SVGColorChooser extends JColorChooser{        /**     * the editor     */    protected SVGEditor editor=null;        /**     * the flavor of a color     */    private DataFlavor colorFlavor=null;        /**     * the flavor of a svg w3c color     */    private DataFlavor w3cSVGColorFlavor=null;        /**     * the constructor of the class     * @param editor the editor     */    public SVGColorChooser(SVGEditor editor){                this.editor=editor;                //adds the w3c standard colors chooser panel        SVGW3CColorChooserPanel w3cColorChooserPanel=new SVGW3CColorChooserPanel(editor);        addChooserPanel(w3cColorChooserPanel);                //creating the color flavors        try{            colorFlavor=new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType+";class=java.awt.Color");            w3cSVGColorFlavor=new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType+";class=fr.itris.glips.svgeditor.colorchooser.SVGW3CColor");        }catch (Exception ex){colorFlavor=DataFlavor.stringFlavor; w3cSVGColorFlavor=DataFlavor.stringFlavor;}    }        /**     * shows a color chooser dialog     * @param initialColor the initial Color set when the color-chooser is shown     * @return the selected color or <code>null</code> if the user opted out     */    public Color showColorChooserDialog(Color initialColor) {    	setColor(initialColor);        SVGColorTracker ok = new SVGColorTracker(this);        JDialog dialog=createDialog(SVGEditor.getParent(), "", true, this, ok, null);        dialog.setVisible(true);        return ok.getColor();    }        /**     * Returns the color corresponding to the given string     * @param frame a svg frame     * @param colorString a string representing a color     * @return the color corresponding to the given string     */    public Color getColor(SVGFrame frame, String colorString){                Color color=null;                if(colorString==null){                        colorString="";                    }else if(editor.getResource()!=null){                        //checking if the given string represents a w3c color            color=frame.getSVGEditor().getResource().getW3CColorsMap().get(colorString);        }                if(color==null){                        try{color=Color.getColor(colorString);}catch (Exception ex){color=null;}                        if(color==null && colorString.length()==7){                                int r=0, g=0, b=0;                                try{                    r=Integer.decode("#"+colorString.substring(1,3)).intValue();                    g=Integer.decode("#"+colorString.substring(3,5)).intValue();                    b=Integer.decode("#"+colorString.substring(5,7)).intValue();                                        color=new Color(r,g,b);                }catch (Exception ex){color=null;}                            }else if(color==null && colorString.indexOf("rgb(")!=-1){                                String tmp=colorString.replaceAll("\\s*[rgb(]\\s*", "");                                tmp=tmp.replaceAll("\\s*[)]\\s*", "");                tmp=tmp.replaceAll("\\s+", ",");                tmp=tmp.replaceAll("[,]+", ",");                                int r=0, g=0, b=0;                                try{                    r=new Integer(tmp.substring(0, tmp.indexOf(","))).intValue();                    tmp=tmp.substring(tmp.indexOf(",")+1, tmp.length());                                        g=new Integer(tmp.substring(0, tmp.indexOf(","))).intValue();                    tmp=tmp.substring(tmp.indexOf(",")+1, tmp.length());                                        b=new Integer(tmp).intValue();                                        color=new Color(r,g,b);                }catch (Exception ex){color=null;}            }        }                return color;    }        /**     * Returns the string representation of the given color     * @param color a color     * @return the string representation of the given color     */    public String getColorString(Color color){                if(color==null){                        color=Color.black;        }                if(color instanceof SVGW3CColor){                        return ((SVGW3CColor)color).getId();        }                String	sr=Integer.toHexString(color.getRed()),        sg=Integer.toHexString(color.getGreen()),        sb=Integer.toHexString(color.getBlue());                if(sr.length()==1){                        sr="0".concat(sr);        }                if(sg.length()==1){                        sg="0".concat(sg);        }                if(sb.length()==1){                        sb="0".concat(sb);        }                return (("#".concat(sr)).concat(sg)).concat(sb);    }        /**     * checks each color value in each attribute and in the style property of the given element     * @param frame a frame     * @param element an element     */    @SuppressWarnings(value="all")    public void checkColorString(SVGFrame frame, Element element){            }        /**     * returns the data flavor of the given color     * @param color a color     * @return the data flavor of the given color     */    public DataFlavor getColorFlavor(Color color){                DataFlavor flavor=null;                if(color!=null){                        if(color instanceof SVGW3CColor){                                flavor=w3cSVGColorFlavor;                            }else{            	                flavor=colorFlavor;            }        }                return flavor;    }        /**     * returns whether the given flavor is a color flavor     * @param flavor a flavor     * @return whether the given flavor is a color flavor     */    public boolean isColorDataFlavor(DataFlavor flavor){                boolean isColorDataFlavor=false;                if(flavor!=null){                        isColorDataFlavor=(flavor.isMimeTypeEqual(colorFlavor) || flavor.isMimeTypeEqual(w3cSVGColorFlavor));        }                return isColorDataFlavor;    }        /**     * @return the list of the data flavors     */    public Collection<DataFlavor> getColorDataFlavors(){                LinkedList<DataFlavor> dataFlavors=new LinkedList<DataFlavor>();                dataFlavors.add(colorFlavor);        dataFlavors.add(w3cSVGColorFlavor);                return dataFlavors;    }        /**     * disposes the colors and blinking of the canvas linked with the given project file, if no     * other canvas is linked to this project     * @param projectFile a project file     */    @SuppressWarnings(value="all")    public void disposeColorsAndBlinkings(File projectFile) { }        /**     * the project file corresponding to the given uri     * @param uri a uri     * @return the project file corresponding to the given uri     */    @SuppressWarnings(value="all")    public File getProjectFile(String uri) {    	    	return null;    }        /**     * @return Returns the editor.     */    public SVGEditor getSVGEditor() {        return editor;    }        /**     * the class of the svg color tracker     * @author Jordi SUC     */    protected class SVGColorTracker implements ActionListener {    	    	/**    	 * the color chooser    	 */       private JColorChooser chooser;               /**        * the color        */       private Color color;       /**        * the constructor of the class        * @param c the color chooser        */        public SVGColorTracker(JColorChooser c) {        	            chooser=c;        }        public void actionPerformed(ActionEvent e) {            color=chooser.getColor();        }        /**         * @return the color         */        public Color getColor() {            return color;        }    }}

⌨️ 快捷键说明

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