Wednesday, 23 December 2009

Tutorial J2ME : Membuat Canvas

Pertama Kita Membuat 2 Kelas.
Kelas Pertama Yakni  utama.java , Source Codenya sebagai berikut :

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

public class utama extends MIDlet {
Display display;
cover cover;

public utama()
{
display = Display.getDisplay(this);
cover = new cover(this, display);
}

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

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

public void keluar()
{
destroyApp(false);
notifyDestroyed();
}
}

Kemudian Kelas Ke 2  Yakni cover.java, Source Codenya Sebagai Berikut :

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;

public class cover extends Canvas implements CommandListener{
Display display;
utama utama;
Command keluar;
public cover(utama utama, Display display)
{
this.utama = utama;
this.display = display;
keluar = new Command("Exit", Command.EXIT, 1);
addCommand(keluar);
setCommandListener(this);
}

public void paint(Graphics g)
{
g.setColor(255, 0, 0);
g.drawRect(50, 60, 60, 60);
g.setColor(0, 0, 255);
g.fillRect(130, 60, 60, 60);
g.setColor(0, 255, 255);
g.fillTriangle(80, 140, 50, 200, 110, 200);
g.setColor(255, 255, 0);
g.drawLine(160, 140, 130, 200);
g.drawLine(160, 140, 190, 200);
g.drawLine(130, 200, 190, 200);
g.setColor(255, 0, 255);
g.drawString("Belajar Pemrograman", getWidth()/2, 230, Graphics.HCENTER|Graphics.TOP);
}

public void commandAction(Command c, Displayable d) {
if(c==keluar)
{
utama.keluar();
}
}
}

Kemudian Jalankan Dan Hasilmya Seperti Di Bawah Ini :


Membuat Folder Dengan Java

import java.io.File;

public class Main{
public static void main(String[] args) {
try{
boolean success ;
success = (new File("C:/Documents and Settings/Administrator/My Documents/keren/Mr.bayu")).mkdir();
if(success){
System.out.println("direktori tunggal berhasil dibuat");
}else{
System.out.println("direktori tunggal gagal dibuat");
}
}catch(Exception e){
e.printStackTrace();
}
}
}

Hasilnya :


Shutdown Dengan Java

Tampilan Aplikasinya Sebagai Berikut :














Ini Source Code Lengkapnya :

import javax.swing.JOptionPane;

public class utama extends javax.swing.JFrame{

Runtime rt = Runtime.getRuntime();
public utama() {
initComponents();
}
public void tampil()
{
if(JOptionPane.showConfirmDialog(rootPane, "Batal?", "Konfirmasi", JOptionPane.DEFAULT_OPTION)==0)
{
try
{
rt.exec("cmd /c shutdown -a");
}catch(Exception e)
{
JOptionPane.showMessageDialog(rootPane, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
@SuppressWarnings("unchecked")
//
private void initComponents() {

jPanel1 = new javax.swing.JPanel();
bshut3 = new javax.swing.JButton();
bshut1 = new javax.swing.JButton();
bshut2 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Shutdown Demo");
setLocationByPlatform(true);
setResizable(false);
getContentPane().setLayout(null);

jPanel1.setBackground(new java.awt.Color(255, 0, 0));
jPanel1.setLayout(null);

bshut3.setFont(new java.awt.Font("Tahoma", 0, 14));
bshut3.setText("Log Off");
bshut3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bshut3ActionPerformed(evt);
}
});
jPanel1.add(bshut3);
bshut3.setBounds(20, 120, 270, 30);

bshut1.setFont(new java.awt.Font("Tahoma", 0, 14));
bshut1.setText("Shutdown");
bshut1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bshut1ActionPerformed(evt);
}
});
jPanel1.add(bshut1);
bshut1.setBounds(20, 20, 270, 30);

bshut2.setFont(new java.awt.Font("Tahoma", 0, 14));
bshut2.setText("Restart");
bshut2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bshut2ActionPerformed(evt);
}
});
jPanel1.add(bshut2);
bshut2.setBounds(20, 70, 270, 30);

getContentPane().add(jPanel1);
jPanel1.setBounds(0, 0, 310, 170);

java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-315)/2, (screenSize.height-200)/2, 315, 200);
}//


private void bshut3ActionPerformed(java.awt.event.ActionEvent evt) {
try
{
if(JOptionPane.showConfirmDialog(rootPane, "Yakin Ingin Log Off?", "Konfirmasi", JOptionPane.YES_NO_OPTION)==0)
{
rt.exec("cmd /c shutdown -l -t 30");
}
}catch(Exception e)
{
JOptionPane.showMessageDialog(rootPane, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}

private void bshut1ActionPerformed(java.awt.event.ActionEvent evt) {
try
{
rt.exec("cmd /c shutdown -s -t 30");
tampil();
}catch(Exception e)
{
JOptionPane.showMessageDialog(rootPane, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}

private void bshut2ActionPerformed(java.awt.event.ActionEvent evt) {
try
{
rt.exec("cmd /c shutdown -r -t 30");
tampil();
}catch(Exception e)
{
JOptionPane.showMessageDialog(rootPane, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}

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

// Variables declaration - do not modify
private javax.swing.JButton bshut1;
private javax.swing.JButton bshut2;
private javax.swing.JButton bshut3;
private javax.swing.JPanel jPanel1;
// End of variables declaration

}

Sorry Sangat Simple Soalnya Baru Belajar Juga.... :-D