My program asks the user how much circles they want drawn and is supposed to draw an N amount of nested circles based on the user input. My code so far is as follows:
import javax.swing.*;
import java.awt.*;
public class DrawCircles extends JFrame {
DrawCircles(){
add(new Circle());
}
public static void main(String[] args) {
String number = JOptionPane.showInputDialog(null, "Please enter the number of circles you wish to display");
int circles = Integer.parseInt(number);
DrawCircles d = new DrawCircles();
d.setTitle("Nested Circles");
d.setSize(500, 500);
d.setVisible(true);
d.setLocation(200,200);
}//end main method
}//end class
class Circle extends JPanel{
public void paint(Graphics g){
g.drawOval(135, 125, 200, 200);
}//end paint()
}//end class
I have no idea how to take the user input from the main method and use it in a for loop to draw an N amount of circles. Also, I have to adjust my JFrame size accordingly to the amount of circles I will have to fit all of them in it and I don't know how to do that either. Thanks in advance.
Start by not overriding paint
, but instead use paintComponent
, and make sure you are calling super.paintComponent
when you do. Take a look at Performing custom painting for more details.
Simply pass the circles
value as a parameter to the constructor of the Circle
class and/or provided a setter to change the value
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.