Trying to understand this code, can someone help?
posted 10 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hey everyone! I have this code that is suppose to write 50 rectangles and 50 circles on a white screen. The 50 rectangles
and circles puts out on the screen randomly with color, size and position.
I just want to figure out how to write one of these and what to think of when writing one of these codes. And if possible if
anyone could explain how this code works? =)
The thing is I have tried to compile it but it doesn't work.
Error = "Syntax error on token "else", delete this token
Syntax error on tokens, Catches expected instead"
and circles puts out on the screen randomly with color, size and position.
I just want to figure out how to write one of these and what to think of when writing one of these codes. And if possible if
anyone could explain how this code works? =)
The thing is I have tried to compile it but it doesn't work.
Error = "Syntax error on token "else", delete this token
Syntax error on tokens, Catches expected instead"
posted 10 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hi Robin Al-salehy,
Nevertheless, this class is not written fully, as it suppose to extend superclass.
In order to make it "work" as you say, you need to create an instance of this class, somewhere in the other tester class, which contains "main" method. Something like:The thing is I have tried to compile it but it doesn't work.
Nevertheless, this class is not written fully, as it suppose to extend superclass.
Robin Al-salehy
Greenhorn
Posts: 19
posted 10 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
You are right sir! =)
I have made another one.
What should I search on Google to read more about how to make the program: Outputs Random rectangles and circles with random size, color and position?
I have made another one.
What should I search on Google to read more about how to make the program: Outputs Random rectangles and circles with random size, color and position?
posted 10 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Please don't double‑space all your code. Blank lines where required please. I shall remove some excess spaces.
Robin Al-salehy
Greenhorn
Posts: 19
posted 10 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I apologize, I write the code like that so I can clearly see what's going on.
But does anybody have an answer on how to make the size of the rectangle and circles different?
But does anybody have an answer on how to make the size of the rectangle and circles different?
Campbell Ritchie
Marshal
Posts: 81618
593
posted 10 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
You have set all the shapes to have 10px height and 10px width. You would have to alter the height and width you are passing to the draw instructions. Lines 13 14 22 and 25. Pass something different from b and h.
Robin Al-salehy
Greenhorn
Posts: 19
posted 10 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I don't understand what you mean there? :S
posted 10 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
In lines 13 and 14 you set values for b and h, and with these two
fixed values you draw all of your 100 rectangles and ovals.
You do not set any color, so yes, you see little to no variance
in witdth, height and color.
As Campbell writes, if you do want to see some variation in
sizes and colors, you need to get some random values for
these as well. For instance:
Another tip: although in this simple case it doesn't really matter,
you should not override 'paint', but 'paintComponent'.
That is because 'paint' does more than simply redrawing
your panel.
Greetz,
Piet
edit: added 'super.paintComponent(g)
fixed values you draw all of your 100 rectangles and ovals.
You do not set any color, so yes, you see little to no variance
in witdth, height and color.
As Campbell writes, if you do want to see some variation in
sizes and colors, you need to get some random values for
these as well. For instance:
Another tip: although in this simple case it doesn't really matter,
you should not override 'paint', but 'paintComponent'.
That is because 'paint' does more than simply redrawing
your panel.
Greetz,
Piet
edit: added 'super.paintComponent(g)
There are three kinds of actuaries: those who can count, and those who can't.
Campbell Ritchie
Marshal
Posts: 81618
593
posted 10 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Thank you for explaining it so much better than I did.
Robin Al-salehy
Greenhorn
Posts: 19
posted 10 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Yeah thanks! =)
Here is the code perfectly done.
I implanted background WHITE and "fill" instead of "draw". In the switch
Here is the code perfectly done.
I implanted background WHITE and "fill" instead of "draw". In the switch
Campbell Ritchie
Marshal
Posts: 81618
593
posted 10 years ago
Add the @Override annotation to the paintComponent method. Give it protected access, not coderanch.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Add the @Override annotation to the paintComponent method. Give it protected access, not coderanch.
| It sure was nice of your sister to lend us her car. Let's show our appreciation by sharing this tiny ad: Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |







