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

📄 svgvisualresourcegradientcolorchooserchildwidget.java

📁 完全基于java开发的svg矢量绘图工具
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/* * Created on 26 août 2004 *  ============================================= 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.visualresources;import java.util.*;import javax.swing.*;import javax.swing.border.*;import javax.swing.event.*;import java.awt.*;import java.awt.geom.*;import java.awt.event.*;import fr.itris.glips.svgeditor.*;import javax.swing.plaf.metal.*;import org.w3c.dom.*;import fr.itris.glips.svgeditor.canvas.*;/** * the class of the widget that will be displayed in the properties dialog, and enabling to modify the color values * of the children of a gradient resource node *  * @author Jordi SUC, Maciej Wojtkiewicz */public class SVGVisualResourceGradientColorChooserChildWidget extends SVGVisualResourceChildWidget{		/**	 * the constructor of the class	 * @param resourceObject the resource object whose children will be modified by this widget	 */    public SVGVisualResourceGradientColorChooserChildWidget(SVGVisualResourceObject resourceObject){                super(resourceObject);        this.resourceObject=resourceObject;        buildComponent();    }        /**     * builds the component to be displayed     */    protected void buildComponent(){                if(resourceObject!=null && resourceObject.getChildren().getFirst()!=null){            final SVGVisualResourceObject fresourceObject=resourceObject;            final SVGEditor svgEditor=resourceObject.getResourceModel().getVisualResources().getSVGEditor();            SVGVisualResourceObjectChild childObject=(SVGVisualResourceObjectChild)resourceObject.getChildren().getFirst();                        //the labels            String widgetLabel=childObject.getChildModel().getAbsoluteName();            String 	stopProperties="", stopColor="", stopOpacity="", offset="", labelAddStop="", labelRemoveStop="";            if(bundle!=null){                                try{                    widgetLabel=bundle.getString(widgetLabel);                    stopProperties=bundle.getString("vresource_stopproperties");                    stopColor=bundle.getString("vresource_stop-color");                    stopOpacity=bundle.getString("vresource_stop-opacity");                    offset=bundle.getString("vresource_offset");                    labelAddStop=bundle.getString("vresource_addstop");                    labelRemoveStop=bundle.getString("vresource_removestop");                }catch (Exception ex){}            }                        this.label=widgetLabel;                        //the gradient items list            final LinkedList gradientItems=new LinkedList();                        //the width of a gradient item            final int itemWidth=12;                        //the panel containing all the elements            JPanel content=new JPanel();                        //the panel containing the arrows            final JPanel arrowsPanel=new JPanel(){                            	@Override                protected void paintComponent(Graphics g) {                                        super.paintComponent(g);                                        //paints all the representations of the items                    SVGGradientChooserItem item=null;                                        for(Iterator it=gradientItems.iterator(); it.hasNext();){                                                try{                            item=(SVGGradientChooserItem)it.next();                        }catch (Exception ex){item=null;}                                                if(item!=null){                                                        item.paintArrow((Graphics2D)g);                        }                    }                }            };                        //the dimension of the arrows panel            final Dimension arrowsPanelSize=new Dimension(200+itemWidth, 20);            arrowsPanel.setPreferredSize(arrowsPanelSize);                        //a comparator to sort the gradient items list            final Comparator comparator=new Comparator(){                                public int compare(Object arg0, Object arg1){                                        if(		arg0!=null && arg1!=null &&                            arg0 instanceof SVGGradientChooserItem && arg1 instanceof SVGGradientChooserItem){                                                SVGGradientChooserItem 	item0=(SVGGradientChooserItem)arg0,                         											item1=(SVGGradientChooserItem)arg1;                                                return item0.getXPosition()-item1.getXPosition();                    }                                        return 0;                }             };                        //the dimension of the arrows panel            final Dimension gradientPanelSize=new Dimension(200+itemWidth, 42);            //the panel containing the gradients            final JPanel gradientPanel=new JPanel(){                            	@Override                protected void paintComponent(Graphics g) {                                        super.paintComponent(g);                                        int 	sq=8,                     		lx=(gradientPanelSize.width-itemWidth)/sq,                     		ly=gradientPanelSize.height/sq;                    //drawing the background that symbolizes transparency                    for(int i=0; i<=lx; i++){                                                for(int j=0; j<=ly; j++){                                                        g.setColor((i+j)%2==0?Color.gray:Color.darkGray);                            g.fillRect(i*sq+itemWidth/2, j*sq, ((i*sq+itemWidth/2)>(gradientPanelSize.width-itemWidth)?sq-(i*sq+itemWidth/2):sq), sq);                        }                    }                    //sorts the list of the gradient items                    Collections.sort(gradientItems, comparator);                                        //paints the gradients represented by each item                    SVGGradientChooserItem item=null, previousItem=null;                                        for(Iterator it=gradientItems.iterator(); it.hasNext();){                                                try{                            item=(SVGGradientChooserItem)it.next();                        }catch (Exception ex){item=null;}                                                if(item!=null){                                                        //paints the gradient for the item                            item.paintGradient((Graphics2D)g, previousItem, it.hasNext());                            previousItem=item;                        }                    }                                        //painting the border                    g.setColor(Color.black);                    g.drawRect(itemWidth/2, 0, gradientPanelSize.width-itemWidth, gradientPanelSize.height-1);                }            };                        gradientPanel.setPreferredSize(gradientPanelSize);                        //the labels for each widget            final JLabel 	stopColorLbl=new JLabel(stopColor.concat(" :")),            					stopOpacityLbl=new JLabel(stopOpacity.concat(" :")),            					offsetLbl=new JLabel(offset.concat(" :"));                        stopColorLbl.setFont(theFont);            stopOpacityLbl.setFont(theFont);            offsetLbl.setFont(theFont);                        //the panel containing the add and remove button            JPanel buttonPanel=new JPanel();                        //the icons            final ImageIcon 	addIcon=svgEditor.getResource().getIcon("New", false),            							deleteIcon=svgEditor.getResource().getIcon("Delete", false),            							deleteDisabledIcon=svgEditor.getResource().getIcon("Delete", true);            //the add and remove buttons            final JButton addBt=new JButton(addIcon), deleteBt=new JButton(deleteIcon);            //dealing with the buttons' properties            addBt.setFont(smallFont);            deleteBt.setFont(smallFont);                        addBt.setToolTipText(labelAddStop);            deleteBt.setToolTipText(labelRemoveStop);                        addBt.setMargin(new Insets(1,1,1,1));            deleteBt.setMargin(new Insets(1,1,1,1));                        addBt.setPreferredSize(new Dimension(20, 20));            deleteBt.setPreferredSize(new Dimension(20, 20));                        deleteBt.setEnabled(false);                        //adds the buttons to the button panel            buttonPanel.setLayout(new GridLayout(2, 1, 0, 1));            buttonPanel.add(addBt);            buttonPanel.add(deleteBt);                        //the label displaying the position en percentage            final JTextField positionValueTxt=new JTextField("", 4);            positionValueTxt.setFont(theFont);            positionValueTxt.setAlignmentX(JTextField.RIGHT_ALIGNMENT);            positionValueTxt.setEditable(false);                        //the color button and its properties            final JButton colorBt=new JButton();            colorBt.setMargin(new Insets(1,1,1,1));            colorBt.setPreferredSize(new Dimension(20, 15));            colorBt.setEnabled(false);                        //the color panel that will be inserted in the color button            final JPanel colorPanel=new JPanel();                        //the color for the disabled state of the color button            final Color disabledColorPanelColor=new Color(200, 200, 200);            colorPanel.setBackground(disabledColorPanelColor);            colorBt.add(colorPanel);            //the slider for choosing the opacity//                        //the panel that will contain the widgets            final JPanel displayAndSlider=new JPanel();                        //the initial value            int val=100;            final JSlider slider=new JSlider(0, 100, val);                        final JLabel displayedValue=new JLabel(format.format(val)+" %");            displayedValue.setPreferredSize(new Dimension(30, 20));            slider.setPreferredSize(new Dimension(130, 25));            displayedValue.setFont(theFont);            //creates the panel that will be displayed            displayAndSlider.setLayout(new BoxLayout(displayAndSlider, BoxLayout.X_AXIS));            displayAndSlider.add(slider);            displayAndSlider.add(displayedValue);                        //the listener managing the arrows on the arrows panel            final SVGGradientChooserArrowsListener arrowsListener=new SVGGradientChooserArrowsListener(){                                /**                 * tells whether the gradient items have been modified                 */                private boolean isModified=false;                                /**                 * tells if the current arrow can be dragged or not                 */                private boolean canDrag=false;                                /**                 * the currently selected item                 */                protected SVGGradientChooserItem currentItem=null;                                /**                 * @return the currently selected item                 */                public SVGGradientChooserItem getCurrentItem(){                                        return currentItem;                }                                public void setCurrentItem(SVGGradientChooserItem item){                                        currentItem=item;                                        //sets if the delete button should be enabled or not                    if(item!=null){                        deleteBt.setEnabled(true);                        deleteBt.setIcon(deleteIcon);                        colorBt.setEnabled(true);                        slider.setEnabled(true);                                                if(item.getColor()!=null){                            selectGradientChooserItem(item);                            colorPanel.setBackground(item.getColor());                            positionValueTxt.setText(item.getXPositionPercent()+"%");                            displayedValue.setText((int)(item.getOpacity()*100)+"%");                            slider.setValue((int)(item.getOpacity()*100));                        }                                            }else{                                                deleteBt.setEnabled(false);                        deleteBt.setIcon(deleteDisabledIcon);                        colorBt.setEnabled(false);                        colorPanel.setBackground(disabledColorPanelColor);                        displayedValue.setText("0%");                        slider.setEnabled(false);                        positionValueTxt.setText("");                    }                                        refresh();                }                                /**                 * tells whether the gradient items have been modified                 */                public boolean isModified(){                                        return isModified;                }                                /**                 * the method used to refresh the arrows                 */                public void refresh(){                                        arrowsPanel.repaint();                    gradientPanel.repaint();                }                                /**                 * creates and adds a gradient item to the list                 * @param resChild a resource child                 */                public void createGradientItem(SVGVisualResourceObjectChild resChild){                                        if(resChild!=null){                                                SVGGradientChooserItem item=new SVGGradientChooserItem(resChild, arrowsPanelSize, gradientPanelSize);                                                if(item!=null){                                                        gradientItems.add(item);                            setCurrentItem(item);                                                        synchronized(this){isModified=true;}                        }                    }                }                                /**                 * removes a gradient item from the list                 */                public void removeCurrentGradientItem(){                                        if(currentItem!=null){                                                SVGVisualResourceObjectChild resChild=currentItem.getResourceObjectChild();

⌨️ 快捷键说明

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