java 2d introduces many new capabilities for creating unique and
java 2d introduces many new capabilities for creating unique and impressive graphics.we’ll add a small subset of these features to the drawing application you created in exercise 14.17. in this version, you’ll enable the user to specify gradients for filling shapes and to change stroke characteristics for drawing lines and outlines of shapes. the user will be able to choose which colors compose the gradient and set the width and dash length of the stroke. first, you must update the myshape hierarchy to support java 2d functionality. make the following changes in class myshape:a) change abstract method draw’s parameter type from graphics to graphics2d.b) change all variables of type color to type paint to enable support for gradients. [note:recall that class color implements interface paint.]c) add an instance variable of type stroke in class myshape and a stroke parameter in the constructor to initialize the new instance variable. the default stroke should be an instance of class basicstroke.classes myline, myboundedshape, myoval and myrectangle should each add a stroke parameter to their constructors. in the draw methods, each shape should set the paint and the stroke before drawing or filling a shape. since graphics2d is a subclass of graphics, we can continue to use graphics methods drawline, drawoval, filloval, and so on. to draw the shapes. when these methods are called, they’ll draw the appropriate shape using the specified paint and stroke settings.next, you’ll update the drawpanel to handle the java 2d features. change all color variables to paint variables. declare an instance variable currentstroke of type stroke and provide a set method for it. update the calls to the individual shape constructors to include the paint and stroke arguments. in method paintcomponent, cast the graphics reference to type graphics2d and use the graphics2d reference in each call to myshape method draw.next, make the new java 2d features accessible from the gui. create jpanel of gui components for setting the java 2d options. add these components at the top of the drawframe below the panel that currently contains the standard shape controls (see fig. 15.34). these gui components should include:a) a check box to specify whether to paint using a gradient.b) two jbuttons that each show a jcolorchooser dialog to allow the user to choose the first and second color in the gradient. (these will replace the jcombobox used for choosing the color in exercise 14.17.)c) a text field for entering the stroke width.d) a text field for entering the stroke dash length.e) a check box for selecting whether to draw a dashed or solid line. if the user selects to draw with a gradient, set the paint on the drawpanel to be a gradient of the two colors chosen by the user. the expression new gradientpaint( 0, 0, color1, 50, 50, color2, true ) ) creates a gradientpaint that cycles diagonally from the upper-left to the bottom-right every 50 pixels. variables color1 and color2 represent the colors chosen by the user. if the user does not select to use a gradient, then simply set the paint on the drawpanel to be the first color chosen by the user.for strokes, if the user chooses a solid line, then create the stroke with the expressionnew basicstroke( width, basicstroke.cap_round, basicstroke.join_round )where variable width is the width specified by the user in the line-width text field. if the user chooses a dashed line, then create the stroke with the expression new basicstroke( width, basicstroke.cap_round, basicstroke.join_round,10, dashes, 0) where width again is the width in the line-width field, and dashes is an array with one element whose value is the length specified in the dash-length field. the panel and stroke objects should be passed to the shape object’s constructor when the shape is created in drawpanel.
Price Type: Negotiable
Total Proposals: 4
1 Current viewersl
20 Total views