Wednesday, 4 November 2015

Circle Shape Stage In JavaFx

In this code I show you how can we use javafx graphics to create stage of custom shapes.In this program I have create an stage of circle shape you can create stage of any shape by making change in this line. 

Circle dragger = new Circle(100, 100, 100,Color.CADETBLUE);

Enjoy reading.

CODE:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class mybej extends Application {
Double intx,inty;
@Override
public void start(final Stage stage) {

stage.initStyle(StageStyle.TRANSPARENT);
Text text = new Text("Transparent STAGE");
Circle dragger = new Circle(100, 100, 100,Color.CADETBLUE);
text.setFont(new Font(40));
Group g = new Group();
Button min = new Button("-");
min.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
stage.setIconified(true);
}
});
Button close = new Button("X");
close.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
stage.close();
}
});

dragger.setOnMousePressed(new EventHandler<MouseEvent>() {
public void handle(MouseEvent me) {
intx = (me.getScreenX() - stage.getX());
inty = me.getScreenY() - stage.getY();
}
});

//when screen is dragged, translate it accordingly
dragger.setOnMouseDragged(new EventHandler<MouseEvent>() {
public void handle(MouseEvent me) {
stage.setX(me.getScreenX() - intx);
stage.setY(me.getScreenY() - inty);
}
});
HBox hBox = new HBox();
hBox.setSpacing(10);
hBox.setPadding(new Insets(25,0,0,60));
hBox.setAlignment(Pos.TOP_CENTER);
hBox.getChildren().addAll(min,close);

g.getChildren().addAll(dragger,hBox);
final Scene scene = new Scene(g,300, 250);
scene.setFill(Color.TRANSPARENT);
stage.setScene(scene);
stage.show();

}

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

OUTPUT:

Round shape stage in javafx

Saturday, 31 October 2015

Anagram Word Puzzle Game In Java

Today we are going to share with you an exciting word puzzle game in java this game generates an random scramble word user have to guess correct word if user guess correct word hi/her score will be increased.In this game user have also an option to skip the word.That is the complete description of a project.Download and enjoy this project don't forget to share your reviews.

SNAPSHOT:




DOWNLOAD THIS PROJECT:

                                                     DOWNLOAD

Thursday, 22 October 2015

, ,

Employee Management Project In Java

This project completely  based on jdbc and jswing.This is a perfect project for beginners who want to learn database management system in java.Following are the features of this project.

FEATURES:

Save employee record.
It can calculate salary of employee.
It can search employee record.
It can delete employee record.

OUTPUT: 





DOWNLOAD THIS PROJECT:


                           DOWNLOAD


Searching In Jtable On Every Key Press

When working with huge data we often need to search specific data in our table in this tutorial we shows how to search data in jtable using textfield  when ever user press key from keyboard it searches the data according to key press rowfilter is used to perform this task.

RowFilter<M,I>:

M: Here we have to specify the name of model we want to add row filter.
I: Type of identifier is specified here.

CODING:

import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class table extends JFrame{

table()
{
final JTextField t1 = new JTextField(15);
setTitle("Searching In JTable");
JLabel l1 = new JLabel("Search");
setDefaultCloseOperation(EXIT_ON_CLOSE);      
String columnNames[] = {"Product Name","Price"};
Object rowData[][]={{"Light",300},{"Fan",400},{"Bulb",100},{"Egg",50},{"Biscuit",20},{"Milk",80}};
TableModel model = new DefaultTableModel(rowData, columnNames);


final TableRowSorter<TableModel> sorter1 = new TableRowSorter<>(model);

JTable table1 = new JTable(model);
table1.setRowSorter(sorter1);
setLayout(new FlowLayout(FlowLayout.CENTER));
JScrollPane js = new JScrollPane(table1,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
add(l1);
add(t1);
add(js);

t1.getDocument().addDocumentListener(new DocumentListener(){
@Override public void insertUpdate(DocumentEvent e) {
search(t1.getText());

}
@Override public void removeUpdate(DocumentEvent e) {
search(t1.getText());
}
@Override public void changedUpdate(DocumentEvent e) {
search(t1.getText());
}
public void search(String s)
{
if (s.length() == 0) {

sorter1.setRowFilter(null);
} else {

sorter1.setRowFilter(RowFilter.regexFilter(s));
}
}
});
setSize(500,600);
setVisible(true);
}
}

class tab
{
public static void main(String[] args)  {

table x= new table();

}

}

OUTPUT:





Wednesday, 21 October 2015

,

How To Open And Save File In Java Fx

Before reading this article you should read Tutorial on file chooser in javafx,Tutorial on context menu in javafx. In this tutorial we will see how to use file chooser to open and save  image files in java fx.following are the explanations of methods used in these tutorials.

imageIo.write(RenderedImage im,String format,File outputlocation)

Writes an image using an  ImageWriter that supports the given specified format of a image. If there is already a File present, its will be over written.
im: Name of image that is to be written.

format: In this parameter you have to specify format in which you want to write a image.

outputlocation: Here you have to specify the location of image where you have to write.

HOW TO SAVE IMAGE FROM JAVAFX APPLICATION USING FILE CHOOSER:

import java.io.File;
import java.io.IOException;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javax.imageio.ImageIO;

public class mybej extends Application
{
public static void main(String[] args)
{
launch(args);
}
@Override
public void start( final Stage primaryStage) {
final Group root = new Group();
final Image pic = new Image ("fac.jpg");
ImageView iv = new ImageView();
iv.setImage(pic);

final FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Save File Chooser");
final Scene scene = new Scene(root, 600, 350,Color.WHITE);
final ContextMenu cm = new ContextMenu(); 
MenuItem cmItem2 = new MenuItem("Save Image");
cm.getItems().add(cmItem2);
cmItem2.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {

fileChooser.setTitle("Save Image");

File file = fileChooser.showSaveDialog(primaryStage);
if (file != null) {
try {
ImageIO.write(SwingFXUtils.fromFXImage(pic, null), "png", file);
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}
}
);
scene.addEventHandler(MouseEvent.MOUSE_CLICKED,
new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent e) {
if (e.getButton() == MouseButton.SECONDARY)  
cm.show(root, e.getX(),e.getY());
}
}); 
primaryStage.setScene(scene);
root.getChildren().addAll(iv);
primaryStage.show();
}

}

OUTPUT:



HOW TO OPEN IMAGE FILE USING FILE CHOOSER IN JAVA FX:


import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

public final class mybej extends Application {

private Desktop desktop = Desktop.getDesktop();

@Override
public void start(final Stage stage) {
stage.setTitle("File Chooser Sample");
final FileChooser fileChooser = new FileChooser();
File file = fileChooser.showOpenDialog(stage);

if (file != null) {
openFile(file);
}


Group root = new Group();

stage.setScene(new Scene(root));

}

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

private void openFile(File file) {
try {
desktop.open(file);
} catch (IOException ex) {
System.out.print(ex);
}
}
}

How To Create Musical Piano In JavaFx

 After using lots of application based on audio I get motivated to create a program based on audio,this program used AudioClip class to play a music when button of piano is pressed and I have used play method to play particular audioclip when piano button is pressed following code show how to create a musical piano in javafx.

CODING:

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.media.AudioClip;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class Mypiano extends Application
{

@Override
public void start(Stage primaryStage)
{

Group root = new Group();  
Rectangle r = new Rectangle(300,60,60,200);
r.setFill(Color.WHITE);
r.setStroke(Color.BLACK);
Rectangle ra = new Rectangle(340,60,50,130);
ra.setFill(Color.BLACK);
Rectangle r1 = new Rectangle(361,60,60,200);
r1.setFill(Color.WHITE);
r1.setStroke(Color.BLACK);
Rectangle r1a = new Rectangle(400,60,50,130);
r1a.setFill(Color.BLACK);
Rectangle r2 = new Rectangle(421,60,60,200);
r2.setFill(Color.WHITE);
r2.setStroke(Color.BLACK);
Rectangle r2a = new Rectangle(461,60,50,130);
r2a.setFill(Color.BLACK);
Rectangle r3 = new Rectangle(483,60,60,200);
r3.setFill(Color.WHITE);
r3.setStroke(Color.BLACK);
Rectangle r3a = new Rectangle(524,60,20,130);
r3a.setFill(Color.BLACK);
Rectangle r4 = new Rectangle(545,60,60,200);
r4.setFill(Color.WHITE);
r4.setStroke(Color.BLACK);
Rectangle r4a = new Rectangle(584,60,20,130);
r4a.setFill(Color.BLACK);

final AudioClip note1 =
new AudioClip(getClass().getResource("Note1.wav").toString());
final AudioClip note2 =
new AudioClip(getClass().getResource("Note2.wav").toString());
final AudioClip note3 =
new AudioClip(getClass().getResource("Note3.wav").toString());

final AudioClip note4 =
new AudioClip(getClass().getResource("Note4.wav").toString());


final AudioClip note5 =
new AudioClip(getClass().getResource("Note5.wav").toString());


final AudioClip note6 =
new AudioClip(getClass().getResource("Note6.wav").toString());

final AudioClip note7 =
new AudioClip(getClass().getResource("Note7.wav").toString());

final AudioClip note8 =
new AudioClip(getClass().getResource("Note8.wav").toString());
r.setOnMouseClicked(new EventHandler<MouseEvent>()
{
@Override
public void handle(MouseEvent me) {

note1.play();

}
});
r1.setOnMouseClicked(new EventHandler<MouseEvent>()
{
@Override
public void handle(MouseEvent me) {

note2.play();

}
});
r2.setOnMouseClicked(new EventHandler<MouseEvent>()
{
@Override
public void handle(MouseEvent me) {

note3.play();

}
});
r3.setOnMouseClicked(new EventHandler<MouseEvent>()
{
@Override
public void handle(MouseEvent me) {

note4.play();

}
});
r4.setOnMouseClicked(new EventHandler<MouseEvent>()
{
@Override
public void handle(MouseEvent me) {

note5.play();

}
});
ra.setOnMouseClicked(new EventHandler<MouseEvent>()
{
@Override
public void handle(MouseEvent me) {

note6.play();

}
});

r1a.setOnMouseClicked(new EventHandler<MouseEvent>()
{
@Override
public void handle(MouseEvent me) {

note7.play();

}
});
r2a.setOnMouseClicked(new EventHandler<MouseEvent>()
{
@Override
public void handle(MouseEvent me) {

note1.play();

}
});
r3a.setOnMouseClicked(new EventHandler<MouseEvent>()
{
@Override
public void handle(MouseEvent me) {

note2.play();

}
});
r4a.setOnMouseClicked(new EventHandler<MouseEvent>()
{
@Override
public void handle(MouseEvent me) {

note8.play();

}
});


root.getChildren().addAll(r,ra,r1,r1a,r2,r2a,r3,r3a,r4,r4a);
Scene scene = new Scene(root, 600, 300);
primaryStage.setTitle("PIANO");
primaryStage.setScene(scene);
primaryStage.show();
}

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

}

OUTPUT:


Tutorial On Button In Java Fx

In this article I have tried to show you how to use button in javafx application button in javafx can be used in three different modes.
  1. NORMAL:Creates a simple push button.
  2. DEFAULT:Creates a simple button that can be operated by enter key.
  3. CANCEL:Creates a button that can be operated by escape button.
In this program we have shown you how to create default button.setLayoutX,setLayoutX is used to position of button in screen.

CODING:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;


public class mainfx extends Application 
{

@Override
public void start(Stage primaryStage) {
Group g = new Group();
primaryStage.setTitle("FX Button");
Button btn = new Button("Press To Exit");
btn.setMinWidth(100);
btn.setLayoutX(100);
btn.setLayoutY(100);

btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.exit(1);
}
});

g.getChildren().add(btn);
Scene sc = new Scene(g,400,300);
primaryStage.setScene(sc);
primaryStage.show();
}


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

}

APPLYING MOUSE EVENT IN BUTTON:

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;

public class mainfx extends Application {

Button btn = new Button();
@Override
public void start(Stage primaryStage) {
Group g = new Group();
primaryStage.setTitle("Button Event In JavaFx");
btn = new Button("Enter Mouse To Rotate");
btn.setMinWidth(100);
btn.setLayoutX(100);
btn.setLayoutY(100);

btn.addEventHandler(MouseEvent.MOUSE_ENTERED,
new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent e) {
btn.getTransforms().add(new Rotate(45));
}
});    

g.getChildren().add(btn);
Scene sc = new Scene(g,400,300);
primaryStage.setScene(sc);
primaryStage.show();
}


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

}