bugFix and working version with drag&drop and 2 dimension array

master
Cemal Odabasioglu 2022-05-10 18:53:45 +02:00
parent 02370ee10a
commit 677c3aed6d
1 changed files with 107 additions and 73 deletions

View File

@ -1,6 +1,8 @@
package latice.application;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javafx.application.Application;
import javafx.event.EventHandler;
@ -43,9 +45,12 @@ public class LaticeApplicationWindow extends Application{
Tile blueBird = new Tile(Color.NAVYBLUE, Shape.BIRD);
Tile greenLeaf = new Tile(Color.GREEN, Shape.FEATHER);
Tile redFlower = new Tile(Color.RED, Shape.FLOWER);
ArrayList<Image> listTileImage = Rack.getRackTileImage();
ArrayList<Image> listTileImage = Rack.getRackTileImage();
ArrayList<Tile> listOfTile = new ArrayList<Tile>();
Map<Rectangle, Tile> assocRectangleTile = new HashMap<Rectangle, Tile>();
public static int indexTileClicked;
@ -57,23 +62,19 @@ public class LaticeApplicationWindow extends Application{
}
@Override
public void start(Stage primaryStage) throws Exception {
//root layout
BorderPane root = new BorderPane();
//Title
Text title = new Text("Latice");
title.setFont(new Font(30));
root.setTop(title);
root.setAlignment(title, Pos.CENTER);
//Image
//rectangle.setFill(realColor.TRANSPARENT);
Pane pane = new Pane();
BackgroundImage myBG= new BackgroundImage(image,
BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.CENTER,
@ -81,19 +82,23 @@ public class LaticeApplicationWindow extends Application{
pane.setBackground(new Background(myBG));
Rectangle r[] = new Rectangle[81];
int counter = 0;
//Creating rectangle for tiles placement
Rectangle[][] r = new Rectangle[9][9];
int counterI = 0;
int counterJ = 0;
for (int i=1; i<10 ; i++) {
for (int j=1; j < 10 ; j++) {
r[counter] = new Rectangle(i*52+358,j*52+3,50,50);
r[counter].setFill(realColor.TRANSPARENT);
r[counter].setStroke(realColor.BLACK);
pane.getChildren().add(r[counter]);
System.out.println(r[counter]);
counter++;
r[counterI][counterJ] = new Rectangle(i*52+358,j*52+3,50,50);
r[counterI][counterJ].setFill(realColor.TRANSPARENT);
pane.getChildren().add(r[counterI][counterJ]);
System.out.println(r[counterI][counterJ]);
System.out.println(counterJ);
counterJ++;
}
System.out.println(counterI);
counterJ = 0;
counterI++;
}
root.setCenter(pane);
@ -108,13 +113,9 @@ public class LaticeApplicationWindow extends Application{
rackBox.setSpacing(10);
rackBox.setPadding(new Insets(15,20, 10,10));
ArrayList<Tile> listOfTile = new ArrayList<Tile>();
System.out.println("Hello Latice !");
//Creating tiles
for (Color color : Color.values()) {
for (Shape shape : Shape.values()) {
Tile tile = new Tile(color, shape);
@ -135,13 +136,18 @@ public class LaticeApplicationWindow extends Application{
//With Image
Rack rack2 = new Rack(deck);
HBox rackImage = rack2.createImageTileOfRack(); // TODO Create the deck
HBox rackImage = rack2.createImageTileOfRack();
root.setBottom(rackImage);
ArrayList<Tile> listRackTile = Rack.getListRackTile();
//------------------------------------------------------------------------
//Setting OnDragDetected on tiles
for (int i=0; i<5; i++) {
int a = i;
rackImage.getChildren().get(a).setOnDragDetected(new EventHandler<MouseEvent>() {
@ -150,67 +156,77 @@ public class LaticeApplicationWindow extends Application{
public void handle(MouseEvent arg0) {
Dragboard dragboard = rackImage.getChildren().get(a).startDragAndDrop(TransferMode.ANY);
ClipboardContent content = new ClipboardContent();
dragboard.setDragView(listTileImage.get(a));
content.putString("Hello !");
setIndexTileClicked(a);
dragboard.setContent(content);
arg0.consume();
}
});
}
ImagePattern imagePattern = new ImagePattern(listTileImage.get(0));
for(int i=1; i<81; i++) {
int a = i;
System.out.println(a);
r[a].setOnDragEntered(new EventHandler<DragEvent>() {
@Override
public void handle(DragEvent arg0) {
if (arg0.getDragboard().hasString()){
System.out.println((indexTileClicked));
ImagePattern imagePattern = new ImagePattern(listTileImage.get(getIndexTileClicked()));
//Setting drag & drop on rectangles
for(int i=0; i<9; i++) {
for(int j=0; j<9; j++) {
int a = i;
int b = j;
r[a][b].setOnDragEntered(new EventHandler<DragEvent>() {
@Override
public void handle(DragEvent arg0) {
if (arg0.getDragboard().hasString()){
Dragboard dragboard = arg0.getDragboard();
System.out.println("got files :" + dragboard.getFiles());
r[a][b].setFill(new ImagePattern(listTileImage.get(getIndexTileClicked())));
}
arg0.consume();
}
});
r[a][b].setOnDragExited(new EventHandler<DragEvent>() {
@Override
public void handle(DragEvent arg0) {
if (arg0.isDropCompleted() == false) {
r[a][b].setFill(realColor.TRANSPARENT);
}
arg0.consume();
}
});
r[a][b].setOnDragOver(new EventHandler <DragEvent>() {
@Override
public void handle(DragEvent arg0) {
arg0.acceptTransferModes(TransferMode.ANY);
arg0.consume();
}
});
r[a][b].setOnDragDropped(new EventHandler<DragEvent>() {
@Override
public void handle(DragEvent arg0) {
System.out.println("entered");
Dragboard dragboard = arg0.getDragboard();
System.out.println("got files :" + dragboard.getFiles());
r[a].setFill(imagePattern);
r[a][b].setFill(new ImagePattern(listTileImage.get(getIndexTileClicked())));
arg0.setDropCompleted(true);
assocRectangleTile.put(r[a][b], listRackTile.get(getIndexTileClicked()));
System.out.println(assocRectangleTile.toString());
arg0.consume();
}
arg0.consume();
}
});
r[a].setOnDragExited(new EventHandler<DragEvent>() {
@Override
public void handle(DragEvent arg0) {
if (arg0.isDropCompleted() == false) {
r[a].setFill(realColor.TRANSPARENT);
}
arg0.consume();
}
});
r[a].setOnDragOver(new EventHandler <DragEvent>() {
@Override
public void handle(DragEvent arg0) {
arg0.acceptTransferModes(TransferMode.ANY);
arg0.consume();
}
});
r[a].setOnDragDropped(new EventHandler<DragEvent>() {
@Override
public void handle(DragEvent arg0) {
System.out.println("entered");
Dragboard dragboard = arg0.getDragboard();
System.out.println("got files :" + dragboard.getFiles());
r[a].setFill(imagePattern);
arg0.setDropCompleted(true);
arg0.consume();
}
});
}
});
}
}
@ -218,11 +234,29 @@ public class LaticeApplicationWindow extends Application{
Scene scene = new Scene(root, 1280, 720);
//primaryStage.setResizable(false);
primaryStage.setResizable(false);
primaryStage.setTitle("Latice");
primaryStage.setScene(scene);
primaryStage.show();
}
//getter to get the index of the mouse clicked tile
public static int getIndexTileClicked() {
return indexTileClicked;
}
//Setter to set the mouse clicked tile
public static void setIndexTileClicked(int indexTileClicked) {
LaticeApplicationWindow.indexTileClicked = indexTileClicked;
}
}