如何从一个类到另一个类的主方法显示方法

时间:2016-07-27 04:30:57

标签: java bluej turtle-graphics

我正在使用Java为我的计算机科学课做动画(我正在使用BlueJ)。我现在遇到的问题是我无法将第一堂课的方法显示到第二堂课的主要方法中。这是我正在处理的代码。它不是最有序的代码,但这是我到目前为止所做的。

/**
* 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);

}    
}  

这是可行的版本,但它不是最有序的方式。对于这个,所有代码都在主方法中,这不是我想要改变的最佳格式。

/**
* 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); 
}    
}  

2 个答案:

答案 0 :(得分:0)

通过查看你的问题:“我无法将第一堂课的方法显示到第二堂课的主要方法中。”

要将类的方法访问到另一个类: 1)访问非静态方法: - 我们必须将第一个类的对象创建为第二个类。然后使用对象我们称之为第一类公共方法。

2)访问静态方法: - 通过使用类名,您可以调用类的公共方法。

class First
    {
       public void fun()
       {
           System.out.println("fun called");
       }

       public static void static_fun()
       {
           System.out.println("static_fun called");
       }

    }


class Second
{
    public static void main(String args[])

    //calling non-static method
    First obj=new First();
    obj.fun();

   //calling static method
   First.static_fun()
}

答案 1 :(得分:0)

java类只是对象的蓝图。因此,您必须首先创建对象或实例化该类。实例化对象是通过调用类名,为其指定变量,然后将其分配给对象构造函数来完成的。

Animation animation = new Animation();

创建对象后,您可以使用“。”或点运算符来调用类文件中创建的方法。

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

从类文件中调用静态方法。您不需要实例化该对象。只需使用类名后跟“。”。或点运算符。

Animation.animation();

所以

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();

    }    
}