Write a complete program in a class named ShowDesign that uses the DrawingPanel
ID: 3530049 • Letter: W
Question
Write a complete program in a class named ShowDesign that uses the DrawingPanel The window is 200 pixels wide and 200 pixels tall. The background is white and the foreground is black. There are 20 pixels between each of the four rectangles, and the rectangles are concentric (their centers are at the same point). Use a loop to draw the repeated rectangles. (You don't need to include any import statements at the top of your program. This class will be re-used in the next problem, so you may want to save it to a temporary file before moving on.)Explanation / Answer
import java.awt.*; import java.awt.event.*; import java.util.ArrayList; import javax.swing.*; import javax.swing.event.*; import java.util.*; interface Shape { // this method should render your Shape. If the shape is in the "hot" state, // meaning the mouse is over the shape, it should render fully opaque, with // the color specified by setColor. If the shape is not in the hot state, it // should render a white background (fill) and just a border in the color specified // by setColor. public void render(Graphics g); // determine if the given point is within the shape. Keep track of if the // shape is currently hit tested or not, and call listener.OnHitTestChange() // whenever the state changes. You will need this state to determine if it should be // rendered in the "hot" state or not. public boolean hitTest(Point pt, HitTestChangeListener listener); // allow the outline color to be set (or fill color while in the "hot" state). public void setColor(Color color); // calculate the area of the shape. public double getArea(); // get/set the position of the shape. This point can be anywhere on (or even off) // the shape that you want, but you must be consistent and accept a change to // setPosition(). public Point getPosition(); public void setPosition(Point point); } class Rectangle implements Shape { private int width; private int height; private int x; private int y; public Rectangle(int width, int height, int y, int x) { this.width = width; this.height = height; this.x = x; this.y = y; } public double getWidth() { return width; } public double getHeight() { return height; } public void render(Graphics g) { g.drawRect(x, y, width, height); } public boolean hitTest(Point pt, HitTestChangeListener listener) { return true; } public void setColor(Color color) { setColor(Color.BLACK); } public double getArea() { return width * height; } public Point getPosition() { Point p = new Point(); return p; } public void setPosition(Point point) { System.out.println(point); } } // The ShapeFactory will create shapes of the specified type, sized reasonably for the given // panel with and height class ShapeFactory { private enum Shapes { RECTANGLE, SQUARE, CIRCLE, OVAL, TRIANGLE /* HEXAGON Bonus */ /* OCTAGON Bonus */}; public static String[] getSupportedShapes() { // have the enumeration provide strings for each enum value String[] ret = new String[Shapes.values().length]; for (int i = 0; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.