Saturday, 26 July 2014

JAVAFX : How to update .css runtime

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class TrackerClient extends Application {
@Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("Submit");
StackPane root = new StackPane();
root.getChildren().add(btn);
final Scene scene = new Scene(root, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
scene.getStylesheets().add(getClass().getResource("style1.css").toExternalForm());
primaryStage.addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent keyEvent) {
if (keyEvent.getCode().equals(KeyCode.F5)) {
scene.getStylesheets().clear();
scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());
} else {
scene.getStylesheets().clear();
scene.getStylesheets().add(getClass().getResource("style1.css").toExternalForm());
}
}
});
}
public static void main(String[] args) {
launch(args);
}
}
//style.css
.button{
-fx-text-fill: rgb(49, 89, 23);
-fx-border-color: rgb(49, 89, 23);
-fx-border-radius: 5;
-fx-padding: 3 6 6 6;
}
//style1.css
.button{
-fx-text-fill: rgb(149, 189, 123);
-fx-border-color: rgb(149, 189, 123);
-fx-border-radius: 7;
-fx-padding: 5 8 8 8;
}
view raw CSSUpdate.java hosted with ❤ by GitHub

Friday, 25 July 2014


A small prototype for a switch-lights problem (four or more consecutive lights will be turned off) By Henry Chen https://henry416.wordpress.com

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class LightSwitch extends Application {
int[] array = new int[25];
Circle[] light = new Circle[25];
@Override
public void start(Stage stage) {
init(stage);
stage.show();
}
public void init(Stage stage) {
Group root = new Group();
Scene scene = new Scene(root, 700, 500, Color.WHITE);
for (int i = 0; i < 25; i++) {
circleChange(i, root);
}
root.getChildren().addAll(light);
stage.setTitle("Lights Problem");
stage.setScene(scene);
}
public void circleChange(final int i, final Group root) {
light[i] = new Circle(i * 25 + 25, 100, 10);
light[i].setOnMouseClicked(new EventHandler<MouseEvent>() {
public void handle(MouseEvent me) {
if (array[i] == 1)
array[i] = 0;
else
array[i] = 1;
int counter = 0;
int place = -1;
boolean x = false;
for (int a = 0; a < 25; a++) {
if (array[a] == 1) {
if (x == false) {
place = a;
x = true;
}
counter++;
} else {
if (counter >= 4 && x == true)
a = 26;
else {
x = false;
counter = 0;
place = -1;
}
}
}
if (counter >= 4 && x == true)
for (int b = 0; b < counter; b++)
array[b + place] = 0;
paint();
me.consume();
}
});
}
public void paint() {
for (int a = 0; a < 25; a++) {
if (array[a] == 0)
light[a].setFill(Color.BLACK);
else
light[a].setFill(Color.RED);
}
}
public static void main(String args[]) {
Application.launch(args);
}
}


Wednesday, 23 July 2014

Exception : Numbers of source Raster bands and source color space components do not match when read image

import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class StrangeImageTest {
public static void main(String[] args) throws IOException {
final BufferedImage image = readImage(new File("BigPoster.jpg.jpg"));
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame f = new JFrame();
f.getContentPane().add(new JLabel(new ImageIcon(image)));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
});
}
static BufferedImage readImage(File file) throws IOException {
return readImage(new FileInputStream(file));
}
static BufferedImage readImage(InputStream stream) throws IOException {
Iterator<ImageReader> imageReaders = ImageIO
.getImageReadersBySuffix("jpg");
ImageReader imageReader = imageReaders.next();
ImageInputStream iis = ImageIO.createImageInputStream(stream);
imageReader.setInput(iis, true, true);
Raster raster = imageReader.readRaster(0, null);
int w = raster.getWidth();
int h = raster.getHeight();
BufferedImage result = new BufferedImage(w, h,
BufferedImage.TYPE_INT_RGB);
int rgb[] = new int[3];
int pixel[] = new int[3];
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
raster.getPixel(x, y, pixel);
int Y = pixel[0];
int CR = pixel[1];
int CB = pixel[2];
toRGB(Y, CB, CR, rgb);
int r = rgb[0];
int g = rgb[1];
int b = rgb[2];
int bgr = ((b & 0xFF) << 16) | ((g & 0xFF) << 8) | (r & 0xFF);
result.setRGB(x, y, bgr);
}
}
return result;
}
// Based on http://www.equasys.de/colorconversion.html
private static void toRGB(int y, int cb, int cr, int rgb[]) {
float Y = y / 255.0f;
float Cb = (cb - 128) / 255.0f;
float Cr = (cr - 128) / 255.0f;
float R = Y + 1.4f * Cr;
float G = Y - 0.343f * Cb - 0.711f * Cr;
float B = Y + 1.765f * Cb;
R = Math.min(1.0f, Math.max(0.0f, R));
G = Math.min(1.0f, Math.max(0.0f, G));
B = Math.min(1.0f, Math.max(0.0f, B));
int r = (int) (R * 255);
int g = (int) (G * 255);
int b = (int) (B * 255);
rgb[0] = r;
rgb[1] = g;
rgb[2] = b;
}
}

JavaFX: Zoom all Pane Content/Children on Stage resize

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.image.*;
import javafx.scene.layout.Pane;
import javafx.scene.transform.Scale;
import javafx.stage.Stage;
public class ScaledPaneSample extends Application {
public static void main(String[] args) throws Exception {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
// create a pane with various objects on it.
final Pane pane = new Pane();
pane.setStyle("-fx-background-color: linear-gradient(to bottom right, derive(goldenrod, 20%), derive(goldenrod, -40%));");
final Label resizeMe = new Label("Resize me");
resizeMe.setAlignment(Pos.CENTER);
resizeMe.prefWidthProperty().bind(pane.widthProperty());
final ImageView iv1 = new ImageView(
new Image(
"http://icons.iconarchive.com/icons/kidaubis-design/cool-heroes/128/Ironman-icon.png"));
final ImageView iv2 = new ImageView(
new Image(
"http://icons.iconarchive.com/icons/kidaubis-design/cool-heroes/128/Starwars-Stormtrooper-icon.png"));
iv1.relocate(10, 10);
iv2.relocate(80, 60);
Button button = new Button("Zap!");
button.relocate(25, 140);
pane.getChildren().addAll(resizeMe, iv1, iv2, button);
// layout the scene.
final Group group = new Group(pane);
final Scene scene = new Scene(group);
stage.setScene(scene);
stage.show();
// scale the entire scene as the stage is resized.
final double initWidth = scene.getWidth();
final double initHeight = scene.getHeight();
Scale scale = new Scale();
scale.xProperty().bind(scene.widthProperty().divide(initWidth));
scale.yProperty().bind(scene.heightProperty().divide(initHeight));
scale.setPivotX(0);
scale.setPivotY(0);
group.getTransforms().addAll(scale);
}
}

Wednesday, 16 July 2014

JavaFx Group

@DefaultProperty(value="children")
public class Group
extends Parent
  • A Group node contains an ObservableList of children that are rendered in order whenever this node is rendered. A Group will take on the collective bounds of its children and is not directly resizable.
  • Any transform, effect, or state applied to a Group will be applied to all children of that group. Such transforms and effects will NOT be included in this Group's layout bounds, however if transforms and effects are set directly on children of this Group, those will be included in this Group's layout bounds.
  • By default, a Group will "auto-size" its managed resizable children to their preferred sizes during the layout pass to ensure that Regions and Controls are sized properly as their state changes. If an application needs to disable this auto-sizing behavior, then it should set autoSizeChildren to false and understand that if the preferred size of the children change, they will not automatically resize (so buyer beware!).
  • Group Example:
    import javafx.scene.*;
    import javafx.scene.paint.*;
    import javafx.scene.shape.*;
    import java.lang.Math;

    Group g = new Group();
    for (int i = 0; i < 5; i++) {
       Rectangle r = new Rectangle();
        r.setY(i * 20);
        r.setWidth(100);
        r.setHeight(10);
        r.setFill(Color.RED);
        g.getChildren().add(r);
    }

Wednesday, 9 July 2014

How to get IP address in Java using InetAddress

An Internet Protocol address (IP address) is a numerical label assigned to each device (e.g., computer, printer) participating in a computer network that uses the Internet Protocol for communication. The designers of the Internet Protocol defined an IPv4 address as a 32-bit number.
In this tutorial we are going to see how can you get the IP Address that is assigned to your own machine inside your local network and the IP Addresses assigned to specific Domain Names(e.g. www.google.com…).
To do that we are going to use InetAddress.To be more specific we are going to use:
  • getLocalHost().getHostAddress() method of InetAddress to get the IP Address of our machine in our local network
  • getByName() method of InetAddress to get the IP Address of a specific Domain Name
  • getAllByName() method of InetAddress to get all the IP Address of a specific Domain Name.
package com.miscl2;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class GetIpAddress {
public static void main(String[] args) throws UnknownHostException {
// print the IP Address of your machine (inside your local network)
System.out.println(InetAddress.getLocalHost().getHostAddress());
// print the IP Address of a web site
System.out.println(InetAddress.getByName("www.jaypthakkar.blogspot.in"));
// print all the IP Addresses that are assigned to a certain domain
InetAddress[] inetAddresses = InetAddress
.getAllByName("www.google.com");
for (InetAddress ipAddress : inetAddresses) {
System.out.println(ipAddress);
}
}
}
/*
OUTPUT
----------------------------------------
192.168.1.89
www.jaypthakkar.blogspot.in/74.125.236.108
www.google.com/74.125.236.114
www.google.com/74.125.236.116
www.google.com/74.125.236.112
www.google.com/74.125.236.113
www.google.com/74.125.236.115
*/