Path: blob/master/src/demo/share/jfc/J2Ddemo/java2d/demos/Paint/Gradient.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 static java.awt.Color.black;35import static java.awt.Color.blue;36import static java.awt.Color.cyan;37import static java.awt.Color.green;38import static java.awt.Color.lightGray;39import static java.awt.Color.magenta;40import static java.awt.Color.orange;41import static java.awt.Color.red;42import static java.awt.Color.white;43import static java.awt.Color.yellow;44import java.awt.Color;45import java.awt.Component;46import java.awt.Dimension;47import java.awt.Font;48import java.awt.GradientPaint;49import java.awt.Graphics;50import java.awt.Graphics2D;51import java.awt.event.ActionEvent;52import java.awt.event.ActionListener;53import java.awt.font.TextLayout;54import java2d.ControlsSurface;55import java2d.CustomControls;56import javax.swing.Icon;57import javax.swing.JMenu;58import javax.swing.JMenuBar;59import javax.swing.JMenuItem;606162@SuppressWarnings("serial")63public class Gradient extends ControlsSurface {6465protected Color innerC, outerC;6667public Gradient() {68setBackground(white);69innerC = green;70outerC = blue;71setControls(new Component[] { new DemoControls(this) });72}7374@Override75public void render(int w, int h, Graphics2D g2) {7677int w2 = w / 2;78int h2 = h / 2;79g2.setPaint(new GradientPaint(0, 0, outerC, w * .35f, h * .35f, innerC));80g2.fillRect(0, 0, w2, h2);81g2.setPaint(new GradientPaint(w, 0, outerC, w * .65f, h * .35f, innerC));82g2.fillRect(w2, 0, w2, h2);83g2.setPaint(new GradientPaint(0, h, outerC, w * .35f, h * .65f, innerC));84g2.fillRect(0, h2, w2, h2);85g2.setPaint(new GradientPaint(w, h, outerC, w * .65f, h * .65f, innerC));86g2.fillRect(w2, h2, w2, h2);8788g2.setColor(black);89TextLayout tl = new TextLayout(90"GradientPaint", g2.getFont(), g2.getFontRenderContext());91tl.draw(g2, (int) (w / 2 - tl.getBounds().getWidth() / 2),92(int) (h / 2 + tl.getBounds().getHeight() / 2));93}9495public static void main(String[] s) {96createDemoFrame(new Gradient());97}9899100static class DemoControls extends CustomControls implements ActionListener {101102Gradient demo;103Color[] colors = { red, orange, yellow, green, blue, lightGray, cyan,104magenta };105String[] colorName = { "Red", "Orange", "Yellow", "Green",106"Blue", "lightGray", "Cyan", "Magenta" };107JMenuItem[] innerMI = new JMenuItem[colors.length];108JMenuItem[] outerMI = new JMenuItem[colors.length];109ColoredSquare[] squares = new ColoredSquare[colors.length];110JMenu imenu, omenu;111112@SuppressWarnings("LeakingThisInConstructor")113public DemoControls(Gradient demo) {114super(demo.name);115this.demo = demo;116JMenuBar inMenuBar = new JMenuBar();117add(inMenuBar);118JMenuBar outMenuBar = new JMenuBar();119add(outMenuBar);120Font font = new Font(Font.SERIF, Font.PLAIN, 10);121122imenu = inMenuBar.add(new JMenu("Inner Color"));123imenu.setFont(font);124imenu.setIcon(new ColoredSquare(demo.innerC));125omenu = outMenuBar.add(new JMenu("Outer Color"));126omenu.setFont(font);127omenu.setIcon(new ColoredSquare(demo.outerC));128for (int i = 0; i < colors.length; i++) {129squares[i] = new ColoredSquare(colors[i]);130innerMI[i] = imenu.add(new JMenuItem(colorName[i]));131innerMI[i].setFont(font);132innerMI[i].setIcon(squares[i]);133innerMI[i].addActionListener(this);134outerMI[i] = omenu.add(new JMenuItem(colorName[i]));135outerMI[i].setFont(font);136outerMI[i].setIcon(squares[i]);137outerMI[i].addActionListener(this);138}139}140141@Override142public void actionPerformed(ActionEvent e) {143for (int i = 0; i < colors.length; i++) {144if (e.getSource().equals(innerMI[i])) {145demo.innerC = colors[i];146imenu.setIcon(squares[i]);147break;148} else if (e.getSource().equals(outerMI[i])) {149demo.outerC = colors[i];150omenu.setIcon(squares[i]);151break;152}153}154demo.repaint();155}156157@Override158public Dimension getPreferredSize() {159return new Dimension(200, 37);160}161162@Override163@SuppressWarnings("SleepWhileHoldingLock")164public void run() {165// goto double buffering166if (demo.getImageType() <= 1) {167demo.setImageType(2);168}169Thread me = Thread.currentThread();170while (thread == me) {171for (int i = 0; i < innerMI.length; i++) {172if (i != 4) {173try {174Thread.sleep(4444);175} catch (InterruptedException e) {176return;177}178innerMI[i].doClick();179}180}181}182thread = null;183}184185186class ColoredSquare implements Icon {187188Color color;189190public ColoredSquare(Color c) {191this.color = c;192}193194@Override195public void paintIcon(Component c, Graphics g, int x, int y) {196Color oldColor = g.getColor();197g.setColor(color);198g.fill3DRect(x, y, getIconWidth(), getIconHeight(), true);199g.setColor(oldColor);200}201202@Override203public int getIconWidth() {204return 12;205}206207@Override208public int getIconHeight() {209return 12;210}211} // End ColoredSquare class212} // End DemoControls213} // End Gradient class214215216217