游戏效果
类关系如下
不用看sample包和sql包。
BaseObject
package JavaBigJob;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.scene.Parent;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import java.security.Key;
public class BaseObject extends Parent {
//为了方便动态绑定,用Property类型
//坐标
private DoubleProperty xProperty = new SimpleDoubleProperty(0);
private DoubleProperty yProperty = new SimpleDoubleProperty(0);
//宽,高
private DoubleProperty widthProperty = new SimpleDoubleProperty(0);
private DoubleProperty heightProperty = new SimpleDoubleProperty(0);
private double speed = 1;//初始速度
public BaseObject() {
// this.xProperty.bind(super.xProperty());
}
public double getxProperty() {
return xProperty.get();
}
public DoubleProperty xProperty() {
return xProperty;
}
public void setxProperty(double xProperty) {
this.xProperty.set(xProperty);
}
public double getyProperty() {
return yProperty.get();
}
public DoubleProperty yProperty() {
return yProperty;
}
public void setyProperty(double yProperty) {
this.yProperty.set(yProperty);
}
public double getWidthProperty() {
return widthProperty.get();
}
public DoubleProperty widthPropertyProperty() {
return widthProperty;
}
public void setWidthProperty(double widthProperty) {
this.widthProperty.set(widthProperty);
}
public double getHeightProperty() {
return heightProperty.get();
}
public DoubleProperty heightPropertyProperty() {
return heightProperty;
}
public void setHeightProperty(double heightProperty) {
this.heightProperty.set(heightProperty);
}
public double getSpeed(){return this.speed;}
public void setSpeed(double speed){this.speed=speed;}
public void moveX(double x){
System.out.println(getxProperty()+" "+x);
this.setxProperty(getxProperty()+x);
System.out.println("moveX");
}
public void moveY(double y){
this.setyProperty(this.getyProperty()+y);
}
//判断是否发生矩形碰撞
public boolean isImpact(BaseObject baseObject){
if(getxProperty() + getWidthProperty() > baseObject.getxProperty() && getxProperty() < baseObject.getxProperty() + baseObject.getWidthProperty() && getyProperty() + getHeightProperty() > baseObject.getyProperty() && getyProperty() < baseObject.getyProperty() + baseObject.getHeightProperty()){
return true;
}
return false;
}
public interface CanMove {//定义一个接口,子类若能移动则需要继承此接口
void onKeyBoardPressed(KeyEvent event);
void onKeyBoardReleased(KeyEvent event);
static void add(){
System.out.println("nb");
}
}
}
BaseRectangle
平台类,设置成不可见,导入BaseRectangle到平台集合中。
package JavaBigJob;
import javafx.scene.effect.Bloom;
import javafx.scene.effect.BoxBlur;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Rectangle;
public class BaseRectangle extends BaseObject {
private Rectangle r = null;
private BoxBlur boxBlur = null;
private Bloom bloom = null;
private double width;
private double height;
public BaseRectangle(double width, double height) {
this.width = width;
this.height = height;
r = new Rectangle();
//矩形与该类实例化的对象进行动态绑定
r.widthProperty().bindBidirectional(this.widthPropertyProperty());
r.heightProperty().bindBidirectional(this.heightPropertyProperty());
r.xProperty().bindBidirectional(this.xProperty());
r.yProperty().bindBidirectional(this.yProperty());
//矩形宽度和长度
r.setWidth(width);
r.setHeight(height);
//动态绑定后把矩形添加
this.getChildren().addAll(r);
}
public BaseRectangle(double width, double height, double Arc1, double Arc2, Color color) {
this(width,height);
//设置圆角角度
r.setArcWidth(Arc1);
r.setArcHeight(Arc2);
r.setFill(color);
//模糊化
boxBlur = new BoxBlur();
boxBlur.setWidth(5);
boxBlur.setHeight(5);
//动态化
bloom = new Bloom();
bloom.setThreshold(50);
//上面两种属性与举行绑定
r.setEffect(boxBlur);
r.setEffect(bloom);
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public void setVisible(Boolean b){
r.setVisible(b);
this.setVisible(b);
}
// public void onMouse(MouseEvent event){
if (event.getX() >= this.getWidthProperty()/2 && event.getX() <= GameWindow.WIDTH - this.getWidthProperty()/2 && event.getY()<=Window.HEIGHT) {
// System.out.println(event.getX());
// System.out.println(event.getY());
// this.setxProperty(event.getX()/* - this.getWidthProperty()/2*/);
// this.setyProperty(event.getY()/* - this.getHeightProperty()/2*/);
}
// }
}
BaseScene
用来加载各种时间线和物体
(时间线:人物跳跃,人物移动,人物下落,人物与平台的碰撞判定。子弹飞行,子弹碰撞。
物体:两个人物,背景,平台)
package JavaBigJob;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Parent;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.util.Duration;
import java.util.ArrayList;
import java.util.Collections;
//场景类,继承了Parent
//加载了 循环时间轴,和各个物体以及场景
public class BaseScene extends Parent {
public BaseRectangle br;
Rectangle rectangleSecen;
public ArrayList<BaseRectangle> brArrayList = new ArrayList<BaseRectangle>();
static ArrayList<Rectangle> bulletArrayList = new ArrayList<Rectangle>();
public static Man man1 = new Man();;
public static Man2 man2 = new Man2();
private int width;//场景的宽度长度,用于边界判断。
private int height;
static Bullet bullet;
private ReplaceClothes replaceClothes;
KeyFrame keyFrame;//栈帧keyframe,每一帧要执行的事件,执行后通过timeline刷新
// Timeline timeline;
//bg为背景版
public Rectangle bg;
public ImageView imageView;
public BaseScene(int width,int height) {
this.width = width;
this.height = height;
initAll();
}
public void initBackGround(){
imageView = new ImageView(new Image("JavaBigJob/Picture/89.png"));
imageView.setFitWidth(1111);
imageView.setFitHeight(800);
bg = new Rectangle(0,0,GameWindow.WIDTH,GameWindow.HEIGHT);
bg.setFill(Color.BLACK);
this.getChildren().add(imageView);
}
public void initBullet(){
bullet = new Bullet();
getChildren().addAll(bullet);
}
public void initMan(){
man1.setxProperty(700);
man1.setyProperty(100);
this.getChildren().add(man1);
}
public void initMan2(){
man2.setxProperty(200);
man2.setyProperty(100);
this.getChildren().add(man2);
}
public void initReplace(){
//加载换装页面,而不是new一个节点;
replaceClothes = new ReplaceClothes();
}
//加载时间轴
public void initTimeline(){
moveTimeline();
Man.dropOutTimeline(brArrayList);
// Man.jumpTimeline();
Man2.dropOutTimeline(brArrayList);
// Man2.jumpTimeline();
}
//加载临时平台
public void initTmpPlat(){
//临时平台
br = new BaseRectangle(320,5,10,10,Color.YELLOW);
br.setxProperty(710);
br.setyProperty(525);
BaseRectangle br2 = new BaseRectangle(420,5,10,10,Color.BLUE);
br2.setxProperty(350);
br2.setyProperty(400);
BaseRectangle br3 = new BaseRectangle(860,5,10,10,Color.GREEN);
br3.setxProperty(135);
br3.setyProperty(173);
BaseRectangle br4 = new BaseRectangle(875,5,10,10,Color.RED);
br4.setxProperty(150);
br4.setyProperty(650);
BaseRectangle br5 = new BaseRectangle(350,5,10,10,Color.GOLD);
br5.setxProperty(85);
br5.setyProperty(525);
BaseRectangle br6 = new BaseRectangle(350,5,10,10,Color.BLACK);
br6.setxProperty(693);
br6.setyProperty(525);
BaseRectangle br7 = new BaseRectangle(350,5,10,10,Color.GOLD);
br7.setxProperty(70);
br7.setyProperty(285);
BaseRectangle br8 = new BaseRectangle(350,5,10,10,Color.BLACK);
br8.setxProperty(705);
br8.setyProperty(285);
//后续改变下面的方法
Collections.addAll(brArrayList,br,br2,br3,br4,br5,br6,br7,br8);
// brArrayList.add(br);
// brArrayList.add(br2);
this.getChildren().addAll();
}
public void initAll(){
//加载背景
this.initBackGround();
//临时平台
this.initTmpPlat();
//加载物体
//加载玩家1
this.initMan();
//加载玩家2
this.initMan2();
this.initBullet();
//换装页面
this.initReplace();
//把已加载的背景与物体 添加到游戏场景中
// this.getChildren().addAll(bg,man1,man2);
//加载时间线
this.initTimeline();
}
public void moveTimeline(){
Timeline timeline;
timeline = new Timeline();
timeline.setCycleCount(-1);//无限循环
KeyFrame keyFrame = new KeyFrame(Duration.millis(4), new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {//重复执行该方法
BaseScene.man1.move();//左右移动and跳跃,坠落
BaseScene.man2.move();
}
});
timeline.getKeyFrames().add(keyFrame);//为时间轴添加栈帧
timeline.play();//时间轴循环执行每个栈帧
}
public void loadKeyBoard(){//全局键盘方法
//通过按压,抬起标记人物四个方向上是否移动,然后通过时间轴不断刷新来判断标记,进一步执行移动
this.getScene().setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
System.out.println("Pressed");
man1.onKeyBoardPressed(event);
man2.onKeyBoardPressed(event);
bullet.onKeyBoardPressed(event);
}
});
getScene().setOnKeyReleased(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
System.out.println("Released");
man1.onKeyBoardReleased(event);
man2.onKeyBoardReleased(event);
bullet.onKeyBoardReleased(event);
}
});
}
}
Bullet
package JavaBigJob;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Parent;
import javafx.scene.effect.Bloom;
import javafx.scene.effect.BoxBlur;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.util.Duration;
import java.util.*;
import static JavaBigJob.BaseScene.*;
public class Bullet extends BaseObject implements BaseObject.CanMove{
// private Rectangle r = null;
int i=0,j=0;
int z=0;
boolean dirctionClock=false;
boolean dirctionTwoClok=false;
boolean isOne = false;
static Rectangle[] rectangles = new Rectangle[100];
static boolean select = true;
static boolean selectTwo = true;
boolean Out = false;
private BoxBlur boxBlur = null;
private Bloom bloom = null;
static boolean shoutOpen = false;
static boolean shoutOpenTwo = false;
private double BulletSpeed;
static boolean isShow = false;
Rectangle rectangle = null;
static Rectangle rectangle1 = null;
ArrayList<Bullet> list = new ArrayList<Bullet>();
ArrayList<Bullet> list2 = new ArrayList<>();
public Bullet() {
Rectangle rectangle=new Rectangle();
rectangle.setX(man1.gun.getX()+40);
rectangle.setY(man1.gun.getY());
rectangle.setFill(Color.PINK);
rectangle.setWidth(12);
rectangle.setHeight(12);
this.setxProperty(120);
this.setyProperty(120);
rectangle.setArcWidth(34);
rectangle.setArcHeight(56);
BulletSpeed = 3;
// 模糊化
boxBlur = new BoxBlur();
boxBlur.setWidth(5);
boxBlur.setHeight(5);
//动态化
bloom = new Bloom();
bloom.setThreshold(50);
//上面两种属性与举行绑定
rectangle.setEffect(boxBlur);
rectangle.setEffect(bloom);
}
public Rectangle HandGunBullet(){
Rectangle rectangle=new Rectangle();
rectangle.setX(man2.hand.getX()+40);
rectangle.setY(man2.hand.getY());
rectangle.setFill(Color.RED);
rectangle.setWidth(12);
rectangle.setHeight(12);
this.setxProperty(140);
this.setyProperty(140);
rectangle.setArcWidth(34);
rectangle.setArcHeight(56);
BulletSpeed = 3;
// 模糊化
boxBlur = new BoxBlur();
boxBlur.setWidth(6);
boxBlur.setHeight(6);
//动态化
bloom = new Bloom();
bloom.setThreshold(55);
//上面两种属性与举行绑定
rectangle.setEffect(boxBlur);
rectangle.setEffect(bloom);
return rectangle;
}
public void RifleGunBullet(){
rectangle=new Rectangle();
rectangle.setFill(Color.BLUE);
rectangle.setWidth(20);
rectangle.setHeight(9);
rectangle.setX(man1.gun.getX()+40);
rectangle.setY(man1.gun.getY());
this.setxProperty(140);
this.setyProperty(150);
rectangle.setArcWidth(20);
rectangle.setArcHeight(20);
BulletSpeed = 3;
// 模糊化
boxBlur = new BoxBlur();
boxBlur.setWidth(5);
boxBlur.setHeight(5);
//动态化
bloom = new Bloom();
bloom.setThreshold(50);
//上面两种属性与举行绑定
rectangle.setEffect(boxBlur);
rectangle.setEffect(bloom);
}
public Rectangle snipGunBullet(){
Rectangle rectangle=new Rectangle();
rectangle.setFill(Color.GOLD);
rectangle.setWidth(28);
rectangle.setHeight(7);
rectangle.setX(man1.gun.getX()+40);
rectangle.setY(man1.gun.getY());
this.setxProperty(140);
this.setyProperty(150);
rectangle.setArcWidth(20);
rectangle.setArcHeight(20);
BulletSpeed = 3;
// 模糊化
boxBlur = new BoxBlur();
boxBlur.setWidth(5);
boxBlur.setHeight(5);
//动态化
bloom = new Bloom();
bloom.setThreshold(50);
//上面两种属性与举行绑定
rectangle.setEffect(boxBlur);
rectangle.setEffect(bloom);
return rectangle;
}
public Rectangle ShotGunBullet(){
Rectangle rectangle = new Rectangle();
rectangle.setFill(Color.GOLD);
rectangle.setWidth(15);
rectangle.setHeight(20);
rectangle.setX(man2.hand.getX()+40);
rectangle.setY(man2.hand.getY());
this.setxProperty(140);
this.setyProperty(150);
rectangle.setArcWidth(1);
rectangle.setArcHeight(32);
BulletSpeed = 3;
// 模糊化
boxBlur = new BoxBlur();
boxBlur.setWidth(5);
boxBlur.setHeight(5);
//动态化
bloom = new Bloom();
bloom.setThreshold(50);
//上面两种属性与举行绑定
rectangle.setEffect(boxBlur);
rectangle.setEffect(bloom);
return rectangle;
}
public static void MoveLeft(Bullet bullet){
if(shoutOpen == true&&isImpacte(man2,bullet)==false){
// rectangle.setX(rectangle.getX()-1);
bullet.rectangle.setX(bullet.rectangle.getX()-1);
// bullet.moveX(-1);
}
}
public static void MoveRight(Bullet bullet){
if(shoutOpen == true&&isImpacte(man2,bullet)==false){
// rectangle.setX(rectangle.getX()+1);
bullet.rectangle.setX(bullet.rectangle.getX()+1);
}
}
public static void MoveTwoLeft(Bullet bullet){
if(shoutOpenTwo == true&&isImpacteTwo(man1,bullet)==false){
bullet.rectangle.setX(bullet.rectangle.getX()-1);
}
}
public static void MoveTwoRight(Bullet bullet){
if(shoutOpenTwo == true&&isImpacteTwo(man1,bullet)==false){
bullet.rectangle.setX(bullet.rectangle.getX()+1);
}
}
public void shot(Bullet bullet){
Timeline timeline;
timeline = new Timeline();
timeline.setCycleCount(-1);
KeyFrame keyFrame = new KeyFrame(Duration.millis(1), new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
//
if(bullet.select == false /*&&*/ /*bullet.isOne == false*/){
man2.moveX(-4);
System.out.println("移动了man2!!!!!!!!!!!!!!!");
bullet.select = true;
}
if(bullet.rectangle.getX()< 0 || bullet.rectangle.getX() > 1100 || isImpacte(man2,bullet) == true){
getChildren().remove(bullet.rectangle);
getChildren().remove(bullet);
// timeline.stop();
}
}
});
timeline.getKeyFrames().add(keyFrame);//为时间轴添加栈帧
timeline.play();//时间轴循环执行每个栈帧
}
public void shotTwo(Bullet bullet){
Timeline timeline;
timeline = new Timeline();
timeline.setCycleCount(-1);
KeyFrame keyFrame = new KeyFrame(Duration.millis(0.5), new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
if(bullet.selectTwo == false ){
man1.moveX(5);
System.out.println("!!!!!!!!!!!!!!!移动了man1");
bullet.selectTwo = true;
}
if(bullet.getxProperty() < 0 || bullet.getxProperty() > 1100 || isImpacteTwo(man1,bullet) == true){
getChildren().remove(bullet.rectangle);
getChildren().remove(bullet);
// timeline.stop();
}
}
});
timeline.getKeyFrames().add(keyFrame);//为时间轴添加栈帧
timeline.play();//时间轴循环执行每个栈帧
}
public void ShotMove(Bullet bullet){
Timeline timeline;
timeline = new Timeline();
timeline.setCycleCount(-1);
KeyFrame keyFrame = new KeyFrame(Duration.millis(4), new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
if (bullet.dirctionClock) {
MoveLeft(bullet);
}
else
MoveRight(bullet);
}
});
timeline.getKeyFrames().add(keyFrame);//为时间轴添加栈帧
timeline.play();//时间轴循环执行每个栈帧
}
public void ShotMoveTwo(Bullet bullet){
Timeline timeline;
timeline = new Timeline();
timeline.setCycleCount(-1);
KeyFrame keyFrame = new KeyFrame(Duration.millis(4), new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
if(bullet.dirctionTwoClok){
MoveTwoLeft(bullet);
}
// else if(isImpacteN(man2,man1)){
// MoveTwoLeft(bullet);
// }
else
MoveTwoRight(bullet);
}
});
timeline.getKeyFrames().add(keyFrame);//为时间轴添加栈帧
timeline.play();//时间轴循环执行每个栈帧
}
@Override
public void onKeyBoardPressed(KeyEvent event){
if(event.getCode()==KeyCode.J){
shoutOpen=true;
Bullet bullet = new Bullet();
list.add(bullet);
if(Man.direction==0){
list.get(i).dirctionClock = true;
}
else {
list.get(i).dirctionClock = false;
}
list.get(i).rectangle = bullet.snipGunBullet();
shot(list.get(i));
ShotMove(list.get(i));
getChildren().add(list.get(i).rectangle);
list.remove(0);
}
if(event.getCode()==KeyCode.K){
shoutOpenTwo = true;
Bullet bullet = new Bullet();
list2.add(bullet);
if(Man2.direction==0){
list2.get(z).dirctionTwoClok = true;
}
else {
list2.get(z).dirctionTwoClok = false;
}
list2.get(z).rectangle = bullet.HandGunBullet();
shotTwo(list2.get(z));
ShotMoveTwo(list2.get(z));
getChildren().add(list2.get(z).rectangle);
list2.remove(0);
}
}
@Override
public void onKeyBoardReleased(KeyEvent event) {
}
public static boolean isImpacte(BaseObject baseObject,Bullet bullet) {
if(bullet.rectangle.getX()==baseObject.getxProperty()+Man.width&&bullet.rectangle.getY() >= baseObject.getyProperty()-Man.height/2&&bullet.rectangle.getY()<=baseObject.getyProperty()+Man.height){
bullet.select = false;
bullet.isOne = true;
return true;
}
return false;
}
public static boolean isImpacteTwo(BaseObject baseObject,Bullet bullet) {
if(bullet.rectangle.getX()==baseObject.getxProperty()-Man.width/2+5&&bullet.rectangle.getY() >= baseObject.getyProperty()-Man.height/2&&bullet.rectangle.getY()<=baseObject.getyProperty()+Man.height){
bullet.selectTwo = false;
bullet.isOne = true;System.out.println("$$$$$$$$$$$$$$$$$$$$$$$");
return true;
}
return false;
}
public static boolean isImpacteN(BaseObject baseObject,BaseObject baseObject1) {
if(baseObject.getxProperty()>baseObject1.getxProperty()){
return true;
}
return false;
}
// @Override
// public void run() {
//
// Timeline timeline;
// timeline = new Timeline();
// timeline.setCycleCount(-1);
// KeyFrame keyFrame = new KeyFrame(Duration.millis(4), new EventHandler<ActionEvent>() {
// @Override
// public void handle(ActionEvent actionEvent) {
// System.out.println("多线程}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}");
// if(Man.direction==0){
//
// MoveLeft(rectangle);
// }
// else if(Man.direction==1)
// MoveRight(rectangle);
// }
// });
// timeline.getKeyFrames().add(keyFrame);//为时间轴添加栈帧
// timeline.play();//时间轴循环执行每个栈帧
// }
}
DatabaseUtil
package JavaBigJob;
import java.sql.*;
public class DataBaseUtil {
//2.通过DriverManager获取数据库连接
// String url = "jdbc:mysql://localhost:3306/label?sever";
String url = "jdbc:mysql://localhost:3306/label?severTimezone=GMT%2B8";
String driver = "com.mysql.cj.jdbc.Driver";
String user = "root";
String password = "wenchi0124";
Connection conn = null;
ResultSet rs = null;
Statement statement = null;
PreparedStatement ps = null;
public DataBaseUtil() {
try {
conn = DriverManager.getConnection(url,user,password);
System.out.println("line success");
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
public void close(){
try{
if(rs != null){
rs.close();
}
if(ps != null){
ps.close();
}
if(statement != null){
statement.close();
}
if(conn != null){
conn.close();
}
}
catch (SQLException throwables) {
throwables.printStackTrace();
}
finally {
// this.close();
}
}
public ResultSet queryExecute(String sql) {
try {
Class.forName(driver);
System.out.println("query_success01");
statement = conn.createStatement();
rs = statement.executeQuery(sql);
return rs;
}catch (Exception ex){
ex.printStackTrace();
return null;
}
finally {
// this.close();
}
}
//重载
public ResultSet queryExecute(String sql,String[] paras){
try {
Class.forName(driver);
System.out.println("query_success02");
// conn = DriverManager.getConnection(url,user,password);
ps = conn.prepareStatement(sql);
for(int i =0;i < paras.length;i++){
ps.setString(i+1,paras[i]);
}
rs = ps.executeQuery();
return rs;
}catch (Exception ex){
ex.printStackTrace();
}
finally {
// this.close();
}
return null;
}
public boolean updateExecute(String sql,String[] paras){
boolean b = true;
try {
Class.forName(driver);
System.out.println("update_success03");
// conn = DriverManager.getConnection(url,user,password);
ps = conn.prepareStatement(sql);
for(int i =0;i < paras.length;i++){
ps.setString(i+1,paras[i]);
}
if(ps.executeLargeUpdate() != 1){
b = false;
}
}catch (Exception ex){
b = false;
ex.printStackTrace();
}
finally {
// this.close();
}
return b;
}
}
GameWindow
package JavaBigJob;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;
public class GameWindow /*extends Application*/ {
public static final int WIDTH = 1100;
public static final int HEIGHT = 700;
GameWindow(){
BaseScene bs = new BaseScene(WIDTH,HEIGHT);
Scene bsScene = new Scene(bs,WIDTH,HEIGHT);
Stage stage = new Stage();
stage.setTitle("javaBigWork");
stage.setScene(bsScene);//添加bs至scene中,下一步通过bs。getScene返回Scene的实例化对象
bs.loadKeyBoard();//因为实例化了scene,加载scene的全局键盘事件
stage.show();
}
}
login.css
给登录界面加的css样式
#paneId{
-fx-background-image: url('Picture/login.png');
/*-fx-background-image-opacity: 0.5;*/
-fx-background-color: #0155fd;
}
#labelLogin {
-fx-text-fill: rgba(238, 116, 116, 0.88);
-fx-font-size: 10pt;
-fx-font-family: "Sans-serif";
-fx-font-weight: 500;
-fx-font-smoothing-type: inherit;
/*-fx-font-weight: bold;*/
}
#warn{
-fx-text-fill: #30ec9a;
-fx-font-family: "Microsoft Yahei";
-fx-background-color: gray;
-fx-font-weight: bold;
-fx-font-size: 10pt;
-fx-border-color: #0155fd;
}
#btLogin {
-fx-text-fill: #2ffd01;
-fx-font-family: "Microsoft Yahei";
-fx-text-alignment: center;
/*-fx-wrap-text: true;*/
-fx-cell-size: 30pt;
-fx-font-weight: bold;
-fx-background-color: linear-gradient(#6169b1, rgba(253, 119, 1, 0.42));
-fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );
}
#tfLogin {
-fx-animated: true;
-fx-border-width: 2pt;
-fx-border-color: gray;
/*-fx-border-radius:10px;*/
}
LoginFrame
package JavaBigJob;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.sql.ResultSet;
import java.sql.SQLException;
public class LoginFrame extends Application{
Label nameLabel = new Label("User Name :");
Label passwordLabel = new Label("Password : ");
Label lack_information = new Label("用户信息填写不全!");
Label Mismatch_information = new Label("用户名或密码不正确");
Label user_nonentity = new Label("用户不存在");;
Label user_exit = new Label("用户已存在");
Label sign_successful = new Label("注册成功!");
Label login_successful = new Label("登陆成功!");
HBox user = new HBox();
HBox password = new HBox();
TextField tfUser = new TextField();
PasswordField tfPassword = new PasswordField();
Button btLogIn = new Button("Log in");
Button btSignIn = new Button("Sign in");
HBox h3 = new HBox();//装按钮
VBox pane = new VBox();
@Override
public void start(Stage stage) {
//css
btLogIn.setId("btLogin");//设置id
btSignIn.setId("btLogin");
nameLabel.setId("labelLogin");
passwordLabel.setId("labelLogin");
tfUser.setId("tfLogin");
tfPassword.setId("tfLogin");
lack_information.setId("warn");
Mismatch_information.setId("warn");
user_nonentity.setId("warn");
user_exit.setId("warn");
sign_successful.setId("warn");
user.getChildren().addAll(nameLabel,tfUser);
user.setAlignment(Pos.CENTER);
user.setSpacing(20);
password.getChildren().addAll(passwordLabel,tfPassword);
password.setAlignment(Pos.CENTER);
password.setSpacing(20);
h3.setAlignment(Pos.CENTER);
btLogIn.setAlignment(Pos.BASELINE_RIGHT);
btSignIn.setAlignment(Pos.BASELINE_RIGHT);
h3.getChildren().addAll(btLogIn,btSignIn);
h3.setSpacing(20);
pane.setAlignment(Pos.CENTER);
pane.setSpacing(20);
pane.getChildren().addAll(user,password,h3);
Scene scene = new Scene(pane,400,250);
pane.setId("paneId");
//添加css样式
scene.getStylesheets().add(
LoginFrame.class.getResource("login.css")
.toExternalForm());
stage.setScene(scene);
stage.setTitle("Welcome!");
stage.show();
btLogIn.setOnAction(e->{
if(user_exist()==false){
System.out.println("用户不存在");
HBox hBox = new HBox();
Label label = user_nonentity;
ImageView image = new ImageView("JavaBigJob/Picture/No.png");
image.setFitWidth(150);
image.setFitHeight(120);
hBox.setAlignment(Pos.CENTER);
hBox.setSpacing(10);
hBox.getChildren().addAll(image,label);
Scene sub_scene = new Scene(hBox,400,200);
sub_scene.getStylesheets().add(
LoginFrame.class.getResource("login.css")
.toExternalForm());
Stage stage1 = new Stage();
stage1.setScene(sub_scene);
stage1.setTitle("ERROR");
stage1.show();
}
else if(detection_information()){
if(user_right()){
stage.hide();
//登陆成功
new GameWindow();
}
}
});
btSignIn.setOnAction(e->{
if(user_exist()){
System.out.println("用户已存在");
HBox hBox = new HBox();
Label label = user_exit;
ImageView image = new ImageView("JavaBigJob/Picture/No.png");
image.setFitWidth(150);
image.setFitHeight(120);
hBox.setAlignment(Pos.CENTER);
hBox.setSpacing(10);
hBox.getChildren().addAll(image,label);
Scene sub_scene = new Scene(hBox,400,200);
sub_scene.getStylesheets().add(
LoginFrame.class.getResource("login.css")
.toExternalForm());
Stage stage1 = new Stage();
stage1.setScene(sub_scene);
stage1.setTitle("ERROR");
stage1.show();
}
else if(detection_information())
{
DataBaseUtil db = new DataBaseUtil();
String sql = "INSERT INTO user (name,passwd) VALUES (?,?)";
db.updateExecute(sql,new String[]{tfUser.getText(),tfPassword.getText()});
HBox hBox = new HBox();
Label label = sign_successful;
ImageView image = new ImageView("JavaBigJob/Picture/yes.png");
image.setFitWidth(150);
image.setFitHeight(120);
hBox.setAlignment(Pos.CENTER);
hBox.setSpacing(10);
hBox.getChildren().addAll(image,label);
Scene sub_scene = new Scene(hBox,400,200);
sub_scene.getStylesheets().add(
LoginFrame.class.getResource("login.css")
.toExternalForm());
Stage stage1 = new Stage();
stage1.setScene(sub_scene);
stage1.show();
}
});
}
public static void main(String[] args) {
DataBaseUtil db = new DataBaseUtil();
launch();
}
public Boolean user_exist(){//判断是否已存在用户
DataBaseUtil db = new DataBaseUtil();
String sql = "select count(*) from user where name = '"+tfUser.getText()+"'";
try {
ResultSet rs = db.queryExecute(sql);
rs.next();
if(rs.getInt(1)==1){
return true;
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return false;
}
public Boolean user_right(){
DataBaseUtil db = new DataBaseUtil();
String sql = "select count(*) from user where name = ? and passwd = ?";
try {
ResultSet rs = db.queryExecute(sql,new String[]{tfUser.getText(),tfPassword.getText()});
rs.next();
if(rs.getInt(1)==1){
return true;
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
System.out.println("用户名或密码不正确");
HBox hBox = new HBox();
Label label = Mismatch_information;
ImageView image = new ImageView("JavaBigJob/Picture/No.png");
image.setFitWidth(150);
image.setFitHeight(120);
hBox.setAlignment(Pos.CENTER);
hBox.setSpacing(10);
hBox.getChildren().addAll(image,label);
Scene sub_scene = new Scene(hBox,400,200);
sub_scene.getStylesheets().add(
LoginFrame.class.getResource("login.css")
.toExternalForm());
Stage stage1 = new Stage();
stage1.setScene(sub_scene);
stage1.setTitle("ERROR");
stage1.show();
return false;
}
public Boolean detection_information(){//判断信息是否填写完全
if (tfUser.getText().equals("")||tfPassword.getText().equals("")){
System.out.println("信息不全!");
HBox hBox = new HBox();
Label label = lack_information;
ImageView image = new ImageView("JavaBigJob/Picture/No.png");
image.setFitWidth(150);
image.setFitHeight(120);
hBox.setAlignment(Pos.CENTER);
hBox.setSpacing(10);
hBox.getChildren().addAll(image,label);
Scene sub_scene = new Scene(hBox,400,200);
sub_scene.getStylesheets().add(
LoginFrame.class.getResource("login.css")
.toExternalForm());
Stage stage1 = new Stage();
stage1.setScene(sub_scene);
stage1.setTitle("ERROR");
stage1.show();
return false;//信息不全
}
return true;//信息全
}
}
Man
package JavaBigJob;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.effect.Effect;
import javafx.scene.effect.PerspectiveTransform;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.util.Duration;
import java.util.ArrayList;
import static JavaBigJob.BaseScene.man1;
public class Man extends BaseObject implements BaseObject.CanMove {
static double high = 3;//跳跃时的高度
static boolean isOnPlat = false;
static int dropSpeed = 1;
static int times = 0;
static int height = 68;
static int width = 42;
static boolean jump = false;//是否正在跳跃
static boolean down = false;//掉到下一层
static boolean PressJump = false;//跳跃(是否正在按压跳跃键)
boolean moveLeft = false;
// boolean moveUp = false;
boolean moveRight = false;
// boolean moveDown = false;
static int direction = 0;//0表示人物向左,1表示人物向右
//头部不能与this.xProperty直接绑定(头是突出来的,必须改变坐标才能美观),直接绑定无法动态改变头部的相对位置(不能在add方法中添加变量,变量即使改变,也始终为第一次的值)。
ImageView body = new ImageView("JavaBigJob/Picture/198.png");
ImageView head = new ImageView("JavaBigJob/Picture/229.png");
ImageView foot1 = new ImageView("JavaBigJob/Picture/181.png");
ImageView foot2 = new ImageView("JavaBigJob/Picture/181.png");
ImageView hand = new ImageView("JavaBigJob/Picture/102.png");
ImageView gun = new ImageView("JavaBigJob/Picture/456.png");
ImageView clothes = new ImageView("JavaBigJob/Picture/198.png");
public Man(){
// ReplaceClothes replaceClothes = new ReplaceClothes();
// Image image = replaceClothes.GetImage();
// clothes = new ImageView(image);
clothes.xProperty().bind(body.xProperty());
clothes.yProperty().bind(body.yProperty());
clothes.setFitHeight(55);
clothes.setFitWidth(35);
clothes.scaleXProperty().bind(body.scaleXProperty());
body.setScaleX(-1);
//设置主角图片以及绑定图片在场景中的位置
body.setFitHeight(55);
body.setFitWidth(35);
body.xProperty().bind(this.xProperty());
body.yProperty().bind(this.yProperty());
head.setFitWidth(30);
head.setFitHeight(32);
head.xProperty().bind(this.xProperty().add(-8));
head.yProperty().bind(this.yProperty().add(-12));
hand.xProperty().bind(this.xProperty().add(-4));
hand.yProperty().bind(this.yProperty().add(20));
hand.scaleXProperty().bind(body.scaleXProperty());
hand.rotateProperty().bind(body.yProperty());
head.scaleXProperty().bind(body.scaleXProperty());//绑定翻转
head.rotateProperty().bind(body.rotateProperty());//绑定角度
foot1.xProperty().bind(this.xProperty());
foot1.yProperty().bind(this.yProperty().add(55));
foot1.rotateProperty().bind(body.rotateProperty());
foot1.scaleXProperty().bind(body.scaleXProperty());
foot2.xProperty().bind(this.xProperty().add(15));
foot2.yProperty().bind(this.yProperty().add(55));
foot2.rotateProperty().bind(body.rotateProperty());
foot2.scaleXProperty().bind(body.scaleXProperty());
gun.xProperty().bind(hand.xProperty().add(-35));
gun.yProperty().bind(hand.yProperty().add(-4));
gun.scaleXProperty().bind(hand.scaleXProperty());
//添加到Man
getChildren().addAll(body,clothes,head,foot1,foot2,hand,gun);
}
public void onKeyBoardPressed(KeyEvent event){
if(event.getCode() == KeyCode.RIGHT){
moveRight = true;
}
if(event.getCode() == KeyCode.LEFT){
moveLeft = true;
}
if(event.getCode() == KeyCode.UP){
PressJump = true;
System.out.println("按压了!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
if(event.getCode() == KeyCode.DOWN){
down = true;
}
}
public void onKeyBoardReleased(KeyEvent event){
if(event.getCode() == KeyCode.RIGHT){
moveRight = false;
}
if(event.getCode() == KeyCode.LEFT){
moveLeft = false;
}
if(event.getCode() == KeyCode.UP){
PressJump = false;
}
if(event.getCode() == KeyCode.DOWN){
down = false;
}
}
public void move(){
if(moveLeft == true){
head.xProperty().bind(this.xProperty().add(-7));
hand.xProperty().bind(this.xProperty().add(-5));
gun.xProperty().bind(this.xProperty().add(-38));
this.direction = 0;//向左
body.setScaleX(-1);//设置翻转
moveX(-this.getSpeed());
}
if(PressJump == true && jump == false &&isOnPlat == true){//正在按下跳跃&不在跳跃过程中&在平台上,进入if后执行跳跃timeline,
this.jumpTimeline();
PressJump = false;
isOnPlat = false;
}
if(moveRight == true){
head.xProperty().bind(this.xProperty().add(14));
hand.xProperty().bind(this.xProperty().add(25));
gun.xProperty().bind(this.xProperty().add(23));
this.direction = 1;//向右
body.setScaleX(1);
moveX(this.getSpeed());
}
if(down == true && isOnPlat==true){
moveY(1);
down = false;
}
}
public static void dropOutTimeline(ArrayList<BaseRectangle> brArrayList){
System.out.println("---------------");
Timeline timeline;
timeline = new Timeline();
timeline.setCycleCount(-1);//无限循环
KeyFrame keyFrame = new KeyFrame(Duration.millis(4), new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {//重复执行该方法
// System.out.println("--------0-------");
// System.out.println("---------1------");
if(!Man.jump){
if(ObjectCollections.Man1DropJudge(brArrayList)){
System.out.println("碰撞到平台了!!!!!!!!!");
Man.isOnPlat = true;
// dropSpeed = 0;
}
else{
Man.isOnPlat = false;
BaseScene.man1.moveY(dropSpeed);
System.out.println("在坠落!");
}
}
}
});
timeline.getKeyFrames().add(keyFrame);//为时间轴添加栈帧
timeline.play();//时间轴循环执行每个栈帧
}
public void jumpTimeline(){
jump = true;//
isOnPlat = false;
Timeline timeline;
timeline = new Timeline();
timeline.setCycleCount(90);//90
System.out.println("&&&&&&&执行了jump_timeline!!!!!");
// high = 3;//初始高度为3
// times = 0;
KeyFrame keyFrame = new KeyFrame(Duration.millis(4), new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
//重复执行该方法
times++;
System.out.println("&&&&&&&跳跃了!!!!!!!!!!");
man1.moveY(-high);
}
});
timeline.getKeyFrames().add(keyFrame);//为时间轴添加栈帧
timeline.play();//时间轴循环执行每个栈帧
jump = false;
}
}
Man2
package JavaBigJob;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.util.Duration;
import java.util.ArrayList;
import static JavaBigJob.BaseScene.man1;
import static JavaBigJob.BaseScene.man2;
public class Man2 extends BaseObject implements BaseObject.CanMove{
static double high = 3;//跳跃时的高度
static boolean isOnPlat = false;
static int dropSpeed = 1;
static int height = 68;
static int width = 42;
static boolean jump = false;//是否正在跳跃
static boolean down = false;//掉到下一层
static boolean PressJump = false;//跳跃(是否正在按压跳跃键)
boolean moveLeft = false;
// boolean moveUp = false;
boolean moveRight = false;
// boolean moveDown = false;
static int direction = 1;//0表示人物向左,1表示人物向右
//头部不能与this.xProperty直接绑定(头是突出来的,必须改变坐标才能美观),直接绑定无法动态改变头部的相对位置(不能在add方法中添加变量,变量即使改变,也始终为第一次的值)。
ImageView body = new ImageView("JavaBigJob/Picture/200.png");
ImageView head = new ImageView("JavaBigJob/Picture/230.png");
ImageView foot1 = new ImageView("JavaBigJob/Picture/182.png");
ImageView foot2 = new ImageView("JavaBigJob/Picture/182.png");
ImageView hand = new ImageView("JavaBigJob/Picture/104.png");
ImageView clothes = new ImageView("JavaBigJob/Picture/200.png");
public Man2(){
clothes.setFitHeight(55);
clothes.setFitWidth(35);
//设置主角图片以及绑定图片在场景中的位置
body.setFitHeight(55);
body.setFitWidth(35);
body.xProperty().bind(this.xProperty());
body.yProperty().bind(this.yProperty());
head.setFitWidth(30);
head.setFitHeight(32);
head.xProperty().bind(this.xProperty().add(14));
head.yProperty().bind(this.yProperty().add(-12));
head.scaleXProperty().bind(body.scaleXProperty());//绑定翻转
head.rotateProperty().bind(body.rotateProperty());//绑定角度
hand.xProperty().bind(this.xProperty().add(25));
hand.yProperty().bind(this.yProperty().add(20));
hand.scaleXProperty().bind(body.scaleXProperty());
hand.rotateProperty().bind(body.yProperty());
foot1.xProperty().bind(this.xProperty());
foot1.yProperty().bind(this.yProperty().add(55));
foot1.rotateProperty().bind(body.rotateProperty());
foot1.scaleXProperty().bind(body.scaleXProperty());
foot2.xProperty().bind(this.xProperty().add(15));
foot2.yProperty().bind(this.yProperty().add(55));
foot2.rotateProperty().bind(body.rotateProperty());
foot2.scaleXProperty().bind(body.scaleXProperty());
clothes.xProperty().bind(body.xProperty());
clothes.yProperty().bind(body.yProperty());
clothes.scaleXProperty().bind(body.scaleXProperty());//衣服绑定
clothes.rotateProperty().bind(body.rotateProperty());
//添加到环境
getChildren().addAll(body,clothes,head,foot1,foot2,hand);
}
public void onKeyBoardPressed(KeyEvent event){
if(event.getCode() == KeyCode.D){
moveRight = true;
}
if(event.getCode() == KeyCode.A){
moveLeft = true;
}
if(event.getCode() == KeyCode.W){
PressJump = true;
}
if(event.getCode() == KeyCode.S){
down = true;
}
}
public void onKeyBoardReleased(KeyEvent event){
if(event.getCode() == KeyCode.D){
moveRight = false;
}
if(event.getCode() == KeyCode.A){
moveLeft = false;
}
if(event.getCode() == KeyCode.W){
PressJump = false;
}
if(event.getCode() == KeyCode.S){
down = false;
}
}
public void move(){
if(moveLeft == true){
head.xProperty().bind(this.xProperty().add(-7));
hand.xProperty().bind(this.xProperty().add(-5));
this.direction = 0;//向左
body.setScaleX(-1);//设置翻转
moveX(-this.getSpeed());
}
if(PressJump == true && jump == false &&isOnPlat == true){
this.jumpTimeline();
PressJump = false;
isOnPlat = false;
}
if(moveRight == true){
head.xProperty().bind(this.xProperty().add(14));
hand.xProperty().bind(this.xProperty().add(25));
this.direction = 1;//向右
body.setScaleX(1);
moveX(this.getSpeed());
}
if(down == true && isOnPlat==true){
moveY(1);
down = false;
}
}
public static void dropOutTimeline(ArrayList<BaseRectangle> brArrayList){
System.out.println("---------------");
Timeline timeline;
timeline = new Timeline();
timeline.setCycleCount(-1);//无限循环
KeyFrame keyFrame = new KeyFrame(Duration.millis(4), new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {//重复执行该方法
// System.out.println("--------0-------");
// System.out.println("---------1------");
if(ObjectCollections.Man2DropJudge(brArrayList)&&!Man2.jump){
System.out.println("碰撞到平台了!!!!!!!!!");
Man2.isOnPlat = true;
// dropSpeed = 0;
}
else{
Man2.isOnPlat = false;
BaseScene.man2.moveY(dropSpeed);
}
}
});
timeline.getKeyFrames().add(keyFrame);//为时间轴添加栈帧
timeline.play();//时间轴循环执行每个栈帧
}
public void jumpTimeline(){
jump = true;
isOnPlat = false;
Timeline timeline;
timeline = new Timeline();
timeline.setCycleCount(90);//无限循环
System.out.println("&&&&&&&执行了jump_timeline!!!!!");
high = 3;//初始高度为3
KeyFrame keyFrame = new KeyFrame(Duration.millis(4), new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
//重复执行该方法
// if(high >= 0.1 ){
// high -= 0.01;
System.out.println("&&&&&&&跳跃了!!!!!!!!!!");
man2.moveY(-high);
// }
}
});
timeline.getKeyFrames().add(keyFrame);//为时间轴添加栈帧
timeline.play();//时间轴循环执行每个栈帧
jump = false;
}
}
ObjectCollections
package JavaBigJob;
import java.util.ArrayList;
import java.util.List;
import static JavaBigJob.BaseScene.man1;
import static JavaBigJob.BaseScene.man2;
public class ObjectCollections {
public static Boolean Man1DropJudge(ArrayList<BaseRectangle> brArrayList){
if(brArrayList==null){
System.out.println("平台集合中没有元素");
return false;
}
for(BaseRectangle br: brArrayList){
if(man1.getyProperty()+Man.height==br.getyProperty()&&(man1.getxProperty()<=br.getxProperty()+br.getWidth())&&man1.getxProperty()+Man.width>=br.getxProperty()){
return true;
}
// System.out.println(man1.getHeightProperty()+" "+man1.getxProperty()+" "+br.getxProperty()+" "+br.getxProperty()+br.getWidth());
}
return false;
}
public static Boolean Man2DropJudge(ArrayList<BaseRectangle> brArrayList){
if(brArrayList==null){
System.out.println("平台集合中没有元素");
return false;
}
for(BaseRectangle br: brArrayList){
if(man2.getyProperty()+Man2.height==br.getyProperty()&&(man2.getxProperty()<=br.getxProperty()+br.getWidth())&&man2.getxProperty()+Man2.width>=br.getxProperty()){
return true;
}
// System.out.println(man2.getHeightProperty()+" "+man2.getxProperty()+" "+br.getxProperty()+" "+br.getxProperty()+br.getWidth());
}
return false;
}
}
ReplaceClothes
package JavaBigJob;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Border;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.util.ArrayList;
public class ReplaceClothes extends Parent {
Image image = new Image("JavaBigJob/Picture/207.png");
Image HeadImage = new Image("JavaBigJob/Picture/227.png");
Image FootImage = new Image("JavaBigJob/Picture/178.png");
Image HandImage = new Image("JavaBigJob/Picture/670.png");
Image BodyImage = new Image("JavaBigJob/Picture/195.png");
Image Man2ClothesImage = new Image("JavaBigJob/Picture/207.png");
Image Man2headImage = new Image("JavaBigJob/Picture/230.png");
Image Man2footImage = new Image("JavaBigJob/Picture/182.png");
Image Man2BodyImage = new Image("JavaBigJob/Picture/200.png");
Image Man2HandImage = new Image("JavaBigJob/Picture/104.png");
ImageView imageView;
ImageView HeadImageView;
ImageView FootImageView;
ImageView HandImageView;
ImageView BodyImageView;
ImageView Man2ClothesImageView;
ImageView Man2headImageView;
ImageView Man2footImageView;
ImageView Man2BodyImageView;
ImageView Man2HandImageView;
int i = 207;
int HeadCount = 227;
int FootCount = 178;
int HandCount = 670;
int BodyCount = 195;
int Man2ClothesCount = 207;
int Man2HeadCount = 227;
int Man2FootCount = 178;
int Man2HandCount = 670;
int Man2BodyCount = 195;
public void Photo(){//循环展示衣服图片
if(i==225){
i=207;
}
if(i==222){
i++;
}
image = new Image("JavaBigJob/Picture/"+i+".png");
imageView = new ImageView(image);
i++;
}
public void Man2Photo(){//循环展示衣服图片
if(Man2ClothesCount==225){
Man2ClothesCount=207;
}
if(Man2ClothesCount==222){
Man2ClothesCount++;
}
Man2ClothesImage = new Image("JavaBigJob/Picture/"+Man2ClothesCount+".png");
Man2ClothesImageView = new ImageView(Man2ClothesImage);
Man2ClothesCount++;
}
public void BodyPhoto(){
if(BodyCount == 206){
BodyCount = 195;
}
BodyImage = new Image("JavaBigJob/Picture/"+BodyCount+".png");
BodyImageView = new ImageView(BodyImage);
BodyCount++;
}
public void Man2BodyPhoto(){
if(Man2BodyCount == 206){
Man2BodyCount = 195;
}
Man2BodyImage = new Image("JavaBigJob/Picture/"+Man2BodyCount+".png");
Man2BodyImageView = new ImageView(Man2BodyImage);
Man2BodyCount++;
}
public void HeadPhoto(){
if(HeadCount==238){
HeadCount = 227;
}
HeadImage = new Image("JavaBigJob/Picture/"+HeadCount+".png");
HeadImageView = new ImageView(HeadImage);
HeadCount++;
}
public void Man2HeadPhoto(){
if(Man2HeadCount==238){
Man2HeadCount = 227;
}
Man2headImage = new Image("JavaBigJob/Picture/"+Man2HeadCount+".png");
Man2headImageView = new ImageView(Man2headImage);
Man2HeadCount++;
}
public void FootPhoto(){
if(FootCount == 189)
FootCount = 178;
FootImage = new Image("JavaBigJob/Picture/"+FootCount+".png");
FootImageView = new ImageView(FootImage);
FootCount++;
}
public void Man2FootPhoto(){
if(Man2FootCount == 189)
Man2FootCount = 178;
Man2footImage = new Image("JavaBigJob/Picture/"+Man2FootCount+".png");
Man2footImageView = new ImageView(Man2footImage);
Man2FootCount++;
}
public void HandPhoto(){
if(HandCount == 679){
HandCount = 670;
}
HandImage = new Image("JavaBigJob/Picture/"+HandCount+".png");
HandImageView = new ImageView(HandImage);
HandCount++;
}
public void Man2HandPhoto(){
if(Man2HandCount == 679){
Man2HandCount = 670;
}
Man2HandImage = new Image("JavaBigJob/Picture/"+Man2HandCount+".png");
Man2HandImageView = new ImageView(Man2HandImage);
Man2HandCount++;
}
public ReplaceClothes() {
Button btnHat = new Button("更换头部");
Button btnBody = new Button("更换身体");
Button btnHand = new Button("更换手部");
Button btnClothes = new Button("更换衣服");
Button btnFoot = new Button("更换脚步");
Button btnOk = new Button("确定");
Button btnMan2Hat = new Button("更换头部2");
Button btnMan2Body = new Button("更换身体2");
Button btnMan2Hand = new Button("更换手部2");
Button btnMan2Clothes = new Button("更换衣服2");
Button btnMan2Foot = new Button("更换脚步2");
Button btnMan2Ok = new Button("确定2");
//css
GridPane gridPane = new GridPane();
gridPane.setPadding(new Insets(0,0,50,0));//与装他的容器的间距
gridPane.setAlignment(Pos.CENTER);
gridPane.add(btnClothes,3,10);
gridPane.add(btnBody,1,10);
gridPane.add(btnOk,2,11);
gridPane.add(btnHat,0,10);
gridPane.add(btnHand,2,10);
gridPane.add(btnFoot,4,10);
gridPane.add(btnMan2Clothes,3,12);
gridPane.add(btnMan2Body,2,12);
gridPane.add(btnMan2Ok,2,13);
gridPane.add(btnMan2Hat,0,12);
gridPane.add(btnMan2Hand,1,12);
gridPane.add(btnMan2Foot,4,12);
gridPane.setHgap(10);//设置表格单元间的横向间距
gridPane.setVgap(20);//设置表格单元间的纵向间距
gridPane.setId("gridPane");
btnClothes.setOnAction(e->{
Photo();//调用方法将imageVIEW赋值
gridPane.add(imageView,1,8); //将图片显示到界面上
});
btnMan2Clothes.setOnAction(e->{
Man2Photo();
gridPane.add(Man2ClothesImageView,2,8);
});
btnHand.setOnAction(e->{
HandPhoto();
gridPane.add(HandImageView,1,6);
});
btnMan2Hand.setOnAction(e->{
Man2HandPhoto();
gridPane.add(Man2HandImageView,2,6);
});
btnHat.setOnAction(e->{
HeadPhoto();
gridPane.add(HeadImageView,1,4);
});
btnMan2Hat.setOnAction(e->{
Man2HeadPhoto();
gridPane.add(Man2headImageView,2,4);
});
btnFoot.setOnAction(e->{
FootPhoto();
gridPane.add(FootImageView,1,9);
});
btnMan2Foot.setOnAction(e->{
Man2FootPhoto();
gridPane.add(Man2footImageView,2,9);
});
btnOk.setOnAction(e->{
BaseScene.man1.clothes.setImage(image); //更换衣服
BaseScene.man1.head.setImage(HeadImage);
BaseScene.man1.hand.setImage(HandImage);
BaseScene.man1.foot1.setImage(FootImage);
BaseScene.man1.foot2.setImage(FootImage);
BaseScene.man1.body.setImage(BodyImage);
// gridPane.add(BaseScene.man1,3,1);
});
btnMan2Ok.setOnAction(e->{
BaseScene.man2.clothes.setImage(Man2ClothesImage); //更换衣服
BaseScene.man2.head.setImage(Man2headImage);
BaseScene.man2.hand.setImage(Man2HandImage);
BaseScene.man2.foot1.setImage(Man2footImage);
BaseScene.man2.foot2.setImage(Man2footImage);
BaseScene.man2.body.setImage(Man2BodyImage);
});
btnBody.setOnAction(e->{
BodyPhoto();
gridPane.add(BodyImageView,1,5);
});
btnMan2Body.setOnAction(e->{
Man2BodyPhoto();
gridPane.add(Man2BodyImageView,2,5);
});
Scene scene = new Scene(gridPane,1000,600);
scene.getStylesheets().add(ReplaceClothes.class.getResource("replaceClothes.css").toExternalForm());
Stage primaryStage = new Stage();
primaryStage.setTitle("换装");
primaryStage.setScene(scene);
primaryStage.show();
}
public Image GetImage(){
return this.image;
}
}
换装页面由于用的是gridpane所以css样式不同于正常的节点,我这边加了没有显示。如果想加的话去看官方文档吧。
这边是jar包。
图片资源想要的话私信发下邮箱,看缘分发。主要是提供代码,提供思路。因为算法实现还蛮难的。
数据库设计如下。
登陆界面流程图就是用powerdesigner画的
要注意的点其实很多,不过注释写的很清楚,多看应该还是可以看懂的。