package javafximagefilechooser; import java.awt.Color; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; import javafx.application.Application; import javafx.embed.swing.SwingFXUtils; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.Menu; import javafx.scene.control.MenuBar; import javafx.scene.control.MenuItem; import javafx.scene.image.ImageView; import javafx.scene.layout.StackPane; import javafx.scene.layout.VBox; import javafx.stage.FileChooser; import javafx.stage.Stage; import javax.imageio.ImageIO; /** * * @web http://java-buddy.blogspot.com */ public class JavaFXImageFileChooser extends Application { @Override public void start(Stage primaryStage) { final FileChooser fileChooser = new FileChooser(); final Button openButton = new Button("Open Image"); openButton.setOnAction( new EventHandler<ActionEvent>() { @Override public void handle(final ActionEvent e) { setExtFilters(fileChooser); File file = fileChooser.showOpenDialog(primaryStage); if (file != null) { openNewImageWindow(file); } } }); StackPane root = new StackPane(); root.getChildren().add(openButton); Scene scene = new Scene(root, 400, 150); primaryStage.setTitle("java-buddy.blogspot.com"); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } private void setExtFilters(FileChooser chooser){ chooser.getExtensionFilters().addAll( new FileChooser.ExtensionFilter("All Images", "*.*"), new FileChooser.ExtensionFilter("PNG", "*.png") ); } private void openNewImageWindow(File file){ Stage secondStage = new Stage(); MenuBar menuBar = new MenuBar(); Menu menuFile = new Menu("File"); MenuItem menuItem_Save = new MenuItem("Save Image"); menuFile.getItems().addAll(menuItem_Save); menuBar.getMenus().addAll(menuFile); Label name = new Label(file.getAbsolutePath()); BufferedImage bufferedImage = null; try { bufferedImage = ImageIO.read(file); } catch (IOException ex) { Logger.getLogger(JavaFXImageFileChooser.class.getName()).log(Level.SEVERE, null, ex); } ImageView imageView = new ImageView(); final BufferedImage imageToWrite = new BufferedImage( bufferedImage.getWidth(), bufferedImage.getHeight(), BufferedImage.TYPE_INT_RGB); imageToWrite.createGraphics().drawImage(bufferedImage, 0, 0, Color.WHITE, null); menuItem_Save.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { FileChooser fileChooser = new FileChooser(); fileChooser.setTitle("Save Image"); File file = fileChooser.showSaveDialog(secondStage); if (file != null) { try { ImageIO.write(imageToWrite, "jpg", file); } catch (IOException ex) { Logger.getLogger( JavaFXImageFileChooser.class.getName()).log(Level.SEVERE, null, ex); } } } }); final VBox vbox = new VBox(); vbox.setAlignment(Pos.CENTER); vbox.setSpacing(10); vbox.setPadding(new Insets(0, 10, 0, 10)); vbox.getChildren().addAll(name, imageView); imageView.setFitHeight(400); imageView.setPreserveRatio(true); imageView.setImage(SwingFXUtils.toFXImage(bufferedImage, null)); imageView.setSmooth(true); imageView.setCache(true); Scene scene = new Scene(new VBox(), 400, 350); ((VBox)scene.getRoot()).getChildren().addAll(menuBar, vbox); secondStage.setTitle(file.getName()); secondStage.setScene(scene); secondStage.show(); } }
Saturday, December 27, 2014
JavaFX + ImageIO: Convert PNG file to JPG
This example show how to choose PNG file using JavaFX FileChooser, open with ImageIO (actually any supported image format), and save in JPG using ImageIO.
Thursday, December 25, 2014
JavaFX FileChooser, open and save image file
Example to open and save png files, using JavaFX FileChooser.
package javafximagefilechooser; import java.awt.Desktop; import java.io.File; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; import javafx.application.Application; import javafx.embed.swing.SwingFXUtils; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.Menu; import javafx.scene.control.MenuBar; import javafx.scene.control.MenuItem; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.StackPane; import javafx.scene.layout.VBox; import javafx.stage.FileChooser; import javafx.stage.Stage; import javax.imageio.ImageIO; /** * * @web http://java-buddy.blogspot.com */ public class JavaFXImageFileChooser extends Application { @Override public void start(Stage primaryStage) { final FileChooser fileChooser = new FileChooser(); final Button openButton = new Button("Open Image"); openButton.setOnAction( new EventHandler<ActionEvent>() { @Override public void handle(final ActionEvent e) { setExtFilters(fileChooser); File file = fileChooser.showOpenDialog(primaryStage); if (file != null) { openNewImageWindow(file); } } }); StackPane root = new StackPane(); root.getChildren().add(openButton); Scene scene = new Scene(root, 400, 150); primaryStage.setTitle("java-buddy.blogspot.com"); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } private void setExtFilters(FileChooser chooser){ chooser.getExtensionFilters().addAll( new FileChooser.ExtensionFilter("All Images", "*.*"), new FileChooser.ExtensionFilter("PNG", "*.png") ); } private void openNewImageWindow(File file){ Stage secondStage = new Stage(); MenuBar menuBar = new MenuBar(); Menu menuFile = new Menu("File"); MenuItem menuItem_Save = new MenuItem("Save Image"); menuFile.getItems().addAll(menuItem_Save); menuBar.getMenus().addAll(menuFile); Label name = new Label(file.getAbsolutePath()); Image image = new Image(file.toURI().toString()); ImageView imageView = new ImageView(); menuItem_Save.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { FileChooser fileChooser = new FileChooser(); fileChooser.setTitle("Save Image"); File file = fileChooser.showSaveDialog(secondStage); if (file != null) { try { ImageIO.write(SwingFXUtils.fromFXImage(imageView.getImage(), null), "png", file); } catch (IOException ex) { Logger.getLogger( JavaFXImageFileChooser.class.getName()).log(Level.SEVERE, null, ex); } } } }); final VBox vbox = new VBox(); vbox.setAlignment(Pos.CENTER); vbox.setSpacing(10); vbox.setPadding(new Insets(0, 10, 0, 10)); vbox.getChildren().addAll(name, imageView); imageView.setFitHeight(400); imageView.setPreserveRatio(true); imageView.setImage(image); imageView.setSmooth(true); imageView.setCache(true); Scene scene = new Scene(new VBox(), 400, 350); ((VBox)scene.getRoot()).getChildren().addAll(menuBar, vbox); secondStage.setTitle(file.getName()); secondStage.setScene(scene); secondStage.show(); } }
Wednesday, December 24, 2014
To list ImageIO supported read/write file format on your system
Example to list suffixes, format names and MIME types of supported image format on your system, by ImageIO, for read and write.
package javasupportedimageformat; import javax.imageio.ImageIO; /** * * @web http://java-buddy.blogspot.com */ public class JavaSupportedImageFormat { public static void main(String[] args) { //reader support System.out.println("ImageIO reader supported file suffixes:"); String readerFileSuffixes[] = ImageIO.getReaderFileSuffixes(); for(String f : readerFileSuffixes){ System.out.println(f); } System.out.println(); System.out.println("ImageIO reader supported format names:"); String readerFormatNames[] = ImageIO.getReaderFormatNames(); for(String f : readerFormatNames){ System.out.println(f); } System.out.println(); System.out.println("ImageIO reader supported MIME types:"); String readerMIMETypes[] = ImageIO.getReaderMIMETypes(); for(String f : readerMIMETypes){ System.out.println(f); } System.out.println(); //writer support System.out.println("ImageIO writer supported file suffixes:"); String writerFileSuffixes[] = ImageIO.getWriterFileSuffixes(); for(String f : writerFileSuffixes){ System.out.println(f); } System.out.println(); System.out.println("ImageIO writer supported format names:"); String writerFormatNames[] = ImageIO.getWriterFormatNames(); for(String f : writerFormatNames){ System.out.println(f); } System.out.println(); System.out.println("ImageIO writer supported MIME types:"); String writerMIMETypes[] = ImageIO.getWriterMIMETypes(); for(String f : writerMIMETypes){ System.out.println(f); } System.out.println(); } }
Monday, December 22, 2014
Building home gateways with Eclipse SmartHome
Despite the buzz around the Internet of Things (IoT) there are still many devices, systems and protocols for home automation that cannot be directly hooked up to your wifi and used through IP.
To enable smartphone apps to access them and to integrate them with other devices, an IP gateway is required - if it connects to more than just one specific system, this is usually called a home gateway.
Eclipse SmartHome is a flexible software framework for such home gateways. Being initiated by the popular open-source project openHAB, the Eclipse SmartHome project leads the way from the niche to the mass market by fostering industry adoption and by introducing new concepts for setup and configuration procedures of such solutions.
In this webinar you will learn about the concepts of Eclipse SmartHome, what services it provides and how to build a customized solution with it.
Slides are available on Slideshare: http://slideshare.net/xthirtynine/eclipse-smart-home-webinar
Saturday, December 13, 2014
get NetworkInterface and my IP address
Example to display name and ip address of NetworkInterface.
reference: https://docs.oracle.com/javase/tutorial/networking/nifs/listing.html
reference: https://docs.oracle.com/javase/tutorial/networking/nifs/listing.html
package javamyipaddress; import static java.lang.System.out; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.util.Collections; import java.util.Enumeration; /** * * @web http://java-buddy.blogspot.com/ * ref: https://docs.oracle.com/javase/tutorial/networking/nifs/listing.html */ public class JavaMyIpAddress { public static void main(String args[]) throws SocketException { Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces(); for (NetworkInterface netint : Collections.list(nets)) displayInterfaceInformation(netint); } static void displayInterfaceInformation(NetworkInterface netint) throws SocketException { out.printf("Display name: %s\n", netint.getDisplayName()); out.printf("Name: %s\n", netint.getName()); Enumeration<InetAddress> inetAddresses = netint.getInetAddresses(); for (InetAddress inetAddress : Collections.list(inetAddresses)) { out.printf("InetAddress: %s\n", inetAddress); } out.printf("\n"); } }
Saturday, December 6, 2014
JavaFX example, setCursor() and getCursor()
JavaFX example to set cursor type to node by calling setCursor() and get cursor type by calling getCursor().
package javafxhello; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.scene.Cursor; import javafx.scene.Node; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class JavaFXHello extends Application { Scene scene; Button createButton(Cursor cursor) { Button btn = new Button(); btn.setMnemonicParsing(false); //Sets the value of the property mnemonicParsing false to display '_'. /* MnemonicParsing property to enable/disable text parsing. If this is set to true, then the Label text will be parsed to see if it contains the mnemonic parsing character '_'. When a mnemonic is detected the key combination will be determined based on the succeeding character, and the mnemonic added. */ btn.setCursor(cursor); btn.setText(btn.getCursor().toString()); btn.setOnAction((ActionEvent event) -> { Node src = (Node) event.getSource(); scene.setCursor(src.getCursor()); }); return btn; } @Override public void start(Stage primaryStage) { VBox vBox = new VBox(); scene = new Scene(vBox, 300, 250); Button btnCLOSED_HAND = createButton(Cursor.CLOSED_HAND); Button btnCROSSHAIR = createButton(Cursor.CROSSHAIR); Button btnDEFAULT = createButton(Cursor.DEFAULT); Button btnDISAPPEAR = createButton(Cursor.DISAPPEAR); Button btnE_RESIZE = createButton(Cursor.E_RESIZE); Button btnH_RESIZE = createButton(Cursor.H_RESIZE); Button btnHAND = createButton(Cursor.HAND); Button btnMOVE = createButton(Cursor.MOVE); Button btnN_RESIZE = createButton(Cursor.N_RESIZE); Button btnNE_RESIZE = createButton(Cursor.NE_RESIZE); Button btnNONE = createButton(Cursor.NONE); Button btnNW_RESIZE = createButton(Cursor.NW_RESIZE); Button btnOPEN_HAND = createButton(Cursor.OPEN_HAND); Button btnS_RESIZE = createButton(Cursor.S_RESIZE); Button btnSE_RESIZE = createButton(Cursor.SE_RESIZE); Button btnSW_RESIZE = createButton(Cursor.SW_RESIZE); Button btnTEXT = createButton(Cursor.TEXT); Button btnV_RESIZE = createButton(Cursor.V_RESIZE); Button btnW_RESIZE = createButton(Cursor.W_RESIZE); Button btnWAIT = createButton(Cursor.WAIT); vBox.getChildren().addAll( btnCLOSED_HAND, btnCROSSHAIR, btnDEFAULT, btnDISAPPEAR, btnE_RESIZE, btnH_RESIZE, btnHAND, btnMOVE, btnN_RESIZE, btnNE_RESIZE, btnNONE, btnNW_RESIZE, btnOPEN_HAND, btnS_RESIZE, btnSE_RESIZE, btnSW_RESIZE, btnTEXT, btnV_RESIZE, btnW_RESIZE, btnWAIT); primaryStage.setTitle("Hello World!"); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } }
Wednesday, December 3, 2014
JavaFX example of using scene.nodeOrientationProperty().setValue(NodeOrientation.RIGHT_TO_LEFT)
package javafxhello; import javafx.application.Application; import javafx.geometry.NodeOrientation; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class JavaFXHello extends Application { @Override public void start(Stage primaryStage) { HBox hBox123 = new HBox(); hBox123.getChildren().addAll( new Label(" Label 1 "), new Label(" Label 2 "), new Label(" Label 3 ") ); HBox hBoxABC = new HBox(); hBoxABC.getChildren().addAll( new Label(" Label A "), new Label(" Label B "), new Label(" Label C ") ); VBox vBox = new VBox(); vBox.getChildren().addAll(hBox123, hBoxABC); Scene scene = new Scene(vBox, 300, 250); scene.nodeOrientationProperty().setValue(NodeOrientation.RIGHT_TO_LEFT); //hBox123.nodeOrientationProperty().setValue(NodeOrientation.RIGHT_TO_LEFT); //hBoxABC.nodeOrientationProperty().setValue(NodeOrientation.RIGHT_TO_LEFT); primaryStage.setTitle("Hello World!"); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } }
Tuesday, December 2, 2014
Java ME Embedded for Raspberry Pi, Release 8.1
The Oracle Java ME Embedded release 8.1 software for the Raspberry Pi platform is a ready-to-run binary for use with an off-the-shelf Raspberry Pi Model B board. The Oracle Java ME Embedded release 8.1 software underwent sanity check for use with a Raspberry Pi Model B+ board. See the Usage Notes for more details.
The Oracle Java ME Embedded software uses an optimized platform stack for small embedded devices, which includes the Connected Limited Device Configuration (CLDC) HotSpot Implementation (Java Virtual Machine) version 8, the Micro Edition Embedded Profile (MEEP) application environment, the Generic Connection Framework (GCF) API, and enhanced support for various Java Specification Requests (JSRs).
~ Release Notes
~ Getting Started Guide
The Oracle Java ME Embedded software uses an optimized platform stack for small embedded devices, which includes the Connected Limited Device Configuration (CLDC) HotSpot Implementation (Java Virtual Machine) version 8, the Micro Edition Embedded Profile (MEEP) application environment, the Generic Connection Framework (GCF) API, and enhanced support for various Java Specification Requests (JSRs).
~ Release Notes
~ Getting Started Guide