Confusion on why an object wont draw a circle when trying to grab the window size - javafx

So, I have a pane being made in one class
#Override //Overridden nerd
public void start(Stage primaryStage) {
//makes a ball
Ball ball = new Ball();
//pause and resume animation
ball.setOnMousePressed(e -> ball.pause());
ball.setOnMouseReleased(e -> ball.play());
//increase/decrease speed
ball.setOnKeyPressed(e-> {
if(e.getCode() == KeyCode.UP) {
ball.increaseSpeed();
}
else if (e.getCode() == KeyCode.DOWN) {
ball.decreaseSpeed();
}
});
//creating a scene and setting its background
Scene scene = new Scene(ball, 500, 500);
primaryStage.setTitle("Random Walker");
primaryStage.setScene(scene);
primaryStage.show();
ball.requestFocus();
}
public static void main(String[] args) {
Application.launch(args);
}
}
and im trying to get the animation to start the ball at the center off the pane, and then bounce off the walls (im not on the bouncing part yet so I know that isnt perfect yet but im trying)
My problem is that when I try to get the ball to draw off of the current size of the window its not drawing anything at all, but will work if I manually put in the default screen size. any insight would be great, as I've been struggling with trying to finding any way to do this for a while, thanks.
public class setup extends Application {
#Override //Overridden nerd
public void start(Stage primaryStage) {
//makes a ball
Ball ball = new Ball();
//pause and resume animation
ball.setOnMousePressed(e -> ball.pause());
ball.setOnMouseReleased(e -> ball.play());
//increase/decrease speed
ball.setOnKeyPressed(e-> {
if(e.getCode() == KeyCode.UP) {
ball.increaseSpeed();
}
else if (e.getCode() == KeyCode.DOWN) {
ball.decreaseSpeed();
}
});
//creating a scene and setting its background
Scene scene = new Scene(ball, 500, 500);
primaryStage.setTitle("Random Walker");
primaryStage.setScene(scene);
primaryStage.show();
ball.requestFocus();
}
public static void main(String[] args) {
Application.launch(args);
}
}

Related

JavaFX button unselect

I'm trying to make a sprite editor using JavaFX using buttons as the pixels. I am able to change the colour of each button on press, but I'm trying to get it so if I click and drag I can paint multiple pixels.
The problem I'm finding is that after clicking and holding onto a button, I am unable to select the new button when I move the mouse over new button to select that one too. If I click and drag re-entering that button I can get the "Paint Dragged Pixel" debug message, but not if I enter a new pixel with the mouse down, which is what I want. Also I can get the pixel button to print "Entered Pixel" when the mouse enters any button, but not for when I click and drag to a new pixel.
I think the problem is that when I click on one pixel I am locked in, and unable to select a new pixel by hovering over a new one. Is there a way to unbind this selection, or is the problem a different one.
Main Application:
public class Main extends Application {
boolean mousePressed = false;
public boolean isMousePressed() {
return mousePressed;
}
#Override
public void start(Stage primaryStage) throws Exception{
BorderPane borderPane = new BorderPane();
primaryStage.setTitle("SpriteSheet");
Group root = new Group();
Scene scene = new Scene(borderPane, 500,200);
scene.setFill(Color.BLACK);
primaryStage.setScene(scene);
GridPane gridPane = new GridPane();
borderPane.setCenter(root);
for(int x = 0; x < 10; x++)
{
for(int y = 0; y < 10; y++) {
PixelButton button = new PixelButton();
button.setParentMain(this);
button.setOnMousePressed(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
mousePressed = true;
System.out.println("mouseDown");
}
});
button.setOnMouseReleased(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
mousePressed = false;
System.out.println("mouseUp");
}
});
gridPane.add(button, x, y);
}
}
root.getChildren().add(gridPane);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
The class for the PixelButton.
public class PixelButton extends Button {
Main parentMain;
public void setParentMain(Main parent) {
parentMain = parent;
}
public PixelButton() {
this.setMinSize(10, 10);
this.setPrefSize(10, 10);
this.setOnMousePressed(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
}
});
this.setOnMouseEntered(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
System.out.println("Entered Pixel");
if(parentMain.isMousePressed()){
System.out.println("Paint Dragged Pixel");
}
}
});
}
}
Thank you in advance.
Ok so I have been thinking about this one for a bit and have finally come up with a solution I simplified my solution a bit and used rectangles instead of buttons but you can transfer most of the code to the buttons as well so to start with this is not the exact functionality you were looking for but as close as I could get basically I fire an event on mouse press that releases the mouse click and as long as that event is not coming from the rectangle then dont flip the painting boolean paint and so you basically click to enter a "Paint Mode" and click again to get out of coloring tiles
public class Main extends Application {
private boolean mousePressed;
#Override
public void start(Stage primaryStage) throws Exception{
BorderPane borderPane = new BorderPane();
primaryStage.setTitle("SpriteSheet");
Group root = new Group();
Scene scene = new Scene(borderPane, 500,200);
// scene.setFill(Color.BLACK);
primaryStage.setScene(scene);
GridPane gridPane = new GridPane();
borderPane.setCenter(root);
for(int x = 0; x < 10; x++) {
for(int y = 0; y < 10; y++) {
Rectangle rectangle = new Rectangle(10, 10);
rectangle.setOnMousePressed(event -> {
mousePressed = true;
System.out.println("mouseDown");
rectangle.fireEvent(new MouseEvent(MouseEvent.MOUSE_RELEASED,
rectangle.getLayoutX(), rectangle.getLayoutY(), rectangle.getLayoutX(), rectangle.getLayoutY(),
MouseButton.PRIMARY, 1,
false, false, false, false,
false, false, false, false,
false, false, null));
});
rectangle.setOnMouseReleased(event -> {
System.out.println(event.getSource());
if(!event.getSource().toString().equals("Rectangle[x=0.0, y=0.0, width=10.0, height=10.0, fill=0x000000ff]")) {
mousePressed = false;
System.out.println("mouseUp");
}
});
rectangle.setOnMouseMoved(event -> {
if(mousePressed) {
rectangle.setFill(Color.BLUE);
}
});
gridPane.add(rectangle, x, y);
}
}
root.getChildren().add(gridPane);
primaryStage.show();
}
public static void main(String[] args) { launch(args); }
}

Why does my JavaFX Preloader occasionally show up gray/black and other times it correctly loads?

I'm trying to get my JavaFX Preloader splash sccreen to show up before my application. I'm using Eclipse IDE and when I click "run", half the time the splash screen will display correctly and the other half of the time I will get a gray or black screen instead of where the image should be.
I'm not sure what the issue is to cause it to only display correctly sometimes.
SplashController:
public class SplashController extends Preloader {
private static final double WIDTH = 676;
private static final double HEIGHT = 227;
private Stage preloaderStage;
private Label progressText;
private Pane splashScreen;
public SplashController() {}
#Override
public void init() throws Exception {
ImageView splash =
new ImageView(new Image(Demo.class.getResource("pic.png").toString()));
progressText =
new Label("VERSION: " + getVersion() + " ~~~ Loading plugins, please wait...");
splashScreen = new VBox();
splashScreen.getChildren().addAll(splash, progressText);
progressText.setAlignment(Pos.CENTER);
}
#Override
public void start(Stage primaryStage) throws Exception {
this.preloaderStage = primaryStage;
Scene splashScene = new Scene(splashScreen);
this.preloaderStage.initStyle(StageStyle.UNDECORATED);
final Rectangle2D bounds = Screen.getPrimary().getBounds();
this.preloaderStage.setScene(splashScene);
this.preloaderStage.setX(bounds.getMinX() + bounds.getWidth() / 2 - WIDTH / 2);
this.preloaderStage.setY(bounds.getMinY() + bounds.getHeight() / 2 - HEIGHT / 2);
this.preloaderStage.show();
}
}
And then in my main class Demo I simply have:
public class Demo extends Application {
#Override
public void start(Stage stage) throws Exception {
FXMLLoader loader = new
FXMLLoader(Demo.class.getResource("FXMLDocument.fxml"));
GridPane root = loader.load();
--------other app code here---------
}
public static void main(String[] args) {
LauncherImpl.launchApplication(Demo.class, SplashController.class, args);
}
}
Likely, you are executing some long running process on the JavaFX application thread or a thread involved in the application startup, which prevents the smooth operation of the preloader.
I suggest you review an Oracle Preloader sample and compare to your application. Ensure that you are using concurrent features like Task correctly, similar to the linked example. Check the linked sample works in your environment.
Source code (just copied from the Oracle Preloader sample link)
Note how in the start method of the main LongAppInit application class, that a Task and thread is spawned to ensure that the long application initiation does not take place on the JavaFX application thread. Also see how the notifyPreloader() method of application is called at various times within the long application initialization to let the preloader know of the current state of the initialization process so that it can reflect the progress accurately in the UI in real time.
LongAppInitPreloader.java
public class LongAppInitPreloader extends Preloader {
ProgressBar bar;
Stage stage;
boolean noLoadingProgress = true;
private Scene createPreloaderScene() {
bar = new ProgressBar(0);
BorderPane p = new BorderPane();
p.setCenter(bar);
return new Scene(p, 300, 150);
}
public void start(Stage stage) throws Exception {
this.stage = stage;
stage.setScene(createPreloaderScene());
stage.show();
}
#Override
public void handleProgressNotification(ProgressNotification pn) {
//application loading progress is rescaled to be first 50%
//Even if there is nothing to load 0% and 100% events can be
// delivered
if (pn.getProgress() != 1.0 || !noLoadingProgress) {
bar.setProgress(pn.getProgress()/2);
if (pn.getProgress() > 0) {
noLoadingProgress = false;
}
}
}
#Override
public void handleStateChangeNotification(StateChangeNotification evt) {
//ignore, hide after application signals it is ready
}
#Override
public void handleApplicationNotification(PreloaderNotification pn) {
if (pn instanceof ProgressNotification) {
//expect application to send us progress notifications
//with progress ranging from 0 to 1.0
double v = ((ProgressNotification) pn).getProgress();
if (!noLoadingProgress) {
//if we were receiving loading progress notifications
//then progress is already at 50%.
//Rescale application progress to start from 50%
v = 0.5 + v/2;
}
bar.setProgress(v);
} else if (pn instanceof StateChangeNotification) {
//hide after get any state update from application
stage.hide();
}
}
}
LongAppInit.java
public class LongInitApp extends Application {
Stage stage;
BooleanProperty ready = new SimpleBooleanProperty(false);
private void longStart() {
//simulate long init in background
Task task = new Task<Void>() {
#Override
protected Void call() throws Exception {
int max = 10;
for (int i = 1; i <= max; i++) {
Thread.sleep(200);
// Send progress to preloader
notifyPreloader(new ProgressNotification(((double) i)/max));
}
// After init is ready, the app is ready to be shown
// Do this before hiding the preloader stage to prevent the
// app from exiting prematurely
ready.setValue(Boolean.TRUE);
notifyPreloader(new StateChangeNotification(
StateChangeNotification.Type.BEFORE_START));
return null;
}
};
new Thread(task).start();
}
#Override
public void start(final Stage stage) throws Exception {
// Initiate simulated long startup sequence
longStart();
stage.setScene(new Scene(new Label("Application started"),
400, 400));
// After the app is ready, show the stage
ready.addListener(new ChangeListener<Boolean>(){
public void changed(
ObservableValue<? extends Boolean> ov, Boolean t, Boolean t1) {
if (Boolean.TRUE.equals(t1)) {
Platform.runLater(new Runnable() {
public void run() {
stage.show();
}
});
}
}
});;
}
}

Get instance of drop target object

I need to have the instance of the object on which the user dragged something. I'm looking at event.getTarget(), but I'm still not able to get the actual object.
Here is what I have now:
scrollPane.setOnDragOver(new EventHandler<DragEvent>() {
private Node hoveredNode;
#Override
public void handle(DragEvent event) {
double windowHeight = scrollPane.getHeight();
if(!event.getTarget().getClass().getName().contains("FlowPane"))
logger.severe(event.getTarget().getClass().getName() + "");
double topBar = (20*windowHeight)/100;
double bottomBar = windowHeight - topBar;
event.acceptTransferModes(TransferMode.LINK);
if(event.getY() > 0 && event.getY() < topBar && scrollPane.getVvalue() > 0) {
scrollPane.setVvalue(scrollPane.getVvalue()-0.001);
}
else if(event.getY() < windowHeight && event.getY() > bottomBar && scrollPane.getVvalue() < 1){
scrollPane.setVvalue(scrollPane.getVvalue()+0.001);
}
}
});
Now I'm just logging the target class name if it's not a FlowPane. I need to have the instance of the actual object, because I want to apply the hover effect on it.
Can you suggest me something to work on?
You want to use event.getTarget() or event.getSource(), as you already do, but you have to cast the object you retrieve to a specific class. Then you can modify it.
For a reference, take a look at the following SSCCE.
public class JavaFXTest extends Application {
#Override
public void start(Stage primaryStage) {
StackPane root = new StackPane();
Text text = new Text("Test");
text.setOnDragDetected((Event event) -> {
((Text)event.getSource()).setStyle("-fx-stroke: red;");
event.consume();
});
root.getChildren().add(text);
Scene scene = new Scene(root, 600, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Nothing fancy, once you start trying to drag the text it will turn red.

How to write a MouseListener for JavaFX

I want to write a little game where I shoot from an object to a specific direction on a JavaFX Panel using my mouse position.
I want to turn a Line in the direction where my Mouse is.
Line line = new Line(startPosX, startPosY, mouseDirectionX, mouseDirectionY);
How can I do that?
Add a MOUSE_MOVED event filter like this:
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
Pane root = new Pane();
Line line = new Line( 400,200,400,200);
root.addEventFilter(MouseEvent.MOUSE_MOVED, e -> {
line.setEndX(e.getSceneX());
line.setEndY(e.getSceneY());
});
root.getChildren().add(line);
Scene scene = new Scene(root, 800, 400);
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
If you want the line to be limited in length, you'll have to do the proper calculations of course via the angle.

Cannot catch mouse events after having a certain transparency as a JavaFX Scene fill

I am trying to create an anchor pane that is transparent but you can drag it in the same time.
I set the stage transparent, I give the scene a transparent fill, and I set up the OnMousePressed etc on the pane.
The program behaves very strangely for me. There is a certain threshold for the transparency: if you are below this threshold, the pane will not catch mouse events. If above, then it will.
I give you an example. When I was writing this example, I found that the threshold here is between 0.5 and 0.6. It means: If I set the transparency of the fill to 0.5 or below the window will not be dragable, if to 0.6 or above, it will be.
My test code:
public class Main extends Application {
Stage primaryStage;
class Delta {
double x, y;
}
final Delta dragDelta = new Delta();
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
primaryStage.initStyle(StageStyle.TRANSPARENT);
showMain();
}
public void showMain() {
AnchorPane ap = new AnchorPane();
ap.setOnMousePressed(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent mouseEvent) {
dragDelta.x = primaryStage.getX() - mouseEvent.getScreenX();
dragDelta.y = primaryStage.getY() - mouseEvent.getScreenY();
}
});
ap.setOnMouseDragged(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent mouseEvent) {
primaryStage.setX(mouseEvent.getScreenX() + dragDelta.x);
primaryStage.setY(mouseEvent.getScreenY() + dragDelta.y);
}
});
Scene scene = new Scene(ap);
scene.setFill(Color.rgb(252, 0, 4, 0.6));
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String args[]) {
launch();
}
}
Can you explain or reproduce this behavior? Thank you!
This is not a bug - it's a feature. The general rule is that you cannot click on things which you cannot see. I'd prefer it if this decision would have been left to the programmer.

Resources