Advertisement
Guest User

Untitled

a guest
Apr 5th, 2011
498
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.91 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.Color;
  3. import java.awt.Dimension;
  4. import java.awt.Graphics;
  5. import java.awt.HeadlessException;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.util.Random;
  9.  
  10. import javax.swing.JButton;
  11. import javax.swing.JFrame;
  12. import javax.swing.JPanel;
  13. import javax.swing.SwingUtilities;
  14.  
  15.  
  16. public class Rectangles extends JFrame {
  17.  
  18.     private int rectangles=1;
  19.     private Random random = new Random();
  20.    
  21.     public Rectangles() {
  22.         super();
  23.         setLayout(new BorderLayout());
  24.         JButton more = new JButton("more");
  25.         JButton fewer = new JButton("fewer");
  26.         more.addActionListener(new ActionListener() {
  27.            
  28.             @Override
  29.             public void actionPerformed(ActionEvent e) {
  30.                 rectangles*=2; 
  31.                 Rectangles.this.repaint();
  32.             }
  33.         });
  34.         fewer.addActionListener(new ActionListener() {
  35.            
  36.             @Override
  37.             public void actionPerformed(ActionEvent e) {
  38.                 if(rectangles>=2){
  39.                     rectangles/=2;
  40.                     Rectangles.this.repaint();
  41.                 }
  42.             }
  43.         });
  44.         JPanel panel = new JPanel(){
  45.  
  46.             @Override
  47.             protected void paintComponent(Graphics g) {
  48.                 super.paintComponent(g);
  49.                 g.setColor(Color.BLACK);
  50.                 for(int i=1;i<=rectangles;i++){
  51.                     int x= (int)Math.round(random.nextDouble()*getWidth());
  52.                     int y=(int)Math.round(random.nextDouble()*getHeight());
  53.                     int w = (int)Math.round(random.nextDouble()*(getWidth()-x));
  54.                     int h = (int)Math.round(random.nextDouble()*(getHeight()-y));
  55.                     g.drawRect(x, y, w, h);
  56.                 }
  57.             }
  58.            
  59.         };
  60.         add(more,BorderLayout.EAST);
  61.         add(fewer,BorderLayout.WEST);
  62.         add(panel,BorderLayout.CENTER);
  63.     }
  64.    
  65.     public static void main(String[] args) {
  66.         SwingUtilities.invokeLater(new Runnable(){
  67.  
  68.             @Override
  69.             public void run() {
  70.                 Rectangles r = new Rectangles();
  71.                 r.setTitle("Random rectangles demo");
  72.                 r.setSize(new Dimension(400,300));
  73.                 r.setVisible(true);
  74.             }
  75.            
  76.         });
  77.        
  78.     }
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement