Question: 1. Your job is to add another button with some compound shape without modifying any of the files except for the BasicShapeFrameTest which will add
1. Your job is to add another button with some compound shape without modifying any of the files except for the BasicShapeFrameTest which will add your button. Your compound shape should consist of at least two different basic shapes. I created a compound shape made up of a circle and a square, as you can see above. The addShape method used in BasicShapeFrameTest takes a Shape parameter. Thus, you need to produce an object that implements the Shape interface. You will need to create two classes to implement the Shape interface and to implement the PathIterator interface. Your classes will most likely contain instance variables containing the shape objects that make up the compound shape. You will make extensive use of the Shape and PathIterator methods already defined for you within these shapes. These methods will do the bulk of the work for you. Most of the methods required by the Shape interface are simple to implement. The only moderately challenging ones involve PathIterator. For implementing PathIterator, you will want to make extensive use of the same PathIterator methods of your shapes that make up the compound shape. For example, for the currentSegment method you will return a currentSegment of one of the shapes making up the compound shape. When all the segments of one shape are completed (determined by the isDone method), then you can return the currentSegment from the next shape.
2. Add Javadoc comments to record the members of your team in the BasicShapeFram- eTest file and also to document the code that you add to the project. Export your lab into a ZIP archive for submission.
Existing Code:
BasicShapeFrameTest.java
import java.awt.geom.*;
import javax.swing.*;
/**
A test program for the basic shape program
(before adding the interesting buttons)
*/
public class BasicShapeFrameTest
{
public static void main(String[] args)
{
ShapeFrame frame = new ShapeFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(WIDTH, HEIGHT);
frame.setTitle("BasicShapeFrameTest");
frame.addShape(new Rectangle2D.Double(0, 0, 20, 30));
frame.addShape(new Ellipse2D.Double(0, 0, 30, 30));
frame.addShape(new Line2D.Double(0, 0, 30, 30));
frame.setVisible(true);
}
public static final int WIDTH = 300;
public static final int HEIGHT = 300;
}
PositionedShape.java
import java.awt.*;
import java.awt.geom.*;
/**
A shape with a position offset.
*/
public class PositionedShape
{
/**
Constructs a PositionedShape.
@param aPosition the top left position
@param aShape the shape to be positioned
*/
public PositionedShape(Point2D aPosition, Shape aShape)
{
position = aPosition;
shape = aShape;
}
/**
Draws the shape at its intended position.
@param g2 the graphics context
*/
public void draw(Graphics2D g2)
{
AffineTransform oldTransform = g2.getTransform();
g2.translate(position.getX(), position.getY());
g2.draw(shape);
g2.setTransform(oldTransform);
}
private Point2D position;
private Shape shape;
}
ShapeFrame.java
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
/**
A Frame with buttons to select the current drawing shape.
*/
public class ShapeFrame extends JFrame
{
/**
Constructs the frame, adds the drawing panel and
three basic buttons.
*/
public ShapeFrame()
{
Container contentPane = getContentPane();
shapePanel = new ShapePanel();
contentPane.add(shapePanel, BorderLayout.CENTER);
buttonPanel = Box.createVerticalBox();
contentPane.add(buttonPanel, BorderLayout.WEST);
}
/**
Adds a shape button to the button panel.
@param aShape the shape that appears on the button
face and in the drawing panel.
*/
public void addShape(final Shape aShape)
{
System.out.println("Adding shape.");
System.out.println("Shape class = " + aShape.getClass());
System.out.println("Path iterator class = " + aShape.getPathIterator(null).getClass());
JButton button = new JButton(new ShapeIcon(aShape,
BUTTON_WIDTH, BUTTON_HEIGHT));
button.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
shapePanel.setShape(aShape);
}
});
buttonPanel.add(button);
}
private ShapePanel shapePanel;
private Box buttonPanel;
private int BUTTON_WIDTH = 50;
private int BUTTON_HEIGHT = 20;
}
ShapeIcon.java
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
/**
An icon that is painted by drawing a Shape
*/
public class ShapeIcon implements Icon
{
/**
Construct a ShapeIcon.
@param aShape the shape to use when painting
@param aWidth the width of the icon
@param aHeight the height of the icon
*/
public ShapeIcon(Shape aShape, int aWidth, int aHeight)
{
shape = aShape;
width = aWidth;
height = aHeight;
}
public int getIconWidth()
{
return width;
}
public int getIconHeight()
{
return height;
}
public void paintIcon(Component c, Graphics g, int x, int y)
{
double shapeWidth = shape.getBounds().getWidth();
double shapeHeight = shape.getBounds().getHeight();
double scaleX = Math.max(1, shapeWidth / width);
double scaleY = Math.max(1, shapeHeight / height);
double scale = 1 / Math.max(scaleX, scaleY);
Graphics2D g2 = (Graphics2D)g;
AffineTransform oldTransform = g2.getTransform();
g2.translate(x, y);
g2.scale(scale, scale);
g2.setColor(Color.black);
g2.draw(shape);
g2.setTransform(oldTransform);
}
private Shape shape;
private int width;
private int height;
}
ShapePanel.java
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
/**
A panel that holds positioned shapes.
*/
public class ShapePanel extends JPanel
{
/**
Constructs a shape panel.
*/
public ShapePanel()
{
shapes = new ArrayList
addMouseListener(new
MouseAdapter()
{
public void mousePressed(MouseEvent event)
{
if (currentShape == null) return;
shapes.add(new PositionedShape(event.getPoint(),
currentShape));
repaint();
}
});
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
for (int i = 0; i < shapes.size(); i++)
{
PositionedShape s = (PositionedShape)shapes.get(i);
s.draw(g2);
}
}
/**
Set the current drawing shape.
@param aShape the shape to add when the mouse is clicked.
*/
public void setShape(Shape aShape)
{
currentShape = aShape;
}
private Shape currentShape;
private ArrayList
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
