Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Disusun Oleh:
Nama
Kelas
: 3 TCA
NIM
: 061230701276
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) {
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
}
}
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();
}
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);
}
}