home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-12-14 | 3.1 KB | 126 lines |
- import java.awt.*;
- import java.awt.event.*;
- import java.awt.geom.*;
- import java.awt.image.BufferedImage;
-
- public class TwoDArcs2 extends Canvas {
-
- private int w, h, x, y;
- //Declare the Arc objects
- private Arc2D.Float arc1 = new Arc2D.Float(Arc2D.CHORD);
- private Arc2D.Float arc2 = new Arc2D.Float(Arc2D.OPEN);
- private Arc2D.Float arc3 = new Arc2D.Float(Arc2D.PIE);
-
- //Buffering support
- private BufferedImage offImg;
-
-
- public TwoDArcs2() {
- setBackground(Color.white);
- }
-
-
- public void drawArcs(Graphics2D g2)
- {
-
- //Set the width of the outline
-
- g2.setStroke(new BasicStroke(5.0f));
-
-
- //Draw the chord
- arc1.setFrame(140,30, 67, 46);
- arc1.setAngleStart(45);
- arc1.setAngleExtent(270);
- g2.setColor(Color.blue);
- g2.draw(arc1);
- g2.setColor(Color.gray);
- g2.fill(arc1);
- g2.setColor(Color.black);
- g2.drawString("Arc2D.CHORD", 140, 20);
-
- //Draw the open arc
- arc2.setFrame(140,100, 67, 46);
- arc2.setAngleStart(45);
- arc2.setAngleExtent(270);
- g2.setColor(Color.gray);
- g2.draw(arc2);
- g2.setColor(Color.green);
- g2.fill(arc2);
- g2.setColor(Color.black);
- g2.drawString("Arc2D.OPEN", 140, 90);
-
- //Draw the pie chart
- arc3.setFrame(140, 200, 67, 46);
- arc3.setAngleStart(45);
- arc3.setAngleExtent(270);
- g2.setColor(Color.gray);
- g2.draw(arc3);
- g2.setColor(Color.red);
- g2.fill(arc3);
- g2.setColor(Color.black);
- g2.drawString("Arc2D.PIE",140, 190);
- }
-
-
- public Graphics2D createDemoGraphics2D(Graphics g) {
- Graphics2D g2 = null;
-
- if (offImg == null || offImg.getWidth() != w ||
- offImg.getHeight() != h) {
- offImg = (BufferedImage) createImage(w, h);
- }
-
- if (offImg != null) {
- g2 = offImg.createGraphics();
- g2.setBackground(getBackground());
- }
-
- // .. set attributes ..
- g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
- RenderingHints.VALUE_ANTIALIAS_ON);
-
- // .. clear canvas ..
- g2.clearRect(0, 0, w, h);
-
- return g2;
- }
-
-
- public void paint(Graphics g) {
-
- Dimension d = getSize();
- w = d.width;
- h = d.height;
- //System.out.println(w * 2);
- //System.out.println(h);
-
-
- if (w <= 0 || h <= 0)
- return;
-
- Graphics2D g2 = createDemoGraphics2D(g);
- drawArcs(g2);
- g2.dispose();
-
- if (offImg != null && isShowing()) {
- g.drawImage(offImg, 0, 0, this);
- }
-
- }
-
- public static void main(String argv[]) {
- final TwoDArcs2 demo = new TwoDArcs2();
- WindowListener l = new WindowAdapter()
- {
- public void windowClosing(WindowEvent e){System.exit(0);}
- };
- Frame f = new Frame("Java2D Arcs");
- f.addWindowListener(l);
- f.add("Center", demo);
- f.pack();
- f.setSize(400,300);
- f.show();
- }
- }
-