Java Graphics How to - Draw open PIE








Question

We would like to know how to draw open PIE.

Answer

/*www  . j  a  va  2  s .  c o  m*/
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);
  }
}