Sei sulla pagina 1di 20

Laporan Praktek Sistem Operasi

Disusun Oleh:
Nama

: Danny Julian Wahyudi

Kelas

: 3 TCA

NIM

: 061230701276

Dosen : Ali Firdaus S.Kom M.Kom

Jurusan Teknik Komputer


Politeknik Negeri Sriwijaya Palembang
2013

Menggunakan Program Demos di Sun Java


1. Buka Program Sun Java
2. Setelah terbuka klik open project

3. Pilih program Demos

4. Setelah di buka akan muncul pesan bahwa program berhasil di-loaded

5. Klik menu Run untuk menjalankan program

6. Setelah itu akan muncul tampilan handpone

7. Terdapat beberapa pilihan di layar handpone


a. Colors : Program untuk melihat komposisi warna. Misalnya untuk membuat
warna kuning kita harus membuat kompisisi warna hijau 255 dan merah 255.
b. Properties : Program untuk melihat memori, platform dan profile yang digunakan.
c. Http : Program yang digunakan untuk browsing internet.
d. FontTestlet : Program yang berisi jenis-jenis font.
e. ManyBalls : Game.

Colors

Properties

Http

FontTestlet

ManyBalls

Source Code
1. Colors
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.MIDlet;
public class Chooser extends MIDlet implements CommandListener {
private Display display; // Our display
private FontChooser fonts;
private TextSample sample;
private ColorChooser colors;
private Command exitCommand = new Command("Exit", Command.EXIT, 1);
private Command textColorCommand = new Command("Text Color",
Command.SCREEN, 3);
private Command backgroundColorCommand = new Command("Background Color",
Command.SCREEN, 4);
private Command fontsCommand = new Command("Fonts", Command.SCREEN, 11);
private Command okCommand = new Command("Ok", Command.SCREEN, 2);
private Command okFgCommand = new Command("Ok", Command.SCREEN, 2);
private Command okBgCommand = new Command("Ok", Command.SCREEN, 2);

public Chooser() {
display = Display.getDisplay(this);
sample = new TextSample();
sample.addCommand(exitCommand);
sample.addCommand(textColorCommand);
sample.addCommand(backgroundColorCommand);
sample.addCommand(fontsCommand);
sample.setCommandListener(this);
}
public void startApp() {
display.setCurrent(sample);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable s) {
if (c == exitCommand) {
destroyApp(true);
notifyDestroyed();
} else if (c == fontsCommand) {
if (fonts == null) {

fonts = new FontChooser();


fonts.setFace(sample.getFace());
fonts.setStyle(sample.getStyle());
fonts.setSize(sample.getSize());
fonts.addCommand(okCommand);
fonts.setCommandListener(this);
}
display.setCurrent(fonts);
} else if (c == backgroundColorCommand) {
if (colors == null) {
colors = new ColorChooser(display.isColor());
colors.setCommandListener(this);
}
colors.addCommand(okBgCommand);
colors.removeCommand(okFgCommand);
colors.setColor(sample.getBackgroundColor());
display.setCurrent(colors);
} else if (c == textColorCommand) {
if (colors == null) {
colors = new ColorChooser(display.isColor());
colors.setCommandListener(this);
}
colors.addCommand(okFgCommand);
colors.removeCommand(okBgCommand);
colors.setColor(sample.getForegroundColor());
display.setCurrent(colors);
} else if (c == okCommand) {
if (s == fonts) {
sample.setStyle(fonts.getStyle());
sample.setFace(fonts.getFace());
sample.setSize(fonts.getSize());
}
display.setCurrent(sample);
} else if (c == okFgCommand) {
sample.setForegroundColor(colors.getColor());
display.setCurrent(sample);
} else if (c == okBgCommand) {
sample.setBackgroundColor(colors.getColor());
display.setCurrent(sample);
}
}
}

2. Properties
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class PropExample extends MIDlet implements CommandListener {
private Display display;
private Form props;
private StringBuffer propbuf;
private Command exitCommand = new Command("Exit", Command.EXIT, 1);
private boolean firstTime;
public PropExample() {
display = Display.getDisplay(this);

firstTime = true;
props = new Form("System Properties");
}
public void startApp() {
Runtime runtime = Runtime.getRuntime();
runtime.gc();
long free = runtime.freeMemory();
if (firstTime) {
long total = runtime.totalMemory();
propbuf = new StringBuffer(50);
props.append("Free Memory = " + free + "\n");
props.append("Total Memory = " + total + "\n");
props.append(showProp("microedition.configuration"));
props.append(showProp("microedition.profiles"));
props.append(showProp("microedition.platform"));
props.append(showProp("microedition.locale"));
props.append(showProp("microedition.encoding"));
props.addCommand(exitCommand);
props.setCommandListener(this);
display.setCurrent(props);
firstTime = false;
} else {
props.set(0, new StringItem("", "Free Memory = " + free + "\n"));
}
display.setCurrent(props);
}
public void commandAction(Command c, Displayable s) {
if (c == exitCommand) {
destroyApp(false);
notifyDestroyed();
}
}
String showProp(String prop) {
String value = System.getProperty(prop);
propbuf.setLength(0);
propbuf.append(prop);
propbuf.append(" = ");
if (value == null) {
propbuf.append("<undefined>");
} else {
propbuf.append("\"");
propbuf.append(value);
propbuf.append("\"");
}
propbuf.append("\n");
return propbuf.toString();
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}

3. Http

public class HttpTest extends MIDlet implements CommandListener, Runnable {


private Command getCommand = new Command("Get", Command.SCREEN, 1);
private Command postCommand = new Command("Post", Command.SCREEN, 1);
private Command headCommand = new Command("Head", Command.SCREEN, 1);
private Command chooseCommand = new Command("Choose", Command.SCREEN, 2);
private Command addCommand = new Command("Add", Command.SCREEN, 1);
private Command addSaveCommand = new Command("OK", Command.SCREEN, 1);
private Command okCommand = new Command("OK", Command.OK, 1);
private Command cancelCommand = new Command("Cancel", Command.CANCEL, 1);
private Display display;
private String url;
private Vector urls;
private List list;
private TextBox addTextBox;
private Command currentCommand;
private Thread commandThread;
private int attempt;
private TextBox t;
private boolean firstTime;
public HttpTest() {
urls = new Vector();
urls.addElement("http://cds.cmsg.sun.com:80/serverscript/serverscript");
urls.addElement("http://www.sun.com/");
urls.addElement("https://java.com");
urls.addElement("-----------------------");
urls.addElement("http://localhost:8080/");
urls.addElement("-----------------------");
urls.addElement("shttp://host/notsupportedprotocol");
urls.addElement("http://:8080/missinghost");
urls.addElement("http://mal\\formed:axyt/url???");
urls.addElement("http://www.sun.com/no/such/page/");
urls.addElement("http://www.sun.com:29999/no/such/port/");
urls.addElement("http://no.such.site/");
urls.addElement("http://www.sun.com/bad_proxy/");
url = (String)urls.elementAt(0);
display = Display.getDisplay(this);
firstTime = true;
}
static final void DEBUG(String s) {
if (true) {
System.out.println(s);
}
}
private String time2str(long time) {
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
c.setTime(new Date(time));
return c.toString();
}
public void startApp() {
if (firstTime) {
String u = getAppProperty("HttpTest-Url");
if (u != null) {
url = u;
}
mainScreen();
firstTime = false;
} else {
display.setCurrent(t);

}
}
void mainScreen() {
String s =
"URL = " + url + ". Press Get or Post to fetch it, or Choose to " + "use another URL";
t = new TextBox("Http Test", s, s.length(), 0);
setCommands(t, false);
display.setCurrent(t);
}
void chooseScreen() {
list = new List("Choose URL", Choice.EXCLUSIVE);
for (int i = 0; i < urls.size(); i++) {
list.append((String)urls.elementAt(i), null);
}
setCommands(list, true);
display.setCurrent(list);
}
void addScreen() {
addTextBox = new TextBox("New URL", "http://", 200, 0);
addTextBox.addCommand(addSaveCommand);
addTextBox.addCommand(cancelCommand);
addTextBox.setCommandListener(this);
display.setCurrent(addTextBox);
}
private void readContents(String request) {
StringBuffer b = new StringBuffer();
++attempt;
b.append("attempt " + attempt + " content of " + request + " " + url + "\n");
HttpConnection c = null;
OutputStream os = null;
InputStream is = null;
TextBox t = null;
try {
long len = -1;
int ch = 0;
long count = 0;
int rc;
DEBUG(request + " Page: " + url);
c = (HttpConnection)Connector.open(url);
DEBUG("c= " + c);
c.setRequestMethod(request);
c.setRequestProperty("foldedField", "first line\r\n second line\r\n third line");
if (request == HttpConnection.POST) {
String m = "Test POST text.";
DEBUG("Posting: " + m);
os = c.openOutputStream();
os.write(m.getBytes());
os.close();
}
rc = c.getResponseCode();
if (rc != HttpConnection.HTTP_OK) {
b.append("Response Code: " + c.getResponseCode() + "\n");
b.append("Response Message: " + c.getResponseMessage() + "\n\n");
}
is = c.openInputStream();
DEBUG("is = " + is);
if (c instanceof HttpConnection) {
len = ((HttpConnection)c).getLength();
}

DEBUG("len = " + len);


if (len != -1) {
DEBUG("Content-Length: " + len);
for (int i = 0; i < len; i++) {
if ((ch = is.read()) != -1) {
if (ch <= ' ') {
ch = ' ';
}
b.append((char)ch);
count++;
if (count > 200) {
break;
}
}
}
} else {
byte[] data = new byte[100];
int n = is.read(data, 0, data.length);
for (int i = 0; i < n; i++) {
ch = data[i] & 0x000000ff;
b.append((char)ch);
}
}
try {
if (is != null) {
is.close();
}
if (c != null) {
c.close();
}
} catch (Exception ce) {
DEBUG("Error closing connection");
}
try {
len = is.available();
DEBUG("Inputstream failed to throw IOException after close");
} catch (IOException io) {
DEBUG("expected IOException (available())");
io.printStackTrace();
}
t = new TextBox("Http Test", b.toString(), b.length(), 0);
is = null;
c = null;
} catch (IOException ex) {
ex.printStackTrace();
DEBUG(ex.getClass().toString());
DEBUG(ex.toString());
DEBUG("Exception reading from http");
if (c != null) {
try {
String s = null;
if (c instanceof HttpConnection) {
s = ((HttpConnection)c).getResponseMessage();
}
DEBUG(s);
if (s == null) {
s = "No Response message";
}
t = new TextBox("Http Error", s, s.length(), 0);
} catch (IOException e) {

e.printStackTrace();
String s = e.toString();
DEBUG(s);
if (s == null) {
s = ex.getClass().getName();
}
t = new TextBox("Http Error", s, s.length(), 0);
}
try {
c.close();
} catch (IOException ioe) {
}
} else {
t = new TextBox("Http Error", "Could not open URL", 128, 0);
}
} catch (IllegalArgumentException ille) {
t = new TextBox("Illegal Argument", ille.getMessage(), 128, 0);
} catch (Exception e) {
t = new TextBox("Error", e.toString(), 128, 0);
}
if (is != null) {
try {
is.close();
} catch (Exception ce) {
;
}
}
if (c != null) {
try {
c.close();
} catch (Exception ce) {
;
}
}
setCommands(t, false);
display.setCurrent(t);
}
private void readHeaders(String request) {
HttpConnection c;
TextBox t;
StringBuffer b;
try {
try {
c = (HttpConnection)Connector.open(url);
} catch (IllegalArgumentException e) {
String m = e.getMessage();
t = new TextBox("Illegal argument", e.getMessage(), 128, 0);
setCommands(t, false);
display.setCurrent(t);
return;
} catch (ConnectionNotFoundException e) {
t = new TextBox("Error", "Protocol not supported", 128, 0);
setCommands(t, false);
display.setCurrent(t);
return;
} catch (Exception e) {
t = new TextBox("Error", e.toString(), 128, 0);
setCommands(t, false);
display.setCurrent(t);
return;

}
try {
c.setRequestMethod(request);
b = new StringBuffer();
b.append("URL: ");
b.append(c.getURL());
b.append("\nProtocol: ");
b.append(c.getProtocol());
b.append("\nHost: " + c.getHost());
b.append("\nFile: " + c.getFile());
b.append("\nRef: " + c.getRef());
b.append("\nQuery: ");
b.append(c.getQuery());
b.append("\nPort: ");
b.append(c.getPort());
b.append("\nMethod: ");
b.append(c.getRequestMethod());
if (c instanceof HttpsConnection) {
SecurityInfo sslInfo = ((HttpsConnection)c).getSecurityInfo();
Certificate cert = sslInfo.getServerCertificate();
b.append("\nSecure protocol: ");
b.append(sslInfo.getProtocolName());
b.append("\nSecure protocol version: ");
b.append(sslInfo.getProtocolVersion());
b.append("\nCipher suite: ");
b.append(sslInfo.getCipherSuite());
if (cert == null) {
b.append("\nNo server Certificate.");
} else {
b.append("\nServer certificate \n\t Type: ");
b.append(cert.getType());
b.append("\n\t Version: ");
b.append(cert.getVersion());
b.append("\n\t Serial number: ");
b.append(cert.getSerialNumber());
b.append("\n\t Issuer: ");
b.append(cert.getIssuer());
b.append("\n\t Subject: ");
b.append(cert.getSubject());
b.append("\n\t Signature algorithm: ");
b.append(cert.getSigAlgName());
b.append("\n\t Not valid before: ");
b.append(time2str(cert.getNotBefore()));
b.append("\n\t Not valid after: ");
b.append(time2str(cert.getNotAfter()));
}
}
b.append("\nResponseCode: ");
b.append(c.getResponseCode());
b.append("\nResponseMessage:");
b.append(c.getResponseMessage());
b.append("\nContentLength: ");
b.append(c.getLength());
b.append("\nContentType: ");
b.append(c.getType());
b.append("\nContentEncoding: ");
b.append(c.getEncoding());
b.append("\nContentExpiration: ");
b.append(c.getExpiration());
b.append("\nDate: ");

b.append(c.getDate());
b.append("\nLast-Modified: ");
b.append(c.getLastModified());
b.append("\n\n");
int h = 0;
while (true) {
try {
String key = c.getHeaderFieldKey(h);
if (key == null) {
break;
}
String value = c.getHeaderField(h);
b.append(key);
b.append(": ");
b.append(value);
b.append("\n");
h++;
} catch (Exception e) {
break;
}
}
t = new TextBox("Http Test", b.toString(), b.length(), 0);
setCommands(t, false);
display.setCurrent(t);
} finally {
c.close();
}
} catch (ConnectionNotFoundException e) {
t = new TextBox("Error", "Could not Connect.", 128, 0);
setCommands(t, false);
display.setCurrent(t);
return;
} catch (CertificateException e) {
StringBuffer m = new StringBuffer(256);
String s;
Certificate cert = e.getCertificate();
m.append(e.getMessage());
if (cert != null) {
m.append("\nServer certificate \n\t Type: ");
m.append(cert.getType());
m.append("\n\t Version: ");
m.append(cert.getVersion());
m.append("\n\t Serial number: ");
m.append(cert.getSerialNumber());
m.append("\n\t Issuer: ");
m.append(cert.getIssuer());
m.append("\n\t Subject: ");
m.append(cert.getSubject());
m.append("\n\t Signature algorithm: ");
m.append(cert.getSigAlgName());
m.append("\n\t Not valid before: ");
m.append(time2str(cert.getNotBefore()));
m.append("\n\t Not valid after: ");
m.append(time2str(cert.getNotAfter()));
}
s = m.toString();
t = new TextBox("Certificate Error", s, s.length(), 0);
setCommands(t, false);
display.setCurrent(t);

return;
} catch (IOException ex) {
ex.printStackTrace();
}
}
void setCommands(Displayable d, boolean islist) {
if (islist) {
d.addCommand(addCommand);
d.addCommand(okCommand);
} else {
d.addCommand(exitCommand);
d.addCommand(chooseCommand);
d.addCommand(getCommand);
d.addCommand(postCommand);
d.addCommand(headCommand);
}
d.setCommandListener(this);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable s) {
synchronized (this) {
if (commandThread != null) {
return;
}
currentCommand = c;
commandThread = new Thread(this);
commandThread.start();
}
}
public void run() {
if (currentCommand == exitCommand) {
destroyApp(false);
notifyDestroyed();
} else if (currentCommand == getCommand) {
readContents(HttpConnection.GET);
} else if (currentCommand == postCommand) {
readContents(HttpConnection.POST);
} else if (currentCommand == headCommand) {
readHeaders(HttpConnection.HEAD);
} else if (currentCommand == chooseCommand) {
chooseScreen();
} else if (currentCommand == okCommand) {
int i = list.getSelectedIndex();
if (i >= 0) {
url = list.getString(i);
}
mainScreen();
} else if (currentCommand == addSaveCommand) {
urls.addElement(addTextBox.getString().trim());
chooseScreen();
} else if (currentCommand == addCommand) {
addScreen();
} else if (currentCommand == cancelCommand) {
chooseScreen();
}
synchronized (this) {

commandThread = null;
}
}
}

4. FontTestlet
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.Font;
import javax.microedition.lcdui.Graphics;
//import java.io.*;
//import javax.microedition.midlet.*;
//import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class FontTestlet extends MIDlet implements CommandListener {
private Display myDisplay;
private FontCanvas myCanvas;
private int currentFace = Font.FACE_SYSTEM;
private Command monospaceCommand = new Command("monospace", Command.ITEM,
1);
private Command proportionalCommand = new Command("proportional",
Command.ITEM, 1);
private Command systemCommand = new Command("system", Command.ITEM, 1);
private Command exit = new Command("Exit", Command.EXIT, 3);
public FontTestlet() {
super();
myDisplay = Display.getDisplay(this);
myCanvas = new FontCanvas(this); // pointer to myself
myCanvas.setCommandListener(this);
myCanvas.addCommand(monospaceCommand);
myCanvas.addCommand(proportionalCommand);
myCanvas.addCommand(exit);
}
public void init() throws MIDletStateChangeException {
}
public void startApp() throws MIDletStateChangeException {
myDisplay.setCurrent(myCanvas);
}
public void pauseApp() {
}
public void destroyApp(boolean cond) {
myDisplay.setCurrent((Displayable)null);
myCanvas.destroy();
notifyDestroyed();
}
public void paint(Graphics g) {
String title;
int height = 0;
g.setColor(0x00000000);
g.fillRect(0, 0, myCanvas.getWidth(), myCanvas.getHeight());
g.setColor(0x00ffffff);
switch (currentFace) {

case Font.FACE_SYSTEM:
title = "System";
break;
case Font.FACE_PROPORTIONAL:
title = "Proportional";
break;
case Font.FACE_MONOSPACE:
title = "Monospaced";
break;
default:
title = "unknown";
break;
}
g.drawString(title, 0, 0, Graphics.TOP | Graphics.LEFT);
height += g.getFont().getHeight();
g.setFont(Font.getFont(currentFace, Font.STYLE_PLAIN, Font.SIZE_LARGE));
g.drawString("Regular plain", 0, height, Graphics.TOP | Graphics.LEFT);
height += g.getFont().getHeight();
g.setFont(Font.getFont(currentFace, Font.STYLE_ITALIC, Font.SIZE_LARGE));
g.drawString("Regular ital", 0, height, Graphics.TOP | Graphics.LEFT);
height += g.getFont().getHeight();
g.setFont(Font.getFont(currentFace, Font.STYLE_BOLD, Font.SIZE_LARGE));
g.drawString("Bold plain", 0, height, Graphics.TOP | Graphics.LEFT);
height += g.getFont().getHeight();
g.setFont(Font.getFont(currentFace, Font.STYLE_BOLD | Font.STYLE_ITALIC,
Font.SIZE_LARGE));
g.drawString("Bold ital", 0, height, Graphics.TOP | Graphics.LEFT);
}
Command getCurrentCommand() {
switch (currentFace) {
case Font.FACE_MONOSPACE:
return monospaceCommand;
case Font.FACE_PROPORTIONAL:
return proportionalCommand;
case Font.FACE_SYSTEM:default:
return systemCommand;
}
}
public void commandAction(Command cmd, Displayable disp) {
myCanvas.addCommand(getCurrentCommand());
if (cmd == monospaceCommand) {
myCanvas.removeCommand(monospaceCommand);
currentFace = Font.FACE_MONOSPACE;
} else if (cmd == proportionalCommand) {
myCanvas.removeCommand(proportionalCommand);
currentFace = Font.FACE_PROPORTIONAL;
} else if (cmd == systemCommand) {
myCanvas.removeCommand(systemCommand);
currentFace = Font.FACE_SYSTEM;
} else if (cmd == exit) {
destroyApp(true);
}
myCanvas.repaint();
}
}

5. ManyBalls

import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class ManyBalls extends MIDlet implements CommandListener {
Display display;
ManyCanvas canvas; // The main screen
private Command exitCommand = new Command("Exit", Command.EXIT, 99);
private Command toggleCommand = new Command("Stop/Go", Command.SCREEN, 1);
private Command helpCommand = new Command("Help", Command.HELP, 2);
private Form helpScreen;

public ManyBalls() {
display = Display.getDisplay(this);
canvas = new ManyCanvas(display, 40);
canvas.addCommand(exitCommand);
canvas.addCommand(toggleCommand);
canvas.addCommand(helpCommand);
canvas.setCommandListener(this);
}
public void startApp() throws MIDletStateChangeException {
canvas.start();
}
public void pauseApp() {
canvas.pause();
}
public void destroyApp(boolean unconditional) throws MIDletStateChangeException {
canvas.destroy();
}
public void commandAction(Command c, Displayable s) {
if (c == toggleCommand) {

if (canvas.isPaused()) {
canvas.start();
} else {
canvas.pause();
}
} else if (c == helpCommand) {
canvas.pause();
showHelp();
} else if (c == exitCommand) {
try {
destroyApp(false);
notifyDestroyed();
} catch (MIDletStateChangeException ex) {
}
}
}
void showHelp() {
if (helpScreen == null) {
helpScreen = new Form("Many Balls Help");
helpScreen.append("^ = faster\n");
helpScreen.append("v = slower\n");
helpScreen.append("< = fewer\n");
helpScreen.append("> = more\n");
}
helpScreen.addCommand(toggleCommand);
helpScreen.setCommandListener(this);
display.setCurrent(helpScreen);
}
}

Potrebbero piacerti anche