Advertisement
advictoriam

Untitled

Feb 6th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.18 KB | None | 0 0
  1. import java.awt.Dimension;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import javax.swing.JButton;
  5. import javax.swing.JFrame;
  6. import javax.swing.JPanel;
  7. import javax.swing.Timer;
  8.  
  9. public class RectangleMover implements ActionListener
  10. {
  11.    public static RectangleComponent component;
  12.    public static void main(String[] args)
  13.    {
  14.       JFrame frame = new JFrame();
  15.  
  16.       frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
  17.       frame.setTitle("A moving rectangle");
  18.       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  19.  
  20.       JPanel panel = new JPanel();
  21.       component = new RectangleComponent();
  22.       component.setPreferredSize(new Dimension(COMPONENT_WIDTH, COMPONENT_HEIGHT));
  23.       panel.add(component);
  24.  
  25.       JButton button = new JButton("Move");
  26.       panel.add(button);
  27.  
  28.       frame.add(panel);
  29.       frame.setVisible(true);      
  30.    }
  31.    
  32.    public void actionPerformed(ActionEvent e)
  33.    {
  34.       component.moveBy(10, 10);
  35.    }
  36.    
  37.    private static final int FRAME_WIDTH = 300;
  38.    private static final int FRAME_HEIGHT = 400;
  39.    private static final int COMPONENT_WIDTH = 200;
  40.    private static final int COMPONENT_HEIGHT = 400;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement