Path: blob/master/test/jdk/java/awt/MultipleGradientPaint/MultiGradientTest.java
41149 views
/*1* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 6296064 652153326* @summary A simple manual test for visual verification of GradientPaint27* LinearGradientPaint, and RadialGradientPaint.28* @run main/manual MultiGradientTest29* @author campbelc30*/3132import java.awt.BorderLayout;33import java.awt.Color;34import java.awt.Dimension;35import java.awt.GradientPaint;36import java.awt.Graphics;37import java.awt.Graphics2D;38import java.awt.LinearGradientPaint;39import java.awt.MultipleGradientPaint.ColorSpaceType;40import java.awt.MultipleGradientPaint.CycleMethod;41import java.awt.Paint;42import java.awt.Polygon;43import java.awt.RadialGradientPaint;44import java.awt.Rectangle;45import java.awt.RenderingHints;46import java.awt.geom.AffineTransform;47import java.awt.geom.NoninvertibleTransformException;48import java.awt.geom.Point2D;49import java.awt.event.ActionEvent;50import java.awt.event.ActionListener;51import java.awt.event.MouseAdapter;52import java.awt.event.MouseEvent;53import java.awt.event.WindowAdapter;54import java.awt.event.WindowEvent;55import java.util.Arrays;56import java.util.ArrayList;57import java.util.EnumSet;58import java.util.List;59import javax.swing.AbstractListModel;60import javax.swing.BoxLayout;61import javax.swing.ComboBoxModel;62import javax.swing.JCheckBox;63import javax.swing.JComboBox;64import javax.swing.JFrame;65import javax.swing.JLabel;66import javax.swing.JPanel;67import javax.swing.JSpinner;68import javax.swing.SpinnerNumberModel;69import javax.swing.event.ChangeEvent;70import javax.swing.event.ChangeListener;7172public class MultiGradientTest extends JPanel {7374private static final Color[] COLORS = {75new Color(0, 0, 0),76new Color(128, 128, 128),77new Color(255, 0, 0),78new Color(255, 255, 0),79new Color(0, 255, 0),80new Color(0, 255, 255),81new Color(128, 0, 255),82new Color(128, 128, 128),83};8485private static enum PaintType {BASIC, LINEAR, RADIAL};86private static enum ShapeType {RECT, ELLIPSE, MULTIPLE};87private static enum XformType {IDENTITY, TRANSLATE, SCALE, SHEAR, ROTATE};8889private PaintType paintType = PaintType.LINEAR;90private ShapeType shapeType = ShapeType.RECT;91private XformType xformType = XformType.IDENTITY;92private CycleMethod cycleMethod = CycleMethod.NO_CYCLE;93private ColorSpaceType colorSpace = ColorSpaceType.SRGB;94private Object antialiasHint = RenderingHints.VALUE_ANTIALIAS_OFF;95private Object renderHint = RenderingHints.VALUE_RENDER_SPEED;96private AffineTransform transform = new AffineTransform();9798private int numColors;99100private GradientPanel gradientPanel;101private ControlsPanel controlsPanel;102103private MultiGradientTest() {104numColors = COLORS.length;105106setLayout(new BorderLayout());107gradientPanel = new GradientPanel();108add(gradientPanel, BorderLayout.CENTER);109controlsPanel = new ControlsPanel();110add(controlsPanel, BorderLayout.SOUTH);111}112113private class GradientPanel extends JPanel {114private int startX, startY, endX, endY;115private int ctrX, ctrY, focusX, focusY;116private float radius;117private Paint paint;118119private GradientPanel() {120startX = 20;121startY = 20;122endX = 100;123endY = 100;124ctrX = 100;125ctrY = 100;126focusX = 100;127focusY = 100;128radius = 100.0f;129130makeNewPaint();131132MouseAdapter l = new MyMouseAdapter();133addMouseListener(l);134addMouseMotionListener(l);135}136137public void paintComponent(Graphics g) {138Graphics2D g2d = (Graphics2D)g.create();139140int w = getWidth();141int h = getHeight();142g2d.setColor(Color.black);143g2d.fillRect(0, 0, w, h);144145g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,146antialiasHint);147g2d.setRenderingHint(RenderingHints.KEY_RENDERING,148renderHint);149150g2d.transform(transform);151g2d.setPaint(paint);152153switch (shapeType) {154default:155case RECT:156g2d.fillRect(0, 0, w, h);157break;158159case ELLIPSE:160g2d.fillOval(0, 0, w, h);161break;162163case MULTIPLE:164g2d.fillRect(0, 0, w/2, h/2);165g2d.fillOval(w/2, 0, w/2, h/2);166g2d.drawOval(0, h/2, w/2, h/2);167g2d.drawLine(0, h/2, w/2, h);168g2d.drawLine(0, h, w/2, h/2);169Polygon p = new Polygon();170p.addPoint(w/2, h);171p.addPoint(w, h);172p.addPoint(3*w/4, h/2);173g2d.fillPolygon(p);174break;175}176177switch (paintType) {178default:179case BASIC:180case LINEAR:181g2d.setColor(Color.white);182g2d.fillRect(startX-1, startY-1, 2, 2);183g2d.drawString("1", startX, startY + 12);184g2d.fillRect(endX-1, endY-1, 2, 2);185g2d.drawString("2", endX, endY + 12);186break;187188case RADIAL:189g2d.setColor(Color.white);190g2d.fillRect(ctrX-1, ctrY-1, 2, 2);191g2d.drawString("C", ctrX, ctrY + 12);192g2d.fillRect(focusX-1, focusY-1, 2, 2);193g2d.drawString("F", focusX, focusY + 12);194break;195}196197g2d.dispose();198}199200private void updatePoints(int x, int y) {201Point2D inv = new Point2D.Double(x, y);202203try {204inv = transform.inverseTransform(inv, null);205} catch (NoninvertibleTransformException e) {206e.printStackTrace();207}208209x = (int)inv.getX();210y = (int)inv.getY();211212switch (paintType) {213default:214case BASIC:215case LINEAR:216// pick the closest point to move217if (inv.distance(startX, startY) < inv.distance(endX, endY)) {218startX = x;219startY = y;220} else {221endX = x;222endY = y;223}224break;225226case RADIAL:227// pick the closest point to move228if (inv.distance(ctrX, ctrY) < inv.distance(focusX, focusY)) {229ctrX = x;230ctrY = y;231} else {232focusX = x;233focusY = y;234}235break;236}237238updatePaint();239}240241private void makeNewPaint() {242Color[] colors = Arrays.copyOf(COLORS, numColors);243float[] fractions = new float[colors.length];244for (int i = 0; i < fractions.length; i++) {245fractions[i] = ((float)i) / (fractions.length-1);246}247248switch (paintType) {249case BASIC:250boolean cyclic = (cycleMethod != CycleMethod.NO_CYCLE);251paint = new GradientPaint(startX, startY, Color.RED,252endX, endY, Color.BLUE, cyclic);253break;254255default:256case LINEAR:257paint =258new LinearGradientPaint(new Point2D.Float(startX, startY),259new Point2D.Float(endX, endY),260fractions, colors,261cycleMethod, colorSpace,262new AffineTransform());263break;264265case RADIAL:266paint =267new RadialGradientPaint(new Point2D.Float(ctrX, ctrY),268radius,269new Point2D.Float(focusX, focusY),270fractions, colors,271cycleMethod, colorSpace,272new AffineTransform());273break;274}275276switch (xformType) {277default:278case IDENTITY:279transform = new AffineTransform();280break;281case TRANSLATE:282transform = AffineTransform.getTranslateInstance(2, 2);283break;284case SCALE:285transform = AffineTransform.getScaleInstance(1.2, 1.4);286break;287case SHEAR:288transform = AffineTransform.getShearInstance(0.1, 0.1);289break;290case ROTATE:291transform = AffineTransform.getRotateInstance(Math.PI / 4,292getWidth()/2,293getHeight()/2);294break;295}296}297298public void updatePaint() {299makeNewPaint();300repaint();301}302303private class MyMouseAdapter extends MouseAdapter {304@Override305public void mouseClicked(MouseEvent e) {306updatePoints(e.getX(), e.getY());307}308309@Override310public void mouseDragged(MouseEvent e) {311updatePoints(e.getX(), e.getY());312}313}314315public Dimension getPreferredSize() {316return new Dimension(400, 400);317}318}319320private class ControlsPanel extends JPanel implements ActionListener {321private JComboBox cmbPaint, cmbCycle, cmbSpace, cmbShape, cmbXform;322private JCheckBox cbAntialias, cbRender;323private JSpinner spinNumColors;324325private ControlsPanel() {326cmbPaint = createCombo(this, paintType);327cmbPaint.setSelectedIndex(1);328cmbCycle = createCombo(this, cycleMethod);329cmbSpace = createCombo(this, colorSpace);330cmbShape = createCombo(this, shapeType);331cmbXform = createCombo(this, xformType);332333int max = COLORS.length;334SpinnerNumberModel model = new SpinnerNumberModel(max, 2, max, 1);335spinNumColors = new JSpinner(model);336spinNumColors.addChangeListener(new ChangeListener() {337public void stateChanged(ChangeEvent e) {338numColors = ((Integer)spinNumColors.getValue()).intValue();339gradientPanel.updatePaint();340}341});342add(spinNumColors);343344cbAntialias = createCheck(this, "Antialiasing");345cbRender = createCheck(this, "Render Quality");346}347348private JComboBox createCombo(JPanel panel, Enum e) {349JComboBox cmb = new JComboBox();350cmb.setModel(new EnumComboBoxModel(e.getClass()));351cmb.addActionListener(this);352panel.add(cmb);353return cmb;354}355356private JCheckBox createCheck(JPanel panel, String name) {357JCheckBox cb = new JCheckBox(name);358cb.addActionListener(this);359panel.add(cb);360return cb;361}362363public void actionPerformed(ActionEvent e) {364Object source = e.getSource();365366if (source == cmbPaint) {367paintType = (PaintType)cmbPaint.getSelectedItem();368} else if (source == cmbCycle) {369cycleMethod = (CycleMethod)cmbCycle.getSelectedItem();370} else if (source == cmbSpace) {371colorSpace = (ColorSpaceType)cmbSpace.getSelectedItem();372} else if (source == cmbShape) {373shapeType = (ShapeType)cmbShape.getSelectedItem();374} else if (source == cmbXform) {375xformType = (XformType)cmbXform.getSelectedItem();376} else if (source == cbAntialias) {377antialiasHint = cbAntialias.isSelected() ?378RenderingHints.VALUE_ANTIALIAS_ON :379RenderingHints.VALUE_ANTIALIAS_OFF;380} else if (source == cbRender) {381renderHint = cbRender.isSelected() ?382RenderingHints.VALUE_RENDER_QUALITY :383RenderingHints.VALUE_RENDER_SPEED;384}385386gradientPanel.updatePaint();387}388}389390private static class EnumComboBoxModel<E extends Enum<E>>391extends AbstractListModel392implements ComboBoxModel393{394private E selected = null;395private List<E> list;396397public EnumComboBoxModel(Class<E> en) {398EnumSet<E> ens = EnumSet.allOf(en);399list = new ArrayList<E>(ens);400selected = list.get(0);401}402403public int getSize() {404return list.size();405}406407public E getElementAt(int index) {408return list.get(index);409}410411public void setSelectedItem(Object anItem) {412selected = (E)anItem;413this.fireContentsChanged(this, 0, getSize());414}415416public E getSelectedItem() {417return selected;418}419}420421public static void main(String[] args) {422final JFrame frame = new JFrame("Multistop Gradient Demo");423frame.addWindowListener(new WindowAdapter() {424public void windowClosing(WindowEvent e) {425frame.dispose();426}427});428frame.add(new MultiGradientTest());429frame.pack();430frame.setLocationRelativeTo(null);431frame.setVisible(true);432}433}434435436