2017-10-16 9 views
0

Je suis très nouveau à Java et j'ai besoin d'un peu d'aide sur une tâche. L'affectation consistait à obtenir l'entrée de l'utilisateur (rayon, coordonnée x, coordonnée y &) pour dessiner 3 cercles colorés différents dans un dessinPanel, j'ai cette partie vers le bas. La deuxième partie nous demande une méthode statique qui compare les rayons de deux cercles et permet à l'utilisateur de savoir si l'un est plus petit, plus grand ou de la même taille que l'autre. J'ai du mal à comprendre comment utiliser l'entrée pour les rayons dans la méthode qui compare les deux.Obtenir l'entrée de l'utilisateur pour drawingpanel et l'utiliser dans une autre méthode

Voici mon code à ce jour:

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

    } 


} 

Répondre

0

Vous pouvez réduire la réutilisation de code dans votre mise en œuvre. Faire une méthode générique pour créer le cercle pour les paramètres d'entrée donnés.

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

Ensuite, une méthode générique pour la comparaison des rayons.

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

Maintenant dans le mehod principal obtenir les entrées nécessaires et les passer à ces méthodes.

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

}

Hope this est ce que vous cherchez ..

+0

Que dois-je faire si je dois comparer chaque cercle à l'autre en appelant la méthode de comparaison 3 fois. Ex: compareCircles (rb, rg); compareCircles (rb, rr); comparer les cercles (rr, rg); –

+0

Vous devez appeler la fonction comaparison 3 fois après yo créer 3 cercles comme vous l'avez mentionné dans le commentaire ci-dessus – Neero

+0

Est-ce que cela fonctionne pour vous ?? As tu besoin d'aide – Neero