英文:
How to load images on stage with EventHandler<MouseEvent>?
问题
import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.image.*;
import java.io.*;
import javafx.geometry.*;
import javafx.scene.Group;
public class background extends Application {
private class Tile extends StackPane {
private Text text = new Text();
public Tile() {
Rectangle border = new Rectangle(200, 200);
border.setFill(null);
border.setStroke(Color.BLACK);
text.setFill(Color.BLACK);
text.setFont(Font.font("Verdana", FontWeight.BOLD, 95));
setAlignment(Pos.CENTER);
getChildren().addAll(border, text);
}
private void drawX() {
text.setText("X");
}
private void drawO() {
text.setText("O");
}
}
private Parent createContent() {
Pane root = new Pane();
root.setPrefSize(600, 600);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
background.Tile tile = new background.Tile();
tile.setTranslateX(j * 200);
tile.setTranslateY(i * 200);
root.getChildren().add(tile);
}
}
return root;
}
// launch the application
public void start(Stage stage) {
try {
// set title for the stage
stage.setTitle("creating Background");
// create a label
Label label = new Label("Name : ");
// create a text field
TextField textfield = new TextField();
// set preferred column count
textfield.setPrefColumnCount(10);
// create a button
Button button = new Button("OK");
// add the label, text field and button
HBox hbox = new HBox(label, textfield, button);
// create a scene
Scene scene = new Scene(hbox, 600, 600);
// create a input stream
FileInputStream input = new FileInputStream("c:\\fill.png");
// create a image
Image image = new Image(input);
// create a background image
BackgroundImage backgroundimage = new BackgroundImage(image,
BackgroundRepeat.NO_REPEAT,
BackgroundRepeat.NO_REPEAT,
BackgroundPosition.DEFAULT,
BackgroundSize.DEFAULT);
// create Background
Background background = new Background(backgroundimage);
// set background
hbox.setBackground(background);
// set the scene
stage.setScene(scene);
stage.show();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
// Main Method
public static void main(String args[]) {
// launch the application
launch(args);
}
}
英文:
I have this piece of code. I want to add images. Here I have cross board which is divided on nine rectangles. Here is a image crossboard
|1|2|3|
|4|5|6|
|7|8|9|
I would like to load another image: X.png or O.png if I click on rectangle for example number 1 an image will be loaded.
How to do this in JavaFx? Is it possible?
import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.canvas.*;
import javafx.scene.web.*;
import javafx.scene.layout.*;
import javafx.scene.image.*;
import java.io.*;
import javafx.geometry.*;
import javafx.scene.Group;
public class background extends Application {
private class Tile extends StackPane {
private Text text = new Text();
public Tile() {
Rectangle border = new Rectangle(200, 200);
border.setFill(null);
border.setStroke(Color.BLACK);
text.setFill(Color.BLACK);
text.setFont(Font.font("Verdana", FontWeight.BOLD, 95));
setAlignment(Pos.CENTER);
getChildren().addAll(border, text);
}
private void drawX() {
text.setText("X");
}
private void drawO() {
text.setText("O");
}
}
private Parent createContent() {
Pane root = new Pane();
root.setPrefSize(600, 600);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
background.Tile tile = new background.Tile();
tile.setTranslateX(j * 200);
tile.setTranslateY(i * 200);
root.getChildren().add(tile);
}
}
return root;
}
// launch the application
public void start(Stage stage)
{
try {
// set title for the stage
stage.setTitle("creating Background");
// create a label
Label label = new Label("Name : ");
// create a text field
TextField textfield = new TextField();
// set preferred column count
textfield.setPrefColumnCount(10);
// create a button
Button button = new Button("OK");
// add the label, text field and button
HBox hbox = new HBox(label, textfield, button);
// set spacing
// hbox.setSpacing(10);
// set alignment for the HBox
/// hbox.setAlignment(Pos.CENTER);
// create a scene
Scene scene = new Scene(hbox, 600, 600);
// create a input stream
FileInputStream input = new FileInputStream("c:\\fill.png");
// create a image
Image image = new Image(input);
// create a background image
BackgroundImage backgroundimage = new BackgroundImage(image,
BackgroundRepeat.NO_REPEAT,
BackgroundRepeat.NO_REPEAT,
BackgroundPosition.DEFAULT,
BackgroundSize.DEFAULT);
// create Background
Background background = new Background(backgroundimage);
// set background
hbox.setBackground(background);
// set the scene
stage.setScene(scene);
stage.show();
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
// Main Method
public static void main(String args[])
{
// launch the application
launch(args);
}
}
答案1
得分: 0
一个很酷的事情是设置一个`GridPane`对象,并将其用于显示交叉板,然后更改内部对象的状态(一个按钮也可以,或者一个带有背景的简单窗格)以将其显示为圆圈或交叉。这确实是你需要实现它的内容。
[![在这里输入图片描述][1]][1]
在内部,你可以放置一个对象来监听点击,然后你将能够相当轻松地模拟井字游戏。
你可以使用一个内部变量来记忆游戏的当前状态。
试试吧。
[1]: https://i.stack.imgur.com/KK3Va.png
英文:
One cool thing you can do is to setup a GridPane
object and use it in order to display the cross-board, then change the state of the inner object (a button too is okay, or a simple pane with a background) to display it as a circle or a cross. It's really what you need to realise it.
Inside, you can then put an object which will listen for a click and you will be able to emulate the tic-tac-toe game pretty easily.
You can use an internal variable to keep in memory the current state of the game.
Give it a shoot.
专注分享java语言的经验与见解,让所有开发者获益!
评论