Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please do this in Java. Please, do it correctly I need this done in correct way. Thank you so much for your help. (Interactive Drawing

Please do this in Java. Please, do it correctly I need this done in correct way. Thank you so much for your help.

(Interactive Drawing Application) In this exercise, you'll implement a GUI application that uses the MyShape hierarchy from GUI and Graphics Case Study Exercise 10.2 to create an interactive drawing application. You'll create two classes for the GUI and provide a test class that launches the application. The classes of the MyShape hierarchy require no additional changes. The first class to create is a subclass of JPanel called DrawPanel, which represents the area on which the user draws the shapes. Class DrawPanel should have the following instance variables:

An array shapes of type MyShape that will store all the shapes the user draws. An integer shapeCount that counts the number of shapes in the array.

An integer shapeType that determines the type of shape to draw.

A MyShape currentShape that represents the current shape the user is drawing.

A Color currentColor that represents the current drawing color.

A boolean filledShape that determines whether to draw a filled shape.

A JLabel statusLabel that represents the status bar. The status bar will display the coordinates of the current mouse position.

Class DrawPanel should also declare the following methods:

Overridden method paintComponent that draws the shapes in the array. Use instance variable shapeCount to determine how many shapes to draw. Method paintComponent should also call currentShape's draw method, provided that currentShape is not null.

Set methods for the shapeType, currentColor and filledShape.

Method clearLastShape should clear the last shape drawn by decrementing instance variable shapeCount. Ensure that shapeCount is never less than zero.

Method clearDrawing should remove all the shapes in the current drawing by setting shapeCount to zero.

Methods clearLastShape and clearDrawing should call repaint (inherited from JPanel) to refresh the drawing on the DrawPanel by indicating that the system should call method paintComponent. Class DrawPanel should also provide event handling to enable the user to draw with the mouse. Create a single inner class that both extends MouseAdapter and implements MouseMotionListener to handle all mouse events in one class. In the inner class, override method mousePressed so that it assigns currentShape a new shape of the type specified by shapeType and initializes both points to the mouse position. Next, override method mouseReleased to finish drawing the current shape and place it in the array. Set the second point of currentShape to the current mouse position and add currentShape to the array. Instance variable shapeCount determines the insertion index. Set currentShape to null and call method repaint to update the drawing with the new shape. Override method mouseMoved to set the text of the statusLabel so that it displays the mouse coordinates this will update the label with the coordinates every time the user moves (but does not drag) the mouse within the DrawPanel.

Next, override method mouseDragged so that it sets the second point of the currentShape to the current mouse position and calls method repaint. This will allow the user to see the shape while dragging the mouse. Also, update the JLabel in mouseDragged with the current position of the mouse. Create a constructor for DrawPanel that has a single JLabel parameter. In the constructor, initialize statusLabel with the value passed to the parameter. Also initialize array shapes with 100 entries, shapeCount to 0, shapeType to the value that represents a line, currentShape to null and currentColor to Color.BLACK. The constructor should then set the background color of the DrawPanel to Color.WHITE and register the MouseListener andMouseMotionListener so the JPanel properly handles mouse events.

Next, create a JFrame subclass called DrawFrame that provides a GUI that enables the user to control various aspects of drawing. For the layout of the DrawFrame, we recommend aBorderLayout, with the components in the NORTH region, the main drawing panel in the CENTER region, and a status bar in the SOUTH region, as in Figure below. In the top panel, create the components listed below. Each component's event handler should call the appropriate method in class DrawPanel.

A button to undo the last shape drawn.

A button to clear all shapes from the drawing.

A combo box for selecting the color from the 13 predefined colors.

A combo box for selecting the shape to draw.

A checkbox that specifies whether a shape should be filled or unfilled.

Allow the user to save a drawing into a file or load a prior drawing from a file using object serialization. Add buttons Load (to read objects from a file) and Save (to write objects to a file). Use an ObjectOutputStream to write to the file and an ObjectInputStream to read from the file. Write the array of MyShape objects using method writeObject (class ObjectOutputStream), and read the array using method readObject (ObjectInputStream).

Declare and create the interface components in DrawFrame's constructor. You'll need to create the status bar JLabel before you create the DrawPanel, so you can pass the JLabel as an argument to DrawPanel's constructor. Finally, create a test class that initializes and displays the DrawFrame to execute the application.

image text in transcribed

Here is the Exercise 10.2:

This exercise is to realize an abstract class MyShape as illustrated in the above right figure. Notice that shape classes have many similarities. Using inheritance, we can "factor out" the common features from all three classes and place them in a single shape superclass. Then, using variables of the superclass type, we can manipulate shape objects polymorphically for the rest of its lifetime as a MyShape. Since MyShape represents any shape in general, you cannot implement a draw method without knowing exactly what shape it is. The data representing the coordinates and color of the shapes in the hierarchy should be declared as private members of class MyShape. In addition to the common data, class MyShape should declare the following methods:

A no-argument constructor that sets all the coordinates of the shape to 0 and the color to Color.BLACK.

A constructor that initializes the coordinates and color to the values of the arguments supplied.

Set methods for the individual coordinates and color that allow the programmer to set any piece of data independently for a shape in the hierarchy.

Get methods for the individual coordinates and color that allow the programmer to retrieve any piece of data independently for a shape in the hierarchy.

The abstract method public abstract void draw(Graphics g); which the program's paintComponent method will call to draw a shape on the screen.

To ensure proper encapsulation, all data in class MyShape must be private. This requires declaring proper set and get methods to manipulate the data. Class MyLine should provide a no-argument constructor and a constructor with arguments for the coordinates and color. Classes MyOval and MyRectangle should provide a no-argument constructor and a constructor with arguments for the coordinates, color and determining whether the shape is filled. The no-argument constructor should, in addition to setting the default values, set the shape to be an unfilled shape.

You can draw lines, rectangles and ovals if you know two points in space. Lines require x1, y1, x2 and y2 coordinates. The drawLine method of the Graphics class will connect the two points supplied with a line. If you have the same four coordinate values (x1, y1, x2 and y2) for ovals and rectangles, you can calculate the four arguments needed to draw them. Each requires an upper-left x-coordinate value (the smaller of the two x-coordinate values), an upper-left y-coordinate value (the smaller of the two y-coordinate values), a width (the absolute value of the difference between the two x-coordinate values) and a height (the absolute value of the difference between the two y-coordinate values). Rectangles and ovals should also have a filled flag that determines whether to draw the shape as a filled shape. Class MyBoundedShape should declare two constructors that mimic those of class MyShape, only with an added parameter to set whether the shape is filled. Class MyBoundedShape should also declare get and set methods for manipulating the filled flag and methods that calculate the upper-left x-coordinate, upper-left y-coordinate, width and height. Remember, the values needed to draw an oval or a rectangle can be calculated from two (x, y) coordinates. If designed properly, the new MyOval and MyRectangle classes should each have two constructors and a draw method.

There should be no MyLine, MyOval or MyRectangle variables in the program only MyShape variables that contain references to MyLine, MyOval and MyRectangle objects. Method paintComponent should walk through the MyShape array and draw every shape (i. e., polymorphically calling every shape's draw method).

Requirements

Realize your implementation that satisfies the following minimal requirements:

2 pt: properly exhibits right logic, i.e., readable and compilable coding

2 pt: properly realizes undo and clear interfaces

2 pt: properly realizes color and shape selection interfaces

2 pt: properly realizes a filled and unfilled interface

2 pt: properly realizes shape resize/repaint interface at initial drawings, not after deserialization

2 pt: properly realizes load and save interfaces

Java Drawings | Undo Clear Red | | Rectangle Filled (483 3) Java Drawings | Undo Clear Red | | Rectangle Filled (483 3)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Automating Access Databases With Macros

Authors: Fish Davis

1st Edition

1797816349, 978-1797816340

More Books

Students also viewed these Databases questions

Question

How are capital and working capital different?

Answered: 1 week ago