Question: Please read this carefully needs to be in JAVA Java 2D introduces many new capabilities for creating unique and impressive graphics. Well add a small

Please read this carefully needs to be in JAVA

Java 2D introduces many new capabilities for creating unique and impressive graphics. Well add a small subset of these features to the drawing application you created in Exercise 12.17. Code for this is included. In this version, youll 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 draws 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, theyll draw the appropriate shape using the specified Paint and Stroke settings.

Next, youll 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 a 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 These GUI components should include:

a) A checkbox 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 12.17.)

c) A text field for entering the Stroke width.

d) A text field for entering the Stroke dash length.

e) A checkbox for selecting whether to draw a dashed or solid line.

Here is the code just need to fill in the rest of the indicated spaces.

public class MyShape

{

import java.awt.Color;

import java.awt.Graphics;

public abstract class MyShape

{

private int x1; // x coordinate of first endpoint

private int y1; // y coordinate of first endpoint

private int x2; // x coordinate of second endpoint

private int y2; // y coordinate of second endpoint

private Color color; // color of this shape

//--------------------------------------------------------------------

// no-arg constructor

//--------------------------------------------------------------------

public MyShape()

{

this(0, 0, 0, 0, Color.BLACK);

}

//--------------------------------------------------------------------

// constructor with location and color parameters

//--------------------------------------------------------------------

public MyShape(int x1, int y1, int x2, int y2, Color color)

{

this.x1 = (x1 >= 0 ? x1 : 0);

this.y1 = (y1 >= 0 ? y1 : 0);

this.x2 = (x2 >= 0 ? x2 : 0);

this.y2 = (y2 >= 0 ? y2 : 0);

this.color = color;

}

public void setX1(int x1)

{

this.x1 = (x1 >= 0 ? x1 : 0);

}

public int getX1()

{

return x1;

}

public void setX2(int x2)

{

this.x2 = (x2 >= 0 ? x2 : 0);

}

public int getX2()

{

return x2;

}

public void setY1(int y1)

{

this.y1 = (y1 >= 0 ? y1 : 0);

}

public int getY1()

{

return y1;

}

public void setY2(int y2)

{

this.y2 = (y2 >= 0 ? y2 : 0);

}

public int getY2()

{

return y2;

}

public void setColor(Color color)

{

this.color = color;

}

public Color getColor()

{

return color;

}

public abstract void draw(Graphics g);

} // end class MyShape

}

import java.awt.Color;

import java.awt.Graphics;

public class MyRect extends MyBoundedShape

{

//--------------------------------------------------------------------

// no-arg constructor

//--------------------------------------------------------------------

public MyRect()

{

// just calls default superclass constructor

}

//--------------------------------------------------------------------

// constructor with location, color and fill parameters

//--------------------------------------------------------------------

public MyRect(int x1, int y1, int x2, int y2,

Color color, boolean filled)

{

super(x1, y1, x2, y2, color, filled);

}

// draw rectangle

public void draw(Graphics g)

{

g.setColor(getColor());

if (isFilled())

g.fillRect(getUpperLeftX(), getUpperLeftY(),

getWidth(), getHeight());

else

g.drawRect(getUpperLeftX(), getUpperLeftY(),

getWidth(), getHeight());

}

} // end class MyRec

import java.awt.Color;

import java.awt.Graphics;

public class MyOval extends MyBoundedShape

{

//--------------------------------------------------------------------

// no-arg constructor

//--------------------------------------------------------------------

public MyOval()

{

// just calls default superclass constructor

}

//--------------------------------------------------------------------

// constructor with location, color and fill parameters

//--------------------------------------------------------------------

public MyOval(int x1, int y1, int x2, int y2,

Color color, boolean isFilled)

{

super(x1, y1, x2, y2, color, isFilled);

}

// draw oval

public void draw(Graphics g)

{

g.setColor(getColor());

if (isFilled())

g.fillOval(getUpperLeftX(), getUpperLeftY(),

getWidth(), getHeight());

else

g.drawOval(getUpperLeftX(), getUpperLeftY(),

getWidth(), getHeight());

}

} // end class MyOval

import java.awt.Color;

import java.awt.Graphics;

public class MyLine extends MyShape

{

//--------------------------------------------------------------------

// no-arg constructor

//--------------------------------------------------------------------

public MyLine()

{

// just calls default superclass constructor

}

//--------------------------------------------------------------------

// constructor with location and color parameters

//--------------------------------------------------------------------

public MyLine(int x1, int y1, int x2, int y2, Color color)

{

super(x1, y1, x2, y2, color);

}

// draw line in specified color

public void draw(Graphics g)

{

g.setColor(getColor());

g.drawLine(getX1(), getY1(), getX2(), getY2());

}

} // end class MyLine

import java.awt.Color;

import java.awt.Graphics;

public abstract class MyBoundedShape extends MyShape

{

private boolean filled; // whether this shape is filled

//--------------------------------------------------------------------

// no-arg constructor

//--------------------------------------------------------------------

public MyBoundedShape()

{

this.filled = false;

}

//--------------------------------------------------------------------

// constructor with location parameters

//--------------------------------------------------------------------

public MyBoundedShape(int x1, int y1, int x2, int y2,

Color color, boolean isFilled)

{

super(x1, y1, x2, y2, color);

this.filled = filled;

}

public int getUpperLeftX()

{

return Math.min(getX1(), getX2());

}

public int getUpperLeftY()

{

return Math.min(getY1(), getY2());

}

public int getWidth()

{

return Math.abs(getX2() - getX1());

}

public int getHeight()

{

return Math.abs(getY2() - getY1());

}

public boolean isFilled()

{

return filled;

}

public void setFilled(boolean filled)

{

this.filled = filled;

}

} // end class MyBoundedShape

import javax.swing.JFrame;

public class InteractiveDrawingApplication

{

public static void main(String[] args)

{

DrawFrame application = new DrawFrame();

application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

application.setSize(500, 400);

application.setVisible(true);

}

} // end class InteractiveDrawingApplication

import java.awt.Color;

import java.awt.Graphics;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.awt.event.MouseMotionListener;

import javax.swing.JLabel;

import javax.swing.JPanel;

public class DrawPanel extends JPanel

{

private MyShape[] shapes; // array containing all the shapes

private int shapeCount; // total number of shapes

private int shapeType; // type of shape to draw

private MyShape currentShape; // current shape being drawn

private Color currentColor; // color of the shape

private boolean filledShape; // whether this shape is filled

private JLabel statusLabel; // label to display mouse coordinates

//--------------------------------------------------------------------

// constructor

//--------------------------------------------------------------------

public DrawPanel(JLabel status)

{

shapes = new MyShape[100];

shapeCount = 0; // initially we have no shapes

setShapeType(0); // initially set to draw lines

setDrawingColor(Color.BLACK); // start drawing with black

setFilledShape(false); // not filled by default

currentShape = null;

setBackground(Color.WHITE);

// add the mouse listeners

MouseHandler mouseHandler = new MouseHandler();

addMouseListener(mouseHandler);

addMouseMotionListener(mouseHandler);

// set the status label for displaying mouse coordinates

statusLabel = status;

} // end DrawPanel constructor

// draw shapes polymorphically

public void paintComponent(Graphics g)

{

super.paintComponent(g);

for (int i = 0; i < shapeCount; i++)

shapes[i].draw(g);

if (currentShape != null)

currentShape.draw(g);

}

// set type of shape to draw

public void setShapeType(int shapeType)

{

if (shapeType < 0 || shapeType > 2)

shapeType = 0;

this.shapeType = shapeType;

}

// set drawing color

public void setDrawingColor(Color c)

{

currentColor = c;

}

// clear last shape drawn

public void clearLastShape()

{

if (shapeCount > 0)

{

shapeCount--;

repaint();

}

}

// clear all drawings on this panel

public void clearDrawing()

{

shapeCount = 0;

repaint();

}

// set whether shape is filled

public void setFilledShape(boolean isFilled)

{

filledShape = isFilled;

}

// handle mouse events for this JPanel

private class MouseHandler extends MouseAdapter

implements MouseMotionListener

{

// creates and sets the initial position for the new shape

public void mousePressed(MouseEvent e)

{

if (currentShape != null)

return;

// create requested shape

switch (shapeType)

{

case 0:

currentShape = new MyLine(e.getX(), e.getY(), e.getX(), e.getY(), currentColor);

break;

case 1:

currentShape = new MyOval(e.getX(), e.getY(), e.getX(), e.getY(), currentColor, filledShape);

break;

case 2:

currentShape = new MyRect(e.getX(), e.getY(), e.getX(), e.getY(), currentColor, filledShape);

break;

}

}

// fix current shape onto the panel

public void mouseReleased(MouseEvent e)

{

if (currentShape == null)

return;

// set the second point on the shape

currentShape.setX2(e.getX());

currentShape.setY2(e.getY());

// set the shape only if there is room in the array

if (shapeCount < shapes.length)

{

shapes[shapeCount] = currentShape;

shapeCount++;

}

currentShape = null; // clear the temporary drawing shape

repaint();

}

// update shape to current mouse position while dragging

public void mouseDragged(MouseEvent e)

{

if (currentShape != null)

{

currentShape.setX2(e.getX());

currentShape.setY2(e.getY());

repaint();

}

mouseMoved(e); // update status bar

}

// update status bar to show current mouse coordinates

public void mouseMoved(MouseEvent e)

{

statusLabel.setText(

String.format("(%d,%d)", e.getX(), e.getY()));

}

} // end class MouseHandler

} // end class DrawPanel

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.ItemEvent;

import java.awt.event.ItemListener;

import javax.swing.JButton;

import javax.swing.JCheckBox;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

public class DrawFrame extends JFrame

implements ItemListener, ActionListener

{

// Array of possible colors

private Color[] colors = { Color.BLACK, Color.BLUE, Color.CYAN,

Color.DARK_GRAY, Color.GRAY, Color.GREEN,

Color.LIGHT_GRAY, Color.MAGENTA, Color.ORANGE,

Color.PINK, Color.RED, Color.WHITE, Color.YELLOW };

// Array of names corresponding to the possible colors

private String[] colorNames = {"Black", "Blue", "Cyan", "Dark Gray", "Gray",

"Green", "Light Gray", "Magenta", "Orange",

"Pink", "Red", "White", "Yellow"};

// Array of possible shape names

private String[] shapes = {"Line", "Oval", "Rectangle"};

private DrawPanel drawPanel; // panel to contain the drawing

private JButton undoButton;

private JButton clearButton;

private JComboBox colorChoices;

private JComboBox shapeChoices;

private JCheckBox filledCheckBox; // check box to toggle whether shapes have fill

//--------------------------------------------------------------------

// constructor

//--------------------------------------------------------------------

public DrawFrame()

{

super("Java Drawings");

// create a panel to store the components at the top of the frame

JPanel topPanel = new JPanel();

undoButton = new JButton("Undo");

undoButton.addActionListener(this);

topPanel.add(undoButton);

clearButton = new JButton("Clear");

clearButton.addActionListener(this);

topPanel.add(clearButton);

colorChoices = new JComboBox(colorNames);

colorChoices.addItemListener(this);

topPanel.add(colorChoices);

shapeChoices = new JComboBox(shapes);

shapeChoices.addItemListener(this);

topPanel.add(shapeChoices);

filledCheckBox = new JCheckBox("Filled");

filledCheckBox.addItemListener(this);

topPanel.add(filledCheckBox);

// add the top panel to the frame

add(topPanel, BorderLayout.NORTH);

// create a label for the status bar

JLabel statusLabel = new JLabel("(0,0)");

// add the status bar at the bottom

add(statusLabel, BorderLayout.SOUTH);

// create the DrawPanel with its status bar label

drawPanel = new DrawPanel(statusLabel);

add(drawPanel); // add the drawing area to the center

} // end DrawFrame constructor

// handle selections made to a combobox or checkbox

public void itemStateChanged(ItemEvent e)

{

if (e.getSource() == shapeChoices) // choosing a shape

drawPanel.setShapeType(shapeChoices.getSelectedIndex());

else if (e.getSource() == colorChoices) // choosing a color

drawPanel.setDrawingColor(

colors[colorChoices.getSelectedIndex()]);

else if (e.getSource() == filledCheckBox) // filled/unfilled

drawPanel.setFilledShape(filledCheckBox.isSelected());

}

// handle button clicks

public void actionPerformed(ActionEvent e)

{

if (e.getSource() == undoButton)

drawPanel.clearLastShape();

else if (e.getSource() == clearButton)

drawPanel.clearDrawing();

}

} // end class DrawFrame

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!