home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / plugin / applets / ArcTest / ArcTest.java < prev    next >
Encoding:
Java Source  |  2002-09-06  |  5.0 KB  |  171 lines

  1. /*
  2.  * Copyright (c) 2002 Sun Microsystems, Inc. All  Rights Reserved.
  3.  * 
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  * 
  8.  * -Redistributions of source code must retain the above copyright
  9.  *  notice, this list of conditions and the following disclaimer.
  10.  * 
  11.  * -Redistribution in binary form must reproduct the above copyright
  12.  *  notice, this list of conditions and the following disclaimer in
  13.  *  the documentation and/or other materials provided with the distribution.
  14.  * 
  15.  * Neither the name of Sun Microsystems, Inc. or the names of contributors
  16.  * may be used to endorse or promote products derived from this software
  17.  * without specific prior written permission.
  18.  * 
  19.  * This software is provided "AS IS," without a warranty of any kind. ALL
  20.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
  21.  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  22.  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
  23.  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
  24.  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
  25.  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
  26.  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
  27.  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
  28.  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
  29.  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  30.  * 
  31.  * You acknowledge that Software is not designed, licensed or intended for
  32.  * use in the design, construction, operation or maintenance of any nuclear
  33.  * facility.
  34.  */
  35.  
  36. /*
  37.  * @(#)ArcTest.java    1.9 02/06/13
  38.  */
  39.  
  40. import java.awt.*;
  41. import java.awt.event.*;
  42. import java.applet.*;
  43.  
  44. /**
  45.  * An interactive test of the Graphics.drawArc and Graphics.fillArc
  46.  * routines. Can be run either as a standalone application by
  47.  * typing "java ArcTest" or as an applet in the AppletViewer.
  48.  */
  49. public class ArcTest extends Applet {
  50.     ArcControls controls;   // The controls for marking and filling arcs
  51.     ArcCanvas canvas;       // The drawing area to display arcs
  52.  
  53.     public void init() {
  54.     setLayout(new BorderLayout());
  55.     canvas = new ArcCanvas();
  56.     add("Center", canvas);
  57.     add("South", controls = new ArcControls(canvas));
  58.     }
  59.  
  60.     public void destroy() {
  61.         remove(controls);
  62.         remove(canvas);
  63.     }
  64.  
  65.     public void start() {
  66.     controls.setEnabled(true);
  67.     }
  68.  
  69.     public void stop() {
  70.     controls.setEnabled(false);
  71.     }
  72.  
  73.     public void processEvent(AWTEvent e) {
  74.         if (e.getID() == Event.WINDOW_DESTROY) {
  75.             System.exit(0);
  76.         }
  77.     }
  78.  
  79.     public static void main(String args[]) {
  80.     Frame f = new Frame("ArcTest");
  81.     ArcTest    arcTest = new ArcTest();
  82.  
  83.     arcTest.init();
  84.     arcTest.start();
  85.  
  86.     f.add("Center", arcTest);
  87.     f.setSize(300, 300);
  88.     f.show();
  89.     }
  90.  
  91.     public String getAppletInfo() {
  92.         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.";
  93.     }
  94. }
  95.  
  96. class ArcCanvas extends Canvas {
  97.     int        startAngle = 0;
  98.     int        endAngle = 45;
  99.     boolean    filled = false;
  100.     Font    font = new java.awt.Font("Courier", Font.BOLD, 12);
  101.  
  102.     public void paint(Graphics g) {
  103.     Rectangle r = getBounds();
  104.     int hlines = r.height / 10;
  105.     int vlines = r.width / 10;
  106.  
  107.     g.setColor(Color.pink);
  108.     for (int i = 1; i <= hlines; i++) {
  109.         g.drawLine(0, i * 10, r.width, i * 10);
  110.     }
  111.     for (int i = 1; i <= vlines; i++) {
  112.         g.drawLine(i * 10, 0, i * 10, r.height);
  113.     }
  114.  
  115.     g.setColor(Color.red);
  116.     if (filled) {
  117.         g.fillArc(0, 0, r.width - 1, r.height - 1, startAngle, endAngle);
  118.     } else {
  119.         g.drawArc(0, 0, r.width - 1, r.height - 1, startAngle, endAngle);
  120.     }
  121.  
  122.     g.setColor(Color.black);
  123.     g.setFont(font);
  124.     g.drawLine(0, r.height / 2, r.width, r.height / 2);
  125.     g.drawLine(r.width / 2, 0, r.width / 2, r.height);
  126.     g.drawLine(0, 0, r.width, r.height);
  127.     g.drawLine(r.width, 0, 0, r.height);
  128.     int sx = 10;
  129.     int sy = r.height - 28;
  130.     g.drawString("S = " + startAngle, sx, sy);
  131.     g.drawString("E = " + endAngle, sx, sy + 14);
  132.     }
  133.  
  134.     public void redraw(boolean filled, int start, int end) {
  135.     this.filled = filled;
  136.     this.startAngle = start;
  137.     this.endAngle = end;
  138.     repaint();
  139.     }
  140. }
  141.  
  142. class ArcControls extends Panel
  143.                   implements ActionListener {
  144.     TextField s;
  145.     TextField e;
  146.     ArcCanvas canvas;
  147.  
  148.     public ArcControls(ArcCanvas canvas) {
  149.     Button b = null;
  150.  
  151.     this.canvas = canvas;
  152.     add(s = new TextField("0", 4));
  153.     add(e = new TextField("45", 4));
  154.     b = new Button("Fill");
  155.     b.addActionListener(this);
  156.     add(b);
  157.     b = new Button("Draw");
  158.     b.addActionListener(this);
  159.     add(b);
  160.     }
  161.  
  162.     public void actionPerformed(ActionEvent ev) {
  163.     String label = ev.getActionCommand();
  164.  
  165.     canvas.redraw(label.equals("Fill"),
  166.                   Integer.parseInt(s.getText().trim()),
  167.                   Integer.parseInt(e.getText().trim()));
  168.     }
  169. }
  170.  
  171.