Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Programming only. Involves filling in missing code in specified areas. (all files located below) Copy & paste all source code with your answer. Also

Java Programming only. Involves filling in missing code in specified areas. (all files located below)

Copy & paste all source code with your answer.

Also take screenshot of it compiled.

Problem

Complete development of the Java graphical application GProblem2.java. The program 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-seven, closed-shape that is color-filled with a randomly-chosen color. Note GProblem2.javais GProblem1.java with added shapes and color.

For example, the solution to this problem randomly-chooses between eight shapesSquare, Triangle, Circle, Diamond, RomanCross, SwissCross, Star4 (a 4-pointed star), and Star8 (an 8-pointed star)and eight colors (two of which are randomly-generated combinations of red/green/blue using the technique)image text in transcribed

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

// Chapter #9, Graphics Problem #2

// "Polymorphic" Draw()-ing with programmer-defined 'type' attribute

// MyShape is useful for tending to common attributes (including 'type'),

// but is *NOT* absolutely necessary// GProblem2.java

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

import java.util.Scanner;

import java.awt.Graphics;

import java.awt.Polygon;

import javax.swing.JPanel;

import javax.swing.JFrame;

import java.util.Random;

import java.awt.Color;

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

public class GProblem2

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

{

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

public static void main(String[] args)

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

{

Scanner IN = new Scanner(System.in);

System.out.printf("n? ");

int n = IN.nextInt();

System.out.printf("size? ");

int size = IN.nextInt();

JFrame application = new JFrame("Chapters #8 and #9, Graphics Problem #2");

application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

MyPanel panel = new MyPanel(n);

application.add(panel);

application.setSize(size,size);

application.setVisible(true);

}

}

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

class MyPanel extends JPanel

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

{

private Random randomNumbers = new Random();

private final int OFFSET = 10;

private final double MARGIN = 0.10;

private final Color myColors[] =

{

Color.RED,

Color.BLUE,

Color.GREEN,

Student must add five new colors here!

};

private int n;

private int W;

private int H;

private int size;

private double S;

private double B;

private MyShape shapes[];

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

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(7)+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:

case 4:

case 5:

case 6:

Student must add instantiation of four new shapes!

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

S = size;

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

{

if ( shapes[i].GetType().GetType() == ShapeType.SQUARE.GetType() )

{

( (MySquare)shapes[i]).Draw(g);

}

if ( shapes[i].GetType().GetType() == ShapeType.TRIANGLE.GetType() )

{

( (MyTriangle)shapes[i]).Draw(g);

}

Student must add Draw() function references for four new shapes here!

if ( shapes[i].GetType().GetType() == ShapeType.STAR8.GetType() )

{

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

}

}

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

enum ShapeType

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

{

SQUARE(1),

TRIANGLE(2),

Student must add ShapeType constants for four new shapes here!

STAR8(7);

private final int type;

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

ShapeType(int type)

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

{

this.type = type;

}

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

public int GetType()

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

{

return( type );

}

}

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

class MyShape

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

{

protected ShapeType type;

protected Color drawColor;

protected int r;

protected int c;

protected MyPanel drawPanel;

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

public MyShape(ShapeType type,Color drawColor,int r,int c,MyPanel drawPanel)

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

{

this.type = type;

this.drawColor = drawColor;

this.r = r;

this.c = c;

this.drawPanel = drawPanel;

}

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

public ShapeType GetType()

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

{

return( type );

}

}

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

class MySquare extends MyShape

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

{

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

public MySquare(Color drawColor,int r,int c,MyPanel drawPanel)

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

{

super(ShapeType.SQUARE,drawColor,r,c,drawPanel);

}

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

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

/* The old-fashioned way, but *WITHOUT* a fill color.

g.drawLine(drawPanel.ULx(c),drawPanel.ULy(r),drawPanel.LRx(c),drawPanel.ULy(r));

g.drawLine(drawPanel.LRx(c),drawPanel.ULy(r),drawPanel.LRx(c),drawPanel.LRy(r));

g.drawLine(drawPanel.LRx(c),drawPanel.LRy(r),drawPanel.ULx(c),drawPanel.LRy(r));

g.drawLine(drawPanel.ULx(c),drawPanel.LRy(r),drawPanel.ULx(c),drawPanel.ULy(r));

*/

}

}

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

class MyTriangle extends MyShape

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

{

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

public MyTriangle(Color drawColor,int r,int c,MyPanel drawPanel)

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

{

super(ShapeType.TRIANGLE,drawColor,r,c,drawPanel);

}

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

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

}

}

Student must add four new shape class definitions here!

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

class MyStar8 extends MyShape

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

{

final double RADIUS;

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

public MyStar8(Color drawColor,int r,int c,MyPanel drawPanel,double RADIUS)

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

{

super(ShapeType.STAR8,drawColor,r,c,drawPanel);

this.RADIUS = RADIUS;

}

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

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

}

}

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

// Chapter #9, Graphics Problem #2B

// Polymorphic Draw()-ing with MyShape empty-bodied concrete Draw() method

// that *should be* an abstract method!

// GProblem2B.java

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

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

@Override

public void paintComponent(Graphics g)

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

{

super.paintComponent(g);

W = getWidth();

H = getHeight();

size = ((W

S = size;

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

shapes[i].Draw(g);

}

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

class MyShape

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

{

protected Color drawColor;

protected int r;

protected int c;

protected MyPanel drawPanel;

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

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!"

}

}

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

// Chapter #9, Graphics Problem #2C

// "Polymorphic" Draw()-ing with instanceof operator

// MyShape useful for tending to common attributes, but is *NOT* absolutely necessary

// GProblem2C.java

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

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

@Override

public void paintComponent(Graphics g)

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

{

super.paintComponent(g);

W = getWidth();

H = getHeight();

size = ((W

S = size;

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

{

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

((MyRomanCross) shapes[i]).Draw(g);

else if ( shapes[i] instanceof MyDiamond )

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

}

}

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

class MyShape

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

{

protected Color drawColor;

protected int r;

protected int c;

protected MyPanel drawPanel;

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

public MyShape(Color drawColor,int r,int c,MyPanel drawPanel)

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

{

this.drawColor = drawColor;

this.r = r;

this.c = c;

this.drawPanel = drawPanel;

}

}

Chapters #8 and P9, Graphics Problem #2

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions