游戲效果



類關系如下

不用看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 onm ouse(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畫的

要注意的點其實很多,不過注釋寫的很清楚,多看應該還是可以看懂的,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/235577.html
標籤:其他
上一篇:演算法實作自動掃雷游戲
