Hi Jungs,
ich habe im Netz das schöne Bouncing Ball Beispiel gefunden, wo ein Ball immer von Ecke zu Ecke hüpft...
So, ich habe jedoch ein sehr, sehr ausführliches Beispiel gefunden, wo erst einmal auf einen Start-Button gedrückt werden muss, damit alles los geht...
Ich habe das versucht zu entfernen, jedoch ist dann leider kein Ball mehr zu sehen...
Kann mir jemand helfen, dass zu start des Programms sofort der Ball hüpft. Auch die Buttons Start und Ende wären zu viel...
Der Code:
ich habe im Netz das schöne Bouncing Ball Beispiel gefunden, wo ein Ball immer von Ecke zu Ecke hüpft...
So, ich habe jedoch ein sehr, sehr ausführliches Beispiel gefunden, wo erst einmal auf einen Start-Button gedrückt werden muss, damit alles los geht...
Ich habe das versucht zu entfernen, jedoch ist dann leider kein Ball mehr zu sehen...
Kann mir jemand helfen, dass zu start des Programms sofort der Ball hüpft. Auch die Buttons Start und Ende wären zu viel...
Der Code:
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
///////////////////////////////////////////////////////////////////
// main() creating a frame object and displaying it on the screen.
public class Bounce {
public static void main(String args[]) {
JFrame frame = new BounceFrame();
frame.show();
}
}
class BounceFrame extends JFrame {
private JPanel canvas;
private static final int XSIZE = 10;
private static final int YSIZE = 10;
private int x = 0;
private int y = 0;
private int dx = 2;
private int dy = 2;
// Constructor is just creating the pane filling it with information.
public BounceFrame() {
setSize(300, 200);
setTitle("Bounce");
addWindowListener(new WindowAdapter() {
public void windowCLosing(WindowEvent e) {
System.exit(0);
}
});
Container contentPane = getContentPane();
canvas = new JPanel();
contentPane.add(canvas, "Center");
JPanel p = new JPanel();
addButton(p, "Start", new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Ball b = new Ball(canvas); // Creating the ball and bouncing it.
b.bounce();
}
});
addButton(p, "Close", new ActionListener() {
public void actionPerformed(ActionEvent evt) {
System.exit(0);
}
});
contentPane.add(p, "South");
}
/////////////////////////////////////////////////////////////////
// Declaring the actions caused by adding a button.
public void addButton(Container c, String title, ActionListener a) {
JButton b = new JButton(title);
c.add(b);
b.addActionListener(a);
}
/////////////////////////////////////////////////////////////////
// Class to move the ball across the screen.
class Ball {
private JPanel box;
public Ball(JPanel b) {
box = b;
}
// Drawing the ball.
public void draw() {
Graphics g = box.getGraphics();
g.fillOval(x, y, XSIZE, YSIZE);
g.dispose();
}
// Method actually moving the ball across screen.
public void move() {
Graphics g = box.getGraphics();
g.setXORMode(box.getBackground());
g.fillOval(x, y, XSIZE, YSIZE);
x += dx;
y += dy;
Dimension d = box.getSize();
if (x<0) {
x = 0;
dx = -dx;
}
if (x + XSIZE >= d.width) {
x = d.width - XSIZE;
dx = -dx;
}
if (y < 0) {
y = 0;
dy = -dy;
}
if (y + YSIZE >= d.height) {
y = d.height - YSIZE;
dy = -dy;
}
g.fillOval(x, y, XSIZE, YSIZE);
g.dispose();
}
// Move the ball, then sleep for 25 ms, and move again, etc.
public void bounce() {
draw();
for (int i=1; i<500; i++) {
move();
try {
Thread.sleep(25);
}
catch (InterruptedException e) {
System.err.println();
}
}
}
}
}