简体   繁体   中英

Java Ellipse drawing

I am writing a shape class for a circle its supposed to output the shape plus its dimensions. i am trying to do the same thing i did for my square and rectangle class but its not working. i will post here my main method, my rectangle class as a reference for what works and lastly my circle class which works but when i try to use it in the main method it gives me the error "The method add(component) in the type Container is not applicable for the arguments (Ellipse2D.Double)" on the line where it says frame3.add(myCircle);

import java.awt.Component;
import java.awt.geom.Ellipse2D;
import java.util.Scanner;
import javax.swing.JFrame;


public class Main {

    static int input;
    static int length;
    static int width;
    static int height;

    public static void main(String[] args) {

        Scanner sc1 = new Scanner(System.in);
        while(true) {
            System.out.print("\n1 for square\n2 for restangle\n3 for circle\n4 for triangle\n5 to exit\n\nWhich shape do you want? ");
            input = sc1.nextInt();
            if(input == (5)) {
                break;
            }
            if(input == (1)) {


                System.out.print("Input the length: ");
                length = sc1.nextInt();
                Square mySquare = new Square(length);

                JFrame frame = new JFrame();

                frame.setSize(300,400);
                frame.setTitle("ShapeViewer");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                frame.add(mySquare);
                frame.setVisible(true);}

            if(input==(2)) {


                System.out.print("Input the length: ");
                length = sc1.nextInt();
                System.out.print("Input the width: ");
                width = sc1.nextInt();
                Rectanglebox myRectangle = new Rectanglebox(length,width);

                JFrame frame2 = new JFrame();

                frame2.setSize(300,400);
                frame2.setTitle("ShapeViewer");
                frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                frame2.add(myRectangle);
                frame2.setVisible(true);}

           if(input==(3)) {

              System.out.print("Input the height: ");
              height = sc1.nextInt();
              System.out.print("Input the width: ");
              width = sc1.nextInt();
              Ellipse2D.Double myCircle = new Ellipse2D.Double(50,50,width,height);
              JFrame frame3 = new JFrame();

              frame3.setSize(300,400);
              frame3.setTitle("ShapeViewer");
              frame3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

              frame3.add(myCircle);
              frame3.setVisible(true);
          }

            }


        }
    }

import javax.swing.JComponent;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.Rectangle;

public class Rectanglebox extends JComponent{

    private int length;
    private int width;

    public Rectanglebox(int length,int width) {
        this.length = length;
        this.width = width;
    }

    public double getArea() {
        return length * width;
    }
    public double getPerimeter() {
        return (length * 2) + (width * 2);
    }

    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D)g;
        Rectangle square = new Rectangle(50,50,length,width);
        g2.draw(square);
        g2.drawString("Area: "+getArea(), 10, 110+width);
        g2.drawString("Perimeter: " + getPerimeter(), 110, 110+width);
        g2.drawString("Square", 110, 20);
        g2.drawString("Length: " +length, 10, 125+width);
        g2.drawString("Width: " +width, 80, 125+width);

        g2.setColor(Color.black);
        g2.fill(square);
    }
}

import javax.swing.JComponent;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;

public class Circle extends JComponent{

    private int height;
    private int width;

    public Circle(int height,int width) {
        this.height = height;
        this.width = width;
    }

    public double getArea() {
        return Math.PI * (width / 2) * (width / 2);
    }
    public double getCircumference() {
        return 2 * Math.PI * (width / 2);
    }
    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D)g;
        Ellipse2D.Double ellipse = new Ellipse2D.Double(50,50,width,height);
        g2.draw(ellipse);
        g2.drawString("Area: "+getArea(), 110, 110+height);
        g2.drawString("Perimeter: " + getCircumference(), 200, 110+height);
        g2.drawString("Square", 110, 20);
        g2.drawString("Length: " +height, 10, 110+height);
        g2.setColor(Color.black);
        g2.fill(ellipse);
    }
}

Here you are attempting to add a Shape to the frame :

Ellipse2D.Double myCircle = new Ellipse2D.Double(50,50,width,height);
//...
frame3.add(myCircle);

You are supposed to add a Component , not a Shape , so you probably wanted to write :

Circle myCircle = new Circle(height, width);
//...
frame3.add(myCircle);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM