Draw a Circle Based on Entered Values Javafx
#one
Random Circle Generator with user input
Posted 06 Apr 2010 - 12:23 AM
Here is my problem. I am trying to generate a number of circles in random locations and with random sizes in my defined JFrame. This is to be done based on the input (int) from the user. I am using Scanner for the input and not having whatever issues there. The JFrame works and the code compiles and executes fine but only i circle appears. How practice I get the user inputted integer to tell the programme to draw the circles at random locals and with random sizes the number of times the user has entered? I volition post the code of the program below. It is separated into a component and viewer.
Component Lawmaking:
import javax.swing.JComponent; import javax.swing.JPanel; import coffee.awt.Graphics; import java.awt.Graphics2D; import java.util.Random; import java.awt.geom.Ellipse2D; import javax.swing.JFrame; import java.util.Scanner; /** A CircleComponent draws a number of random circles. */ public class CircleComponent extends JComponent { /** Constructs a CircleComponent that draws a given number of circles. @param n the number of circles to draw. */ public void CircleComponent(int n) { /** Constructs a circle n times, based on user input, in random locations with random bore. */ n = 1; Random generator = new Random(); } int n = 1; public void paintComponent(Graphics g) { //Recover Graphics2D Graphics2D g2 = (Graphics2D) g; //Depict the circle Ellipse2D.Double circle = new Ellipse2D.Double(l, 100, 30, sixty); g2.draw(circle); } private Random generator; private int number; } and the Viewer Lawmaking:
import javax.swing.JFrame; import java.util.Scanner; import javax.swing.JLabel; /** */ public class CircleViewer { public static void main(Cord[] args) { Scanner in = new Scanner(Organisation.in); System.out.println("Enter the number of circles y'all want to see "); Organisation.out.print("(Please enter an integer between i and 20): "); int i = in.nextInt(); JFrame frame2 = new JFrame(); frame2.setSize(800, 800); frame2.setTitle("Circle Viewer"); frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); CircleComponent component = new CircleComponent (); frame2.add together(component); frame2.setVisible(true); } } Any help with the random generation dictated by the user input would be great. Additionally, all of the circles must stay completely inside the frame. I am not sure how to fix that either but call back I tin effigy it out after some sleep .
EDIT: Inserted code tags, PennyBoki. Delight in futurity. And try not to utilize large fonts. Thanks.
This mail has been edited by PennyBoki: 06 April 2010 - 01:39 AM
Reason for edit:: code tags and large fonts
#ii
Re: Random Circle Generator with user input
Posted 06 Apr 2010 - 02:21 AM
Hello I made corrections in your codes and added comments, please follow the comments and I call back yous'll clear things upwards a little chip.
Component Code:
import javax.swing.JComponent; import javax.swing.JPanel; import coffee.awt.Graphics; import java.awt.Graphics2D; import java.util.Random; import java.awt.geom.Ellipse2D; import javax.swing.JFrame; import coffee.util.Scanner; import java.lang.Math; /** A CircleComponent draws a number of random circles. */ public class CircleComponent extends JComponent { /** Constructs a CircleComponent that draws a given number of circles. @param due north the number of circles to draw. */ int hlp;//I need this to pass n to the for loop so that I can count the number of circles CircleComponent(int northward) { /** Constructs a circle n times, based on user input, in random locations with random bore. */ hlp=n; // hlp gets the value of n, that is i in CircleViewr.coffee } public void paintComponent(Graphics g) { Random generator = new Random(); //I create generator here instead of the constructor //Recover Graphics2D Graphics2D g2 = (Graphics2D) 1000; //Draw the circle for(int i=0;i<hlp;i++)//I apply for loop so That i could repeat the drawing hlp times { /*Encounter the genetartor.nextInt()%100+100 I use it to generate me some random locations*/ Ellipse2D.Double circumvolve = new Ellipse2D.Double(generator.nextInt()%100+100, generator.nextInt()%100+100, 50, 50); g2.draw(circumvolve); //System.out.println(generator.nextInt()%100+100); } } }
and the Viewer Code:
import javax.swing.JFrame; import coffee.util.Scanner; import javax.swing.JLabel; /** */ public class CircleViewer { public static void principal(Cord[] args) { Scanner in = new Scanner(Organisation.in); System.out.println("Enter the number of circles you want to see "); System.out.print("(Please enter an integer between ane and 20): "); int i = in.nextInt(); JFrame frame2 = new JFrame(); frame2.setSize(800, 800); frame2.setTitle("Circle Viewer"); frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); /*You need to pass the input of the user so that it could go to the constructor and *then to the for loop when you count the circles*/ CircleComponent component = new CircleComponent (i); frame2.add(component); frame2.setVisible(true); } } I hope this will assist you lot larn!
#3
Re: Random Circle Generator with user input
Posted 06 April 2010 - 08:55 AM
Thanks for the aid. I accept a problem with the input of (i) into the line:
CircleComponent component = new CircleComponent(i);
It returns this mistake lawmaking:
CircleViewer.coffee:23: cannot find symbol
symbol : constructor CircleComponent(int)
location: class CircleComponent
CircleComponent component = new CircleComponent(i);
This fault goes abroad when I remove the (i) but and so no circle is present when the plan runs. Prior to inserting the
generator.nextInt()%100+100
in the CircleComponent I was seeing at least one circle appear when I ran the programme. now I am seeing nada.
???
#iv
Re: Random Circle Generator with user input
Posted 06 Apr 2010 - 03:41 PM
A constructor has no render blazon...
public class CircleComponent extends JComponent { /** Constructs a CircleComponent that draws a given number of circles. @param n the number of circles to describe. */ public CircleComponent(int n) { this.northward = north; // This ceases to exist beyond this method/constructor Random generator = new Random(); } int due north = one; As you can come across, i prepare the object'due south "n" variable (this.n) to the "n" passed to the method. Also, the Random object needs to be an instance variable to use elsewhere.
#5
Re: Random Circumvolve Generator with user input
Posted 06 April 2010 - 06:02 PM
Cheers for the assistance. I am at present seeing random circles. I was able to gear up that problem and I at present accept circles with random size and location. Still, I am non getting the input in the viewer to dictate the number of circles. I will post the updated code below.
Component Code
import javax.swing.JComponent; import javax.swing.JPanel; import java.awt.Graphics; import java.awt.Graphics2D; import java.util.Random; import java.awt.geom.Ellipse2D; import javax.swing.JFrame; import java.util.*; /** A CircleComponent draws a number of random circles. */ public class CircleComponent extends JComponent { /** Constructs a CircleComponent that draws a given number of circles. @param n the number of circles to draw. */ int nu; //I employ this to pass the user input, n, through the loop. public void CircleComponent(int due north) { //Constructs a circle n times, based on user input, in random locations with random bore. int i=northward; //Gets the value of n, for utilise in CircleViewer. } public void paintComponent(Graphics g) { //Create random generator Random generator = new Random(); //Recover Graphics2D Graphics2D g2 = (Graphics2D) grand; //Construct the loop allowing the input from the user, hlp, to be used as a parameter. for(int n=one;n<=20;north++) { //Draw the circle with random locations and random size available. Ellipse2D.Double circle = new Ellipse2D.Double(generator.nextInt()%200+200,generator.nextInt()%200+200,generator.nextInt()%100+100,generator.nextInt()%100+100); g2.draw(circumvolve); } } } Viewer Code
import javax.swing.JComponent; import javax.swing.JPanel; import java.awt.Graphics; import java.awt.Graphics2D; import java.util.Random; import java.awt.geom.Ellipse2D; import javax.swing.JFrame; import coffee.util.*; /** A CircleComponent draws a number of random circles. */ public class CircleComponent extends JComponent { /** Constructs a CircleComponent that draws a given number of circles. @param n the number of circles to draw. */ int nu; //I apply this to pass the user input, n, through the loop. public void CircleComponent(int due north) { //Constructs a circle n times, based on user input, in random locations with random diameter. int i=northward; //Gets the value of due north, for use in CircleViewer. } public void paintComponent(Graphics g) { //Create random generator Random generator = new Random(); //Recover Graphics2D Graphics2D g2 = (Graphics2D) one thousand; //Construct the loop assuasive the input from the user, hlp, to exist used equally a parameter. for(int northward=1;n<=20;due north++) { //Draw the circumvolve with random locations and random size available. Ellipse2D.Double circle = new Ellipse2D.Double(generator.nextInt()%200+200,generator.nextInt()%200+200,generator.nextInt()%100+100,generator.nextInt()%100+100); g2.describe(circle); } } } Thanks for the help. This is my first time doing whatsoever programming. I have been learning Coffee for about x weeks now and it is nevertheless a little foreign to me.
Source: https://www.dreamincode.net/forums/topic/166377-random-circle-generator-with-user-input/
0 Response to "Draw a Circle Based on Entered Values Javafx"
Post a Comment