8217235: Create automated test for SwingSet ColorChooserDemoTest

Reviewed-by: serb, shurailine
This commit is contained in:
Vikrant Agarwal 2019-01-24 07:15:42 -08:00 committed by Abdul Kolarkunnu
parent 37a7f89c34
commit 5d8b93bb54
5 changed files with 763 additions and 0 deletions

View File

@ -0,0 +1,328 @@
/*
* Copyright (c) 2019, 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.
*/
import static com.sun.swingset3.demos.colorchooser.ColorChooserDemo.BACKGROUND;
import static com.sun.swingset3.demos.colorchooser.ColorChooserDemo.CHOOSER_TITLE;
import static com.sun.swingset3.demos.colorchooser.ColorChooserDemo.DEMO_TITLE;
import static com.sun.swingset3.demos.colorchooser.ColorChooserDemo.GRADIENT_1;
import static com.sun.swingset3.demos.colorchooser.ColorChooserDemo.GRADIENT_2;
import static com.sun.swingset3.demos.colorchooser.ColorChooserDemo.PERIMETER;
import java.awt.Color;
import java.awt.event.KeyEvent;
import org.jemmy2ext.JemmyExt.ByClassChooser;
import org.jtregext.GuiTestListener;
import org.netbeans.jemmy.ClassReference;
import org.netbeans.jemmy.DialogWaiter;
import org.netbeans.jemmy.operators.JButtonOperator;
import org.netbeans.jemmy.operators.JColorChooserOperator;
import org.netbeans.jemmy.operators.JComponentOperator;
import org.netbeans.jemmy.operators.JDialogOperator;
import org.netbeans.jemmy.operators.JFrameOperator;
import org.netbeans.jemmy.operators.JSliderOperator;
import org.netbeans.jemmy.operators.JSpinnerOperator;
import org.netbeans.jemmy.operators.JTabbedPaneOperator;
import org.netbeans.jemmy.operators.JTextFieldOperator;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import com.sun.swingset3.demos.colorchooser.BezierAnimationPanel;
import com.sun.swingset3.demos.colorchooser.BezierAnimationPanel.BezierColor;
import com.sun.swingset3.demos.colorchooser.ColorChooserDemo;
/*
* @test
* @key headful
* @summary Verifies SwingSet3 ColorChooserDemo by performing simple interaction
* with all the controls that are shown in the ColorChooserDialog.
*
* @library /sanity/client/lib/jemmy/src
* @library /sanity/client/lib/Extensions/src
* @library /sanity/client/lib/SwingSet3/src
* @modules java.desktop
* java.logging
* @build com.sun.swingset3.demos.colorchooser.ColorChooserDemo
* @run testng ColorChooserDemoTest
*/
@Listeners(GuiTestListener.class)
public class ColorChooserDemoTest {
private static final String OK_BUTTON_TITLE = "OK";
private static final String CANCEL_BUTTON_TITLE = "Cancel";
private static final String RESET_BUTTON_TITLE = "Reset";
private static final String HSV = "HSV";
private static final String RGB = "RGB";
private static final String HSL = "HSL";
private static final String CMYK = "CMYK";
private static final int HSV_NUMBER_OF_SLIDERS_AND_SPINNERS = 4;
private static final int RGB_NUMBER_OF_SLIDERS_AND_SPINNERS = 4;
private static final int HSL_NUMBER_OF_SLIDERS_AND_SPINNERS = 4;
private static final int CMYK_NUMBER_OF_SLIDERS_AND_SPINNERS = 5;
private static final int HSV_HUE_INDEX = 0;
private static final int HSV_SATURATION_INDEX = 1;
private static final int HSV_VALUE_INDEX = 2;
private static final int HSV_TRANSPARENCY_INDEX = 3;
private static final int HSL_HUE_INDEX = 0;
private static final int HSL_SATURATION_INDEX = 1;
private static final int HSL_LIGHTNESS_INDEX = 2;
private static final int HSL_TRANSPARENCY_INDEX = 3;
private static final int RGB_RED_INDEX = 0;
private static final int RGB_GREEN_INDEX = 1;
private static final int RGB_BLUE_INDEX = 2;
private static final int RGB_ALPHA_INDEX = 3;
private static final int RGB_COLORCODE_TEXT_FIELD_INDEX = 4;
private static final int CMYK_CYAN_INDEX = 0;
private static final int CMYK_MAGENTA_INDEX = 1;
private static final int CMYK_YELLOW_INDEX = 2;
private static final int CMYK_BLACK_INDEX = 3;
private static final int CMYK_ALPHA_INDEX = 4;
private final Color resetColor = new Color(125, 125, 125);
private JDialogOperator colorChooserDialog;
private JButtonOperator okButton;
private JButtonOperator cancelButton;
private JButtonOperator resetButton;
private JColorChooserOperator colorChooser;
private JButtonOperator backgroundButton;
private JButtonOperator gradient1Button;
private JButtonOperator gradient2Button;
private JButtonOperator perimeterButton;
private JTabbedPaneOperator tabOperator;
private JComponentOperator bezierAnimationPanel;
private JSliderOperator[] sliders = new JSliderOperator[5];
private JSpinnerOperator[] spinners = new JSpinnerOperator[5];
private JButtonOperator lastFocusedButton;
@Test
public void test() throws Exception {
new ClassReference(ColorChooserDemo.class.getCanonicalName()).startApplication();
JFrameOperator frame = new JFrameOperator(DEMO_TITLE);
bezierAnimationPanel = new JComponentOperator(frame, new ByClassChooser(BezierAnimationPanel.class));
initializePanelButtons(frame);
checkBackgroundColorChooser();
checkGradient1ColorChooser();
checkGradient2ColorChooser();
checkPerimeterColorChooser();
}
private void checkBackgroundColorChooser() throws Exception {
basicCheck(backgroundButton, BezierColor.BACKGROUND);
checkAllColorChoosers(backgroundButton);
}
private void checkGradient1ColorChooser() throws Exception {
basicCheck(gradient1Button, BezierColor.GRADIENT_A);
}
private void checkGradient2ColorChooser() throws Exception {
basicCheck(gradient2Button, BezierColor.GRADIENT_B);
}
private void checkPerimeterColorChooser() throws Exception {
basicCheck(perimeterButton, BezierColor.OUTER);
}
private void pushButtonAndInitialize(JButtonOperator jbo) throws InterruptedException {
// Wait for focus to return to last focused button
lastFocusedButton.waitHasFocus();
jbo.pushNoBlock();
lastFocusedButton = jbo;
// Wait till the ColorChooserDemo Dialog Opens
new DialogWaiter().waitDialog(CHOOSER_TITLE, false, false);
initializeDialog();
}
private void initializePanelButtons(JFrameOperator frame) {
backgroundButton = new JButtonOperator(frame, BACKGROUND);
gradient1Button = new JButtonOperator(frame, GRADIENT_1);
gradient2Button = new JButtonOperator(frame, GRADIENT_2);
perimeterButton = new JButtonOperator(frame, PERIMETER);
lastFocusedButton = backgroundButton;
}
private void initializeDialog() {
colorChooserDialog = new JDialogOperator(CHOOSER_TITLE);
tabOperator = new JTabbedPaneOperator(colorChooserDialog);
colorChooser = new JColorChooserOperator(colorChooserDialog);
okButton = new JButtonOperator(colorChooserDialog, OK_BUTTON_TITLE);
cancelButton = new JButtonOperator(colorChooserDialog, CANCEL_BUTTON_TITLE);
resetButton = new JButtonOperator(colorChooserDialog, RESET_BUTTON_TITLE);
}
private void basicCheck(JButtonOperator jbo, BezierColor bezierColor) throws Exception {
Color testColor = new Color(100, 26, 155);
Color testColor2 = new Color(10, 40, 50);
checkDefaultColorChooser(jbo, testColor, bezierColor);
checkCancelButton(jbo, testColor2);
checkResetButton(jbo, testColor2);
}
private void checkDefaultColorChooser(JButtonOperator jbo, Color testColor, BezierColor bezierColor)
throws Exception {
BezierAnimationPanel bezierPanel;
pushButtonAndInitialize(jbo);
// Check ColorChooser color is being set and used accordingly
// in the animation panel
setAndWaitColor(testColor);
pushButtonAndWaitDialogClosed(okButton);
bezierPanel = (BezierAnimationPanel) bezierAnimationPanel.getSource();
colorChooser.waitStateOnQueue(jColorChooser -> (bezierPanel.getBezierColor(bezierColor).equals(testColor)));
}
private void checkCancelButton(JButtonOperator jbo, Color testColor) throws Exception {
pushButtonAndInitialize(jbo);
setAndWaitColor(testColor);
pushButtonAndWaitDialogClosed(cancelButton);
}
private void checkResetButton(JButtonOperator jbo, Color testColor) throws Exception {
pushButtonAndInitialize(jbo);
Color initialColor = colorChooser.getColor();
setAndWaitColor(testColor);
resetButton.push();
waitJColorChooserColor(initialColor);
pushButtonAndWaitDialogClosed(okButton);
}
private void checkAllColorChoosers(JButtonOperator jbo) throws Exception {
pushButtonAndInitialize(jbo);
checkHSV();
checkHSL();
checkRGB();
checkCMYK();
pushButtonAndWaitDialogClosed(okButton);
}
private void waitJColorChooserColor(Color expectedColor) {
colorChooser.waitStateOnQueue(jColorChooser -> colorChooser.getColor().equals(expectedColor));
}
private void setAndWaitColor(Color color) {
colorChooser.setColor(color);
// Wait for the Color to be set
waitJColorChooserColor(color);
}
private void resetColor() {
colorChooser.setColor(resetColor);
// Wait for the Color to be reset
waitJColorChooserColor(resetColor);
}
private void checkHSV() {
tabOperator.selectPage(HSV);
initializeSliderAndSpinner(HSV_NUMBER_OF_SLIDERS_AND_SPINNERS);
resetColor();
setAndCheckSlider(sliders[HSV_SATURATION_INDEX], 50, new Color(125, 62, 62));
setAndCheckSlider(sliders[HSV_VALUE_INDEX], 80, new Color(204, 102, 102));
setAndCheckSlider(sliders[HSV_HUE_INDEX], 50, new Color(204, 187, 102));
setAndCheckSlider(sliders[HSV_TRANSPARENCY_INDEX], 50, new Color(204, 187, 102, 127));
setAndCheckSpinner(spinners[HSV_SATURATION_INDEX], 25, new Color(204, 195, 153, 127));
setAndCheckSpinner(spinners[HSV_VALUE_INDEX], 40, new Color(102, 97, 76, 127));
setAndCheckSpinner(spinners[HSV_HUE_INDEX], 25, new Color(102, 87, 76, 127));
setAndCheckSpinner(spinners[HSV_TRANSPARENCY_INDEX], 100, new Color(102, 87, 76, 0));
}
private void checkHSL() {
tabOperator.selectPage(HSL);
initializeSliderAndSpinner(HSL_NUMBER_OF_SLIDERS_AND_SPINNERS);
resetColor();
setAndCheckSlider(sliders[HSL_SATURATION_INDEX], 50, new Color(187, 62, 62));
setAndCheckSlider(sliders[HSL_LIGHTNESS_INDEX], 80, new Color(229, 178, 178));
setAndCheckSlider(sliders[HSL_HUE_INDEX], 180, new Color(178, 229, 229));
setAndCheckSlider(sliders[HSL_TRANSPARENCY_INDEX], 50, new Color(178, 229, 229, 127));
setAndCheckSpinner(spinners[HSL_SATURATION_INDEX], 25, new Color(191, 216, 216, 127));
setAndCheckSpinner(spinners[HSL_LIGHTNESS_INDEX], 40, new Color(76, 127, 127, 127));
setAndCheckSpinner(spinners[HSL_HUE_INDEX], 25, new Color(127, 97, 76, 127));
setAndCheckSpinner(spinners[HSL_TRANSPARENCY_INDEX], 50, new Color(127, 97, 76, 127));
}
private void checkRGB() {
String sampleColor = "111111";
tabOperator.selectPage(RGB);
initializeSliderAndSpinner(RGB_NUMBER_OF_SLIDERS_AND_SPINNERS);
JTextFieldOperator colorCode = new JTextFieldOperator(colorChooserDialog, RGB_COLORCODE_TEXT_FIELD_INDEX);
resetColor();
setAndCheckSlider(sliders[RGB_GREEN_INDEX], 50, new Color(125, 50, 125, 255));
setAndCheckSlider(sliders[RGB_BLUE_INDEX], 80, new Color(125, 50, 80, 255));
setAndCheckSlider(sliders[RGB_RED_INDEX], 50, new Color(50, 50, 80, 255));
setAndCheckSlider(sliders[RGB_ALPHA_INDEX], 125, new Color(50, 50, 80, 125));
setAndCheckSpinner(spinners[RGB_GREEN_INDEX], 25, new Color(50, 25, 80, 125));
setAndCheckSpinner(spinners[RGB_BLUE_INDEX], 40, new Color(50, 25, 40, 125));
setAndCheckSpinner(spinners[RGB_RED_INDEX], 25, new Color(25, 25, 40, 125));
setAndCheckSpinner(spinners[RGB_ALPHA_INDEX], 255, new Color(25, 25, 40, 255));
colorCode.setText(sampleColor);
// Wait for the sampleColor to be set in the color code text field.
colorCode.waitText(sampleColor);
colorCode.getFocus();
colorCode.pressKey(KeyEvent.VK_TAB);
// Wait for the color to be set
waitJColorChooserColor(new Color(17, 17, 17, 255));
}
private void checkCMYK() {
tabOperator.selectPage(CMYK);
initializeSliderAndSpinner(CMYK_NUMBER_OF_SLIDERS_AND_SPINNERS);
resetColor();
setAndCheckSlider(sliders[CMYK_MAGENTA_INDEX], 50, new Color(125, 100, 125, 255));
setAndCheckSlider(sliders[CMYK_YELLOW_INDEX], 80, new Color(125, 100, 85, 255));
setAndCheckSlider(sliders[CMYK_CYAN_INDEX], 50, new Color(100, 100, 85, 255));
setAndCheckSlider(sliders[CMYK_BLACK_INDEX], 50, new Color(164, 164, 140, 255));
setAndCheckSlider(sliders[CMYK_ALPHA_INDEX], 125, new Color(164, 164, 140, 125));
setAndCheckSpinner(spinners[CMYK_MAGENTA_INDEX], 25, new Color(164, 184, 140, 125));
setAndCheckSpinner(spinners[CMYK_YELLOW_INDEX], 40, new Color(164, 184, 172, 125));
setAndCheckSpinner(spinners[CMYK_CYAN_INDEX], 25, new Color(184, 184, 172, 125));
setAndCheckSpinner(spinners[CMYK_BLACK_INDEX], 100, new Color(139, 139, 130, 125));
setAndCheckSpinner(spinners[CMYK_ALPHA_INDEX], 255, new Color(139, 139, 130, 255));
}
private void setAndCheckSlider(JSliderOperator slider, int sliderValue, Color expectedColor) {
slider.setValue(sliderValue);
// Wait for slider to attain the specified value
slider.waitStateOnQueue(jSlider -> slider.getValue() == sliderValue);
colorChooser.waitStateOnQueue(jColorChooser -> (colorChooser.getColor().equals(expectedColor)));
}
private void setAndCheckSpinner(JSpinnerOperator spinner, int spinnerValue, Color expectedColor) {
spinner.setValue(spinnerValue);
// Wait for spinner to attain the specified value
spinner.waitStateOnQueue(jSpinner -> (int) spinner.getValue() == spinnerValue);
colorChooser.waitStateOnQueue(jColorChooser -> (colorChooser.getColor().equals(expectedColor)));
}
private void initializeSliderAndSpinner(int numberOfSlidersAndSpinners) {
for (int i = 0; i < numberOfSlidersAndSpinners; i++) {
sliders[i] = new JSliderOperator(colorChooserDialog, i);
spinners[i] = new JSpinnerOperator(colorChooserDialog, i);
}
}
private void pushButtonAndWaitDialogClosed(JButtonOperator button) {
button.push();
// Wait for the color chooser dialog to close.
colorChooserDialog.waitClosed();
}
}

View File

@ -0,0 +1,254 @@
/*
* Copyright (c) 2007, 2019, 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.
*/
package com.sun.swingset3.demos.colorchooser;
import java.awt.*;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;
import java.awt.geom.GeneralPath;
import java.awt.image.BufferedImage;
import java.util.Hashtable;
import java.util.Map;
import javax.swing.*;
import static com.sun.swingset3.demos.colorchooser.BezierAnimationPanel.BezierColor.*;
/**
* BezierAnimationPanel
*
* @author Jim Graham
* @author Jeff Dinkins (removed dynamic setting changes, made swing friendly)
* @version 1.16 11/17/05
*/
public class BezierAnimationPanel extends JPanel implements Runnable {
public static enum BezierColor {
BACKGROUND, OUTER, GRADIENT_A, GRADIENT_B
}
private final Map<BezierColor, Color> colors = new Hashtable<BezierColor, Color>();
private GradientPaint gradient = null;
private static final int NUMPTS = 6;
private final float[] animpts = new float[NUMPTS * 2];
private final float[] deltas = new float[NUMPTS * 2];
private BufferedImage img;
private Thread anim;
private final Object lock = new Object();
/**
* BezierAnimationPanel Constructor
*/
public BezierAnimationPanel() {
setOpaque(true);
colors.put(BACKGROUND, new Color(0, 0, 153));
colors.put(OUTER, new Color(255, 255, 255));
colors.put(GRADIENT_A, new Color(255, 0, 101));
colors.put(GRADIENT_B, new Color(255, 255, 0));
addHierarchyListener(new HierarchyListener() {
public void hierarchyChanged(HierarchyEvent e) {
if (isShowing()) {
start();
} else {
stop();
}
}
});
}
public Color getBezierColor(BezierColor bezierColor) {
return colors.get(bezierColor);
}
public void setBezierColor(BezierColor bezierColor, Color value) {
if (value != null) {
colors.put(bezierColor, value);
}
}
public void start() {
Dimension size = getSize();
for (int i = 0; i < animpts.length; i += 2) {
animpts[i] = (float) (Math.random() * size.width);
animpts[i + 1] = (float) (Math.random() * size.height);
deltas[i] = (float) (Math.random() * 4.0 + 2.0);
deltas[i + 1] = (float) (Math.random() * 4.0 + 2.0);
if (animpts[i] > size.width / 6.0f) {
deltas[i] = -deltas[i];
}
if (animpts[i + 1] > size.height / 6.0f) {
deltas[i + 1] = -deltas[i + 1];
}
}
anim = new Thread(this);
anim.setPriority(Thread.MIN_PRIORITY);
anim.start();
}
public synchronized void stop() {
anim = null;
notify();
}
private static void animate(float[] pts, float[] deltas, int index, int limit) {
float newpt = pts[index] + deltas[index];
if (newpt <= 0) {
newpt = -newpt;
deltas[index] = (float) (Math.random() * 3.0 + 2.0);
} else if (newpt >= (float) limit) {
newpt = 2.0f * limit - newpt;
deltas[index] = -(float) (Math.random() * 3.0 + 2.0);
}
pts[index] = newpt;
}
public void run() {
Thread me = Thread.currentThread();
while (getSize().width <= 0) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
return;
}
}
Graphics2D g2d = null;
Graphics2D bufferG2D = null;
BasicStroke solid = new BasicStroke(9.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 9.0f);
GeneralPath gp = new GeneralPath(GeneralPath.WIND_NON_ZERO);
int rule = AlphaComposite.SRC_OVER;
AlphaComposite opaque = AlphaComposite.SrcOver;
AlphaComposite blend = AlphaComposite.getInstance(rule, 0.9f);
AlphaComposite set = AlphaComposite.Src;
Dimension oldSize = getSize();
Shape clippath = null;
while (anim == me) {
Dimension size = getSize();
if (size.width != oldSize.width || size.height != oldSize.height) {
img = null;
clippath = null;
if (bufferG2D != null) {
bufferG2D.dispose();
bufferG2D = null;
}
}
oldSize = size;
if (img == null) {
img = (BufferedImage) createImage(size.width, size.height);
}
if (bufferG2D == null) {
bufferG2D = img.createGraphics();
bufferG2D.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_DEFAULT);
bufferG2D.setClip(clippath);
}
g2d = bufferG2D;
float[] ctrlpts;
for (int i = 0; i < animpts.length; i += 2) {
animate(animpts, deltas, i, size.width);
animate(animpts, deltas, i + 1, size.height);
}
ctrlpts = animpts;
int len = ctrlpts.length;
gp.reset();
float prevx = ctrlpts[len - 2];
float prevy = ctrlpts[len - 1];
float curx = ctrlpts[0];
float cury = ctrlpts[1];
float midx = (curx + prevx) / 2.0f;
float midy = (cury + prevy) / 2.0f;
gp.moveTo(midx, midy);
for (int i = 2; i <= ctrlpts.length; i += 2) {
float x1 = (midx + curx) / 2.0f;
float y1 = (midy + cury) / 2.0f;
prevx = curx;
prevy = cury;
if (i < ctrlpts.length) {
curx = ctrlpts[i];
cury = ctrlpts[i + 1];
} else {
curx = ctrlpts[0];
cury = ctrlpts[1];
}
midx = (curx + prevx) / 2.0f;
midy = (cury + prevy) / 2.0f;
float x2 = (prevx + midx) / 2.0f;
float y2 = (prevy + midy) / 2.0f;
gp.curveTo(x1, y1, x2, y2, midx, midy);
}
gp.closePath();
synchronized (lock) {
g2d.setComposite(set);
g2d.setBackground(getBezierColor(BACKGROUND));
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
// g2d.clearRect(bounds.x-5, bounds.y-5, bounds.x + bounds.width
// + 5, bounds.y + bounds.height + 5);
g2d.clearRect(0, 0, getWidth(), getHeight());
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(getBezierColor(OUTER));
g2d.setComposite(opaque);
g2d.setStroke(solid);
g2d.draw(gp);
g2d.setPaint(gradient);
Rectangle bounds = gp.getBounds();
gradient = new GradientPaint(bounds.x, bounds.y, getBezierColor(GRADIENT_A), bounds.x + bounds.width,
bounds.y + bounds.height, getBezierColor(GRADIENT_B), true);
g2d.setComposite(blend);
g2d.fill(gp);
}
repaint();
Thread.yield();
}
if (g2d != null) {
g2d.dispose();
}
}
public void paint(Graphics g) {
synchronized (lock) {
Graphics2D g2d = (Graphics2D) g;
if (img != null) {
g2d.setComposite(AlphaComposite.Src);
g2d.drawImage(img, null, 0, 0);
}
}
}
}

View File

@ -0,0 +1,170 @@
/*
* Copyright (c) 2007, 2019, 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.
*/
package com.sun.swingset3.demos.colorchooser;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import com.sun.swingset3.demos.JGridPanel;
import com.sun.swingset3.demos.ResourceManager;
import com.sun.swingset3.DemoProperties;
/**
* JColorChooserDemo
*
* @author Jeff Dinkins
* @version 1.1 07/16/99
*/
@DemoProperties(value = "JColorChooser Demo", category = "Choosers", description = "Demonstrates JColorChooser, a component which allows the user to pick a color.", sourceFiles = {
"com/sun/swingset3/demos/colorchooser/ColorChooserDemo.java",
"com/sun/swingset3/demos/colorchooser/BezierAnimationPanel.java", "com/sun/swingset3/demos/JGridPanel.java",
"com/sun/swingset3/demos/ResourceManager.java",
"com/sun/swingset3/demos/colorchooser/resources/ColorChooserDemo.properties",
"com/sun/swingset3/demos/colorchooser/resources/images/ColorChooserDemo.gif" })
public class ColorChooserDemo extends JPanel {
private static final ResourceManager resourceManager = new ResourceManager(ColorChooserDemo.class);
private final BezierAnimationPanel bezAnim = new BezierAnimationPanel();
public static final String BACKGROUND = resourceManager.getString("ColorChooserDemo.background");
public static final String GRADIENT_1 = resourceManager.getString("ColorChooserDemo.grad_a");
public static final String GRADIENT_2 = resourceManager.getString("ColorChooserDemo.grad_b");
public static final String PERIMETER = resourceManager.getString("ColorChooserDemo.outer_line");
public static final String CHOOSER_TITLE = resourceManager.getString("ColorChooserDemo.chooser_title");
private final JButton outerColorButton = new JButton(PERIMETER);
private final JButton backgroundColorButton = new JButton(BACKGROUND);
private final JButton gradientAButton = new JButton(GRADIENT_1);
private final JButton gradientBButton = new JButton(GRADIENT_2);
public static final String DEMO_TITLE = ColorChooserDemo.class.getAnnotation(DemoProperties.class).value();
/**
* main method allows us to run as a standalone demo.
*/
public static void main(String[] args) {
JFrame frame = new JFrame(ColorChooserDemo.class.getAnnotation(DemoProperties.class).value());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new ColorChooserDemo());
frame.setPreferredSize(new Dimension(800, 600));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
/**
* ColorChooserDemo Constructor
*/
public ColorChooserDemo() {
setLayout(new BorderLayout());
outerColorButton.setIcon(new ColorSwatch(BezierAnimationPanel.BezierColor.OUTER));
backgroundColorButton.setIcon(new ColorSwatch(BezierAnimationPanel.BezierColor.BACKGROUND));
gradientAButton.setIcon(new ColorSwatch(BezierAnimationPanel.BezierColor.GRADIENT_A));
gradientBButton.setIcon(new ColorSwatch(BezierAnimationPanel.BezierColor.GRADIENT_B));
ActionListener l = new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
final BezierAnimationPanel.BezierColor bezierColor = ((ColorSwatch) button.getIcon()).getBezierColor();
Color current = bezAnim.getBezierColor(bezierColor);
final JColorChooser chooser = new JColorChooser(current != null ? current : Color.WHITE);
ActionListener colorChooserListener = new ActionListener() {
public void actionPerformed(ActionEvent ae) {
bezAnim.setBezierColor(bezierColor, chooser.getColor());
}
};
JDialog dialog = JColorChooser.createDialog(ColorChooserDemo.this, CHOOSER_TITLE, true, chooser,
colorChooserListener, null);
dialog.setVisible(true);
}
};
outerColorButton.addActionListener(l);
backgroundColorButton.addActionListener(l);
gradientAButton.addActionListener(l);
gradientBButton.addActionListener(l);
// Add control buttons
JPanel buttonPanel = new JPanel(new GridLayout(1, 4, 15, 0));
buttonPanel.add(backgroundColorButton);
buttonPanel.add(gradientAButton);
buttonPanel.add(gradientBButton);
buttonPanel.add(outerColorButton);
// Add everything to the panel
JGridPanel pnContent = new JGridPanel(1, 0, 1);
pnContent.cell(buttonPanel, JGridPanel.Layout.CENTER).cell(bezAnim);
pnContent.setBorder(new EmptyBorder(10, 0, 0, 0));
add(pnContent);
}
private class ColorSwatch implements Icon {
private final BezierAnimationPanel.BezierColor bezierColor;
public ColorSwatch(BezierAnimationPanel.BezierColor bezierColor) {
this.bezierColor = bezierColor;
}
public int getIconWidth() {
return 11;
}
public int getIconHeight() {
return 11;
}
public BezierAnimationPanel.BezierColor getBezierColor() {
return bezierColor;
}
public void paintIcon(Component c, Graphics g, int x, int y) {
g.setColor(Color.black);
g.fillRect(x, y, getIconWidth(), getIconHeight());
g.setColor(bezAnim.getBezierColor(bezierColor));
g.fillRect(x + 2, y + 2, getIconWidth() - 4, getIconHeight() - 4);
}
}
}

View File

@ -0,0 +1,11 @@
### ColorChooser Demo ###
ColorChooserDemo.accessible_description=The ColorChooser allows a user to pick a color either from a pallete or by choosing RGB or HSB values
ColorChooserDemo.tooltip=JColorChooser demo
ColorChooserDemo.name=ColorChooser Demo
ColorChooserDemo.chooser_title=Choose a Color
ColorChooserDemo.background=Background
ColorChooserDemo.grad_a=Gradient 1
ColorChooserDemo.grad_b=Gradient 2
ColorChooserDemo.outer_line=Perimeter
ColorChooserDemo.cup=Image of the Java Trademark Coffee Cup