Tag Archives: Java

Open Source Projects Based on Java

Show us some work previously done with java..

This is a question asked by one of my students during the java lesson. There are lot of free and open source java projects that you can find according to your interests and knowledge. Here are some small but interesting Java open source projects I’ve found on Sourceforge, and you can browse many more left there.

Studying and contributing to open source projects is one of the best ways to improve your programming skills.

JIPT – Java Image Processing Toolkit
This is an Image Processing toolkit written in Java. The source code is intended to help you understand processes such as Color Inversion, Edge Detection, Fourier Transform, Morphological process, Laplacian Sharpening, Gaussian Noise Adding, and history.

Java-Chat
Multi-user chat which works with any operating system which supports java.

Lobo Evolution – Java Web Browser
Lobo Evolution is a fork of Lobo Browser. The project continuing the work of Lobo Browser(lobochief). Lobo Evolution is an extensible all-Java web browser and RIA platform. It supports HTML 4, Javascript (AJAX) and CSS 3 plus direct JavaFX and Java (Swing/AWT) rendering. Cobra is the web browser’s renderer API; also a Javascript-aware HTML parser.

Calcoo – Scientific calculator
Calcoo is a scientific calculator (with both RPN and algebraic modes) designed to provide maximum usability.

Java SID Player Music Library V2
This is a Commodore 64 emulator specialized for sound reproduction. It is also a SID player for music collections like HVSC and CGSC. It is a full C64 emulator and can play games of the GameBase64 project as well.

JConvert – Unit Conversion
JConvert is a free unit conversion program that has a friendly user interface and can also be used with external applications. Conversions can be modified and/or added easily. Supports multiple languages. Downloads and runs in seconds! Try it now.

Java LAN Messenger
A java based LAN messenger.

Java Open Chess 
Java Open Chess is a project written in Java in NetBeans IDE. There is possibility to play for 2 players on local computers and via network connection. There is no option for now to play against computer. This functionality will be implemented soon.

Java YouTube Video Downloader
YTD2 is a free Java application for downloading youtube videos as flv/mp4 files. Should run on all platforms where Java is supported. GNU General Public License V3. Using hc.apache.org components. Based on the idea of “ytd” by rafalfr.

Java-Calculator
Scientific Calculator includes the following features: Traditional algebraic or RPN operation, Result history, Math Functions, Percentages, Binary / octal / hexadecimal conversion and Large input/output display.

Using Action Listeners in Java (continued with previous)

As we discussed in previous post, this is continuing back again for some extend. This simple demonstration is about adding an action to the event performed by the user in application.

In below application it contain a JFrame window, flow layout for the content pane and a JButton.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.awt.*;
import javax.swing.*;
 
public class ButtonListener extends JFrame
{
 
public ButtonListener(String title)
{
super (title);
setBounds(100,100,200,150);
setDefaultCloseOperation(this.EXIT_ON_CLOSE);
setResizable(false);
 
Container c = getContentPane();
c.setLayout (new FlowLayout());
 
JButton b=new JButton("Click Me");
c.add(b);
}
 
public static void main (String[]args)
{JFrame f = new ButtonListener("Button Listner");
f.show();
}
}
import java.awt.*;
import javax.swing.*;

public class ButtonListener extends JFrame
{

public ButtonListener(String title)
{
super (title);
setBounds(100,100,200,150);
setDefaultCloseOperation(this.EXIT_ON_CLOSE);
setResizable(false);

Container c = getContentPane();
c.setLayout (new FlowLayout());

JButton b=new JButton("Click Me");
c.add(b);
}

public static void main (String[]args)
{JFrame f = new ButtonListener("Button Listner");
f.show();
}
}

This is how it looks like when it runs.

Button Listener Window

A component can be activated by adding “Action Listeners” to them. Now I want to display a message box with the message “You clicked on JButton” when user clicks on the button.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class ButtonListener extends JFrame implements ActionListener
{
 
public ButtonListener(String title)
{
super (title);
setBounds(100,100,200,150);
setDefaultCloseOperation(this.EXIT_ON_CLOSE);
setResizable(false);
 
Container c = getContentPane();
c.setLayout (new FlowLayout());
 
JButton b=new JButton("Click Me");
b.addActionListener(this);
c.add(b);
}
 
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null,"You clicked on JButton");
}
 
public static void main (String[]args)
{JFrame f = new ButtonListener("Button Listner");
f.show();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ButtonListener extends JFrame implements ActionListener
{

public ButtonListener(String title)
{
super (title);
setBounds(100,100,200,150);
setDefaultCloseOperation(this.EXIT_ON_CLOSE);
setResizable(false);

Container c = getContentPane();
c.setLayout (new FlowLayout());

JButton b=new JButton("Click Me");
b.addActionListener(this);
c.add(b);
}

public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null,"You clicked on JButton");
}

public static void main (String[]args)
{JFrame f = new ButtonListener("Button Listner");
f.show();
}
}

public class ButtonListener extends JFrame implements ActionListener

First of all we implement our class as an action listener by adding “implements ActionListener” to the end of the class title. Now we have implemented required sub classes in our main public class “ButtonListener”.

import java.awt.event.*;

We need to import this package which contain events before use them in our class.

public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null,”You clicked on JButton”);
}

This is the method for our event. This method invokes when ever user perform any action on the form. JOptionPane.showMessageDialog(null,”You clicked on JButton”); is the statement for display the message box in our application. JOptionPane is a class which produces message boxes, input boxes, dialog boxes and etc.

b.addActionListener(this);

We add the action listener to the button component. In here we have implemented all necessary sub classes for the events in our public class. Therefore we use the keyword “this” instead of the class name.

See what after compiling and running the application.

Message Box

When user click on the “b” button it executes the “actionPerformed” event. Then immediately it comes across the public class which works as the action listener. Then ButtonListener class invokes the method actionPerformed() in it. The command on the actionPerformed() method is JOptionPane.showMessageDialog(null,”You clicked on JButton”); showing the messagebox to the user.

Using Frames, Components and Containers in Java

If you are using JCreator, Eclipse, Intellij Idea, Netbeans or any other IDE made for Java, creating a GUI application can be done in minutes by drag and dropping components from the toolbox without much effort. But most novices don’t have any idea about what happening on there behind the seen. We know the goal in application development is to create the best possible software in the least amount of the time. Frameworks and IDEs makes your work easier. But when in learning, actually it brings an exciting and enjoyable experience when your application is 100% done with the code made by your fingers rather than using third party tools. Let’s shift back to the simple text editor for a while! This simple guide is to build a graphical user interface application in a very basic way.

The below Java application code builds a simple form window by using the “swing” components in Java. Compile and run it to see how it works.

1
2
3
4
5
6
7
8
9
10
11
12
import javax.swing.* ;
 
public class JFrameWindow
{
public static void main(String[]args)
{
JFrame f=new JFrame ("JFrame Window");
f.setBounds(100,100,400,300);
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
f.show();
}
}
import javax.swing.* ;

public class JFrameWindow
{
public static void main(String[]args)
{
JFrame f=new JFrame ("JFrame Window");
f.setBounds(100,100,400,300);
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
f.show();
}
}

import javax.swing.*;

All graphical components we need to create application are integrated in this “swing” package. After importing that in our java application, we can use built in classes on there anywhere in our application code.

JFrame f=new JFrame (“JFrame Window”);

We have created the new object “f” by using the JFrame class on swing package. Then we have set the location and size of the frame by using setBounds method on JFrame and setDefaultCloseOperation for decide the exit functionality on the frame window (if you miss this, the window will not respond with close button). And finally we invoke the show() method on JFrame class to display the window.

And here how it looks like.

JFrame Window
Let’s see how to add components and containers to this frame. Components are various elements like buttons, text boxes, check boxes, labels and etc. Containers are uses for place the components as the designer preferences. Frames and panels are the examples for the containers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.awt.*;
import javax.swing.*;
 
public class Flow extends JFrame
{
 
public Flow(String title)
{
super(title);
setDefaultCloseOperation(this.EXIT_ON_CLOSE);
}
 
public static void main(String [] args)
{
Flow f= new Flow("Flow Layout");
Container c=f.getContentPane();
c.setLayout(new FlowLayout());
 
for (int i=1; i<10; i++)
{
c.add(new JButton(""+i));
}
 
f.pack();
f.show();
}
}
import java.awt.*;
import javax.swing.*;

public class Flow extends JFrame
{

public Flow(String title)
{
super(title);
setDefaultCloseOperation(this.EXIT_ON_CLOSE);
}

public static void main(String [] args)
{
Flow f= new Flow("Flow Layout");
Container c=f.getContentPane();
c.setLayout(new FlowLayout());

for (int i=1; i<10; i++)
{
c.add(new JButton(""+i));
}

f.pack();
f.show();
}
}

In above code we have extended the JFrame class to our class. Therefore we could access all members in JFrame class in our program’s public class “Flow”. In class constructor “public Flow(String title)” initializes the title of the JFrame and the default close operation. Let’s see the next procedures line by line.

Flow f= new Flow(“Flow Layout”);

We create object “f” by using the Frame class (inherited JFrame class).

Container c=f.getContentPane();

In here we have created a variable called “c” in Container type. And “c” has assigned to the f’s container. the c variable contains the f’s container now.

c.setLayout(new FlowLayout());

We set the c’s layout as flow layout. In flow layout all components in it behaves in same distance gap. Even whether the form resizes components inside it keep the same distance from each other components.

for (int i=1; i<10; i++)
{
c.add(new JButton(“”+i));
}

This is simply adding 9 buttons by using a for loop which runs 10 iterations. c.add(new JButton(“”+i)) code adds the button component to the c container by using the JButton class on swing package we imported.

f.pack();
f.show();

The pack() method on the “f” use to integrate the layouts and components in the Frame. And as the final step we view the form by calling the show method on the “f”.

This is what you see after compiling and running the code.

Flow Layout

Besides the Flow layout there are many other layouts such as Border layout, Grid layout and Box layout which arrange and organizes components in various styles.

As you noticed, still the buttons on this application doesn’t perform any action when you click on them. The components requires action listeners to do something on the program. We’ll meet again with a new post on how to add action listeners to designed components sooner.