import java.awt.*;
import java.awt.event.*;
/**
*
* Beschreibung
*
* @version 1.0 vom 30.03.2006
* @author
*/
public class Diagramm extends Frame {
// Anfang Variablen
private Panel panel1 = new Panel(null);
private TextField tf1 = new TextField();
private TextField tf2 = new TextField();
private Button button1 = new Button();
private Button button2 = new Button();
private Button button3 = new Button();
private Button button4 = new Button();
private Panel pac = new Panel(null);
// Ende Variablen
public Diagramm(String title) {
// Frame-Initialisierung
super(title);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) { System.exit(0); }
});
int frameWidth = 380;
int frameHeight = 480;
setSize(frameWidth, frameHeight);
Panel cp = new Panel(null);
add(cp);
// Anfang Komponenten
panel1.setBounds(0, 0, 377, 121);
panel1.setBackground(Color.BLACK);
pac.setForeground(Color.RED);
cp.add(panel1);
pac.setBounds(0, 120, 377, 329);
pac.setBackground(Color.BLACK);
cp.add(pac);
tf1.setBounds(16, 16, 201, 24);
tf1.setText("");
panel1.add(tf1);
tf2.setBounds(16, 56, 201, 24);
tf2.setText("");
panel1.add(tf2);
button1.setBounds(232, 16, 137, 17);
button1.setLabel("Übernehmen");
panel1.add(button1);
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button1ActionPerformed(evt);
}
});
button2.setBounds(232, 40, 137, 17);
button2.setLabel("Sortieren");
panel1.add(button2);
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button2ActionPerformed(evt);
}
});
button3.setBounds(232, 96, 137, 17);
button3.setLabel("Löschen");
panel1.add(button3);
button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button3ActionPerformed(evt);
}
});
button4.setBounds(232, 64, 137, 17);
button4.setLabel("Diagramm");
panel1.add(button4);
button4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button4ActionPerformed(evt);
}
});
// Ende Komponenten
setResizable(false);
setVisible(true);
}
// Anfang Ereignisprozeduren
public void button1ActionPerformed(ActionEvent evt) {
Graphics g = pac.getGraphics();
boolean wahr = true;
// Fehler: splitter2 war zuvor zu klein, nämlich 10
int[] splitter2 = new int[100];
String text = tf1.getText();
String[] splitter = text.split(",");
for (int a = 0; a < splitter.length; a++) {
int c = Integer.parseInt(splitter[a]);
splitter2[a] = c;
}
if (splitter.length > 10) {
g.drawString("Es wurden zu viele Zahlen eingegeben!", 30, 75);
wahr = false;
} else {
for (int i = 0; i < splitter.length; i++) {
if (splitter2[i] > 20) {
g.drawString("Es wurden zu grosse Zahlen eingegeben!", 30,90);
wahr = false;
}
}
}
if (wahr == true) {
for (int j = 0; j < splitter.length; j++) {
int d = splitter2[j];
// Fehler: vorher: y Koordinate -> j * 10
//jetzt: y Koordinate (j +1) * 10
// da j bei Null anfing fehlte immer die erste Zahl
g.drawString("" + d, 30, (j+1) * 15);
}g.drawString("Bitte vor der Zeichnung auf Löschen klicken!",60,15);
}
}
public void button2ActionPerformed(ActionEvent evt) {
int[] splitter2 = new int[100];
String text = tf1.getText();
String[] splitter = text.split(",");
for (int a = 0; a < splitter.length; a++) {
int c = Integer.parseInt(splitter[a]);
splitter2[a] = c;
}
int s;
boolean o=false;
do {
for(int r=0;r<splitter.length ;r++ ) {
if(splitter2[r]>splitter2[r+1]) {
s=splitter2[r];
splitter2[r]=splitter2[r+1];
splitter2[r+1]=s;
o=true;
}
}
} while(o==true);
int ausgabe=splitter2[0];
tf2.setText(""+ausgabe);
}
public void button3ActionPerformed(ActionEvent evt) {
Graphics g=pac.getGraphics();
int br=pac.getWidth();
int fh=pac.getHeight();
g.clearRect(0,0,br,fh);
g.drawLine(10,10,10,fh-10);
g.drawLine(10,fh-10,br-10,fh-10);
}
public void button4ActionPerformed(ActionEvent evt) {
Graphics g=pac.getGraphics();
int br=pac.getWidth();
int fh=pac.getHeight();
g.drawLine(10,10,10,fh-10);
g.drawLine(10,fh-10,br-10,fh-10);
int[] splitter2 = new int[100];
String text = tf1.getText();
String[] splitter = text.split(",");
int m=33;
for (int a = 0; a < splitter.length; a++) {
int c = Integer.parseInt(splitter[a]);
splitter2[a] = c;
}
for (int p=0;p<splitter2.length ;p++ ) {
int l=splitter2[p];
l=l*15;
int sp=fh-10-l;
g.drawRect(m,sp,15,l);
m=m+35;
}
}
// Ende Ereignisprozeduren
public static void main(String[] args) {
new Diagramm("Diagramm");
}
}