Restart Button für einfach Applikation

Status
Nicht offen für weitere Antworten.

pageho

Mitglied
hallo

hab mich jetzt entschlossen mich hier anzumelden weil ich nicht weiterkomme

und zwar hab ich hir ein simples applet:

Code:
import java.applet.Applet;
import java.util.*;
import java.awt.*;

public class SimpleDraw extends Applet {
   Vector  drawnShapes;
   Choice  shapeChoice;
   Choice  colorChoice;
   
   /** Create the Graphic User Interface (GUI). */
   public void init() {
      drawnShapes = new Vector();
      
      shapeChoice = new Choice();
      shapeChoice.addItem("Circle");
      shapeChoice.addItem("Square");
      add(shapeChoice);
      
      colorChoice = new Choice();
      colorChoice.addItem("Red");
      colorChoice.addItem("Green");
      colorChoice.addItem("Blue");
      add(colorChoice);
   }
   
   /** Draw all the shapes. */
   public void paint(Graphics g) {
      Shape s;
      int numShapes;
      
      numShapes = drawnShapes.size();
      for (int i = 0; i < numShapes; i++) {
      	
      	// casting (Typen-Konvertierung): circle -> shape bzw. square -> shape
         s = (Shape)drawnShapes.elementAt(i); 
         
         // When the shape draws, circles and squares each invoke their own
         // draw method, depending on which shape this is.
         s.draw(g);  
      }
   }
   
   /** Create a new shape. */
   public boolean mouseUp(Event e, int x, int y) {
   
      Shape s;  // This shape will be either a circle or a square.
   
      String shapeString = shapeChoice.getSelectedItem();
      String colorString = colorChoice.getSelectedItem();
      
      if (shapeString.equals("Circle"))
         s = new Circle();
      else
         s = new Square();
      
      if (colorString.equals("Red"))
         s.color = Color.red;
      else if (colorString.equals("Green"))
         s.color = Color.green;
      else
         s.color = Color.blue;
         
      s.x = x;
      s.y = y;
      
      drawnShapes.addElement(s);
      
      repaint();
      
      return true;
   }
}

/** Shapes provide common characteristics for the circle and square. */
abstract class Shape {
   static public final int shapeRadius = 20;
   
   Color color;
   int x;
   int y;
   
   abstract void draw(Graphics g);
}

/** Draws and maintains circle information. */
class Circle extends Shape {
   void draw(Graphics g) {
      g.setColor(this.color);
      g.fillOval(this.x - shapeRadius, this.y - shapeRadius, shapeRadius * 2, shapeRadius * 2);
   }
}

/** Draws and maintains square information. */
class Square extends Shape{
   void draw(Graphics g) {
      g.setColor(this.color);
      g.fillRect(this.x - shapeRadius, this.y - shapeRadius, shapeRadius * 2, shapeRadius * 2);  
   }
}

ich möchte hier ein button einfügen der einen restart des ganzen im applet ausführt


Danke im Vorraus
 
wieso kompletten neustart?
wenn du nur willst das alles gemalte weg kommt mach einfach drawnShapes.clear() und danach ein repaint()
 
SebiB90 hat gesagt.:
wieso kompletten neustart?
wenn du nur willst das alles gemalte weg kommt mach einfach drawnShapes.clear() und danach ein repaint()


danke für die schnelle antwort

die einzige frage die mir da noch bleibt ist, wie setze ich das ganze auf einen button? hab da eben noch nie mit gearbeitet


danke
 
Einfach
Code:
Button clear = new Button("Alles löschen");
clear.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        drawnShapes.clear();
        repaint()
    }});
add(clear);
in deine init()-Methode an gewünschter Stelle einfügen
 
Leroy42 hat gesagt.:
Hier noch ein
Code:
";"
😀
ich danke dir vielmals für deine lösung


hab das jetzt so gemacht, nur noch ein fehler ist da, der bringt die ganze zeit:
cannot resolve symbol - class ActionListener

was heisst das genau?



danke
 
Status
Nicht offen für weitere Antworten.

Neue Themen


Zurück
Oben