Win a copy of Secure Financial Transactions with Ansible, Terraform, and OpenSCAP this week in the Cloud/Virtualization forum!
  • 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:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

how to draw the 6 petals of a flower?

 
Ranch Hand
Posts: 197
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


here is a piece of code, after I don't know how to do this project, I looked in the API, however I know that you need a g2d.draw or g.draw to draw
 
Saloon Keeper
Posts: 4358
163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A couple of months ago I drew many colored petals of a flower> golden daisies
 
ronald deux
Ranch Hand
Posts: 197
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


what do you think of this code?
a little help would be welcome, what should be changed or what should be put as a method or other, I do not want any code otherwise I will not move forward
 
Master Rancher
Posts: 4256
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also posted here: https://www.dreamincode.net/forums/topic/419770-how-to-draw-the-6-petals-of-a-flower/
 
Marshal
Posts: 72478
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moved to our GUIs forum.

Have you looked through the methods of Graphics, and the different classes in the java.awt.geom package? You may find something for drawing such curves without having to mess about with loops.
Don't divide by π; use the methods of the Math class for converting degrees and radians.
The fact that you have to convert ints to doubles and vice versa is a problem with Swing® which you mightn't suffer with FX.
 
Piet Souris
Saloon Keeper
Posts: 4358
163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some remarks:

Math.cos (and sin and tan) works with radians. So, line 25 and 26 might not do what you expect it to do.

Line 26 limits the values of r to the interval [-1, 1], and so, newX and newY are also limited to that interval. That mens that your drawings appear in the upper left corner of your panel, hardly to see.

In the code I pointed to yesterday, I did two things to get the effect: first, I set the origin of the panel to the midpoint of it, and second I draw the same ellips over and over, but between two ellipses I rotate a little.

If I do that here, and use ellipses for simplicity, then this is what I got:

and

Admitted, it looks not like a rose, but it is a start.

Your idea is certainly interesting. Letting the angle go from 0 to PI, and setting r to say 200 * Math.sin(angle), and  calculating x and y as if r and angle were polar coordinates. Such techniques give a lot of modelling possibilities. Another idea is to work with Path2D. Using Bezier curves might give nice leaves too. But to get something that looks like a rose, hmmm. Maybe the guys at DreamInCode have some ideas.
 
ronald deux
Ranch Hand
Posts: 197
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I almost succeeded, on the other hand a straight line which goes down diagonally,
 
Piet Souris
Saloon Keeper
Posts: 4358
163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very nice indeed!

To get rid of that line, just issue
 
ronald deux
Ranch Hand
Posts: 197
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found
thank you
 
Norm Radder
Master Rancher
Posts: 4256
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another idea for the project - using a timer call drawline every x milliseconds to draw the next segment of the drawing so that you can see the pedals being drawn as it happens instead of drawing the lines all at once.
 
Piet Souris
Saloon Keeper
Posts: 4358
163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just put:

right before the g.drawLine
 
Campbell Ritchie
Marshal
Posts: 72478
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would you want to sleep that Thread, Piet?
 
Piet Souris
Saloon Keeper
Posts: 4358
163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Never do: Thread.sleep on the EDT. This case is the exception that confirms the rule.

Norm wanted to see the drawing happening, and this is a very easy way to get that done. The drawing is just a one-off thing (the frame is not resizable), and so it is justified. Well, my opinion, that is.

But I guess the whole thing still is mysterious, even if you see it happening right before your eyes. Then use a spreadsheet, use many values for the angle from 0 to 2*PI, set a column of corresponding r-values, and make a graph of the angles with the r's. Another idea is: find your Spirograph box, and see if you can reproduce a similar figure.
 
ronald deux
Ranch Hand
Posts: 197
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I will try, I know the spirograph
I just tried your code
it works
 
Piet Souris
Saloon Keeper
Posts: 4358
163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My code to slow down the drawing worked, becauce that code was not running on the EDT, Well, never mind.

Have you found out about getting 6 petals, as your qriginal question was? In your working code, you have that

So, when angle = 0, r = 300, and when angle = 15 degrees, r = 0, since cos(6 * 15 degrees) = 0;

Tha means that in steps of 15 degrees, we get half a petal, and therefore in steps of 30 degrees we get a full petal. There are 12 steps of 30 degrees in 360 degrees, and that is why you see 12 petals. Can you find out how to get 6 petals?

This method (r = 300 * cos(factor * angle) works nicely, as long as factor is a divisor of 90. It is not easy to get this method to deliver a random number of petals.

I mentioned the possible use of a Path2D object, that represents a leave. You can have without effort any number of petals and it is quite fast. I made a demo. The leave here is a Path2D that is made of two slightly different quadratic Bezier curves. Well, enjoy.
 
ronald deux
Ranch Hand
Posts: 197
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no, I must have asked the question too quickly,
I thought it would give a 6 petal rose
otherwise thank you for the code
 
You'll never get away with this you overconfident blob! The most you will ever get is this tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic