Really need help with this problem. And I know JavaFX but not familar with Swing
ID: 3878378 • Letter: R
Question
Really need help with this problem. And I know JavaFX but not familar with Swing.
Here is the problem:
Create, test, and document a Java program with the following classes.
Figure: This abstract class (given to you) represents a figure that can be drawn on the GUI.
Rectangle: This extends Figure and implements the functionality needed to draw filled
rectangles with a given color.
Circle: This extends Figure and implements the functionality needed to draw filled circles with
a given color.
The GUI should appear as below. (Minor deviations are fine.)
The current date should appear in the bottom left corner of the drawing. All figures appearing in the GUI should be listed with their String representations in the JTextArea appearing on the right side.
As in the video file, to draw the rectangle, the user first clicks one coordinate and then clicks the second coordinate. To draw a circle, the user first clicks the center and then the second click point is on the circumference.
When the exit button is clicked, serialize the figures and the content of the text area to disk in a file named “figures”. When the GUI is started, check for the existence of a file named “figures” and if it exists, open it to populate the figures and the corresponding list in the text area. You must not assume any specific directory structure. For example, do not write to a file named “C:Tempigures.” Instead, use “figures” to ensure that it gets stored in or retrieved from the current direc
Thanks a lot!
Figures GUI Red Green Blue RectangleCircle Exit 15-Jan-2015Explanation / Answer
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.ArrayList; public class ShapeDrawWithMenus extends JApplet { JCheckBoxMenuItem addLargeShapes; . JCheckBoxMenuItem addBorderedShapes; JRadioButtonMenuItem red, green, blue; JPopupMenu popup; public void init() { setBackground(Color.gray); ShapeCanvas canvas = new ShapeCanvas(); setContentPane(canvas); JMenuBar menubar = new JMenuBar(); setJMenuBar(menubar); JMenu addShapeMenu = new JMenu("Add"); addShapeMenu.setMnemonic('A'); menubar.add(addShapeMenu); JMenu shapeColorMenu = new JMenu("Color"); shapeColorMenu.setMnemonic('C'); menubar.add(shapeColorMenu); JMenu optionsMenu = new JMenu("Options"); optionsMenu.setMnemonic('O'); menubar.add(optionsMenu); JMenuItem rect = new JMenuItem("Rectangle"); rect.setAccelerator( KeyStroke.getKeyStroke("ctrl R") ); addShapeMenu.add(rect); rect.addActionListener(canvas); JMenuItem roundrect = new JMenuItem("Circle"); roundrect.setAccelerator( KeyStroke.getKeyStroke("ctrl D") ); addShapeMenu.add(circle); roundrect.addActionListener(canvas); ButtonGroup colorGroup = new ButtonGroup(); red = new JRadioButtonMenuItem("Red"); shapeColorMenu.add(red); colorGroup.add(red); red.setSelected(true); green = new JRadioButtonMenuItem("Green"); shapeColorMenu.add(green); colorGroup.add(green); blue = new JRadioButtonMenuItem("Blue"); shapeColorMenu.add(blue); colorGroup.add(blue); JMenuItem clear = new JMenuItem("Clear"); clear.setAccelerator( KeyStroke.getKeyStroke("ctrl C") ); clear.addActionListener(canvas); optionsMenu.add(clear); optionsMenu.addSeparator(); addLargeShapes = new JCheckBoxMenuItem("Add Large Shapes"); addLargeShapes.setSelected(true); optionsMenu.add(addLargeShapes); addBorderedShapes = new JCheckBoxMenuItem("Add Shapes with Border"); addBorderedShapes.setSelected(true); optionsMenu.add(addBorderedShapes); optionsMenu.addSeparator(); JMenu background = new JMenu("Background Color"); optionsMenu.add(background); background.add("Red").addActionListener(canvas); background.add("Green").addActionListener(canvas); background.add("Blue").addActionListener(canvas); popup = new JPopupMenu(); popup.add("Delete Shape").addActionListener(canvas); popup.add("Bring to Front").addActionListener(canvas); popup.addSeparator(); popup.add("Make Large").addActionListener(canvas); popup.add("Make Small").addActionListener(canvas); popup.addSeparator(); popup.add("Add Black Border").addActionListener(canvas); popup.add("Remove Black Border").addActionListener(canvas); popup.addSeparator(); popup.add("Set Color to Red").addActionListener(canvas); popup.add("Set Color to Green").addActionListener(canvas); popup.add("Set Color to Blue").addActionListener(canvas); } public Insets getInsets() { return new Insets(3,3,3,3); } class ShapeCanvas extends JPanel implements ActionListener, MouseListener, MouseMotionListener { ArrayList shapes = new ArrayList(); ShapeCanvas() { setBackground(Color.white); addMouseListener(this); addMouseMotionListener(this); } public void paintComponent(Graphics g) { super.paintComponent(g); //. int top = shapes.size(); for (int i = 0; i = 0; i-- ) { Shape s = (Shape)shapes.get(i); if (s.containsPoint(x,y)) { clickedShape = s; break; } } if (clickedShape == null) { return; } else if (evt.isPopupTrigger()) { popup.show(this,x-10,y-2); } else if (evt.isShiftDown()) { shapes.remove(clickedShape); shapes.add(clickedShape); repaint(); } else { draggedShape = clickedShape; prevDragX = x; prevDragY = y; } } public void mouseDragged(MouseEvent evt) { if (draggedShape == null) { return; } int x = evt.getX(); int y = evt.getY(); draggedShape.moveBy(x - prevDragX, y - prevDragY); prevDragX = x; prevDragY = y; repaint(); } public void mouseReleased(MouseEvent evt) { if (draggedShape == null) { return; } int x = evt.getX(); int y = evt.getY(); if (evt.isPopupTrigger()) { popup.show(this,x-10,y-2); } else { draggedShape.moveBy(x - prevDragX, y - prevDragY); if ( draggedShape.left >= getSize().width || draggedShape.top >= getSize().height || draggedShape.left + draggedShape.width < 0 || draggedShape.top + draggedShape.height < 0 ) { shapes.remove(draggedShape); } repaint(); } draggedShape = null; } public void mouseEntered(MouseEvent evt) { } public void mouseExited(MouseEvent evt) { } public void mouseMoved(MouseEvent evt) { } public void mouseClicked(MouseEvent evt) { } } static abstract class Shape { int left, top; int width, height; Color color = Color.white; boolean drawOutline; void reshape(int left, int top, int width, int height) { this.left = left; this.top = top; this.width = width; this.height = height; } void setSize(int width, int height) { this.width = width; this.height = height; } void moveBy(int dx, int dy) { left += dx; top += dy; } void setColor(Color color) { this.color = color; } void setDrawOutline(boolean draw) { drawOutline = draw; } boolean containsPoint(int x, int y) { if (x >= left && x = top && yRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.