In Java 7, transparent GIF doesn't animate properly - animation

So, I'm having an issue with a program. I have a GIF that's transparent. The animation works properly, except that instead of showing one image at a time, it sort of stacks the images on top of each other. I tried overriding the paintIcon method of the ImageIcon class to clear the canvas, but that didn't work either. ANy thoughts?
My code:
public class GifRunner extends JPanel {
JLabel label;
public GifRunner() {
super();
label = new JLabel();
ImageIcon icon = new ImageIcon(getClass().getResource("/animation.gif");
label.setIcon(icon);
add(label);
}
public static void main(String[] args) {
JFrame frame = new JFrame("");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GifRunner panel = new GifRunner();
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
}
}
Any help would be much appreciated

you can override the ImageIcon's paintIcon function and manually clear the canvas:
class ClearImageIcon extends ImageIcon{
public ClearImageIcon(String filename){super(filename);}
#Override
public synchronized void paintIcon(Component c, Graphics g, int x, int y) {
Graphics2D g2 = (Graphics2D)g.create();
g2.setBackground(new Color(0,0,0,0));
g2.clearRect(0, 0, getIconWidth(), getIconHeight());
super.paintIcon(c, g2, x, y);
}
}
it will draw every frame nicely on to the screen.

Works fine for me...
Windows 7, Java 7.
Original
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class AnimatedGifTest {
public static void main(String[] args) {
new AnimatedGifTest();
}
public AnimatedGifTest() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
ImageIcon image = new ImageIcon("C:\\Users\\swhitehead\\Documents\\My Dropbox\\Ponies\\appleture_animated_gif_by_inkwell_pony-d4uggao.gif");
JLabel label = new JLabel(image);
add(label);
}
}
}

Related

How to disable macOS genie effect when JavaFX FileChooser opens?

When opening a JavaFX FileChooser within a Stage (only way to make it modal), there is an annoying "Genie" effect (see code and movie below). How can it be disabled ?
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.stage.Stage;
import javafx.stage.FileChooser;
import javafx.concurrent.Task;
public class Test {
private static void initFX() {
// This method is invoked on the JavaFX thread
Stage stage = new Stage();
FileChooser fileChooser = new FileChooser();
stage.setMaxHeight(0);
stage.setWidth(0);
stage.setTitle("Choose an file");
stage.setResizable(false);
stage.setAlwaysOnTop(true);
stage.show();
fileChooser.showOpenDialog(stage);
stage.hide();
}
public static void main(String[] args) {
final JFXPanel fxPanel = new JFXPanel();
Platform.runLater(new Runnable() {
#Override
public void run() {
initFX();
}
});
}
}

Dialogue Positioning Javafx

I am trying to create a Javafx game and I want the user to be able to input their name using a dialogue. The problem is that when I try to position the dialogue using setY() or setX() nothing changes. I should note that I am using an online IDE so if this is the problem please somebody tell me.
import javafx.stage.Modality;
import javafx.scene.control.*;
import javafx.scene.control.Dialog;
import javafx.geometry.*;
import javafx.stage.Stage;
import javafx.scene.Scene;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
VBox page = new VBox();
Scene scene = new Scene(page, 600, 700);
primaryStage.setScene(scene);
primaryStage.show();
setUserName();
}
public static void setUserName() {
Dialog<String> dialog = new Dialog<String>();
dialog.setTitle("Dialog");
ButtonType type = new ButtonType("Ok", ButtonData.OK_DONE);
dialog.initModality(Modality.APPLICATION_MODAL);
dialog.initStyle(StageStyle.UNDECORATED);
dialog.setContentText("Hello World");
dialog.setY(100);
dialog.showAndWait();
}
public static void main(String[] args) {
launch(args);
}
}

JavaFx Browser support Oracle Forms

I want to create a simple project in JavaFx, which can support Oracle forms 10g. currently code i'm using can render and HTML page but i unable to render any Oracle forms 10g. Is it possible to run oracle form 10g in javaFx browser and how ?
import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.VPos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class Main extends Application {
private Scene scene;
#Override public void start(Stage stage) {
// create the scene
stage.setTitle("Web View");
scene = new Scene(new Browser(),900,600, Color.web("#666970"));
stage.setScene(scene);
scene.getStylesheets().add("webviewsample/BrowserToolbar.css");
stage.show();
}
public static void main(String[] args){
launch(args);
}
}
class Browser extends Region {
final WebView browser = new WebView();
final WebEngine webEngine = browser.getEngine();
public Browser() {
//apply the styles
getStyleClass().add("browser");
// load the web page
webEngine.load("http://www.google.com");
//add the web view to the scene
getChildren().add(browser);
}
private Node createSpacer() {
Region spacer = new Region();
HBox.setHgrow(spacer, Priority.ALWAYS);
return spacer;
}
#Override protected void layoutChildren() {
double w = getWidth();
double h = getHeight();
layoutInArea(browser,0,0,w,h,0, HPos.CENTER, VPos.CENTER);
}
#Override protected double computePrefWidth(double height) {
return 900;
}
#Override protected double computePrefHeight(double width) {
return 600;
}
}

Slide in/out JavaFX stage from/to right side of the screen

I have a task to make JavaFX application. When I start it the window should slides smoothly from right side of screen and when I click on "x" button it's should slides out to right side and then finishes.
I found possible to use simple Timeline animation for this. I made the window to slide in, when I run app, but can't figure out how to slide out window.
I tried to handle this defining handler via setOnCloseRequest() method of stage, but stuck with two problems:
can't implement animation
after click on "x" application closed immediately even if I use consume() method of Window event
Code:
public class Main extends Application {
public static void main(String[] args) {
Application.launch(args);
}
#Override
public void start(Stage stage) {
stage.setTitle("Main");
Group root = new Group();
Scene scene = new Scene(root, 300, 250);
stage.setScene(scene);
Rectangle2D primScreenBounds = Screen.getPrimary().getVisualBounds();
stage.setX(primScreenBounds.getMinX() + primScreenBounds.getWidth());
System.out.println(primScreenBounds.getWidth());
stage.setY(primScreenBounds.getMinY());
stage.setWidth(0);
stage.setHeight(primScreenBounds.getHeight());
Timeline timeline = new Timeline();
timeline.setAutoReverse(true);
WritableValue<Double> writableWidth = new WritableValue<Double>() {
#Override
public Double getValue() {
return stage.getWidth();
}
#Override
public void setValue(Double value) {
stage.setWidth(value);
}
};
KeyValue kv = new KeyValue(writableWidth, 600d);
KeyFrame kf = new KeyFrame(Duration.millis(3000), kv);
timeline.getKeyFrames().addAll(kf);
timeline.play();
stage.show();
stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
#Override
public void handle(WindowEvent event) {
event.consume();
}
});
}
}
This works (kind of, it is not very smooth) for me:
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.value.WritableValue;
import javafx.event.EventHandler;
import javafx.geometry.Rectangle2D;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import javafx.util.Duration;
public class SlidingWindow extends Application {
public static void main(String[] args) {
Application.launch(args);
}
#Override
public void start(Stage stage) {
stage.setTitle("Main");
Group root = new Group();
Scene scene = new Scene(root);
stage.setScene(scene);
Rectangle2D primScreenBounds = Screen.getPrimary().getVisualBounds();
double screenRightEdge = primScreenBounds.getMaxX() ;
stage.setX(screenRightEdge);
System.out.println(primScreenBounds.getWidth());
stage.setY(primScreenBounds.getMinY());
stage.setWidth(0);
stage.setHeight(primScreenBounds.getHeight());
Timeline timeline = new Timeline();
WritableValue<Double> writableWidth = new WritableValue<Double>() {
#Override
public Double getValue() {
return stage.getWidth();
}
#Override
public void setValue(Double value) {
stage.setX(screenRightEdge - value);
stage.setWidth(value);
}
};
KeyValue kv = new KeyValue(writableWidth, 600d);
KeyFrame kf = new KeyFrame(Duration.millis(3000), kv);
timeline.getKeyFrames().addAll(kf);
timeline.play();
stage.show();
stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
#Override
public void handle(WindowEvent event) {
Timeline timeline = new Timeline();
KeyFrame endFrame = new KeyFrame(Duration.millis(3000), new KeyValue(writableWidth, 0.0));
timeline.getKeyFrames().add(endFrame);
timeline.setOnFinished(e -> Platform.runLater(() -> stage.hide()));
timeline.play();
event.consume();
}
});
}
}
The Platform.runLater(...) seems necessary to prevent a slew of NullPointerExceptions when the window is hidden, probably because the animation is causing some system to try to access a stage that no longer exists.

Add image to a button at a specific position JavaFX

When I add image and text to a button, by default elements are set horizontally. How can I change this behavior to get text under image ?
Set the contentDisplayProperty on the button.
button.setContentDisplay(ContentDisplay.TOP);
Here is an executable example:
import javafx.application.Application;
import javafx.event.*;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class ButtonGraphicTest extends Application {
#Override public void start(final Stage stage) throws Exception {
final Label response = new Label();
final ImageView imageView = new ImageView(
new Image("http://icons.iconarchive.com/icons/eponas-deeway/colobrush/128/heart-2-icon.png")
);
final Button button = new Button("I love you", imageView);
button.setStyle("-fx-base: coral;");
button.setContentDisplay(ContentDisplay.TOP);
button.setOnAction(new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent event) {
response.setText("I love you too!");
}
});
final VBox layout = new VBox(10);
layout.setAlignment(Pos.CENTER);
layout.getChildren().addAll(button, response);
layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 10; -fx-font-size: 20;");
stage.setScene(new Scene(layout));
stage.show();
}
public static void main(String[] args) { launch(args); }
}
// icon license: (creative commons with attribution) http://creativecommons.org/licenses/by-nc-nd/3.0/
// icon artist attribution page: (eponas-deeway) http://eponas-deeway.deviantart.com/gallery/#/d1s7uih

Resources