Making listener wait javafx - java

I checked some questions here but I couldn't find a proper answer. Maybe I am using wrong keywords to search. But what I found didn't help. So here is my question. I have some circles and line that are sliding in a certain way.
What I want is to update balance variable after the animations perform.
DoubleProperty balance = new SimpleDoubleProperty();
And I update the balance as follows:
if (shapeADone == false) {
intersecting = arc.centerXProperty().lessThan(boldLine.getEndX() - 13);
intersecting.addListener((obs, wasIntersecting, isNowIntersecting) -> {
System.out.println("Collision!");
animation1.stop();
animation2.stop();
animation3.stop();
});
again = true;
balance.setValue(-1);
} else {
fadeB();
balance.setValue(1);
}
But in main method I want to do something like this
level1.balance.addListener(ov -> {
System.out.println("The new value is " +
level1.balance.doubleValue());
if (level1.balance.getValue()==1) {
//delay setting scene
primaryStage.setScene(scene2);
}
});
I have some animations to perform before setting scene2 but since balance instantly updating, my animations can't be performed.
I want to know if there is a way to delay listening balance or setting scene.
I tried Thread.sleep and balance.wait but it gives runtime errors.
Edit: This question clearly shows that I was lack of knowlodge about javafx. What I want to do is simple and solution is even more simple. When animation perform to the end I update the value of balance. All I wanted was make sure that animation is showed to the end.

Here is the answer :
else{
animation3.setOnFinished(event -> balance.setValue(1));
fadeB();
}
As Sedrick's comment I change the code like this.
Adding setOnFinish to animations which need to be performed solve the problem. It's working.

Related

RotateTransition causing GUI to freeze in javaFX [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
In general, I do not tend to ask for help when it comes to bug fixing, however, I have a very weird one I have spent 2 days trying to fix it without any success. My class is basically a roulette with multiple dificulties so, I used a pieChart to change the dificulties and redraw the roulette fast and with the cool animations that comes with pieChart, however, the method that makes my roulette roll to a certain angle has a big problem, after the RotateTransition ends the GUI freezes complitly. I don´t know why, cause i have other RotateTranstions in other clases and they work fine, also, if i remove the rotateTransition or use other component in it (i tried using images insted of pieChart) it works just fine. I don´t know if pieChart has something special that makes it imposible to use with RotateTransition, also the only exeption to the freeze is the button that allow me to bet again, and yes i already tried "Platform.runLatter()"
#Override
public void initialize(URL url, ResourceBundle rb) {
rotateTransition = new RotateTransition();
rotateTransition.setNode(pieChart);
rotateTransition.setDuration(Duration.seconds(1));
rotateTransition.setByAngle(-360);
rotateTransition.setCycleCount(1);
rotateTransition.setAutoReverse(false);
pieChart.setData(dataForX1_5);
pieChart.setStartAngle(90);
applyDataWithCustomColor(dataForX1_5, "green", "red");
betButton.setOnAction(eh -> {
double betMultiplier=1.5;
if(rbX1_5==group.getSelectedToggle()){
pieChart.setData(dataForX1_5);
applyDataWithCustomColor(dataForX1_5, "green", "red");
betMultiplier=1.5;
}
if(rbX2==group.getSelectedToggle()){
pieChart.setData(dataForX2);
applyDataWithCustomColor(dataForX2, "green", "red");
betMultiplier=2;
}
if(rbX5==group.getSelectedToggle()){
pieChart.setData(dataForX5);
applyDataWithCustomColor(dataForX5, "green", "red");
betMultiplier=5;
}
if(rbX10==group.getSelectedToggle()){
pieChart.setData(dataForX10);
applyDataWithCustomColor(dataForX10, "green", "red");
betMultiplier=10;
}
if(rbX25==group.getSelectedToggle()){
pieChart.setData(dataForX25);
applyDataWithCustomColor(dataForX25, "green", "red");
betMultiplier=25;
}
if(rbX50==group.getSelectedToggle()){
pieChart.setData(dataForX50);
applyDataWithCustomColor(dataForX50, "green", "red");
betMultiplier=50;
}
try {
int betAmount = betAmountSpinner.getValue();
int spectedResult = (int) (betAmount * betMultiplier);
if (coins >= betAmount) {
dataOutputStream.writeUTF("PlaceBetOnUpgrade");
dataOutputStream.writeInt(betAmount);
dataOutputStream.writeInt(spectedResult);
if (dataInputStream.readBoolean()) {
int result = dataInputStream.readInt();
int payment = dataInputStream.readInt();
coins = coins - betAmount;
rotateTo(result, payment);
updateCoinsLabel();
}
}
} catch (IOException ex) {
ex.printStackTrace();
}
});
}
private void rotateTo(int result, int payment) {
rotateTransition.setToAngle(result * 3.60);
rotateTransition.setOnFinished(rotateEvent -> {
if (0 == payment) {
resultLabel.setText("Lost");
} else {
resultLabel.setText("Win");
coins = coins + payment;
updateCoinsLabel();
}
});
rotateTransition.play();
}
private void updateCoinsLabel() {
coinsLabel.setText(String.valueOf(coins));
}
Well i endend up findind the solution to my problem and it was exactly where i said i should be. The problem is that even though you can technically rotate a pieChart using RotateTransition, you have to be aware that when doing it, you are not only rotating the pieChart that you see on the screen but also a transparent layer that is behind it and that even when disable the labels and legends will ocuppy the form of a rectangular with the max posible size. Therefore, when you rotate the pieChart the transparent layer will overlap all your components causing a freeze without any exception. The solution that most would recommend is just to set it as mouse transparent and allow click penetration but, it won´t work, cause it is bugged and will only allow the click penetration in the pieChart but not in the transparent layer, so if you want to rotate a pieChart you need it to be in the center of the screen so even if it roates to a 90º angle it will only overlap the things that are outside of the screen, so it doesn´t matter. if you need to place it elsewhere, you will need to set its max size to something diferent to Max_size and check that it nevers ovelaps anywhing, however, it certainly limits your view quality in high resolutions.

How to handle event in a program running in a cycle (loop)?

I´ve been dealing with this for some days now I´ve serach over the internet and tried everything that came up to my mind but nothing works. My problem is that I am making a pexeso game, we have some additionaly library directly from school or something like that which allows us to draw some pictures instead of comad line only... (We didn´t have graphics yet) So my problem is that my game is running in cycle and waiting for my click som I am checking the condition if click was made. And if I want to click I have to have just a method public void vyberSuradnice(int, int) declared in my code.
So the problem is that my game is runinng in cycle and checking if click was made. But when the click was made the method vyberSuradnice is executed and there I am setting the value off atrrbiute - cakatNaKlik on false, so click was made and one step of game can be made.. But since the function is running in cycle, then even If I clicked and in the method vyberSuradnice the value of attribute is changed, my function which is running in cycle isn´t respond to that change, so the game isn´t going on.
this is the method for clicking
public void vyberSuradnice(int paSuradnicaX, int paSuradnicaY) {
this.riadokOdkry = (paSuradnicaY ) / 25;
this.stlpecOdkry = (paSuradnicaX - 10) / 25;
if (this.riadokOdkry > this.aPocetRiadkov || this.stlpecOdkry > this.aPocetStlpcov) {
System.out.println("Klikli ste mimo hracieho pola ");
} else {
this.cakatNaKlik = false;
}
}
This the part of code where I am waiting for cakatNaklik - false value
while (uhadol) {
if (!this.cakatNaKlik) {
if (this.pocetUhadnutych >= (this.aPocetRiadkov * this.aPocetStlpcov) / 2) {
uhadol = false;
}
this.hraciaPlocha[this.riadokOdkry][this.stlpecOdkry].setUhadnute(true);
But even if the value is changed in method vyberSuradnice this condition is not triggered. But when I make something like this :
while (uhadol) {
System.out.print(this.cakatNaKlik);
if (!this.cakatNaKlik) {
if (this.pocetUhadnutych >= (this.aPocetRiadkov * this.aPocetStlpcov) / 2) {
uhadol = false;
}
this.hraciaPlocha[this.riadokOdkry][this.stlpecOdkry].setUhadnute(true);
the game is working like by writing the variable refresh it or something... but I am getting neverending print of true true or false on command line and this is something I can´t afford to have...
I know this may can be dan by threads but it´s something I can´t allowed to make and I am basically trying to do two things at once.
Is there any other to do this that the variable will be refreshed even without that println and the code will work ?
Thanks very much for every help
Thanks everybody for help, I finally solved it. I just tried tu put a sleep before the if condition. It only need to sleep for even a one miliseconds and it´s seems that the condition is refreshed so it works.
Try this:
Implement an ActionListener interface or extend a class that implements an ActionListener interface. For example:
public class MyClass implements ActionListener {
}
Register an instance of the event handler class as a listener on one or more components. For example:
someComponent.addActionListener(instanceOfMyClass);
instanceOfClass = this; //(if it is handled in the same Class)
Include code that implements the methods in listener interface. For example:
public void actionPerformed(ActionEvent e) {
...//code that reacts to the action...
}
Reference:
https://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

I don't understand this java behaviour

I had a question about making a JButton flash colors ,like you would see when an answer was given in millionaire tv show. I got the answer there on how to do it properly but I also managed to "do it" in this way which raised some questions I couldn't answer totally.
As you see in the code bellow I am calling a
JOptionPane.showMessageDialog() after the JButton.setBackground and right before i stall the code using
do {
}while(time+i*100>System.currentTimeMillis());
Ignore the robot.keypress for the time. Lets get to the point. If i didn't use the JOptionPane before the code stalling , the ui would seem frozen and the button wouldn't repaint. But calling the JOptionPane.ShowMessageDialog() gives "time" to the button to repaint. Then the code is stalled normally and I achieve the sequential color repaint. I used the robot.keypress to close the Pane and achieve the effect desired.
My Questions: First, what happens when the JOptionPane is created that allows the button to repaint ? And secondly why the robot works only before the JOptionPane is called? I tried calling after the Pane was called like one would assume it should happen , but it wouldn't work in that case.
Extra: This didn't work in a mac it seems to only work for windows. Not quite sure.
public static void paintbutton(int bnr,boolean corr) {
long time;
try {
Robot robot = new Robot();
for (int i=5;i>1;i--){
b[bnr-1].setBackground(null);
// Simulate a key press
robot.keyPress(KeyEvent.VK_SPACE);
robot.keyRelease(KeyEvent.VK_SPACE);
JOptionPane.showMessageDialog(null,"hi");
time = System.currentTimeMillis();
do {
}while(time+i*100>System.currentTimeMillis());
b[bnr-1].setBackground(i==1?(corr?Color.green:Color.red):Color.yellow);
// Simulate a key press
robot.keyPress(KeyEvent.VK_SPACE);
robot.keyRelease(KeyEvent.VK_SPACE);
JOptionPane.showMessageDialog(null,"hi");
time = System.currentTimeMillis();
do {
}while(time+i*100>System.currentTimeMillis());
}
} catch (AWTException e) {
System.err.println("error");
}
}
To avoid confusion as to the nature of this question!
The code in the state below doesn't work and I know it shouldn't. I am curious on how adding JOptionPane solves that.
public static void paintbutton(int bnr,boolean corr) {
long time;
for (int i=5;i>1;i--){
b[bnr-1].setBackground(null);
time = System.currentTimeMillis();
do {
}while(time+i*100>System.currentTimeMillis());
b[bnr-1].setBackground(i==1?(corr?Color.green:Color.red):Color.yellow);
time = System.currentTimeMillis();
do {
}while(time+i*100>System.currentTimeMillis());
}
}

GWT infinite scroll with discarding start-of-list results

Looking for a GWT DataGrid component which implements infinite scroll, BUT also makes sure to discard the results no longer visible on the screen : such as the previously loaded results that are not shown anymore.
This is to avoid a memory hog.
I've been trying to find this on Google, but no luck so far.
Please note : I could take a JS library and adapt it to what I need, but I don't think it would work good with GWT's DataGrid component.
Edit: I am interested specifically in an infinite scroll which ALSO discards/releases the topmost results that are not visible (and loads them up as appropriate).
Any ideas ?
As a matter of fact the showcase example has an infinite scrolling CellList. (you can find the code there).
Although this was done with a CellList the same principles should also apply to a DataGrid.
Check out the ShowMorePagerPanel.java file.
Update:
The onScroll function of ShowMorePagerPanel.java will add the new records at the bottom. However you can easily change the behavior:
Something along the lines (not tested tough):
HasRows display = getDisplay();
if (display == null) {
return;
}
boolean loadData = false;
// If scrolling up, change newStart
int oldScrollPos = lastScrollPos;
lastScrollPos = scrollable.getVerticalScrollPosition();
// get the current visible Range
Range currentRange = display.getVisibleRange();
if (oldScrollPos >= lastScrollPos) {
int newStart = Math.max(
currentRange.getStart() - incrementSize,0);
loadData = true;
}
int maxScrollTop = scrollable.getWidget().getOffsetHeight()
- scrollable.getOffsetHeight();
if (lastScrollPos >= maxScrollTop) {
// We are near the end, so increase the page size.
int newPageSize = Math.min(
display.getVisibleRange().getLength() + incrementSize,
display.getRowCount());
loadData = true;
}
if (loadData) {
display.setVisibleRange(newStart, newPageSize);
}

Do not change right the icon,java

i have a problem with my code. I think that my problem is easy,but i have compiled for 3 days without good results. I have three images. They are put on screen one-one each time. User choose from 4 button if image's side is up, down, right or left. Also, i want to understand if user was wrong and then i will count errors. When user make 3 errors then the game will stop. I have shown code below. Please help me if you have any good idea.
The problem is that at the first loop,run right.It goes at the first if. After that it do the loop and then it does not go to second if.
if it is more helpful,some details:
i want to make a programma that it will show to user an image.This image has 4 sides (up,down,right,left).When the image is at "up side",user has to click on up button,when the image is at "down side",user has to click on down button etc. User can do 3 errors max. At first,program show the image at right side,if user clicks on right button then i want to show the "second image" at left side.If user does not at left side,then i want to add an error(error++) and after it shows the third image at up side etc. I hope it is more helpful to understand. If you can't please let me know.
My program is at Netbeans,java.
Thank you
public void actionPerformed(ActionEvent e)
{
while(errors<3)
{
image.setIcon(createImageIcon("visual1" + e.getActionCommand() + ".PNG"));
if (k==1)
{
if(e.getSource() == right_button)
{
image.setIcon(createImageIcon("visual2" + e.getActionCommand() + ".PNG"));
}
}
else if ( k==2 )
{
if(e.getSource() == left_button )
{
image.setIcon(createImageIcon("visual3" + e.getActionCommand() + ".PNG"));
}
}
else if (k==3 )
{
if(e.getSource() == up_button)
{
System.out.print("if3");
}
}
else
{
errors++;
}
k=k+1;
}
}
You should consider calling Repaint and Invalidate, right after you update your GUI like this -
mainframe.repaint();
mainframe.invalidate();
here mainframe is your JFrame object.
A problem I see with your while loop is that it is at risk of getting stuck in an infinite loop, since the variable used as an exit criterion is only updated some of the time, in an else block. I think you should re-arrange your logic:
Get rid of that while loop as it will only cause trouble. It is useful for a linear command line program but not for an event-driven GUI program like yours.
Read in all images and create all ImageIcons in the class constructor, and store them in variables. There's no need to re-read the images multiple times (unless they're huge).
Instead of using a while loop, increment the error variable in your method above, and then write the method so that it will change behaviors depending on the value of error (depending on the state of the class).
e.g.,
// somewhere in your code create your icons
Icon rightIcon = ......;
Icon leftIcon = .....;
Icon upIcon = .....;
Icon downIcon = .....;
// elsewhere in your code
public void actionPerformed(ActionEvent e) {
if (errors >= 3) {
// notify user of error
return; // end this method
}
// check if current icon matches image
// if so, change icon
// if not increment error
}
Note that an enum Direction {UP, DOWN, LEFT, RIGHT} and a Map<Direction, Icon> could be helpful here.

Categories

Resources