Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Sunday, January 18, 2009

Netbeans and Nimbus Theme on Windows Vista



Actually I should name this topic "Netbeans and Nimbus Look & Feel (L&F)" but that would eliminate many search terms that use word "theme" instead :)

To set your Netbeans, in my case it's 6.5, to use the new Nimbus theme, just look under your Netbeans installation folder/etc/netbeans.conf file. It is usually located at C:\Program Files\NetBeans 6.5\etc\netbeans.conf. Then just append --laf Nimbus to netbeans_default_options.

So it looks like this:

...
# command line switches:

netbeans_default_options="-J-Dorg.glassfish.v3.installRoot=\"C:\Program Files\glassfish-v3-prelude\" -J-Dcom.sun.aas.installRoot=\"C:\Program Files\glassfish-v2ur2\" -J-client -J-Xverify:none -J-Xss2m -J-Xms32m -J-XX:PermSize=32m -J-XX:MaxPermSize=200m -J-Dapple.laf.useScreenMenuBar=true -J-Dsun.java2d.noddraw=true --laf Nimbus"
# Note that a default -Xmx is selected for you automatically.
...
Then save the file and restart your Netbeans, and proud of your new Netbeans Nimbus theme!

Note:
  • To edit the netbeans.conf file under Windows, you may need editor that capable of editing unix text file. I recommend using Netbeans itself to edit this file instead of Notepad.
  • If you want to change the theme back to default then you can just remove the added options from netbeans.conf file. I recommend you to bookmark this page so you will not forget where to set this option :P
Hope this helps!

Saturday, September 27, 2008

Syntax Highlight/Format C#, Java, PHP Code in Blogger

The method I used to put syntax highlighted code in my blog is to use the "Code Format" tool provided by Manoli.net. It works fine but I am just lazy to convert my code every time I post a new entry.

I then try to find another solution. One option is to use SyntaxHighlighter, a Java Script library that does all the syntax highlight jobs for you.

After installation, one problem I faced is that all the "new line" I wrote in "Edit Html" of Compose Page in Blogger are converted to "
". That's not very nice : (

Like everytime, I performed a search and found a good solution here, Using SyntaxHighlighter on BLOGGER. The purpose of additional script is to convert all the "
" found in the code to "new line" chracter. It works fine and here is my sample of highlighted Java code.

(Of course, I will not use silly program like Hello World for this :D )

And here is the result:



Simple, isn't it ?

Wednesday, January 30, 2008

Easily display BufferedImage

In many situations, you may want to display the image from the instance of BufferedImage for debugging purposes.

The obvious way is to create a JPanel and overrides its paint(Graphics g) method. But that consumes time and energy!

The better solution is to instantiate an object of ImageIcon and use its setImage(Image image) method to host the BufferedImage instance in it. After that, you can easily display the image by using JOptionPane.

Friday, December 14, 2007

Uninstalled Netbeans 5.5.1 from my Ubuntu

It is not because I do not like Netbeans or something like that. But it is because I got the brand new Netbeans 6.0 IDE installed on my computer. So I think it is OK to uninstall the old version now :)

You can get Netbeans 6.0 IDE here!


Different versions of Netbeans IDE actually can coexist without any conflict. Unfortunately, my HDD space of Ubuntu partition is very limited (at 6 GB). So I need to keep only the programs I actually use.

Uninstalling program from Ubuntu Linux is done differently from Windows. I installed the Netbeans IDE using the Package Manager. But I cannot find a way to uninstall the program using it. So I checked Application->Add/Remove... but still got no luck. So I finally browsed to Netbeans 5.5.1 folder located at /home/m3rlinez/netbeans-5.5.1/_uninst/ and found an uninstall script (uninstaller.sh). I double clicked on it and selected Run in Terminal. Then, the nice GUI popped up and I uninstalled Netbeans.



After my experience from using the latest release of Ubuntu (7.10). I think Ubuntu is now ready for many computer geeks and Java developers. But it may not be ready my mom :( The team has to get more works done.

Friday, December 7, 2007

My Java Remote Method Invocation Tutorial in Thai

My assignment in course Distributed System forced me to study Java Remote Method Invocation or, in short, Java RMI. The requirements are to invoke some methods on the server, host my own server and let my client invoke method in it.

Java RMI is in the same position as .NET Remoting object I mentioned before. It facilitates the interprocess communication. However, I think both method are too hard to use. The .NET Remoting requires ugly configuations while Java RMI requires the policy file. They should be easier.

So, to help the less experience Java programmers getting started, I decided to publish an article on how to make use of Java RMI with Eclipse IDE. The article is in Thai language. You can find it here, http://m3rlinez.googlepages.com/RMITutorial.pdf

Edit: I got some feedbacks from my friends and found that there are still some mistakes in the tutorial. I will correct these when I have time.

Friday, June 22, 2007

Running a J2ME application on Windows Mobile 5.0 devices

It is a real pain for Windows Mobile users to get a simple J2ME program to run on their mobile devices. I owned a Dopod 818 pro which is running WM5, and now I have to get my J2ME application to run on it.

Dopod actually bundled an application named MIDLet manager to run J2ME program. But after using it for a while, I experienced some weird behaviors such as automatic closing of the running program. So I think I should find other JVM for my phone.

After some googlings, I found that IBM J9 is one of the good choices. I download the installation package and follow instructions in install.pdf. There is also a good guide, which covers the installation for other platforms, at Markus blog too.



I first faced with the weird Exception from emulator.exe which says it cannot download the jad files.

An exception occurred
[//J9/IBM/examples/golfscoretrackersuite.jad] while downloading from file://J9/IBM/examples/golfscoretrackersuite.jad


Then, I found out that I mistyped the URL. It has to be “file:///{path}” instead of “file://{path}” (Note that there are THREE SLASHES) :P

I can now get the example ran.

Wednesday, May 2, 2007

Really simple blob detector

Few days ago while i was reading news at Blognone, I spotted an interesting topic on how to detect
(and calculate the area of ..) circles in an image. This is a well-known problem in the field of Computer Vision known as Blob Detection. I had some experiences in implementing the blob detector in C and C# but had never done it in Java. So I decided to write one. The code was more compact and straight to the point than my C and C# version.

This is the input image.



And here is my code.




package blobdetector;

import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Queue;
import javax.imageio.ImageIO;
import javax.management.Query;

/**
*
* @author m3rlinezatgmaildotcom
*/
public class Main {

public Main() {
}

public static boolean isBlack(BufferedImage image,int posX,int posY){
// หาสีที่สุดที่สนใจ
int color = image.getRGB(posX,posY);

// หาค่าความสว่างจากการเฉลี่ย RGB
int brightness =
(color & 0xFF) +
((color >> 2) & 0xFF) +
((color >> 4) & 0xFF);
brightness /= 3;
return brightness < 128;
}

public static void main(String[] args) {
if(args.length != 1){
System.err.println("ERROR: Pass filename as argument.");
return;
}

String filename = args[0];
// String filename = "C:\\Users\\Natthawut\\Desktop\\Polymorphism\\blob.jpg";
try {
BufferedImage bimg = ImageIO.read(new File(filename));

// map สำหรับเก็บว่าจุดใดบ้างที่ได้รับการสำรวจไปแล้ว
boolean[][] painted =
new boolean[bimg.getHeight()][bimg.getWidth()];

// วนรอบทุกจุดในรูป
for(int i = 0 ; i < bimg.getHeight() ; i++){
for(int j = 0 ; j < bimg.getWidth() ; j++) {
// System.out.println(i + " " + j + " b " + isBlack(bimg,j,i));
// ถ้าจุดนั้นเป็นสีดำ และยังไม่เคยถูกสำรวจ
if(isBlack(bimg,j,i) && !painted[i][j]){

// ทำการ floodfill
Queue<Point> queue = new LinkedList<Point>();
queue.add(new Point(j,i));

int pixelCount = 0;
while(!queue.isEmpty()){
Point p = queue.remove();

// เช็คว่าจุดที่ดึงมาอยู่ในขอบเขต
if((p.x >= 0) && (p.x < bimg.getWidth() && (p.y >= 0) && (p.y < bimg.getHeight()))){
if(!painted[p.y][p.x] && isBlack(bimg,p.x,p.y)){
painted[p.y][p.x] = true;
pixelCount++;

// ใส่จุดรอบๆจุดที่ดึงออกมาลงไปในคิว
queue.add(new Point(p.x + 1,p.y)); queue.add(new Point(p.x - 1,p.y));
queue.add(new Point(p.x,p.y + 1)); queue.add(new Point(p.x,p.y - 1));
}
}
}
System.out.println("Blob detected : " + pixelCount + " pixels");
}

}
}

} catch (IOException ex) {
ex.printStackTrace();
}

}

}




And here is the output.

Blob detected : 1 pixels
Blob detected : 1339 pixels
Blob detected : 1 pixels
Blob detected : 5582 pixels
Blob detected : 1 pixels
Blob detected : 1 pixels
Blob detected : 1 pixels
Blob detected : 1 pixels
Blob detected : 1 pixels
Blob detected : 1 pixels
Blob detected : 1 pixels
Blob detected : 1 pixels
Blob detected : 1 pixels
Blob detected : 1 pixels
Blob detected : 1 pixels
Blob detected : 4018 pixels
Blob detected : 1 pixels
Blob detected : 1 pixels
Blob detected : 1 pixels
Blob detected : 1 pixels
Blob detected : 1 pixels
Blob detected : 1 pixels
Blob detected : 1 pixels
Blob detected : 1 pixels
Blob detected : 1 pixels
Blob detected : 1 pixels
Blob detected : 1 pixels
Blob detected : 1 pixels
Blob detected : 1 pixels
Blob detected : 1 pixels
Blob detected : 1 pixels


Edit: P'Deans4J suggested me to write the same program using recursion too. Here is my code in recursive version. I added another static method "floodfill" which returns number of pixels in current blob.




package blobdetector;

import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Queue;
import javax.imageio.ImageIO;
import javax.management.Query;

/**
*
* @author m3rlinezatgmaildotcom
*/
public class Main {

public Main() {
}

public static boolean isBlack(BufferedImage image,int posX,int posY){
// หาสีที่สุดที่สนใจ
int color = image.getRGB(posX,posY);

// หาค่าความสว่างจากการเฉลี่ย RGB
int brightness =
(color & 0xFF) +
((color >> 2) & 0xFF) +
((color >> 4) & 0xFF);
brightness /= 3;
return brightness < 128;
}

public static int floodfill(
BufferedImage image,
boolean[][] painted,
int posX, int posY){

// ตรวจสอบขอบเขต
if((posX < 0) || (posX >= image.getWidth()) || (posY < 0) || (posY >= image.getHeight()))
return 0;

if(!painted[posY][posX] && isBlack(image,posX,posY)){
painted[posY][posX] = true;
return 1 + floodfill(image,painted,posX+1,posY) +
floodfill(image,painted,posX-1,posY) +
floodfill(image,painted,posX,posY+1) +
floodfill(image,painted,posX,posY-1);
}

return 0;
}

public static void main(String[] args) {
if(args.length != 1){
System.err.println("ERROR: Pass filename as argument.");
return;
}

String filename = args[0];

try {
BufferedImage bimg = ImageIO.read(new File(filename));

// map สำหรับเก็บว่าจุดใดบ้างที่ได้รับการสำรวจไปแล้ว
boolean[][] painted =
new boolean[bimg.getHeight()][bimg.getWidth()];


// วนรอบทุกจุดในรูป
for(int i = 0 ; i < bimg.getHeight() ; i++){
for(int j = 0 ; j < bimg.getWidth() ; j++) {

// ถ้าจุดนั้นเป็นสีดำ และยังไม่เคยถูกสำรวจ
if(isBlack(bimg,j,i) && !painted[i][j]){

int pixelCount = floodfill(bimg,painted,j,i);

System.out.println("Blob detected : " + pixelCount + " pixels");
}

}
}

} catch (IOException ex) {
ex.printStackTrace();
}

}

}


While the recursive version uses less LOC, easier to understand and easier to code than the first solution, its performance is not as good as the first one and it actually gives me java.lang.StackOverflowError when used with the sample image. But if the problem's size is small, I prefer implementing the recursive version too.

Tuesday, April 17, 2007

Polymorphism example in Java, C# and C++

It has been a while since I coded my first C program. Now, because many of my friends at the department began to interest themselves in C++ programming. So I think I should learn some of the basics of it too :P

C++ is a powerful programming language with tons of language features and hard-to-understand syntaxes. However, I am only interested in its object oriented features such as Class, Inheritance, Composition, Polymorphism, blah blah. Thanks to my background knowledge in OO, it did not take me long to understand all these.

I tried to create a very simple Polymorphism example in C++ using the famous open-source IDE, Dev-C++. Unfortunately, I experienced some problems about building C++ project with multiple files. The IDE kept telling me that there is multiple definition of functions in my code. So, I went googling and found this page, Organizing Code Files in C and C++. It provided me the basic understandings of compiling and linking process.

After finished coding the C++ example, I thought I should create the Java and C# version to compare with it too. I noticed that C# is more like C++ in that the functions (or methods) to be overriden must declared to be virtual while there is no such modifier in Java.


Here are my output and codes.



C++

main.cpp
#include 
#include "shape.h"
#include "circle.h"

using namespace
std;

int
main(int argc, char *argv[])
{

Shape *shape = new Shape();
shape->calculateArea();
shape->test();

Shape *circle = new Circle(10);
circle->calculateArea();
circle->test();

system("PAUSE");
return
EXIT_SUCCESS;
}

shape.h
#ifndef _SHAPE_CLASS
#define _SHAPE_CLASS

class
Shape{
public
:
virtual
void showName();
virtual
double calculateArea();
void
test(){
showName();
}
};


#endif

shape.cpp
#include "shape.h"
#include

using namespace
std;

void
Shape::showName(){
cout << "Shape: I am a shape!" << endl;
}


double
Shape::calculateArea(){
cout << "Shape: Dunno how to calc my area ..." << endl;
return
0.0;
}

circle.h
#ifndef _CIRCLE_CLASS
#define _CIRCLE_CLASS

#include "shape.h"

class
Circle : public Shape{
public
:
Circle(double radius) : radius_(radius){}
virtual
void showName();
virtual
double calculateArea();
protected
:
double
radius_;

};


#endif

circle.cpp
#include "circle.h"
#include
#include

using namespace
std;

void
Circle::showName(){
cout << "Circle: I am a circle!" << endl;
}


double
Circle::calculateArea(){
cout << "Circle : My area = " << M_PI*radius_*radius_ << endl;
return
M_PI*radius_*radius_;
}


C#
Program.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace CSharpPolymorphism
{
class Program
{
static void Main(string[] args)
{
Shape shape = new Shape();
shape.showName();
shape.calculateArea();

Circle circle = new Circle(10.0);
circle.showName();
circle.calculateArea();

Console.Read();
}
}
}

Shape.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace CSharpPolymorphism
{
class Shape
{
public virtual void showName()
{
Console.WriteLine("Shape: I am a shape!");
}

public virtual double calculateArea()
{
Console.WriteLine("Shape: Dunno how to calc my area ...");
return 0.0;
}
}
}

Circle.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace CSharpPolymorphism
{
class Circle : Shape
{
protected double radius;

public Circle(double radius)
{
this.radius = radius;
}
public override void showName()
{
Console.WriteLine("Circle: I am a circle!");
}

public override double calculateArea()
{
Console.WriteLine("Circle : My area = " +
Math.PI * radius * radius);
return Math.PI * radius * radius;
}
}
}

Java
Main.java
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Shape shape = new Shape();
shape.showName();
shape.calculateArea();

Circle circle = new Circle(10.0);
circle.showName();
circle.calculateArea();

}

}

Shape.java
public class Shape {

/** Creates a new instance of Shape */
public Shape() {
}

public void showName(){
System.out.println("Shape: I am a shape!");
}

public double calculateArea(){
System.out.println("Shape: Dunno how to calc my area ...");
return 0.0;
}
}

Circle.java
public class Circle extends Shape{

protected double radius;
/** Creates a new instance of Circle */
public Circle(double radius) {
this.radius = radius;
}

public void showName() {
System.out.println("Circle: I am a circle!");
}

public double calculateArea() {
System.out.println("Circle : My area = " + Math.PI*radius*radius);
return Math.PI*radius*radius;
}

}

Sunday, March 4, 2007

Access modifier : "private" for distinct instances of the same class

Many of us who are reading this blog might have learned the use of access modifiers in Java. I found that there is a small confusion about the use of "private" access modifier.

private
member
Accessible only in its class(which defins it).
protected
member
Accessible only within its package and its subclasses
public
class

interface

member

Accessible anywhere

Accessible anywhere

Accessible anywhere its class is.

Let us create a new class named "Student".

public class Student {
private int score;

public Student() {
score = 89;
}

public void modifyScore(Student that,int newValue){
that.score = newValue;
}

public String toString() {
return "Score = " + score;
}

}


Imagine a situation where instance "A" of class "Student" trying to modify the "score" member of instance "B" of the same class, The misunderstanding is this cannot be done because of the score's access modifier is private. In fact, it can be done correctly. As the rule states that private members are accessible only in its class. So instances could access the private members of other instances too.

Wednesday, January 24, 2007

J2ME Applications to run on Sony's PSP




People at java4psp are porting J2ME Virtual Machine to the PSP which means we will see J2ME apps (and games) running on the PSP soon! The project's name is BLUEKVM. This also enable we programmers to program the PSP in Java language (which is easier to code/debug than those alien C/C++).

Welcome to PSP Java Development!

This is an experimental project on the Java technology for the PSP platform.

Current Subprojects
1. BLUEKVM - A port of the J2ME technology's Kilobyte Virtual Machine (KVM). The KVM core libraries, virtual machine

features, security, input/output, and networking is defined by the Connected Limited Device Configuration,

version 1.1.

Please visit http://java.sun.com/javame/index.jsp for more details of the J2ME technology.

Download the BLUEKVM demo version (bluekvm-psp-demo-version.zip)
to see a sample Java code running on your PSP!

2. PlayStation Device Profile (under research)


QPOINT Software Group
PHYSIKA, THe U.P. Applied Physics Society

Thursday, January 18, 2007

UTF-8 in Netbeans 5.5

I have Netbeans 5.5 with JDK 6.0 running on my Windows Vista RC1 for a month now. Problem arises when I need to use Thai language in my Java Applet project.


From the figure in the left, I try to draw a string, "สวัสดีครับ", but it was displayed as "??????????". By the way, If you enter your Thai texts via Netbeans' properties editor they will be displayed properly like the caption of JButton in my applet. (If you examined the designer generated sources, you would see escaped string instead of pure Thai text)

To fix the problem, right click on your java source file, click on properties, change the encoding to "utf-8".


After changed the file encoding, you have to close your file and reopen it. You can now edit your file and it will be saved as UTF-8.



But if you compile your file at this point, you will get something like "unmappable character for encoding XXX" error. There is one more little thing to change, Go to project properties window by right click on the project name in the project navigator and click on properties, Go to build->compile and add "-encoding utf-8" at the Additional Compiler Options.


This is my result,


This solution may apply to other foreign languages too.

Sunday, December 10, 2006

Running Java SE Application in Windows Vista


Running Java SE Application in Windows Vista with Aero will cause Windows to automatically change its theme to Vista Basic (without Aero). This is really annoying especially when debugging Java application in the IDE.

A workaround to this problem is to install Java Runtime Environment (JRE) 6 Release Candidate (download page). Java applications (made with JFrame) would support Aero after JRE 6.0 RC is installed.