Jumat, 17 April 2009

Aplikasi Java dengan Tombol Save dan Load

Sintak aplikasi java ini terdiri atas :

import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/*
* Cyberjava.java
*
* Created on 17 Apr 09, 19:02:13
*/

/**
*
* @author Any one
*/
public class Pizaini extends javax.swing.JFrame {
int a, b;
String hur= ";";


/** Creates new form Pizaini */
public Pizaini() {
initComponents();
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
//
private void initComponents() {

jToolBar1 = new javax.swing.JToolBar();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jSeparator1 = new javax.swing.JSeparator();
jLabel2 = new javax.swing.JLabel();
massa = new javax.swing.JTextField();
perc = new javax.swing.JTextField();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jSeparator2 = new javax.swing.JSeparator();
jLabel3 = new javax.swing.JLabel();
hasil = new javax.swing.JLabel();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenu3 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jMenuItem2 = new javax.swing.JMenuItem();
jMenuItem7 = new javax.swing.JMenuItem();
jMenuItem3 = new javax.swing.JMenuItem();
jMenuItem4 = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
jMenuItem5 = new javax.swing.JMenuItem();
jMenuItem6 = new javax.swing.JMenuItem();
jMenu4 = new javax.swing.JMenu();
jMenu5 = new javax.swing.JMenu();
jMenu6 = new javax.swing.JMenu();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jToolBar1.setRollover(true);

jButton1.setText("Save");
jButton1.setFocusable(false);
jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jToolBar1.add(jButton1);

jButton2.setText("Load");
jButton2.setFocusable(false);
jButton2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
jButton2.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jToolBar1.add(jButton2);

jButton5.setText("Information");
jButton5.setFocusable(false);
jButton5.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
jButton5.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
jToolBar1.add(jButton5);

jButton6.setText("Check");
jButton6.setFocusable(false);
jButton6.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
jButton6.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
jToolBar1.add(jButton6);

jButton7.setText("Create");
jButton7.setFocusable(false);
jButton7.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
jButton7.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
jToolBar1.add(jButton7);

jLabel1.setText("Masukkan Nilai Massa :");

jLabel2.setText("Masukkan Nilai Percepatan :");

jButton3.setText("Proses");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});

jButton4.setText("Clear");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});

jLabel3.setText("Hasil Perhitungan :");

jMenu1.setText("File");

jMenu3.setText("Setting");

jMenuItem1.setText("jMenuItem1");
jMenu3.add(jMenuItem1);

jMenuItem2.setText("jMenuItem2");
jMenu3.add(jMenuItem2);

jMenu1.add(jMenu3);

jMenuItem7.setText("Open");
jMenu1.add(jMenuItem7);

jMenuItem3.setText("Save");
jMenu1.add(jMenuItem3);

jMenuItem4.setText("Exit");
jMenu1.add(jMenuItem4);

jMenuBar1.add(jMenu1);

jMenu2.setText("Edit");

jMenuItem5.setText("Copy");
jMenu2.add(jMenuItem5);

jMenuItem6.setText("Paste");
jMenu2.add(jMenuItem6);

jMenuBar1.add(jMenu2);

jMenu4.setText("View");
jMenuBar1.add(jMenu4);

jMenu5.setText("Tools");
jMenuBar1.add(jMenu5);

jMenu6.setText("About");
jMenuBar1.add(jMenu6);

setJMenuBar(jMenuBar1);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 481, Short.MAX_VALUE)
.addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 481, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGap(46, 46, 46)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2))
.addGap(59, 59, 59)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(massa)
.addComponent(perc, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 74, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(28, 28, 28))
.addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 481, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGap(44, 44, 44)
.addComponent(jLabel3)
.addContainerGap(302, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGap(240, 240, 240)
.addComponent(hasil, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(169, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(9, 9, 9)
.addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jButton3)
.addComponent(massa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(53, 53, 53)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jButton4)
.addComponent(perc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(hasil, javax.swing.GroupLayout.PREFERRED_SIZE, 12, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(72, Short.MAX_VALUE))
);

pack();
}//

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
a = Integer.parseInt(massa.getText());
b = Integer.parseInt(perc.getText());
int has = a * b;
hasil.setText(""+has);
// TODO add your handling code here:
}

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
massa.setText("");
perc.setText("");
hasil.setText("");
// TODO add your handling code here:
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
java.io.File file = new java.io.File("Save.txt");
java.io.PrintWriter output = null;
try {
output = new java.io.PrintWriter(file);
} catch (FileNotFoundException ex) {
Logger.getLogger(Pizaini.class.getName()).log(Level.SEVERE, null, ex);
}
output.print(massa.getText()+hur);
output.print(perc.getText());
output.close();

//option pane
JOptionPane.showMessageDialog(null, "Saved Successfully...!","Success",JOptionPane.WARNING_MESSAGE);
// TODO add your handling code here:
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
//event hand
int respon2 = JOptionPane.showConfirmDialog(null, "Are You Sure ?", "Tanya Dulu...", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);

java.io.File file = new java.io.File("Save.txt");
Scanner input = null;
try {
input = new Scanner(file);
} catch (FileNotFoundException ex) {
Logger.getLogger(Pizaini.class.getName()).log(Level.SEVERE, null, ex);
}
input.useDelimiter(";");
while (input.hasNext()){
String m = input.next();
String p = input.next();
massa.setText(m);
perc.setText(p);
}
input.close();
// TODO add your handling code here:
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Pizaini().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JLabel hasil;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenu jMenu3;
private javax.swing.JMenu jMenu4;
private javax.swing.JMenu jMenu5;
private javax.swing.JMenu jMenu6;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JMenuItem jMenuItem3;
private javax.swing.JMenuItem jMenuItem4;
private javax.swing.JMenuItem jMenuItem5;
private javax.swing.JMenuItem jMenuItem6;
private javax.swing.JMenuItem jMenuItem7;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JSeparator jSeparator2;
private javax.swing.JToolBar jToolBar1;
private javax.swing.JTextField massa;
private javax.swing.JTextField perc;
// End of variables declaration

}

lebih lengkap download di sini.

Animasi java pesawat terbang

animasi pesawat terbang dengan nama Project3.java
yaitu :

import java.awt.*;
import java.applet.*;
public class Animasi extends Applet{
int mouseX,mouseY,clicks;
boolean mouseIn;
Image pic1, pic2, pic3;
public boolean mouseDown(Event evt,int x,int y){
clicks=clicks+20;
mouseX=x;
mouseY=y;
repaint ();
return true;
}
public boolean mouseEnter(Event evt,int x,int y){
mouseIn=true;
repaint();
return true;
}
public boolean mouseExit(Event evt,int x,int y){
mouseIn=false;
repaint();
return true;
}

public void init(){
pic1=getImage(getCodeBase(),"duke.running.gif");
pic2=getImage(getCodeBase(),"blkg.jpg");
pic3=getImage(getCodeBase(),"Pesawat.JPG");
}
public void paint (Graphics g){
g.drawImage(pic3, 10, 70 ,120, 50, this);
int x = 10;
int y = 500;
while (true) {
g.drawImage(pic3, x, y ,120, 50, this);
x += 3;
y -= 1;

try {
Thread.sleep(40);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}


}

lebih lengkap download disini

Rumus Gaya dengan Java

Tugas 2 java terdiri atas 2 file

1. file internal
yang berisi :
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class internal extends JInternalFrame{
//private JInternalFrame saya = new JInternalFrame ();
private JLabel label1 = new JLabel("massa (m) ="),
label2 = new JLabel("percepatan (a)="),
label3 = new JLabel("gaya (F) =");

private JTextField txt1 = new JTextField(),
txt2 = new JTextField(),
txt3 = new JTextField();
private JLabel gambar = new JLabel (new ImageIcon("adhi2.jpg"));
private JButton btnCari = new JButton("hitung");

public internal(){
super("GAYA",false,true,true,true);
getContentPane().setLayout(null);
label1.setBounds(30,20,100,20);
label2.setBounds(30,60,100,20);
label3.setBounds(30,100,100,20);
gambar.setBounds(0,0,250,250);
txt1.setBounds(130,20,100,20);
txt2.setBounds(130,60,100,20);
txt3.setBounds(130,100,100,20);
btnCari.setBounds(130,140,100,20);

getContentPane().add(label1);
getContentPane().add(label2);
getContentPane().add(label3);
getContentPane().add(txt1);
getContentPane().add(txt2);
getContentPane().add(txt3);
getContentPane().add(btnCari);
getContentPane().add(gambar);
btnCari.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int Hasil = Integer.parseInt(txt1.getText()) * Integer.parseInt(txt2.getText());
txt3.setText(Integer.toString(Hasil));
}
});
setVisible(true);
setSize(280,280);
}
}

untuk file ke dua ber judul RmsAdhi
yang isi nya :
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class RmsAdhi{

private JDesktopPane baru = new JDesktopPane ();
private JFrame adhi = new JFrame("aplikasi menghitung Gaya");

private JPanel panel1 = new JPanel();

private JMenuBar cahya = new JMenuBar ();

private JMenu file = new JMenu ("file"),
edit = new JMenu ("edit"),
search = new JMenu ("search"),
view = new JMenu ("view");



private JMenuItem filenew = new JMenuItem ("new"),
fileopen = new JMenuItem ("open"),
filesave = new JMenuItem ("save"),
filesaveas = new JMenuItem ("exit"),
editundo = new JMenuItem ("undo"),
editcopy = new JMenuItem ("copy"),
editpaste= new JMenuItem ("paste"),
searchfind = new JMenuItem ("find"),
searchreplace = new JMenuItem ("replace"),
searchgoto = new JMenuItem ("go to"),
viewshow = new JMenuItem("show"),
viewwrap = new JMenuItem ("wrap"),
viewzoomin= new JMenuItem ("zoom in ");


private JToolBar tool = new JToolBar ();
private JButton btnnew = new JButton (new ImageIcon("adhie.jpg"));


//private JLabel gambar = new JLabel (new ImageIcon("g.jpg"));


public RmsAdhi (){
file.add(filenew);
file.add(fileopen);
file.addSeparator();
file.add(filesave);
file.add(filesaveas);

edit.add(editundo);
edit.add(editcopy);
edit.addSeparator();
edit.add(editpaste);

search.add(searchfind);
search.add(searchreplace);
search.add(searchgoto);
view.add(viewshow);
view.add(viewwrap);
view.add(viewzoomin);

cahya.add(file);
cahya.add(edit);
cahya.add(search);
cahya.add(view);
adhi.setJMenuBar(cahya);

ActionListener mulai = new ActionListener(){
public void actionPerformed(ActionEvent e){
internal x = new internal ();
baru.add(x);
x.setVisible(true);
}
};
btnnew.addActionListener(mulai);

tool.add(btnnew);

baru.setBackground(Color.blue);

adhi.getContentPane().add(tool, BorderLayout.NORTH);

adhi.getContentPane().add(baru, BorderLayout.CENTER);
//afdal.getContentPane().add(gambar);

adhi.setVisible(true);
adhi.setSize (800,500);
adhi.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public static void main (String[] args ){

new RmsAdhi ();
}
}

atau download  di sini.