Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Displaying the applet

$
0
0
I had created the code as a JFrame first to make sure the code worked. But as soon as I changed the extend part to Applet it stoped working.

My original JFrame code that worked
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package piechartapplet;
import java.awt.*;
import javax.swing.*;

/**
 *
 * @author Shawna Rowe
 * 
 * 
 * Write an applet that includes a pie chart. 
 * Use a news article with statistics that are good candidates for a pie chart: 
 *  for example, political candidate preferences; percentages of those for, 
 * against, or undecided about a ballot measure; and so forth.
 * 
 * Cite the source for your input statistics.
 * Submit the applet along with an HTML file to launch it. 
 * 
 */
public class PieChartApplet extends JFrame {
    int Murder, Rape, Burglary, Theft, total;  // statistics to enter
    float percMur, percRape, PercBurg, PercTheft; // percentages
    /*
     * The layout method sets up the applet similar to
     * the set up of a constructor
     */
    public PieChartApplet()
    {
      setTitle("Pie Chart Statistics");
            setSize(1400, 1400);
            setVisible(true);
            setLayout(new GridLayout(2, 3));
            setBackground(Color.WHITE);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
 
    //Create the outer circle of the pie chart
    /**
     *
     * @param g
     */
    @Override
    public void paint(Graphics g)
    {
     
        Graphics2D g2d = (Graphics2D)g;
    g2d.setColor(Color.BLACK); //outside line of circle
     int x = 0, y = 0, w = 200, h= 200; //defines size
    int startPosistion, degrees; //will be used to draw pie slice
      
    
    //define the numbers to go with the crime
    
  Assault = 848;
    Rape = 393;
    Burglary = 7854;
    larcenyTheft = 24877;
    vehicleTheft = 1075;
    
    //calculate percentages
    total = Assault + Rape + Burglary + larcenyTheft + vehicleTheft ;
    percMur =  (Assault * 100.0f) / total;
    percRape =(Rape * 100.0f) / total;
    PercBurg =(Burglary * 100.0f) / total;
    percLTheft =(larcenyTheft * 100.0f) / total;
    percVTheft = (vehicleTheft * 100.0f) / total;
    
    
    
    /*
     * Used an example found
     * http://mainline.brynmawr.edu/Courses/cs110/fall2003/Applets/PieChart/PieChart.html
     * 
     * 
     * 
     * 
     */
    //display pie chart
    startPosistion = 0;
    degrees =(int)(percMur * 360/100);
    g.setColor(Color.RED);
    g.fillArc(x, y, w, h, startPosistion, degrees);
   
    startPosistion = degrees;
    degrees =(int)(percRape * 360/100);
    g.setColor(Color.YELLOW);
    g.fillArc(x, y, w, h, startPosistion, degrees);
   
    startPosistion = startPosistion + degrees;
    degrees =(int)(PercBurg * 360/100);
    g.setColor(Color.BLUE);
    g.fillArc(x, y, w, h, startPosistion, degrees);
   
    startPosistion = startPosistion + degrees;
    degrees =(int)(percLTheft * 360/100);
    g.setColor(Color.GREEN);
    g.fillArc(x, y, w, h, startPosistion, degrees);
    
    startPosistion = startPosistion + degrees;
    degrees =(int)(percVTheft * 360/100);
    g.setColor(Color.ORANGE);
    g.fillArc(x, y, w, h, startPosistion, degrees);
   
    
   
    }
    

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        PieChartApplet f = new PieChartApplet();
      
    
    }

}






This is my Applet Code now,

package piechartapplet;
import java.awt.*;
import javax.swing.*;

/**
 *
 * @author Shawna Rowe
 * 
 * 
 * Write an applet that includes a pie chart. 
 * Use a news article with statistics that are good candidates for a pie chart: 
 *  for example, political candidate preferences; percentages of those for, 
 * against, or undecided about a ballot measure; and so forth.
 * 
 * Cite the source for your input statistics.
 * Submit the applet along with an HTML file to launch it. 
 * 
 */
public class PieChartApplet extends JApplet {
    int Assault, Rape, Burglary, larcenyTheft, vehicleTheft, total;  // statistics to enter
    float percMur, percRape, PercBurg, percLTheft, percVTheft;  // percentages

 
    //Create the outer circle of the pie chart
    /**
     *
     * @param g
     */
    @Override
    public void paint(Graphics g)
    {
     
        Graphics2D g2d = (Graphics2D)g;
    g2d.setColor(Color.BLACK); //outside line of circle
   int x = 0, y = 0, w = 200, h= 200; //defines size
    
    int startPosistion, degrees; //will be used to draw pie slice
      
    
    //define the numbers to go with the crime
    
   Assault = 848;
    Rape = 393;
    Burglary = 7854;
    larcenyTheft = 24877;
    vehicleTheft = 1075;
    
    //calculate percentages
    total = Assault + Rape + Burglary + larcenyTheft + vehicleTheft ;
    percMur =  (Assault * 100.0f) / total;
    percRape =(Rape * 100.0f) / total;
    PercBurg =(Burglary * 100.0f) / total;
    percLTheft =(larcenyTheft * 100.0f) / total;
    percVTheft = (vehicleTheft * 100.0f) / total;
    
    
    
    /*
     * Used an example found
     * http://mainline.brynmawr.edu/Courses/cs110/fall2003/Applets/PieChart/PieChart.html
     * 
     * 
     * 
     * 
     */
    //display pie chart
    startPosistion = 0;
    degrees =(int)(percMur * 360/100);
    g.setColor(Color.RED);
    g.fillArc(x, y, w, h, startPosistion, degrees);
   
    startPosistion = degrees;
    degrees =(int)(percRape * 360/100);
    g.setColor(Color.YELLOW);
    g.fillArc(x, y, w, h, startPosistion, degrees);
   
    startPosistion = startPosistion + degrees;
    degrees =(int)(PercBurg * 360/100);
    g.setColor(Color.BLUE);
    g.fillArc(x, y, w, h, startPosistion, degrees);
   
    startPosistion = startPosistion + degrees;
    degrees =(int)(percLTheft * 360/100);
    g.setColor(Color.GREEN);
    g.fillArc(x, y, w, h, startPosistion, degrees);
    
    startPosistion = startPosistion + degrees;
    degrees =(int)(percVTheft * 360/100);
    g.setColor(Color.ORANGE);
    g.fillArc(x, y, w, h, startPosistion, degrees);
   
    }
   
  

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        PieChartApplet f = new PieChartApplet();
      
    
    }

}



When I run this code, it says build succesful but the applet does not open. When I open the HTML file I created
&lt!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
      
    <applet code="PieChartApplet.class" width="350" height="350">
         This applet will display statistics of Maine Crimes in 2011.
        The statistics were found at 
    </applet>
    </body>
</html>


I get this error: Application error NoClassDefFoundError PieChartApplet(wrong name: piechartapplet/PieChartApplet)

Obviously that error means that I've used the wrong name somewhere!? But where? I know a bit about web design and a bit about java but putting them together I guess I am useless. Have I saved the file in a wrong folder? Is it calling upon the wrong java class (Highly doubt as this is the ONLY java class in this file)? Can someone help!?

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>