Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Programming only. Involves filling in missing code in specified areas. Copy & paste all source code with your answer. Also take screenshot of it

Java Programming only. Involves filling in missing code in specified areas.

Copy & paste all source code with your answer.

Also take screenshot of it compiled.

image text in transcribed

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

// Chapter #4, Graphics Problem #1

// GProblem1.java

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

import java.util.Scanner;

import java.awt.Graphics;

import javax.swing.JPanel;

import javax.swing.JFrame;

import java.util.Random;

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

public class GProblem1

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

{

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

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("Chapter #4, Graphics Problem #1");

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 int n;

private Random randomNumbers = new Random();

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

public MyPanel(int n)

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

{

this.n = n;

}

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

public void paintComponent(Graphics g)

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

{

super.paintComponent(g);

final int OFFSET = 10;

final double MARGIN = 0.10;

int W = getWidth();

int H = getHeight();

int size = ((W

double S = size;

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

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

int c = 0;

while ( c

{

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

c++;

}

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

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

*********************************

* STUDENT PROVIDES MISSING CODE *

*********************************

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

r = 1;

while ( r

{

c = 1;

while ( c

{

int ULx = Rounded((c-1)*S + (S*MARGIN))+OFFSET;

int ULy = Rounded((r-1)*S + (S*MARGIN))+OFFSET;

int LRx = Rounded(ULx + B);

int LRy = Rounded(ULy + B);

int xc = Rounded(ULx+B/2);

int yc = Rounded(ULy+B/2);

switch ( randomNumbers.nextInt(5)+1 )

{

case 1: // Draw square (the hard way)

g.drawLine(ULx,ULy,LRx,ULy);

g.drawLine(LRx,ULy,LRx,LRy);

g.drawLine(LRx,LRy,ULx,LRy);

g.drawLine(ULx,LRy,ULx,ULy);

break;

case 2: // Draw triangle

g.drawLine(ULx,LRy, xc,ULy);

g.drawLine(LRx,LRy, xc,ULy);

g.drawLine(LRx,LRy,ULx,LRy);

break;

case 3: // Draw student-designed shape (any shape) #1

*********************************

* STUDENT PROVIDES MISSING CODE *

*********************************

break;

case 4: // Draw student-designed shape (any shape) #2

*********************************

* STUDENT PROVIDES MISSING CODE *

*********************************

break;

case 5: // Draw student-designed shape (any shape)32 #3

*********************************

* STUDENT PROVIDES MISSING CODE *

*********************************

break;

}

c++;

}

r++;

}

}

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

private static int Rounded(double x)

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

{

return( (int) (x + 0.5) );

}

}

Problem Complete development of the Java graphical application GProblemI 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 JErame which contains a JPanel in hich the n-by-n array of squares is displayed), then draws a randomly-chosen shape in each of the n? squares. Administrator MS-DOS -java GProblem1 CNCOURSES CS2323Java Chapter4javac GProblen1.java CE COURSES CS2323Java Chapter4>jva GProbleni ize? 780 2a Chepter 4 Graphics Problem1

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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