Advertisement
kajacx

Karel Hrkal

Dec 15th, 2011
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.00 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. public class Simple2DApp {
  6.    
  7.     private static Rectangle r1, r2;
  8.     private static JPanel drawPanel;
  9.    
  10.     public static void main(String[] args) {
  11.        
  12.         r1 = new Rectangle(30, 20, 60, 30);
  13.         r2 = new Rectangle(360, 130, 120, 205);
  14.        
  15.         JFrame mainFrame = new JFrame("Easy 2D Java App");
  16.         mainFrame.setBounds(50, 50, 800, 600);
  17.         mainFrame.setLayout(null);
  18.        
  19.         drawPanel = new MyPanel(Color.red, Color.blue);
  20.         drawPanel.setBounds(20, 20, 750, 420);
  21.         drawPanel.setBackground(Color.white);
  22.        
  23.         ActionListener listener = new MyActionListener();
  24.        
  25.         Button increaseBut = new Button("Increase");
  26.         increaseBut.setActionCommand("increase");
  27.         increaseBut.addActionListener(listener);
  28.         increaseBut.setBounds(30, 510, 100, 35);
  29.        
  30.         Button decreaseBut = new Button("Decrease");
  31.         decreaseBut.setActionCommand("decrease");
  32.         decreaseBut.addActionListener(listener);
  33.         decreaseBut.setBounds(160, 510, 100, 35);
  34.        
  35.         Button switchBut = new Button("Switch");
  36.         switchBut.setActionCommand("switch");
  37.         switchBut.addActionListener(listener);
  38.         switchBut.setBounds(300, 510, 100, 35);
  39.        
  40.         mainFrame.add(drawPanel);
  41.         mainFrame.add(increaseBut);
  42.         mainFrame.add(decreaseBut);
  43.         mainFrame.add(switchBut);
  44.        
  45.         mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  46.         mainFrame.setVisible(true);
  47.        
  48.     }
  49.    
  50.     private static class MyPanel extends JPanel {
  51.         private final Color r1Color, r2Color;
  52.  
  53.         public MyPanel(Color r1Color, Color r2Color) {
  54.             this.r1Color = r1Color;
  55.             this.r2Color = r2Color;
  56.         }
  57.        
  58.         public void paint(Graphics g) {
  59.             super.paint(g);
  60.             g.setColor(r1Color);
  61.             g.fillRect(r1.x, r1.y, r1.width, r1.height);
  62.             g.setColor(r2Color);
  63.             g.fillRect(r2.x, r2.y, r2.width, r2.height);
  64.         }
  65.        
  66.     }
  67.    
  68.     private static class MyActionListener implements ActionListener {
  69.        
  70.         public void actionPerformed(ActionEvent e) {
  71.             if(e.getActionCommand().equals("increase")) {
  72.                 r1.height += 2;
  73.                 r1.width += 2;
  74.                 r1.x--;
  75.                 r1.y--;
  76.                 r2.height += 2;
  77.                 r2.width += 2;
  78.                 r2.x--;
  79.                 r2.y--;
  80.             }
  81.             else if(e.getActionCommand().equals("decrease")) {
  82.                 r1.height -= 2;
  83.                 r1.width -= 2;
  84.                 r1.x++;
  85.                 r1.y++;
  86.                 r2.height -= 2;
  87.                 r2.width -= 2;
  88.                 r2.x++;
  89.                 r2.y++;
  90.             }
  91.             else if(e.getActionCommand().equals("switch")) {
  92.                 Rectangle buff = r1;
  93.                 r1 = r2;
  94.                 r2 = buff;
  95.             }
  96.             drawPanel.repaint();
  97.         }
  98.        
  99.     }    
  100.    
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement