Advertisement
sedran

A Simple Drawing Example

Jan 29th, 2012
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.78 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Dimension;
  3. import java.awt.Graphics;
  4. import java.awt.Toolkit;
  5. import java.util.ArrayList;
  6. import java.util.Iterator;
  7. import java.util.Random;
  8.  
  9. import javax.swing.JFrame;
  10. import javax.swing.JPanel;
  11.  
  12. public class Serdar extends JPanel implements Runnable {
  13.     private Random r = new Random();
  14.     private static final long serialVersionUID = 1L;
  15.     private ArrayList<Oval> list = new ArrayList<Oval>();
  16.  
  17.     public static void main(String args[])  {
  18.         JFrame window = new JFrame("Water");
  19.         window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  20.         Serdar serdar = new Serdar();
  21.         Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
  22.         dim.width -= 100; dim.height -= 100;
  23.         serdar.setPreferredSize(dim);
  24.         window.setContentPane(serdar);
  25.         window.pack();
  26.         window.setLocationRelativeTo(null);
  27.         window.setVisible(true);
  28.     }
  29.    
  30.     public Serdar() {
  31.         super();
  32.         Thread t = new Thread(this);
  33.         t.start();
  34.     }
  35.    
  36.     public void paint(Graphics g) {
  37.         Iterator<Oval> it = list.iterator();
  38.         g.setColor(Color.WHITE);
  39.         g.fillRect(0, 0, getWidth(), getHeight());
  40.         while( it.hasNext() ) {
  41.             Oval o = it.next();
  42.             if( o.r >= 50 ) {
  43.                 it.remove();
  44.                 continue;
  45.             }
  46.             g.setColor(o.c);
  47.             o.r += 2;
  48.             g.drawOval(o.x-o.r, o.y-o.r, 2*o.r, 2*o.r);
  49.         }
  50.     }
  51.  
  52.     public void run() {
  53.         long time = 0;
  54.         while(true) {
  55.             repaint();
  56.             time += 50;
  57.             try {
  58.                 Thread.sleep(50);
  59.             } catch (Exception e) {}
  60.             if( time % 300 == 0 ) {
  61.                 Color c = new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255));
  62.                 list.add(new Oval(r.nextInt(getWidth()), r.nextInt(getHeight()), c));
  63.             }
  64.         }
  65.     }
  66.    
  67.     private class Oval {
  68.         public Oval(int x, int y, Color c) {
  69.             this.x = x;
  70.             this.y = y;
  71.             this.c = c;
  72.         }
  73.         int x, y, r = 0;
  74.         Color c;
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement