Results 1 to 8 of 8
Thread: Offsetting X and Y coordinates
- 02-24-2012, 07:52 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 4
- Rep Power
- 0
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
- 02-24-2012, 08:34 PM #2
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.
- 02-24-2012, 08:44 PM #3
Member
- Join Date
- Feb 2012
- Posts
- 4
- Rep Power
- 0
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
- 02-24-2012, 08:48 PM #4
Member
- Join Date
- Feb 2012
- Posts
- 4
- Rep Power
- 0
Re: Offsetting X and Y coordinates
oh that website is not gonna help wahst a good website to lok at for that
- 02-24-2012, 08:49 PM #5
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.
- 02-24-2012, 08:52 PM #6
Member
- Join Date
- Feb 2012
- Posts
- 4
- Rep Power
- 0
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?
- 02-24-2012, 08:56 PM #7
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
- 02-25-2012, 02:53 PM #8
Similar Threads
-
Help Getting Image Coordinates
By Witik in forum New To JavaReplies: 2Last Post: 09-06-2010, 01:02 PM -
Coordinates
By Witik in forum New To JavaReplies: 3Last Post: 09-06-2010, 07:05 AM -
coordinates and radius
By tepong in forum New To JavaReplies: 1Last Post: 08-21-2010, 06:38 PM -
Help with plotting x-y coordinates
By Fidelcashflow in forum New To JavaReplies: 3Last Post: 12-02-2009, 11:17 AM -
Getting mouse coordinates
By nishant.4545 in forum Advanced JavaReplies: 3Last Post: 07-20-2009, 11:28 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks