Search code examples
javaappletjapplet

Troubles with a piechart applet


Alright the deal is I am trying to use the drawPie method to create my pie chart in an applet. After attempting google searches I find multiple tutorials that explain part of the process but not all of it. While trying to knit together partial information I am not getting the results I want.

    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;

    import javax.swing.JApplet;
    import javax.swing.JComponent;

public class JLW_PieApplet extends JApplet {

class PieData extends JComponent {
    PieValue[] slices = new PieValue[4];

    PieData() {
        slices[0] = new PieValue(35, Color.red);
        slices[1] = new PieValue(33, Color.green);
        slices[2] = new PieValue(20, Color.pink);
        slices[3] = new PieValue(12, Color.blue);

    }

    public void paint(Graphics g) {
        drawPie((Graphics2D)g, getBounds(), slices);
    }
}

}


Solution

  • You have a PieData component within your applet but you never add it, so you need to add it in init and bring in drawPie from your link above:

    public class JLW_PieApplet extends JApplet {
    
       public void init() {
          add(new PieData());
       }
    
       class PieData extends JComponent {
          PieValue[] slices = new PieValue[4];
    
          PieData() {
             slices[0] = ...
          }
    
          @Override
          protected void paintComponent(Graphics g) {
             super.paintComponent(g);
             drawPie((Graphics2D) g, getBounds(), slices);
          }
    
          public void drawPie(Graphics2D g, Rectangle area, PieValue[] slices) {
          ...