Results 1 to 6 of 6
Thread: Creating a curve using graphics
- 03-05-2013, 08:30 PM #1
Member
- Join Date
- Mar 2013
- Posts
- 5
- Rep Power
- 0
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?
Thanks for your time in advance
My output:Java 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); } } }

Desired Output:
- 03-05-2013, 09:40 PM #2
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 657
- Rep Power
- 1
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,
JimThe Java™ Tutorial
YAT -- Yet Another Typo
- 03-05-2013, 11:40 PM #3
Member
- Join Date
- Mar 2013
- Posts
- 5
- Rep Power
- 0
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
output:Java 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); }

And once again the output that I am looking to achieve:
(Notice how much space there is between the top asterisks)
- 03-06-2013, 12:18 AM #4
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 657
- Rep Power
- 1
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,
JimThe Java™ Tutorial
YAT -- Yet Another Typo
- 03-06-2013, 05:24 AM #5
Re: Creating a curve using graphics
Moved from New to Java
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 03-13-2013, 12:42 PM #6
Member
- Join Date
- Mar 2013
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Creating a Mathematical Curve in Java
By Zie_Mordecai in forum Java 2DReplies: 5Last Post: 01-16-2013, 09:11 AM -
Creating Graphics using Java
By dashingncool in forum Advanced JavaReplies: 3Last Post: 02-29-2012, 01:17 PM -
Freehand curve smoothing using Bezier Curve
By JavaIsChallenging in forum Java 2DReplies: 3Last Post: 12-14-2011, 07:56 PM -
i am having a problem with creating graphics
By umpire43055 in forum AWT / SwingReplies: 3Last Post: 08-07-2009, 05:26 AM -
How to Draw Curve with mouse
By Java Tip in forum java.awtReplies: 0Last Post: 06-23-2008, 11:20 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks