2014-02-18 1 views
-2
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(); что я делаю неправильно? Это мой круг.как добавить цвет в круг в классе водителя (java)

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

, что в круге классе? – Sanjeev

+0

Вам также нужно получить информацию о цвете. Тогда 'setColor', если у вас уже есть' setColor', определенный в вашем классе 'Circle'. –

+0

@ Sanjeev я отредактировал его, так что мой класс круга тоже есть. – user3264347

ответ

0

Пара вещей:

Вы не можете создать круг, используя new Circle(), потому что нет нулевой аргумент Constructer в классе Circle, так что вы должны использовать new Circle(radius,color) Constructer.

Во-вторых для рисования цветного круга вы должны сделать круг в методе дра, что-то вроде этого:

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 в основной программе и создать new circle в его метод краски и вызвать метод circle.draw(graphics) пропускания paint's graphics

Надеется, что это помогает

Смежные вопросы