Question: Re-develop the Java graphical application GProblem3 (Located below and completed) to use the following interface //--------------------------------------------------------- // Chapter #10, Graphics Problem #3B // Polymorphic Draw()-ing

Re-develop the Java graphical application GProblem3 (Located below and completed) to use the following interface

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

// Chapter #10, Graphics Problem #3B

// Polymorphic Draw()-ing with MyShape Draw() method added

// via an interface

// DrawInterface.java

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

import java.awt.Graphics;

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

public interface DrawInterface

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

{

void Draw(Graphics g);

}

Name the interface version of GProblem3 to GProblem3B.java.

Take screenshot of it compiled.

Sample screenshot:

Re-develop the Java graphical application GProblem3 (Located below and completed) to use

GPROBLEM3

//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,

new Color(120, 200, 128),/* new color 1 */

new Color(randomNumbers.nextInt(255), randomNumbers.nextInt(255),

randomNumbers.nextInt(255))/* new color 2 (random) */,

new Color(randomNumbers.nextInt(255), randomNumbers.nextInt(255),

randomNumbers.nextInt(255)) /* new color 3 (random) */};

/**

* 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

for (int c = 1; c

Color fillColor = myColors[randomNumbers

.nextInt(myColors.length)];

i++;

switch (randomNumbers.nextInt(8) + 1) {

case 1: // Draw Square

this.shapes[i] = new MySquare(fillColor, r, c, this);

break;

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;

case 8: // Draw the Cross with sharp edges (new shape)

this.shapes[i] = new MyCrossWithSharpEdges(fillColor, r, c,

this);

break;

}

}

}

@Override

public void paintComponent(Graphics g) {

super.paintComponent(g);

W = getWidth();

H = getHeight();

size = ((W

S = size / n;

B = S * (1 - 2 * MARGIN);

// Draw vertical lines of S-by-S squares

for (int c = 0; c

g.drawLine(Rounded(c * S) + OFFSET, 0 + OFFSET, Rounded(c * S)

+ OFFSET, size + OFFSET);

g.drawLine(size + OFFSET, 0 + OFFSET, size + OFFSET, size + OFFSET);

// Draw horizontal lines of S-by-S squares

for (int r = 0; r

g.drawLine(0 + OFFSET, Rounded(r * S) + OFFSET, size + OFFSET,

Rounded(r * S) + OFFSET);

g.drawLine(0 + OFFSET, size + OFFSET, size + OFFSET, size + OFFSET);

// Draw a shape circumscribed by the B-by-B square contained in each of

// the S-by-S squares

for (int i = 1; i

/**

* just call the draw method, the corresponding draw method will be

* called polymorphically, no need of checking via instanceof

* operator

*/

shapes[i].Draw(g);

/*

* if (shapes[i] instanceof MySquare) ((MySquare)

* shapes[i]).Draw(g); else if (shapes[i] instanceof MyTriangle)

* ((MyTriangle) shapes[i]).Draw(g); else if (shapes[i] instanceof

* MyCircle) ((MyCircle) shapes[i]).Draw(g); else if (shapes[i]

* instanceof MyDiamond) ((MyDiamond) shapes[i]).Draw(g); else if

* (shapes[i] instanceof MyRomanCross) ((MyRomanCross)

* shapes[i]).Draw(g); else if (shapes[i] instanceof MySwissCross)

* ((MySwissCross) shapes[i]).Draw(g); // else if (shapes[i]

* instanceof MyStar4) // ((MyStar4) shapes[i]).Draw(g); else //if (

* shapes[i] instanceof MyStar8 ) ((MyStar8) shapes[i]).Draw(g);

*/

}

}

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));

}

}

//MyShape.java

import java.awt.Color; import java.awt.Graphics;

public abstract 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 abstract void Draw(Graphics g); }

//MyCrossWithSharpEdges.java

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Polygon;

public class MyCrossWithSharpEdges extends MyShape {

/**

* Constructor

*

* @param drawColor

* - color of the cross

* @param r

* - row number

* @param c

* - column number

* @param drawPanel

* - panel on which the cross with sharp edges will be drawn

*/

public MyCrossWithSharpEdges(Color drawColor, int r, int c,

MyPanel drawPanel) {

super(drawColor, r, c, drawPanel);

}

/**

* Draws the Cross with sharp points

*/

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), drawPanel.ULy(r) + dy);

polygon.addPoint(drawPanel.xc(c) - dx, drawPanel.yc(r));

polygon.addPoint(drawPanel.ULx(c) + dx, drawPanel.LRy(r));

polygon.addPoint(drawPanel.xc(c), drawPanel.yc(r) + dy);

polygon.addPoint(drawPanel.LRx(c), drawPanel.LRy(r) - dy);

polygon.addPoint(drawPanel.xc(c) + dx, drawPanel.yc(r));

polygon.addPoint(drawPanel.LRx(c) - dx, drawPanel.ULy(r));

polygon.addPoint(drawPanel.xc(c), drawPanel.yc(r) - dy);

g.setColor(drawColor);

g.fillPolygon(polygon);

}

}

//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); } }

public class Test {

public static void main(String[] args) {

try {

String temp = JOptionPane.showInputDialog("Enter the size: ");

if (temp != null) {

int n = Integer.parseInt(temp);

if (n > 0) {

JFrame frame = new JFrame();

frame.add(new MyPanel(n));

frame.setVisible(true);

frame.setSize(500, 500);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

} else {

JOptionPane.showMessageDialog(null, "Invalid size:");

}

}

} catch (Exception e) {

JOptionPane.showMessageDialog(null, "Invalid size:");

}

}

}

SAMPLE SCREENSHOT

the following interface //--------------------------------------------------------- // Chapter #10, Graphics Problem #3B // Polymorphic

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!