2017-10-16 32 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

相關問題