Java Tutorial/2D Graphics/Arc

Материал из Java эксперт
Перейти к: навигация, поиск

Arc2D Chord

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Arc2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
class MyCanvas extends JComponent {
  public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D)g;
    
    //Draw the chord
    
      Arc2D.Float arc1 = new Arc2D.Float(Arc2D.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);  
  }
}
public class Graphics2DDrawArcChord {
  public static void main(String[] a) {
    JFrame window = new JFrame();
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setBounds(30, 30, 300, 300);
    window.getContentPane().add(new MyCanvas());
    window.setVisible(true);
  }
}





Arc2D OPEN

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Arc2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
class MyCanvas extends JComponent {
  public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D)g;
    
    //Draw the open arc 
    Arc2D.Float arc = new Arc2D.Float(Arc2D.OPEN); 
    arc.setFrame(140,100, 67, 46); 
    arc.setAngleStart(45); 
    arc.setAngleExtent(270); 
    g2.setColor(Color.gray);  
    g2.draw(arc);  
    g2.setColor(Color.green); 
    g2.fill(arc);  
    g2.setColor(Color.black); 
    g2.drawString("Arc2D.OPEN", 140, 90); 
  }
}
public class Graphics2DDrawArcOPEN {
  public static void main(String[] a) {
    JFrame window = new JFrame();
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setBounds(30, 30, 300, 300);
    window.getContentPane().add(new MyCanvas());
    window.setVisible(true);
  }
}





Arc2D PIE

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Arc2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
class MyCanvas extends JComponent {
  public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    // Draw the pie chart
    Arc2D.Float arc = new Arc2D.Float(Arc2D.PIE);
    arc.setFrame(140, 200, 67, 46);
    arc.setAngleStart(45);
    arc.setAngleExtent(270);
    g2.setColor(Color.gray);
    g2.draw(arc);
    g2.setColor(Color.red);
    g2.fill(arc);
    g2.setColor(Color.black);
    g2.drawString("Arc2D.PIE", 140, 190);
  }
}
public class Graphics2DDrawArcPIE {
  public static void main(String[] a) {
    JFrame window = new JFrame();
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setBounds(30, 30, 300, 300);
    window.getContentPane().add(new MyCanvas());
    window.setVisible(true);
  }
}





Compares two arcs and returns true if they are equal or both null.

import java.awt.geom.Arc2D;
public class Main {

  /**
   * Compares two arcs and returns <code>true</code> if they are equal or
   * both <code>null</code>.
   *
   * @param a1  the first arc (<code>null</code> permitted).
   * @param a2  the second arc (<code>null</code> permitted).
   *
   * @return A boolean.
   */
  public static boolean equal(final Arc2D a1, final Arc2D a2) {
      if (a1 == null) {
          return (a2 == null);
      }
      if (a2 == null) {
          return false;
      }
      if (!a1.getFrame().equals(a2.getFrame())) {
          return false;
      }
      if (a1.getAngleStart() != a2.getAngleStart()) {
          return false;
      }
      if (a1.getAngleExtent() != a2.getAngleExtent()) {
          return false;
      }
      if (a1.getArcType() != a2.getArcType()) {
          return false;
      }
      return true;
  }
}





Draw Arc

import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
class MyCanvas extends JComponent {
  public void paint(Graphics g) {
    g.drawArc (10, 10, 200, 200,50,50);  
  }
}
public class DrawArc {
  public static void main(String[] a) {
    JFrame window = new JFrame();
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setBounds(30, 30, 300, 300);
    window.getContentPane().add(new MyCanvas());
    window.setVisible(true);
  }
}





Drawing a Pie Chart

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.JComponent;
import javax.swing.JFrame;
class Slice {
  double value;
  Color color;
  public Slice(double value, Color color) {
    this.value = value;
    this.color = color;
  }
}
class MyComponent extends JComponent {
  Slice[] slices = { new Slice(5, Color.black), new Slice(33, Color.green),
      new Slice(20, Color.yellow), new Slice(15, Color.red) };
  MyComponent() {
  }
  public void paint(Graphics g) {
    drawPie((Graphics2D) g, getBounds(), slices);
  }
  void drawPie(Graphics2D g, Rectangle area, Slice[] slices) {
    double total = 0.0D;
    for (int i = 0; i < slices.length; i++) {
      total += slices[i].value;
    }
    double curValue = 0.0D;
    int startAngle = 0;
    for (int i = 0; i < slices.length; i++) {
      startAngle = (int) (curValue * 360 / total);
      int arcAngle = (int) (slices[i].value * 360 / total);
      g.setColor(slices[i].color);
      g.fillArc(area.x, area.y, area.width, area.height, startAngle, arcAngle);
      curValue += slices[i].value;
    }
  }
}
public class PieChart {
  public static void main(String[] argv) {
    JFrame frame = new JFrame();
    frame.getContentPane().add(new MyComponent());
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}





Fill Arc

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
class MyCanvas extends JComponent {
  public void paint(Graphics g) {
    g.setColor(Color.RED);
    g.fillArc(20, 20, 200, 200,200,200);
  }
}
public class FillArc {
  public static void main(String[] a) {
    JFrame window = new JFrame();
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setBounds(30, 30, 300, 300);
    window.getContentPane().add(new MyCanvas());
    window.setVisible(true);
  }
}