Friday, October 24, 2014

Trouble using a method from a different class
































In order to explain my problem I will first state what I am trying to do. I have a program that reads some text, and draws or deletes objects on a JFrame according to what I say. My program works fine for the most part. So whenever I say something like makeStuff(star) in my console, a star is drawn on screen. I kept track of types of objects in an ArrayList, and add and remove certain objects depending on the type of text I input. My problem is when I get to the point when there are no more objects of the same type on screen, meaning I keep clearing objects of the same type until there are no more, when I go to draw another object of the same type, it does not draw the object. Keep in mind that, I have two classes, one for my console and one for graphical objects to be drawn. Strangely enough if I call clear twice then I can draw which doesn't make a lot of sense. When going through the debugging everything is working right, including the counters I made, and the amount of objects of the same type have the right amount when they are supposed to. I did find a work around by simply making the object in the console program class instead of calling the draw method from the sandbox class but my console class is probably a 1000 lines of code and is far too big already. I just want to use the draw method from the other class. Here are some revelant lines of code as to what I am trying to do:
































Console Class
















































Java Code:





































if (cmd.equals("clear") && arg.equals("star")) { //use a boolean for when this part is done
if (starCounter > 0) {
box.undoStar();
starCounter -= 1;
starNum -= box.getWidth() / 6;
freeCommand();
}
if (starCounter == 0) {
multSCounter = 0;
println("There are no more stars to delete");
starNum = 30;
freeCommand();
}

}

if (cmd.equals("makeStuff") && arg.equals("star")) {


if (multSCounter > 0) {
box.deleteStars();
box.drawStar(starNum, 50);
freeCommand();
}

if(starCounter==0){
box.add(star,50,100);
box.historyG.add(star);
//box.drawStar(starNum, 50);
starCounter++;
freeCommand();
//sCleared=false;
}
else if (starCounter > 0 && multSCounter == 0) {
println("You can only make one object at a time using this type of command");
freeCommand();
}



Sandbox class

public void drawStar(int xpos, int ypos){

star=new Star(50);
star.setFillColor(Color.yellow);
star.setFilled(true);
historyG.add(star);
add(star,xpos,ypos);

}

public void undoStar(){
if (historyG.size() > 0) {
GObject LastGObject = historyG.get(historyG.size() - 1);
historyG.remove(LastGObject);
this.remove(LastGObject);
}

if(isSMoved==true){
GObject LastGObject = historyG.get(historyG.size() - 1);
this.remove(LastGObject);
isSMoved=false;
}
}



































I really can't figure this one out and won't anytime soon. My only thought is that something is lost in translation from class to class.














































No comments:

Post a Comment