我使用NetBeans为我的Java类创建了两个独立的项目 . 最终,我想把两者结合起来 . 我希望他们俩都在同一个输出窗口上播放 .
一个项目是使用JavaFX创建的,一个是Java应用程序 . JavaFX项目是圣诞老人在输出窗口(src图像)中移动的动画 . 另一个是随着键盘移动的雪人 .
我怎样才能实现这个目标?我们还没有在课堂上学到这一点,但我想将它用于我的下一个项目 .
JavaFX的:
import javafx.animation.PathTransition;
import javafx.util.Duration;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
/**
* The "Santa Sleigh" JavaFX animation.
*/
public class SantaSleigh extends Application
{
/**
* Instantiates JavaFX Line and ImageView objects and places them on
* a PathTransition object in which the "Santa Sleigh" image object follows
* the line object right of the output window.
*
* @param primaryStage a Stage window object
*/
@Override
public void start(Stage primaryStage)
{
Pane pane = new Pane();
ImageView imageView = new ImageView("images/ss.png");
pane.getChildren().add(imageView);
PathTransition path = new PathTransition();
path.setDuration( Duration.millis(10000) );
path.setPath( new Line(-90, 100, 900, 20) );
path.setNode(imageView);
path.setCycleCount(5);
path.play();
Scene scene = new Scene(pane, 600, 400);
primaryStage.setTitle("Santa Sleigh Animation");
primaryStage.setScene(scene);
primaryStage.show();
}
}
Java应用程序:(您可以使用一个名为“ss.png”的图像文件 . )
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.scene.input.KeyEvent;
import javafx.event.EventHandler;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.Color;
import javafx.scene.Scene;
import javafx.scene.layout.Pane; // Class SnowMan extends Pane
import javafx.stage.Stage;
/**
* Moves a Snowman around the output window using Keyboard.
*/
public class SnowmanMoves extends Application
{
// The SnowMan object
private SnowMan snowMan;
// The starting x- and y-coordinates for the SnowkMan object
private int x = 150;
private int y = 150;
/**
* Instantiates SnowMan object and a KeyEvent handler to move the SnowMan
* around the output window.
*
* @param primaryStage a Stage window object
*/
@Override
public void start(Stage primaryStage)
{
snowMan = new SnowMan();
Scene scene = new Scene(snowMan, 300, 300);
KeyEventHandler e = new KeyEventHandler();
scene.setOnKeyPressed(e);
primaryStage.setTitle("SnowMan");
primaryStage.setResizable(false);
primaryStage.setScene(scene);
primaryStage.show();
snowMan.requestFocus();
}
/**
*
* @param args the String[] array command line parameter
*/
public static void main(String[] args) {
launch(args);
}
/**
* The event handler for the keyboard that uses the DOWN, UP,
* LEFT and RIGHT keys to move a StickMan object.
*/
public class KeyEventHandler implements EventHandler // Instead of
{
/**
* Validates for DOWN, UP, LEFT and RIGHT keys to move StickMan.
*
* @param e the KeyEvent parameter which stores keystroke information
*/
@Override
public void handle(KeyEvent e)
{
switch ( e.getCode() )
{
case DOWN:
y += 10;
break;
case UP:
y -= 10;
break;
case LEFT:
x -= 10;
break;
case RIGHT:
x += 10;
break;
}
snowMan.drawSnowMan();
}
}
/**
* A nested class that creates a SnowMan in a Pane.
*/
public class SnowMan extends Pane
{
private final Circle head;
private final Circle righteye;
private final Circle lefteye;
private final Circle nose;
private final Circle upperbody;
private final Circle lowerbody;
private final Line leftArmBottom;
private final Line rightArmBottom;
private final Rectangle topHat;
private final Rectangle bottomHat;
private final Circle button1;
private final Circle button2;
private final Circle button3;
/**
* The constructor instantiates each of the elements of the StickMan object.
*/
public SnowMan()
{
head = new Circle(20);
righteye = new Circle(3);
lefteye = new Circle(3);
nose = new Circle(2.3);
upperbody = new Circle(24);
lowerbody = new Circle(33);
leftArmBottom = new Line();
rightArmBottom = new Line();
topHat = new Rectangle();
bottomHat = new Rectangle();
button1 = new Circle(3);
button2 = new Circle(3);
button3 = new Circle(3);
drawSnowMan();
}
/**
* All graphical elements are calculates from the x- and y-coordinate
* center of the StickMan object (where the arms meet the body).
*/
public void drawSnowMan()
{
// Draw the head
head.setStroke(Color.GREY);
head.setFill(Color.WHITE);
head.setCenterX(x);
head.setCenterY(y - 40);
//Draw the eyes
righteye.setStroke(Color.BLACK);
righteye.setFill(Color.BLACK);
righteye.setCenterX(x - 6);
righteye.setCenterY(y - 40);
lefteye.setStroke(Color.BLACK);
lefteye.setFill(Color.BLACK);
lefteye.setCenterX(x + 6);
lefteye.setCenterY(y - 40);
//Draw the nose
nose.setStroke(Color.ORANGE);
nose.setFill(Color.ORANGE);
nose.setCenterX(x);
nose.setCenterY(y - 36);
// Draw the body
upperbody.setStroke(Color.GREY);
upperbody.setFill(Color.WHITE);
upperbody.setCenterX(x);
upperbody.setCenterY(y + 3);
lowerbody.setStroke(Color.GREY);
lowerbody.setFill(Color.WHITE);
lowerbody.setCenterX(x);
lowerbody.setCenterY(y + 55);
// Draw the arms
leftArmBottom.setStartX(x - 24);
leftArmBottom.setStartY(y + 5);
leftArmBottom.setEndX(x - 43);
leftArmBottom.setEndY(y - 25);
rightArmBottom.setStartX(x + 24);
rightArmBottom.setStartY(y + 5);
rightArmBottom.setEndX(x + 49);
rightArmBottom.setEndY(y - 25);
// Draw the Hat
topHat.setX(x - 15);
topHat.setY(y - 80);
topHat.setWidth(30);
topHat.setHeight(27);
bottomHat.setX(x - 21);
bottomHat.setY(y - 53);
bottomHat.setWidth(43);
bottomHat.setHeight(3);
//Draw the Buttons
button1.setStroke(Color.RED);
button1.setFill(Color.RED);
button1.setCenterX(x);
button1.setCenterY(y - 9);
button2.setStroke(Color.RED);
button2.setFill(Color.RED);
button2.setCenterX(x);
button2.setCenterY(y);
button3.setStroke(Color.RED);
button3.setFill(Color.RED);
button3.setCenterX(x);
button3.setCenterY(y + 10);
// Add the elements to the Pane
getChildren().addAll(lowerbody, upperbody, head, leftArmBottom,
rightArmBottom, topHat, bottomHat, righteye, lefteye, nose,
button1, button2, button3);
}
}
}
从我有限的理解,我应该能够创建一个新的主类,并将它们分类,通过同一个窗格显示?我该怎么做呢?