2017-10-16 1 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); 

}

희망 당신은 내가 비교 방법을 3 회 호출하여 서로 각 원을 비교해야하는 경우 어떻게해야합니까 ..

+0

찾고있는 것입니다. 예 : compareCircles (rb, rg); compareCircles (rb, rr); compareCircles (rr, rg); –

+0

위의 설명에서 언급 한 것처럼 comaparison 함수를 세 번 호출해야합니다. 서클을 만들려면 – Neero

+0

이것이 효과가 있습니까 ?? 도움이 필요한가요 – Neero

관련 문제