Hellow i am a new in forum and java codding also.
I have to make an arraylist wich take circle and square .
The suser give tha point x,y and colour for the shape but additional cycle gets the radius and the square of the 2 sideσ.
and draw the cicles and sqares with the colour give user and the x,y in the end inside of every shape write the perimetr and area.
heare is my codeCode :
import java.awt.Graphics; public class Circle extends Shape{ private double radius; public Circle(double x,double y, double radius){ setPosition(x,y); this.radius=radius; } public Circle(double radius){ setPosition(0, 0); this.radius=radius; } public double area() { return 2 * Math.PI * radius * radius; } public double perimeter(){ return 2*3.14*radius; } public String toString() { return super.toString() + ": Center " + "with Radius = " + radius + " with area : " + this.area(); } }
Code :
import java.awt.Graphics; import javax.swing.JPanel; public class Square extends Shape { private double height, width; public Square(double x,double y, double height,double width){ setPosition(x,y); this.height=height; this.width=width; } public Square(double height, double width) { super.setPosition(0,0); this.height=height; this.width=width; } public void setdimensions(double h, double w) { height = h; width = w; } public double area() { return height * width; } public double perimeter() {return 2*height+2*width;} public String toString() { return super.toString() + ": Rectangle(" + height + " x " + width + ")" + " with area : " + this.area(); } }
Code :
import java.awt.Graphics; import javax.swing.JPanel; public abstract class Shape { private double x,y; public void setPosition(double x, double y){ this.x=x; this.y=y; } protected double getX() { return x; } protected double getY() { return y; } public abstract double area(); public abstract double perimeter(); public String toString() { return "Shape(" + x + ", " + y + ")"; } }
Code :
import java.awt.Color; import java.awt.GradientPaint; import java.awt.Graphics; import java.awt.Graphics2D; import java.util.ArrayList; import java.util.Scanner; import javax.swing.JFrame; import javax.swing.JPanel; public class Main extends JPanel{ static ArrayList<Shape> shape=new ArrayList<Shape>(); public void init() //called by another class method { JFrame frame = new JFrame("schematics"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Shape myPanel = new Shape(); myPanel.setBackground(Color.WHITE); frame.add( myPanel ); frame.setSize(1000, 540 ); frame.setVisible( true ); myPanel.setSize(1000, 500); myPanel.setVisible(true); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.BLACK); g.translate(0,500); for(int i = 0; i<shape.size(); i++){ g.fillOval(shape.get(i).getX(),shape.get(i).getX(), 50, 50); } public static void main( String args[] ) { int epil ; double Rad,Sid,Sid1,X,Y; Scanner scan=new Scanner(System.in); System.out.println("Give choice : 1 Circle 2 Square "); epil=scan.nextInt(); while(epil!=0){ if(epil==1){ System.out.println("Give coordinates X and Y"); X=scan.nextDouble(); Y=scan.nextDouble(); System.out.println("Give Radius"); Rad=scan.nextDouble(); Circle cir=new Circle(Rad); cir.setPosition(X,Y); shape.add(cir); //d.createCircle(X,Y,Rad); } if(epil==2){ System.out.println("Give coordinates X and Y"); X=scan.nextDouble(); Y=scan.nextDouble(); System.out.println("Give Sides"); Sid=scan.nextDouble(); Sid1=scan.nextDouble(); Square sqr=new Square(Sid,Sid1); sqr.setPosition(X,Y); shape.add(sqr); //d.createCircle(X,Y,Sid); } System.out.println("Give choice : 1 Circle 2 Square "); epil=scan.nextInt();} System.out.println(shape); // shape.get(k).draw(g); } }