2017-10-16 148 views
0

我对Java很新,我需要一些帮助。这个任务是为了获得用户输入(半径,x坐标,y坐标为&)在drawingPanel中绘制3个不同的彩色圆圈,我将该部分向下。第二部分要求我们提供一种比较两个圆的半径的静态方法,并让用户知道其中一个是更小,更大还是更大。我无法弄清楚如何在比较两者的方法中使用半径输入。获取用户输入的drawingpanel并在另一个方法中使用它

这是到目前为止我的代码:

import java.awt.*; 
import java.util.*; 
public class Circles { 
    public static final Scanner CONSOLE = new Scanner(System.in); 


    public static void blueCircle(Graphics g) { 
    g.setColor(Color.BLUE); 
    int r = CONSOLE.nextInt(); 
    int x = CONSOLE.nextInt(); 
    int y = CONSOLE.nextInt(); 
    g.fillOval(0 + x, 0 + y, r * 2, r * 2); 
    } 
    public static void greenCircle(Graphics g) { 
    g.setColor(Color.GREEN); 
    int r = CONSOLE.nextInt(); 
    int x = CONSOLE.nextInt(); 
    int y = CONSOLE.nextInt(); 
    g.fillOval(0 + x, 0 + y, r * 2, r * 2); 
    } 
    public static void redCircle(Graphics g) { 
    g.setColor(Color.RED); 
    int r = CONSOLE.nextInt(); 
    int x = CONSOLE.nextInt(); 
    int y = CONSOLE.nextInt(); 
    g.fillOval(0 + x, 0 + y, r * 2, r * 2); 

    } 
    public static void compareCircles(int r1, int r2) { 
    int x; 
    if (r1 < r2) 
     x = -1; 
    if (r1 == r2) 
     x = 0; 
    if (r1 > r2) 
     x = 1; 
    return; 
    } 
    public static void main(String[] args) { 
    DrawingPanel panel = new DrawingPanel(400, 300); 
    Graphics g = panel.getGraphics(); 
    System.out.println("Enter values for the radius, x , & y-coordinates of blue circle: "); 
    blueCircle(g); 
    System.out.println("Enter values for the radius, x , & y-coordinates of green circle: "); 
    greenCircle(g); 
    System.out.println("Enter values for the radius, x , & y-coordinates of red circle: "); 
    redCircle(g); 

    } 


} 

回答

0

可以减少你实现代码重用。为给定输入参数创建圆的通用方法。

public static void blueCircle(Graphics g, int r, int x, int y, Color c) { 
    g.setColor(c); 
    g.fillOval(0 + x, 0 + y, r * 2, r * 2); 
} 

然后一种用于半径比较的通用方法。

public static String compareCircles(int r1, int r2) { 
     String output = ""; 
     if (r1 < r2) 
      output = r1+" circle is smaller than "+r2; 
     if (r1 == r2) 
      output = "both circles are in same size."; 
     if (r1 > r2) 
      output = r1+" circle is larger than "+r2; 
     return output; 
} 

现在在主要方法中获得必要的输入并将它们传递给这些方法。

public static void main(String[] args) { 
    DrawingPanel panel = new DrawingPanel(400, 300); 
    Graphics g = panel.getGraphics(); 
    System.out.println("Enter values for the radius, x , & y-coordinates of blue circle: "); 
    int rBlue = CONSOLE.nextInt(); 
    int xBlue = CONSOLE.nextInt(); 
    int yBlue = CONSOLE.nextInt(); 
    // Better to do the validation for the user inputs 
    blueCircle(g, rBlue, xBlue, yBlue, Color.BLUE); 
    // Do the same thing for the other two circles. 
    // Then call the comparison method with your r input values like below. 
    //String output = compareCircles(rBlue, rGreen); 

}

希望这是你在找什么..

+0

如果我有通过调用比较法3次比较每个圈子彼此应该怎么办。例如:compareCircles(rb,rg); compareCircles(rb,rr); compareCircles(RR,RG); –

+0

你必须在你创建3个圈子之后3次调用comaparison函数,如你在上面评论中提到的 – Neero

+0

这是适用于你的吗?你需要帮助吗 – Neero

相关问题