Java-FX How to set image in a GridPane on click - game Othello - image

Hi i'm trying to click on a position (from 0 to 7) in my GridPane.
I would set an image inside it. I tryed everything but i can't see any improvement...
This is my board
Here my code on click on grid
#FXML
private void clickGrid(MouseEvent event) {
myGrid = new GridPane();
black = new Image("othello/images/black.png");
white = new Image("othello/images/white.png");
empty = new Image("othello/images/empty.png");
Node source = (Node)event.getSource() ;
Integer colIndex = GridPane.getColumnIndex(source);
Integer rowIndex = GridPane.getRowIndex(source);
System.out.printf("Mouse clicked cell [%d, %d]%n", colIndex.intValue(), rowIndex.intValue());
myGrid.add(new ImageView(white), colIndex, rowIndex);
}
Here my code when i click restart
#FXML
private void restartGame(ActionEvent event)throws Exception{
myGrid = new GridPane();
black = new Image("othello/images/black.png");
white = new Image("othello/images/white.png");
empty = new Image("othello/images/empty.png");
for (int i = 0; i < 8; i++){ //Per righe
for (int j = 0; j < 8; j++){ // Per colonne
myGrid.add(new ImageView(empty), i, j);
}
}
myGrid.add(new ImageView(black), 3, 3);
myGrid.add(new ImageView(black), 4, 3);
myGrid.add(new ImageView(white), 4, 4);
myGrid.add(new ImageView(white), 4, 3);
}
black is my piece colored of black, for white is white.
Source path
I have main project in src of netbeans.
Inside it, i have:
- othello (it contains my main)
- othello.images (it cointains all my image also backgrounds)
- othello.view (it contains my FXML files)
- othello.model (now nothing)
- othello.controller (it contains the controllers about the fxml files)

I think you don't see new images because you add to a new Grid, not to the existent one:
myGrid = new GridPane(); // !!! here a problem
myGrid.add(new ImageView(white), colIndex, rowIndex);

Don't create a new GridPane on every click:
myGrid = new GridPane(); // delete this
delete this line, and add an image to the GridPane you've prepared in FXML

Related

adding images to a gridpane javafx

I'm adding a list of images from a directory using an arraylist.When images are added,my ScrollPane gets crowded.How can I keep spacings between images ?
here's my code
File file = new File("D:\\SERVER\\Server Content\\Apps\\icons");
File[] filelist1 = file.listFiles();
ArrayList<File> filelist2 = new ArrayList<>();
hb = new HBox();
for (File file1 : filelist1) {
filelist2.add(file1);
}
System.out.println(filelist2.size());
gridpane.setPadding(new Insets(50,50,50,50));
gridpane.setHgap(20);
gridpane.setVgap(20);
int imageCol = 0;
int imageRow = 0;
for (int i = 0; i < filelist2.size(); i++) {
System.out.println(filelist2.get(i).getName());
image = new Image(filelist2.get(i).toURI().toString());
pic = new ImageView();
pic.setFitWidth(130);
pic.setFitHeight(130);
pic.setImage(image);
hb.getChildren().add(pic);
gridpane.add(pic, imageCol, imageRow );
imageCol++;
// To check if all the 4 images of a row are completed
if(imageCol > 2){
// Reset Column
imageCol=0;
// Next Row
imageRow++;
}
Try using HBox and VBox.
Basically, they are like little containers where you store your stuff and you can add gaps into it!
HBox ab = new HBox(10); <--The 10 is adding space (Answer to your question)
If you want to add stuff into HBox, simply write
ab.getChildren().addAll(your content here);

JavaFX Tree View Height According to Content

I have number of treeviews in VBox.
I want treeview to take a height based on the number of nodes visible.
Say if I collapse root node of treeview then height of that treeview should also change.
In the case of this image Item0 shows 7 subitems and then there is a lot of whitespace and then Item1 starts, Item1 is collapsed now , so Item2 should start immediately below Item1.
Please tell me what is the right way of doing this.
Try this
#Override
public void start(Stage primaryStage) {
VBox treeContainer = new VBox();
TreeItem<String> hiddenRootItem = new TreeItem<String>();
TreeView<String> tree = new TreeView<String>(hiddenRootItem);
tree.setShowRoot(false);
treeContainer.getChildren().add(tree);
for (int j = 0; j < 3; ++j) {
TreeItem<String> rootItem = new TreeItem<String>("Item " + j);
rootItem.setExpanded(true);
String[] names = {"SubItem1", "SubItem2", "SubItem3", "SubItem4", "SubItem5", "SubItem6", "SubItem7",};
for (int i = 0; i < names.length; i++) {
TreeItem<String> item = new TreeItem<String>(names[i]);
rootItem.getChildren().add(item);
}
hiddenRootItem.getChildren().add(rootItem);
}
StackPane root = new StackPane();
root.getChildren().add(treeContainer);
Scene scene = new Scene(root, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}

How to create grid border?

This is what I'm trying to create. White areas are System.Windows.Shapes.Rectangle in a Grid. This is my code for creating grid, columns, rows and rectangles;
Grid newGrid = new Grid();
for(int r=0; r<10; r++ ) {
newGrid.RowDefinitions.Add(
new RowDefinition { Height = new GridLength(30) });
for( int c=0; c<10; c++ ) {
newGrid.ColumnDefinitions.Add(
new ColumnDefinition { Width = new GridLength(30) });
Rectangle rec = new Rectangle{
Fill = new SolidColorBrush(Colors.White)
};
Grid.SetColumn(rec, c);
Grid.SetRow(rec, r);
newGrid.Children.Add(rec);
}
}
LayoutRoot.Children.Add(newGrid);
But I have not any idea how can add borders as we can see in picture. Thanks for suggestions.
Try
SolidColorBrush blackBrush = new SolidColorBrush();
blackBrush.Color = Colors.Black;
rec.Stroke = blackBrush;

javafx 2.1 how to save an Image after modification using ColorAdjust

I just make a class that let me modify an image like saturation brightness, contrast and hue using ColorAjust Class.
But i dont know how to save that image after making those modifications.
Here is the code:
final Stage imageProcessStage = new Stage();
imageProcessStage.initModality(Modality.APPLICATION_MODAL);
imageImageProcess = new Image(ImagePathImageProcess);
imageViewImageProcess = ImageViewBuilder.create().image(imageImageProcess).build();
ColorAdjust colorAdjust = ColorAdjustBuilder.create().build();
imageViewImageProcess.setEffect(colorAdjust);
//
Label saturationLabel = LabelBuilder.create().text("Saturation").build();
GridPane.setConstraints(saturationLabel, 0, 0);
Slider saturationSlider = SliderBuilder.create().value(50).build();
colorAdjust.saturationProperty().bind(saturationSlider.valueProperty().divide(50).subtract(1));
GridPane.setConstraints(saturationSlider, 1, 0);
GridPane.setHgrow(saturationSlider, Priority.ALWAYS);
Label saturationValueLabel = LabelBuilder.create().minWidth(75).maxWidth(75).build();
saturationValueLabel.textProperty().bind(colorAdjust.saturationProperty().multiply(100).asString("%.2f%%"));
GridPane.setConstraints(saturationValueLabel, 2, 0);
//
Label hueLabel = LabelBuilder.create().text("Hue").build();
GridPane.setConstraints(hueLabel, 0, 1);
Slider hueSlider = SliderBuilder.create().value(50).build();
colorAdjust.hueProperty().bind(hueSlider.valueProperty().divide(50).subtract(1));
GridPane.setConstraints(hueSlider, 1, 1);
GridPane.setHgrow(hueSlider, Priority.ALWAYS);
Label hueValueLabel = LabelBuilder.create().minWidth(75).maxWidth(75).build();
hueValueLabel.textProperty().bind(colorAdjust.hueProperty().multiply(100).asString("%.2f%%"));
GridPane.setConstraints(hueValueLabel, 2, 1);
//
Label brightnessLabel = LabelBuilder.create().text("Brightness").build();
GridPane.setConstraints(brightnessLabel, 0, 2);
Slider brightnessSlider = SliderBuilder.create().value(50).build();
colorAdjust.brightnessProperty().bind(brightnessSlider.valueProperty().divide(50).subtract(1));
GridPane.setConstraints(brightnessSlider, 1, 2);
GridPane.setHgrow(brightnessSlider, Priority.ALWAYS);
Label brightnessValueLabel = LabelBuilder.create().minWidth(75).maxWidth(75).build();
brightnessValueLabel.textProperty().bind(colorAdjust.brightnessProperty().multiply(100).asString("%.2f%%"));
GridPane.setConstraints(brightnessValueLabel, 2, 2);
//
Label contrastLabel = LabelBuilder.create().text("Contrast").build();
GridPane.setConstraints(contrastLabel, 0, 3);
Slider contrastSlider = SliderBuilder.create().value(50).build();
colorAdjust.contrastProperty().bind(contrastSlider.valueProperty().divide(50).subtract(1));
GridPane.setConstraints(contrastSlider, 1, 3);
GridPane.setHgrow(contrastSlider, Priority.ALWAYS);
Label contrastValueLabel = LabelBuilder.create().minWidth(75).maxWidth(75).build();
contrastValueLabel.textProperty().bind(colorAdjust.contrastProperty().multiply(100).asString("%.2f%%"));
GridPane.setConstraints(contrastValueLabel, 2, 3);
//Validate Button
Button btnValider = new Button("Valider");
btnValider.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
// SAVE IMAGE HERE
}
});
Upgrade to JavaFX 2.2 and use the following code in your button event handler.
ImageIO.write(
SwingFXUtils.fromFXImage(
imageViewImageProcess.snapshot(null, null), null
),
"png",
new File("valider.png")
);
Note that the 2.2 is currently in developer preview rather than a GA product, so you may encounter some issues and bugs until the new 2.2 methods have been thoroughly QAed.
Here is a complete, executable example: https://gist.github.com/2870355
A general purpose and simpler version of the code:
private void saveImage(
Image i,
String extension,
String pathname)
throws IOException {
ImageIO.write(
SwingFXUtils.fromFXImage(
image,
null),
extension,
new File(
pathname));
}

How to create PowePoint presentation in OpenXML format with Apache Poi and XSLF?

If I go to Apache POI XSLF there should be samples for both OLE2 and OpenXML specs, but there are only the OLE2 based Horrible Slide Layout Format examples.
Could please anybody help me out with XML Slide Layout Format example ? The API is quite different.
It is not like with spreadsheet where one just change the implementation of HSSFWorkbook to XSSFWorkbook.
How would this look like with XSLF implementation ? POI apparently can't create a document from scratch, so we need an existing empty dummy document, right ?
//table data
String[][] data = {
{"INPUT FILE", "NUMBER OF RECORDS"},
{"Item File", "11,559"},
{"Vendor File", "300"},
{"Purchase History File", "10,000"},
{"Total # of requisitions", "10,200,038"}
};
SlideShow ppt = new SlideShow();
Slide slide = ppt.createSlide();
//create a table of 5 rows and 2 columns
Table table = new Table(5, 2);
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[i].length; j++) {
TableCell cell = table.getCell(i, j);
cell.setText(data[i][j]);
RichTextRun rt = cell.getTextRun().getRichTextRuns()[0];
rt.setFontName("Arial");
rt.setFontSize(10);
cell.setVerticalAlignment(TextBox.AnchorMiddle);
cell.setHorizontalAlignment(TextBox.AlignCenter);
}
}
//set table borders
Line border = table.createBorder();
border.setLineColor(Color.black);
border.setLineWidth(1.0);
table.setAllBorders(border);
//set width of the 1st column
table.setColumnWidth(0, 300);
//set width of the 2nd column
table.setColumnWidth(1, 150);
slide.addShape(table);
table.moveTo(100, 100);
FileOutputStream out = new FileOutputStream(file);
ppt.write(out);
out.close();
It is not implemented yet, org.apache.poi version 3.8-beta3, when it will be implemented is very unknown to me.
XMLSlideShow.java
public MasterSheet createMasterSheet() throws IOException {
throw new IllegalStateException("Not implemented yet!");
}
public Slide createSlide() throws IOException {
throw new IllegalStateException("Not implemented yet!");
}

Resources