public class Driver
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
double circumference;
double radius = 5.5;
double pi = 3.1415926;
double area = 0;
Circle circleobject = new Circle();
//Get Radius
System.out.println("Circle's Radius");
radius = keyboard.nextDouble();
System.out.println("New Circle Object");
Circle circle = new Circle();
System.out.println("Area of Circle: " + Circle.getArea());
System.out.println("Circumference of Circle: " + Circle.getCircumference());
}
}
我不明白如何使圆圈成为彩色圆圈。无论是红色,绿色还是蓝色。我需要添加什么才能使圆圈变成任何颜色?此外,似乎我无法使用Circle circleobject = new Circle()行创建一个圆形对象; 我究竟做错了什么? 这是我的圈子课。
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
/**
* A Circle is a figure that has a radius, a circumference,
* and an area.
* @author Kelvynn Cayanan
* @version 02/09/2014
*/
public class Circle
{
// Constants
public static final double pi = 3.1415926;
// instance variables
private double circumference;
private double area;
private double radius;
private Color color;
/**
* Constructs a circle of radius aRadius
* @param aRadius is the radius of the circle
*/
public Circle(double aRadius, Color type)
{
radius = aRadius; // assigns value to radius
circumference = ( 2 * pi * radius); // arithmetic for circumference
area = ( pi * radius * radius); // arithmetic for area
color = type; // displays type of color
}
public void draw (Graphics circle)
{
circle.setColor (color);
}
/**
*/
public double getRadius()
{
return radius;
}
public double getCircumference()
{
return circumference;
}
public double getArea()
{
return area;
}
public Color getColor()
{
return color;
}
}
答案 0 :(得分:0)
一些事情:
您无法使用new Circle()
创建圈子,因为您的Circle类中没有零参数构造函数,因此您必须使用new Circle(radius,color)
构造函数。
其次,要绘制彩色圆圈,您需要在绘制方法中创建一个圆形,如下所示:
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
Ellipse2D.Double cir = new Ellipse2D.Double();
cir.width = 2*radius;
cir.height = 2*radius;
cir.x = <upper left x co-ordinate>;
cir.y = <upper left y co-ordinate>;
g2d.draw(cir);
g2d.setPaint(color); // setting fill color
g2d.fill(cir);
在调用时,您需要在主程序中创建JFrame
并在其paint方法中创建new circle
并通过传递circle.draw(graphics)
希望这有帮助