Tuesday, January 31, 2012

JavaFX exercise: Reflection effect

Reflection effect
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javafx_extext;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.Reflection;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;

/**
*
* @author erix7
*/
public class JavaFX_exText extends Application {

 /**
  * @param args the command line arguments
  */
 public static void main(String[] args) {
     launch(args);
 }

 @Override
 public void start(Stage primaryStage) {
     primaryStage.setTitle("java-buddy.blogspot.com");
     Group root = new Group();
     Scene scene = new Scene(root, 400, 300, Color.WHITE);
  
     Reflection reflection = new Reflection();
  
     Text text = new Text(50, 50, "Serif");
     text.setFill(Color.RED);
     text.setFont(Font.font("Serif", 20));
     text.setEffect(reflection);
  
     Text text2 = new Text(50, 100, "SanSerif");
     text2.setFill(Color.GREEN);
     text2.setFont(Font.font("SanSerif", 30));
     text2.setEffect(reflection);
  
     Text text3 = new Text(50, 150, "Monospaced");
     text3.setFill(Color.BLUE);
     text3.setFont(Font.font("Monospaced", 40));
     text3.setEffect(reflection);
  
     root.getChildren().add(text);
     root.getChildren().add(text2);
     root.getChildren().add(text3);
  
     primaryStage.setScene(scene);
     primaryStage.show();
 }
}


- JavaFX Reflection Effect

JavaFX exercise: add shadow effect on text

shadow effect on text
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javafx_extext;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.DropShadow;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;

/**
*
* @author erix7
*/
public class JavaFX_exText extends Application {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("java-buddy.blogspot.com");
Group root = new Group();
Scene scene = new Scene(root, 400, 300, Color.WHITE);

DropShadow shadow = new DropShadow();
shadow.setOffsetX(5.0);
shadow.setOffsetY(5.0);
shadow.setColor(Color.BLACK);

Text text = new Text(50, 50, "Serif");
text.setFill(Color.RED);
text.setFont(Font.font("Serif", 20));
text.setEffect(shadow);

Text text2 = new Text(50, 100, "SanSerif");
text2.setFill(Color.GREEN);
text2.setFont(Font.font("SanSerif", 30));
text2.setEffect(shadow);

Text text3 = new Text(50, 150, "Monospaced");
text3.setFill(Color.BLUE);
text3.setFont(Font.font("Monospaced", 40));
text3.setEffect(shadow);

root.getChildren().add(text);
root.getChildren().add(text2);
root.getChildren().add(text3);

primaryStage.setScene(scene);
primaryStage.show();
}
}

JavaFX exercise: Set text font

Set text font
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javafx_extext;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;

/**
*
* @author erix7
*/
public class JavaFX_exText extends Application {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("java-buddy.blogspot.com");
Group root = new Group();
Scene scene = new Scene(root, 400, 300, Color.WHITE);

Text text = new Text(50, 50, "Serif");
text.setFill(Color.RED);
text.setFont(Font.font("Serif", 20));

Text text2 = new Text(50, 100, "SanSerif");
text2.setFill(Color.GREEN);
text2.setFont(Font.font("SanSerif", 30));

Text text3 = new Text(50, 150, "Monospaced");
text3.setFill(Color.BLUE);
text3.setFont(Font.font("Monospaced", 40));

root.getChildren().add(text);
root.getChildren().add(text2);
root.getChildren().add(text3);

primaryStage.setScene(scene);
primaryStage.show();
}
}

JavaFX exercise: Set text color using javafx.scene.paint.Color

Set text color using javafx.scene.paint.Color
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javafx_extext;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;

/**
*
* @author erix7
*/
public class JavaFX_exText extends Application {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("java-buddy.blogspot.com");
Group root = new Group();
Scene scene = new Scene(root, 400, 300, Color.WHITE);

Text text = new Text(50, 50, "Hello Java-Buddy");
int red = 255;
int green = 0;
int blue = 0;
text.setFill(Color.rgb(red, green, blue, 0.9));

Text text2 = new Text(50, 100, "Hello Java-Buddy");
text2.setFill(Color.GREEN);
Text text3 = new Text(50, 150, "Hello Java-Buddy");
text3.setFill(Color.hsb(40, 0.7, 0.8));

root.getChildren().add(text);
root.getChildren().add(text2);
root.getChildren().add(text3);

primaryStage.setScene(scene);
primaryStage.show();
}
}

JavaFX exercise: display text on Scene

display text on Scene
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javafx_extext;

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

/**
*
* @author erix7
*/
public class JavaFX_exText extends Application {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("java-buddy.blogspot.com");

Text text = new Text(50, 50, "Hello Java-Buddy");

StackPane root = new StackPane();
root.getChildren().add(text);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
}

Thursday, January 26, 2012

Hello JavaFX 2

JavaFX 2.0 is the next step in the evolution of Java as a rich client platform, shortening development time and easing deployment of data-driven business and enterprise client applications. Know more...http://www.oracle.com/us/technologies/javafx/overview/index.html (with FREE download of JavaFX 2.0 Data Sheet in PDF format)

JavaFX 2.0 is the latest major update release for JavaFX. Many of the new features introduced in JavaFX 2.0 are incompatible with JavaFX 1.3. If you are developing a new application in JavaFX, it is recommended that you start with JavaFX 2.0. You can find current online JavaFX 2.0 Documentation here: http://docs.oracle.com/javafx/

Before Create project of JavaFX 2, may be you have to download and install JavaFX SDK here: http://www.oracle.com/technetwork/java/javafx/downloads/index.html

- Start NetBeans IDE, click File -> New Project...

- Select JavaFX in Categories, aand JavaFX Application in Project, click Next.
New a project of JavaFX Application

- Enter Project Name, helloworldJavaFX2. Make sure both checkbox of Create Application Class and Set as Main Project is checked. the Application class name will updated automatically when we enter Project Name. Click Finish.
Setup new project of JavaFX

- The Project Wizard will generate the default project for you.
Auto-generated JavaFX 2 project

- Build: Click Run -> Build Main Project.

- Run: Click Run -> Run Main Project, or press F6, or click on the Green Arrow icon.
Hello World! using JavaFX 2

Tuesday, January 24, 2012

How to run NetBeans compiled program

Last article show how to create a Hello World program using NetBeans IDE 7.1, and also run it - inside NetBeans IDE. If you try to run it in command line using java (ex. java HelloNetBeans), may be you will be complaiined with Error of java.lang.NoClassDefFoundError. It should be a normal case.

Now, click Run -> Clean and Build Main Project in NetBeans IDE top menu. NetBeans' Output window will show you how to run it in command line. such as:
java -jar "C:\Users\erix7\Documents\NetBeansProjects\helloNetBeans\dist\helloNetBeans.jar"

NetBeans IDE Output show you How to run NetBeans compiled program
Run the generated jar using java in command line

Hello World! using NetBeans IDE 7.1

To verify our installation of NetBeans 7.1, we are going to create a helloworld using NetBeans.

- Click File -> New Project... in NetBeans IDE top menu.

- Select Java from Categories, and Java Application from Projects. Then click Next.
New Java Application

- Enter helloNetBeans in Project Name field, make sure both checkbox of Create Main Class and Set as Main Project is checked. the Main class name will updated automatically when we enter Project Name. Click Finish.
Enter Name and Location

- A default project will be generated for you.
Auto generated code

- Modify the code in main() to say something.
public class HelloNetBeans {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.println("Hello World! from NetBeans.");
}
}


- Final Save(File -> Save), Build (Run -> Build Project) and Run (Run -> Run Main Project). If everything go correctly, you can see the word "Hello World! from NetBeans." in the Output windows, that means your installation is properly.
Hello World shown in Output window

Related Article:
- How to run NetBeans compiled program

Sunday, January 22, 2012

Hello World of Java 7 on Windows 8, in "cmd" DOS Prompt

After installed of Java environment, it's time to code our first Java program - helloworld.

The first exercise of helloworld we create and compile in "cmd" (DOS Prompt) environment.

- Create a text file helloworld.java, using any editor program you like.

- Entent the code, and save it:
public class helloworld{

public static void main(String[] args){
System.out.println("Hello World!");
}

}

helloworld.java

- Start a "cmd" window in Windows 8, refer to the post "Run 'cmd' in Windows 8" if you don't know how. Change to the directory of helloworld.java.

- Type the command to compile the java program:
javac helloworld.java

- Type the command to run the compiled class:
java helloworld

- If your JDK environment is set properly, the output should be:
Hello World!

helloworld.java

*** If you cannot run javac/java due to missing in path setting, refer to the post "Set PATH for JDK 7 on Windows 8".

Tuesday, January 17, 2012

Install NetBeans IDE on Windows 8

NetBeans IDE is open-source and free IDE, Runs on Windows, Linux, Mac OS X and Solaris, to develop desktop, mobile and web applications with Java, PHP, C/C++ and more. The current version is NetBeans IDE 7.1.

- Browse to NetBeans download page, http://netbeans.org/downloads/index.html. Make sure the Platform selection is suit your platform, and click to select the bundles to download. In my case, I download with All bundles of 249MB. Actually, you can add or remove packs later using the IDE's Plugin Manager (Tools | Plugins).
Download to install

- Your download should start automatically. Click to run it after download completed.




- Accept the suggested NetBeans IDE directory. The JDK directory should be the detected directory with JDK. (Refer to the post Install JDK 7 on Windows 8 if yo haven't install it)


- Accept the suggested install GlassFish directory.
install GlassFish directory

- Review your setting and click Install.



- Finish.
Finish

- You should have shortcut to NetBeans IDE 7.1 on both your Metro UI and Desktop.
NetBeans IDE shortcut on Metro UI
NetBeans IDE shortcut on Desktop

Related:
- Create Hello World! using NetBeans IDE 7.1 to verify your installation.

Set PATH for JDK 7 on Windows 8

You can run the JDK without setting the PATH environment variable, or you can optionally set it so that you can conveniently run the JDK executable files (javac.exe, java.exe, javadoc.exe, and so forth) from any directory without having to type the full path of the command.

To setup PATH in Windows 8:
Run Control Panel in Windows 8's Metro UI


Click More Settings in Control... on the left.


Search "Environment Variables" in Control Panel, and click "Edit the system environment variables".


Click on "Environment Variables..."


Scroll down to highligh and edit path items.


My full path of the bin folder of the JDK is:
C:\Program Files\Java\jdk1.7.0_02\bin

So add ";C:\Program Files\Java\jdk1.7.0_02\bin" in the path string.


Click OK... to finish.

Re-open Windows 8's "cmd" window, now i can start java and javac in anywhere.



Updated@2013-01-17: Set PATH environment variable to JDK on official Windows 8

Run "cmd" in Windows 8

To run "cmd" in Windows 8, move mouse cursor to the bottom-left corner in Metro UI, click Search in the pop-up menu.


Search "cmd", click on the listed icon of "cmd".


That's!

Install JDK 7 on Windows 8

Browse to Oracle download page(http://www.oracle.com/technetwork/java/javase/downloads/index.html) to download Java Platform (JDK), the current version is Java Platform (JDK) 7u2.
Browse to Oracle download page

Click to Accept License Agreement, select download Windows x86 ot x64. It's jdk-7u2-windows-i586.exe for Windows x86 in my case.
Download setup file

Execute the downloaded file.




Accept the Destination Folder, click Next.
Accept the Destination Folder


Click Continue button to continue JavaFx 2.0 SDK Setup.
JDK Setup complete

Install JavaFx 2.0 SDK Setup, click Next.
Install JavaFx 2.0 SDK Setup

Accept the JavaFx 2.0 SDK installation director.
Accept the JavaFx 2.0 SDK installation director



Complete by clicking on Close button.
JavaFX 2.0 SDK Setup - Complete

A browser will open for you to register your software.
register
In order to verify the setup correct, you have to run "cmd" in Windows. Refer to the article about how to run "cmd" in Windows 8.

Update to this step, you cannot run javac except you are in C:\Program Files\Java\jdk1.7.0_02\bin. Because you haven't set PATH.
Update to this step, you cannot run javac except you are in C:\Program Files\Java\jdk1.7.0_02\bin

To setup path, refer to the post "Set PATH for JDK 7 on Windows 8".

Refer to the post "Hello World of Java 7 on Windows 8, in 'cmd' DOS Prompt" to verify your JDK installation.


Updated@2013-01-17: Install JDK 7 on official Windows 8