Java fx slowly moving image - java

Currently i'm trying to make an image slowly slide using Java fx from one coordinate to another. For this construction i'm using a pane and have added a shaft imageview and a elevator image to it. I've been succesfull in changing the coordinates of the image but I want the image to slowly slide over to that position. As a result I added a loop that with a thread.sleep method to slow down the looping speed. Unforunately this does not give the desired effect.
Hopefully one of you has a good suggestion I could use for this.
My loop:
Button een = new Button("1");
een.setOnAction(new EventHandler<ActionEvent>(){
#Override
public void handle(ActionEvent event) {
if(elevator.getY()>300){
while(elevator.getY()!=300){
elevator.setY(elevator.getY()-1);
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
Logger.getLogger(LayoutSample.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
});
Yours truly

No loops, no sleep, TranslateTransition, linked javadoc includes a sample.

Related

Image animation issues codename one

want to animate my logo by zooming it to an splash screen.
tried the following so far:
#Override
protected void postSpash1(Form f) {
findLogoLabel(f).setPreferredW(findLogoLabel(f).getPreferredW() * 5);
findAnimateContainer(f).animateLayoutAndWait(2000);
findLogoLabel(f).setPreferredSize(null);
}
The image doesn't animate and enlarges itself. However it changes its size abruptly after 2 sec which is not what I'm looking for. I want the zooming effect(the img should grow bigger slowly to a certain extent)
I tried other ways too, but the label moves from up to down smoothly instead of bg img getting bigger
findLogoLabel(f).setPreferredH(findLogoLabel(f).getPreferredH() * 3);
findAnimateContainer(f).animateLayoutAndWait(2000);
findLogoLabel(f).setPreferredSize(null);
PS: I set the bg image in logoLabel (Label) , Animatecontainer is the container that contains logoLabel.
Blocking API's are somewhat tricky as they might collide with some other behaviors preventing the rest of the current dispatch thread from finishing its job.
Also you set the null after animating instead of before...
So in your case this should work (with old Java 5 syntax):
#Override
protected void postSpash1(final Form f) {
findLogoLabel(f).setPreferredW(findLogoLabel(f).getPreferredW() * 5);
Display.getInstance().callSerially(new Runnable() {
public void run() {
findLogoLabel(f).setPreferredSize(null);
findAnimateContainer(f).animateLayoutAndWait(2000);
}
});
}
With cool Java 8 lambdas:
#Override
protected void postSpash1(final Form f) {
findLogoLabel(f).setPreferredW(findLogoLabel(f).getPreferredW() * 5);
Display.getInstance().callSerially(() -> {
findLogoLabel(f).setPreferredSize(null);
findAnimateContainer(f).animateLayoutAndWait(2000);
});
}

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.

Dynamically change the color of a Rectangle in Javafx

I am creating a two javafx.scene.shape.Rectangle objects in a GridPane and doing the following.
rectArray = new Rectangle[2];
boardGrid.setStyle("-fx-background-color: #C0C0C0;");
rectArray[0] = new Rectangle(12,12);
rectArray[0].setFill(Color.AQUA);
boardGrid.add(rectArray[0], 2, 0);
rectArray[1] = new Rectangle(12,12);
rectArray[1].setFill(Color.BLACK);
boardGrid.add(rectArray[1], 2, 1);
Button buttonStart = new Button("Change color");
buttonStart.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
rectArray[0].setFill(Color.RED);
try {
Thread.sleep(2000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
rectArray[1].setFill(Color.AZURE);
}
});
boardGrid.add(buttonStart, 3, 1);
initializeScene(primaryStage, boardGrid);
...
When I run the code I am able to see two rectangles (One in Aqua and one in black) and when I click the button, I will have to wait for the 2 seconds to view the change in colors of both rectangles.
I change the color of one rectangle before Thread.sleep(2000) is called and then I change the color of the next rectangle.
My question is why am I supposed to wait for 2 seconds? Is there a way that I can dynamically update the colors of the rectangle?
You are sleeping on the UI thread which blocks any further processing (including refreshing the screen).
If you need to delay some code you can use a PauseTransition to wait for two seconds and use its onFinished method to run the rest of your code after the wait.

Animated gif does not play - mouse listener - event mouse entered

I have a button that I have replaced with an image, on hovering I want the image to play an animated gif. I have added a mouse listener and entered the code for changing the image to the gif. The image changes to the gif; however the gif does not animate. I have had a look for previous answers on this site, there are few but none have been able to help.
#Override
public void mouseEntered(MouseEvent arg0) {
try
{
Image img = ImageIO.read(getClass().getResource("images\\button_1_hover.gif"));
btnShip1.setIcon(new ImageIcon(img));
}
catch (IOException ex) {}
}
Don't use a MouseListener for this, just set up the icon using,
setPressedIcon(Icon),
setRolloverIcon(Icon) etc. See this answer for an example.
Don't attempt to load the image 'as needed', but instead load them & set them up on the button when it is initialized.
Change getClass().getResource("images\\button_1_hover.gif") togetClass().getResource("/images/button_1_hover.gif")
Change the method used to load the image. ImageIO will not typically load an animated GIF correctly. See Show an animated BG in Swing for details.
Change code of the form catch (Exception e) { .. to catch (Exception e) { e.printStackTrace(); // very informative! ..

J2Me swipe image left and right

I am learning Java Program for Mobile, I am trying to create a program thru which I can show images on screen by dragging from left to right, or right to left.
I have touch screen mobile, no harrdware keyboard.
I am trying the below code in which I have two forms and I want to show the other form when I swip the fingure on screen.
import java.io.IOException;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class ImageDisplay extends MIDlet{
private Display display;
private Form form1,form2;
private Image myImage1,myImage2;
public void startApp(){
display = Display.getDisplay(this);
form1 = new Form ("Image Display");
form2 = new Form("Second Image");
try {
myImage1 = Image.createImage("/bgscaled.jpg");
myImage2 = Image.createImage("/spla77sh.jpg");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
form1.append(myImage1);
form2.append(myImage2);
display.setCurrent(form1);
display.setCurrent(form2);
}
public void pauseApp(){
}
public void destroyApp(boolean unconditional){
notifyDestroyed();
}
}
You do not have a way to be notified by an LCDUI Form when a screen swipe happens.
You can add "Next" and "Previous" buttons and change the forms when the buttons are clicked.
You will need to use Canvas instead of the Form screens as Canvas has various methods (e.g., pointerDragged) that can be used to determine touch, release and dragging actions.
Using Canvas can be a lot of work though (because your code will be responsible for every pixel that is drawn on the display). However, you may find J2ME Polish useful to transfer your Form UI to Canvas. (I'm not sure, but J2ME Polish might also take care of the swiping behaviour you are looking for, too.)

Categories