I need to write a program that can draw stuff on a frame, let's say, trees.
Their IDs, colors, positions etc. are in a txt file, line by line, the first word of the line is the name of their type which will be the name of the subclass.
These parameters are given by the "Trees" class. The drawing method of the various types of trees are written in subclasses.
So I thought I should make a class for getting the data from the text file and then pass the scanner as a parameter to the Trees class' constructor, where the variables are specified.
The problem is, when I'm trying to make the subclasses, i either cant use the variables from the Trees class, or don't know how to use super() with a scanner.
Also, am I supposed to put each one of them in a Map like Map<ID, Trees> after the reading or put them in a Map in the Trees class? How am i supposed to draw each one of them on the background?
Maybe this task is not as complicated as I think it is, so feel free to tell me if you have any better ideas on how i should solve it.
I thought I should make a Trees class like this:
public class Trees {
public Trees() {} // Probably this is why I can't use the variables in the subclass
public Trees(Scanner scan) {
ID = scan.nextInt();
color = scan.next();
// Should I put them in a Map here?
}
public String color;
public int ID;
}
class appleTree extends Trees {
// an apple tree's own paint() method
}
And then my other file looks like this:
public class getData {
getData(String file)
{
Scanner scan = new Scanner(new File(file));
while(scan.hasNext())
{
Trees trees = new Trees(scan);
// read from file here
}
}
// make JFrame
}
class Drawing extends JFrame {
// This one has a paint() method that draws the background, how should I draw the trees here?
// I can see that if I say appleTree a = new appleTree(); a.paint(g); it draws perfectly whatever I did in the appleTree class' paint() method,
// but that's just a general drawing method for any apple tree, how do I draw that exact apple tree i specified in my txt file?
}