1: import javax.swing.JPanel;
2: import javax.swing.SwingUtilities;
3: import javax.swing.JWindow;
4: import java.util.*;
5: import java.awt.*;
6: import java.awt.event.*;
7: import java.awt.image.*;
8: import java.awt.geom.*;
9:
10: public class Bola extends JWindow{
11: private ArrayList balls = new ArrayList();
12: private Timer timer;
13: private TimerTask task;
14: private int counter;
15: private Robot robot;
16: private BufferedImage image;
17: private Dimension windowDimension;
18: public Bola(){
19: super();
20: windowDimension = Toolkit.getDefaultToolkit().getScreenSize();
21: try {
22: robot = new Robot();
23: }
24: catch (Exception e){
25: System.out.print(e.getMessage());
26: }
27: timer = new Timer();
28: task = new TimerTask(){
29: public void run(){
30: if (counter%100==0 && balls.size()<=20){
31: balls.add(new ball(windowDimension));
32: }
33: for (int i = 0;i < balls.size(); i++){
34: ball b = (ball)balls.get(i);
35: b.move();
36: }
37: counter++;
38: repaint();
39: }
40: };
41: timer.schedule(task,0,1);
42: setSize(windowDimension);
43: setAlwaysOnTop(true);
44: setVisible(true);
45: addMouseListener(new MouseAdapter(){
46: public void mousePressed(MouseEvent m){
47: System.exit(1);
48: }
49: });
50: image = robot.createScreenCapture(new Rectangle(this.getLocationOnScreen(),windowDimension));
51: add(new JPanel(){
52: public void paintComponent(Graphics g){
53: Graphics2D g2 = (Graphics2D)g;
54: g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
55: g2.drawImage(image,0,0,null);
56: for (int i = 0;i < balls.size(); i++){
57: ball b = (ball)balls.get(i);
58: b.draw(g2);
59: }
60: }
61: });
62: }
63: public static void main(String[] args){
64: SwingUtilities.invokeLater(new Runnable(){
65: public void run(){
66: new Bola();
67: }
68: });
69: }
70: }
71: class ball {
72: private int x,y,xC=2,yC=-2,ballSize = 100;
73: private Random rand;
74: private int width,height;
75: public ball(Dimension dimension){
76: rand = new Random();
77: width = (int)dimension.getWidth();
78: height = (int)dimension.getHeight();
79: x = 0;
80: y = rand.nextInt(height-ballSize);
81: }
82: public void draw(Graphics2D g2){
83: g2.drawOval(x,y,ballSize,ballSize);
84: }
85: public void move(){
86: x+=xC;
87: y+=yC;
88: if (x<=0){
89: x=0;xC=2;
90: }
91: if (x>=width-ballSize){
92: x=width-ballSize;xC=-2;
93: }
94: if (y<=0){
95: y=0;yC=2;
96: }
97: if (y>=height-ballSize){
98: y=height-ballSize;yC=-2;
99: }
100: }
101: }
Comments