Results 1 to 6 of 6
  1. #1
    moreCowbell is offline Member
    Join Date
    Mar 2013
    Posts
    5
    Rep Power
    0

    Default 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

    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);
    		}
    	}
    }
    My output:
    Creating a curve using graphics-myop.jpg

    Desired Output:
    Creating a curve using graphics-graphicbounce.png

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,538
    Rep Power
    5

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    moreCowbell is offline Member
    Join Date
    Mar 2013
    Posts
    5
    Rep Power
    0

    Default 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
    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);
    
    		}
    output:
    Creating a curve using graphics-dp.jpg

    And once again the output that I am looking to achieve:
    (Notice how much space there is between the top asterisks)
    Creating a curve using graphics-graphicbounce.png

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,538
    Rep Power
    5

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Creating a curve using graphics

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    moreCowbell is offline Member
    Join Date
    Mar 2013
    Posts
    5
    Rep Power
    0

    Default Re: Creating a curve using graphics

    Yeah I ended up figuring it out eventually. Thanks again for the help!

Similar Threads

  1. Creating a Mathematical Curve in Java
    By Zie_Mordecai in forum Java 2D
    Replies: 5
    Last Post: 01-16-2013, 09:11 AM
  2. Creating Graphics using Java
    By dashingncool in forum Advanced Java
    Replies: 3
    Last Post: 02-29-2012, 01:17 PM
  3. Freehand curve smoothing using Bezier Curve
    By JavaIsChallenging in forum Java 2D
    Replies: 3
    Last Post: 12-14-2011, 07:56 PM
  4. i am having a problem with creating graphics
    By umpire43055 in forum AWT / Swing
    Replies: 3
    Last Post: 08-07-2009, 05:26 AM
  5. How to Draw Curve with mouse
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-23-2008, 11:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •