home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / DEMO / APPLETS / GraphicsTest / GraphicsTest.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  10.8 KB  |  624 lines

  1. /*
  2.  * @(#)GraphicsTest.java    1.4 96/12/06
  3.  *
  4.  * Copyright (c) 1994-1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  7.  * modify and redistribute this software in source and binary code form,
  8.  * provided that i) this copyright notice and license appear on all copies of
  9.  * the software; and ii) Licensee does not utilize the software in a manner
  10.  * which is disparaging to Sun.
  11.  *
  12.  * This software is provided "AS IS," without a warranty of any kind. ALL
  13.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  14.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  15.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  16.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  17.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  18.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  19.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  20.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  21.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  22.  * POSSIBILITY OF SUCH DAMAGES.
  23.  *
  24.  * This software is not designed or intended for use in on-line control of
  25.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  26.  * the design, construction, operation or maintenance of any nuclear
  27.  * facility. Licensee represents and warrants that it will not use or
  28.  * redistribute the Software for such purposes.
  29.  */
  30.  
  31. import java.awt.*;
  32. import java.util.*;
  33. import java.awt.event.*;
  34. import java.applet.Applet;
  35.  
  36. class GraphicsPanel extends Panel {
  37.   ActionListener al;
  38.   ItemListener il;
  39.   
  40.   public GraphicsCards cards;
  41.   
  42.   GraphicsPanel(EventListener listener) {
  43.     al = (ActionListener)listener;
  44.     il = (ItemListener)listener;
  45.     
  46.     setLayout(new BorderLayout());
  47.     
  48.     add("Center", cards = new GraphicsCards());
  49.     
  50.     Panel p = new Panel();
  51.     //p.setLayout(new BorderLayout());
  52.     
  53.     Button b = new Button("next");
  54.     b.addActionListener(al);
  55.     p.add(b);
  56.     
  57.     b = new Button("previous");
  58.     b.addActionListener(al);
  59.     p.add(b);
  60.     
  61.     p.add(new Label("go to:", Label.RIGHT));
  62.     
  63.     Choice c = new Choice();
  64.     c.addItemListener(il);
  65.     p.add(c);
  66.     
  67.     c.addItem("Arc");
  68.     c.addItem("Oval");
  69.     c.addItem("Polygon");
  70.     c.addItem("Rect");
  71.     c.addItem("RoundRect");
  72.     
  73.     add("North", p);
  74.     
  75.     setSize(400, 400);
  76.   }
  77.   
  78.   public Dimension getPreferredSize() {
  79.     return new Dimension(200, 100);
  80.   }
  81. }
  82.  
  83. public class GraphicsTest extends Applet 
  84. implements ActionListener, ItemListener
  85. {
  86.   
  87.   GraphicsPanel mainPanel;
  88.   
  89.   public void init() 
  90.   {
  91.     setLayout(new BorderLayout());
  92.     add("Center", mainPanel = new GraphicsPanel(this));
  93.   }  // end init()
  94.  
  95.   public void actionPerformed(ActionEvent e)
  96.   {
  97.     String arg = e.getActionCommand();
  98.  
  99.     if ("next".equals(arg)) 
  100.       {
  101.     ((CardLayout)mainPanel.cards.getLayout()).next(mainPanel.cards);
  102.       } 
  103.     else if ("previous".equals(arg)) 
  104.       {
  105.     ((CardLayout)mainPanel.cards.getLayout()).previous(mainPanel.cards);
  106.       }
  107.   }
  108.   
  109.   public void itemStateChanged(ItemEvent e)
  110.   {
  111.     ((CardLayout)mainPanel.cards.getLayout()).show(mainPanel.cards,(String)e.getItem());
  112.   }
  113.   
  114.   public static void main(String args[]) 
  115.   {
  116.     AppletFrame.startApplet("GraphicsTest", "Graphics Test", args);
  117.   }
  118.   
  119.   public String getAppletInfo() {
  120.     return "An interactive demonstration of some graphics.";
  121.   }
  122. }   // end class GraphicsTest
  123.  
  124.  
  125. class GraphicsCards extends Panel 
  126. {
  127.   public GraphicsCards() 
  128.   {
  129.     setLayout(new CardLayout());
  130.     add("Arc", new ArcCard());
  131.     add("Oval", new ShapeTest( new OvalShape() ) );
  132.     add("Polygon", new ShapeTest( new PolygonShape() ) );
  133.     add("Rect", new ShapeTest( new RectShape() ) );
  134.     add("RoundRect", new ShapeTest( new RoundRectShape() ) );
  135.   }
  136. }   // end class GraphicsCards
  137.  
  138.  
  139. class ArcCard extends Panel 
  140. {
  141.   public ArcCard() 
  142.   {
  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. {
  154.   // class variables
  155.   boolean filled;
  156.   
  157.   public ArcDegreePanel(boolean filled) 
  158.   {
  159.     this.filled = filled;
  160.   }
  161.   
  162.   void arcSteps(Graphics g, 
  163.         int step, 
  164.         int x, 
  165.         int y, 
  166.         int w, 
  167.         int h, 
  168.         Color c1,  
  169.         Color c2) 
  170.   {
  171.     int a1 = 0;
  172.     int a2 = step;
  173.     int progress = 0;
  174.     g.setColor(c1);
  175.     for (; (a1+a2) <= 360; a1 = a1+a2, a2 += 1 ) 
  176.       {
  177.     if (g.getColor() == c1) 
  178.       {
  179.         g.setColor(c2);
  180.       } 
  181.     else 
  182.       {
  183.         g.setColor(c1);
  184.       }
  185.     
  186.     if (filled) 
  187.       {
  188.         g.fillArc(x, y, w, h, a1, a2);
  189.       } 
  190.     else 
  191.       {
  192.         g.drawArc(x, y, w, h, a1, a2);
  193.       }
  194.     
  195.     progress = a1+a2;
  196.       }  // end for
  197.     
  198.     if (progress != 360) 
  199.       {
  200.     if (filled) 
  201.       {
  202.         g.fillArc(x, y, w, h, a1, 360 - progress);
  203.       } 
  204.     else 
  205.       {
  206.         g.drawArc(x, y, w, h, a1, 360 - progress);
  207.       }
  208.       }  // end if
  209.   }  // end arcSteps()
  210.   
  211.   public void paint(Graphics g)
  212.   {
  213.     Rectangle r = getBounds();
  214.     
  215.     arcSteps(g, 3, 0, 0, r.width, r.height, Color.orange, Color.blue);
  216.     
  217.     arcSteps(g, 
  218.          2, 
  219.          r.width / 4, 
  220.          r.height / 4, 
  221.          r.width / 2, 
  222.          r.height / 2, 
  223.          Color.yellow, 
  224.          Color.green);
  225.     
  226.     arcSteps(g, 
  227.          1, 
  228.          (r.width  * 3) / 8, 
  229.          (r.height * 3) / 8, 
  230.          r.width / 4, 
  231.          r.height / 4, 
  232.          Color.magenta, 
  233.          Color.white);
  234.     
  235.   }  // end paint()
  236. }   // end class ArcDegreePanel
  237.  
  238.  
  239. class ArcPanel extends Panel 
  240. {
  241.   // class variables
  242.   boolean filled;
  243.   
  244.   public ArcPanel(boolean filled) 
  245.   {
  246.     this.filled = filled;
  247.   }
  248.   
  249.   public void paint(Graphics g) 
  250.   {
  251.     Rectangle r = getBounds();
  252.     
  253.     g.setColor(Color.yellow);
  254.     if (filled) 
  255.       {
  256.     g.fillArc(0, 0, r.width, r.height, 0, 45);
  257.       } 
  258.     else 
  259.       {
  260.     g.drawArc(0, 0, r.width, r.height, 0, 45);
  261.       }
  262.     
  263.     g.setColor(Color.green);
  264.     if (filled) 
  265.       {
  266.     g.fillArc(0, 0, r.width, r.height, 90, -45);
  267.       } 
  268.     else 
  269.       {
  270.     g.drawArc(0, 0, r.width, r.height, 90, -45);
  271.       }
  272.     
  273.     g.setColor(Color.orange);
  274.     if (filled) 
  275.       {
  276.     g.fillArc(0, 0, r.width, r.height, 135, -45);
  277.       } 
  278.     else 
  279.       {
  280.     g.drawArc(0, 0, r.width, r.height, 135, -45);
  281.       }
  282.     
  283.     g.setColor(Color.magenta);
  284.     
  285.     if (filled) 
  286.       {
  287.     g.fillArc(0, 0, r.width, r.height, -225, 45);
  288.       } 
  289.     else 
  290.       {
  291.     g.drawArc(0, 0, r.width, r.height, -225, 45);
  292.       }
  293.     
  294.     g.setColor(Color.yellow);
  295.     if (filled) 
  296.       {
  297.     g.fillArc(0, 0, r.width, r.height, 225, -45);
  298.       } 
  299.     else  
  300.       {
  301.     g.drawArc(0, 0, r.width, r.height, 225, -45);
  302.       }
  303.     
  304.     g.setColor(Color.green);
  305.     if (filled) 
  306.       {
  307.     g.fillArc(0, 0, r.width, r.height, -135, 45);
  308.       } 
  309.     else 
  310.       {
  311.     g.drawArc(0, 0, r.width, r.height, -135, 45);
  312.       }
  313.     
  314.     g.setColor(Color.orange);
  315.     if (filled) 
  316.       {
  317.     g.fillArc(0, 0, r.width, r.height, -45, -45);
  318.       } 
  319.     else 
  320.       {
  321.     g.drawArc(0, 0, r.width, r.height, -45, -45);
  322.       }
  323.     
  324.     g.setColor(Color.magenta);
  325.     if (filled) 
  326.       {
  327.     g.fillArc(0, 0, r.width, r.height, 315, 45);
  328.       } 
  329.     else 
  330.       {
  331.     g.drawArc(0, 0, r.width, r.height, 315, 45);
  332.       }
  333.     
  334.   }  // end paint()
  335.   
  336. }   // end class ArcPanel
  337.  
  338.  
  339. abstract class Shape 
  340. {
  341.   abstract void draw(Graphics g, int x, int y, int w, int h);
  342.   abstract void fill(Graphics g, int x, int y, int w, int h);
  343. }
  344.  
  345.  
  346. class RectShape extends Shape 
  347. {
  348.   void draw(Graphics g, int x, int y, int w, int h) 
  349.   {
  350.     g.drawRect(x, y, w, h);
  351.   }
  352.   
  353.   void fill(Graphics g, int x, int y, int w, int h) 
  354.   {
  355.     g.fillRect(x, y, w, h);
  356.   }
  357. }
  358.  
  359.  
  360. class OvalShape extends Shape 
  361. {
  362.   void draw(Graphics g, int x, int y, int w, int h) 
  363.   {
  364.     g.drawOval(x, y, w, h);
  365.   }
  366.   
  367.   void fill(Graphics g, int x, int y, int w, int h) 
  368.   {
  369.     g.fillOval(x, y, w, h);
  370.   }
  371. }
  372.  
  373.  
  374. class RoundRectShape extends Shape 
  375. {
  376.   void draw(Graphics g, int x, int y, int w, int h) 
  377.   {
  378.     g.drawRoundRect(x, y, w, h, 10, 10);
  379.   }
  380.   
  381.   void fill(Graphics g, int x, int y, int w, int h) 
  382.   {
  383.     g.fillRoundRect(x, y, w, h, 10, 10);
  384.   }
  385. }
  386.  
  387. class PolygonShape extends Shape 
  388. {
  389.   // class variables
  390.   Polygon p;
  391.   Polygon pBase;
  392.   
  393.   public PolygonShape() 
  394.   {
  395.     pBase = new Polygon();
  396.     pBase.addPoint(0, 0);
  397.     pBase.addPoint(10, 0);
  398.     pBase.addPoint(5, 15);
  399.     pBase.addPoint(10, 20);
  400.     pBase.addPoint(5, 20);
  401.     pBase.addPoint(0, 10);
  402.     pBase.addPoint(0, 0);
  403.   }
  404.   
  405.   void scalePolygon(float w, float h)
  406.   {
  407.     p = new Polygon();
  408.     for (int i = 0; i < pBase.npoints; ++i)
  409.       {
  410.     p.addPoint( (int) (pBase.xpoints[i] * w), 
  411.             (int) (pBase.ypoints[i] * h) );
  412.       }
  413.     
  414.   }
  415.   
  416.   void draw(Graphics g, int x, int y, int w, int h) 
  417.   {
  418.     Graphics ng = g.create();
  419.     ng.translate(x, y);
  420.     scalePolygon( (float) ( (float) w / (float) 10 ), 
  421.           (float) ( (float) h / (float) 20 ) );
  422.     ng.drawPolygon(p);
  423.   }
  424.   
  425.   void fill(Graphics g, int x, int y, int w, int h) 
  426.   {
  427.     Graphics ng = g.create();
  428.     ng.translate(x, y);
  429.     scalePolygon( (float) ( (float) w / (float) 10 ), 
  430.           (float) ( (float) h / (float) 20 ) );
  431.     ng.fillPolygon(p);
  432.   }
  433. }
  434.  
  435.  
  436. class ShapeTest extends Panel 
  437. {
  438.   Shape   shape;
  439.   int        step;
  440.   
  441.   public ShapeTest(Shape shape, int step) 
  442.   {
  443.     this.shape = shape;
  444.     this.step = step;
  445.   }
  446.   
  447.   public ShapeTest(Shape shape) 
  448.   {
  449.     this(shape, 10);
  450.   }
  451.   
  452.   public void paint(Graphics g) 
  453.   {
  454.     Rectangle bounds = getBounds();
  455.     
  456.     int cx, cy, cw, ch;
  457.     
  458.     Color color;
  459.     
  460.     for (color=Color.red, 
  461.        cx=bounds.x, 
  462.        cy=bounds.y, 
  463.        cw=bounds.width / 2, 
  464.        ch=bounds.height ; 
  465.      
  466.      cw > 0 && ch > 0 ;
  467.      
  468.      cx+=step, 
  469.        cy += step, 
  470.        cw -= (step * 2), 
  471.        ch -= (step * 2),
  472.        color=ColorUtils.darker(color, 0.9) ) 
  473.       {
  474.     g.setColor(color);
  475.     shape.draw(g, cx, cy, cw, ch);
  476.       }
  477.     
  478.     for ( cx=bounds.x + bounds.width / 2, 
  479.         cy=bounds.y,
  480.         cw=bounds.width / 2, ch=bounds.height ; 
  481.       
  482.       cw > 0 && ch > 0;
  483.       
  484.       cx+=step, 
  485.         cy += step, 
  486.         cw -= (step * 2),
  487.         ch -= (step * 2) ) 
  488.       {
  489.     if (g.getColor() == Color.red) 
  490.       {
  491.         g.setColor(Color.blue);
  492.       } 
  493.     else 
  494.       {
  495.         g.setColor(Color.red);
  496.       }
  497.     
  498.     shape.fill(g, cx, cy, cw, ch);
  499.       }  // end for
  500.     
  501.   }  // end paint()
  502.   
  503. }   // end class ShapeTest
  504.  
  505.  
  506. class ColorUtils 
  507. {
  508.   static Color brighter(Color c, double factor) 
  509.   {
  510.     return new Color( Math.min((int)(c.getRed()  *(1/factor)), 255), 
  511.               Math.min((int)(c.getGreen()*(1/factor)), 255),
  512.               Math.min((int)(c.getBlue() *(1/factor)), 255) );
  513.   }
  514.   
  515.   static Color darker(Color c, double factor) 
  516.   {
  517.     return new Color( Math.max((int)(c.getRed()  *factor), 0), 
  518.               Math.max((int)(c.getGreen()*factor), 0),
  519.               Math.max((int)(c.getBlue() *factor), 0) );
  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.  
  604.  
  605.  
  606.  
  607.  
  608.  
  609.  
  610.  
  611.  
  612.  
  613.  
  614.  
  615.  
  616.  
  617.  
  618.  
  619.  
  620.  
  621.  
  622.  
  623.  
  624.