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

Java Question Thanks to an expert, I had finished question 4, But now I struggle

ID: 3757329 • Letter: J

Question

Java Question

Thanks to an expert, I had finished question 4, But now I struggle below java question 5.

Could anyone please help me?

Thank you very much.

Question 4) 15 marks Prepare a GUI panel called ShapeSketcher for drawing a collection of shapes. It should have the following features It should contain a set of five radio buttons for choosing which shape to next add to the sketch A checkbox for whether or not the next shape should be filled in A button for changing the colour of the shape to next draw (the class JColorChooser might be useful) A sketch area to show all the previously drawn shapes. Each shape is added to the sketch by rubberbandina, dragging the mouse from its start point to a control point, with the shape being resized while the mouse is dragged Update the GUI so that if the mouse is dragged in a flat horizontal or flat vertical direction, the user should be informed by the letter H or Vappearing alongside the current mouse position Add a button to clear all the shapes drawn and another button to undo the last drawn shape, careful with exceptions that could occur Arrange components and draw area in a suitable way with suitable layouts. HINT: May be useful to create an innerclass which also extends JPanel of a fixed size to handle the drawing of shapes and listening for mouse events. Also recommended to place your other "J" components in another separate panel Essentially ShapeSketcher should extend JPanel and contain two more panels, one for holding the components and one for drawing the shapes and listening for mouse events objects. . . . . . . . Put all your JFrame setup inside the main method Question 5) 10 marks Further modify the ShapeSketcher class so that it also contains buttons for opening and saving sketches. Make the appropriate changes so that each shape can be serialized to a file using appropriate filtering streams. The user should be asked what file to open and save to using a FileChooser. Make sure appropriate exceptions are handled and displayed to user with a OptionPane.showMessageDialog method if the user tries to open an invalid file

Explanation / Answer

import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.colorchooser.*; public class ShapeSketcher extends JPanel { private Shape shape; private int x; private int y; private int width; private int height; protected JColorChooser colorChoose; protected Color selectedColor; Point newPoint; Point endPoint; boolean isCheckOval = false; boolean isCheckLine = false; boolean isCheckSqu = false; boolean isCheckRect = false; boolean isCheckCir = false; boolean isFilledShape = false; private JFrame frame = new JFrame(); private JPanel panel = new JPanel(); JRadioButton line, oval, circle, rect, square; JButton colour; JCheckBox fillShape; private DrawPanel drawPanel; private ButtonListener buttonListener; public ShapeSketcher() { setPreferredSize(new Dimension(600,600)); drawPanel = new DrawPanel(); buttonListener = new ButtonListener(); add(drawPanel); colorChoose = new JColorChooser(drawPanel.getForeground()); line = new JRadioButton("Line"); oval = new JRadioButton("Oval"); circle = new JRadioButton("Circle"); rect = new JRadioButton("Rectangle"); square = new JRadioButton("Square"); fillShape = new JCheckBox("Fill Shape"); colour = new JButton("Colour"); add(new JLabel("Choose shape to draw")); ButtonGroup group = new ButtonGroup(); group.add(line); group.add(oval); group.add(circle); group.add(rect); group.add(square); add(line); add(oval); add(circle); add(rect); add(square); add(fillShape); add(colour); line.addActionListener(buttonListener); oval.addActionListener(buttonListener); circle.addActionListener(buttonListener); rect.addActionListener(buttonListener); square.addActionListener(buttonListener); fillShape.addActionListener(buttonListener); colour.addActionListener(buttonListener); drawPanel.addMouseListener(drawPanel); drawPanel.addMouseMotionListener(drawPanel); } public class DrawPanel extends JPanel implements MouseListener, MouseMotionListener { public DrawPanel() { setPreferredSize(new Dimension(500,500)); setBackground(Color.WHITE); newPoint = null; endPoint = null; addMouseListener(this); addMouseMotionListener(this); } public void paintComponent(Graphics g) { super.paintComponent(g); if (newPoint != null) { if(getCheckLine(true) && line == line) { g.setColor(selectedColor); g.drawLine(newPoint.x, newPoint.y, endPoint.x, endPoint.y); } if(getCheckRect(true) && rect == rect ) { g.setColor(selectedColor); x = Math.min(newPoint.x, endPoint.x); y = Math.min(newPoint.y, endPoint.y); width = Math.abs(newPoint.x - endPoint.x); height = Math.abs(newPoint.y - endPoint.y); if(isFilledShape == true) { g.fillRect(x, y, width, height); } else { if(isFilledShape == false) { g.drawRect(x, y, width, height); } } } if(getCheckOval(true) && oval == oval) { g.setColor(selectedColor); x = Math.min(newPoint.x, endPoint.x); y = Math.min(newPoint.y, endPoint.y); width = Math.abs(newPoint.x - endPoint.x); height = Math.abs(newPoint.y - endPoint.y); if(isFilledShape == true) { g.fillOval(x, y, width, height); } else { if(isFilledShape == false) { g.drawOval(x, y, width, height); } } } if(getCheckCircle(true) && circle == circle) { g.setColor(selectedColor); x = Math.min(newPoint.x, endPoint.x); y = Math.min(newPoint.y, endPoint.y); width = Math.abs(newPoint.x - endPoint.x); height = Math.abs(newPoint.y - endPoint.y); if(isFilledShape == true) { g.fillOval(x, y, width, height); } else { if(isFilledShape == false) { g.drawOval(x, y, width, height); } } } if(getCheckSquare(true) && square == square) { g.setColor(selectedColor); x = Math.min(newPoint.x, endPoint.x); y = Math.min(newPoint.y, endPoint.y); width = Math.abs(newPoint.x - endPoint.x); height = Math.abs(newPoint.y - endPoint.y); if(isFilledShape == true) { g.fillRect(x, y, width, height); } else { if(isFilledShape == false) { g.drawRect(x, y, width, height); } } } } } public void mouseDragged(MouseEvent e) { endPoint = newPoint; endPoint = e.getPoint(); repaint(); } public void mouseMoved(MouseEvent e) { endPoint = e.getPoint(); } public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void mousePressed(MouseEvent e) { newPoint = e.getPoint(); } public void mouseReleased(MouseEvent e) { newPoint = endPoint; } } public boolean getCheckLine(boolean getShape) { return isCheckLine; } public boolean getCheckOval(boolean getShape) { return isCheckOval; } public boolean getCheckRect(boolean getShape) { return isCheckRect; } public boolean getCheckSquare(boolean getShape) { return isCheckSqu; } public boolean getCheckCircle(boolean getShape) { return isCheckCir; } public boolean getFilledShape(boolean getShape) { return isFilledShape; } public class ButtonListener extends JPanel implements ActionListener { public ButtonListener() { } public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if(source == line) { isCheckLine = true; isCheckRect = false; isCheckSqu = false; isCheckOval = false; isCheckCir = false; } if(source == square) { isCheckSqu = true; isCheckRect = false; isCheckLine = false; isCheckOval = false; isCheckCir = false; } if(source == oval) { isCheckOval = true; isCheckRect = false; isCheckLine = false; isCheckSqu = false; isCheckCir = false; } if(source == rect) { isCheckRect = true; isCheckOval = false; isCheckLine = false; isCheckSqu = false; isCheckCir = false; } if(source == circle) { isCheckCir = true; isCheckOval = false; isCheckLine = false; isCheckSqu = false; isCheckLine = false; } if(source == colour) { selectedColor = JColorChooser.showDialog(frame, "Choose a color" , colorChoose.getColor()); } if(source == fillShape) { isFilledShape = true; } else { if(source != fillShape) { isFilledShape = false; } } } } public static void main(String[] args) { JFrame frame = new JFrame("Shape Sketcher"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new ShapeSketcher()); ShapeSketcher panel = new ShapeSketcher(); frame.getContentPane().add(panel); frame.pack(); Toolkit tk = Toolkit.getDefaultToolkit(); Dimension screenDimension = tk.getScreenSize(); Dimension frameDimension = frame.getSize(); frame.setLocation((screenDimension.width-frameDimension.width)/2, (screenDimension.height-frameDimension.height)/2); frame.setVisible(true); } }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote