• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

Trying to understand this code, can someone help?

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"

 
Sheriff
Posts: 9059
667
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Robin Al-salehy,

The thing is I have tried to compile it but it doesn't work.

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:

Nevertheless, this class is not written fully, as it suppose to extend superclass.
 
Robin Al-salehy
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 81618
593
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Campbell Ritchie
Marshal
Posts: 81618
593
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand what you mean there? :S
 
Bartender
Posts: 5755
217
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Campbell Ritchie
Marshal
Posts: 81618
593
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for explaining it so much better than I did.
 
Robin Al-salehy
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah thanks! =)

Here is the code perfectly done.

I implanted background WHITE and "fill" instead of "draw". In the switch

 
Campbell Ritchie
Marshal
Posts: 81618
593
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
reply
    Bookmark Topic Watch Topic
  • New Topic