Hi
Heute ist glaub ich nicht mein Tag :cry: Schon die andere Übung (siehe mein anderes Thema) hab ich irgendwas nicht geschnallt und hier wieder.
Man muss so ein "Kuchendiagramm" zeichnen. Ich würde das Programm gerne testen aber es steht immer "Applet nicht initialisiert" wieso?!?!
Heute ist glaub ich nicht mein Tag :cry: Schon die andere Übung (siehe mein anderes Thema) hab ich irgendwas nicht geschnallt und hier wieder.
Man muss so ein "Kuchendiagramm" zeichnen. Ich würde das Programm gerne testen aber es steht immer "Applet nicht initialisiert" wieso?!?!
Code:
package playcards;
import java.awt.*;
public class StringClass {
private final int startX = 100, startY = 100;
private final int xSpace = 20, ySpace = 20;
private String[] array = new String[10];
private String newValue;
private boolean newEntry = false;
private boolean searchEntry = false;
private boolean foundEntry = false;
public boolean inputString() {
boolean success = false;
for(int i = 0; i < this.array.length; i++){
if(this.array[i] == null) {
this.array[i] = this.newValue;
return success = true;
}
}
return success = false;
}
public void display(Graphics g) {
g.drawString(Boolean.toString(searchEntry), 200, 200);
g.drawString(Boolean.toString(foundEntry), 200, 220);
if(this.searchEntry) {
if(this.foundEntry) {
g.drawString("Your searched item has been found", 400, 200);
} else {
g.drawString("Your searched item has NOT been found", 400, 200);
}
this.foundEntry = false;
this.searchEntry = false;
}
if (this.newEntry) {
if (this.inputString()) {
g.drawString("The new entry '" + this.newValue + "' has been added", 300,
100);
}
else {
g.drawString("Sorry, the array is already full!", 300, 100);
}
this.newValue = "";
this.newEntry = false;
}
for (int i = 0; i < array.length; i++) {
g.drawString(array[i], this.startX + this.xSpace * i, this.startY + this.ySpace * i);
}
}
public void setNewValue(String newString) {
this.newValue = newString;
this.newEntry = true;
}
public void seekEntry(String word) {
this.searchEntry = true;
for(int i = 0; i < array.length; i++){
if(array[i].equals(word)) {
this.foundEntry = true;
}
}
}
}
-----------------------------------------------------------------------------------------------------------
Die eigentliche Klasse mit den Buttons und Textfeldern, ActionListener etc.
-----------------------------------------------------------------------------------------------------------
package playcards;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Uebung05 extends Applet implements ActionListener {
private StringClass myString = new StringClass();
private TextField newEntry, searchEntry;
private Button enter, search;
public void init() {
newEntry = new TextField(20);
add(newEntry);
enter = new Button("Enter new value");
add(enter);
enter.addActionListener(this);
searchEntry = new TextField(20);
add(searchEntry);
search = new Button("Search for this value");
add(search);
search.addActionListener(this);
}
public void actionPerformed(ActionEvent event) {
if(event.getSource() == search) {
myString.seekEntry(searchEntry.getText());
}
if(event.getSource() == enter) {
if (newEntry.getText() != "") {
myString.setNewValue(newEntry.getText());
}
}
repaint();
}
public void paint(Graphics g){
myString.display(g);
}
}