Sunday, November 10, 2013

Get screen size using javafx.stage.Screen

javafx.stage.Screen class describes the characteristics of a graphics destination such as monitor. In a virtual device multi-screen environment in which the desktop area could span multiple physical screen devices, the bounds of the Screen objects are relative to the Screen.primary. We can obtain the width and height of screen by calling Screen.getPrimary().getVisualBounds().getWidth() and Screen.getPrimary().getVisualBounds().getHeight().

This example set the application run in full screen.
application run in full screen
JavaFX application run in full screen

package javafx_screen;

import javafx.application.Application;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Screen;
import javafx.stage.Stage;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFX_Screen extends Application {

    @Override
    public void start(Stage primaryStage) {
        
        Rectangle2D visualBounds = Screen.getPrimary().getVisualBounds();
        
        Label label = new Label();
        label.setText(
                visualBounds.getWidth() + " x " + visualBounds.getHeight());

        StackPane root = new StackPane();
        root.getChildren().add(label);
        
        Scene scene = new Scene(root, 
                visualBounds.getWidth(), visualBounds.getHeight());
        
        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

}

Saturday, November 9, 2013

JavaFX example: how to set icon of application window

This example show how to set icon of application window, by calling:

Stage.getIcons().add(applicationIcon);

Also show how to set icon dynamically at run-time, when 'Hello World' button clicked.

set icon of application window
set icon of application window

package javafx_applicationicon;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFX_ApplicationIcon extends Application {

    @Override
    public void start(final Stage primaryStage) {

        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            
            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
                
                //load another image from internet
                //and dynamically add it as new apllication icon
                Image anotherIcon = new Image("http://goo.gl/kYEQl");
                primaryStage.getIcons().add(anotherIcon);
            }
        });
        
        StackPane root = new StackPane();
        root.getChildren().add(btn);
        
        Scene scene = new Scene(root, 300, 250);
        
        //set icon of the application
        Image applicationIcon = new Image(getClass().getResourceAsStream("dukes_36x36.png"));
        primaryStage.getIcons().add(applicationIcon);
        
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
    
}

Sunday, November 3, 2013

Store numbers of different types in a List

To store numbers of different types (Integer, Float, Double...) in a single List, we can define the list of type Number.

Example:

package javanumber;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaNumber {

    public static void main(String[] args) {
        List<Number> numberList = new ArrayList<>();
        numberList.add((int)123);
        numberList.add(123456.7);
        numberList.add((double)456.789);
        numberList.add(0.1f);
        
        for(Number num:numberList){
            System.out.println(num + " of type " + num.getClass());
        }
    }
}

List contain numbers of different type
A single List contain numbers of Integer, Double and Integer