代码之家  ›  专栏  ›  技术社区  ›  Jocelyn Galvan

获取drawingpanel的用户输入,并在另一种方法中使用它

  •  0
  • Jocelyn Galvan  · 技术社区  · 8 年前

    我对java很陌生,我需要一些关于作业的帮助。任务是让用户输入(半径、x坐标和y坐标)在drawingPanel中绘制3个不同颜色的圆,我把这部分记下来了。第二部分要求我们提供一种静态方法,用于比较两个圆的半径,并让用户知道其中一个圆是否更小、更大或与另一个圆的大小相同。在比较两者的方法中,我很难找出如何使用半径的输入。

    以下是我目前的代码:

    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);
    
      }
    
    
    }
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   Neero    8 年前

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

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

    希望这就是你想要的。。