【问题标题】:how to add color to circle in driver class (java)如何在驱动程序类(java)中为圆圈添加颜色
【发布时间】:2014-02-18 05:42:33
【问题描述】:
    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;
        }
    }

【问题讨论】:

  • Circle 类有什么?
  • 您还需要输入颜色。然后setColor,假设您已经在Circle 类中定义了setColor
  • @Sanjeev 我编辑了它,所以我的圈子课程也在那里。

标签: java colors


【解决方案1】:

几件事:

您无法使用 new Circle() 创建圆,因为您的 Circle 类中没有零参数构造函数,因此您必须使用 new Circle(radius,color) 构造函数。

其次,要绘制彩色圆圈,您需要在 draw 方法中制作一个圆圈,如下所示:

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,并通过传递paint's graphics 调用circle.draw(graphics) 方法

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2017-05-06
    • 1970-01-01
    • 2012-08-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-29
    • 1970-01-01
    • 1970-01-01
    • 2013-06-15
    相关资源
    最近更新 更多