I have a question regarding JavaFX. I taught myself Java and now I'm learning JavaFX.
I've been trying to update the location of a 50x50 black block on the screen. I have a YAxis variable that when I change changes the location of the block.
I want the block to "flow" down the screen similar to Tetris.
My code is messy, as I'm just messing around with it, so please excuse that:
package gamefx;
import javafx.animation.Animation;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.geometry.Rectangle2D;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class GameFX extends Application {
public Image img = new Image("Block1.png");
public ImageView image = new ImageView(img);
public int YAxis = -200;
#Override
public void start(Stage primaryStage) throws InterruptedException{
primaryStage.setTitle("Game");
StackPane stckp1 = new StackPane();
Scene scn = new Scene(stckp1, 700, 700);
primaryStage.setScene(scn);
primaryStage.show();
image.setTranslateY(YAxis);
stckp1.getChildren().add(image);
}
}
Since you want to see your block moving, you need an animation. See Oracle tutorial for more information.
A sample code, quickly written:
TranslateTransition tt = new TranslateTransition(Duration.millis(2000), image);
tt.setByY(yAxis);
tt.setCycleCount(1);
tt.play();
Side-note: Variable names shall not start with an upper-case letter. Use yAxis instead of YAxis.
Related
This question already has an answer here:
How do I determine the correct path for FXML files, CSS files, Images, and other resources needed by my JavaFX Application?
(1 answer)
Closed 11 months ago.
I get the same error again and again when i try to add images to my JavaFX.
Error: at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
(more lines follow).
It must be related to the path to the images that I specified. I have already read through the general "path" tutorial on StackOverflow without success.
I just want to make a simple scrollBar which enables scrolling through some Images i added to a VBox.
Heres my directory:
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.Shadow;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class Scrollbar3 extends Application {
// Variablen
final ScrollBar scrollbar = new ScrollBar();
final String[] images = {
"Bilder/bild0.jpg", // 0
"Bilder/bild1.jpg",
"Bilder/bild2.jpg",
"Bilder/bild3.jpg",
"Bilder/bild4.jpg",
};
DropShadow shadow = new DropShadow();
final VBox vbox = new VBox();
#Override
public void start(Stage primaryStage) throws Exception {
// Scene / root
Group root = new Group();
Scene scene = new Scene(root, 400, 400);
root.getChildren().addAll(vbox, scrollbar);
// Effekt
shadow.setColor(Color.BLACK);
shadow.setOffsetX(10);
shadow.setOffsetY(10);
// VBox
vbox.setLayoutX(5);
vbox.setSpacing(10);
vbox.setPadding(new Insets(20));
// Scrollbar
scrollbar.setLayoutX(scene.getWidth() - scrollbar.getWidth());
scrollbar.setOrientation(Orientation.VERTICAL);
scrollbar.setPrefHeight(400);
scrollbar.setMax(2000);
// Bilder
for(int i = 0; i < images.length; i++) {
final ImageView imageView = new ImageView(new Image(images[i]));
imageView.setEffect(shadow);
vbox.getChildren().add(imageView);
}
// Eventhanlding / Listener
scrollbar.valueProperty().addListener(new ChangeListener<Number>() {
#Override
public void changed(ObservableValue<? extends Number> observableValue, Number oldValue, Number newValue) {
vbox.setLayoutY(-newValue.doubleValue());
}
});
// Stage
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
It looks like you are treating your images as resource images because they are contained in the source folder.
Change the line with the image creation to
final ImageView imageView = new ImageView(new Image(this.getClass().getResourceAsStream(images[i])));
and add an "/" in front of your image paths.
I am new to overflow so I apologize in advance if I am not precise enough.
During a class project I have to make a tanquin game in javafx. I must be able to cut an image into x part in order to insert them then in an Arraylist with an id and the part of the image. But I have the error attached here.
I guess the reader is null and that the error is related to that. But I don't know how to fix it.
I never used those library so i dont know how to use them.
Thank you in advance for your reply.
Code:
package sample;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.PixelReader;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Image image = new Image("https://scontent-yyz1-1.cdninstagram.com/v/t51.2885-15/sh0.08/e35/c180.0.720.720a/s640x640/75272157_461406551233157_7963091763107249286_n.jpg?_nc_ht=scontent-yyz1-1.cdninstagram.com&_nc_cat=105&_nc_ohc=eu-9K8wyx6oAX9H695F&oh=429cc1e6dcec03badc9aac439b6b8ac0&oe=5EAD8EA3", true);
PixelReader reader = image.getPixelReader();
WritableImage newImage = new WritableImage(reader,100,100,100,100);
ImageView imageView = new ImageView(newImage);
HBox hbox = new HBox(imageView);
Scene scene = new Scene(hbox, 640, 640);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
It doesn't matter in the following code whether i pass the value 50 or 300 to the function. all my buttons are always in a row unless i resize the window. According to theory after the row of my nodes reaches 50 pixels (suppose i pass that value to the setPrefWrapLength() method) my next node should be in the next row. But that is not happening. Can someone explain me why?
package test2;
import com.sun.java.swing.plaf.gtk.GTKConstants;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Test2 extends Application {
#Override
public void start(Stage primaryStage) {
Button btn1 = new Button("one");
Button btn2 = new Button("two");
Button btn3 = new Button("three");
Button btn4 = new Button("four");
FlowPane fpane = new FlowPane(Orientation.HORIZONTAL, 10, 10, btn1, btn2, btn3, btn4);
fpane.setPrefWrapLength(50);
Scene scene = new Scene(fpane, 300, 250);
primaryStage.setTitle("Flow Pane");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
update :
I found out the following line in the javaFX documentation and i think this is in context with this question
Note that prefWrapLength is used only for calculating the preferred size and may not reflect the actual wrapping dimension, which tracks the actual size of the flowpane.
We are currently working on a JavaFX Project for which we need to implement Text animation like movement from Left to Right with variant speeds. Execution environment for the application is an Embedded device - Stick PC.
We have used Translate Transition API https://docs.oracle.com/javase/8/javafx/api/javafx/animation/TranslateTransition.html for achieving animation effect but we are facing smoothness issues in it. Text is moving with jerks and movement speed is slow compared to animation in Laptop/Desktop
package application;
import javafx.animation.Interpolator;
import javafx.animation.Timeline;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.scene.CacheHint;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;
public class SampleAnimationTest extends Application {
// private final Logger logger = Logger.getLogger("genLog");
#Override
public void start(Stage primaryStage) throws Exception {
StackPane group = new StackPane();
Rectangle rectangle = new Rectangle();
rectangle.setHeight(1080);
rectangle.setWidth(1920);
rectangle.setFill(Color.WHITE);
rectangle.setTranslateX(0);
rectangle.setTranslateY(0);
rectangle.setCache(true);
rectangle.setCacheHint(CacheHint.SPEED);
Text text = new Text("THIS IS A LONG TEXT FOR TESTING TEXT ANIMATION IN JAVAFX");
text.setFill(Color.BLACK);
text.setUnderline(false);
text.setFont(Font.font("Meiryo", 509.3899));
text.setTextAlignment(TextAlignment.CENTER);
text.setCache(true);
text.setCacheHint(CacheHint.SPEED);
TranslateTransition tt = new TranslateTransition();
tt.setNode(text);
Rectangle rClip = new Rectangle();
rClip.setWidth(rectangle.getWidth());
rClip.setHeight(rectangle.getHeight());
rClip.translateXProperty().bind(rectangle.translateXProperty());
group.getChildren().add(rectangle);
group.getChildren().add(text);
group.setClip(rClip);
Group group2 = new Group();
group2.getChildren().add(group);
Scene scene = new Scene(group2, 1920, 1080);
primaryStage.setMaximized(true);
primaryStage.setTitle("Decorations Example");
primaryStage.setScene(scene);
primaryStage.show();
tt.fromXProperty().bind(rectangle.translateXProperty().add(rectangle.getLayoutBounds().getWidth()));
tt.toXProperty().bind(rectangle.translateXProperty().subtract(text.getLayoutBounds().getWidth()));
tt.setRate(0.077364);
tt.setInterpolator(Interpolator.LINEAR);
tt.setCycleCount(Timeline.INDEFINITE);
tt.playFromStart();
}
public static void main(String[] args) {
launch(args);
}
}
I'm currently working on a project which relies on the representation of file sizes through rectangles. What I have been stuck on for quite a while however, was scaling of the height to a size that could fit inside of my Stage. This is a crucial part of the project as it allows the user to visually compare the sizes of the files they are viewing from any directory on their machine.
EDIT: I tried adding an image but I haven't got enough reputation yet :(.
http://tinypic.com/r/2e56pol/8
Why don't you just bind the height of the rectangle to the scene's using:
rectangle.heightProperty().bind(scene.heightProperty());
A complete example :
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class DemoTabPane extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
Rectangle rectangle = new Rectangle(20,20,200,200);
rectangle.setStroke(Color.BLACK);
Scene scene = new Scene(new HBox(rectangle), 100, 100);
rectangle.heightProperty().bind(scene.heightProperty());
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}