Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

java You need to implemenl a program thal draws disTerent ligures based on uscr

ID: 3717032 • Letter: J

Question


java

You need to implemenl a program thal draws disTerent ligures based on uscr choicc. You need o implemcnt the following class hicrarchy Shape Oval Square Triangle Shape is an abstract class that has draw as n abstra class, draw takes thc index of the shape in the array of shapes. Hach other class (Oval, Square, Triangle) implemens draw and calculates the starting position of the shape using the provided index. In the test class promote the user to enter the number of requested shapes of each type. Then creales the requested number of cach shape type and save them in an array. In a for loop it draws cach shape Sample Output: Number of Ovals: 2 Number of Triangles: 2 Number of Squares: 3

Explanation / Answer

Below I had written code for the above java program and it is working fine.

Hope this helps...

Thankyou.. :)

import java.io.*;

import java.util.*;

import javax.swing.JFrame;

import java.awt.Graphics;

import java.awt.Color;

class Draw extends JFrame{

static int a=0,b=0,c=0;

public Draw(){

//to Set JFrame title

super("Draw A Figures In JFrame");

//Set default close operation for JFrame

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size

setSize((a+b+c)*150,160);

//Make JFrame visible

setVisible(true);

}

public void paint(Graphics g){

super.paint(g);

int gap=0;

for(int i=1;i<=(a+b+c);i++){

if(i<=a){

//draw circle outline

g.drawRoundRect(i*50+gap,50,100,80,100,100);

//set color to Green

g.setColor(Color.GREEN);

//fill circle with GREEN color

g.fillRoundRect(i*50+gap,50,100,80,100,100);

gap=gap+80;

}

else if(i<=a+b){

//draw triangle outline

int x[]={i*50+gap,i*50+gap,i*50+gap+50};

int y[]={i*50+gap,i*50+gap,i*50+gap+50};

System.out.println("Xpoints:"+x[0]+" "+x[1]+" "+x[2]);

System.out.println("Ypoints:"+y[0]+" "+y[1]+" "+y[2]);

g.drawPolygon(x,y,3);

//set color to Green

g.setColor(Color.GREEN);

//fill triangle with GREEN color

g.fillPolygon(x,y,3);

gap=gap+80;

}

else if(i<=a+b+c){

//draw rectangle outline

g.drawRect(i*50+gap,50,100,80);

//set color to Green

g.setColor(Color.GREEN);

//fill rectangle with GREEN color

g.fillRect(i*50+gap,50,100,80);

gap=gap+80;

}

}

}

public static void main(String[]args){

Scanner in = new Scanner(System.in);

System.out.print("Enter number of Circles:");

a = in.nextInt();

System.out.print("Enter number of Triangles:");

b = in.nextInt();

System.out.print("Enter number of Rectangles:");

c = in.nextInt();

Draw drawing=new Draw();

}

}