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
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);
}
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.
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
Re: Offsetting X and Y coordinates
oh that website is not gonna help wahst a good website to lok at for that
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.
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?
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
Re: Offsetting X and Y coordinates
Moved from New to Java
db