Привет, я не могу распечатать результаты программы, которая сравнивает размер трех кругов с помощью DrawingPanel.

Привет, начинающий программист, мне нужна помощь в печати результатов для этой программы. Я уже создал метод для сравнения радиуса кругов, однако я хотел бы включить цвет сравниваемых кругов при печати результатов, однако я не могу сделать это, как показано (?). как я могу напечатать другой цвет?

пример: когда я сравниваю зеленый и красный «Зеленый круг больше красного круга» и когда я сравниваю зеленый и синий «Зеленый круг меньше синего круга»

 //class
 // main method
    //asked for input and stored values in variable for r1, x1, y1
    g.setColor(Color.GREEN);
    g.fillOval(x1-r1, y1-r1, r1*2, r1*2);
   //asked for input and stored values in variable for r2, x2, y2
    g.setColor(Color.RED);
    g.fillOval(x2-r2, y2-r2, r2*2, r2*2);

    //asked for input and stored values in variable for r3, x3, y3
    g.setColor(Color.BLUE);
    g.fillOval(x3-r3, y3-r3, r3*2, r3*2); 
    int compare = size(r1, r2);
    result1(compare);
    compare = size(r1, r3);
    result1(compare);
    compare = size(r2, r3);
    result1(compare);


public static int size(int r1, int r2){
    if(r1 < r2){
      return -1;
    }else if( r1 == r2){
      return 0;
    }else{
      return 1;
    }
  }
   public static void result1(int n){
    if (n == -1){
      System.out.println("The " + ? + "cirlce is smaller than the " + ? + "circle.");
    }else if(n == 0){
      System.out.println("The " + ? + "circle is the same size as the " + ? + "cirle.");
    }else{
      System.out.println("The " + ? + "circle is bigger than the " + ? + "circle");
    } 

Спасибо за помощь.


person Johan14th    schedule 17.10.2016    source источник


Ответы (2)


Создайте кружок класса, например

public class MyCircle {
    int radius;
    Color color;

    public MyCircle(int radius, Color color) {
        this.radius = radius;
        this.color = color;
    }

    //Getter Setter...
    public int getRadius() {
        return radius;
    }

    public void setRadius(int radius) {
        this.radius = radius;
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }
}

Создайте метод для получения названия цвета в виде строки

public static String getColorAsName(MyCircle circle) {
    String colorName = null;
    if (circle.getColor().equals(Color.BLACK)) {
        colorName = "BLACK";
    } else if(circle.getColor().equals(Color.RED)) {
        colorName = "RED";
    }
    ...

    return colorName;
}

Затем создайте круговые объекты, например

MyCircle c1 = new Circle(r1, Color.GREEN)
MyCircle c2 = new Circle(r2, Color.GREEN)
etc... 

Используя getter-setter, вы можете обновлять атрибуты при необходимости.

Изменить метод

public static void result1(int n){

to

public static void result1(int n, MyCircle firstCircle, MyCircle lastCircle){
    if (n == -1){
        System.out.println("The " + getColorAsName(firstCircle) + " cirlce is smaller than the " + getColorAsName(lastCircle) + " circle.");
...

и вызовите result1() как:

result1(compare, c1, c2);
...
person rakeb.mazharul    schedule 17.10.2016
comment
@mazharul Спасибо, похоже, у меня есть работа, но мне нужно было изменить последние 2 параметра, потому что вместо цвета печатался размер. - person Johan14th; 17.10.2016
comment
Панель DrawingPanel = новая Панель DrawingPanel (400, 300); Графика g = panel.getGraphics(); - person Johan14th; 17.10.2016
comment
@ Johan14th, если ответ полезен, рассмотрите возможность его принятия. - person giusti; 09.10.2018

Вам понадобится такой метод:

public static void result1(int number, int radius1, int radius2){
    // some code
}
person Ronaldo Bini Jr.    schedule 17.10.2016