• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trying to understand this code, can someone help?  RSS feed

 
Robin Al-salehy
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • 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"

 
Liutauras Vilda
Marshal
Posts: 3827
200
BSD
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
I don't understand what you mean there? :S
 
Piet Souris
Rancher
Posts: 1783
55
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Add the @Override annotation to the paintComponent method. Give it protected access, not public.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!