我正在为我的计算机科学类与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);
}
}
这解决了我的问题,但事情是我没有需要包括:animation.drawLine(番石榴,X1,Y1,X2,Y2); animation.prepareTurtleToDraw(myrtle,color,x,y);为了它的工作。我还把我的前两节课结合在一起。 –