in this exercise, you’ll implement a gui application that uses
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:a) an array shapes of type myshape that will store all the shapes the user draws.b) an integer shapecount that counts the number of shapes in the array.c) an integer shapetype that determines the type of shape to draw.d) a myshape currentshape that represents the current shape the user is drawing.e) a color currentcolor that represents the current drawing color.f) a boolean filledshape that determines whether to draw a filled shape.g) 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:a) 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.b) set methods for the shapetype, currentcolor and filledshape.c) method clearlastshape should clear the last shape drawn by decrementing instance variable shapecount. ensure that shapecount is never less than zero.d) 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 mousemotion- listener 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 mouse- dragged 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 draw-panel to color.white and register the mouselistener and mousemotionlistener 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 a borderlayout, 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 fig. 14.49. in the top panel, create the components listed below. each component’s event handler should call the appropriate method in class drawpanel.a) a button to undo the last shape drawn.b) a button to clear all shapes from the drawing.c) a combo box for selecting the color from the 13 predefined colors.d) a combo box for selecting the shape to draw.e) a checkbox that specifies whether a shape should be filled or unfilled.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.
Price Type: Negotiable
Total Proposals: 4
1 Current viewersl
29 Total views