home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / jfc / Java2D / src / java2d / demos / Paint / Gradient.java < prev    next >
Encoding:
Java Source  |  2002-09-06  |  6.9 KB  |  196 lines

  1. /*
  2.  * Copyright (c) 2002 Sun Microsystems, Inc. All  Rights Reserved.
  3.  * 
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  * 
  8.  * -Redistributions of source code must retain the above copyright
  9.  *  notice, this list of conditions and the following disclaimer.
  10.  * 
  11.  * -Redistribution in binary form must reproduct the above copyright
  12.  *  notice, this list of conditions and the following disclaimer in
  13.  *  the documentation and/or other materials provided with the distribution.
  14.  * 
  15.  * Neither the name of Sun Microsystems, Inc. or the names of contributors
  16.  * may be used to endorse or promote products derived from this software
  17.  * without specific prior written permission.
  18.  * 
  19.  * This software is provided "AS IS," without a warranty of any kind. ALL
  20.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
  21.  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  22.  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
  23.  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
  24.  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
  25.  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
  26.  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
  27.  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
  28.  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
  29.  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  30.  * 
  31.  * You acknowledge that Software is not designed, licensed or intended for
  32.  * use in the design, construction, operation or maintenance of any nuclear
  33.  * facility.
  34.  */
  35.  
  36. /*
  37.  * @(#)Gradient.java    1.11 02/06/13
  38.  */
  39.  
  40. package java2d.demos.Paint;
  41.  
  42.  
  43. import java.awt.*;
  44. import java.awt.font.*;
  45. import java.awt.event.*;
  46. import java.awt.geom.Rectangle2D;
  47. import javax.swing.*;
  48. import java2d.ControlsSurface;
  49. import java2d.CustomControls;
  50.  
  51.  
  52.  
  53. public class Gradient extends ControlsSurface {
  54.  
  55.     protected Color innerC, outerC;
  56.     private DemoControls controls;
  57.  
  58.  
  59.     public Gradient() {
  60.         setBackground(Color.white);
  61.         innerC = Color.green;
  62.         outerC = Color.blue;
  63.         setControls(new Component[] { new DemoControls(this) });
  64.     }
  65.  
  66.  
  67.     public void render(int w, int h, Graphics2D g2) {
  68.  
  69.         int w2 = w/2;
  70.         int h2 = h/2;
  71.         g2.setPaint(new GradientPaint(0,0,outerC,w*.35f,h*.35f,innerC));
  72.         g2.fillRect(0, 0, w2, h2);
  73.         g2.setPaint(new GradientPaint(w,0,outerC,w*.65f,h*.35f,innerC));
  74.         g2.fillRect(w2, 0, w2, h2);
  75.         g2.setPaint(new GradientPaint(0,h,outerC,w*.35f,h*.65f,innerC));
  76.         g2.fillRect(0, h2, w2, h2);
  77.         g2.setPaint(new GradientPaint(w,h,outerC,w*.65f,h*.65f,innerC));
  78.         g2.fillRect(w2, h2, w2, h2);
  79.  
  80.         g2.setColor(Color.black);
  81.         TextLayout tl = new TextLayout(
  82.                 "GradientPaint", g2.getFont(), g2.getFontRenderContext());
  83.         tl.draw(g2, (int) (w/2-tl.getBounds().getWidth()/2),
  84.                 (int) (h/2+tl.getBounds().getHeight()/2));
  85.     }
  86.  
  87.  
  88.     public static void main(String s[]) {
  89.         createDemoFrame(new Gradient());
  90.     }
  91.  
  92.  
  93.     static class DemoControls extends CustomControls implements ActionListener {
  94.  
  95.         Gradient demo;
  96.         Color colors[] = 
  97.                 { Color.red, Color.orange, Color.yellow, Color.green,
  98.                   Color.blue, Color.lightGray, Color.cyan, Color.magenta };
  99.         String colorName[] =
  100.                 { "Red", "Orange", "Yellow", "Green", 
  101.                   "Blue", "lightGray", "Cyan", "Magenta" };
  102.         
  103.         JMenuItem innerMI[] = new JMenuItem[colors.length];
  104.         JMenuItem outerMI[] = new JMenuItem[colors.length];
  105.         ColoredSquare squares[] = new ColoredSquare[colors.length];
  106.         JMenu imenu, omenu;
  107.  
  108.         public DemoControls(Gradient demo) {
  109.             super(demo.name);
  110.             this.demo = demo;
  111.             setBackground(Color.gray);
  112.             JMenuBar inMenuBar = new JMenuBar();
  113.             add(inMenuBar);
  114.             JMenuBar outMenuBar = new JMenuBar();
  115.             add(outMenuBar);
  116.             Font font = new Font("serif", Font.PLAIN, 10);
  117.  
  118.             imenu = (JMenu) inMenuBar.add(new JMenu("Inner Color"));
  119.             imenu.setFont(font);
  120.             imenu.setIcon(new ColoredSquare(demo.innerC));
  121.             omenu = (JMenu) outMenuBar.add(new JMenu("Outer Color"));
  122.             omenu.setFont(font);
  123.             omenu.setIcon(new ColoredSquare(demo.outerC));
  124.             for (int i = 0; i < colors.length; i++) {
  125.                 squares[i] = new ColoredSquare(colors[i]);
  126.                 innerMI[i] = imenu.add(new JMenuItem(colorName[i]));
  127.                 innerMI[i].setFont(font);
  128.                 innerMI[i].setIcon(squares[i]);
  129.                 innerMI[i].addActionListener(this);
  130.                 outerMI[i] = omenu.add(new JMenuItem(colorName[i]));
  131.                 outerMI[i].setFont(font);
  132.                 outerMI[i].setIcon(squares[i]);
  133.                 outerMI[i].addActionListener(this);
  134.             } 
  135.         }
  136.  
  137.  
  138.         public void actionPerformed(ActionEvent e) {
  139.             for (int i = 0; i < colors.length; i++) {
  140.                 if (e.getSource().equals(innerMI[i])) {
  141.                     demo.innerC = colors[i];
  142.                     imenu.setIcon(squares[i]);
  143.                     break;
  144.                 } else if (e.getSource().equals(outerMI[i])) {
  145.                     demo.outerC = colors[i];
  146.                     omenu.setIcon(squares[i]);
  147.                     break;
  148.                 }
  149.             }
  150.             demo.repaint();
  151.         }
  152.  
  153.  
  154.         public Dimension getPreferredSize() {
  155.             return new Dimension(200,31);
  156.         }
  157.  
  158.  
  159.         public void run() {
  160.             // goto double buffering
  161.             if (demo.getImageType() <= 1) {
  162.                 demo.setImageType(2);
  163.             }
  164.             Thread me = Thread.currentThread();
  165.             while (thread == me) {
  166.                 for (int i = 0; i < innerMI.length; i++) {
  167.                     if (i != 4) {
  168.                         try {
  169.                             thread.sleep(4444);
  170.                         } catch (InterruptedException e) { return; }
  171.                         innerMI[i].doClick();
  172.                     }
  173.                 }
  174.             }
  175.             thread = null;
  176.         }
  177.  
  178.  
  179.         class ColoredSquare implements Icon {
  180.             Color color;
  181.             public ColoredSquare(Color c) {
  182.                 this.color = c;
  183.             }
  184.     
  185.             public void paintIcon(Component c, Graphics g, int x, int y) {
  186.                 Color oldColor = g.getColor();
  187.                 g.setColor(color);
  188.                 g.fill3DRect(x,y,getIconWidth(), getIconHeight(), true);
  189.                 g.setColor(oldColor);
  190.             }
  191.             public int getIconWidth() { return 12; }
  192.             public int getIconHeight() { return 12; }
  193.         } // End ColoredSquare class
  194.     } // End DemoControls
  195. } // End Gradient class
  196.