home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / VCSAMPL.BIN / ArcTest.java < prev    next >
Text File  |  1997-02-10  |  4KB  |  151 lines

  1. /*
  2.  * @(#)ArcTest.java    1.3 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.*;
  33.  
  34. /**
  35.  * An interactive test of the Graphics.drawArc and Graphics.fillArc
  36.  * routines. Can be run either as a standalone application by
  37.  * typing "java ArcTest" or as an applet in the AppletViewer.
  38.  */
  39. public class ArcTest extends Applet {
  40.     ArcControls controls;
  41.     public void init() {
  42.     setLayout(new BorderLayout());
  43.     ArcCanvas c = new ArcCanvas();
  44.     add("Center", c);
  45.     add("South", controls = new ArcControls(c));
  46.     }
  47.  
  48.     public void start() {
  49.     controls.enable();
  50.     }
  51.  
  52.     public void stop() {
  53.     controls.disable();
  54.     }
  55.  
  56.     public boolean handleEvent(Event e) {
  57.     if (e.id == Event.WINDOW_DESTROY) {
  58.         System.exit(0);
  59.     }
  60.     return false;
  61.     }
  62.  
  63.     public static void main(String args[]) {
  64.     Frame f = new Frame("ArcTest");
  65.     ArcTest    arcTest = new ArcTest();
  66.  
  67.     arcTest.init();
  68.     arcTest.start();
  69.  
  70.     f.add("Center", arcTest);
  71.     f.resize(300, 300);
  72.     f.show();
  73.     }
  74. }
  75.     
  76.  
  77. class ArcCanvas extends Canvas {
  78.     int        startAngle = 0;
  79.     int        endAngle = 45;
  80.     boolean    filled = false;
  81.     Font    font;
  82.  
  83.     public void paint(Graphics g) {
  84.     Rectangle r = bounds();
  85.     int hlines = r.height / 10;
  86.     int vlines = r.width / 10;
  87.  
  88.     g.setColor(Color.pink);
  89.     for (int i = 1; i <= hlines; i++) {
  90.         g.drawLine(0, i * 10, r.width, i * 10);
  91.     }
  92.     for (int i = 1; i <= vlines; i++) {
  93.         g.drawLine(i * 10, 0, i * 10, r.height);
  94.     }
  95.  
  96.     g.setColor(Color.red);
  97.     if (filled) {
  98.         g.fillArc(0, 0, r.width - 1, r.height - 1, startAngle, endAngle);
  99.     } else {
  100.         g.drawArc(0, 0, r.width - 1, r.height - 1, startAngle, endAngle);
  101.     }
  102.  
  103.     g.setColor(Color.black);
  104.     g.setFont(font);
  105.     g.drawLine(0, r.height / 2, r.width, r.height / 2);
  106.     g.drawLine(r.width / 2, 0, r.width / 2, r.height);
  107.     g.drawLine(0, 0, r.width, r.height);
  108.     g.drawLine(r.width, 0, 0, r.height);
  109.     int sx = 10;
  110.     int sy = r.height - 28;
  111.     g.drawString("S = " + startAngle, sx, sy); 
  112.     g.drawString("E = " + endAngle, sx, sy + 14); 
  113.     }
  114.  
  115.     public void redraw(boolean filled, int start, int end) {
  116.     this.filled = filled;
  117.     this.startAngle = start;
  118.     this.endAngle = end;
  119.     repaint();
  120.     }
  121. }
  122.  
  123. class ArcControls extends Panel {
  124.     TextField s;
  125.     TextField e;
  126.     ArcCanvas canvas;
  127.  
  128.     public ArcControls(ArcCanvas canvas) {
  129.     this.canvas = canvas;
  130.     add(s = new TextField("0", 4));
  131.     add(e = new TextField("45", 4));
  132.     add(new Button("Fill"));
  133.     add(new Button("Draw"));
  134.     }
  135.  
  136.     public boolean action(Event ev, Object arg) {
  137.     if (ev.target instanceof Button) {
  138.         String label = (String)arg;
  139.  
  140.         canvas.redraw(label.equals("Fill"),
  141.               Integer.parseInt(s.getText().trim()),
  142.               Integer.parseInt(e.getText().trim()));
  143.  
  144.         return true;
  145.     }
  146.  
  147.     return false;
  148.     }
  149. }
  150.     
  151.