Advertisement
Guest User

BouncingLabels.java

a guest
Nov 4th, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.44 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Container;
  3. import java.awt.Dimension;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.MouseAdapter;
  7. import java.awt.event.MouseEvent;
  8.  
  9. import javax.swing.JFrame;
  10. import javax.swing.JLabel;
  11. import javax.swing.SwingUtilities;
  12. import javax.swing.Timer;
  13.  
  14.  
  15. @SuppressWarnings("serial")
  16. public class BouncingLabels extends JFrame {
  17.  
  18.     // this is our bouncing label component. it bounces around its parent. this could
  19.     // be pulled out into its own class file; its in here to keep the example self
  20.     // contained.
  21.     static class BouncingLabel extends JLabel {
  22.      
  23.         private int fieldWidth, fieldHeight; // width/height of parent at creation time.
  24.         private int velX = 1, velY = 1; // current x and y velocity.
  25.        
  26.         // constructor sets base label properties and starts a timer.
  27.         public BouncingLabel (int fieldWidth, int fieldHeight) {
  28.            
  29.             this.fieldWidth = fieldWidth;
  30.             this.fieldHeight = fieldHeight;
  31.            
  32.             setBounds(0, 0, 60, 20);
  33.             setOpaque(true);
  34.             setBackground(Color.RED);
  35.             setForeground(Color.WHITE);
  36.             setText("HELLO");
  37.             setVisible(true);
  38.          
  39.             // timer will call step() every 10ms.
  40.             new Timer(10, new ActionListener() {
  41.                 @Override public void actionPerformed (ActionEvent e) {
  42.                     step();
  43.                 }
  44.             }).start();
  45.            
  46.         }
  47.        
  48.         // step updates the component position. note that no explicit painting is done.
  49.         void step () {
  50.            
  51.             int x = getX();
  52.             int y = getY();
  53.             int maxx = fieldWidth - getWidth();
  54.             int maxy = fieldHeight - getHeight();
  55.            
  56.             x += velX;
  57.             y += velY;
  58.  
  59.             if ((x >= maxx && velX > 0) || (x <= 0 && velX < 0))
  60.                 velX = -velX;
  61.             if ((y >= maxy && velY > 0) || (y <= 0 && velY < 0))
  62.                 velY = -velY;
  63.            
  64.             setLocation(x, y);
  65.            
  66.         }
  67.        
  68.     }
  69.    
  70.     // BouncingLabels is our main frame; click on it to add a label.
  71.     BouncingLabels () {
  72.        
  73.         // work with the content pane, not the frame itself.
  74.         final Container c = getContentPane();
  75.         c.setPreferredSize(new Dimension(300, 300));
  76.         c.setLayout(null);
  77.         setResizable(false);
  78.         pack();
  79.  
  80.         // add an initial bouncing object.
  81.         c.add(new BouncingLabel(c.getWidth(), c.getHeight()));
  82.        
  83.         // clicking on the frame will add a new object.
  84.         addMouseListener(new MouseAdapter() {
  85.             @Override public void mouseClicked (MouseEvent e) {
  86.                 if (e.getButton() == MouseEvent.BUTTON1)
  87.                     c.add(new BouncingLabel(c.getWidth(), c.getHeight()));
  88.             }            
  89.         });
  90.  
  91.     }
  92.    
  93.     // main method creates and shows a BouncingLabels frame.
  94.     public static void main (String[] args) {
  95.         SwingUtilities.invokeLater(new Runnable() {
  96.             @Override public void run () {
  97.                 BouncingLabels b = new BouncingLabels();
  98.                 b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  99.                 b.setLocationRelativeTo(null);
  100.                 b.setVisible(true);
  101.             }
  102.         });
  103.     }
  104.    
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement