Search code examples
javabluejturtle-graphics

How to Display Methods from a one Class to the Main Method of Another Class


I am working on an animation for my computer science class with Java (I am using BlueJ). The problem I am having right now is that I am unable to display methods from my first class into the main method of my second class. This is the code that I am working on. It is not the most orderly code, but this is what I have so far.

/**
* Write a description of class Animation here.
* 
* @author 
* @version 
*/

import java.util.Scanner;
import java.awt.*;

class TurtleGraphics
{
    //set conditions for turtle to start drawing
    public void prepareTurtleToDraw(Turtle myrtle, Color color, int x, int y)
   {
       myrtle.hide();
       myrtle.penUp();               //pick up the pen to avoid leaving a trail when moving the turtle
       myrtle.setColor(color);       //set myrtle's color
       myrtle.moveTo(x, y);          //move to coordinates
       myrtle.penDown();             //put the pen down to start drawing
    }//end of prepareTurtleToDraw method

    //draw a line
    public void drawLine(Turtle myrtle, int x1, int y1, int x2, int y2)//, int width)
    {
        myrtle.moveTo(x1, y1);      //moves to this coordinate first
        myrtle.moveTo(x2, y2);      //then moves to this coordinate
        //myrtle.setPenWidth(width);  //this adjusts the size of the lines
    }//end of drawLine method   
}
class AnimationGraphics
{       
  public static void pressC()
{
    //Animation animate = new Animation();

    World worldObj = new World();
    Animation turt = new Animation();

    Turtle surtle = new Turtle(-50, 120, worldObj);

    turt.prepareTurtleToDraw(surtle, Color.RED, -50, 120);
    turt.drawLine(surtle, -50, 120, 60, 75);
    turt.drawLine(surtle, 60, 75, 135, 150);
    turt.drawLine(surtle, 135, 150, 25, 225);
    turt.drawLine(surtle, 25, 225, -50, 120);  

    System.out.println(worldObj); 
    String userInput = "";                                  //declare and initialize a String variable
    char key = ' ';                                         //declare and initialize a char variable
    Scanner in = new Scanner(System.in);                    //construct a Scanner object

    System.out.println("Please press the c key multiple times to watch the animation.");

    do  
    {
       userInput = in.next();                                  //accept one token from the keyboard
       in.nextLine();                                          //flush the buffer
       key = userInput.charAt(0);                              //picks off the first character from the userInput String variable         

       Turtle kurtle = new Turtle(-50, 120, worldObj); 

       turt.prepareTurtleToDraw(kurtle, Color.WHITE, -50, 120);
       turt.drawLine(kurtle, -50, 120, 60, 75);
       turt.drawLine(kurtle, 60, 75, 135, 150);
       turt.drawLine(kurtle, 135, 150, 25, 225);
       turt.drawLine(kurtle, 25, 225, -50, 120);  


       Turtle turtle = new Turtle(100, 150, worldObj); 

       turt.prepareTurtleToDraw(turtle, Color.BLACK, 100, 150);
       turt.drawLine(turtle, 50, 150, 200, 150);
       turt.drawLine(turtle, 200, 150, 200, 250);
       turt.drawLine(turtle, 200, 250, 50, 250);
       turt.drawLine(turtle, 50, 250, 50, 150);
       }
       while(key != 'c');                                      //do-while condition statement 


    do  
    {
       userInput = in.next();                                  //accept one token from the keyboard
       in.nextLine();                                          //flush the buffer
       key = userInput.charAt(0);                              //picks off the first character from the userInput String variable         


       Turtle burtle = new Turtle(100, 150, worldObj); 

       turt.prepareTurtleToDraw(burtle, Color.WHITE, 100, 150);
       turt.drawLine(burtle, 50, 150, 200, 150);
       turt.drawLine(burtle, 200, 150, 200, 250);
       turt.drawLine(burtle, 200, 250, 50, 250);
       turt.drawLine(burtle, 50, 250, 50, 150);

       Turtle dyrtle = new Turtle(150, 150, worldObj); 

       turt.prepareTurtleToDraw(dyrtle, Color.RED, 150, 150);
       turt.drawLine(dyrtle, 150, 150, 260, 75);
       turt.drawLine(dyrtle, 260, 75, 335, 150);
       turt.drawLine(dyrtle, 335, 150, 225, 225);
       turt.drawLine(dyrtle, 225, 225, 150, 150);  

       }
       while(key != 'c');                                      //do-while condition statement  



    //do-while loop to wait for the user to enter the letter c
    do  
    {
       userInput = in.next();                                  //accept one token from the keyboard
       in.nextLine();                                          //flush the buffer
       key = userInput.charAt(0);                              //picks off the first character from the userInput String variable         


       Turtle vyrtle = new Turtle(150, 150, worldObj); 

       turt.prepareTurtleToDraw(vyrtle, Color.WHITE, 150, 150);
       turt.drawLine(vyrtle, 150, 150, 260, 75);
       turt.drawLine(vyrtle, 260, 75, 335, 150);
       turt.drawLine(vyrtle, 335, 150, 225, 225);
       turt.drawLine(vyrtle, 225, 225, 150, 150); 

       Turtle mertle = new Turtle(300, 150, worldObj);    

       turt.prepareTurtleToDraw(mertle, Color.BLACK, 250, 150);
       turt.drawLine(mertle, 250, 150, 400, 150);
       turt.drawLine(mertle, 400, 150, 400, 250);
       turt.drawLine(mertle, 400, 250, 250, 250);
       turt.drawLine(mertle, 250, 250, 250, 150);



       }
       while(key != 'c');                                      //do-while condition statement 

    do  
    {
       userInput = in.next();                                  //accept one token from the keyboard
       in.nextLine();                                          //flush the buffer
       key = userInput.charAt(0);                              //picks off the first character from the userInput String variable


       Turtle dertle = new Turtle(250, 150, worldObj);

       turt.prepareTurtleToDraw(dertle, Color.WHITE, 250, 150);
       turt.drawLine(dertle, 250, 150, 400, 150);
       turt.drawLine(dertle, 400, 150, 400, 250);
       turt.drawLine(dertle, 400, 250, 250, 250);
       turt.drawLine(dertle, 250, 250, 250, 150);

       Turtle qyrtle = new Turtle(150, 150, worldObj); 

       turt.prepareTurtleToDraw(qyrtle, Color.RED, 325, 150);
       turt.drawLine(qyrtle, 325, 150, 435, 75);
       turt.drawLine(qyrtle, 435, 75, 510, 150);
       turt.drawLine(qyrtle, 510, 150, 400, 225);
       turt.drawLine(qyrtle, 400, 225, 325, 150);


    }
    while(key != 'c');                                      //do-while condition statement 

    do  
    {
       userInput = in.next();                                  //accept one token from the keyboard
       in.nextLine();                                          //flush the buffer
       key = userInput.charAt(0);                              //picks off the first character from the userInput String variable

        Turtle myrtle = new Turtle(325, 150, worldObj); 

       turt.prepareTurtleToDraw(myrtle, Color.WHITE, 325, 150);
       turt.drawLine(myrtle, 325, 150, 435, 75);
       turt.drawLine(myrtle, 435, 75, 510, 150);
       turt.drawLine(myrtle, 510, 150, 400, 225);
       turt.drawLine(myrtle, 400, 225, 325, 150);

       Turtle bertle = new Turtle(500, 150, worldObj);

       turt.prepareTurtleToDraw(bertle, Color.BLACK, 500, 150);
       turt.drawLine(bertle, 425, 150, 575, 150);
       turt.drawLine(bertle, 575, 150, 575, 250);
       turt.drawLine(bertle, 575, 250, 425, 250);
       turt.drawLine(bertle, 425, 250, 425, 150);


    }
    while(key != 'c');                                      //do-while condition statement 

    do  
    {
       userInput = in.next();                                  //accept one token from the keyboard
       in.nextLine();                                          //flush the buffer
       key = userInput.charAt(0);                              //picks off the first character from the userInput String variable             


       Turtle mertle = new Turtle(500, 150, worldObj);

       turt.prepareTurtleToDraw(mertle, Color.WHITE, 500, 150);
       turt.drawLine(mertle, 425, 150, 575, 150);
       turt.drawLine(mertle, 575, 150, 575, 250);
       turt.drawLine(mertle, 575, 250, 425, 250);
       turt.drawLine(mertle, 425, 250, 425, 150);

    }
    while(key != 'c');                                      //do-while condition statement 
    System.out.println(worldObj); 
}//end of main method
}  

public class AnimationTester1
{
public static void main(String[] args)
{        
   AnimationGraphics animate = new AnimationGraphics();

   //Animation.main();
   //public static void Animation();

   //System.out.println(press);

}    
}  

This is the version that works, but it is not the most orderly way. For this one, all of the code is in the main method which is not the best format which I am trying to change.

/**
* Write a description of class Animation here.
* 
* @author 
* @version 
*/

import java.util.Scanner;
import java.awt.*;
class Animation
{      
//set conditions for turtle to start drawing
public void prepareTurtleToDraw(Turtle myrtle, Color color, int x, int y)
{
   myrtle.hide();
   myrtle.penUp();               //pick up the pen to avoid leaving a trail when moving the turtle
   myrtle.setColor(color);       //set myrtle's color
   myrtle.moveTo(x, y);          //move to coordinates
   myrtle.penDown();             //put the pen down to start drawing
}//end of prepareTurtleToDraw method

//draw a line
public void drawLine(Turtle myrtle, int x1, int y1, int x2, int y2)//, int width)
{
    myrtle.moveTo(x1, y1);      //moves to this coordinate first
    myrtle.moveTo(x2, y2);      //then moves to this coordinate
    //myrtle.setPenWidth(width);  //this adjusts the size of the lines
}//end of drawLine method   

public static void animation()
{
  World worldObj = new World();
  Turtle lertle = new Turtle(300, 150, worldObj);             //create a Turtle object to do the drawing
  Animation turt = new Animation();

  Turtle dyrtle = new Turtle(150, 150, worldObj);    

  turt.prepareTurtleToDraw(lertle, Color.BLACK, 250, 150);
  turt.drawLine(lertle, 250, 150, 400, 150);
  turt.drawLine(lertle, 400, 150, 400, 250);
  turt.drawLine(lertle, 400, 250, 250, 250);
  turt.drawLine(lertle, 250, 250, 250, 150);

  turt.prepareTurtleToDraw(dyrtle, Color.RED, 150, 150);
  turt.drawLine(dyrtle, 150, 150, 260, 75);
  turt.drawLine(dyrtle, 260, 75, 335, 150);
  turt.drawLine(dyrtle, 335, 150, 225, 225);
  turt.drawLine(dyrtle, 225, 225, 150, 150);

  System.out.println(worldObj); 
}

public static void pressC()
{
   String userInput = "";                                  //declare and initialize a String variable
   char key = ' ';                                         //declare and initialize a char variable
   Scanner in = new Scanner(System.in);                    //construct a Scanner object

   System.out.println("Please press the c key to watch the animation.");

   //do-while loop to wait for the user to enter the letter c
   do  
    {
        userInput = in.next();                                  //accept one token from the keyboard
        in.nextLine();                                          //flush the buffer
        key = userInput.charAt(0);                              //picks off the first character from the userInput String variable
        World worldObj = new World();
        Animation turt = new Animation();


        Turtle dertle = new Turtle(250, 150, worldObj);
        Turtle vyrtle = new Turtle(150, 150, worldObj); 

        turt.prepareTurtleToDraw(dertle, Color.WHITE, 250, 150);
        turt.drawLine(dertle, 250, 150, 400, 150);
        turt.drawLine(dertle, 400, 150, 400, 250);
        turt.drawLine(dertle, 400, 250, 250, 250);
        turt.drawLine(dertle, 250, 250, 250, 150);

        turt.prepareTurtleToDraw(vyrtle, Color.WHITE, 150, 150);
        turt.drawLine(vyrtle, 150, 150, 260, 75);
        turt.drawLine(vyrtle, 260, 75, 335, 150);
        turt.drawLine(vyrtle, 335, 150, 225, 225);
        turt.drawLine(vyrtle, 225, 225, 150, 150);
    }
    while(key != 'c');                                      //do-while condition statement 

   System.out.println("Thank you. You may continue");

}//end of main method

}  

public class AnimationTester
{
public static void main(String[] args)
{        
    Animation animate = new Animation();

    World worldObj = new World();
    Animation turt = new Animation();

    Turtle surtle = new Turtle(-50, 120, worldObj);

    turt.prepareTurtleToDraw(surtle, Color.RED, -50, 120);
    turt.drawLine(surtle, -50, 120, 60, 75);
    turt.drawLine(surtle, 60, 75, 135, 150);
    turt.drawLine(surtle, 135, 150, 25, 225);
    turt.drawLine(surtle, 25, 225, -50, 120);  

    System.out.println(worldObj); 
    String userInput = "";                                  //declare and initialize a String variable
    char key = ' ';                                         //declare and initialize a char variable
    Scanner in = new Scanner(System.in);                    //construct a Scanner object

    System.out.println("Please press the c key multiple times to watch the animation.");

    do  
    {
       userInput = in.next();                                  //accept one token from the keyboard
       in.nextLine();                                          //flush the buffer
       key = userInput.charAt(0);                              //picks off the first character from the userInput String variable         

       Turtle kurtle = new Turtle(-50, 120, worldObj); 

       turt.prepareTurtleToDraw(kurtle, Color.WHITE, -50, 120);
       turt.drawLine(kurtle, -50, 120, 60, 75);
       turt.drawLine(kurtle, 60, 75, 135, 150);
       turt.drawLine(kurtle, 135, 150, 25, 225);
       turt.drawLine(kurtle, 25, 225, -50, 120);  


       Turtle turtle = new Turtle(100, 150, worldObj); 

       turt.prepareTurtleToDraw(turtle, Color.BLACK, 100, 150);
       turt.drawLine(turtle, 50, 150, 200, 150);
       turt.drawLine(turtle, 200, 150, 200, 250);
       turt.drawLine(turtle, 200, 250, 50, 250);
       turt.drawLine(turtle, 50, 250, 50, 150);
       }
       while(key != 'c');                                      //do-while condition statement 


    do  
    {
       userInput = in.next();                                  //accept one token from the keyboard
       in.nextLine();                                          //flush the buffer
       key = userInput.charAt(0);                              //picks off the first character from the userInput String variable         


       Turtle burtle = new Turtle(100, 150, worldObj); 

       turt.prepareTurtleToDraw(burtle, Color.WHITE, 100, 150);
       turt.drawLine(burtle, 50, 150, 200, 150);
       turt.drawLine(burtle, 200, 150, 200, 250);
       turt.drawLine(burtle, 200, 250, 50, 250);
       turt.drawLine(burtle, 50, 250, 50, 150);

       Turtle dyrtle = new Turtle(150, 150, worldObj); 

       turt.prepareTurtleToDraw(dyrtle, Color.RED, 150, 150);
       turt.drawLine(dyrtle, 150, 150, 260, 75);
       turt.drawLine(dyrtle, 260, 75, 335, 150);
       turt.drawLine(dyrtle, 335, 150, 225, 225);
       turt.drawLine(dyrtle, 225, 225, 150, 150);  

       }
       while(key != 'c');                                      //do-while condition statement  



    //do-while loop to wait for the user to enter the letter c
    do  
    {
       userInput = in.next();                                  //accept one token from the keyboard
       in.nextLine();                                          //flush the buffer
       key = userInput.charAt(0);                              //picks off the first character from the userInput String variable         


       Turtle vyrtle = new Turtle(150, 150, worldObj); 

       turt.prepareTurtleToDraw(vyrtle, Color.WHITE, 150, 150);
       turt.drawLine(vyrtle, 150, 150, 260, 75);
       turt.drawLine(vyrtle, 260, 75, 335, 150);
       turt.drawLine(vyrtle, 335, 150, 225, 225);
       turt.drawLine(vyrtle, 225, 225, 150, 150); 

       Turtle mertle = new Turtle(300, 150, worldObj);    

       turt.prepareTurtleToDraw(mertle, Color.BLACK, 250, 150);
       turt.drawLine(mertle, 250, 150, 400, 150);
       turt.drawLine(mertle, 400, 150, 400, 250);
       turt.drawLine(mertle, 400, 250, 250, 250);
       turt.drawLine(mertle, 250, 250, 250, 150);



       }
       while(key != 'c');                                      //do-while condition statement 

    do  
    {
       userInput = in.next();                                  //accept one token from the keyboard
       in.nextLine();                                          //flush the buffer
       key = userInput.charAt(0);                              //picks off the first character from the userInput String variable


       Turtle dertle = new Turtle(250, 150, worldObj);

       turt.prepareTurtleToDraw(dertle, Color.WHITE, 250, 150);
       turt.drawLine(dertle, 250, 150, 400, 150);
       turt.drawLine(dertle, 400, 150, 400, 250);
       turt.drawLine(dertle, 400, 250, 250, 250);
       turt.drawLine(dertle, 250, 250, 250, 150);

       Turtle qyrtle = new Turtle(150, 150, worldObj); 

       turt.prepareTurtleToDraw(qyrtle, Color.RED, 325, 150);
       turt.drawLine(qyrtle, 325, 150, 435, 75);
       turt.drawLine(qyrtle, 435, 75, 510, 150);
       turt.drawLine(qyrtle, 510, 150, 400, 225);
       turt.drawLine(qyrtle, 400, 225, 325, 150);


    }
    while(key != 'c');                                      //do-while condition statement 

    do  
    {
       userInput = in.next();                                  //accept one token from the keyboard
       in.nextLine();                                          //flush the buffer
       key = userInput.charAt(0);                              //picks off the first character from the userInput String variable

        Turtle myrtle = new Turtle(325, 150, worldObj); 

       turt.prepareTurtleToDraw(myrtle, Color.WHITE, 325, 150);
       turt.drawLine(myrtle, 325, 150, 435, 75);
       turt.drawLine(myrtle, 435, 75, 510, 150);
       turt.drawLine(myrtle, 510, 150, 400, 225);
       turt.drawLine(myrtle, 400, 225, 325, 150);

       Turtle bertle = new Turtle(500, 150, worldObj);

       turt.prepareTurtleToDraw(bertle, Color.BLACK, 500, 150);
       turt.drawLine(bertle, 425, 150, 575, 150);
       turt.drawLine(bertle, 575, 150, 575, 250);
       turt.drawLine(bertle, 575, 250, 425, 250);
       turt.drawLine(bertle, 425, 250, 425, 150);


    }
    while(key != 'c');                                      //do-while condition statement 

    do  
    {
       userInput = in.next();                                  //accept one token from the keyboard
       in.nextLine();                                          //flush the buffer
       key = userInput.charAt(0);                              //picks off the first character from the userInput String variable             


       Turtle mertle = new Turtle(500, 150, worldObj);

       turt.prepareTurtleToDraw(mertle, Color.WHITE, 500, 150);
       turt.drawLine(mertle, 425, 150, 575, 150);
       turt.drawLine(mertle, 575, 150, 575, 250);
       turt.drawLine(mertle, 575, 250, 425, 250);
       turt.drawLine(mertle, 425, 250, 425, 150);

    }
    while(key != 'c');                                      //do-while condition statement 
    System.out.println(worldObj); 
}    
}  

Solution

  • A java class is only a blueprint of an object. So you must create the object or instantiate the class first. Instantiating an object is done by calling the class name, assigning it a variable, and then have it assigned to the objects constructor.

    Animation animation = new Animation();
    

    After you create an object you can use the "." or dot operator to call the methods created in the class file.

    animation.drawLine(myrtle, x1, y1, x2, y2);
    animation.prepareTurtleToDraw(myrtle, color, x, y);
    animation.pressC();
    

    To call a static method from a class file. You don't need to instantiate the object. Just use the class name followed by the "." or dot operator.

    Animation.animation();
    

    So

    public class AnimationTester1
    {
        public static void main(String[] args)
        {        
           AnimationGraphics animate = new AnimationGraphics();
    
           Animation animation = new Animation();
           animation.drawLine(myrtle, x1, y1, x2, y2);
           animation.prepareTurtleToDraw(myrtle, color, x, y);
           animation.pressC();
           Animation.animation();
    
        }    
    }