home *** CD-ROM | disk | FTP | other *** search
/ Internet Gallery / INTERGAL.bin / intergal / prgs / idv21 / data.z / ArcTest.bsp < prev    next >
Text File  |  1995-10-08  |  4KB  |  151 lines

  1. /*
  2.  * @(#)ArcTest.java    1.9 95/09/01 Sami Shaio
  3.  *
  4.  * Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
  8.  * without fee is hereby granted. 
  9.  * Please refer to the file http://java.sun.com/copy_trademarks.html
  10.  * for further important copyright and trademark information and to
  11.  * http://java.sun.com/licensing.html for further important licensing
  12.  * information for the Java (tm) Technology.
  13.  * 
  14.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  15.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  16.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  18.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  19.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  20.  * 
  21.  * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
  22.  * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
  23.  * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
  24.  * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
  25.  * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
  26.  * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
  27.  * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES").  SUN
  28.  * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
  29.  * HIGH RISK ACTIVITIES.
  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.