java You need to implemenl a program thal draws disTerent ligures based on uscr
ID: 3717032 • Letter: J
Question
java
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();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.