home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 July & August / Pcwk78a98.iso / Internet / Javadraw / DATA.Z / ArcTest.java < prev    next >
Text File  |  1997-02-10  |  4KB  |  153 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 boolean handleEvent(Event e) {
  58.     if (e.id == Event.WINDOW_DESTROY) {
  59.         System.exit(0);
  60.     }
  61.     return false;
  62.     }
  63.  
  64.     public static void main(String args[]) {
  65.     Frame f = new Frame("ArcTest");
  66.     ArcTest    arcTest = new ArcTest();
  67.  
  68.     arcTest.init();
  69.     arcTest.start();
  70.  
  71.     f.add("Center", arcTest);
  72.     f.setSize(300, 300);
  73.     f.show();
  74.     }
  75. }
  76.     
  77.  
  78. class ArcCanvas extends Canvas {
  79.     int        startAngle = 0;
  80.     int        endAngle = 45;
  81.     boolean    filled = false;
  82.     Font    font;
  83.  
  84.     public void paint(Graphics g) {
  85.     Rectangle r = getBounds();
  86.     int hlines = r.height / 10;
  87.     int vlines = r.width / 10;
  88.  
  89.     g.setColor(Color.pink);
  90.     for (int i = 1; i <= hlines; i++) {
  91.         g.drawLine(0, i * 10, r.width, i * 10);
  92.     }
  93.     for (int i = 1; i <= vlines; i++) {
  94.         g.drawLine(i * 10, 0, i * 10, r.height);
  95.     }
  96.  
  97.     g.setColor(Color.red);
  98.     if (filled) {
  99.         g.fillArc(0, 0, r.width - 1, r.height - 1, startAngle, endAngle);
  100.     } else {
  101.         g.drawArc(0, 0, r.width - 1, r.height - 1, startAngle, endAngle);
  102.     }
  103.  
  104.     g.setColor(Color.black);
  105.     g.setFont(font);
  106.     g.drawLine(0, r.height / 2, r.width, r.height / 2);
  107.     g.drawLine(r.width / 2, 0, r.width / 2, r.height);
  108.     g.drawLine(0, 0, r.width, r.height);
  109.     g.drawLine(r.width, 0, 0, r.height);
  110.     int sx = 10;
  111.     int sy = r.height - 28;
  112.     g.drawString("S = " + startAngle, sx, sy); 
  113.     g.drawString("E = " + endAngle, sx, sy + 14); 
  114.     }
  115.  
  116.     public void redraw(boolean filled, int start, int end) {
  117.     this.filled = filled;
  118.     this.startAngle = start;
  119.     this.endAngle = end;
  120.     repaint();
  121.     }
  122. }
  123.  
  124. class ArcControls extends Panel
  125.                   implements ActionListener {
  126.     TextField s;
  127.     TextField e;
  128.     ArcCanvas canvas;
  129.  
  130.     public ArcControls(ArcCanvas canvas) {
  131.     Button b = null;
  132.  
  133.     this.canvas = canvas;
  134.     add(s = new TextField("0", 4));
  135.     add(e = new TextField("45", 4));
  136.     b = new Button("Fill");
  137.     b.addActionListener(this);
  138.     add(b);
  139.     b = new Button("Draw");
  140.     b.addActionListener(this);
  141.     add(b);
  142.     }
  143.  
  144.     public void actionPerformed(ActionEvent ev) {
  145.     String label = ev.getActionCommand();
  146.  
  147.     canvas.redraw(label.equals("Fill"),
  148.                   Integer.parseInt(s.getText().trim()),
  149.                   Integer.parseInt(e.getText().trim()));
  150.     }
  151. }
  152.     
  153.