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

Wednesday, November 4, 2009

Java : sample applet






import java.applet.*;
import java.awt.*;

public class A extends Applet
{
private int w, h;
public void init( )
{
w = 45;
h = 50;
}

public void paint(Graphics g)
{
g.drawRect(w, h, 20, 80);
}
}


Sample J2Me Midlet for sound recording



import java.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;

public class VoiceRecordMidlet extends MIDlet {
private Display display;

public void startApp() {
display = Display.getDisplay(this);
display.setCurrent(new VoiceRecordForm());
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
notifyDestroyed();
}
}

class VoiceRecordForm extends Form implements CommandListener {
private StringItem message;
private StringItem errormessage;
private final Command record, play;
private Player player;
private byte[] recordedAudioArray = null;
public VoiceRecordForm() {
super("Recording Audio");
message = new StringItem("", "Select Record to start recording.");
this.append(message);
errormessage = new StringItem("", "");
this.append(errormessage);
record = new Command("Record", Command.OK, 0);
this.addCommand(record);
play = new Command("Play", Command.BACK, 0);
this.addCommand(play);
this.setCommandListener(this);
}
public void commandAction(Command comm, Displayable disp) {
if (comm == record) {
Thread t = new Thread() {
public void run() {
try {
player = Manager.createPlayer("capture://audio?encoding=pcm");
player.realize();
RecordControl rc = (RecordControl) player.getControl("RecordControl");
ByteArrayOutputStream output = new ByteArrayOutputStream();
rc.setRecordStream(output);
rc.startRecord();
player.start();
message.setText("Recording...");
Thread.sleep(5000);
message.setText("Recording Done!");
rc.commit();
recordedAudioArray = output.toByteArray();
player.close();
} catch (Exception e) {
errormessage.setLabel("Error");
errormessage.setText(e.toString());
}
}
};
t.start();

}
else if (comm == play) {
try {
ByteArrayInputStream recordedInputStream = new ByteArrayInputStream(recordedAudioArray);
Player p2 = Manager.createPlayer(recordedInputStream, "audio/basic");
p2.prefetch();
p2.start();
} catch (Exception e) {
errormessage.setLabel("Error");
errormessage.setText(e.toString());
}
}
}
}

Wednesday, June 10, 2009

How to set up Jogre for the web

Set up jdk, apache tomcat, and hsqldb
==============================

apt-get install sun-java6-jdk
apt-get install tomcat5.5 tomcat5.5-wepapps tomcat5.5-admin
apt-get install hsqldb-server

Set up jogre
=========

wget http://voxel.dl.sourceforge.net/sourceforge/jogre/jogre_beta_0.3_bin.zip
cd /opt
unzip ~/jogre_beta_0.3.bin.zip
adduser jogre
chown -R jogre:jogre jogre
su jogre
cd
echo "export CLASSPATH=$CLASSPATH:/usr/share/java/hsqldb.jar:." >> .profile
source .profile
java org.hsqldb.Server &
cd /opt/jogre/server
chmod a+x *.sh


Configuration
==========

./server.sh &

#I prefer the gui; it's easier to just change the db settings with this tool
./administrator.sh &
#Login with admin, admin
#Change the database from xml to hsqldb
#The path to the db is jdbc:hsqldb:hsql://localhost/jogre_hsqldb

#restart the jogre server
killall server.sh
./server.sh &


#With apache tomcat admin (http://localhost:8180/admin), add jogreweb.war

Sunday, June 7, 2009

Java - convert byte array to String



int len = (int)c.getLength();

if (len > 0) {
int actual = 0;
int bytesread = 0 ;
byte[] data = new byte[len];
while ((bytesread != len) && (actual != -1)) {
actual = is.read(data, bytesread, len - bytesread);
bytesread += actual;

}

//assuming utf-8 encoding
String test = new String(data,0,data.length,"UTF8");
System.out.println(test);


Java - how to make an anonymous thread

Creating an anonymous thread is pretty easy. If you want to make a thread that performs a simple operation anonymous threads can be convenient.

Java threads perform their task in the run() function, which is initiated with the start method.

Here is an example:



Thread t = new Thread( new Runnable(){
public void run(){
System.out.println(" do some work");


}
});

t.start();

Saturday, June 6, 2009

Hello world Midlet


import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;


public class LoginScreen extends MIDlet {

public LoginScreen() {
// TODO Auto-generated constructor stub
}

protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub

}

protected void pauseApp() {
// TODO Auto-generated method stub

}

protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub

Form f = new Form("Login Form");
Display d = Display.getDisplay(this);
d.setCurrent(f);

f.append("Hello");

}

}

Java Me Notes

Links

Main site

http://java.sun.com/javame/index.jsp

Download

http://java.sun.com/products/sjwtoolkit/download.html

Eclipse plugin

http://download.eclipse.org/dsdp/mtj/updates/0.9/stable/

Need to use eclipse to export jad and jar for the project (which is in deploy)


Conventions

startApp() --> like main

import javax.microedition.lcdui.*; //required to use display

Display // interface with phone screen

Form //set up the screen then attach it to display

myform.append("some text"); //a label

Tuesday, May 26, 2009

Create Messagebox / popup for Java

//Assuming that the current object is a JFrame:

JOptionPane.showMessageDialog(this, "Some Message");