Universität Paderborn - Home Universität Paderborn
Die Universität der Informationsgesellschaft

Objektorientierte Programmierung WS 2013/2014 - Datei BallWorld.java

import java.awt.*;
import java.awt.event.*;


public class BallWorld extends Frame { 
    public static void main(String[] args) {
        Color bColor = new Color((float) Math.random(), (float) Math.random(),
                (float) Math.random());
        BallWorld world = new BallWorld(bColor);

        world.setVisible(true);
    }
    private static final int FrameWidth = 300;
    private static final int FrameHeight = 200;
    private MovableBall aBall;
    private BallWorld(Color ballColor) {
        // constructor for new ball world
        // resize our frame
        setSize(FrameWidth, FrameHeight);
        setTitle("Ball World");

        WindowListener wl = new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        };

        addWindowListener(wl);

        // initialize object data field
        aBall = new MovableBall(10, 15, 10);
        aBall.setColor(ballColor);
        aBall.setMotion(3.0, 6.0);
    }

    public void paint(Graphics g) {
        // first, draw the ball
        aBall.paint(g);
        // then move it slightly
        aBall.move();
        if ((aBall.x() < 0) || (aBall.x() > FrameWidth)) {
            aBall.setMotion(-aBall.xMotion(), aBall.yMotion());
        }
        if ((aBall.y() < 0) || (aBall.y() > FrameHeight)) {
            aBall.setMotion(aBall.xMotion(), -aBall.yMotion());
        }
        repaint();
        try {
            Thread.currentThread().sleep(154);
        } catch (InterruptedException e) {}
    }
}

Generiert mit Camelot | Probleme mit Camelot? | Geändert am: 05.11.2013