#java #if-statement #drawing #system.out
#java #if-statement #рисование #system.out
Вопрос:
Привет, начинающий программист, мне нужна помощь в печати результатов для этой программы. Я уже создал метод для сравнения радиуса кругов, однако я хотел бы включить цвет сравниваемых кругов при печати результатов, однако, похоже, я не могу этого сделать, как показано (?). как я могу напечатать другой цвет?
пример: когда я сравниваю зеленый и красный «Зеленый круг больше красного круга», когда я сравниваю зеленый с синим «Зеленый круг меньше синего круга»
//class
// main method
//asked for input and stored values in variable for r1, x1, y1
g.setColor(Color.GREEN);
g.fillOval(x1-r1, y1-r1, r1*2, r1*2);
//asked for input and stored values in variable for r2, x2, y2
g.setColor(Color.RED);
g.fillOval(x2-r2, y2-r2, r2*2, r2*2);
//asked for input and stored values in variable for r3, x3, y3
g.setColor(Color.BLUE);
g.fillOval(x3-r3, y3-r3, r3*2, r3*2);
int compare = size(r1, r2);
result1(compare);
compare = size(r1, r3);
result1(compare);
compare = size(r2, r3);
result1(compare);
public static int size(int r1, int r2){
if(r1 < r2){
return -1;
}else if( r1 == r2){
return 0;
}else{
return 1;
}
}
public static void result1(int n){
if (n == -1){
System.out.println("The " ? "cirlce is smaller than the " ? "circle.");
}else if(n == 0){
System.out.println("The " ? "circle is the same size as the " ? "cirle.");
}else{
System.out.println("The " ? "circle is bigger than the " ? "circle");
}
Спасибо за помощь.
Ответ №1:
Создайте класс circle, например
public class MyCircle {
int radius;
Color color;
public MyCircle(int radius, Color color) {
this.radius = radius;
this.color = color;
}
//Getter Setter...
public int getRadius() {
return radius;
}
public void setRadius(int radius) {
this.radius = radius;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
}
Создайте метод для получения имени цвета в виде строки
public static String getColorAsName(MyCircle circle) {
String colorName = null;
if (circle.getColor().equals(Color.BLACK)) {
colorName = "BLACK";
} else if(circle.getColor().equals(Color.RED)) {
colorName = "RED";
}
...
return colorName;
}
Затем создайте объекты circle, например
MyCircle c1 = new Circle(r1, Color.GREEN)
MyCircle c2 = new Circle(r2, Color.GREEN)
etc...
Используя getter-setter, вы можете обновлять атрибуты при необходимости.
Изменить метод
public static void result1(int n){
Для
public static void result1(int n, MyCircle firstCircle, MyCircle lastCircle){
if (n == -1){
System.out.println("The " getColorAsName(firstCircle) " cirlce is smaller than the " getColorAsName(lastCircle) " circle.");
...
и вызовите result1()
, как:
result1(compare, c1, c2);
...
Комментарии:
1. @majharul Спасибо, что, похоже, сработало, однако мне нужно было изменить последние 2 параметра, потому что он будет печатать размер вместо цвета.
2. Панель DrawingPanel = новая панель DrawingPanel(400, 300); Graphics g = panel.getGraphics();
3. @Johan14th если ответ полезен, пожалуйста, подумайте о его принятии.
Ответ №2:
Вам понадобится такой метод:
public static void result1(int number, int radius1, int radius2){
// some code
}