home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / javafile / ch11 / TwoDArcs2.java < prev    next >
Encoding:
Java Source  |  1998-12-14  |  3.1 KB  |  126 lines

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.awt.geom.*;
  4. import java.awt.image.BufferedImage;
  5.  
  6. public class TwoDArcs2 extends Canvas {
  7.  
  8.     private int w, h, x, y;
  9.     //Declare the Arc objects
  10.     private Arc2D.Float arc1 = new Arc2D.Float(Arc2D.CHORD);
  11.     private Arc2D.Float arc2 = new Arc2D.Float(Arc2D.OPEN);
  12.     private Arc2D.Float arc3 = new Arc2D.Float(Arc2D.PIE);
  13.  
  14.     //Buffering support
  15.     private BufferedImage offImg; 
  16.  
  17.  
  18.     public TwoDArcs2() {
  19.         setBackground(Color.white);
  20.     }
  21.  
  22.  
  23.     public void drawArcs(Graphics2D g2)
  24.     {
  25.  
  26.     //Set the width of the outline
  27.  
  28.       g2.setStroke(new BasicStroke(5.0f));
  29.  
  30.  
  31.      //Draw the chord
  32.       arc1.setFrame(140,30, 67, 46);
  33.       arc1.setAngleStart(45);
  34.       arc1.setAngleExtent(270);
  35.       g2.setColor(Color.blue);
  36.       g2.draw(arc1);
  37.       g2.setColor(Color.gray);
  38.       g2.fill(arc1);
  39.       g2.setColor(Color.black);
  40.       g2.drawString("Arc2D.CHORD", 140, 20);
  41.  
  42. //Draw the open arc
  43.       arc2.setFrame(140,100, 67, 46);
  44.       arc2.setAngleStart(45);
  45.       arc2.setAngleExtent(270);
  46.       g2.setColor(Color.gray);
  47.       g2.draw(arc2);
  48.       g2.setColor(Color.green);
  49.       g2.fill(arc2);
  50.       g2.setColor(Color.black);
  51.       g2.drawString("Arc2D.OPEN", 140, 90);
  52.  
  53. //Draw the pie chart
  54.       arc3.setFrame(140, 200, 67, 46);
  55.       arc3.setAngleStart(45);
  56.       arc3.setAngleExtent(270);
  57.       g2.setColor(Color.gray);
  58.       g2.draw(arc3);
  59.       g2.setColor(Color.red);
  60.       g2.fill(arc3);
  61.       g2.setColor(Color.black);
  62.       g2.drawString("Arc2D.PIE",140, 190); 
  63.    }
  64.  
  65.  
  66.     public Graphics2D createDemoGraphics2D(Graphics g) {
  67.         Graphics2D g2 = null;
  68.  
  69.         if (offImg == null || offImg.getWidth() != w ||
  70.                         offImg.getHeight() != h) {
  71.             offImg = (BufferedImage) createImage(w, h);
  72. }
  73.  
  74.         if (offImg != null) {
  75.             g2 = offImg.createGraphics();
  76.             g2.setBackground(getBackground());
  77.         }
  78.  
  79.         // .. set attributes ..
  80.         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  81.                             RenderingHints.VALUE_ANTIALIAS_ON);
  82.  
  83.         // .. clear canvas ..
  84.         g2.clearRect(0, 0, w, h);
  85.  
  86.         return g2;
  87.     }
  88.  
  89.  
  90.     public void paint(Graphics g) {
  91.  
  92.         Dimension d = getSize();
  93.         w = d.width;
  94.         h = d.height;
  95.         //System.out.println(w * 2);
  96.         //System.out.println(h);
  97.  
  98.  
  99.         if (w <= 0 || h <= 0)
  100.             return;
  101.  
  102.         Graphics2D g2 = createDemoGraphics2D(g);
  103.         drawArcs(g2);
  104.         g2.dispose();
  105.  
  106.         if (offImg != null && isShowing())  {
  107.             g.drawImage(offImg, 0, 0, this);
  108.         }
  109.  
  110. }
  111.  
  112.     public static void main(String argv[]) {
  113.         final TwoDArcs2 demo = new TwoDArcs2();
  114.         WindowListener l = new WindowAdapter()
  115.         {
  116.             public void windowClosing(WindowEvent e){System.exit(0);}
  117. };
  118.         Frame f = new Frame("Java2D Arcs");
  119.         f.addWindowListener(l);
  120.         f.add("Center", demo);
  121.         f.pack();
  122.         f.setSize(400,300);
  123.         f.show();
  124.     }
  125. }
  126.