Online Test Programmieren 2

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Online Test Programmieren 2

    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

    Quellcode

    1. package OnlineTest;
    2. import java.awt.*;
    3. import java.awt.event.*;
    4. @SuppressWarnings("serial")
    5. public class Animation extends Frame implements ItemListener {
    6. private Canvas canvas;
    7. public static String buttontext = "Start";
    8. public static void main(String[] args) {
    9. new Animation();
    10. }
    11. /**
    12. * Konstruktor der GUI
    13. */
    14. public Animation() {
    15. super("Mini Animation");
    16. this.canvas = new AnimationCanvas();
    17. //Fenster schliessen
    18. addWindowListener(new WindowAdapter() {
    19. public void windowClosing(WindowEvent e) {
    20. System.exit(0);
    21. }
    22. });
    23. //Optionen fuer die Zeichenflaeche
    24. canvas.setBackground(Color.LIGHT_GRAY);
    25. canvas.setSize(800, 600);
    26. //Layout Initialisierung
    27. GridBagLayout gridB = new GridBagLayout();
    28. GridBagConstraints layout = new GridBagConstraints();
    29. setLayout(gridB);
    30. //Layout Variablen
    31. layout.gridwidth = 1;
    32. layout.gridheight = 2;
    33. layout.gridx = 1;
    34. layout.gridy = 1;
    35. layout.fill = GridBagConstraints.BOTH; //horizontal und vertikal auf MAX
    36. //Zeichenflaeche hinzufuegen
    37. gridB.setConstraints(canvas, layout);
    38. add(canvas);
    39. //Hier beginnt die rechte Spalte
    40. layout.gridx++;
    41. layout.gridheight = 1;
    42. //Choice x-period
    43. Choice xxx = new Choice();
    44. for (int x = 1; x <= 10; x += 1)
    45. xxx.add(x + " balls(s)");
    46. xxx.addItemListener(new ItemListener() {
    47. public void itemStateChanged(ItemEvent event) {
    48. AnimationCanvas.balls = ((Choice) event.getSource()).getSelectedIndex()+1;
    49. System.out.println("ausgewaehlt: "+AnimationCanvas.balls);
    50. }
    51. });
    52. gridB.setConstraints(xxx, layout);
    53. add(xxx);
    54. layout.gridy++;
    55. // Start Button
    56. Button button = new Button("Start");
    57. button.addActionListener(new ActionListener() {
    58. @SuppressWarnings("static-access")
    59. public void actionPerformed(ActionEvent e) {
    60. Animation.buttontext = "Stop";
    61. if(((AnimationCanvas) canvas).cancel == true) {
    62. ((Button)e.getSource()).setLabel("Stop");
    63. ((AnimationCanvas) canvas).startAnimation();
    64. } else {
    65. canvas.repaint();
    66. ((AnimationCanvas) canvas).stopAnimation();
    67. ((Button)e.getSource()).setLabel("Start");
    68. }
    69. }
    70. });
    71. gridB.setConstraints(button, layout);
    72. add(button);
    73. setSize(800,600);
    74. setLocation(150,100);
    75. setVisible(true);
    76. pack();
    77. }
    78. public void itemStateChanged(ItemEvent e) {
    79. }
    80. }
    Alles anzeigen


    AnimationCanvas.java

    Quellcode

    1. package OnlineTest;
    2. import java.awt.*;
    3. @SuppressWarnings("serial")
    4. public class AnimationCanvas extends Canvas implements Runnable {
    5. public static boolean cancel = true;
    6. public static int balls = 1;
    7. /**
    8. * startet den Thread fuer die Animation
    9. */
    10. public void startAnimation() {
    11. cancel = false;
    12. try {
    13. new Thread(this).start();
    14. } catch (Exception e){}
    15. }
    16. /**
    17. * startet den Thread fuer die Animation
    18. */
    19. public void stopAnimation() {
    20. cancel = true;
    21. new Thread(this).stop();
    22. }
    23. /**
    24. * zeichnet die Lissajou Kurve wieder und wieder
    25. */
    26. public void run() {
    27. MyPencil[] pencils = new MyPencil[AnimationCanvas.balls];
    28. int maxSchrittweite = 25; //=speed
    29. for(int i=0; i<AnimationCanvas.balls; i++) {
    30. pencils[i] = new MyPencil(getGraphics(), this.getWidth(), this.getHeight(),
    31. (int)(Math.random()*this.getWidth()), (int)(Math.random()*this.getHeight()),
    32. maxSchrittweite);
    33. }
    34. while(!cancel) { //zeichne so lange bis cancel gesetzt wird
    35. // repaint();
    36. for(int i=0; i<AnimationCanvas.balls; i++) {
    37. pencils[i].setPositions(getGraphics());
    38. }
    39. //Schlafe hier (nicht in der MyPencil, sonst bewegen sich nicht alle zusammen)
    40. try {
    41. Thread.sleep(40);
    42. } catch (Exception e) {}
    43. }
    44. }
    45. }
    Alles anzeigen



    MyPencil.java

    Quellcode

    1. package OnlineTest;
    2. import java.awt.*;
    3. /**
    4. * hier wird gezeichnet
    5. * @author Torben Brodt
    6. */
    7. public class MyPencil {
    8. private int x, y, dx, dy;
    9. private int size=50;
    10. private int maxX, maxY;
    11. /**
    12. * wird mit der schrittweite multipliziert
    13. * gibt also die richtung an
    14. * @return 1 oder -1
    15. */
    16. private int leftRightOrUpDown() {
    17. double d = Math.random();
    18. if (d<0.5)
    19. return -1;
    20. else
    21. return 1;
    22. }
    23. /**
    24. * Grafikobjekt
    25. * @param g
    26. */
    27. public void setPositions(Graphics g) {
    28. // alte Position mit Blockuebermalen
    29. g.setColor(Color.LIGHT_GRAY);
    30. g.fillOval(x, y, 50, 50);
    31. int addierex = leftRightOrUpDown()*dx;
    32. x += addierex;
    33. if (x > maxX - size || x < 0) {
    34. //mache rueckgaengig und gehe in die andere richtung
    35. x += -2 * addierex;
    36. }
    37. int addierey = leftRightOrUpDown()*dy;
    38. y += addierey;
    39. if (y > maxY - size || y < 0) {
    40. //mache rueckgaengig und gehe in die andere richtung
    41. y += -2 * addierey;
    42. }
    43. g.setColor(Color.BLACK);
    44. g.fillOval(this.x, this.y, this.size, this.size);
    45. }
    46. /**
    47. * Konstruktor
    48. * @param g Grafikobjekt
    49. * @param maxX breite der spalte
    50. * @param maxY hoehe der spalte
    51. * @param posX x position des kreises
    52. * @param posY y position des kreises
    53. * @param speed schrittweite
    54. */
    55. public MyPencil(Graphics g, int maxX, int maxY, int posX, int posY, int speed) {
    56. this.maxX=maxX;
    57. this.maxY=maxY;
    58. this.x=posX;
    59. this.y=posY;
    60. //dynamische Geschwindigkeit
    61. this.dx = speed;
    62. this.dy = speed;
    63. }
    64. }
    Alles anzeigen