我正在为圆形写一个形状类,它应该输出形状加上它的尺寸。我正在尝试为我的方形和矩形类做同样的事情,但它不起作用。我将在这里发布我的主要方法,我的矩形类作为参考什么工作,最后我的圆类工作,但当我尝试在主方法中使用它时,它给我错误"方法添加(组件)在类型Container中不适用于参数(Ellipse2D.Double)"在它所说的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);
}
}
答案 0 :(得分:0)
您正尝试在框架中添加Shape
:
Ellipse2D.Double myCircle = new Ellipse2D.Double(50,50,width,height);
//...
frame3.add(myCircle);
你应该添加Component
,而不是Shape
,所以你可能想写一下:
Circle myCircle = new Circle(height, width);
//...
frame3.add(myCircle);