Rabu, 05 November 2008

Digital Clock

package jamdigital;
import java.util.Date;
/**
*
* @author glaShSial
*/
public class JamDigital extends javax.swing.JFrame {
private int jam,menit,detik;
private String Sjam,Smenit,Sdetik;
private JLabel jmlbl;

public JamDigital() {
initComponents();
}
@SuppressWarnings("unchecked")
//
private void initComponents() {

jmlbl = new javax.swing.JLabel();

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setAlwaysOnTop(true);
setResizable(false);
setUndecorated(false);
addWindowListener(new WindowAdapter() {
public void windowActivated(WindowEvent evt) {
formWindowActivated(evt);
}
});

jmlbl.setFont(new Font("Arial Narrow", 1, 18)); // NOI18N
jmlbl.setText("jLabel1");

GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jmlbl, GroupLayout.DEFAULT_SIZE, 86, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jmlbl)
.addContainerGap(13, Short.MAX_VALUE))
);

pack();
}//
private void formWindowActivated(java.awt.event.WindowEvent evt) {

}

private void formComponentShown(java.awt.event.ComponentEvent evt) {
// TODO add your handling code here:
TimerTask tt=new TimerTask() {

@Override
public void run() {
//System.out.println("xx");
Date dt=new Date();
Calendar cld=Calendar.getInstance();
cld.setTime(dt);
jam=dt.getHours();
menit=dt.getMinutes();
detik=dt.getSeconds();

Sjam=Integer.toString(jam);
Smenit=Integer.toString(menit);
Sdetik=Integer.toString(detik);
jmlbl.setText(""+Sjam +" : "+Smenit +" : "+Sdetik+"");
}
};
Timer t=new Timer();
t.schedule(tt,0, 100);
}

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JamDigital().setVisible(true);
}
});
}
}

if you want to add some styles, you have to add these code at the main class this project is added with a substance library which is can be downloaded in the internet
the substance used to decorate the display

public static void main(String[] args) throws UnsupportedLookAndFeelException {
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
UIManager.setLookAndFeel(new SubstanceMagmaLookAndFeel());
EventQueue.invokeLater(new Runnable() {
public void run() {
new JamDigital().setVisible(true);
}
});
}

Selasa, 21 Oktober 2008

Using Jtree as Menu


1. Drag Jtree to your project from project palette
2. Right click Jtree and choose change variable name, just click it
3. Name the Jtree with “tr1”
4. Right click Jtree and choose Costumize Code and paste these code
tr1 = new org.jdesktop.swingx.JXTree();
tr1.setModel(new treeMnu().getTreeModel());
tr1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
tr1MouseClicked(evt);
}
});
jScrollPane1.setViewportView(tr1);
5. Run the project, and the project will appear like the image in left side
6. You have to remember that you must create class treeMnu, here is the code for class treeMnu



import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

/**
*
* @author glaShSial
*/
public class treeMnu {
private DefaultTreeModel treeModel;
private DefaultMutableTreeNode mnuUtama;
private DefaultMutableTreeNode subMenu;
private DefaultMutableTreeNode vData,vAnggota,lAnggota,lPeminjaman,exit;

public treeMnu() {
mnuUtama=new DefaultMutableTreeNode("Menu Utama", true);

subMenu=new DefaultMutableTreeNode("View", true);
vData=new DefaultMutableTreeNode("Data Peminjaman");
// vData.s
vAnggota=new DefaultMutableTreeNode("Anggota");
subMenu.add(vData);
subMenu.add(vAnggota);
mnuUtama.add(subMenu);

subMenu=new DefaultMutableTreeNode("Laporan", true);
lAnggota=new DefaultMutableTreeNode("Anggota");
lPeminjaman=new DefaultMutableTreeNode("Peminjaman");
subMenu.add(lAnggota);
subMenu.add(lPeminjaman);
mnuUtama.add(subMenu);

subMenu=new DefaultMutableTreeNode("Tutup",true);
exit=new DefaultMutableTreeNode("Keluar");


subMenu.add(exit);
mnuUtama.add(subMenu);

treeModel=new DefaultTreeModel(mnuUtama);


}


public DefaultMutableTreeNode getMnuUtama() {
return mnuUtama;
}

public DefaultTreeModel getTreeModel() {
return treeModel;
}


}

Minggu, 19 Oktober 2008

Get time and Date

this class help us how to get date time in java programming
please use it and try it into your work
just copy and paste it into your java code


package time;

import java.text.SimpleDateFormat;
import java.util.Calendar;


public class DateNow {
public static String now(String dateFormat) {
Calendar kalender = Calendar.getInstance();
SimpleDateFormat frmt = new SimpleDateFormat(dateFormat);
return frmt.format(kalender.getTime());

}

public static void main(String[] args) {

System.out.println("Jam saat ne "+DateNow.now("hh:mm:ss"));
System.out.println("Tanggal saat ne"+DateNow.now("dd.MM.yy"));
System.out.println("Tanggal dan Jam "+DateNow.now(" dd-MM-yy hh:mm:ss"));

}

}

good luck

send me some messages if you get some trouble in this code at saerukzam@yahoo.co.id

Rabu, 20 Agustus 2008

Multimedia in java me

to begin writing these code yaou have to read "Bismillah" to make you understand the code easyly

here is the main class of the project

package balengku;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;


/**
* @author glaShSial
*/
public class Saefan extends MIDlet implements CommandListener{
private Command cmdExit;
private Command cmdPlay;

private Display display;
private TextField textField;
private Gauge gauge;
public Form form;

private static final int GAUGE_LEVELS = 4;
private static final int GAUGE_MAX = 12;
private static final String DEFAULT_URL ="http://localhost/23Y132.mpg";

public Saefan() {
display = Display.getDisplay(this);
form = new Form("DoraMPEGPlayer");
textField = new TextField("MPEG Video URL", DEFAULT_URL, 100,TextField.ANY);
gauge = new Gauge("Loading MPEG Video", false, GAUGE_MAX, 0);
cmdExit = new Command("Exit", Command.EXIT, 2);
cmdPlay = new Command("Play", Command.SCREEN, 1);
form.addCommand(cmdPlay);
form.addCommand(cmdExit);
form.setCommandListener(this);
form.append(textField);
}

public void startApp() {
display.setCurrent(form);
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

public void commandAction(Command c, Displayable d) {
//throw new UnsupportedOperationException("Not supported yet.");
if (c == cmdExit) {
destroyApp(false);
notifyDestroyed();
} else if(c == cmdPlay) {
gauge.setValue(0);
form.append(gauge);
Rukzam mpegCanvas = new Rukzam(this);
mpegCanvas.initializeVideo(textField.getString());
}
}

public void updateGauge(){
int current = gauge.getValue();
current = (current + GAUGE_MAX/GAUGE_LEVELS);
gauge.setValue(current);
}
}

class Saefan was the main class of the project
so you have to write the code bellow to make the new class for the next class where it was named saefan too

here is the code:


package balengku;

import java.io.IOException;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;
import javax.microedition.media.control.VideoControl;

/**
*
* @author glaShSial
*/
public class Rukzam extends Canvas implements CommandListener,PlayerListener,Runnable{
private Saefan parent;
private Display display;

private Player player;
private VideoControl videoControl;

private String url;
private Thread initializer;

private Command btnClose;
private Command btnReplay;

public Rukzam(Saefan parent) {
super();
this.parent = parent;
display = Display.getDisplay(parent);
btnClose = new Command("Close", Command.SCREEN, 1);
addCommand(btnClose);
setCommandListener(this);
}

public void initializeVideo(String url){
this.url = url;
initializer = new Thread(this);
initializer.start();
}

protected void paint(Graphics g) {
//throw new UnsupportedOperationException("Not supported yet.");
g.setColor(0, 0, 0);
g.fillRect(0, 0, getWidth(), getHeight());
}

public void commandAction(Command c, Displayable d) {
//throw new UnsupportedOperationException("Not supported yet.");
if (c == btnReplay) {
try{
player.start();
} catch (MediaException me) {
Alert alert = new Alert("MediaException thrown",
me.getMessage(), null, AlertType.ERROR);
display.setCurrent(alert);
}
} else if (c == btnClose) {
player.close();
parent.form.delete(1);
display.setCurrent(parent.form);
url = null;
parent = null;
}

}

public void playerUpdate(Player p, String event, Object eventData) {
//throw new UnsupportedOperationException("Not supported yet.");
if (event == PlayerListener.END_OF_MEDIA) {
if (btnReplay == null) {
btnReplay = new Command("Replay", Command.SCREEN, 1);
addCommand(btnReplay);
}
}
}

public void run() {
//throw new UnsupportedOperationException("Not supported yet.");
try {
player = Manager.createPlayer(url);
parent.updateGauge();
player.addPlayerListener(this);
player.realize();
parent.updateGauge();
player.prefetch();
parent.updateGauge();
} catch (IOException ioe) {
Alert alert = new Alert("IOException thrown", ioe.getMessage(),
null, AlertType.ERROR);
display.setCurrent(alert);
} catch (MediaException me) {
Alert alert = new Alert("MediaException thrown", me.getMessage(),
null, AlertType.ERROR);
display.setCurrent(alert);
}
playVideo();
}

public void playVideo() {
try {
/* ambil video control dan set menjadi display sedang aktif */
videoControl = (VideoControl)player.getControl("VideoControl");
if (videoControl != null) {
videoControl.initDisplayMode(videoControl.USE_DIRECT_VIDEO,
this);
}

parent.updateGauge();
int cHeight = this.getHeight();
int cWidth = this.getWidth();
videoControl.setDisplaySize(cWidth, cHeight);
display.setCurrent(this);
videoControl.setVisible(true);
player.start();
} catch (MediaException me) {
Alert alert = new Alert("MediaException thrown", me.getMessage(),
null, AlertType.ERROR);
display.setCurrent(alert);
}
}



}


Good luck and enjoy the code....
e-mail me here saerukzam@yahoo.co.id or saefan@amikom.ac.id or to my YM: saerukzam@yahoo.co.id if you get some mistakes wich you don't undestand