File: MultiGradientTest.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (434 lines) | stat: -rw-r--r-- 14,822 bytes parent folder | download | duplicates (16)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
 * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code 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 General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 6296064 6521533
 * @summary A simple manual test for visual verification of GradientPaint
 * LinearGradientPaint, and RadialGradientPaint.
 * @run main/manual MultiGradientTest
 * @author campbelc
 */

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.LinearGradientPaint;
import java.awt.MultipleGradientPaint.ColorSpaceType;
import java.awt.MultipleGradientPaint.CycleMethod;
import java.awt.Paint;
import java.awt.Polygon;
import java.awt.RadialGradientPaint;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Point2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import javax.swing.AbstractListModel;
import javax.swing.BoxLayout;
import javax.swing.ComboBoxModel;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class MultiGradientTest extends JPanel {

    private static final Color[] COLORS = {
        new Color(0, 0, 0),
        new Color(128, 128, 128),
        new Color(255, 0, 0),
        new Color(255, 255, 0),
        new Color(0, 255, 0),
        new Color(0, 255, 255),
        new Color(128, 0, 255),
        new Color(128, 128, 128),
    };

    private static enum PaintType {BASIC, LINEAR, RADIAL};
    private static enum ShapeType {RECT, ELLIPSE, MULTIPLE};
    private static enum XformType {IDENTITY, TRANSLATE, SCALE, SHEAR, ROTATE};

    private PaintType paintType = PaintType.LINEAR;
    private ShapeType shapeType = ShapeType.RECT;
    private XformType xformType = XformType.IDENTITY;
    private CycleMethod cycleMethod = CycleMethod.NO_CYCLE;
    private ColorSpaceType colorSpace = ColorSpaceType.SRGB;
    private Object antialiasHint = RenderingHints.VALUE_ANTIALIAS_OFF;
    private Object renderHint = RenderingHints.VALUE_RENDER_SPEED;
    private AffineTransform transform = new AffineTransform();

    private int numColors;

    private GradientPanel gradientPanel;
    private ControlsPanel controlsPanel;

    private MultiGradientTest() {
        numColors = COLORS.length;

        setLayout(new BorderLayout());
        gradientPanel = new GradientPanel();
        add(gradientPanel, BorderLayout.CENTER);
        controlsPanel = new ControlsPanel();
        add(controlsPanel, BorderLayout.SOUTH);
    }

    private class GradientPanel extends JPanel {
        private int startX, startY, endX, endY;
        private int ctrX, ctrY, focusX, focusY;
        private float radius;
        private Paint paint;

        private GradientPanel() {
            startX = 20;
            startY = 20;
            endX   = 100;
            endY   = 100;
            ctrX   = 100;
            ctrY   = 100;
            focusX = 100;
            focusY = 100;
            radius = 100.0f;

            makeNewPaint();

            MouseAdapter l = new MyMouseAdapter();
            addMouseListener(l);
            addMouseMotionListener(l);
        }

        public void paintComponent(Graphics g) {
            Graphics2D g2d = (Graphics2D)g.create();

            int w = getWidth();
            int h = getHeight();
            g2d.setColor(Color.black);
            g2d.fillRect(0, 0, w, h);

            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                 antialiasHint);
            g2d.setRenderingHint(RenderingHints.KEY_RENDERING,
                                 renderHint);

            g2d.transform(transform);
            g2d.setPaint(paint);

            switch (shapeType) {
            default:
            case RECT:
                g2d.fillRect(0, 0, w, h);
                break;

            case ELLIPSE:
                g2d.fillOval(0, 0, w, h);
                break;

            case MULTIPLE:
                g2d.fillRect(0, 0, w/2, h/2);
                g2d.fillOval(w/2, 0, w/2, h/2);
                g2d.drawOval(0, h/2, w/2, h/2);
                g2d.drawLine(0, h/2, w/2, h);
                g2d.drawLine(0, h, w/2, h/2);
                Polygon p = new Polygon();
                p.addPoint(w/2, h);
                p.addPoint(w, h);
                p.addPoint(3*w/4, h/2);
                g2d.fillPolygon(p);
                break;
            }

            switch (paintType) {
            default:
            case BASIC:
            case LINEAR:
                g2d.setColor(Color.white);
                g2d.fillRect(startX-1, startY-1, 2, 2);
                g2d.drawString("1", startX, startY + 12);
                g2d.fillRect(endX-1, endY-1, 2, 2);
                g2d.drawString("2", endX, endY + 12);
                break;

            case RADIAL:
                g2d.setColor(Color.white);
                g2d.fillRect(ctrX-1, ctrY-1, 2, 2);
                g2d.drawString("C", ctrX, ctrY + 12);
                g2d.fillRect(focusX-1, focusY-1, 2, 2);
                g2d.drawString("F", focusX, focusY + 12);
                break;
            }

            g2d.dispose();
        }

        private void updatePoints(int x, int y) {
            Point2D inv = new Point2D.Double(x, y);

            try {
                inv = transform.inverseTransform(inv, null);
            } catch (NoninvertibleTransformException e) {
                e.printStackTrace();
            }

            x = (int)inv.getX();
            y = (int)inv.getY();

            switch (paintType) {
            default:
            case BASIC:
            case LINEAR:
                // pick the closest point to move
                if (inv.distance(startX, startY) < inv.distance(endX, endY)) {
                    startX = x;
                    startY = y;
                } else {
                    endX = x;
                    endY = y;
                }
                break;

            case RADIAL:
                // pick the closest point to move
                if (inv.distance(ctrX, ctrY) < inv.distance(focusX, focusY)) {
                    ctrX = x;
                    ctrY = y;
                } else {
                    focusX = x;
                    focusY = y;
                }
                break;
            }

            updatePaint();
        }

        private void makeNewPaint() {
            Color[] colors = Arrays.copyOf(COLORS, numColors);
            float[] fractions = new float[colors.length];
            for (int i = 0; i < fractions.length; i++) {
                fractions[i] = ((float)i) / (fractions.length-1);
            }

            switch (paintType) {
            case BASIC:
                boolean cyclic = (cycleMethod != CycleMethod.NO_CYCLE);
                paint = new GradientPaint(startX, startY, Color.RED,
                                          endX, endY, Color.BLUE, cyclic);
                break;

            default:
            case LINEAR:
                paint =
                    new LinearGradientPaint(new Point2D.Float(startX, startY),
                                            new Point2D.Float(endX, endY),
                                            fractions, colors,
                                            cycleMethod, colorSpace,
                                            new AffineTransform());
                break;

            case RADIAL:
                paint =
                    new RadialGradientPaint(new Point2D.Float(ctrX, ctrY),
                                            radius,
                                            new Point2D.Float(focusX, focusY),
                                            fractions, colors,
                                            cycleMethod, colorSpace,
                                            new AffineTransform());
                break;
            }

            switch (xformType) {
            default:
            case IDENTITY:
                transform = new AffineTransform();
                break;
            case TRANSLATE:
                transform = AffineTransform.getTranslateInstance(2, 2);
                break;
            case SCALE:
                transform = AffineTransform.getScaleInstance(1.2, 1.4);
                break;
            case SHEAR:
                transform = AffineTransform.getShearInstance(0.1, 0.1);
                break;
            case ROTATE:
                transform = AffineTransform.getRotateInstance(Math.PI / 4,
                                                              getWidth()/2,
                                                              getHeight()/2);
                break;
            }
        }

        public void updatePaint() {
            makeNewPaint();
            repaint();
        }

        private class MyMouseAdapter extends MouseAdapter {
            @Override
            public void mouseClicked(MouseEvent e) {
                updatePoints(e.getX(), e.getY());
            }

            @Override
            public void mouseDragged(MouseEvent e) {
                updatePoints(e.getX(), e.getY());
            }
        }

        public Dimension getPreferredSize() {
            return new Dimension(400, 400);
        }
    }

    private class ControlsPanel extends JPanel implements ActionListener {
        private JComboBox cmbPaint, cmbCycle, cmbSpace, cmbShape, cmbXform;
        private JCheckBox cbAntialias, cbRender;
        private JSpinner spinNumColors;

        private ControlsPanel() {
            cmbPaint = createCombo(this, paintType);
            cmbPaint.setSelectedIndex(1);
            cmbCycle = createCombo(this, cycleMethod);
            cmbSpace = createCombo(this, colorSpace);
            cmbShape = createCombo(this, shapeType);
            cmbXform = createCombo(this, xformType);

            int max = COLORS.length;
            SpinnerNumberModel model = new SpinnerNumberModel(max, 2, max, 1);
            spinNumColors = new JSpinner(model);
            spinNumColors.addChangeListener(new ChangeListener() {
                public void stateChanged(ChangeEvent e) {
                    numColors = ((Integer)spinNumColors.getValue()).intValue();
                    gradientPanel.updatePaint();
                }
            });
            add(spinNumColors);

            cbAntialias = createCheck(this, "Antialiasing");
            cbRender = createCheck(this, "Render Quality");
        }

        private JComboBox createCombo(JPanel panel, Enum e) {
            JComboBox cmb = new JComboBox();
            cmb.setModel(new EnumComboBoxModel(e.getClass()));
            cmb.addActionListener(this);
            panel.add(cmb);
            return cmb;
        }

        private JCheckBox createCheck(JPanel panel, String name) {
            JCheckBox cb = new JCheckBox(name);
            cb.addActionListener(this);
            panel.add(cb);
            return cb;
        }

        public void actionPerformed(ActionEvent e) {
            Object source = e.getSource();

            if (source == cmbPaint) {
                paintType = (PaintType)cmbPaint.getSelectedItem();
            } else if (source == cmbCycle) {
                cycleMethod = (CycleMethod)cmbCycle.getSelectedItem();
            } else if (source == cmbSpace) {
                colorSpace = (ColorSpaceType)cmbSpace.getSelectedItem();
            } else if (source == cmbShape) {
                shapeType = (ShapeType)cmbShape.getSelectedItem();
            } else if (source == cmbXform) {
                xformType = (XformType)cmbXform.getSelectedItem();
            } else if (source == cbAntialias) {
                antialiasHint = cbAntialias.isSelected() ?
                    RenderingHints.VALUE_ANTIALIAS_ON :
                    RenderingHints.VALUE_ANTIALIAS_OFF;
            } else if (source == cbRender) {
                renderHint = cbRender.isSelected() ?
                    RenderingHints.VALUE_RENDER_QUALITY :
                    RenderingHints.VALUE_RENDER_SPEED;
            }

            gradientPanel.updatePaint();
        }
    }

    private static class EnumComboBoxModel<E extends Enum<E>>
        extends AbstractListModel
        implements ComboBoxModel
    {
        private E selected = null;
        private List<E> list;

        public EnumComboBoxModel(Class<E> en) {
            EnumSet<E> ens = EnumSet.allOf(en);
            list = new ArrayList<E>(ens);
            selected = list.get(0);
        }

        public int getSize() {
            return list.size();
        }

        public E getElementAt(int index) {
            return list.get(index);
        }

        public void setSelectedItem(Object anItem) {
            selected = (E)anItem;
            this.fireContentsChanged(this, 0, getSize());
        }

        public E getSelectedItem() {
            return selected;
        }
    }

    public static void main(String[] args) {
        final JFrame frame = new JFrame("Multistop Gradient Demo");
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                frame.dispose();
            }
        });
        frame.add(new MultiGradientTest());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}