Question: The program located below prompts the user for n (the size of an n-by-n array of squares) and size (the initial size of the JFrame
The program located below prompts the user for n (the size of an n-by-n array of squares) and size (the initial size of the JFrame that contains a JPanel in which the n-by-n array of squares is displayed), then draws in each of the n2 squares a randomly-chosen-from-a-set-of- closed-shapes that are color-filled with a randomly-chosen color. For example, the solution to this problem randomly-chooses various shapesand various colors.
This code is already complete and compiles (located below) but I want it re-developed to make it workpolymorphically. Call this polymorphic version of our graphics problem GProblem3.java. WhenGProblem3.java runs, its user dialog should be the same as that of GProblem2 (located below) when it runs. Why the same? Only changes the problems implementation is being changed.
Also, add one more concrete MyShape subclass to GProblem3.java, bringing the total to eight distinct shapes. Add three more colors to the array MyColors, bringing the total to eleven colors. Make sure at least one of the new colors is a randomly-generated color.
PLEASE take a screenshot of compiled output and post all source code with answer.
I HAVE provided a sample output.

//MyShape.java
import java.awt.Color; import java.awt.Graphics;
public class MyShape {
// Instance variables protected Color drawColor; protected int r; protected int c; protected MyPanel drawPanel;
/** * Constructor * * @param drawColor * - shape color * @param r * - row number * @param c * - column number * @param drawPanel * - panel to draw the shape on */ public MyShape(Color drawColor, int r, int c, MyPanel drawPanel) { this.drawColor = drawColor; this.r = r; this.c = c; this.drawPanel = drawPanel; }
public void Draw(Graphics g) { // Not abstract syntax, but "abstract-esque" method cuz "I ain't got no // body!" } }
//MySquare.java
import java.awt.Color; import java.awt.Graphics;
public class MySquare extends MyShape {
/** * Constructor * @param drawColor - color of the square * @param r - row number * @param c - column number * @param drawPanel - panel on which square will be drawn */ public MySquare(Color drawColor, int r, int c, MyPanel drawPanel) { super(drawColor, r, c, drawPanel); }
/** * Draws the square */ public void Draw(Graphics g) { int xs[] = new int[4]; int ys[] = new int[4]; xs[0] = drawPanel.ULx(c); ys[0] = drawPanel.ULy(r); xs[1] = drawPanel.LRx(c); ys[1] = drawPanel.ULy(r); xs[2] = drawPanel.LRx(c); ys[2] = drawPanel.LRy(r); xs[3] = drawPanel.ULx(c); ys[3] = drawPanel.LRy(r); g.setColor(drawColor); g.fillPolygon(xs, ys, 4); } }
//MyTriangle.java
public class MyTriangle extends MyShape {
/** * Constructor * * @param drawColor * - color of the triangle * @param r * - row number * @param c * - column number * @param drawPanel * - panel on which triangle will be drawn */ public MyTriangle(Color drawColor, int r, int c, MyPanel drawPanel) { super(drawColor, r, c, drawPanel); }
/** * Draws the triangle */ public void Draw(Graphics g) { Polygon polygon = new Polygon(); polygon.addPoint(drawPanel.ULx(c), drawPanel.LRy(r)); polygon.addPoint(drawPanel.xc(c), drawPanel.ULy(r)); polygon.addPoint(drawPanel.LRx(c), drawPanel.LRy(r)); g.setColor(drawColor); g.fillPolygon(polygon); } }
//MyCircle.java
import java.awt.Color; import java.awt.Graphics;
public class MyCircle extends MyShape {
/** * Constructor * * @param drawColor * - color of the circle * @param r * - row number * @param c * - column number * @param drawPanel * - panel on which circle will be drawn */ public MyCircle(Color drawColor, int r, int c, MyPanel drawPanel) { super(drawColor, r, c, drawPanel); }
/** * Draws the circle */ public void Draw(Graphics g) { g.setColor(drawColor); g.fillOval(drawPanel.ULx(c), drawPanel.ULy(r), (int) drawPanel.B(), (int) drawPanel.B()); } }
//MyDiamond.java
import java.awt.Color; import java.awt.Graphics; import java.awt.Polygon;
public class MyDiamond extends MyShape {
/** * Constructor * * @param drawColor * - color of the diamond * @param r * - row number * @param c * - column number * @param drawPanel * - panel on which diamond will be drawn */ public MyDiamond(Color drawColor, int r, int c, MyPanel drawPanel) { super(drawColor, r, c, drawPanel); }
/** * Draws the diamond */ public void Draw(Graphics g) { double B = drawPanel.B(); int dx = MyPanel.Rounded(B / 7);
Polygon polygon = new Polygon(); polygon.addPoint(drawPanel.xc(c), drawPanel.ULy(r)); polygon.addPoint(drawPanel.ULx(c) + dx, drawPanel.yc(r)); polygon.addPoint(drawPanel.xc(c), drawPanel.LRy(r)); polygon.addPoint(drawPanel.LRx(c) - dx, drawPanel.yc(r)); g.setColor(drawColor); g.fillPolygon(polygon); } }
//MyRomanCross.java
import java.awt.Color; import java.awt.Graphics; import java.awt.Polygon;
public class MyRomanCross extends MyShape {
/** * Constructor * * @param drawColor * - color of the RomanCross * @param r * - row number * @param c * - column number * @param drawPanel * - panel on which RomanCross will be drawn */ public MyRomanCross(Color drawColor, int r, int c, MyPanel drawPanel) { super(drawColor, r, c, drawPanel); }
/** * Draws the RomanCross */ public void Draw(Graphics g) { double B = drawPanel.B(); int dx = MyPanel.Rounded(B / 7); int dy = dx;
Polygon polygon = new Polygon(); polygon.addPoint(drawPanel.xc(c) - dx, drawPanel.ULy(r)); polygon.addPoint(drawPanel.xc(c) - dx, drawPanel.yc(r) - dy); polygon.addPoint(drawPanel.ULx(c), drawPanel.yc(r) - dy); polygon.addPoint(drawPanel.ULx(c), drawPanel.yc(r) + dy); polygon.addPoint(drawPanel.xc(c) - dx, drawPanel.yc(r) + dy); polygon.addPoint(drawPanel.xc(c) - dx, drawPanel.LRy(r));
polygon.addPoint(drawPanel.xc(c) + dx, drawPanel.LRy(r)); polygon.addPoint(drawPanel.xc(c) + dx, drawPanel.yc(r) + dy); polygon.addPoint(drawPanel.LRx(c), drawPanel.yc(r) + dy); polygon.addPoint(drawPanel.LRx(c), drawPanel.yc(r) - dy); polygon.addPoint(drawPanel.xc(c) + dx, drawPanel.yc(r) - dy); polygon.addPoint(drawPanel.xc(c) + dx, drawPanel.ULy(r));
g.setColor(drawColor); g.fillPolygon(polygon); } } //MySwissCross.java
import java.awt.Color; import java.awt.Graphics; import java.awt.Polygon;
public class MySwissCross extends MyShape {
/** * Constructor * * @param drawColor * - color of the SwissCross * @param r * - row number * @param c * - column number * @param drawPanel * - panel on which SwissCross will be drawn */ public MySwissCross(Color drawColor, int r, int c, MyPanel drawPanel) { super(drawColor, r, c, drawPanel); }
/** * Draws the SwissCross */ public void Draw(Graphics g) { double B = drawPanel.B(); int dx = MyPanel.Rounded(B / 7); int dy = dx;
Polygon polygon = new Polygon(); polygon.addPoint(drawPanel.ULx(c) + dx, drawPanel.ULy(r)); polygon.addPoint(drawPanel.ULx(c), drawPanel.ULy(r) + dy); polygon.addPoint(drawPanel.xc(c) - dx, drawPanel.yc(r)); polygon.addPoint(drawPanel.ULx(c), drawPanel.LRy(r) - dy); polygon.addPoint(drawPanel.ULx(c) + dx, drawPanel.LRy(r)); polygon.addPoint(drawPanel.xc(c), drawPanel.yc(r) + dy);
polygon.addPoint(drawPanel.LRx(c) - dx, drawPanel.LRy(r)); polygon.addPoint(drawPanel.LRx(c), drawPanel.LRy(r) - dy); polygon.addPoint(drawPanel.xc(c) + dx, drawPanel.yc(r)); polygon.addPoint(drawPanel.LRx(c), drawPanel.ULy(r) + dy); polygon.addPoint(drawPanel.LRx(c) - dx, drawPanel.ULy(r)); polygon.addPoint(drawPanel.xc(c), drawPanel.yc(r) - dy);
g.setColor(drawColor); g.fillPolygon(polygon); } }
//MyStar8.java
import java.awt.Color; import java.awt.Graphics; import java.awt.Polygon;
public class MyStar8 extends MyShape { final double RADIUS;
/** * Constructor * * @param drawColor * - color of the star with 8 point * @param r * - row number * @param c * - column number * @param drawPanel * - panel on which star will be drawn * @param RADIUS * - radius */ public MyStar8(Color drawColor, int r, int c, MyPanel drawPanel, double RADIUS) { super(drawColor, r, c, drawPanel); this.RADIUS = RADIUS; }
/** * Draws the star with 8 point */ public void Draw(Graphics g) { double B = drawPanel.B(); int dx1 = MyPanel.Rounded(0.382683 * RADIUS * B); int dx2 = MyPanel.Rounded(0.923880 * RADIUS * B); int dx3 = MyPanel.Rounded(0.707107 * B / 2); int dy1 = dx1; int dy2 = dx2; int dy3 = dx3; int x1 = drawPanel.xc(c) - dx3; int x2 = drawPanel.xc(c) - dx2; int x3 = drawPanel.xc(c) - dx1; int x4 = drawPanel.xc(c) + dx1; int x5 = drawPanel.xc(c) + dx2; int x6 = drawPanel.xc(c) + dx3; int y1 = drawPanel.yc(r) - dy3; int y2 = drawPanel.yc(r) - dy2; int y3 = drawPanel.yc(r) - dy1; int y4 = drawPanel.yc(r) + dy1; int y5 = drawPanel.yc(r) + dy2; int y6 = drawPanel.yc(r) + dy3; Polygon polygon = new Polygon(); polygon.addPoint(drawPanel.xc(c), drawPanel.ULy(r)); polygon.addPoint(x4, y2); polygon.addPoint(x6, y1); polygon.addPoint(x5, y3); polygon.addPoint(drawPanel.LRx(c), drawPanel.yc(r)); polygon.addPoint(x5, y4); polygon.addPoint(x6, y6); polygon.addPoint(x4, y5); polygon.addPoint(drawPanel.xc(c), drawPanel.LRy(r)); polygon.addPoint(x3, y5); polygon.addPoint(x1, y6); polygon.addPoint(x2, y4); polygon.addPoint(drawPanel.ULx(c), drawPanel.yc(r)); polygon.addPoint(x2, y3); polygon.addPoint(x1, y1); polygon.addPoint(x3, y2); g.setColor(drawColor); g.fillPolygon(polygon); } }
//MyPanel.java
import java.awt.Color; import java.awt.Graphics; import java.util.Random; import javax.swing.JPanel;
public class MyPanel extends JPanel {
// Instance variables private Random randomNumbers = new Random(); private final int OFFSET = 10; private final double MARGIN = 0.10; private int n; private int W; private int H; private int size; private double S; private double B; private MyShape shapes[]; private final Color myColors[] = { Color.RED, Color.BLUE, Color.GREEN, Color.GRAY, Color.YELLOW, Color.ORANGE, Color.MAGENTA, Color.LIGHT_GRAY };
/** * Constructor * * @param n * - number of squares */ public MyPanel(int n) { int i; this.n = n; this.shapes = new MyShape[n * n + 1]; i = 0; for (int r = 1; r
case 2: // Draw Triangle this.shapes[i] = new MyTriangle(fillColor, r, c, this); break;
case 3:// Draw Circle this.shapes[i] = new MyCircle(fillColor, r, c, this); break;
case 4:// Draw Diamond this.shapes[i] = new MyDiamond(fillColor, r, c, this); break;
case 5:// Draw RomanCross this.shapes[i] = new MyRomanCross(fillColor, r, c, this); break;
case 6:// Draw SwissCross this.shapes[i] = new MySwissCross(fillColor, r, c, this); break;
case 7: // Draw Star8 (8-pointed) this.shapes[i] = new MyStar8(fillColor, r, c, this, 0.10); break; } } }
@Override public void paintComponent(Graphics g) { super.paintComponent(g); W = getWidth(); H = getHeight(); size = ((W
// Draw vertical lines of S-by-S squares for (int c = 0; c
// Draw horizontal lines of S-by-S squares for (int r = 0; r
// Draw a shape circumscribed by the B-by-B square contained in each of // the S-by-S squares for (int i = 1; i
public int ULx(int c) { return (Rounded((c - 1) * S + (S * MARGIN)) + OFFSET); }
public int ULy(int r) { return (Rounded((r - 1) * S + (S * MARGIN)) + OFFSET); }
public int LRx(int c) { return (Rounded(ULx(c) + B)); }
public int LRy(int r) { return (Rounded(ULy(r) + B)); }
public int xc(int c) { return (Rounded(ULx(c) + B / 2)); }
public int yc(int r) { return (Rounded(ULy(r) + B / 2)); }
public double B() { return (B); }
public static int Rounded(double x) { return ((int) (x + 0.5)); } }

Sample Program Output r #10 Graphics Problem #3
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
