【问题标题】:Get user input for drawingpanel and use it in another method获取绘图面板的用户输入并在另一种方法中使用它
【发布时间】:2017-10-16 21:27:02
【问题描述】:

我对 java 很陌生,我需要一些关于作业的帮助。任务是让用户输入(半径、x 坐标和 y 坐标)在绘图面板中绘制 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);

  }


}

【问题讨论】:

    标签: java methods user-input


    【解决方案1】:

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

    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次来比较每个圆圈,我该怎么办。例如:compareCircles(rb,rg);比较圆圈(rb,rr); compareCircles(rr,rg);
    • 你必须调用comaparison函数3次,如你在上面评论中提到的那样创建3个圈子
    猜你喜欢
    • 2021-03-10
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多