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