java - 您好,我似乎无法打印使用 DrawingPanel 比较三个圆圈大小的程序的结果

标签 java if-statement drawing system.out

嗨,初学者程序员,我需要帮助打印该程序的结果。我已经创建了一种比较圆半径的方法,但是我想在打印结果时包括比较圆的颜色,但我似乎无法做到这一点,如(?)所示。如何打印不同的颜色?

例如:当我比较绿色和红色时 “绿色圆圈比红色圆圈大” 当我比较绿色和蓝色时 “绿色圆圈比蓝色圆圈小”

 //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");
    } 

感谢您的帮助。

最佳答案

创建一个类(class)圈子,例如

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){

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);
...

关于java - 您好,我似乎无法打印使用 DrawingPanel 比较三个圆圈大小的程序的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40092750/

相关文章:

java - 使用 http 请求通信 Java-Javascript

UIView CGGradient 剪切到绘制的 "frame"

c# - 创建特定的动态视觉效果 - 带对象的房屋计划(后续)

javascript - 将错误与 if 语句混淆

if-statement - 提供从 if 语句问题返回的函数

c# - 在图像上绘制缩放图片

java - 使用 jsch - java 以 root 用户身份执行 bash 命令

java - 什么是接口(interface)?

java - 如何从阻塞队列创建 react 器 Flux?

iphone - #ifdef 检查设备是否是 iPhone 5