home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / WDESAMPL.BIN / GraphicsTest.java < prev    next >
Text File  |  1996-12-06  |  12KB  |  487 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.applet.Applet;
  33.  
  34. public class GraphicsTest extends Applet 
  35. {
  36.     // class Variables
  37.     GraphicsCards cards;
  38.  
  39.     public void init() 
  40.     {
  41.        setLayout(new BorderLayout());
  42.        add("Center", cards = new GraphicsCards());
  43.        Panel p = new Panel();
  44.        p.add(new Button("next"));
  45.        p.add(new Button("previous"));
  46.        p.add(new Label("go to:", Label.RIGHT));
  47.  
  48.        Choice c;
  49.  
  50.        p.add(c = new Choice());
  51.        c.addItem("Arc");
  52.        c.addItem("Oval");
  53.        c.addItem("Polygon");
  54.        c.addItem("Rect");
  55.        c.addItem("RoundRect");
  56.        add("North", p);
  57.  
  58.        resize(400, 400);
  59.     }  // end init()
  60.  
  61.     public boolean action(Event evt, Object arg) 
  62.     {
  63.        if (evt.target instanceof Choice) 
  64.        {
  65.           ( (CardLayout) cards.getLayout() ).show(cards,(String) arg );
  66.        }
  67.        else 
  68.        {
  69.           if ("next".equals(arg)) 
  70.           {
  71.              ( (CardLayout) cards.getLayout() ).next(cards);
  72.           } 
  73.           else if ("previous".equals(arg)) 
  74.           {
  75.              ( (CardLayout) cards.getLayout() ).previous(cards);
  76.           }
  77.        }
  78.  
  79.        return true;
  80.     }  // end action()
  81.  
  82.     public static void main(String args[]) 
  83.     {
  84.        AppletFrame.startApplet("GraphicsTest", "Graphics Test", args);
  85.     }
  86.  
  87. }   // end class GraphicsTest
  88.  
  89.     
  90. class GraphicsCards extends Panel 
  91. {
  92.     public GraphicsCards() 
  93.     {
  94.        setLayout(new CardLayout());
  95.        add("Arc", new ArcCard());
  96.        add("Oval", new ShapeTest( new OvalShape() ) );
  97.        add("Polygon", new ShapeTest( new PolygonShape() ) );
  98.        add("Rect", new ShapeTest( new RectShape() ) );
  99.        add("RoundRect", new ShapeTest( new RoundRectShape() ) );
  100.     }
  101. }   // end class GraphicsCards
  102.  
  103.  
  104. class ArcCard extends Panel 
  105. {
  106.     public ArcCard() 
  107.     {
  108.        setLayout(new GridLayout(0, 2));
  109.        add(new ArcPanel(true));
  110.        add(new ArcPanel(false));
  111.        add(new ArcDegreePanel(true));
  112.        add(new ArcDegreePanel(false));
  113.     }
  114. }   // end class ArcCard
  115.  
  116.  
  117. class ArcDegreePanel extends Panel 
  118. {
  119.     // class variables
  120.     boolean filled;
  121.  
  122.     public ArcDegreePanel(boolean filled) 
  123.     {
  124.        this.filled = filled;
  125.     }
  126.  
  127.     void arcSteps(Graphics g, 
  128.                   int step, 
  129.                   int x, 
  130.                   int y, 
  131.                   int w, 
  132.                   int h, 
  133.                   Color c1,  
  134.                   Color c2) 
  135.     {
  136.        int a1 = 0;
  137.        int a2 = step;
  138.        int progress = 0;
  139.        g.setColor(c1);
  140.        for (; (a1+a2) <= 360; a1 = a1+a2, a2 += 1 ) 
  141.        {
  142.           if (g.getColor() == c1) 
  143.           {
  144.              g.setColor(c2);
  145.           } 
  146.           else 
  147.           {
  148.              g.setColor(c1);
  149.           }
  150.        
  151.           if (filled) 
  152.           {
  153.              g.fillArc(x, y, w, h, a1, a2);
  154.           } 
  155.           else 
  156.           {
  157.              g.drawArc(x, y, w, h, a1, a2);
  158.           }
  159.        
  160.           progress = a1+a2;
  161.        }  // end for
  162.  
  163.        if (progress != 360) 
  164.        {
  165.           if (filled) 
  166.           {
  167.              g.fillArc(x, y, w, h, a1, 360 - progress);
  168.           } 
  169.           else 
  170.           {
  171.              g.drawArc(x, y, w, h, a1, 360 - progress);
  172.           }
  173.        }  // end if
  174.     }  // end arcSteps()
  175.  
  176.     public void paint(Graphics g)
  177.     {
  178.        Rectangle r = bounds();
  179.  
  180.        arcSteps(g, 3, 0, 0, r.width, r.height, Color.orange, Color.blue);
  181.  
  182.        arcSteps(g, 
  183.                 2, 
  184.                 r.width / 4, 
  185.                 r.height / 4, 
  186.                 r.width / 2, 
  187.                 r.height / 2, 
  188.                 Color.yellow, 
  189.                 Color.green);
  190.  
  191.        arcSteps(g, 
  192.                 1, 
  193.                 (r.width  * 3) / 8, 
  194.                 (r.height * 3) / 8, 
  195.                 r.width / 4, 
  196.                 r.height / 4, 
  197.                 Color.magenta, 
  198.                 Color.white);
  199.  
  200.     }  // end paint()
  201. }   // end class ArcDegreePanel
  202.  
  203.  
  204. class ArcPanel extends Panel 
  205. {
  206.     // class variables
  207.     boolean filled;
  208.  
  209.     public ArcPanel(boolean filled) 
  210.     {
  211.        this.filled = filled;
  212.     }
  213.  
  214.     public void paint(Graphics g) 
  215.     {
  216.        Rectangle r = bounds();
  217.  
  218.        g.setColor(Color.yellow);
  219.        if (filled) 
  220.        {
  221.           g.fillArc(0, 0, r.width, r.height, 0, 45);
  222.        } 
  223.        else 
  224.        {
  225.           g.drawArc(0, 0, r.width, r.height, 0, 45);
  226.        }
  227.  
  228.        g.setColor(Color.green);
  229.        if (filled) 
  230.        {
  231.           g.fillArc(0, 0, r.width, r.height, 90, -45);
  232.        } 
  233.        else 
  234.        {
  235.           g.drawArc(0, 0, r.width, r.height, 90, -45);
  236.        }
  237.     
  238.        g.setColor(Color.orange);
  239.        if (filled) 
  240.        {
  241.           g.fillArc(0, 0, r.width, r.height, 135, -45);
  242.        } 
  243.        else 
  244.        {
  245.           g.drawArc(0, 0, r.width, r.height, 135, -45);
  246.        }
  247.  
  248.        g.setColor(Color.magenta);
  249.  
  250.        if (filled) 
  251.        {
  252.           g.fillArc(0, 0, r.width, r.height, -225, 45);
  253.        } 
  254.        else 
  255.        {
  256.           g.drawArc(0, 0, r.width, r.height, -225, 45);
  257.        }
  258.  
  259.        g.setColor(Color.yellow);
  260.        if (filled) 
  261.        {
  262.           g.fillArc(0, 0, r.width, r.height, 225, -45);
  263.        } 
  264.        else  
  265.        {
  266.           g.drawArc(0, 0, r.width, r.height, 225, -45);
  267.        }
  268.  
  269.        g.setColor(Color.green);
  270.        if (filled) 
  271.        {
  272.           g.fillArc(0, 0, r.width, r.height, -135, 45);
  273.        } 
  274.        else 
  275.        {
  276.           g.drawArc(0, 0, r.width, r.height, -135, 45);
  277.        }
  278.  
  279.        g.setColor(Color.orange);
  280.        if (filled) 
  281.        {
  282.           g.fillArc(0, 0, r.width, r.height, -45, -45);
  283.        } 
  284.        else 
  285.        {
  286.           g.drawArc(0, 0, r.width, r.height, -45, -45);
  287.        }
  288.  
  289.        g.setColor(Color.magenta);
  290.        if (filled) 
  291.        {
  292.           g.fillArc(0, 0, r.width, r.height, 315, 45);
  293.        } 
  294.        else 
  295.        {
  296.           g.drawArc(0, 0, r.width, r.height, 315, 45);
  297.        }
  298.  
  299.     }  // end paint()
  300.  
  301. }   // end class ArcPanel
  302.  
  303.  
  304. abstract class Shape 
  305. {
  306.     abstract void draw(Graphics g, int x, int y, int w, int h);
  307.     abstract void fill(Graphics g, int x, int y, int w, int h);
  308. }
  309.  
  310.  
  311. class RectShape extends Shape 
  312. {
  313.     void draw(Graphics g, int x, int y, int w, int h) 
  314.     {
  315.        g.drawRect(x, y, w, h);
  316.     }
  317.     
  318.     void fill(Graphics g, int x, int y, int w, int h) 
  319.     {
  320.        g.fillRect(x, y, w, h);
  321.     }
  322. }
  323.  
  324.  
  325. class OvalShape extends Shape 
  326. {
  327.     void draw(Graphics g, int x, int y, int w, int h) 
  328.     {
  329.        g.drawOval(x, y, w, h);
  330.     }
  331.     
  332.     void fill(Graphics g, int x, int y, int w, int h) 
  333.     {
  334.        g.fillOval(x, y, w, h);
  335.     }
  336. }
  337.  
  338.  
  339. class RoundRectShape extends Shape 
  340. {
  341.     void draw(Graphics g, int x, int y, int w, int h) 
  342.     {
  343.        g.drawRoundRect(x, y, w, h, 10, 10);
  344.     }
  345.  
  346.     void fill(Graphics g, int x, int y, int w, int h) 
  347.     {
  348.        g.fillRoundRect(x, y, w, h, 10, 10);
  349.     }
  350. }
  351.  
  352. class PolygonShape extends Shape 
  353. {
  354.     // class variables
  355.     Polygon p;
  356.     Polygon pBase;
  357.  
  358.     public PolygonShape() 
  359.     {
  360.        pBase = new Polygon();
  361.        pBase.addPoint(0, 0);
  362.        pBase.addPoint(10, 0);
  363.        pBase.addPoint(5, 15);
  364.        pBase.addPoint(10, 20);
  365.        pBase.addPoint(5, 20);
  366.        pBase.addPoint(0, 10);
  367.        pBase.addPoint(0, 0);
  368.     }
  369.  
  370.     void scalePolygon(float w, float h)
  371.     {
  372.        p = new Polygon();
  373.        for (int i = 0; i < pBase.npoints; ++i)
  374.        {
  375.           p.addPoint( (int) (pBase.xpoints[i] * w), 
  376.                       (int) (pBase.ypoints[i] * h) );
  377.        }
  378.  
  379.     }
  380.  
  381.     void draw(Graphics g, int x, int y, int w, int h) 
  382.     {
  383.        Graphics ng = g.create();
  384.        ng.translate(x, y);
  385.        scalePolygon( (float) ( (float) w / (float) 10 ), 
  386.                      (float) ( (float) h / (float) 20 ) );
  387.        ng.drawPolygon(p);
  388.     }
  389.  
  390.     void fill(Graphics g, int x, int y, int w, int h) 
  391.     {
  392.        Graphics ng = g.create();
  393.        ng.translate(x, y);
  394.        scalePolygon( (float) ( (float) w / (float) 10 ), 
  395.                      (float) ( (float) h / (float) 20 ) );
  396.        ng.fillPolygon(p);
  397.     }
  398. }
  399.  
  400.  
  401. class ShapeTest extends Panel 
  402. {
  403.     Shape   shape;
  404.     int     step;
  405.  
  406.     public ShapeTest(Shape shape, int step) 
  407.     {
  408.        this.shape = shape;
  409.        this.step = step;
  410.     }
  411.  
  412.     public ShapeTest(Shape shape) 
  413.     {
  414.        this(shape, 10);
  415.     }
  416.  
  417.     public void paint(Graphics g) 
  418.     {
  419.        Rectangle bounds = bounds();
  420.  
  421.        int cx, cy, cw, ch;
  422.  
  423.        Color color;
  424.    
  425.        for (color=Color.red, 
  426.             cx=bounds.x, 
  427.             cy=bounds.y, 
  428.             cw=bounds.width / 2, 
  429.             ch=bounds.height ; 
  430.  
  431.             cw > 0 && ch > 0 ;
  432.  
  433.             cx+=step, 
  434.             cy += step, 
  435.             cw -= (step * 2), 
  436.             ch -= (step * 2),
  437.             color=ColorUtils.darker(color, 0.9) ) 
  438.        {
  439.           g.setColor(color);
  440.           shape.draw(g, cx, cy, cw, ch);
  441.        }
  442.  
  443.        for ( cx=bounds.x + bounds.width / 2, 
  444.              cy=bounds.y,
  445.              cw=bounds.width / 2, ch=bounds.height ; 
  446.  
  447.              cw > 0 && ch > 0;
  448.           
  449.              cx+=step, 
  450.              cy += step, 
  451.              cw -= (step * 2),
  452.              ch -= (step * 2) ) 
  453.        {
  454.           if (g.getColor() == Color.red) 
  455.           {
  456.              g.setColor(Color.blue);
  457.           } 
  458.           else 
  459.           {
  460.              g.setColor(Color.red);
  461.           }
  462.  
  463.           shape.fill(g, cx, cy, cw, ch);
  464.        }  // end for
  465.  
  466.     }  // end paint()
  467.  
  468. }   // end class ShapeTest
  469.  
  470.  
  471. class ColorUtils 
  472. {
  473.     static Color brighter(Color c, double factor) 
  474.     {
  475.        return new Color( Math.min((int)(c.getRed()  *(1/factor)), 255), 
  476.                          Math.min((int)(c.getGreen()*(1/factor)), 255),
  477.                          Math.min((int)(c.getBlue() *(1/factor)), 255) );
  478.     }
  479.  
  480.     static Color darker(Color c, double factor) 
  481.     {
  482.        return new Color( Math.max((int)(c.getRed()  *factor), 0), 
  483.                          Math.max((int)(c.getGreen()*factor), 0),
  484.                          Math.max((int)(c.getBlue() *factor), 0) );
  485.     }
  486. }
  487.