home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / plugin / applets / GraphicsTest / GraphicsTest.java < prev    next >
Encoding:
Java Source  |  2002-09-06  |  11.7 KB  |  603 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.  * @(#)GraphicsTest.java    1.13 02/06/13
  38.  */
  39.  
  40. import java.awt.*;
  41. import java.util.*;
  42. import java.awt.event.*;
  43. import java.applet.Applet;
  44.  
  45. class GraphicsPanel extends Panel {
  46.     ActionListener al;
  47.     ItemListener il;
  48.     public GraphicsCards cards;
  49.  
  50.      GraphicsPanel(EventListener listener) {
  51.          al = (ActionListener)listener;
  52.          il = (ItemListener)listener;
  53.  
  54.         setLayout(new BorderLayout());
  55.  
  56.         add("Center", cards = new GraphicsCards());
  57.  
  58.         Panel p = new Panel();
  59.         //p.setLayout(new BorderLayout());
  60.  
  61.         Button b = new Button("next");
  62.         b.addActionListener(al);
  63.         p.add(b);
  64.  
  65.         b = new Button("previous");
  66.         b.addActionListener(al);
  67.         p.add(b);
  68.  
  69.         p.add(new Label("go to:", Label.RIGHT));
  70.  
  71.         Choice c = new Choice();
  72.         c.addItemListener(il);
  73.         p.add(c);
  74.  
  75.         c.addItem("Arc");
  76.         c.addItem("Oval");
  77.         c.addItem("Polygon");
  78.         c.addItem("Rect");
  79.         c.addItem("RoundRect");
  80.  
  81.         add("North", p);
  82.  
  83.         setSize(400, 400);
  84.     }
  85.  
  86.     public Dimension getPreferredSize() {
  87.         return new Dimension(200, 100);
  88.     }
  89. }
  90.  
  91. public class GraphicsTest extends Applet
  92. implements ActionListener, ItemListener {
  93.     GraphicsPanel mainPanel;
  94.  
  95.     public void init() {
  96.         setLayout(new BorderLayout());
  97.         add("Center", mainPanel = new GraphicsPanel(this));
  98.     }
  99.  
  100.     public void destroy() {
  101.         remove(mainPanel);
  102.     }
  103.  
  104.     public void actionPerformed(ActionEvent e) {
  105.         String arg = e.getActionCommand();
  106.  
  107.         if ("next".equals(arg)) {
  108.             ((CardLayout)mainPanel.cards.getLayout()).next(mainPanel.cards);
  109.         }
  110.         else if ("previous".equals(arg)) {
  111.             ((CardLayout)mainPanel.cards.getLayout()).previous(mainPanel.cards);
  112.         }
  113.     }
  114.  
  115.     public void itemStateChanged(ItemEvent e) {
  116.         ((CardLayout)mainPanel.cards.getLayout()).show(mainPanel.cards,(String)e.getItem());
  117.     }
  118.  
  119.     public static void main(String args[]) {
  120.         AppletFrame.startApplet("GraphicsTest", "Graphics Test", args);
  121.     }
  122.  
  123.     public String getAppletInfo() {
  124.         return "An interactive demonstration of some graphics.";
  125.     }
  126. }   // end class GraphicsTest
  127.  
  128.  
  129. class GraphicsCards extends Panel {
  130.     public GraphicsCards() {
  131.         setLayout(new CardLayout());
  132.         add("Arc", new ArcCard());
  133.         add("Oval", new ShapeTest( new OvalShape() ) );
  134.         add("Polygon", new ShapeTest( new PolygonShape() ) );
  135.         add("Rect", new ShapeTest( new RectShape() ) );
  136.         add("RoundRect", new ShapeTest( new RoundRectShape() ) );
  137.     }
  138. }   // end class GraphicsCards
  139.  
  140.  
  141. class ArcCard extends Panel {
  142.     public ArcCard() {
  143.         setLayout(new GridLayout(0, 2));
  144.         add(new ArcPanel(true));
  145.         add(new ArcPanel(false));
  146.         add(new ArcDegreePanel(true));
  147.         add(new ArcDegreePanel(false));
  148.     }
  149. }   // end class ArcCard
  150.  
  151.  
  152. class ArcDegreePanel extends Panel {
  153.     boolean filled;
  154.  
  155.     public ArcDegreePanel(boolean filled) {
  156.         this.filled = filled;
  157.     }
  158.  
  159.     void arcSteps(Graphics g,
  160.                   int step,
  161.                   int x,
  162.                   int y,
  163.                   int w,
  164.                   int h,
  165.                   Color c1,
  166.                   Color c2) {
  167.     int a1 = 0;
  168.     int a2 = step;
  169.     int progress = 0;
  170.     g.setColor(c1);
  171.     for (; (a1+a2) <= 360; a1 = a1+a2, a2 += 1) {
  172.     if (g.getColor() == c1) {
  173.         g.setColor(c2);
  174.         }
  175.     else {
  176.         g.setColor(c1);
  177.         }
  178.  
  179.     if (filled) {
  180.         g.fillArc(x, y, w, h, a1, a2);
  181.         }
  182.     else {
  183.         g.drawArc(x, y, w, h, a1, a2);
  184.         }
  185.  
  186.     progress = a1+a2;
  187.       }  // end for
  188.  
  189.     if (progress != 360) {
  190.           if (filled) {
  191.         g.fillArc(x, y, w, h, a1, 360 - progress);
  192.       }
  193.     else {
  194.         g.drawArc(x, y, w, h, a1, 360 - progress);
  195.         }
  196.       }  // end if
  197.   }  // end arcSteps()
  198.  
  199.     public void paint(Graphics g) {
  200.         Rectangle r = getBounds();
  201.  
  202.         arcSteps(g, 3, 0, 0, r.width, r.height, Color.orange, Color.blue);
  203.  
  204.         arcSteps(g,
  205.                  2,
  206.                  r.width / 4,
  207.                  r.height / 4,
  208.                  r.width / 2,
  209.                  r.height / 2,
  210.                  Color.yellow,
  211.                  Color.green);
  212.  
  213.         arcSteps(g,
  214.                  1,
  215.                  (r.width  * 3) / 8,
  216.                  (r.height * 3) / 8,
  217.                  r.width / 4,
  218.                  r.height / 4,
  219.                  Color.magenta,
  220.                  Color.white);
  221.  
  222.   }  // end paint()
  223. }   // end class ArcDegreePanel
  224.  
  225.  
  226. class ArcPanel extends Panel {
  227.     boolean filled;
  228.  
  229.     public ArcPanel(boolean filled) {
  230.     this.filled = filled;
  231.   }
  232.  
  233.   public void paint(Graphics g)
  234.   {
  235.     Rectangle r = getBounds();
  236.  
  237.     g.setColor(Color.yellow);
  238.     if (filled)
  239.       {
  240.     g.fillArc(0, 0, r.width, r.height, 0, 45);
  241.       }
  242.     else
  243.       {
  244.     g.drawArc(0, 0, r.width, r.height, 0, 45);
  245.       }
  246.  
  247.     g.setColor(Color.green);
  248.     if (filled)
  249.       {
  250.     g.fillArc(0, 0, r.width, r.height, 90, -45);
  251.       }
  252.     else
  253.       {
  254.     g.drawArc(0, 0, r.width, r.height, 90, -45);
  255.       }
  256.  
  257.     g.setColor(Color.orange);
  258.     if (filled)
  259.       {
  260.     g.fillArc(0, 0, r.width, r.height, 135, -45);
  261.       }
  262.     else
  263.       {
  264.     g.drawArc(0, 0, r.width, r.height, 135, -45);
  265.       }
  266.  
  267.     g.setColor(Color.magenta);
  268.  
  269.     if (filled)
  270.       {
  271.     g.fillArc(0, 0, r.width, r.height, -225, 45);
  272.       }
  273.     else
  274.       {
  275.     g.drawArc(0, 0, r.width, r.height, -225, 45);
  276.       }
  277.  
  278.     g.setColor(Color.yellow);
  279.     if (filled)
  280.       {
  281.     g.fillArc(0, 0, r.width, r.height, 225, -45);
  282.       }
  283.     else
  284.       {
  285.     g.drawArc(0, 0, r.width, r.height, 225, -45);
  286.       }
  287.  
  288.     g.setColor(Color.green);
  289.     if (filled)
  290.       {
  291.     g.fillArc(0, 0, r.width, r.height, -135, 45);
  292.       }
  293.     else
  294.       {
  295.     g.drawArc(0, 0, r.width, r.height, -135, 45);
  296.       }
  297.  
  298.     g.setColor(Color.orange);
  299.     if (filled)
  300.       {
  301.     g.fillArc(0, 0, r.width, r.height, -45, -45);
  302.       }
  303.     else
  304.       {
  305.     g.drawArc(0, 0, r.width, r.height, -45, -45);
  306.       }
  307.  
  308.     g.setColor(Color.magenta);
  309.     if (filled)
  310.       {
  311.     g.fillArc(0, 0, r.width, r.height, 315, 45);
  312.       }
  313.     else
  314.       {
  315.     g.drawArc(0, 0, r.width, r.height, 315, 45);
  316.       }
  317.  
  318.   }  // end paint()
  319.  
  320. }   // end class ArcPanel
  321.  
  322.  
  323. abstract class Shape
  324. {
  325.   abstract void draw(Graphics g, int x, int y, int w, int h);
  326.   abstract void fill(Graphics g, int x, int y, int w, int h);
  327. }
  328.  
  329.  
  330. class RectShape extends Shape
  331. {
  332.   void draw(Graphics g, int x, int y, int w, int h)
  333.   {
  334.     g.drawRect(x, y, w, h);
  335.   }
  336.  
  337.   void fill(Graphics g, int x, int y, int w, int h)
  338.   {
  339.     g.fillRect(x, y, w, h);
  340.   }
  341. }
  342.  
  343.  
  344. class OvalShape extends Shape
  345. {
  346.   void draw(Graphics g, int x, int y, int w, int h)
  347.   {
  348.     g.drawOval(x, y, w, h);
  349.   }
  350.  
  351.   void fill(Graphics g, int x, int y, int w, int h)
  352.   {
  353.     g.fillOval(x, y, w, h);
  354.   }
  355. }
  356.  
  357.  
  358. class RoundRectShape extends Shape
  359. {
  360.   void draw(Graphics g, int x, int y, int w, int h)
  361.   {
  362.     g.drawRoundRect(x, y, w, h, 10, 10);
  363.   }
  364.  
  365.   void fill(Graphics g, int x, int y, int w, int h)
  366.   {
  367.     g.fillRoundRect(x, y, w, h, 10, 10);
  368.   }
  369. }
  370.  
  371. class PolygonShape extends Shape
  372. {
  373.   // class variables
  374.   Polygon p;
  375.   Polygon pBase;
  376.  
  377.   public PolygonShape()
  378.   {
  379.     pBase = new Polygon();
  380.     pBase.addPoint(0, 0);
  381.     pBase.addPoint(10, 0);
  382.     pBase.addPoint(5, 15);
  383.     pBase.addPoint(10, 20);
  384.     pBase.addPoint(5, 20);
  385.     pBase.addPoint(0, 10);
  386.     pBase.addPoint(0, 0);
  387.   }
  388.  
  389.   void scalePolygon(float w, float h)
  390.   {
  391.     p = new Polygon();
  392.     for (int i = 0; i < pBase.npoints; ++i)
  393.       {
  394.     p.addPoint( (int) (pBase.xpoints[i] * w),
  395.             (int) (pBase.ypoints[i] * h) );
  396.       }
  397.  
  398.   }
  399.  
  400.   void draw(Graphics g, int x, int y, int w, int h)
  401.   {
  402.     Graphics ng = g.create();
  403.     try {
  404.         ng.translate(x, y);
  405.     scalePolygon( (float) ( (float) w / (float) 10 ),
  406.               (float) ( (float) h / (float) 20 ) );
  407.     ng.drawPolygon(p);
  408.     } finally {
  409.         ng.dispose();
  410.     }
  411.   }
  412.  
  413.   void fill(Graphics g, int x, int y, int w, int h)
  414.   {
  415.     Graphics ng = g.create();
  416.     try {
  417.         ng.translate(x, y);
  418.     scalePolygon( (float) ( (float) w / (float) 10 ),
  419.               (float) ( (float) h / (float) 20 ) );
  420.     ng.fillPolygon(p);
  421.     } finally {
  422.         ng.dispose();
  423.     }
  424.   }
  425. }
  426.  
  427.  
  428. class ShapeTest extends Panel
  429. {
  430.   Shape   shape;
  431.   int        step;
  432.  
  433.   public ShapeTest(Shape shape, int step)
  434.   {
  435.     this.shape = shape;
  436.     this.step = step;
  437.   }
  438.  
  439.   public ShapeTest(Shape shape)
  440.   {
  441.     this(shape, 10);
  442.   }
  443.  
  444.     public void paint(Graphics g) {
  445.         Rectangle bounds = getBounds();
  446.  
  447.         int cx, cy, cw, ch;
  448.  
  449.         Color color;
  450.  
  451.         for (color=Color.red,
  452.                  cx=bounds.x,
  453.                  cy=bounds.y,
  454.                  cw=bounds.width / 2,
  455.                  ch=bounds.height;
  456.              cw > 0 && ch > 0;
  457.  
  458.              cx+=step,
  459.                  cy += step,
  460.                  cw -= (step * 2),
  461.                  ch -= (step * 2),
  462.                  color=ColorUtils.darker(color, 0.9) ) {
  463.             g.setColor(color);
  464.             shape.draw(g, cx, cy, cw, ch);
  465.         }
  466.  
  467.         for (cx=bounds.x + bounds.width / 2,
  468.                  cy=bounds.y,
  469.                  cw=bounds.width / 2, ch=bounds.height;
  470.              cw > 0 && ch > 0;
  471.  
  472.              cx+=step,
  473.                  cy += step,
  474.                  cw -= (step * 2),
  475.                  ch -= (step * 2) ) {
  476.             if (g.getColor() == Color.red) {
  477.                 g.setColor(Color.blue);
  478.             }
  479.             else {
  480.                 g.setColor(Color.red);
  481.             }
  482.  
  483.             shape.fill(g, cx, cy, cw, ch);
  484.         }  // end for
  485.     }  // end paint()
  486. }   // end class ShapeTest
  487.  
  488. class ColorUtils {
  489.     static Color brighter(Color c, double factor) {
  490.         return new Color( Math.min((int)(c.getRed()  *(1/factor)), 255),
  491.                           Math.min((int)(c.getGreen()*(1/factor)), 255),
  492.                           Math.min((int)(c.getBlue() *(1/factor)), 255) );
  493.     }
  494.  
  495.     static Color darker(Color c, double factor) {
  496.         return new Color( Math.max((int)(c.getRed()  *factor), 0),
  497.                           Math.max((int)(c.getGreen()*factor), 0),
  498.                           Math.max((int)(c.getBlue() *factor), 0) );
  499.     }
  500. }
  501.  
  502.  
  503.  
  504.  
  505.  
  506.  
  507.  
  508.  
  509.  
  510.  
  511.  
  512.  
  513.  
  514.  
  515.  
  516.  
  517.  
  518.  
  519.  
  520.  
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.  
  533.  
  534.  
  535.  
  536.  
  537.  
  538.  
  539.  
  540.  
  541.  
  542.  
  543.  
  544.  
  545.  
  546.  
  547.  
  548.  
  549.  
  550.  
  551.  
  552.  
  553.  
  554.  
  555.  
  556.  
  557.  
  558.  
  559.  
  560.  
  561.  
  562.  
  563.  
  564.  
  565.  
  566.  
  567.  
  568.  
  569.  
  570.  
  571.  
  572.  
  573.  
  574.  
  575.  
  576.  
  577.  
  578.  
  579.  
  580.  
  581.  
  582.  
  583.  
  584.  
  585.  
  586.  
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598.  
  599.  
  600.  
  601.  
  602.  
  603.