# Creating a curve using graphics

• 03-05-2013, 09:30 PM
moreCowbell
Creating a curve using graphics
Hello,

My current assignment is to write a program using graphics that will display asterisks that form a gradual curve, and get closer to each other as they reach the bottom of the drawing panel. The x value is not the issue- I want it to increase by 10 pixels which I have done.
The y value is what I am having trouble with. It should start at the 25 pixel and end at the 425 pixel. The curve represents the bounce height of a ball after it is dropped from 10 meters, with y being the height and x being the number of bounces (20). The bounce height will decrease by 20% for every bounce. So after releasing from 10 meters it will bounce 8 meters high, then 6.4 meters, and so on until it bounces 20 time. The code I have below is the closest I have been able to get.
If someone could just show me how to get my asterisks to line up the correct way I can play with the numbers and get them to fall on the correct pixels.
Can anyone out there offer some pointers?

Code:

```import java.awt.*; public class partA {         public static void main(String[] args) {                 DrawingPanel panel = new DrawingPanel(300, 500);                                 Graphics g = panel.getGraphics();                         for(int i=1; i<=20; i++){                                 g.drawString("*", i*10,i*i+25);                 }         } }```
My output:
Attachment 4603

Desired Output:
Attachment 4604
• 03-05-2013, 10:40 PM
jim829
Re: Creating a curve using graphics
You need to "reverse" the coordinates in your drawString method. Subtract the coordinates from their maximum values and use those values as the x, y coordinates.

Also, it is not recommend to call drawString yourself. The preferred method is to override paint(Graphics g) in your panel and put your drawing code in there.

Regards,
Jim
• 03-06-2013, 12:40 AM
moreCowbell
Re: Creating a curve using graphics
Hey Jim, thanks for help!
I never thought about doing it this way. I am now a lot closer to what the project asks for but I am still struggling with getting my asterisks to align like they're supposed to.
I have tried many different formulas for the y value but I still cannot get the correct output.

Right now this is as close as I have got using the follow code
Code:

```public static void main(String[] args) {                 DrawingPanel panel = new DrawingPanel(300, 500);                                 Graphics g = panel.getGraphics();                                 for(int i=1; i<=20; i++){                                 g.drawString("*", 10+200-i*10,430-i*i);                 }```
output:
Attachment 4606

And once again the output that I am looking to achieve:
(Notice how much space there is between the top asterisks)
Attachment 4607
• 03-06-2013, 01:18 AM
jim829
Re: Creating a curve using graphics
I wouldn't think it would matter because it's just a matter of scale. As long as you print the appropriate value against the curve, it shouldn't matter how far apart the asterisks would be.

Having said that, your asterisk spacing is determine by your coordinates. So if you push your coordinates together a little more (i.e plot more of them in the same space), then the asterisks would be closer together. So you can still count from 1 to 20 but change the increment amount. And don't forget to convert doubles to ints for the drawString() method.

Regards,
Jim
• 03-06-2013, 06:24 AM
DarrylBurke
Re: Creating a curve using graphics
Moved from New to Java

db
• 03-13-2013, 01:42 PM
moreCowbell
Re: Creating a curve using graphics
Yeah I ended up figuring it out eventually. Thanks again for the help!