1 /* 2 * 3 * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * - Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * - Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * - Neither the name of Oracle nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 21 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 package java2d.demos.Paint; 33 34 35 import static java.awt.Color.black; 36 import static java.awt.Color.blue; 37 import static java.awt.Color.cyan; 38 import static java.awt.Color.green; 39 import static java.awt.Color.lightGray; 40 import static java.awt.Color.magenta; 41 import static java.awt.Color.orange; 42 import static java.awt.Color.red; 43 import static java.awt.Color.white; 44 import static java.awt.Color.yellow; 45 import java.awt.Color; 46 import java.awt.Component; 47 import java.awt.Dimension; 48 import java.awt.Font; 49 import java.awt.GradientPaint; 50 import java.awt.Graphics; 51 import java.awt.Graphics2D; 52 import java.awt.event.ActionEvent; 53 import java.awt.event.ActionListener; 54 import java.awt.font.TextLayout; 55 import java2d.ControlsSurface; 56 import java2d.CustomControls; 57 import javax.swing.Icon; 58 import javax.swing.JMenu; 59 import javax.swing.JMenuBar; 60 import javax.swing.JMenuItem; 61 62 63 @SuppressWarnings("serial") 64 public class Gradient extends ControlsSurface { 65 66 protected Color innerC, outerC; 67 68 public Gradient() { 69 setBackground(white); 70 innerC = green; 71 outerC = blue; 72 setControls(new Component[] { new DemoControls(this) }); 73 } 74 75 @Override 76 public void render(int w, int h, Graphics2D g2) { 77 78 int w2 = w / 2; 79 int h2 = h / 2; 80 g2.setPaint(new GradientPaint(0, 0, outerC, w * .35f, h * .35f, innerC)); 81 g2.fillRect(0, 0, w2, h2); 82 g2.setPaint(new GradientPaint(w, 0, outerC, w * .65f, h * .35f, innerC)); 83 g2.fillRect(w2, 0, w2, h2); 84 g2.setPaint(new GradientPaint(0, h, outerC, w * .35f, h * .65f, innerC)); 85 g2.fillRect(0, h2, w2, h2); 86 g2.setPaint(new GradientPaint(w, h, outerC, w * .65f, h * .65f, innerC)); 87 g2.fillRect(w2, h2, w2, h2); 88 89 g2.setColor(black); 90 TextLayout tl = new TextLayout( 91 "GradientPaint", g2.getFont(), g2.getFontRenderContext()); 92 tl.draw(g2, (int) (w / 2 - tl.getBounds().getWidth() / 2), 93 (int) (h / 2 + tl.getBounds().getHeight() / 2)); 94 } 95 96 public static void main(String s[]) { 97 createDemoFrame(new Gradient()); 98 } 99 100 101 static class DemoControls extends CustomControls implements ActionListener { 102 103 Gradient demo; 104 Color colors[] = { red, orange, yellow, green, blue, lightGray, cyan, 105 magenta }; 106 String colorName[] = { "Red", "Orange", "Yellow", "Green", 107 "Blue", "lightGray", "Cyan", "Magenta" }; 108 JMenuItem innerMI[] = new JMenuItem[colors.length]; 109 JMenuItem outerMI[] = new JMenuItem[colors.length]; 110 ColoredSquare squares[] = new ColoredSquare[colors.length]; 111 JMenu imenu, omenu; 112 113 @SuppressWarnings("LeakingThisInConstructor") 114 public DemoControls(Gradient demo) { 115 super(demo.name); 116 this.demo = demo; 117 JMenuBar inMenuBar = new JMenuBar(); 118 add(inMenuBar); 119 JMenuBar outMenuBar = new JMenuBar(); 120 add(outMenuBar); 121 Font font = new Font(Font.SERIF, Font.PLAIN, 10); 122 123 imenu = inMenuBar.add(new JMenu("Inner Color")); 124 imenu.setFont(font); 125 imenu.setIcon(new ColoredSquare(demo.innerC)); 126 omenu = outMenuBar.add(new JMenu("Outer Color")); 127 omenu.setFont(font); 128 omenu.setIcon(new ColoredSquare(demo.outerC)); 129 for (int i = 0; i < colors.length; i++) { 130 squares[i] = new ColoredSquare(colors[i]); 131 innerMI[i] = imenu.add(new JMenuItem(colorName[i])); 132 innerMI[i].setFont(font); 133 innerMI[i].setIcon(squares[i]); 134 innerMI[i].addActionListener(this); 135 outerMI[i] = omenu.add(new JMenuItem(colorName[i])); 136 outerMI[i].setFont(font); 137 outerMI[i].setIcon(squares[i]); 138 outerMI[i].addActionListener(this); 139 } 140 } 141 142 @Override 143 public void actionPerformed(ActionEvent e) { 144 for (int i = 0; i < colors.length; i++) { 145 if (e.getSource().equals(innerMI[i])) { 146 demo.innerC = colors[i]; 147 imenu.setIcon(squares[i]); 148 break; 149 } else if (e.getSource().equals(outerMI[i])) { 150 demo.outerC = colors[i]; 151 omenu.setIcon(squares[i]); 152 break; 153 } 154 } 155 demo.repaint(); 156 } 157 158 @Override 159 public Dimension getPreferredSize() { 160 return new Dimension(200, 37); 161 } 162 163 @Override 164 @SuppressWarnings("SleepWhileHoldingLock") 165 public void run() { 166 // goto double buffering 167 if (demo.getImageType() <= 1) { 168 demo.setImageType(2); 169 } 170 Thread me = Thread.currentThread(); 171 while (thread == me) { 172 for (int i = 0; i < innerMI.length; i++) { 173 if (i != 4) { 174 try { 175 Thread.sleep(4444); 176 } catch (InterruptedException e) { 177 return; 178 } 179 innerMI[i].doClick(); 180 } 181 } 182 } 183 thread = null; 184 } 185 186 187 class ColoredSquare implements Icon { 188 189 Color color; 190 191 public ColoredSquare(Color c) { 192 this.color = c; 193 } 194 195 @Override 196 public void paintIcon(Component c, Graphics g, int x, int y) { 197 Color oldColor = g.getColor(); 198 g.setColor(color); 199 g.fill3DRect(x, y, getIconWidth(), getIconHeight(), true); 200 g.setColor(oldColor); 201 } 202 203 @Override 204 public int getIconWidth() { 205 return 12; 206 } 207 208 @Override 209 public int getIconHeight() { 210 return 12; 211 } 212 } // End ColoredSquare class 213 } // End DemoControls 214 } // End Gradient class 215