rendered paste bodyimport java.awt.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ball extends JPanel implements Runnable {
/* Konstruktor */
int x_pos = 25;
int y_pos = 100;
int radius = 20;
public ball () {}
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(new java.awt.color.Color(255,255,255));
g.fillRect(0, 0, 300, 300);
g.setColor(Color.RED);
g.fillOval(x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
}
public void run() {
/* Geringe Priorität setzen */
//Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while (true) {
/* Neuzeichnen */
this.repaint();
/* Verändern der X-Koordinate des Balles */
x_pos++;
try {
Thread.sleep(20);
} catch (InterruptedException e) {
}
/* Priorität wieder auf maximum */
//Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
}
public static void main(String[] args) {
ball b = new ball();
Thread th = new Thread(b);
th.start();
JFrame win = new JFrame("Ball");
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.setContentPane(b);
win.setSize(300, 300);
win.setVisible(true);
}
}