I found a bug from writing a simple chess program. Sometimes,when a chess piece
(ImageView) move through a chessboard line(Line), the line will not refresh.
package chess;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.Light;
import javafx.scene.effect.Lighting;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;
public class Fanorona extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Group root = new Group();
Scene scene = new Scene(root, 900, 500);
FanoronaPane fanoronaPane = new FanoronaPane();
fanoronaPane.setLayoutX(50);
fanoronaPane.setLayoutY(50);
root.getChildren().add(fanoronaPane);
primaryStage.setScene(scene);
primaryStage.show();
}
private class FanoronaPane extends Pane {
private ChessPoint[][] chessPoint = new ChessPoint[9][5];
private Line[] horizentalLine = new Line[5];
private Line[] verticalLine = new Line[9];
private Line[] leftObliqueLine = new Line[5];
private Line[] rightObliqueLine = new Line[5];
private Chess[] blackChess = new Chess[45];
public FanoronaPane() {
this.setPrefSize(800, 400);
Light.Distant light = new Light.Distant();
light.setAzimuth(-135);
Lighting lighting = new Lighting();
lighting.setDiffuseConstant(10);
lighting.setLight(light);
lighting.setSurfaceScale(5.0);
DropShadow ds = new DropShadow();
ds.setInput(lighting);
ds.setRadius(30);
Rectangle rect = new Rectangle(800, 400, Color.ORANGE);
rect.setEffect(ds);
this.getChildren().add(rect);
for(int i=0;i<9;i++){
for(int j=0;j<5;j++){
chessPoint[i][j] = new ChessPoint(i,j);
}
}
for(int i=0;i<5;i++){
horizentalLine[i] = new Line(chessPoint[0][i].posX,chessPoint[0][i].posY,chessPoint[8][i].posX,chessPoint[8][i].posY);
this.getChildren().add(horizentalLine[i]);
}
for(int i=0;i<9;i++){
verticalLine[i] = new Line(chessPoint[i][0].posX,chessPoint[i][0].posY,chessPoint[i][4].posX,chessPoint[i][4].posY);
this.getChildren().add(verticalLine[i]);
}
leftObliqueLine[0] = new Line(chessPoint[0][2].posX, chessPoint[0][2].posY, chessPoint[2][0].posX, chessPoint[2][0].posY);
rightObliqueLine[0] = new Line(chessPoint[0][2].posX, chessPoint[0][2].posY, chessPoint[2][4].posX, chessPoint[2][4].posY);
this.getChildren().addAll(leftObliqueLine[0],rightObliqueLine[0]);
for(int i=0;i<3;i++){
leftObliqueLine[i+1] = new Line(chessPoint[i*2+4][0].posX, chessPoint[i*2+4][0].posY, chessPoint[i*2][4].posX, chessPoint[i*2][4].posY);
rightObliqueLine[i+1] = new Line(chessPoint[i*2][0].posX, chessPoint[i*2][0].posY, chessPoint[i*2+4][4].posX, chessPoint[i*2+4][4].posY);
this.getChildren().addAll(leftObliqueLine[i+1],rightObliqueLine[i+1]);
}
leftObliqueLine[4] = new Line(chessPoint[8][2].posX, chessPoint[8][2].posY, chessPoint[6][4].posX, chessPoint[6][4].posY);
rightObliqueLine[4] = new Line(chessPoint[8][2].posX, chessPoint[8][2].posY, chessPoint[6][0].posX, chessPoint[6][0].posY);
this.getChildren().addAll(leftObliqueLine[4],rightObliqueLine[4]);
blackChess[0] = new Chess(1,1,0);
final TranslateTransition tt = new TranslateTransition(Duration.seconds(1), blackChess[0]);
tt.setByX(100);
System.out.println(blackChess[0].getTranslateX());
blackChess[0].setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
tt.play();
}
});
this.getChildren().add(blackChess[0]);
}
private class ChessPoint {
public int chessX;
public int chessY;
public double posX;
public double posY;
public ChessPoint(int X, int Y) {
this.chessX = X;
this.chessY = Y;
this.posX = chessX * 100;
this.posY = chessY * 100;
}
}
private class Chess extends ImageView{
private int chessX;
private int chessY;
public Chess(int X,int Y,int icon) {
this.chessX = X;
this.chessY = Y;
this.setCache(true);
this.setSmooth(true);
switch(icon){
case 0:this.setImage(new Image("file:ChessImage/smile.png"));break;
}
Light.Distant light = new Light.Distant();
light.setAzimuth(-135);
Lighting lighting = new Lighting();
lighting.setDiffuseConstant(10);
lighting.setLight(light);
lighting.setSurfaceScale(3.0);
DropShadow ds = new DropShadow();
ds.setInput(lighting);
ds.setRadius(1);
this.setEffect(ds);
this.setSmooth(true);
this.setFitWidth(80);
this.setPreserveRatio(true);
this.setTranslateX(-40+chessX*100);
this.setTranslateY(-40+chessY*100);
}
}
}
}
(ImageView) move through a chessboard line(Line), the line will not refresh.
package chess;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.Light;
import javafx.scene.effect.Lighting;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;
public class Fanorona extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Group root = new Group();
Scene scene = new Scene(root, 900, 500);
FanoronaPane fanoronaPane = new FanoronaPane();
fanoronaPane.setLayoutX(50);
fanoronaPane.setLayoutY(50);
root.getChildren().add(fanoronaPane);
primaryStage.setScene(scene);
primaryStage.show();
}
private class FanoronaPane extends Pane {
private ChessPoint[][] chessPoint = new ChessPoint[9][5];
private Line[] horizentalLine = new Line[5];
private Line[] verticalLine = new Line[9];
private Line[] leftObliqueLine = new Line[5];
private Line[] rightObliqueLine = new Line[5];
private Chess[] blackChess = new Chess[45];
public FanoronaPane() {
this.setPrefSize(800, 400);
Light.Distant light = new Light.Distant();
light.setAzimuth(-135);
Lighting lighting = new Lighting();
lighting.setDiffuseConstant(10);
lighting.setLight(light);
lighting.setSurfaceScale(5.0);
DropShadow ds = new DropShadow();
ds.setInput(lighting);
ds.setRadius(30);
Rectangle rect = new Rectangle(800, 400, Color.ORANGE);
rect.setEffect(ds);
this.getChildren().add(rect);
for(int i=0;i<9;i++){
for(int j=0;j<5;j++){
chessPoint[i][j] = new ChessPoint(i,j);
}
}
for(int i=0;i<5;i++){
horizentalLine[i] = new Line(chessPoint[0][i].posX,chessPoint[0][i].posY,chessPoint[8][i].posX,chessPoint[8][i].posY);
this.getChildren().add(horizentalLine[i]);
}
for(int i=0;i<9;i++){
verticalLine[i] = new Line(chessPoint[i][0].posX,chessPoint[i][0].posY,chessPoint[i][4].posX,chessPoint[i][4].posY);
this.getChildren().add(verticalLine[i]);
}
leftObliqueLine[0] = new Line(chessPoint[0][2].posX, chessPoint[0][2].posY, chessPoint[2][0].posX, chessPoint[2][0].posY);
rightObliqueLine[0] = new Line(chessPoint[0][2].posX, chessPoint[0][2].posY, chessPoint[2][4].posX, chessPoint[2][4].posY);
this.getChildren().addAll(leftObliqueLine[0],rightObliqueLine[0]);
for(int i=0;i<3;i++){
leftObliqueLine[i+1] = new Line(chessPoint[i*2+4][0].posX, chessPoint[i*2+4][0].posY, chessPoint[i*2][4].posX, chessPoint[i*2][4].posY);
rightObliqueLine[i+1] = new Line(chessPoint[i*2][0].posX, chessPoint[i*2][0].posY, chessPoint[i*2+4][4].posX, chessPoint[i*2+4][4].posY);
this.getChildren().addAll(leftObliqueLine[i+1],rightObliqueLine[i+1]);
}
leftObliqueLine[4] = new Line(chessPoint[8][2].posX, chessPoint[8][2].posY, chessPoint[6][4].posX, chessPoint[6][4].posY);
rightObliqueLine[4] = new Line(chessPoint[8][2].posX, chessPoint[8][2].posY, chessPoint[6][0].posX, chessPoint[6][0].posY);
this.getChildren().addAll(leftObliqueLine[4],rightObliqueLine[4]);
blackChess[0] = new Chess(1,1,0);
final TranslateTransition tt = new TranslateTransition(Duration.seconds(1), blackChess[0]);
tt.setByX(100);
System.out.println(blackChess[0].getTranslateX());
blackChess[0].setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
tt.play();
}
});
this.getChildren().add(blackChess[0]);
}
private class ChessPoint {
public int chessX;
public int chessY;
public double posX;
public double posY;
public ChessPoint(int X, int Y) {
this.chessX = X;
this.chessY = Y;
this.posX = chessX * 100;
this.posY = chessY * 100;
}
}
private class Chess extends ImageView{
private int chessX;
private int chessY;
public Chess(int X,int Y,int icon) {
this.chessX = X;
this.chessY = Y;
this.setCache(true);
this.setSmooth(true);
switch(icon){
case 0:this.setImage(new Image("file:ChessImage/smile.png"));break;
}
Light.Distant light = new Light.Distant();
light.setAzimuth(-135);
Lighting lighting = new Lighting();
lighting.setDiffuseConstant(10);
lighting.setLight(light);
lighting.setSurfaceScale(3.0);
DropShadow ds = new DropShadow();
ds.setInput(lighting);
ds.setRadius(1);
this.setEffect(ds);
this.setSmooth(true);
this.setFitWidth(80);
this.setPreserveRatio(true);
this.setTranslateX(-40+chessX*100);
this.setTranslateY(-40+chessY*100);
}
}
}
}