jdk/test/java/awt/MultipleGradientPaint/MultiGradientTest.java
author mfang
Wed, 17 Aug 2011 14:18:26 -0700
changeset 10294 8fcdae2a7ec7
parent 5506 202f599c92aa
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6296064 6521533
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary A simple manual test for visual verification of GradientPaint
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * LinearGradientPaint, and RadialGradientPaint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @run main/manual MultiGradientTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @author campbelc
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.BorderLayout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.Color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.Dimension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.GradientPaint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.Graphics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.awt.Graphics2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.awt.LinearGradientPaint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.awt.MultipleGradientPaint.ColorSpaceType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.awt.MultipleGradientPaint.CycleMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.awt.Paint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.awt.Polygon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.awt.RadialGradientPaint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.awt.Rectangle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.awt.RenderingHints;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.awt.geom.AffineTransform;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.awt.geom.NoninvertibleTransformException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.awt.geom.Point2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.awt.event.ActionEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.awt.event.ActionListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.awt.event.MouseAdapter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import java.awt.event.MouseEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import java.awt.event.WindowAdapter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import java.awt.event.WindowEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import java.util.EnumSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import javax.swing.AbstractListModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
import javax.swing.BoxLayout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
import javax.swing.ComboBoxModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
import javax.swing.JCheckBox;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
import javax.swing.JComboBox;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
import javax.swing.JFrame;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
import javax.swing.JLabel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
import javax.swing.JPanel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
import javax.swing.JSpinner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
import javax.swing.SpinnerNumberModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
import javax.swing.event.ChangeEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
import javax.swing.event.ChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
public class MultiGradientTest extends JPanel {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private static final Color[] COLORS = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        new Color(0, 0, 0),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        new Color(128, 128, 128),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        new Color(255, 0, 0),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        new Color(255, 255, 0),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        new Color(0, 255, 0),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        new Color(0, 255, 255),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        new Color(128, 0, 255),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        new Color(128, 128, 128),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private static enum PaintType {BASIC, LINEAR, RADIAL};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private static enum ShapeType {RECT, ELLIPSE, MULTIPLE};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private static enum XformType {IDENTITY, TRANSLATE, SCALE, SHEAR, ROTATE};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private PaintType paintType = PaintType.LINEAR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private ShapeType shapeType = ShapeType.RECT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private XformType xformType = XformType.IDENTITY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private CycleMethod cycleMethod = CycleMethod.NO_CYCLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private ColorSpaceType colorSpace = ColorSpaceType.SRGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private Object antialiasHint = RenderingHints.VALUE_ANTIALIAS_OFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private Object renderHint = RenderingHints.VALUE_RENDER_SPEED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private AffineTransform transform = new AffineTransform();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private int numColors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private GradientPanel gradientPanel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private ControlsPanel controlsPanel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private MultiGradientTest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        numColors = COLORS.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        setLayout(new BorderLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        gradientPanel = new GradientPanel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        add(gradientPanel, BorderLayout.CENTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        controlsPanel = new ControlsPanel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        add(controlsPanel, BorderLayout.SOUTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private class GradientPanel extends JPanel {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        private int startX, startY, endX, endY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        private int ctrX, ctrY, focusX, focusY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        private float radius;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        private Paint paint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        private GradientPanel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            startX = 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            startY = 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            endX   = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            endY   = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            ctrX   = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            ctrY   = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            focusX = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            focusY = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            radius = 100.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            makeNewPaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            MouseAdapter l = new MyMouseAdapter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            addMouseListener(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            addMouseMotionListener(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        public void paintComponent(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            Graphics2D g2d = (Graphics2D)g.create();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            int w = getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            int h = getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            g2d.setColor(Color.black);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            g2d.fillRect(0, 0, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                                 antialiasHint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            g2d.setRenderingHint(RenderingHints.KEY_RENDERING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                                 renderHint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            g2d.transform(transform);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            g2d.setPaint(paint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            switch (shapeType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            case RECT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                g2d.fillRect(0, 0, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            case ELLIPSE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                g2d.fillOval(0, 0, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            case MULTIPLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                g2d.fillRect(0, 0, w/2, h/2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                g2d.fillOval(w/2, 0, w/2, h/2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                g2d.drawOval(0, h/2, w/2, h/2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                g2d.drawLine(0, h/2, w/2, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                g2d.drawLine(0, h, w/2, h/2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                Polygon p = new Polygon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                p.addPoint(w/2, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                p.addPoint(w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                p.addPoint(3*w/4, h/2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                g2d.fillPolygon(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            switch (paintType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            case BASIC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            case LINEAR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                g2d.setColor(Color.white);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                g2d.fillRect(startX-1, startY-1, 2, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                g2d.drawString("1", startX, startY + 12);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                g2d.fillRect(endX-1, endY-1, 2, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                g2d.drawString("2", endX, endY + 12);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            case RADIAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                g2d.setColor(Color.white);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                g2d.fillRect(ctrX-1, ctrY-1, 2, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                g2d.drawString("C", ctrX, ctrY + 12);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                g2d.fillRect(focusX-1, focusY-1, 2, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                g2d.drawString("F", focusX, focusY + 12);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            g2d.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        private void updatePoints(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            Point2D inv = new Point2D.Double(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                inv = transform.inverseTransform(inv, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            } catch (NoninvertibleTransformException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            x = (int)inv.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            y = (int)inv.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            switch (paintType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            case BASIC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            case LINEAR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                // pick the closest point to move
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                if (inv.distance(startX, startY) < inv.distance(endX, endY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    startX = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                    startY = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    endX = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    endY = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            case RADIAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                // pick the closest point to move
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                if (inv.distance(ctrX, ctrY) < inv.distance(focusX, focusY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                    ctrX = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                    ctrY = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                    focusX = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                    focusY = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            updatePaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        private void makeNewPaint() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            Color[] colors = Arrays.copyOf(COLORS, numColors);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            float[] fractions = new float[colors.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            for (int i = 0; i < fractions.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                fractions[i] = ((float)i) / (fractions.length-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            switch (paintType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            case BASIC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                boolean cyclic = (cycleMethod != CycleMethod.NO_CYCLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                paint = new GradientPaint(startX, startY, Color.RED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                                          endX, endY, Color.BLUE, cyclic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            case LINEAR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                paint =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    new LinearGradientPaint(new Point2D.Float(startX, startY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                                            new Point2D.Float(endX, endY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                                            fractions, colors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                                            cycleMethod, colorSpace,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                                            new AffineTransform());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            case RADIAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                paint =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    new RadialGradientPaint(new Point2D.Float(ctrX, ctrY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                                            radius,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                                            new Point2D.Float(focusX, focusY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                                            fractions, colors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                                            cycleMethod, colorSpace,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                                            new AffineTransform());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            switch (xformType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            case IDENTITY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                transform = new AffineTransform();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            case TRANSLATE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                transform = AffineTransform.getTranslateInstance(2, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            case SCALE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                transform = AffineTransform.getScaleInstance(1.2, 1.4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            case SHEAR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                transform = AffineTransform.getShearInstance(0.1, 0.1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            case ROTATE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                transform = AffineTransform.getRotateInstance(Math.PI / 4,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                                                              getWidth()/2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                                                              getHeight()/2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        public void updatePaint() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            makeNewPaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        private class MyMouseAdapter extends MouseAdapter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            public void mouseClicked(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                updatePoints(e.getX(), e.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            public void mouseDragged(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                updatePoints(e.getX(), e.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        public Dimension getPreferredSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            return new Dimension(400, 400);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    private class ControlsPanel extends JPanel implements ActionListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        private JComboBox cmbPaint, cmbCycle, cmbSpace, cmbShape, cmbXform;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        private JCheckBox cbAntialias, cbRender;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        private JSpinner spinNumColors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        private ControlsPanel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            cmbPaint = createCombo(this, paintType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            cmbPaint.setSelectedIndex(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            cmbCycle = createCombo(this, cycleMethod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            cmbSpace = createCombo(this, colorSpace);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            cmbShape = createCombo(this, shapeType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            cmbXform = createCombo(this, xformType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            int max = COLORS.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            SpinnerNumberModel model = new SpinnerNumberModel(max, 2, max, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            spinNumColors = new JSpinner(model);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            spinNumColors.addChangeListener(new ChangeListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                public void stateChanged(ChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    numColors = ((Integer)spinNumColors.getValue()).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    gradientPanel.updatePaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            add(spinNumColors);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            cbAntialias = createCheck(this, "Antialiasing");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            cbRender = createCheck(this, "Render Quality");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        private JComboBox createCombo(JPanel panel, Enum e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            JComboBox cmb = new JComboBox();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            cmb.setModel(new EnumComboBoxModel(e.getClass()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            cmb.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            panel.add(cmb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            return cmb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        private JCheckBox createCheck(JPanel panel, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            JCheckBox cb = new JCheckBox(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            cb.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            panel.add(cb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            return cb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            Object source = e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            if (source == cmbPaint) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                paintType = (PaintType)cmbPaint.getSelectedItem();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            } else if (source == cmbCycle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                cycleMethod = (CycleMethod)cmbCycle.getSelectedItem();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            } else if (source == cmbSpace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                colorSpace = (ColorSpaceType)cmbSpace.getSelectedItem();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            } else if (source == cmbShape) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                shapeType = (ShapeType)cmbShape.getSelectedItem();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            } else if (source == cmbXform) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                xformType = (XformType)cmbXform.getSelectedItem();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            } else if (source == cbAntialias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                antialiasHint = cbAntialias.isSelected() ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                    RenderingHints.VALUE_ANTIALIAS_ON :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                    RenderingHints.VALUE_ANTIALIAS_OFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            } else if (source == cbRender) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                renderHint = cbRender.isSelected() ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                    RenderingHints.VALUE_RENDER_QUALITY :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                    RenderingHints.VALUE_RENDER_SPEED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            gradientPanel.updatePaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    private static class EnumComboBoxModel<E extends Enum<E>>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        extends AbstractListModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        implements ComboBoxModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        private E selected = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        private List<E> list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        public EnumComboBoxModel(Class<E> en) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            EnumSet<E> ens = EnumSet.allOf(en);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            list = new ArrayList<E>(ens);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            selected = list.get(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        public int getSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            return list.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        public E getElementAt(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            return list.get(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        public void setSelectedItem(Object anItem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            selected = (E)anItem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            this.fireContentsChanged(this, 0, getSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        public E getSelectedItem() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            return selected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        final JFrame frame = new JFrame("Multistop Gradient Demo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        frame.addWindowListener(new WindowAdapter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            public void windowClosing(WindowEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                frame.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        frame.add(new MultiGradientTest());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        frame.pack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        frame.setLocationRelativeTo(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        frame.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
}