nea_iyut

Sabtu, 20 Juni 2009

Tugas Besar Kelompok

import java.util.Hashtable; import java.util.Enumeration;

import javax.microedition.lcdui.Item; import javax.microedition.lcdui.List; import javax.microedition.lcdui.Form; import javax.microedition.midlet.MIDlet; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.CommandListener?;

import javax.microedition.media.Player; import javax.microedition.media.Control; import javax.microedition.media.Manager; import javax.microedition.media.PlayerListener?; import javax.microedition.media.control.VideoControl?;

public class PlayVideo? extends MIDlet

implements CommandListener?, PlayerListener? {

private Display display; private List itemList; private Form form;

private Command stopCommand; private Command pauseCommand; private Command startCommand;

private Hashtable items; private Hashtable itemsInfo;

private Player player;

public PlayVideo?() {

display = Display.getDisplay(this);

itemList = new List("Video", List.IMPLICIT);

stopCommand = new Command("Stop", Command.STOP, 1); pauseCommand = new Command("Pause", Command.ITEM, 1); startCommand = new Command("Mulai", Command.ITEM, 1);

form = new Form("Play Video");

form.addCommand(stopCommand); form.addCommand(pauseCommand); form.setCommandListener(this);

items = new Hashtable();

itemsInfo = new Hashtable();

items.put("video.mpeg", "file://video.mpg"); itemsInfo.put("video.mpeg", "video/mpeg");

}

public void startApp() {

for(Enumeration en = items.keys(); en.hasMoreElements();) {

itemList.append((String)en.nextElement(), null);

}

itemList.setCommandListener(this);

display.setCurrent(itemList);

}

public void pauseApp() {

try {

if(player != null) player.stop();

} catch(Exception e) {}

}

public void destroyApp(boolean unconditional) {

if(player != null) player.close();

}

public void commandAction(Command command, Displayable disp) {

if(disp instanceof List) {

List list = ((List)disp);

String key = list.getString(list.getSelectedIndex());

try {

playMedia((String)items.get(key), key);

} catch (Exception e) { }

} else if(disp instanceof Form) {

try {

if(command == stopCommand) {

player.close(); display.setCurrent(itemList); form.removeCommand(startCommand); form.addCommand(pauseCommand);

} else if(command == pauseCommand) {

player.stop(); form.removeCommand(pauseCommand); form.addCommand(startCommand);

} else if(command == startCommand) {

player.start(); form.removeCommand(startCommand); form.addCommand(pauseCommand);

}

} catch(Exception e) { }

}

}

private void playMedia(String locator, String key) throws Exception {

String file = locator.substring(

locator.indexOf("file://") + 6, locator.length());

player =

Manager.createPlayer(

getClass().getResourceAsStream(file), (String)itemsInfo.get(key));

player.addPlayerListener(this);

player.setLoopCount(-1); player.prefetch(); player.realize();

player.start();

}

public void playerUpdate(Player player, String event, Object eventData) {

if(event.equals(PlayerListener?.STARTED) &&

new Long(0L).equals((Long)eventData)) {

VideoControl? vc = null; if((vc = (VideoControl?)player.getControl("VideoControl?")) != null) {

Item videoDisp =

(Item)vc.initDisplayMode(vc.USE_GUI_PRIMITIVE, null);

form.append(videoDisp);

}

display.setCurrent(form);

} else if(event.equals(PlayerListener?.CLOSED)) {

form.deleteAll();

}

}

}

Runnya...

Game JavaQ...

/* Game Sepak Bola
*catatan:Game ini menggunakan java applet.
*
*Gunakan panah pada keyboard untuk bergerak, gunakan spasi untuk
*pergantian pemain
*
* */
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.applet.*;

public class SoccerGame extends JApplet implements KeyListener
{
Ball b;
Paddles p1[] = new Paddles[3];
Paddles2 p2[] = new Paddles2[3];

int active = 0;
int pcactive;
int p1score = 0;
int pcscore = 0;

public void init()
{
addKeyListener(this);
b=new Ball();

//PEMAIN 1
p1[0] = new Paddles();
p1[0].x = 70;
p1[0].y = 250;
p1[1] = new Paddles();
p1[1].x = 70;
p1[1].y = 70;
p1[2] = new Paddles();
p1[2].x = 70;
p1[2].y = 450;

//KOMPUTER, PEMAIN 2
p2[0] = new Paddles2();
p2[0].x = 630;
p2[0].y = 260;
p2[1] = new Paddles2();
p2[1].x = 700;
p2[1].y = 70;
p2[2] = new Paddles2();
p2[2].x = 700;
p2[2].y = 450;
}

public void paint(Graphics g)
{
g.setColor(Color.blue);
g.fillRect(0,0,50,606);
g.fillRect(750,0,50,800);
g.fillRect(0,0,800, 50);
g.fillRect(0,550,800, 50);
g.setColor(Color.black);
g.drawRect(50, 50, 700, 500);
g.setColor(Color.GREEN);
g.fillRect(52, 52, 697, 498); //Lapangan Sepak Bola

//GAWANG!!!!!!!!!! PEMAIN 1
g.setColor(Color.black);
g.drawLine(150, 425, 150, 175);
g.drawLine(150, 426, 50, 426);
g.drawLine(150, 175, 50, 175);

//GAWANG!!!!!!!!!! PEMAIN 2
g.setColor(Color.black);
g.drawLine(650, 425, 650, 175);
g.drawLine(750, 426, 650, 426);
g.drawLine(750, 175, 650, 175);

//Untuk garis
g.setColor(Color.black);
g.drawLine(415, 50, 415, 550);
g.drawOval(365, 260, 100, 100);
g.setColor(b.ballColor);
p1[active].p1paddleColor = Color.white;

for (int i=0; i < p1paddlecolor =" Color.red;" i="0;" i="0;"> 2)
{
active = 0;
}
}
if (p1score == 2 )
{
if (e.getKeyCode()==16)
{
b.speedY = 15;
b.speedX = 15;
}
}

if (e.getKeyCode()==40)//atas
{
p1[active].mvUp=true;
p1[active].mvDwn=false;
}
else if (e.getKeyCode()==38)//bawah
{
p1[active].mvUp=false;
p1[active].mvDwn=true;
}

if (e.getKeyCode()==37) //kiri
{
p1[active].mvLft=true;
p1[active].mvRght=false;
}
else if (e.getKeyCode()==39) //kanan
{
p1[active].mvLft=false;
p1[active].mvRght=true;
}
}

//Untuk paddle


public void keyReleased(KeyEvent e)
{
for (int i=0; i < mvup="false;" mvdwn="false;" mvup="false;" mvdwn="false;" mvlft="false;" mvrght="false;" mvrght="false;" mvlft="false;" x="400;" y="300;" width="10;" height="10;" speedx =" 10;" speedy=" 10;" direction = "left" ballcolor="Color.orange;" x =" x" y =" y" i="0;"> 730)
{
if(speedX >0)
{
speedX = -speedX;
if (speedX < direction= "left"> 0)
{
direction = "right";
}
} //Pembatas Sisi kanan
}
else if (y > 530)
{
if(speedY>0)
{
speedY = -speedY;

} //Pembatas Bawah
}
else if (x < speedx =" -speedX;" direction= "left"> 0)
{
direction = "right";
}
} //Pembatas Sisi Kiri
}
else if (y < speedy =" -speedY;"> 180 && y <> 0 && x < pcscore =" pcscore" y =" 300;" x =" 400;"> 180 && y <>660 && x < p1score =" p1score" y =" 300;" x =" 400;"> 175)
{
speedY = -speedY; //Sudut kiri atas
}
else if (x <> 426)
{
speedY = -speedY; //Sudut kiri bawah
}
if (x + width > 650 && y <> 175)
{
speedY = -speedY; //Sudut kanan atas
}
else if (x + width > 650 && y <> 426)
{
speedY = -speedY; //Sudut kanan bawah
}
}

//Di sini pembagian pemain menjadi 3 area, sehingga pemain dapat bergerak dalam satu waktu
//Berdasarkan pemain yang sedang memiliki bola

public void compCheck()
{
//Komputer
if (x > 55 && x <> 55 && y < y =" p2[1].y"> b.y)
{
p2[1].y = p2[1].y - p2[1].speed;
}
if (p2[1].x > b.x)
{
p2[1].x = p2[1].x - p2[1].speed;
}
else if (p2[1].x < x =" p2[1].x"> 500 && x <> 175 && y < y =" p2[0].y"> b.y)
{
p2[0].y = p2[0].y - p2[0].speed;
}
}

if (x > 55 && x <> 350 && y < y =" p2[2].y"> b.y)
{
p2[2].y = p2[2].y - p2[2].speed;
}
if (p2[2].x > b.x)
{
p2[2].x = p2[2].x - p2[2].speed;
}
else if (p2[2].x < x =" p2[2].x" y =" 250;" x =" 100;" width =" 10;" height =" 50;" speed =" 10;" p1paddlecolor="Color.red;" direction ="=""> y)
{
if (b.y <> x)
{
if (b.x < speedx =" -b.speedX;" direction= "left"> 0)
{
b.direction = "right";
}
}
}
}
}
}
} // akhir checkpaddle

public void move()
{
if (mvUp == true)
{
y = y + speed;
mvDwn = false;
}
else if (mvDwn == true)
{
y = y - speed;
mvUp = false;
}
if (mvLft == true)
{
x = x - speed;
mvRght = false;
}
else if (mvRght == true)
{
x = x + speed;
mvLft = false;
}
}
} //akhir dari class paddles

public class Paddles2
{
int y = 250;
int x = 500;
int width = 10;
int height = 50;
int speed = 2;
Color p2paddleColor=Color.black;

public void checkPaddle2()
{
//cek jika bola mengenai paddle
if (b.direction == "right")
if (b.y + 20 > y)
{
if (b.y <> x)
{
if (b.x < speedx =" -b.speedX;" direction= "left"> 0)
{
b.direction = "right";
}
}
}
}
}
} // akhir checkpaddle
} // akhir class paddles2
} //akhir class soccer

//Untuk SoccerGame.htm
//Untuk Pemanggilan Soocergame.class













//Tampilan Game Menggunakan Java Applet

Source Java...3

class Lingkaran
{
private double jari_jari;

public Lingkaran()
{
setJariJari(1);
}

public void Lingkaran(double r)
{
if(r>0)
jari_jari=r;
else
jari_jari=1.0;
}
public void setJariJari(double r)
{
if(r>0)
jari_jari=r;
}

public double getJariJari()
{
return(jari_jari);
}

public double hitungLuas()
{
return(Math.PI* jari_jari*jari_jari);
}

public double hitungKeliling()
{
return(Math.PI*2.0*jari_jari);
}
}

public class CobaLingkaran_2
{
public static void main(String args[])
{
Lingkaran abc=new Lingkaran();
Lingkaran pqr=new Lingkaran();
Lingkaran xyz=new Lingkaran();

pqr.setJariJari(5);
xyz.setJariJari(3.2);

System.out.println("Data Default");
System.out.println("=====================");
System.out.println("Jari-jari abc: " +abc.getJariJari());
System.out.println("Jari-jari pqr: " +pqr.getJariJari());
System.out.println("Jari-jari xyz: " +xyz.getJariJari());
System.out.println();

abc.setJariJari(5);
pqr.setJariJari(3.2);
xyz.setJariJari(1.8);

System.out.println("Data Sekarang: ");
System.out.println("=====================");
System.out.println("Jari-jari abc: " +abc.getJariJari());
System.out.println("Jari-jari pqr: " +pqr.getJariJari());
System.out.println("Jari-jari xyz: " +xyz.getJariJari());
System.out.println();
}
}

Source java...

//MousePaint.java
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class MousePaint extends Applet {
int last_x, last_y;

public void init(){

//Definisi dan registrasi objek MouseListener
this.addMouseListener(new MouseAdapter(){
public void Pressed(MouseEvent e){
last_x= e.getX();
last_y= e.getY();
}
});

//Definisi dan registrasi objek MouseMotionListener
this.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e) {
Graphics g = getGraphics();
int x = e.getX(), y = e.getY();
g.setColor(Color.black);
g.drawLine (last_x,last_y,x,y);
last_x = x; last_y=y;
}
});

//tombol untuk menghapus layar
Button b = new Button ("HAPUS!");
//Mendefinisikan dan registrasi tombol
b.addActionListener(new ActionListener(){
public void actionPerformade (ActionEvent e) {

//Membersihkan layar
Graphics g=getGraphics();
g.setColor(getBackground());
g.fillRect(0,0, getSize().width,getSize().height);
}
});
this.add(b);
}
}

Source....2

//RetrieveText.java
import java.awt.*;
import java.applet.*;

public class RetrieveText extends Applet
{
Label label;
TextField textField;

public void init()
{
//Membuat label;
label = new Label ("Masukkan teks kemudian tekan Enter...!");
textField = new TextField(20);
add(label);
add(textField);
}
public void paint (Graphics g)
{
g.drawString ("Anda menuliskan : ", 30, 80);

//Mengambil input String
String s = textField.getText();
g.drawString(s, 30, 100);
}

public boolean action(Event event,Object arg)
{
repaint();
return true;
}
}

source...1

//Text.java
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Text extends Applet{

//Inisialisasi
TextField textField = new TextField();
TextArea textArea = new TextArea();
Button insert = new Button ("Insert");
Button clear = new Button ("Clear");

public void init(){
setLayout (null);

//MEmbuat ukkura textField serta textarea
textField.setBounds(10,10,200,25);
textArea.setBounds(10,50,200,75);

//Memasukkan serta menghapus input
insert.setBounds(10,140,75,25);
clear.setBounds(100,140,75,25);
insert.addActionListener(new ButtonHandler());
clear.addActionListener(new ButtonHandler());

add(textField);
add(textArea);
add(insert);
add(clear);}

//Mendeteksi dan menerapkan perintah
class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent ev){
String s=ev.getActionCommand();
if(s.equals("Insert")){
String text =textArea.getText()+textField.getText();
textArea.setText(text);
}
else if(s.equals ("Clear")) textArea.setText("");
}
}
}

Source yang laeennn.....


//BuatMenu.java
import java.awt.*;
import java.awt.event.*;

public class BuatMenu extends Frame {
public static void main (String []args) {

//Daftar menu
MenuItem mi;
Menu file = new Menu ("File" ,true);
file.add ("open");
file.add (mi=new MenuItem ("Close"));

//Menu tidak aktif
mi.setEnabled (false);
file.add ("Quit");
Menu help = new Menu ("Help");
help.add ("about");
MenuBar mb = new MenuBar ();
mb.add (file);
mb.add(help);
mb.setHelpMenu (help);

Frame f = new Frame ("Buat MEnuQ..");
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
f.setMenuBar (mb);
f.setSize(320,160);
f.show();
}
}

Source JavaQ yang laen....


//MyWindow.java
import java.awt.*;
import java.awt.event.*;

public class MyWindow {
public static void main (String args[]){
Frame f= new Frame ("Kurnia Maulani...");

//Memasukkan event pada window
f.addWindowListener (new WindowAdapter (){

//Menangkap event close
public void windowClosing (WindowEvent e){

//Menutup window
System.exit(0);
}
});
f.setSize (320,160);
f.show();
}
}

hasilnya: