Explicit positioning a Pane - java

I'm trying to create a basic UML diagramming tool with JavaFX.
First I did this:
public class Asd extends Application {
Double WINDOW_HEIGHT = (double) 400;
Double WINDOW_WIDTH = (double) 800;
Double RECTANGLE_DEFAULT_HEIGHT = (double) 50;
Double RECTANGLE_DEFAULT_WIDTH = (double) 100;
#Override
public void start(Stage stage) throws Exception {
final Pane root = new Pane();
Rectangle background = new Rectangle(WINDOW_WIDTH, WINDOW_HEIGHT,
Color.WHITE);
root.getChildren().add(background);
Scene scene = new Scene(root, WINDOW_WIDTH, WINDOW_HEIGHT);
// On window click
root.setOnMousePressed(new EventHandler<MouseEvent>() {
public void handle(MouseEvent mouseEvent) {
Rectangle rect = new Rectangle();
rect.setX(mouseEvent.getX());
rect.setY(mouseEvent.getY());
rect.setWidth(RECTANGLE_DEFAULT_WIDTH);
rect.setHeight(RECTANGLE_DEFAULT_HEIGHT);
rect.setFill(null);
rect.setStroke(Color.BLACK);
root.getChildren().add(rect);
}
});
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
With that, I can draw a rectangle (that would represent a UML Class) in the exact point I click.
Now I want to add text. If instead of
root.getChildren().add(rect);
I do
Text text = new Text("mockText");
StackPane stackPane = new StackPane();
stackPane.getChildren().addAll(rect, text);
root.getChildren().add(stackPane);
the text is placed in the center of the rectangle, but all rectangles are positioned at coordinates (0,0)
So, any way to change the position of each StackPane? Or any alternative to add text to the rectangles directly without using StackPane?

OK, got it. Layout elements have methods setLayoutX & setLayoutY that can be used in cases like this one, so I don't need anymore Rectangle.setX/setY & finally the handle method should be:
public void handle(MouseEvent mouseEvent) {
Rectangle rect = new Rectangle();
rect.setWidth(RECTANGLE_DEFAULT_WIDTH);
rect.setHeight(RECTANGLE_DEFAULT_HEIGHT);
rect.setFill(null);
rect.setStroke(Color.BLACK);
Text text = new Text("mockText");
StackPane stackPane = new StackPane();
stackPane.getChildren().addAll(rect, text);
stackPane.setLayoutX(mouseEvent.getX());
stackPane.setLayoutY(mouseEvent.getY());
root.getChildren().add(stackPane);
}

Related

Change exactly one label background color in JavaFx

I'm trying to create Tetris. I created GridPane and filled width and height with labels using nested for loop, then putted GridPane in to the scene. How can I change certain label background all labels have coordinates. I want to change label background because then I can make different tetrominoes for the game. Maybe there are better approaches to making tetrominoes in Tetris which I don't know. My code.
public class Menu extends Application {
private GridPane Grid = new GridPane(); //Layout
private Label label = new Label(); // Label
private int height = 600; private int width = 600; private int pixel = 30;
#Override
public void start(Stage Stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
Stage.setTitle("Tetris");
Grid.setGridLinesVisible(true);
Scene scene = new Scene(Grid, width, height); // Add Layout to scene
FillingLayoutWithLabels(width,height);
Stage.setScene(scene);
Stage.show();
}
private void FillingLayoutWithLabels(int width, int height) {
for(int i = 0;i< width/pixel ;i++){
for(int j = 0;j<height/pixel ; j++){
addLabel(i,j);
}
}
}
public void addLabel(int columnIndex, int rowIndex) {
Label label = new Label();
label.setMinWidth(pixel);
label.setMinHeight(pixel);
label.setBackground(new Background(new BackgroundFill(Color.RED, CornerRadii.EMPTY, Insets.EMPTY)));
label.setBorder(new Border(new BorderStroke(Color.BLACK, BorderStrokeStyle.SOLID, null, new BorderWidths(1))));
Grid.setColumnIndex(label, columnIndex);
Grid.setRowIndex(label, rowIndex);
label.setId(rowIndex+ " " + columnIndex);
Grid.getChildren().add(label);
}
}

For loop for program?

so I am writing a program about the monte carlous math problem and I am doing the visual side of it. I mostly have everything set, the only issue is that I can't put the pixels in a loop for some reason. Any help? I've already tried a for loop though.
public class Main extends Application {
#Override
public void start(Stage primaryStage)
{
int width = 1;
int random = ((int)Math.random()*400)+1;
// Create the Colors
Color white = Color.web("rgb(255,255,255)");
Color black = Color.web("rgb(0,0,0)");
//making the square
Rectangle square = new Rectangle(115,50,400,400);
square.setFill(black);
square.setStroke(white);
square.setStrokeWidth(width);
//Drawing the Circle and pixels
Circle circle = new Circle();
Circle pixel = new Circle();
//Setting the properties of the circle
circle.setCenterX(315.0f); //moves the cirlce left or right
circle.setCenterY(250.0f); //moves the circle up and down
circle.setRadius(200.0f);
circle.setFill(black);
circle.setStroke(white);
circle.setStrokeWidth(width);
//setting properties of the pixels
for(int i = 0; i<1000; i++)
{
pixel.setCenterX((int)(Math.random()*400)+1);
pixel.setCenterY((int)(Math.random()*400)+1);
pixel.setRadius(1);
pixel.setFill(white);
}
//Creating a Group object
Group Root = new Group(circle);
Pane pane = new Pane();
pane.getChildren().addAll(square,circle,pixel);
Scene scene = new Scene(pane,630,500);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
Code
public class PixelLoop extends Application {
#Override
public void start(Stage primaryStage) {
int width = 1;
int random = ((int)Math.random()*400)+1;
// Create the Colors
Color white = Color.web("rgb(255,255,255)");
Color black = Color.web("rgb(0,0,0)");
//making the square
Rectangle square = new Rectangle(115,50,400,400);
square.setFill(black);
square.setStroke(white);
square.setStrokeWidth(width);
//Drawing the Circle and pixels
Circle circle = new Circle();
//Setting the properties of the circle
circle.setCenterX(315.0f); //moves the cirlce left or right
circle.setCenterY(250.0f); //moves the circle up and down
circle.setRadius(200.0f);
circle.setFill(black);
circle.setStroke(white);
circle.setStrokeWidth(width);
//Creating a Group object
Group Root = new Group(circle);
Pane pane = new Pane();
pane.getChildren().addAll(square,circle);
//setting properties of the pixels
for(int i = 0; i<1000; i++) {
Circle pixel = new Circle();
pixel.setCenterX((int)(Math.random()*400)+1);
pixel.setCenterY((int)(Math.random()*400)+1);
pixel.setRadius(1);
pixel.setFill(white);
pane.getChildren().add(pixel);
}
Scene scene = new Scene(pane,630,500);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) { Application.launch(args); }
}
In the beginning there was the problem that you overwrote your object over and over again. That is the reason why there was just one pixel.
As #Tobias Brösamle said, you should create a new object every time you pass through the loop and then add it to the pane.
If you want negativ Coordinates try this

Java AnimationTimer content not showing

I'm trying to learn how JavaFX animations work, so I've tried to create an animation with Earth moving in a circle, based on this tutorial: https://gamedevelopment.tutsplus.com/tutorials/introduction-to-javafx-for-game-development--cms-23835.
For some reason, the scene I want to show is set in the stage, but the graphics or animations aren't. I'm pretty sure this one is trivial, I just can't seem to find the reason. Here's my Main class code:
public class Main extends Application {
private static Stage stage;
private static Window window;
private static Scene scWindow;
#Override
public void start(Stage primaryStage) {
stage = primaryStage;
stage.setTitle("Animation");
showWindow();
stage.show();
}
public static void showWindow() {
window = new Window();
scWindow = new Scene(window, 400, 400);
stage.setScene(scWindow);
}
public static void main(String[] args) {
launch(args);
}
}
and this is the Window class code:
public class Window extends BorderPane {
final long startNanoTime = System.nanoTime();
Image earth = new Image("http://icdn.pro/images/en/g/o/google-earth-icone-8927-128.png");
Image space = new Image("https://space-facts.com/wp-content/uploads/magellanic-clouds.png");
GraphicsContext gc;
public Window() {
Group root = new Group();
Canvas canvas = new Canvas(512, 512);
gc = canvas.getGraphicsContext2D();
root.getChildren().add(canvas);
gc.setFill(Color.BLACK);
Anim a = new Anim();
a.start();
}
private class Anim extends AnimationTimer {
public void handle(long currentNanoTime)
{
double t = (currentNanoTime - startNanoTime) / 1000000000.0;
double x = 232 + 128 * Math.cos(t);
double y = 232 + 128 * Math.sin(t);
gc.drawImage( space, 0, 0 );
gc.drawImage( earth, x, y );
}
}
}
There's no exception thrown or anything, I have no idea what's wrong.
EDIT:
I know that I haven't put anything in the setCenter/Left etc. in the constructor, so actually if it is the reason, how should I put the animation e.g. on the center of the border pane?
You forgot to add root to the scene. Add something like
setCenter(root);
to the constructor of Window.
Or simply use the Canvas as center:
public Window() {
Canvas canvas = new Canvas(512, 512);
gc = canvas.getGraphicsContext2D();
setCenter(canvas);
gc.setFill(Color.BLACK);
Anim a = new Anim();
a.start();
}

How to make a node move whenever mouse is move over the scene (in JavaFX)?

I have written a small JavaFX program in which a Rectangle node is made to moved whenever mouse cursor is moved over the scene containing the rectangle. Here is my code:
public class MovedObjectWhenMouseMoved extends Application{
double nodeX;
double currentMousePos;
double oldMousePos = 0.0;
public static void main(String[] arg){
launch(arg);
}
#Override
public void start(Stage stage) throws Exception {
final Rectangle rect = new Rectangle(50, 50, Color.RED);
rect.setX(20);
rect.setY(20);
AnchorPane anchorPane = new AnchorPane();
anchorPane.getChildren().add(rect);
Scene scene = new Scene(anchorPane,500,500,Color.GREEN);
stage.setScene(scene);
stage.show();
scene.setOnMouseMoved(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent mouseEvent) {
currentMousePos = mouseEvent.getX();
if(currentMousePos>oldMousePos){
rect.setX(rect.getX()+1); // Move right
}else if(currentMousePos<oldMousePos){
rect.setX(rect.getX()-1); // Move Left
}
oldMousePos = currentMousePos;
}
});
}
}
But here the problem is that the node speed is not same as the mouse speed.
How can I rectify this problem? Also please let me know if there is any better approach.
Mouse can change position on more then 1 pixel.
Try this code for handle:
currentMousePos = mouseEvent.getX();
double dX = currentMousePosition - oldMousePos;
rect.setX(rect.getX() + dX);
oldMousePos = currentMousePos;

creating undecorated stage in javafx 2.0

I am trying to create a custom stage in javafx 2.0. I want that my stage drops shadow on the screen as dropped by other windows... I tried with following code :
public class ChatWindow {
final private Stage stage = new Stage(StageStyle.UNDECORATED);
private Scene scene;
private Group rg;
private Text t = new Text();
private double initx = 0, inity = 0;
public ChatWindow() {
rg = new Group();
scene = new Scene(rg, 320, 240);
//scene.setFill(null);
scene.setFill(new Color(0, 0, 0, 0));
stage.setScene(scene);
stage.show();
setupStage();
}
private void setupStage() {
Rectangle r = new Rectangle(5, 5, stage.getWidth() - 10, stage.getHeight() - 10);
r.setFill(Color.STEELBLUE);
r.setEffect(new DropShadow());
rg.setOnMousePressed(new EventHandler<MouseEvent>() {
public void handle(MouseEvent me) {
initx = me.getScreenX() - stage.getX();// - me.getSceneX();
inity = me.getScreenY() - stage.getY();
}
});
rg.setOnMouseDragged(new EventHandler<MouseEvent>() {
public void handle(MouseEvent me) {
stage.setX(me.getScreenX() - initx);
stage.setY(me.getScreenY() - inity);
}
});
rg.getChildren().add(r);
rg.getChildren().add(t);
}
public void setVisible() {
stage.show();
}
}
I can see the shadow fall, but actually their is a white background on which its falling.
So, its useless, as on colored screen the defect will be visible, will make it look ugly..
This is the screen shot on white screen :
And this on colored screen:
HOw to resolve this issue?? Please help.
You should set style StageStyle.TRANSPARENT, see next code:
public class ChatWindow extends Application {
#Override
public void start(final Stage stage) throws Exception {
stage.initStyle(StageStyle.TRANSPARENT); // here it is
Group rg = new Group();
Scene scene = new Scene(rg, 320, 240, Color.TRANSPARENT);
stage.setScene(scene);
stage.show();
Rectangle r = new Rectangle(5, 5, stage.getWidth() - 10, stage.getHeight() - 10);
r.setFill(Color.STEELBLUE);
r.setEffect(new DropShadow());
rg.getChildren().add(r);
}
public static void main(String[] args) {
launch();
}
}

Categories

Resources