2017-10-16 1 views
0

Ich bin sehr neu in Java und ich brauche ein wenig Hilfe bei einer Aufgabe. Die Zuweisung war, Benutzereingaben zu erhalten (Radius, x-Koordinate, & y-Koordinate), um 3 verschiedene farbige Kreise in einem drawingPanel zu zeichnen, ich habe diesen Teil unten. Im zweiten Teil werden wir nach einer statischen Methode gefragt, die die Radien zweier Kreise vergleicht und den Benutzer darüber informiert, ob einer kleiner, größer oder gleich groß wie der andere ist. Ich habe Probleme herauszufinden, wie man die Eingabe für die Radien in der Methode verwendet, die die beiden vergleicht.Holen Sie sich Benutzereingabe für drawingpanel und verwenden Sie es in einer anderen Methode

Hier ist mein Code so weit:

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

    } 


} 

Antwort

0

Sie können die Wiederverwendung von Code in Ihrer Implementierung reduzieren. Erstellen Sie eine generische Methode zum Erstellen des Kreises für bestimmte Eingabeparameter.

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

Dann eine generische Methode zum Radienvergleich.

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

Jetzt im Hauptverfahren die notwendigen Eingaben erhalten und an diese Methoden übergeben.

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

}

Hoffnung das ist, was Sie für ..

+0

suchen Was soll ich tun, wenn ich jeden Kreis miteinander vergleichen sind von der Vergleichsmethode 3 Mal aufgerufen wird. Bsp .: compareCircles (rb, rg); VergleichenCircles (rb, rr); compareCircles (rr, rg); –

+0

Sie müssen die Vergleichsfunktion 3 mal aufrufen, nachdem Sie 3 Kreise erstellt haben, wie Sie in obigem Kommentar erwähnt haben. – Neero

+0

Funktioniert das für Sie? Brauchen Sie Hilfe – Neero

Verwandte Themen