JavaFX Robot.getScreenCapture causing scene to not update prior - java

I have an options menu from which you can save the game and it should briefly close the menu and take a screenshot of the current game.
However, the menu does not close until after calling getScreenCapture even though it runs before causing the screenshot to always be of the settings menu.
(The screenshot itself is working it's just controlling what displays at the time of the screenshot)
!! Without the screenshot line it updates the scene immediately even though the scene change line comes before.
So far I have tried Thread.sleep but no length of time allows for the scene to update
//The initial call to save:
private void saveRoom() {
program.changeState(previous);
room.save();
}
//The changing of scenes: (State extends Scene)
public void changeState(State state) {
currentState = state;
stage.setScene(currentState);
currentState.paintAll();
}
//How the game scene draws to screen:
#Override
public void paintAll() {
ArrayList<Shape> toAdd = new ArrayList<>();
for(Furniture f : furniture) {
f.paint(toAdd);
}
optionsButton.paint(toAdd);
addFurnitureButton.paint(toAdd);
root.getChildren().clear();
root.getChildren().addAll(toAdd);
}
//How the options menu draws to screen (same as game):
#Override
public void paintAll() {
ArrayList<Shape> toAdd = new ArrayList<>();
toolsButton.paint(toAdd);
saveButton.paint(toAdd);
saveAndExitButton.paint(toAdd);
exitButton.paint(toAdd);
closeButton.paint(toAdd);
root.getChildren().clear();
root.getChildren().addAll(toAdd);
}
//The capture command:
robot.getScreenCapture(null, new Rectangle2D(x,y,x1,y1));

Update: I am now using the Scene.snapshot command to take the screenshot instead and it does not cause the problem, but am still open to finding out how to go about using Robot to take a screenshot since unlike Scene.snapshot, it captures the whole screen rather than just the program which could come in handy in a future project.

Related

JavaFX: Moving hundreds of ImageViews simultaneously inside a Pane (Performance problems)

I am currently developing a tower defence game for a university project.
At higher waves there are hundreds of enemies moving arround. My problem is that it's getting very inperforment at about 300+ enemies moving simultaneously.
Every enemy is a children of a Pane which is displayed in my scene.
I've got a method in my EnemyGraphics class which updates the positon by calling the update method:
public class EnemyGraphics extends ImageView implements EventObserver {
...
#Override
public void update() {
Platform.runLater(() -> {
relocate(enemy.getxCoordinate(), enemy.getyCoordinate());
});
}
}
I guess it's getting laggy because every enemy is updating its location on its own every time it moves by calling update().
Is there a way that I can set new coordinates for my ImageView object, without redrawing the scene and in the main FX-Thread creating a timer which redraws the entire scene in a certain interval? Or is there a other solution / method I can call to move images performantly over the pane?
Ok I've found my mistake. Everytime my enemyLogic class compute a new position it called the enemyGraphic object to update its position. During testing I've removed the functionality of the method in the enemyGraphics class but not the call.
To update the postions of all the enemys I've written a method in my GUIcontroller class:
private void startUpdateTicker() {
final AnimationTimer timer = new AnimationTimer() {
#Override
public void handle(long timestamp) {
updateEnemies();
}
};
timer.start();
}
public synchronized void updateEnemies() {
for (EnemieGUI enemy : enemyList) {
enemy.relocate(enemy.getEnemie().getxCoordinate(), enemy.getEnemie().getyCoordinate());
}
}

short tone in app without crashing it

Yes, I did search for the answer before I posted this. :)
Made board game, each piece gets roll of die and then moves step by step on squares. Wanted to follow each step with short beep. Added method "beep();" and placed it in handler that moves position of piece on screen.
When I add beep(); game works couple of moves and then simply stops, without any report or error message. View closes on screen and dialog just says "Unfortunately, BoardLS has stopped [Ok]", and nothing else happens.
When I remove beep(); game works ok.
Please, if anyone knows how beep crashes the game, and how to avoid it, tell me as simple as you can. Thanks.
Here's how I made the beep:
private void beep() {
ToneGenerator beepSound = new ToneGenerator(AudioManager.STREAM_MUSIC, 60);
beepSound.startTone(ToneGenerator.TONE_PROP_BEEP, 75);
} // Short sound
and I call it like this:
private final Handler playMove = new Handler() {
#Override
public void handleMessage(Message playMsg) {
beep();
player.setX(pcX[playPos]);
player.setY(pcY[playPos]);
}
}; // Put Player piece to new coordinates
playPos is square number where the piece is or will be,
pcX and pcY are arrays with on-screen coordinates of squares.
handler is in MainActivity,
and called from AsyncTask with playMove.sendEmptyMessage(0);
AsyncTask works directly with global variables, making no problems here.
Just release created ToneGenerator objects using release() method.
private void beep() {
ToneGenerator beepSound = new ToneGenerator(AudioManager.STREAM_MUSIC, 60);
beepSound.startTone(ToneGenerator.TONE_PROP_BEEP, 75);
beepSound.release();
}

Custom splash screen using JWindow/JPanel doesn't load image correctly when attempting to display it a second time

Thank you in advance for any attempts at trying to help me figure this one out. I've searched all over stack and google and haven't been able to find anything similar to the problem I am having.
I am designing a small program using NetBeans that switches between multiple frames
login screen > settings screen > main screen
My program can take a while in-between frames to load the next one due to a large amount of frame initialization code. To prevent it from looking like the program is unresponsive. I wrote code which displays an intermediary splash screen JWindow which performs all the initialization for the next frame. I realize that there is a splash screen class, but I could not get it to work as desired, this custom implementation of mine does almost everything I need except when I attempt to display the splash screen a second time, during a single program run, the image does not show up on the JWindow.
So run program > splash screen (works) > login > splash screen (image does not load) > settings.
Here is a sample clip of code which displays the splash screen JWindow:
public class Splash extends JPanel {
private static Calendar date;
private static long startTime;
private static final long MAX_TIME = 3000l;
public static final int LOAD_LOGIN = 0;
public static final int LOAD_SETTINGS = 1;
public static JWindow win;
private final ImageIcon img;
public Splash() {
date = Calendar.getInstance(TimeZone.getTimeZone("EST"), Locale.ENGLISH);
img = new ImageIcon("loader.gif");
startTime = date.getTimeInMillis();
this.setSize(300, 300);
win = new JWindow();
win.setSize(300, 300);
win.getContentPane().add(this);
Login.centerWindow(win); // method from login class that centers window
win.setVisible(true);
}
public void runSplash(int flag) {
if (flag == LOAD_LOGIN)
firstRunCheck();
else {
initSettings();
}
long currTime = date.getTimeInMillis();
// holds splash display for at least 3 secs
while ((currTime - startTime) < MAX_TIME) {
currTime = System.currentTimeMillis();
}
win.dispose(); // has been replaced with win.setVisible(false) before,
// same problem
}
#Override
protected void paintComponent(Graphics g)
{
super.paintComponents(g);
Graphics2D g2 = (Graphics2D) g;
//I have read several other questions and sample codes on stack and google
but using this method is the only way for me to get the animated gif image to
load correctly
g2.drawImage(img.getImage(), null, this);
}
}
In order to display this splash screen I instantiate it using new Splash().runSplash(value); where value runs the method that loads the following frames required values.
When I first begin run the program everything works perfectly. When I attempt to move from the login screen to the settings screen the JWindow loads but it just displays as a blank frame.
The program works fine still, the settings frame eventually loads, but the image never shows up and the window just closes. I am unsure as to what the problem is the class is instantiated the exact same way as the first time.
I suspect perhaps I may be incorrectly disposing of resources, but win.dispose() appears to be the correct method according to some of the other problems I have read.
Any help is appreciated thank you.
UPDATE: I am still attempting to resolve this problem. I have used a variety of different implementations to display the splash screen. I have used implementations which use a JLabel instead of JPanel, and use setIcon instead of overriding paintComponent. But the same result is observed whenever the implemented splash screen class is called a second time the image does not appear. Since different implementations are also not working I can only assume that that perhaps I have too many events in the event queue or something. To clarify.
My main method is in the Splash class detailed above. I call the class in the main method using new Splash.runSplash(value). This call works perfectly. I then call the login JFrame using:
if (value == LOAD_LOGIN) //RUN_LOGIN is a final variable int
{
java.awt.EventQueue.invokeLater(new Runnable()
{
#Override
public void run()
{
/* I have also tried this without the invokeLater
command and simply using new Login().setVisible(true).
The program still runs fine, but the image still
does not display */
new Login().setVisible(true);
}
});
}
This call is made at the end of the runSplash(value) method. After the user inputs the correct login information they are taken to the splash screen again using new Splash().runSplash(value) on this call however I cannot get the image to show up on the JWindow.
dispose() method releases the resource. to be able to use it again you have to use setVisible(false) and when you be sure that your code will not use this window again then only use dispose().In your case after first splash just use setVisible(False) and dispose after the second time when setting frame loads if you really want it so.

Button Action in JavaFX executes in a wrong sequence

Update 1: Here is a more interesting thing. When i delete the last line { mainapp.showReportingScreen() ;} now program first execute the findAplliedJars method then show processing screen.
I'm working in JavaFX and struggling with actions of a button. What I want is, when that button is clicked:
do something;
show another screen;
do another thing;
show another screen;
But, my program skips the 2. point.
Here is my code:
public class SomeController implements Initializable {
.
.
.
#Override
public void initialize(URL location, ResourceBundle resources) {
.
.
.
someButton.setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent event) {
Report.setUserName(userNameField.getText());
Report.setPassword(passwordField.getText());
Report.setSmAdress(smAdressField.getText());
mainApp.showProcessingScreen();
Report.findAppliedJars();
mainApp.showReportingScreen();
}
});
}
.
.
.
}
So, without showing processingScreen, my program executes findAppliedJars() and then shows reportingScreen.
What may be the problem? Thanks.
Update 2: Here is my showProcessingScreen() method:
public void showProcessingScreen() {
try {
// Load Report Screen
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("ProcessingScreen.fxml"));
AnchorPane processingScreen = (AnchorPane) loader.load();
// Set report screen into the center of root layout.
rootLayout.setCenter(processingScreen);
ProcessingScreenController controller = loader.getController();
controller.setMainApp(this);
} catch (IOException e) {
e.printStackTrace();
}
}
Unless you somehow pause the execution, the program will continue to execute, and essentially skip over the showing of your processing screen. To show your processing screen for a certain amount of time you need to use an animation and show your reporting screen once the animation has finished. Note the code below is only an outline of what your end result may look like and does not compile.
Transition transition;
// Set up your transition or animation
transition = buildTransition();
// Show your processing screen within the transition
showProcessingScreen();
transition.setOnFinished(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent arg0) {
Report.findAppliedJars();
mainApp.showReportingScreen();
}
});
If you are trying to load something in the background while the processing screen is showing then you will have to use concurrency. If you go this route, the code should look similar to the above, except you would be using the Task class and the setOnSucceeded method. There are other ways to perform this operation, but I believe this is the simplest way.

JavaFX Shape.intersect() performance issue

I have start writing a shooting game JavaFX application. I am using Shape.intersect() to check the collision of bullet and the target. Below is my code and I made it simple so as to post here.
public class TestShapeIntersect extends Application{
AnchorPane anchorPane;
ArrayList<Rectangle> targetObjects;
public static void main(String[] arg){
launch(arg);
}
#Override
public void start(Stage stage) throws Exception {
final Rectangle gun = new Rectangle(50, 50, Color.RED);
anchorPane = new AnchorPane();
anchorPane.getChildren().add(gun);
generateTargetObjects(50); // Number of target objects
anchorPane.getChildren().addAll(targetObjects);
gun.setX(50);
gun.setY(200);
Scene scene = new Scene(anchorPane,300,300,Color.GREEN);
stage.setScene(scene);
stage.show();
scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event) {
Rectangle bullet = new Rectangle(5,10,Color.ORANGE);
bullet.setX(75);
bullet.setY(200);
anchorPane.getChildren().add(bullet);
animateBullet(bullet);
}
});
}
private void generateTargetObjects(int noOfTargetObj) {
targetObjects = new ArrayList<Rectangle>();
for(int i=1; i<=noOfTargetObj;i++){
Rectangle rect = new Rectangle(30, 30, Color.YELLOW);
targetObjects.add(rect);
}
}
void animateBullet(final Rectangle bullet){
Timeline timeline = new Timeline();
timeline.setCycleCount(500);
final KeyFrame kf = new KeyFrame(Duration.millis(2), new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
bullet.setY(bullet.getY()-1);
checkCollision(bullet);
}
});
timeline.getKeyFrames().add(kf);
timeline.play();
}
//This method will check if there is any collision happened between the bullets and the targets.
//If collision happens then both bullet and target object will be disappeared.
void checkCollision(Rectangle bullet){
int noOfTargetObjs = targetObjects.size();
for(int i=0; i<noOfTargetObjs;i++)
{
if(targetObjects.get(i).isVisible()==true && bullet.isVisible()==true){
Shape intersectShape= Shape.intersect(bullet, targetObjects.get(i));
if(intersectShape.getBoundsInLocal().getWidth() != -1){
targetObjects.get(i).setVisible(false);
bullet.setVisible(false);
}
}
}
}
}
I have not yet aligned the nodes properly.Here the 'gun' rectangle will fire 'bullet' rectangle whenever any key press event is detected.
The problem is for every every first bullet fired in each application session, the very first bullet is not animated properly (means the bullet is not going in it path continuously). But after the first bullet has gone the remaining bullets are animated properly. This performance issue increases with the number of 'target' objects increases.
I have found out that the issue is because of this line:
Shape intersectShape= Shape.intersect(bullet, targetObjects.get(i));
Could anyone let me know why this happens and what could be the solution to resolve this issue? Or is it because of the way that I'm implementing?
I experienced a different behaviour when i executed your application. My first shot was moving fine without any interruptions in the translation. But after several shots the application began to slow down. I tried to improve the performance of your code by doing the following steps:
void animateBullet(final Rectangle bullet){
final Timeline timeline = new Timeline();
timeline.setCycleCount(125); //changed
final KeyFrame kf = new KeyFrame(Duration.millis(16), new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
bullet.setY(bullet.getY()-8); //changed
checkCollisionThreaded(bullet); //changed
//added
if(bullet.getX() < 0 || bullet.getX() > bullet.getParent().getBoundsInParent().getWidth()
|| bullet.getY() < 0 || bullet.getY() > bullet.getParent().getBoundsInParent().getHeight())
{
bullet.setVisible(false);
timeline.stop();
AnchorPane ap = (AnchorPane) bullet.getParent();
ap.getChildren().remove(bullet);
}
}
});
timeline.getKeyFrames().add(kf);
timeline.play();
}
Your value for the Duration.millis factor in the KeyFrame was 2, which is not really necessary to run a fluent animation, because JavaFX has a fixed framerate of 60 frame per second, which means that every 16,7 milliseconds a new frame is rendered and displayed. So you can use 16ms as frame-duration without making the animation stutter.
The if-statement checks if the bullet is outside the visible screen, which could happen in your previous code. Non-visible nodes should be removed from the scene graph. It doesn't help if you set a node to setVisible(false), because the node will stay on the scene graph. The Timeline animation should also be stopped, because it would trigger new checkCollision calls. As you can see, i changed the method checkCollision to checkCollisionThreaded. The method is shown below.
public void checkCollisionThreaded(final Rectangle bullet)
{
final int noOfTargetObjs = targetObjects.size();
Task<Integer> t = new Task<Integer>()
{
#Override
protected Integer call() throws Exception
{
for(int i=0; i<noOfTargetObjs;i++)
{
if(targetObjects.get(i).isVisible()==true && bullet.isVisible()==true){
Shape intersectShape= Shape.intersect(bullet, targetObjects.get(i));
if(intersectShape.getBoundsInLocal().getWidth() != -1){
return i;
}
}
}
return -1;
}
#Override
protected void succeeded()
{
super.succeeded();
if(this.getValue().intValue() != -1)
{
Node obj = targetObjects.get(this.getValue().intValue());
obj.setVisible(false);
AnchorPane ap = (AnchorPane) obj.getParent();
ap.getChildren().remove(obj);
targetObjects.remove(this.getValue().intValue());
bullet.setVisible(false);
}
}
};
Thread thread = new Thread(t);
thread.start();
}
There are some violations against the rule "don't touch any objects on the scene graph with a thread different to the JavaFX application thread", but as far as i can see, only reading methods access the scene graph (and it's objects) in the call() method. This method is run on a new Thread, which improves performance. The method succeeded() is run on the JavaFX Application Thread, so that we can safely remove things from our scene graph. I assumed that you want to remove your targets from the scene once they were hit.
It should be said that there might be issues related to the multithreaded code. There could be errors when getting final int noOfTargetObjs = targetObjects.size(); while modifying it on another thread. I left out any synchronization to reduce the complexity of the code.
My guess is that you're sending way too many requests to Shape.intersect(...), which is probably a fairly expensive method to execute. Initially this is causing performance problems, but when the number of calls to the method hits some threshold, the JVM's JIT compiler kicks in and compiles that method, relieving some of the problems. (Again, this is all guesswork.)
Using a TranslateTransition for the bullet and listening to its boundsInParent property to check for collisions seems to work better. I think the reason is that using this technique only checks for collisions when the JavaFX machinery actually moves the bullet. In your code you are performing these checks much more often.
Here's an example.

Categories