Results 1 to 8 of 8
  1. #1
    Alois is offline Member
    Join Date
    Feb 2012
    Posts
    4
    Rep Power
    0

    Default Offsetting X and Y coordinates

    Okay so I am trying to draw trees in java, and i have the stump and treetop as seperate methods using the same random generator for x and y, i need to somehow move the trunk over about 20 and down about 50 so that it generates at the bottom of trees rather than on the left side. Here is the code i have
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    public class TreeTest extends JApplet
    {
       int  TREE_WIDTH = 45;
       int  TREE_HEIGHT = 50;
       int STUMP_WIDTH = 10;
       int STUMP_HEIGHT = 100;
       int x = ((int)(Math.random()*255));
       int y = ((int)(Math.random()*255));
    
    public void paint (Graphics g )
       {
    		super.paint( g );
    		paintTree( g );//method code
    		paintTreeStump (g);
    	}
    
    public void paintTree (Graphics grph)
    	{
    	Color treeColor;
    	treeColor= new Color ((int)(Math.random()*0),
    			 (int)(Math.random()*255),
    			(int)(Math.random()*0));
    	
                paintTreeTop( grph, x,y, treeColor);
    
    	}
    public void paintTreeStump (Graphics grph)
    	{
    		Color stumpColor;
    		stumpColor= (new Color(139,69, 19));
    
    		paintTreeTrunk (grph, x, y, stumpColor);
    	}
     //method declaration
    public void paintTreeTop (Graphics g, int x, int y, Color treeColor)
       {
       		g.setColor(treeColor);
       		g.fillOval(x, y, TREE_WIDTH,
       				TREE_HEIGHT);
       }
    public void paintTreeTrunk(
    				Graphics g, int X, int y, Color stumpColor)
    	{
    		g.setColor(stumpColor);
    		g.fillRect(x, y, STUMP_WIDTH,
    				STUMP_HEIGHT);
    
    	}
    Last edited by Norm; 02-24-2012 at 08:34 PM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Offsetting X and Y coordinates

    Can you explain what you've tried to move the drawing to where you want it to be?
    It can be a trial and error procedure to get the drawing where you want it to be.

    One useful technique is to take a piece of grid paper and make the drawing the way you want it and then note the x,y locations of where each drawing needs to go.
    Last edited by Norm; 02-24-2012 at 08:38 PM.

  3. #3
    Alois is offline Member
    Join Date
    Feb 2012
    Posts
    4
    Rep Power
    0

    Default Re: Offsetting X and Y coordinates

    i got it now i just needed to edit x and y in a different method, im trying to run a loop to draw the trees now using this Programming via Java: Repetition, but i have no clue about how to implement it i want it to start with 0 trees and keep drawing them until it reaches 20

  4. #4
    Alois is offline Member
    Join Date
    Feb 2012
    Posts
    4
    Rep Power
    0

    Default Re: Offsetting X and Y coordinates

    oh that website is not gonna help wahst a good website to lok at for that

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Offsetting X and Y coordinates

    Make a method that creates the x,y locations for a tree and saves them somewhere in a list.
    Have the paint method go through that list of locations for the trees and draw them one by one as it gets each tree's location from the list of tree locations.

  6. #6
    Alois is offline Member
    Join Date
    Feb 2012
    Posts
    4
    Rep Power
    0

    Default Re: Offsetting X and Y coordinates

    But the list would have to random itself though and change each time. I am very new to java, so I haven't dealt with lists before, what is a good resource to how to set it up structurally?

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Offsetting X and Y coordinates

    For the first part of changing your program, use a class like Point to hold the x,y locations for a single tree. Create an object to hold the x,y values and use that in the paint method.
    When that works, look at using an array to hold the 20 different Point objects for each tree.

    The Java tutorials are one place to read:
    The Really Big Index

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

    Default Re: Offsetting X and Y coordinates

    Moved from New to Java

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

Similar Threads

  1. Help Getting Image Coordinates
    By Witik in forum New To Java
    Replies: 2
    Last Post: 09-06-2010, 01:02 PM
  2. Coordinates
    By Witik in forum New To Java
    Replies: 3
    Last Post: 09-06-2010, 07:05 AM
  3. coordinates and radius
    By tepong in forum New To Java
    Replies: 1
    Last Post: 08-21-2010, 06:38 PM
  4. Help with plotting x-y coordinates
    By Fidelcashflow in forum New To Java
    Replies: 3
    Last Post: 12-02-2009, 11:17 AM
  5. Getting mouse coordinates
    By nishant.4545 in forum Advanced Java
    Replies: 3
    Last Post: 07-20-2009, 11:28 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
  •