Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

BY JAVA JPANEL Draw a shape - The user can select a shape from a list of shapes

ID: 3576296 • Letter: B

Question

BY JAVA JPANEL

Draw a shape - The user can select a shape from a list of shapes (circle, rectangle, triangle, line) and specify its parameters in the field Parameters.

The color is selected from a list of colors.

- The parameters for each shape are as follows: o Circle: the center of the circle (x,y) and the radius r. o Rectangle: the coordinates of the left upper corner (x,y), the width w and the height h. o Triangle: coordinates of the three vertices of the triangle (x1,y1), (x2,y2), (x3,y3) o Line: the coordinates of the 2 end points (x1,y1), (x2,y2).

For example if the user wants to draw a blue circle with center (100,150) and radius 50, he has to: 1.Select circle from the shapes list. 2.Enter 100,150,50 in the parameters text field 3.Select blue from the colors list 4.Press the button draw. - Note that if the parameters list the user enters is not appropriate for the shape he selected, he gets and error message.

2- Save the Drawing When the user clicks the button save, he is prompted to enter the name of the file where to save his drawing. The user gets and error message if something wrong happens during the saving.

you assignment you are requested to Implement a simple drawing application with the lollowing design: are Drawing Application Load Draw shape Parameters Color Black The application has to provide the following functionalities 1- Draw a shape The user can select a shape from a list of shapes (circle, rectangle, triangle, line) and specify its parameters in the field Parameters. The color is selected from a list of colors. The parameters for each shape are as follows: o Circle: the center of the circle (xy) and the radius r. o Rectangle: the coordinates of the left upper corner (x,y), the width w and the height h. o Triangle: coordinates of the three vertices of the triangle (x1,yl), (x2,y2), (x3,y3) o Line: the coordinates of the 2 end points (x1,yl), (x2,y2). For example if the user wants to draw a blue circle with center (100,150) and radius 50, he has to 1.Select circle from the shapes list. 2.Enter 100,150,50 in the parameters text field 3 Select blue from the colors list 4 Press the button draw. Note that if the parameters list the user enters is not appropriate for the shape he selected he gets and error message.

Explanation / Answer

import java.awt.Dimension;

import java.awt.EventQueue;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JRadioButton;

import javax.swing.UIManager;

import javax.swing.UnsupportedLookAndFeelException;

public class UserDraw extends JFrame {

    public static void main(String[] args) {

        new UserDraw();

    }

    public UserDraw() {

        EventQueue.invokeLater(new Runnable() {

            @Override

            public void run() {

                try {

                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {

                    ex.printStackTrace();

                }

                JFrame frame = new JFrame("Testing");

                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                frame.add(new ControlPane());

                frame.pack();

                frame.setLocationRelativeTo(null);

                frame.setVisible(true);

            }

        });

    }

    public class ControlPane extends JPanel {

        private JRadioButton circle;

        private JRadioButton square;

        private DrawPane drawPane;

        public ControlPane() {

            setLayout(new GridBagLayout());

            ButtonGroup bg = new ButtonGroup();

            circle = new JRadioButton("Circle");

            square = new JRadioButton("Square");

            bg.add(circle);

            bg.add(square);

            GridBagConstraints gbc = new GridBagConstraints();

            gbc.gridwidth = GridBagConstraints.REMAINDER;

            gbc.weightx = 1;

            JPanel shape = new JPanel();

            shape.add(circle);

            shape.add(square);

            add(shape, gbc);

            JButton draw = new JButton("Draw");

            draw.addActionListener(new ActionListener() {

                @Override

                public void actionPerformed(ActionEvent e) {

                    if (circle.isSelected()) {

                        drawPane.setDrawableShape(DrawableShape.CIRCLE);

                    } else if (square.isSelected()) {

                        drawPane.setDrawableShape(DrawableShape.SQUARE);

                    }

                }

            });

            gbc.gridwidth = GridBagConstraints.REMAINDER;

            add(draw, gbc);

            drawPane = new DrawPane();

            gbc.weightx = 1;

            gbc.weighty = 1;

            gbc.fill = gbc.BOTH;

            add(drawPane, gbc);

        }

    }

    public enum DrawableShape {

        CIRCLE,

        SQUARE

    }

    public class DrawPane extends JPanel {

        private DrawableShape drawableShape;

        public DrawPane() {

        }

        public void setDrawableShape(DrawableShape drawableShape) {

            this.drawableShape = drawableShape;

            repaint();

        }

        public DrawableShape getDrawableShape() {

            return drawableShape;

        }

        @Override

        public Dimension getPreferredSize() {

            return new Dimension(200, 200);

        }

        @Override

        protected void paintComponent(Graphics g) {

            super.paintComponent(g);

            Graphics2D g2d = (Graphics2D) g.create();

          DrawableShape shape = getDrawableShape();

            if (shape != null) {

                int width = getWidth() - 20;

                int height = getHeight() - 20;

                int size = Math.min(width, height);

                int x = (getWidth() - size) / 2;

                int y = (getHeight() - size) / 2;

                if (shape == DrawableShape.CIRCLE) {

                    g2d.fillOval(x, y, size, size);

                } else if (shape == DrawableShape.SQUARE) {

                    g2d.fillRect(x, y, size, size);

                }

            }

            g2d.dispose();

        }

    }

}