1 36 37 40 41 package java2d.demos.Paint; 42 43 44 import java.awt.*; 45 import java.awt.font.*; 46 import java.awt.event.*; 47 import java.awt.geom.Rectangle2D ; 48 import javax.swing.*; 49 import java2d.ControlsSurface; 50 import java2d.CustomControls; 51 52 import static java.awt.Color .*; 53 54 55 56 public class Gradient extends ControlsSurface { 57 58 protected Color innerC, outerC; 59 private DemoControls controls; 60 61 62 public Gradient() { 63 setBackground(white); 64 innerC = green; 65 outerC = blue; 66 setControls(new Component[] { new DemoControls(this) }); 67 } 68 69 70 public void render(int w, int h, Graphics2D g2) { 71 72 int w2 = w/2; 73 int h2 = h/2; 74 g2.setPaint(new GradientPaint(0,0,outerC,w*.35f,h*.35f,innerC)); 75 g2.fillRect(0, 0, w2, h2); 76 g2.setPaint(new GradientPaint(w,0,outerC,w*.65f,h*.35f,innerC)); 77 g2.fillRect(w2, 0, w2, h2); 78 g2.setPaint(new GradientPaint(0,h,outerC,w*.35f,h*.65f,innerC)); 79 g2.fillRect(0, h2, w2, h2); 80 g2.setPaint(new GradientPaint(w,h,outerC,w*.65f,h*.65f,innerC)); 81 g2.fillRect(w2, h2, w2, h2); 82 83 g2.setColor(black); 84 TextLayout tl = new TextLayout( 85 "GradientPaint", g2.getFont(), g2.getFontRenderContext()); 86 tl.draw(g2, (int) (w/2-tl.getBounds().getWidth()/2), 87 (int) (h/2+tl.getBounds().getHeight()/2)); 88 } 89 90 91 public static void main(String s[]) { 92 createDemoFrame(new Gradient()); 93 } 94 95 96 static class DemoControls extends CustomControls implements ActionListener { 97 98 Gradient demo; 99 Color colors[] = 100 { red, orange, yellow, green, blue, lightGray, cyan, magenta }; 101 String colorName[] = 102 { "Red", "Orange", "Yellow", "Green", 103 "Blue", "lightGray", "Cyan", "Magenta" }; 104 105 JMenuItem innerMI[] = new JMenuItem[colors.length]; 106 JMenuItem outerMI[] = new JMenuItem[colors.length]; 107 ColoredSquare squares[] = new ColoredSquare[colors.length]; 108 JMenu imenu, omenu; 109 110 public DemoControls(Gradient demo) { 111 super(demo.name); 112 this.demo = demo; 113 JMenuBar inMenuBar = new JMenuBar(); 114 add(inMenuBar); 115 JMenuBar outMenuBar = new JMenuBar(); 116 add(outMenuBar); 117 Font font = new Font("serif", Font.PLAIN, 10); 118 119 imenu = (JMenu) inMenuBar.add(new JMenu("Inner Color")); 120 imenu.setFont(font); 121 imenu.setIcon(new ColoredSquare(demo.innerC)); 122 omenu = (JMenu) outMenuBar.add(new JMenu("Outer Color")); 123 omenu.setFont(font); 124 omenu.setIcon(new ColoredSquare(demo.outerC)); 125 for (int i = 0; i < colors.length; i++) { 126 squares[i] = new ColoredSquare(colors[i]); 127 innerMI[i] = imenu.add(new JMenuItem(colorName[i])); 128 innerMI[i].setFont(font); 129 innerMI[i].setIcon(squares[i]); 130 innerMI[i].addActionListener(this); 131 outerMI[i] = omenu.add(new JMenuItem(colorName[i])); 132 outerMI[i].setFont(font); 133 outerMI[i].setIcon(squares[i]); 134 outerMI[i].addActionListener(this); 135 } 136 } 137 138 139 public void actionPerformed(ActionEvent e) { 140 for (int i = 0; i < colors.length; i++) { 141 if (e.getSource().equals(innerMI[i])) { 142 demo.innerC = colors[i]; 143 imenu.setIcon(squares[i]); 144 break; 145 } else if (e.getSource().equals(outerMI[i])) { 146 demo.outerC = colors[i]; 147 omenu.setIcon(squares[i]); 148 break; 149 } 150 } 151 demo.repaint(); 152 } 153 154 155 public Dimension getPreferredSize() { 156 return new Dimension(200,37); 157 } 158 159 160 public void run() { 161 if (demo.getImageType() <= 1) { 163 demo.setImageType(2); 164 } 165 Thread me = Thread.currentThread(); 166 while (thread == me) { 167 for (int i = 0; i < innerMI.length; i++) { 168 if (i != 4) { 169 try { 170 thread.sleep(4444); 171 } catch (InterruptedException e) { return; } 172 innerMI[i].doClick(); 173 } 174 } 175 } 176 thread = null; 177 } 178 179 180 class ColoredSquare implements Icon { 181 Color color; 182 public ColoredSquare(Color c) { 183 this.color = c; 184 } 185 186 public void paintIcon(Component c, Graphics g, int x, int y) { 187 Color oldColor = g.getColor(); 188 g.setColor(color); 189 g.fill3DRect(x,y,getIconWidth(), getIconHeight(), true); 190 g.setColor(oldColor); 191 } 192 public int getIconWidth() { return 12; } 193 public int getIconHeight() { return 12; } 194 } } } | Popular Tags |