Java Bouncing Ball Activity
Java Bouncing Ball Activity
package ball;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
Ball(){
setForeground(Color.red);
move = new java.util.Timer();
move.scheduleAtFixedRate(new TimerTask()
{
if(x<0)
dx=1;
if(x>=getWidth()-45)
dx=-1;
if(y<0)
dy=1;
if(y>=getHeight()-45)
dy=-1;
x+=dx;
y+=dy;
repaint();
}
},0,5
);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint(Graphics g)
{
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g2d.fillOval(x,y,width,height);
}
public static void main(String[] args)
{
Ball ball = new Ball();
frame.add(ball);
}
}