Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class Gui {
- public static void main(String[] args) {
- JFrame win = new MainWin();
- win.setVisible(true);
- }
- }
- class MainWin extends JFrame implements ActionListener, FocusListener {
- private Container c = new Container();
- private PaintPanel painting = new PaintPanel();
- private JPanel controls = new JPanel();
- private String[] colorsLabel = {"red", "blue", "green", "yellow"};
- private Color[] colors = {Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW};
- private JComboBox<String> combo = new JComboBox<String>(colorsLabel);
- private JLabel label = new JLabel("DIMEMSIONS");
- private JTextField width = new JTextField(3);
- private JTextField height = new JTextField(3);
- private int w;
- private int h;
- private JCheckBox ovale = new JCheckBox("Ovale");
- private JCheckBox rectangle = new JCheckBox("Rectangle");
- public MainWin() {
- setTitle("FIGURES");
- setSize(400, 250);
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- c = getContentPane();
- c.setLayout(new BorderLayout());
- c.add(painting);
- c.add(controls, "South");
- combo.addActionListener(this);
- width.setText("400");
- width.addActionListener(this);
- width.addFocusListener(this);
- height.setText("250");
- height.addActionListener(this);
- height.addFocusListener(this);
- ovale.addActionListener(this);
- rectangle.addActionListener(this);
- controls.add(combo);
- controls.add(label);
- controls.add(width);
- controls.add(height);
- controls.add(ovale);
- controls.add(rectangle);
- }
- public void actionPerformed(ActionEvent ev) {
- Object source = ev.getSource();
- if (source == combo) {
- for (int i = 0; i < colors.length; i++) {
- if (combo.getSelectedItem() == colorsLabel[i])
- painting.setBackground(colors[i]);
- }
- }
- if (source == width || source == height)
- setPaintingSize();
- if (source == ovale) {
- painting.setOvale(ovale.isSelected());
- painting.repaint();
- }
- if (source == rectangle) {
- painting.setRectangle(rectangle.isSelected());
- painting.repaint();
- }
- }
- public void focusGained(FocusEvent ev) {}
- public void focusLost(FocusEvent ev) {
- setPaintingSize();
- }
- private void setPaintingSize() {
- w = Integer.parseInt(width.getText());
- h = Integer.parseInt(height.getText());
- painting.repaint();
- }
- class PaintPanel extends JPanel {
- private boolean ovale = false;
- private boolean rectangle = false;
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- if (ovale) g.drawOval(80, 20, w, h);
- if (rectangle) g.drawRect(80, 20, w, h);
- }
- public void setOvale(boolean state) {
- ovale = state;
- }
- public void setRectangle(boolean state) {
- rectangle = state;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement