Android Draw Random Circles on Layout
#1
Creating 100 Random circles of random colors & locations in Java
Posted 19 February 2017 - 11:17 PM
I have to write a program that creates 100 random circles of random colors, diameters and locations. Each circle must fit within the window. I take created the Circle class and am able to compile it. I next have to consummate the RunCircles program that volition draw the circles. I am supposed to provide return value for randUniform and randColor and then add code to create and draw the circles using a while loop. I am stuck at what I demand to practise to that. This is what I have and then far. Any help will be greatly
appreciated.
import java.awt.*; import coffee.awt.event.*; import javax.swing.*; import java.util.Random; public class Circle { private int bore; private int ten; private int y; public void render (Graphics page) { int count = 0; int NumOfCircles = 100; int diameter; int 10, y; Random generator = new Random(); while (NumOfCircles <= 100) { diameter = generator.nextInt(sixty); x = generator.nextInt(300); y = generator.nextInt(500); page.setColor(new Colour(generator.nextInt(256), generator.nextInt(256),+ generator.nextInt(256))); folio.fillOval(x, y, bore, diameter); page.setColor(Color.black); page.drawOval(ten,y,diameter,diameter); } } }
import coffee.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.Random; public course RunCircles extends JPanel { public static final int NUM_CIRCLES = 100; public static int WIDTH = 800, HEIGHT = 600; // to generate random locations and colors private Random gen; public RunCircles () { super(); gen = new Random(); setBackground(Colour.black); setPreferredSize(new Dimension(WIDTH, Peak)); } /** randUniform(a, B)returns a uniformly distributed random integer greater than or equal to a and strictly less than b. */ individual int randUniform (int a, int B)/>/> { // PROVIDE THE CORRECT Render VALUE return 0; } /** randColor() returns a random color which is non also close to black */ individual Colour randColor() { // PROVIDE THE Correct RETURN VALUE return new Color(0, 0, 0); } /** Draw 100 circles when the window is made visible */ public void paintComponent (Graphics folio) { // call super method to prepare background color super.paintComponent(page); // Add CODE TO CREATE AND DRAW THE CIRCLES, USING A WHILE LOOP. } public static void main (String [] args) { JFrame frame = new JFrame("RunCircles"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add together(new RunCircles()); frame.pack(); frame.setVisible(truthful); } }
#two
Re: Creating 100 Random circles of random colors & locations in Java
Posted twenty February 2017 - 01:37 AM
Well,
the easiest mode to approach this is to generate a new cirle location generate a new circle diameter and so a new circle color...
now error checking
have the location of your circle and accept the radius which is half the diameter
in the 10 aeroplane check if x+radius > window width and if x-radius < 0
in the y plane check if y+radius > window height and if y-radius < 0
if any of these evaluate true do non draw a circle but return from the function
if all of these are false so your circle is inside the window and you depict information technology.
This postal service has been edited by snoopy11: xx Feb 2017 - 06:54 AM
#3
Re: Creating 100 Random circles of random colors & locations in Java
Posted xx February 2017 - 05:25 AM
Not certain the purlieus tests are right. One of the boundaries is 0.
#iv
Re: Creating 100 Random circles of random colors & locations in Java
Posted xx February 2017 - 06:53 AM
aye you are right i of them should have been nix
but you go the full general thought i will change it
#5
Re: Creating 100 Random circles of random colors & locations in Java
Posted xx February 2017 - 07:07 AM
Quote
I have created the Circle class
Is that the requirement? It looks a little more similar, imo, that they are expecting you lot to add lawmaking to RunCircles
#half-dozen
Re: Creating 100 Random circles of random colors & locations in Java
Posted xx February 2017 - 08:27 AM
Quote
I have created the Circle class
Where does the program create the 100 instances of the Circle class that would be used in the paintComponent method to draw them? I would expect a list of Circle objects that would be used to impress the circles.
#7
Re: Creating 100 Random circles of random colors & locations in Java
Posted twenty February 2017 - 08:45 AM
Quote
I would wait a list of Circle objects
Why?
#8
Re: Creating 100 Random circles of random colors & locations in Coffee
Posted twenty February 2017 - 08:50 AM
I was required to write the Circle class, and then have to consummate the code for the RunCircles plan that will utilise the Circles program. I was supplied with a "shell" for both of them and equally I said I was able to consummate and compile the Circle course. I am struggling with completing the RunCircles program.
I was required to write the Circumvolve class, and and then have to complete the code for the RunCircles programme that will use the Circles program. I was supplied with a "beat out" for both of them and equally I said I was able to consummate and compile the Circumvolve class. I am struggling with completing the RunCircles plan. I have completed most of the RunCircles programme, but am unsure how to complete the lawmaking for lines 32, 41 and 55 in the 2nd programme.
#9
Re: Creating 100 Random circles of random colors & locations in Java
Posted 20 February 2017 - 08:56 AM
Sorry well-nigh the duplicate reply-I tried to edit out my first paragraph on my reply but couldn't find whatsoever way to edit it.
#10
Re: Creating 100 Random circles of random colors & locations in Coffee
Posted 20 February 2017 - 09:19 AM
If there are to be 100 circles with the Circle class representing one instance of a circumvolve,
I would assume there would demand to be a list of Circumvolve objects that the paintComponent() method could use.
One consideration on the pattern would be
are at that place to be 100 circles that are shown over and over as window is manipulated
Or would the code always generate 100 new circles each time?
With the first design, the specs for the circles would demand to be saved somewhere like in a Circle form object
With the second, at that place wouldn't be anything to salve and there wouldn't exist any need for a Circumvolve class.
This mail service has been edited by NormR: 20 February 2017 - 09:twenty AM
#11
Re: Creating 100 Random circles of random colors & locations in Java
Posted 20 February 2017 - 09:23 AM
Well for line 32 you lot have this template code
/** randUniform(a, b )returns a uniformly distributed random integer greater than or equal to a and strictly less than b. */ individual int randUniform (int a, int b ) { // PROVIDE THE Right Render VALUE render 0; }
so y'all need to generate a local random number start of all don't you lot ?
and then its usually something similar
Random rand; int randomNum = rand.nextInt((max - min) + 1) + min; //where max and min are your ranges.
so now nosotros have
/** randUniform(a, b )returns a uniformly distributed random integer greater than or equal to a and strictly less than b. */ individual int randUniform (int a, int b ) { Random rand; int randomNum = rand.nextInt((b - a) + 1) + a; render randomNum; }
Now because we have to be strictly less than b nosotros have to take i away from b don't we ?
/** randUniform(a, b )returns a uniformly distributed random integer greater than or equal to a and strictly less than b. */ private int randUniform (int a, int b ) { Random rand; b = b-1; int randomNum = rand.nextInt((b - a) + 1) + a; return randomNum; }
but by following the instructions step past stride we chop-chop get there
Now I advise you try to do the same with the randColor function.
#12
Re: Creating 100 Random circles of random colors & locations in Java
Posted 20 February 2017 - 09:41 AM
"randUniform(a, b )returns a uniformly distributed
and strictly less than b"
I read that and reread that and then many times my mind kept getting confused on what information technology was referring to! And then randColor should look like this.....?
private Colour randColor(red, light-green, blue) { Random rand; Colour randColor = new color (reddish, greenish, blue) int cerise = randColor (256) int greenish = randColor (256) int blue = randColor (256) render randColor; }
Edited because of forgotten ;
individual Color randColor(red, green, blueish) { Random rand; Color randColor = new color (cherry, dark-green, blue); int red = randColor (256); int green = randColor (256); int blue = randColor (256); return randColor; }
#13
Re: Creating 100 Random circles of random colors & locations in Java
Posted 20 February 2017 - 09:51 AM
No,
you are not reading the instructions which state
/** randColor() returns a random color
which is not too close to blackness */
then what is blackness in r,chiliad,b terms ?
#14
Re: Creating 100 Random circles of random colors & locations in Java
Posted xx February 2017 - 09:59 AM
Since black is 0,0,0 I need to have the color fix at int red = randColor <0 && >256 and the same for greenish and bluish?
#15
Re: Creating 100 Random circles of random colors & locations in Java
Posted 20 February 2017 - x:17 AM
No
< is less than
> is more than
thats merely algebra
Have you done any algebra ?
and just calling randColor recursively wont help you accept to generate three different random numbers one each for
r,chiliad,b.
Source: https://www.dreamincode.net/forums/topic/401555-creating-100-random-circles-of-random-colors-locations-in-java/
0 Response to "Android Draw Random Circles on Layout"
Post a Comment