Arithmetic and Math class

  //filename: SlantedPie.java
  import java.awt.*;
  import java.awt.event.*;
  import java.applet.*;

  public class SlantedPie extends Applet implements ActionListener
  {
    public int slope;
    public Button go;
    public TextField in;
     
    public void init()
    {
      slope = 0;
      
      go = new Button("Go");
      in = new TextField(5);
      
      add(in); add(go);
      
      go.addActionListener(this);
    }
    
    public void drawPie(Graphics g, int x, int y,
                        int width, int height, int angle)
    {
      // the width of the outer edge
      int bandWidth = width/7;
      int bandHeight = height/7;
      
      // the dimensions of the slices
      int propWidth = width - 2*bandWidth;
      int propHeight = height - 2*bandHeight;
      
    // draw the edge
    g.setColor(Color.red);
    g.fillOval(x , y, width, height);
      
    // the yellow slices
    g.setColor(Color.yellow);
    g.fillArc(x + bandWidth, y + bandHeight,
              propWidth, propHeight, angle, 90);
      g.fillArc(x + bandWidth, y + bandHeight,
                propWidth, propHeight, angle + 180, 90);

    // the black slices
    g.setColor(Color.black);
      g.fillArc(x + bandWidth, y + bandHeight,
                propWidth, propHeight, angle + 90, 90);
    g.fillArc(x + bandWidth, y + bandHeight, 
              propWidth, propHeight, angle + 270, 90);
    }
    
    public void drawPieSlanted(Graphics g, int x, int y,
                               int size, int angle, int slope)
    {
      int cos = (int)Math.round((size/2) * Math.cos(slope*Math.PI/180));
      drawPie(g, x, y + (size/2) - cos, size, 2 * cos, angle);
    }
    
    public void paint(Graphics g)
    {
      drawPieSlanted(g, 20, 20, 70, 0, slope);
    }
    
    public void actionPerformed(ActionEvent e)
    {
      slope = Integer.parseInt(in.getText());
      repaint();
    }  
  }

Previou page Next page