Java Graphics How to - Draw Chord Arc








Question

We would like to know how to draw Chord Arc.

Answer

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Arc2D;
/*from   ww  w.j a  v  a  2s . com*/
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 Main {
  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);
  }
}