Advertisement
Guest User

Gui

a guest
Mar 3rd, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.77 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5.  
  6. public class Gui {
  7.     public static void main(String[] args) {
  8.         JFrame win = new MainWin();
  9.         win.setVisible(true);
  10.     }
  11. }
  12.  
  13. class MainWin extends JFrame implements ActionListener, FocusListener {
  14.     private Container c = new Container();
  15.     private PaintPanel painting = new PaintPanel();
  16.     private JPanel controls = new JPanel();
  17.     private String[] colorsLabel = {"red", "blue", "green", "yellow"};
  18.     private Color[] colors = {Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW};
  19.     private JComboBox<String> combo = new JComboBox<String>(colorsLabel);
  20.     private JLabel label = new JLabel("DIMEMSIONS");
  21.     private JTextField width = new JTextField(3);
  22.     private JTextField height = new JTextField(3);
  23.     private int w;
  24.     private int h;
  25.     private JCheckBox ovale = new JCheckBox("Ovale");
  26.     private JCheckBox rectangle = new JCheckBox("Rectangle");
  27.  
  28.     public MainWin() {
  29.         setTitle("FIGURES");
  30.         setSize(400, 250);
  31.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  32.         c = getContentPane();
  33.         c.setLayout(new BorderLayout());
  34.         c.add(painting);
  35.         c.add(controls, "South");
  36.         combo.addActionListener(this);
  37.         width.setText("400");
  38.         width.addActionListener(this);
  39.         width.addFocusListener(this);
  40.         height.setText("250");
  41.         height.addActionListener(this);
  42.         height.addFocusListener(this);
  43.         ovale.addActionListener(this);
  44.         rectangle.addActionListener(this);
  45.         controls.add(combo);
  46.         controls.add(label);
  47.         controls.add(width);
  48.         controls.add(height);
  49.         controls.add(ovale);
  50.         controls.add(rectangle);
  51.        
  52.     }
  53.    
  54.     public void actionPerformed(ActionEvent ev) {
  55.         Object source = ev.getSource();
  56.         if (source == combo) {
  57.             for (int i = 0; i < colors.length; i++) {
  58.                 if (combo.getSelectedItem() == colorsLabel[i])
  59.                     painting.setBackground(colors[i]);         
  60.             }
  61.         }
  62.         if  (source == width || source == height)
  63.             setPaintingSize();
  64.         if (source == ovale) {         
  65.             painting.setOvale(ovale.isSelected());
  66.             painting.repaint();
  67.         }
  68.         if (source == rectangle) {
  69.             painting.setRectangle(rectangle.isSelected());
  70.             painting.repaint();
  71.         }
  72.     }
  73.    
  74.     public void focusGained(FocusEvent ev) {}
  75.  
  76.     public void focusLost(FocusEvent ev) {
  77.         setPaintingSize();
  78.     }
  79.    
  80.     private void setPaintingSize() {
  81.         w = Integer.parseInt(width.getText());
  82.         h = Integer.parseInt(height.getText());
  83.         painting.repaint();
  84.     }
  85.    
  86.     class PaintPanel extends JPanel {
  87.         private boolean ovale = false;
  88.         private boolean rectangle = false;
  89.        
  90.         public void paintComponent(Graphics g) {
  91.             super.paintComponent(g);
  92.             if (ovale) g.drawOval(80, 20, w, h);
  93.             if (rectangle) g.drawRect(80, 20, w, h);
  94.         }
  95.        
  96.         public void setOvale(boolean state) {
  97.             ovale = state;
  98.         }
  99.        
  100.         public void setRectangle(boolean state) {
  101.             rectangle = state;
  102.         }
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement