import java.awt.*;import java.applet.*;import java.awt.event.*;public class Costs extends Applet // Das eigentliche Applet{ Graphics g; public static Label wert1, wert2, wert3, zins, energiepr, anschluss, druck, betrvolstr, invest, wartung, last, leer; public static TextField zinstxt, energieprtxt, anschlusstxt, drucktxt, betrvolstrtxt, investtxt, wartungtxt, lasttxt, leertxt; public Button cal; PieChart pie; public void init() // Vorbereitung { GridBagLayout gridBagLayout; //Aufteilung der Appletflaeche GridBagConstraints c; pie= new PieChart(); wert1=new Label("Wert1"); wert2=new Label("Wert2"); wert3=new Label("Wert3"); zins = new Label("Zinssatz: "); zinstxt = new TextField(" ", 6); energiepr = new Label("Energiepreis: "); energieprtxt = new TextField(" ", 6); anschluss = new Label("Anschlussleistung:"); anschlusstxt = new TextField(" ", 3); druck = new Label("Druck: "); drucktxt = new TextField(" ", 6); betrvolstr = new Label("Betriebsvolumenstrom: "); betrvolstrtxt = new TextField(" ", 6); invest = new Label("Investitionskosten:"); investtxt = new TextField(" ", 3); wartung = new Label("Wartungskosten: "); wartungtxt = new TextField(" ", 6); last = new Label("Laststunden: "); lasttxt = new TextField(" ", 6); leer = new Label("Leerlaufstunden:"); leertxt = new TextField(" ", 3); cal = new Button("Berechne"); cal.addActionListener(new CalculateListener(pie)); gridBagLayout = new GridBagLayout(); this.setLayout(gridBagLayout); c = new GridBagConstraints(); c.gridx = 0; c.gridy = 0; c.weightx = 1; c.anchor = GridBagConstraints.WEST; //c.insets = new Insets(UPPERSPACE, 0, 0, 0); gridBagLayout.setConstraints(zins, c); add(zins); c.gridx = 1; c.gridy = 0; c.weightx = 1; c.anchor = GridBagConstraints.WEST; //c.insets = new Insets(UPPERSPACE, 0, 0, 0); gridBagLayout.setConstraints(zinstxt, c); add(zinstxt); c.gridx = 0; c.gridy = 1; c.weightx = 1; c.anchor = GridBagConstraints.WEST; //c.insets = new Insets(UPPERSPACE, 0, 0, 0); gridBagLayout.setConstraints(energiepr, c); add(energiepr); c.gridx = 1; c.gridy = 1; c.weightx = 1; c.anchor = GridBagConstraints.WEST; //c.insets = new Insets(UPPERSPACE, 0, 0, 0); gridBagLayout.setConstraints(energieprtxt, c); add(energieprtxt); c.gridx = 0; c.gridy = 2; c.weightx = 1; c.anchor = GridBagConstraints.WEST; //c.insets = new Insets(UPPERSPACE, 0, 0, 0); gridBagLayout.setConstraints(anschluss, c); add(anschluss); c.gridx = 1; c.gridy = 2; c.weightx = 1; c.anchor = GridBagConstraints.WEST; //c.insets = new Insets(UPPERSPACE, 0, 0, 0); gridBagLayout.setConstraints(anschlusstxt, c); add(anschlusstxt); c.gridx = 0; c.gridy = 3; c.weightx = 1; c.anchor = GridBagConstraints.WEST; //c.insets = new Insets(UPPERSPACE, 0, 0, 0); gridBagLayout.setConstraints(druck, c); add(druck); c.gridx = 1; c.gridy = 3; c.weightx = 1; c.anchor = GridBagConstraints.WEST; //c.insets = new Insets(UPPERSPACE, 0, 0, 0); gridBagLayout.setConstraints(drucktxt, c); add(drucktxt); c.gridx = 0; c.gridy = 4; c.weightx = 1; c.anchor = GridBagConstraints.WEST; //c.insets = new Insets(UPPERSPACE, 0, 0, 0); gridBagLayout.setConstraints(betrvolstr, c); add(betrvolstr); c.gridx = 1; c.gridy = 4; c.weightx = 1; c.anchor = GridBagConstraints.WEST; //c.insets = new Insets(UPPERSPACE, 0, 0, 0); gridBagLayout.setConstraints(betrvolstrtxt, c); add(betrvolstrtxt); c.gridx = 0; c.gridy = 5; c.weightx = 1; c.anchor = GridBagConstraints.WEST; //c.insets = new Insets(UPPERSPACE, 0, 0, 0); gridBagLayout.setConstraints(invest, c); add(invest); c.gridx = 1; c.gridy = 5; c.weightx = 1; c.anchor = GridBagConstraints.WEST; //c.insets = new Insets(UPPERSPACE, 0, 0, 0); gridBagLayout.setConstraints(investtxt, c); add(investtxt); c.gridx = 0; c.gridy = 6; c.weightx = 1; c.anchor = GridBagConstraints.WEST; //c.insets = new Insets(UPPERSPACE, 0, 0, 0); gridBagLayout.setConstraints(wartung, c); add(wartung); c.gridx = 1; c.gridy = 6; c.weightx = 1; c.anchor = GridBagConstraints.WEST; //c.insets = new Insets(UPPERSPACE, 0, 0, 0); gridBagLayout.setConstraints(wartungtxt, c); add(wartungtxt); c.gridx = 0; c.gridy = 7; c.weightx = 1; c.anchor = GridBagConstraints.WEST; //c.insets = new Insets(UPPERSPACE, 0, 0, 0); gridBagLayout.setConstraints(last, c); add(last); c.gridx = 1; c.gridy = 7; c.weightx = 1; c.anchor = GridBagConstraints.WEST; //c.insets = new Insets(UPPERSPACE, 0, 0, 0); gridBagLayout.setConstraints(lasttxt, c); add(lasttxt); c.gridx = 0; c.gridy = 8; c.weightx = 1; c.anchor = GridBagConstraints.WEST; //c.insets = new Insets(UPPERSPACE, 0, 0, 0); gridBagLayout.setConstraints(leer, c); add(leer); c.gridx = 1; c.gridy = 8; c.weightx = 1; c.anchor = GridBagConstraints.WEST; //c.insets = new Insets(UPPERSPACE, 0, 0, 0); gridBagLayout.setConstraints(leertxt, c); add(leertxt); c.gridx = 0; c.gridy = 9; c.weightx = 1; c.gridwidth = 1; c.anchor = GridBagConstraints.WEST; //c.insets = new Insets(UPPERSPACE, 0, 0, 0); gridBagLayout.setConstraints(cal, c); add(cal); c.gridx = 2; c.gridy = 0; c.weightx = 1; c.anchor = GridBagConstraints.CENTER; c.gridheight = 7; c.fill = GridBagConstraints.NONE; //c.insets = new Insets(UPPERSPACE, 0, 0, 0); gridBagLayout.setConstraints(pie, c); add(pie); c.gridx = 2; c.gridy = 7; c.weightx = 1; c.anchor = GridBagConstraints.WEST; //c.insets = new Insets(UPPERSPACE, 0, 0, 0); gridBagLayout.setConstraints(wert1, c); add(wert1); c.gridx = 2; c.gridy = 8; c.weightx = 1; c.anchor = GridBagConstraints.WEST; //c.insets = new Insets(UPPERSPACE, 0, 0, 0); gridBagLayout.setConstraints(wert2, c); add(wert2); c.gridx = 2; c.gridy = 9; c.weightx = 1; c.anchor = GridBagConstraints.WEST; //c.insets = new Insets(UPPERSPACE, 0, 0, 0); gridBagLayout.setConstraints(wert3, c); add(wert3); } }
import java.awt.*;public class PieChart extends Component{ Graphics g; Font font; FontMetrics fontMetrics; int results =3; //Anzahl der Ergebniswerte int onedegree =1; double percent[]; int angle[]; double input[]; //Feld fuer die uebergebenen Werte double output[]; //Feld fuer die Ergebniswerte String label[]; //Beschriftung der Segmente Color wert1, wert2,wert3; Color color[]; double multiFactor =0; double totalValue =0; //Gesamtwert der Ergebnisse, um in Prozent umzurechnen double totalAngle=0; // Gesamtwert der Winkel public PieChart() //Konstruktor { super(); output = new double[results]; //Ergebnisse percent = new double[results]; //Prozentangabe der Segmente angle = new int[results]; //Winkelangabe der Segmente color = new Color[results]; font = new java.awt.Font("Helvetica", Font.PLAIN, 12); // Schriftart, entspricht Arial fontMetrics = getFontMetrics(font); } public void setValue(double[]value) //Array mit Werten uebergeben { input=new double[9]; for(int i=0;i<value.length;i++) //belege input[] mit den uebergebenen Werten { input[i]=value[i]; } repaint(); } public void paint(Graphics g) //Methode um das Kreisdiagramm zu zeichnen { /**HIER MUSS DIE BERECHNUNG REIN; AUS DEN 9 UEBERGEBENEN WERTEN MUESSEN DREI ENDERGEBNISSE BERECHNET WERDEN!!! */ output[0]=60; //Testwerte fuer die Ergebnisse output[1]=20; output[2]=100; for (int i=0; i < output.length; i++) { totalValue += output[i]; //Maximalwert=Summe aller Werte der Felder ->fuer spaetere Prozentaufteilung!! } totalValue = Math.max(totalValue, 1); onedegree = (int)totalValue/360; for (int i=0; i < output.length; i++) // Umrechnung für die Winkel des Kreisdiagramms { angle[i] =(int) output[i]/onedegree; totalAngle += angle[i] ; } totalAngle = Math.min(totalAngle, 360); // jetzt wird gezeichnet... int x = 0; int y = 0; int width = getSize().width; int height = getSize().height; int initAngle=90; // Anfangswinkel entspricht 0 Uhr int sweepAngle=0; // Ueberstrichener Winkel for (int i=0; i < output.length; i++) { sweepAngle = angle[i]; g.setColor(color[i]); g.fillArc(x,y,width,height,initAngle,sweepAngle); initAngle += sweepAngle; } } }
import java.awt.*;import java.awt.event.*;public class CalculateListener implements ActionListener // Klasse zur Ereignisverarbeitung{ PieChart pie; Graphics g; public double value[]; public CalculateListener(PieChart pie) { this.pie = pie; } public void actionPerformed(ActionEvent e) // Auf Knopfdruck reagieren { pie=new PieChart(); value = new double[9]; //abgelesene Werte for (int i=0; i < value.length; i++) //jeden Wert abarbeiten { try //Ausnahmebehandlung bei falschen Eingaben { Double hilf = new Double(Costs.zinstxt.getText()); value[0] = hilf.doubleValue(); hilf = new Double(Costs.energieprtxt.getText()); value[1] = hilf.doubleValue(); hilf = new Double(Costs.anschlusstxt.getText()); value[2] = hilf.doubleValue(); hilf = new Double(Costs.drucktxt.getText()); value[3] = hilf.doubleValue(); hilf = new Double(Costs.betrvolstrtxt.getText()); value[4] = hilf.doubleValue(); hilf = new Double(Costs.investtxt.getText()); value[5] = hilf.doubleValue(); hilf = new Double(Costs.wartungtxt.getText()); value[6] = hilf.doubleValue(); hilf = new Double(Costs.lasttxt.getText()); value[7] = hilf.doubleValue(); hilf = new Double(Costs.leertxt.getText()); value[8] = hilf.doubleValue(); } catch (NumberFormatException ex) //Ausnahmebehandlung { value[i] = 0; //Frame f=new Frame("Achtung!"); //Dialog box = new Dialog (null, "Es ist ein Eingabefehler aufgetreten!", true); //f.pack(); } } pie.setValue(value); //Belegung der Werte für die Graphik pie.paint(g); //Zeichnen des Kreisdiagramms }}