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

  1. /*
  2.  * @(#)ArcTest.java    1.4 97/02/05
  3.  *
  4.  * Copyright (c) 1994-1997 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.awt.event.*;
  33. import java.applet.*;
  34.  
  35. /**
  36.  * An interactive test of the Graphics.drawArc and Graphics.fillArc
  37.  * routines. Can be run either as a standalone application by
  38.  * typing "java ArcTest" or as an applet in the AppletViewer.
  39.  */
  40. public class ArcTest extends Applet {
  41.     ArcControls controls;
  42.     public void init() {
  43.     setLayout(new BorderLayout());
  44.     ArcCanvas c = new ArcCanvas();
  45.     add("Center", c);
  46.     add("South", controls = new ArcControls(c));
  47.     }
  48.  
  49.     public void start() {
  50.     controls.setEnabled(true);
  51.     }
  52.  
  53.     public void stop() {
  54.     controls.setEnabled(false);
  55.     }
  56.   
  57.     public void processEvent(AWTEvent e) {
  58.         if (e.getID() == Event.WINDOW_DESTROY) {
  59.             System.exit(0);
  60.         }
  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.setSize(300, 300);
  72.     f.show();
  73.     }
  74.  
  75.     public String getAppletInfo() {
  76.         return "An interactive test of the Graphics.drawArc and \nGraphics.fillArc routines. Can be run \neither as a standalone application by typing 'java ArcTest' \nor as an applet in the AppletViewer.";
  77.     }
  78. }
  79.  
  80. class ArcCanvas extends Canvas {
  81.     int        startAngle = 0;
  82.     int        endAngle = 45;
  83.     boolean    filled = false;
  84.     Font    font;
  85.  
  86.     public void paint(Graphics g) {
  87.     Rectangle r = getBounds();
  88.     int hlines = r.height / 10;
  89.     int vlines = r.width / 10;
  90.  
  91.     g.setColor(Color.pink);
  92.     for (int i = 1; i <= hlines; i++) {
  93.         g.drawLine(0, i * 10, r.width, i * 10);
  94.     }
  95.     for (int i = 1; i <= vlines; i++) {
  96.         g.drawLine(i * 10, 0, i * 10, r.height);
  97.     }
  98.  
  99.     g.setColor(Color.red);
  100.     if (filled) {
  101.         g.fillArc(0, 0, r.width - 1, r.height - 1, startAngle, endAngle);
  102.     } else {
  103.         g.drawArc(0, 0, r.width - 1, r.height - 1, startAngle, endAngle);
  104.     }
  105.  
  106.     g.setColor(Color.black);
  107.     g.setFont(font);
  108.     g.drawLine(0, r.height / 2, r.width, r.height / 2);
  109.     g.drawLine(r.width / 2, 0, r.width / 2, r.height);
  110.     g.drawLine(0, 0, r.width, r.height);
  111.     g.drawLine(r.width, 0, 0, r.height);
  112.     int sx = 10;
  113.     int sy = r.height - 28;
  114.     g.drawString("S = " + startAngle, sx, sy); 
  115.     g.drawString("E = " + endAngle, sx, sy + 14); 
  116.     }
  117.  
  118.     public void redraw(boolean filled, int start, int end) {
  119.     this.filled = filled;
  120.     this.startAngle = start;
  121.     this.endAngle = end;
  122.     repaint();
  123.     }
  124. }
  125.  
  126. class ArcControls extends Panel
  127.                   implements ActionListener {
  128.     TextField s;
  129.     TextField e;
  130.     ArcCanvas canvas;
  131.  
  132.     public ArcControls(ArcCanvas canvas) {
  133.     Button b = null;
  134.  
  135.     this.canvas = canvas;
  136.     add(s = new TextField("0", 4));
  137.     add(e = new TextField("45", 4));
  138.     b = new Button("Fill");
  139.     b.addActionListener(this);
  140.     add(b);
  141.     b = new Button("Draw");
  142.     b.addActionListener(this);
  143.     add(b);
  144.     }
  145.  
  146.     public void actionPerformed(ActionEvent ev) {
  147.     String label = ev.getActionCommand();
  148.  
  149.     canvas.redraw(label.equals("Fill"),
  150.                   Integer.parseInt(s.getText().trim()),
  151.                   Integer.parseInt(e.getText().trim()));
  152.     }
  153. }
  154.     
  155.