Results 1 to 5 of 5
  1. #1
    Snake006 is offline Senior Member
    Join Date
    Jun 2013
    Posts
    129
    Rep Power
    0

    Default 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.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Trouble using a method from a different class

    Ok, I have some basic questions and observations.

    1. Since you don't use if/else constructs consistently (or barely at all) I cannot know if your logic above is actually what
    you want to do or not. Example are lines 1, 2, 8, and 17 above. Other places it may or may not be appropriate to use
    an if/else construct.

    2. You talk about calling clear twice but you don't show the method or the calling of the method.

    3. In your drawStar method, I don't see where you are drawing anything.

    Are you using some other package that does stuff for you or is this pure Java? And since your program
    is too big to post I can only suggest you place lots of of print statements in your program. You can control
    them by doing something like this:

    if (DEBUG) {
    System.out.println(.....);
    }

    Make DEBUG a final static boolean and set to true. When you're done declare it false. Then the conditional blocks won't even be compiled
    into the program (so it won't affect processing).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Snake006 is offline Senior Member
    Join Date
    Jun 2013
    Posts
    129
    Rep Power
    0

    Default Re: Trouble using a method from a different class

    Yes I need to explain about calling clear twice. What I meant to say, is that in my console I type "clear(star)" twice and by doing so runs through this if statement twice
    Java Code:
    if (cmd.equals("clear") && arg.equals("star"))
    ...
    Of course the second time through the counter is zero so it doesn't do anything but prints the message "there are no more objects to deleted". That is way I am confused as to why when I go through this statement again nothing is done:
    Java Code:
    if(cmd.equals("makeStuff") && arg.equals("star))
    The Scounter is 0 again, and it calls drawStar but it doesn't draw it. Of course it is worth noting that my Sandbox class extends something from the ACM library called a Graphics program, which handles drawing for me. Of course the star itself is an object than I made myself in a star class, which is then drawn by the graphics program when I say add(star,....whatever). As I said before if I simply say Star star=new Star(50) and say box.add(.....) in the console program when I go into the makeStuff if statement, then I don't have any problems but I would like to be able to use the drawStar method from the other class in that if statement to reduce the amount of code in the console program.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Trouble using a method from a different class

    Here is what is one thing that is confusing.

    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();
                }
      }
    Let's assume you get to line 2. So starCounter can be at least 1 or more. Let's say it is 1. So you decrement it to make it 0. Then you hit the other if statement at line 8 which is now true since starCounter is now 0. Is that what you meant to do? Also, do you need an else keyword after your closing brace on line 14?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Snake006 is offline Senior Member
    Join Date
    Jun 2013
    Posts
    129
    Rep Power
    0

    Default Re: Trouble using a method from a different class

    Yes, but only once starCounter actually reaches 0. I have to say "clear(star)" twice in order to display the message "There are no more stars to delete" below. In otherwords my screen will have to already have to have no stars on screen in order to get that message to display. Perhaps it would be better that immediately after the first clear, that the message be displayed, so I get the message as I do the last clear. I probably should have an else to handle negative numbers, but I don't think that is the issue. It might be that it is actually drawing it but the positioning needs to be adjusted correctly. Maybe since I can see an object after doing clear twice and then using makeStuff, maybe that is possible b/c the positioning has correct settings then and only then. I may just need to fix the clear method. Any thoughts?

Similar Threads

  1. Trouble with a Map Method
    By javadum in forum New To Java
    Replies: 3
    Last Post: 07-16-2012, 02:51 AM
  2. Replies: 5
    Last Post: 12-18-2011, 04:06 AM
  3. Trouble with calling a method
    By jrJava in forum New To Java
    Replies: 6
    Last Post: 01-17-2011, 09:51 PM
  4. trouble with array method
    By kpro862 in forum New To Java
    Replies: 6
    Last Post: 05-12-2010, 12:08 AM
  5. Trouble with method
    By BlueJ2008 in forum New To Java
    Replies: 2
    Last Post: 10-19-2008, 09:05 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
  •