From 02370ee10a6f81097b1715dc6be58604853a410b Mon Sep 17 00:00:00 2001 From: cemal Date: Wed, 4 May 2022 11:26:09 +0200 Subject: [PATCH] improvments to drag n drop in progess --- .../application/LaticeApplicationWindow.java | 168 +++++++++--------- src/main/java/latice/model/Rack.java | 7 +- 2 files changed, 92 insertions(+), 83 deletions(-) diff --git a/src/main/java/latice/application/LaticeApplicationWindow.java b/src/main/java/latice/application/LaticeApplicationWindow.java index 9517c0f..ea9b886 100644 --- a/src/main/java/latice/application/LaticeApplicationWindow.java +++ b/src/main/java/latice/application/LaticeApplicationWindow.java @@ -43,6 +43,7 @@ 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 listTileImage = Rack.getRackTileImage(); @@ -81,18 +82,22 @@ public class LaticeApplicationWindow extends Application{ Rectangle r[] = new Rectangle[81]; + int counter = 0; for (int i=1; i<10 ; i++) { for (int j=1; j < 10 ; j++) { - r[i] = new Rectangle(i*52+336,j*52-17,50,50); - r[i].setFill(realColor.TRANSPARENT); - r[i].setStroke(realColor.BLACK); + + 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[i]); - System.out.println(r[i]); + pane.getChildren().add(r[counter]); + System.out.println(r[counter]); + counter++; } } root.setCenter(pane); + System.out.println(r); @@ -127,89 +132,88 @@ public class LaticeApplicationWindow extends Application{ System.out.println("-----------------"); //deck.displayListTile(); - ArrayList listRackTile = Rack.getListRackTile(); - - Text rackTile1 = new Text(); - rackTile1.setText(listRackTile.get(0).getShape().toString() + listRackTile.get(0).getColor().toString()); - Text rackTile2 = new Text(); - rackTile2.setText(listRackTile.get(1).getShape().toString() + listRackTile.get(1).getColor().toString()); - Text rackTile3 = new Text(); - rackTile3.setText(listRackTile.get(2).getShape().toString() + listRackTile.get(2).getColor().toString()); - Text rackTile4 = new Text(); - rackTile4.setText(listRackTile.get(3).getShape().toString() + listRackTile.get(3).getColor().toString()); - Text rackTile5 = new Text(); - rackTile5.setText(listRackTile.get(4).getShape().toString() + listRackTile.get(4).getColor().toString()); - - - rackTile1.setOnDragDetected(new EventHandler() { - - @Override - public void handle(MouseEvent arg0) { - Dragboard dragboard = rackTile1.startDragAndDrop(TransferMode.ANY); - ClipboardContent content = new ClipboardContent(); - content.putString("Hello !"); - dragboard.setContent(content); - arg0.consume(); - } - - }); - - ImagePattern imagePattern = new ImagePattern(image); - r[1].setOnDragEntered(new EventHandler() { - - @Override - public void handle(DragEvent arg0) { - if (arg0.getDragboard().hasString()){ - r[1].setFill(imagePattern); - } - arg0.consume(); - } - }); - - r[1].setOnDragExited(new EventHandler() { - - @Override - public void handle(DragEvent arg0) { - r[1].setFill(realColor.BLUE); - arg0.consume(); - } - - }); - - r[1].setOnDragDropped(new EventHandler() { - @Override - public void handle(DragEvent arg0) { - System.out.println("entered"); - Dragboard dragboard = arg0.getDragboard(); - boolean success = false; - r[1].setFill(imagePattern); - System.out.println("got files :" + dragboard.getFiles()); - if (dragboard.hasString()){ - success = true; - r[1].setFill(imagePattern); - } - arg0.setDropCompleted(success); - - arg0.consume(); - } - - }); - - rackBox.getChildren().addAll(rackTile1, rackTile2, rackTile3, rackTile4, rackTile5); - rackBox.setAlignment(Pos.CENTER); - root.setBottom(rackBox); - root.setPadding(new Insets(20,20,20,20)); - - - - //With Image + //With Image Rack rack2 = new Rack(deck); HBox rackImage = rack2.createImageTileOfRack(); // TODO Create the deck - + root.setBottom(rackImage); + ArrayList listRackTile = Rack.getListRackTile(); + + for (int i=0; i<5; i++) { + int a = i; + rackImage.getChildren().get(a).setOnDragDetected(new EventHandler() { + + @Override + public void handle(MouseEvent arg0) { + Dragboard dragboard = rackImage.getChildren().get(a).startDragAndDrop(TransferMode.ANY); + ClipboardContent content = new ClipboardContent(); + content.putString("Hello !"); + 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() { + + @Override + public void handle(DragEvent arg0) { + if (arg0.getDragboard().hasString()){ + Dragboard dragboard = arg0.getDragboard(); + + System.out.println("got files :" + dragboard.getFiles()); + r[a].setFill(imagePattern); + } + arg0.consume(); + } + }); + + r[a].setOnDragExited(new EventHandler() { + + @Override + public void handle(DragEvent arg0) { + if (arg0.isDropCompleted() == false) { + r[a].setFill(realColor.TRANSPARENT); + } + arg0.consume(); + } + + }); + + r[a].setOnDragOver(new EventHandler () { + @Override + public void handle(DragEvent arg0) { + arg0.acceptTransferModes(TransferMode.ANY); + arg0.consume(); + } + }); + + r[a].setOnDragDropped(new EventHandler() { + @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(); + } + + }); + } + + + + //-------------------------------------------------------------------------------------- Scene scene = new Scene(root, 1280, 720); diff --git a/src/main/java/latice/model/Rack.java b/src/main/java/latice/model/Rack.java index 44f7f1f..d55b768 100644 --- a/src/main/java/latice/model/Rack.java +++ b/src/main/java/latice/model/Rack.java @@ -11,6 +11,7 @@ import javafx.scene.text.Text; public class Rack { private static ArrayList listRackTile = new ArrayList(); + private static ArrayList rackTileImage = new ArrayList(); public Rack(Deck deck) { @@ -60,10 +61,10 @@ public class Rack { tile = (this.getListRackTile()).get(index); image = new Image(tile.getShape().getStringShape() + "_" + tile.getColor().getStringColor()+ ".png"); + rackTileImage.add(image); imageView = new ImageView(image); imageView.setFitHeight(80); imageView.setFitWidth(80); - rack.getChildren().add(imageView); } @@ -76,6 +77,10 @@ public class Rack { } + public static ArrayList getRackTileImage() { + return rackTileImage; + } +