Soso, Samstag in der früh war mal wieder ein Online Test.
Doch die Aufgabe hab ich ja mal total in den Sand gesetzt.
* repaint() beim Stop vergessen
* Kollisionsabfrage mit der Wand fehlerhaft (falsche Variable wird geändert)
Aber jetzt die Frage: Bei all euren Lösungen (die, die ich gesehen habe) laufen die Bälle gerade Linien entlang - ich habs aber so verstanden, dass die Position zufällig geändert werden soll - meine springen verrückt hin und her - was denkt ihr?
Hier also meine Lösung:
Animation.java
Alles anzeigen
AnimationCanvas.java
Alles anzeigen
MyPencil.java
Alles anzeigen
Doch die Aufgabe hab ich ja mal total in den Sand gesetzt.
* repaint() beim Stop vergessen
* Kollisionsabfrage mit der Wand fehlerhaft (falsche Variable wird geändert)
Aber jetzt die Frage: Bei all euren Lösungen (die, die ich gesehen habe) laufen die Bälle gerade Linien entlang - ich habs aber so verstanden, dass die Position zufällig geändert werden soll - meine springen verrückt hin und her - was denkt ihr?
Hier also meine Lösung:
Animation.java
Quellcode
- package OnlineTest;
- import java.awt.*;
- import java.awt.event.*;
- @SuppressWarnings("serial")
- public class Animation extends Frame implements ItemListener {
- private Canvas canvas;
- public static String buttontext = "Start";
- public static void main(String[] args) {
- new Animation();
- }
- /**
- * Konstruktor der GUI
- */
- public Animation() {
- super("Mini Animation");
- this.canvas = new AnimationCanvas();
- //Fenster schliessen
- addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- System.exit(0);
- }
- });
- //Optionen fuer die Zeichenflaeche
- canvas.setBackground(Color.LIGHT_GRAY);
- canvas.setSize(800, 600);
- //Layout Initialisierung
- GridBagLayout gridB = new GridBagLayout();
- GridBagConstraints layout = new GridBagConstraints();
- setLayout(gridB);
- //Layout Variablen
- layout.gridwidth = 1;
- layout.gridheight = 2;
- layout.gridx = 1;
- layout.gridy = 1;
- layout.fill = GridBagConstraints.BOTH; //horizontal und vertikal auf MAX
- //Zeichenflaeche hinzufuegen
- gridB.setConstraints(canvas, layout);
- add(canvas);
- //Hier beginnt die rechte Spalte
- layout.gridx++;
- layout.gridheight = 1;
- //Choice x-period
- Choice xxx = new Choice();
- for (int x = 1; x <= 10; x += 1)
- xxx.add(x + " balls(s)");
- xxx.addItemListener(new ItemListener() {
- public void itemStateChanged(ItemEvent event) {
- AnimationCanvas.balls = ((Choice) event.getSource()).getSelectedIndex()+1;
- System.out.println("ausgewaehlt: "+AnimationCanvas.balls);
- }
- });
- gridB.setConstraints(xxx, layout);
- add(xxx);
- layout.gridy++;
- // Start Button
- Button button = new Button("Start");
- button.addActionListener(new ActionListener() {
- @SuppressWarnings("static-access")
- public void actionPerformed(ActionEvent e) {
- Animation.buttontext = "Stop";
- if(((AnimationCanvas) canvas).cancel == true) {
- ((Button)e.getSource()).setLabel("Stop");
- ((AnimationCanvas) canvas).startAnimation();
- } else {
- canvas.repaint();
- ((AnimationCanvas) canvas).stopAnimation();
- ((Button)e.getSource()).setLabel("Start");
- }
- }
- });
- gridB.setConstraints(button, layout);
- add(button);
- setSize(800,600);
- setLocation(150,100);
- setVisible(true);
- pack();
- }
- public void itemStateChanged(ItemEvent e) {
- }
- }
AnimationCanvas.java
Quellcode
- package OnlineTest;
- import java.awt.*;
- @SuppressWarnings("serial")
- public class AnimationCanvas extends Canvas implements Runnable {
- public static boolean cancel = true;
- public static int balls = 1;
- /**
- * startet den Thread fuer die Animation
- */
- public void startAnimation() {
- cancel = false;
- try {
- new Thread(this).start();
- } catch (Exception e){}
- }
- /**
- * startet den Thread fuer die Animation
- */
- public void stopAnimation() {
- cancel = true;
- new Thread(this).stop();
- }
- /**
- * zeichnet die Lissajou Kurve wieder und wieder
- */
- public void run() {
- MyPencil[] pencils = new MyPencil[AnimationCanvas.balls];
- int maxSchrittweite = 25; //=speed
- for(int i=0; i<AnimationCanvas.balls; i++) {
- pencils[i] = new MyPencil(getGraphics(), this.getWidth(), this.getHeight(),
- (int)(Math.random()*this.getWidth()), (int)(Math.random()*this.getHeight()),
- maxSchrittweite);
- }
- while(!cancel) { //zeichne so lange bis cancel gesetzt wird
- // repaint();
- for(int i=0; i<AnimationCanvas.balls; i++) {
- pencils[i].setPositions(getGraphics());
- }
- //Schlafe hier (nicht in der MyPencil, sonst bewegen sich nicht alle zusammen)
- try {
- Thread.sleep(40);
- } catch (Exception e) {}
- }
- }
- }
MyPencil.java
Quellcode
- package OnlineTest;
- import java.awt.*;
- /**
- * hier wird gezeichnet
- * @author Torben Brodt
- */
- public class MyPencil {
- private int x, y, dx, dy;
- private int size=50;
- private int maxX, maxY;
- /**
- * wird mit der schrittweite multipliziert
- * gibt also die richtung an
- * @return 1 oder -1
- */
- private int leftRightOrUpDown() {
- double d = Math.random();
- if (d<0.5)
- return -1;
- else
- return 1;
- }
- /**
- * Grafikobjekt
- * @param g
- */
- public void setPositions(Graphics g) {
- // alte Position mit Blockuebermalen
- g.setColor(Color.LIGHT_GRAY);
- g.fillOval(x, y, 50, 50);
- int addierex = leftRightOrUpDown()*dx;
- x += addierex;
- if (x > maxX - size || x < 0) {
- //mache rueckgaengig und gehe in die andere richtung
- x += -2 * addierex;
- }
- int addierey = leftRightOrUpDown()*dy;
- y += addierey;
- if (y > maxY - size || y < 0) {
- //mache rueckgaengig und gehe in die andere richtung
- y += -2 * addierey;
- }
- g.setColor(Color.BLACK);
- g.fillOval(this.x, this.y, this.size, this.size);
- }
- /**
- * Konstruktor
- * @param g Grafikobjekt
- * @param maxX breite der spalte
- * @param maxY hoehe der spalte
- * @param posX x position des kreises
- * @param posY y position des kreises
- * @param speed schrittweite
- */
- public MyPencil(Graphics g, int maxX, int maxY, int posX, int posY, int speed) {
- this.maxX=maxX;
- this.maxY=maxY;
- this.x=posX;
- this.y=posY;
- //dynamische Geschwindigkeit
- this.dx = speed;
- this.dy = speed;
- }
- }