Path: blob/master/src/demo/share/jfc/J2Ddemo/java2d/demos/Paint/GradAnim.java
41175 views
/*1*2* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7*8* - Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10*11* - Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* - Neither the name of Oracle nor the names of its16* contributors may be used to endorse or promote products derived17* from this software without specific prior written permission.18*19* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS20* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,21* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR22* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR23* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,24* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,25* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR26* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF27* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING28* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS29* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.30*/31package java2d.demos.Paint;323334import java.awt.Color;35import java.awt.Component;36import java.awt.Dimension;37import java.awt.GradientPaint;38import java.awt.Graphics2D;39import java.awt.LinearGradientPaint;40import java.awt.MultipleGradientPaint.CycleMethod;41import java.awt.Paint;42import java.awt.RadialGradientPaint;43import java.awt.event.ActionEvent;44import java.awt.event.ActionListener;45import java.awt.geom.Point2D;46import java2d.AnimatingControlsSurface;47import java2d.CustomControls;48import javax.swing.JComboBox;495051/**52* GradientPaint animation.53*/54@SuppressWarnings("serial")55public class GradAnim extends AnimatingControlsSurface {5657private static final int BASIC_GRADIENT = 0;58private static final int LINEAR_GRADIENT = 1;59private static final int RADIAL_GRADIENT = 2;60private static final int FOCUS_GRADIENT = 3;61private static final int MAX_HUE = 256 * 6;62private animval x1, y1, x2, y2;63private int hue = (int) (Math.random() * MAX_HUE);64private int gradientType;6566public GradAnim() {67setBackground(Color.white);68setControls(new Component[] { new DemoControls(this) });69x1 = new animval(0, 300, 2, 10);70y1 = new animval(0, 300, 2, 10);71x2 = new animval(0, 300, 2, 10);72y2 = new animval(0, 300, 2, 10);73gradientType = BASIC_GRADIENT;74}7576@Override77public void reset(int w, int h) {78x1.newlimits(0, w);79y1.newlimits(0, h);80x2.newlimits(0, w);81y2.newlimits(0, h);82}8384@Override85public void step(int w, int h) {86x1.anim();87y1.anim();88x2.anim();89y2.anim();90hue = (hue + (int) (Math.random() * 10)) % MAX_HUE;91}9293public static Color getColor(int hue) {94int leg = (hue / 256) % 6;95int step = (hue % 256) * 2;96int falling = (step < 256) ? 255 : 511 - step;97int rising = (step < 256) ? step : 255;98int r, g, b;99r = g = b = 0;100switch (leg) {101case 0:102r = 255;103break;104case 1:105r = falling;106g = rising;107break;108case 2:109g = 255;110break;111case 3:112g = falling;113b = rising;114break;115case 4:116b = 255;117break;118case 5:119b = falling;120r = rising;121break;122}123return new Color(r, g, b);124}125126@Override127public void render(int w, int h, Graphics2D g2) {128float fx1 = x1.getFlt();129float fy1 = y1.getFlt();130float fx2 = x2.getFlt();131float fy2 = y2.getFlt();132133if ((fx1 == fx2) && (fy1 == fy2)) {134// just to prevent the points from being coincident135fx2++;136fy2++;137}138139Color c1 = getColor(hue);140Color c2 = getColor(hue + 256 * 3);141Paint gp;142143switch (gradientType) {144case BASIC_GRADIENT:145default:146gp = new GradientPaint(fx1, fy1, c1,147fx2, fy2, c2,148true);149break;150case LINEAR_GRADIENT: {151float[] fractions = new float[] { 0.0f, 0.2f, 1.0f };152Color c3 = getColor(hue + 256 * 2);153Color[] colors = new Color[] { c1, c2, c3 };154gp = new LinearGradientPaint(fx1, fy1,155fx2, fy2,156fractions, colors,157CycleMethod.REFLECT);158}159break;160161case RADIAL_GRADIENT: {162float[] fractions = { 0.0f, 0.2f, 0.8f, 1.0f };163Color c3 = getColor(hue + 256 * 2);164Color c4 = getColor(hue + 256 * 4);165Color[] colors = new Color[] { c1, c2, c3, c4 };166float radius = (float) Point2D.distance(fx1, fy1, fx2, fy2);167gp = new RadialGradientPaint(fx1, fy1, radius,168fractions, colors,169CycleMethod.REFLECT);170}171break;172173case FOCUS_GRADIENT: {174float[] fractions = { 0.0f, 0.2f, 0.8f, 1.0f };175Color c3 = getColor(hue + 256 * 4);176Color c4 = getColor(hue + 256 * 2);177Color[] colors = new Color[] { c1, c2, c3, c4 };178float radius = (float) Point2D.distance(fx1, fy1, fx2, fy2);179float max = Math.max(w, h);180// This function will map the smallest radius to181// max/10 when the points are next to each other,182// max when the points are max distance apart,183// and >max when they are further apart (in which184// case the focus clipping code in RGP will clip185// the focus to be inside the radius).186radius = max * (((radius / max) * 0.9f) + 0.1f);187gp = new RadialGradientPaint(fx2, fy2, radius,188fx1, fy1,189fractions, colors,190CycleMethod.REPEAT);191}192break;193}194g2.setPaint(gp);195g2.fillRect(0, 0, w, h);196g2.setColor(Color.yellow);197g2.drawLine(x1.getInt(), y1.getInt(), x2.getInt(), y2.getInt());198}199200201public final class animval {202203float curval;204float lowval;205float highval;206float currate;207float lowrate;208float highrate;209210public animval(int lowval, int highval,211int lowrate, int highrate) {212this.lowval = lowval;213this.highval = highval;214this.lowrate = lowrate;215this.highrate = highrate;216this.curval = randval(lowval, highval);217this.currate = randval(lowrate, highrate);218}219220public float randval(float low, float high) {221return (float) (low + Math.random() * (high - low));222}223224public float getFlt() {225return curval;226}227228public int getInt() {229return (int) curval;230}231232public void anim() {233curval += currate;234clip();235}236237public void clip() {238if (curval > highval) {239curval = highval - (curval - highval);240if (curval < lowval) {241curval = highval;242}243currate = -randval(lowrate, highrate);244} else if (curval < lowval) {245curval = lowval + (lowval - curval);246if (curval > highval) {247curval = lowval;248}249currate = randval(lowrate, highrate);250}251}252253public void newlimits(int lowval, int highval) {254this.lowval = lowval;255this.highval = highval;256clip();257}258}259260public static void main(String[] argv) {261createDemoFrame(new GradAnim());262}263264265class DemoControls extends CustomControls implements ActionListener {266267GradAnim demo;268JComboBox<String> combo;269270@SuppressWarnings("LeakingThisInConstructor")271public DemoControls(GradAnim demo) {272super(demo.name);273this.demo = demo;274combo = new JComboBox<>();275combo.addActionListener(this);276combo.addItem("2-color GradientPaint");277combo.addItem("3-color LinearGradientPaint");278combo.addItem("4-color RadialGradientPaint");279combo.addItem("4-color RadialGradientPaint with focus");280combo.setSelectedIndex(0);281add(combo);282}283284@Override285public void actionPerformed(ActionEvent e) {286int index = combo.getSelectedIndex();287if (index >= 0) {288demo.gradientType = index;289}290if (!demo.animating.running()) {291demo.repaint();292}293}294295@Override296public Dimension getPreferredSize() {297return new Dimension(200, 41);298}299300@Override301@SuppressWarnings("SleepWhileHoldingLock")302public void run() {303Thread me = Thread.currentThread();304while (thread == me) {305for (int i = 0; i < combo.getItemCount(); i++) {306combo.setSelectedIndex(i);307try {308Thread.sleep(4444);309} catch (InterruptedException e) {310return;311}312}313}314thread = null;315}316}317}318319320