001: /*
002: * $Id: GradientPreviewPanel.java,v 1.7 2007/01/08 06:01:52 joshy Exp $
003: *
004: * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
005: * Santa Clara, California 95054, U.S.A. All rights reserved.
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
020: */
021:
022: package org.jdesktop.swingx.color;
023:
024: import java.awt.Color;
025: import java.awt.Graphics;
026: import java.awt.Graphics2D;
027: import java.awt.Paint;
028: import java.awt.RenderingHints;
029: import java.awt.event.MouseEvent;
030: import java.awt.geom.Point2D;
031: import java.util.List;
032: import javax.swing.event.MouseInputAdapter;
033: import org.apache.batik.ext.awt.LinearGradientPaint;
034: import org.apache.batik.ext.awt.MultipleGradientPaint;
035: import org.apache.batik.ext.awt.RadialGradientPaint;
036: import org.jdesktop.swingx.JXGradientChooser;
037: import org.jdesktop.swingx.JXPanel;
038: import org.jdesktop.swingx.multislider.Thumb;
039:
040: /**
041: *
042: * @author jm158417
043: */
044: public class GradientPreviewPanel extends JXPanel {
045: private Paint checker_texture = null;
046: private Point2D start, end;
047: public JXGradientChooser picker;
048: boolean moving_start = false;
049: boolean moving_end = false;
050:
051: public GradientPreviewPanel() {
052: start = new Point2D.Float(10, 10);
053: end = new Point2D.Float(80, 10);
054: checker_texture = ColorUtil.getCheckerPaint();
055: MouseInputAdapter ma = new GradientMouseHandler();
056: this .addMouseListener(ma);
057: this .addMouseMotionListener(ma);
058: }
059:
060: public void setGradient() {
061: repaint();
062: }
063:
064: public void setGradient(MultipleGradientPaint grad) {
065: if (grad instanceof LinearGradientPaint) {
066: LinearGradientPaint paint = (LinearGradientPaint) grad;
067: this .start = paint.getStartPoint();
068: this .end = paint.getEndPoint();
069: } else {
070: RadialGradientPaint paint = (RadialGradientPaint) grad;
071: this .start = paint.getCenterPoint();
072: this .end = new Point2D.Double(start.getX(), start.getY()
073: + paint.getRadius());
074: }
075: repaint();
076: }
077:
078: public MultipleGradientPaint getGradient() {
079: // calculate the color stops
080: List<Thumb<Color>> stops = picker.getSlider().getModel()
081: .getSortedThumbs();
082: int len = stops.size();
083:
084: // set up the data for the gradient
085: float[] fractions = new float[len];
086: Color[] colors = new Color[len];
087: int i = 0;
088: for (Thumb<Color> thumb : stops) {
089: colors[i] = (Color) thumb.getObject();
090: fractions[i] = thumb.getPosition();
091: i++;
092: }
093:
094: // get the final gradient
095: MultipleGradientPaint paint = calculateGradient(fractions,
096: colors);
097: return paint;
098: }
099:
100: protected void paintComponent(Graphics g) {
101: try {
102: Graphics2D g2 = (Graphics2D) g;
103:
104: // fill the background with checker first
105: g2.setPaint(checker_texture);
106: g.fillRect(0, 0, getWidth(), getHeight());
107:
108: Paint paint = getGradient();
109: // fill the area
110: if (paint != null) {
111: g2.setPaint(paint);
112: } else {
113: g2.setPaint(Color.black);
114: }
115:
116: g.fillRect(0, 0, getWidth(), getHeight());
117:
118: drawHandles(g2);
119: } catch (Exception ex) {
120: System.out.println("ex: " + ex);
121: }
122: }
123:
124: private MultipleGradientPaint calculateGradient(
125: final float[] fractions, final Color[] colors) {
126: // set up the end points
127: Point2D start = this .start;
128: Point2D end = this .end;
129: if (picker.reversedCheck.isSelected()) {
130: start = this .end;
131: end = this .start;
132: }
133:
134: // set up the cycle type
135: MultipleGradientPaint.CycleMethodEnum cycle = MultipleGradientPaint.NO_CYCLE;
136: if (picker.repeatedRadio.isSelected()) {
137: cycle = MultipleGradientPaint.REPEAT;
138: }
139: if (picker.reflectedRadio.isSelected()) {
140: cycle = MultipleGradientPaint.REFLECT;
141: }
142:
143: // create the underlying gradient paint
144: MultipleGradientPaint paint = null;
145: if (picker.styleCombo.getSelectedItem().toString().equals(
146: "Linear")) {
147: paint = new org.apache.batik.ext.awt.LinearGradientPaint(
148: (float) start.getX(), (float) start.getY(),
149: (float) end.getX(), (float) end.getY(), fractions,
150: colors, cycle);
151: }
152: if (picker.styleCombo.getSelectedItem().toString().equals(
153: "Radial")) {
154: paint = new org.apache.batik.ext.awt.RadialGradientPaint(
155: start, (float) start.distance(end), start,
156: fractions, colors, cycle,
157: MultipleGradientPaint.SRGB);
158: }
159: return paint;
160: }
161:
162: private void drawHandles(final Graphics2D g2) {
163: g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
164: RenderingHints.VALUE_ANTIALIAS_ON);
165: // draw the points and gradient line
166: g2.setColor(Color.black);
167: g2.drawOval((int) start.getX() - 5, (int) start.getY() - 5, 10,
168: 10);
169: g2.setColor(Color.white);
170: g2.drawOval((int) start.getX() - 4, (int) start.getY() - 4, 8,
171: 8);
172:
173: g2.setColor(Color.black);
174: g2.drawOval((int) end.getX() - 5, (int) end.getY() - 5, 10, 10);
175: g2.setColor(Color.white);
176: g2.drawOval((int) end.getX() - 4, (int) end.getY() - 4, 8, 8);
177:
178: g2.setColor(Color.darkGray);
179: g2.drawLine((int) start.getX(), (int) start.getY(), (int) end
180: .getX(), (int) end.getY());
181: g2.setColor(Color.gray);
182: g2.drawLine((int) start.getX() - 1, (int) start.getY() - 1,
183: (int) end.getX() - 1, (int) end.getY() - 1);
184: }
185:
186: private class GradientMouseHandler extends MouseInputAdapter {
187:
188: public void mousePressed(MouseEvent evt) {
189: moving_start = false;
190: moving_end = false;
191: if (evt.getPoint().distance(start) < 5) {
192: moving_start = true;
193: start = evt.getPoint();
194: return;
195: }
196:
197: if (evt.getPoint().distance(end) < 5) {
198: moving_end = true;
199: end = evt.getPoint();
200: return;
201: }
202:
203: start = evt.getPoint();
204: }
205:
206: public void mouseDragged(MouseEvent evt) {
207: if (moving_start) {
208: start = evt.getPoint();
209: } else {
210: end = evt.getPoint();
211: }
212: firePropertyChange("gradient", null, getGradient());
213: repaint();
214: }
215: }
216: }
|