Graphics, Threads and HTTPConnections in MIDLets

40
Graphics, Threading and HTTP-Connection in MIDP Jussi Pohjolainen TAMK University of Applied Sciences

Transcript of Graphics, Threads and HTTPConnections in MIDLets

Page 1: Graphics, Threads and HTTPConnections in MIDLets

Graphics, Threading and HTTP-Connection in MIDP

Jussi PohjolainenTAMK University of Applied Sciences

Page 2: Graphics, Threads and HTTPConnections in MIDLets

GRAPHICS IN MIDP

Page 3: Graphics, Threads and HTTPConnections in MIDLets

Class Hierarchy

javax.microedition.lcduijavax.microedition.lcdui javax.microedition.lcdui.gamejavax.microedition.lcdui.game

DisplayableDisplayable

AlertAlert ListList FormForm TextBoxTextBox

ScreenScreen

CanvasCanvas GameCanvasGameCanvas

Page 4: Graphics, Threads and HTTPConnections in MIDLets

Using Graphics

• Class: javax.microedition.lcdui.Canvas• Create a subclass:

– class MyCanvas extends Canvas

• Canvas-class has only one abstract method:– paint(graphics g)

• It is possible to override methods that deal with events

Page 5: Graphics, Threads and HTTPConnections in MIDLets

Simple Exampleclass MyCanvas extends Canvas{

public void paint(Graphics g){// draw

}

}

class MyMidlet extends MIDlet{

public void startApp(){MyCanvas mycanvas = new MyCanvas();

Display.getDisplay(this).setCurrent(mycanvas);

}

}

Page 6: Graphics, Threads and HTTPConnections in MIDLets

Repainting

• You never call the paint() method.• Instead of you use repaint():

– By using this method, you ask the framework to repaint the canvas

– Framework decides when is the best time to repaint the canvas

• There is a also:– repaint(int x, int y, int width, int height)

Page 7: Graphics, Threads and HTTPConnections in MIDLets

Coordinates

• Upper-Left (0,0)• Translate the origon

– translate() – metodi

• Origo's position:– getTranslateX()– getTranslateY()

x

y

Page 8: Graphics, Threads and HTTPConnections in MIDLets

Graphics-classes drawing methods

• See the API!– drawLine(..)– drawRect(...)– drawRoundRect(...)– drawArc(...)– fillTriangle(...)– fillRect(...)– fillRoundRect(...)– fillArc(...)

Page 9: Graphics, Threads and HTTPConnections in MIDLets

Graphics-classes Color related methods

• public void setColor(int r, int g, int b)

• int getColor()

• int getRedComponent()

• int getBlueComponent()

• int getGreenComponent()

• isColor()

• numColors()

Page 10: Graphics, Threads and HTTPConnections in MIDLets

Line types

• setStrokeStyle(Graphics.DOTTED)• setStrokeStyle(Graphics.SOLID)

Page 11: Graphics, Threads and HTTPConnections in MIDLets

Drawing text

• public void drawString(String str, int x, int y, int anchor)

• What is anchor?

HeipodeiTOP|LEFT

TOP|HCENTERTOP|RIGHT

BASELINE|RIGHT

BOTTOM|RIGHTBOTTOM | HCENTER

BASELINE | HCENTER

BASELINE|LEFT

BOTTOM | LEFT

Page 12: Graphics, Threads and HTTPConnections in MIDLets

Fonts

• Types– Font.FACE_PROPORTIONAL– Font.FACE_MONOSPACE– Font.FACE_SYSTEM

• Styles– Font.STYLE_PLAIN– Font.STYLE_BOLD– Font.STYLE_ITALIC– Font.STYLE_UNDERLINE

• Sizes– Font.SIZE_SMALL– Font.SIZE_MEDIUM– Font.SIZE_LARGE

Page 13: Graphics, Threads and HTTPConnections in MIDLets

Using Font

• Font f = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_ITALIC, Font.SIZE_SMALL);

• g.setFont(f);• g.drawString(”Hello”, 0,0, Graphics.TOP |

Graphics.LEFT);

Page 14: Graphics, Threads and HTTPConnections in MIDLets

Measuring text

• public int charWidth(char c)

• public int charsWidth(char ch, int offset, int length)

• public int stringWidth(String str)

• public int substringWidth(String str, int offset, int len)

Page 15: Graphics, Threads and HTTPConnections in MIDLets

Pictures

• MIDP Specification supports atleast png-format• You can put the pictures in the res-folder

– => .jar

• Methods– static Image createImage(String name) (Image-class)

– void drawImage(Image img, int x, int y, int anchor) (Graphics-class)

Page 16: Graphics, Threads and HTTPConnections in MIDLets

Picture's anchorsTOP | HCENTERTOP | LEFT

VCENTER | LEFT

BOTTOM | LEFT BOTTOM | HCENTER BOTTOM | RIGHT

VCENTER | RIGHT

TOP | RIGHT

VCENTER | HCENTER

Page 17: Graphics, Threads and HTTPConnections in MIDLets

Event handling• Canvas-class

– protected void keyPressed(int keyCode)– protected void keyReleased(int keyCode)– protected void keyRepeated(int keyCode) – protected void pointerPressed(int x, int y)– protected void pointerReleased(int x, int y)– protected void pointerDragged(int x, int y)

Page 18: Graphics, Threads and HTTPConnections in MIDLets

Example: Using Graphics

Page 19: Graphics, Threads and HTTPConnections in MIDLets

THREADS

Page 20: Graphics, Threads and HTTPConnections in MIDLets

What are threads?

• Multithreading.. – Methods that are running at the same time– For example: downloading, checking and

analyzing is happening in a program at the same time.

• In Java:– An instance of class java.lang.Thread

Page 21: Graphics, Threads and HTTPConnections in MIDLets

JVM and Threads

• JVM is responsible scheduling the threads• Sometimes native OS's thread system is

used... and sometimes it is not.– When it comes to threads, very little is

guaranteed.

• Different JVMs run threads in a different way!

Page 22: Graphics, Threads and HTTPConnections in MIDLets

Making a thread

• A thread begins as an instance of java-lang.Thread

• Thread class has following methods (among others)– start()– yield()– sleep()– run()

Page 23: Graphics, Threads and HTTPConnections in MIDLets

run-method

• This is the method that you want to be executed in a separed thread

• public void run(){– You job code goes to here

• }

• Where does the run() method go?• Two choices

– 1) Extend the java.lang.Thread class– 2) Implement the Runnable interface

Page 24: Graphics, Threads and HTTPConnections in MIDLets

1) Extending java.lang.Thread

• The simplest way to define code to run in a separate thread is to– Extend the Thread class– Override the run() method

class MyThread extends Thread{

public void run(){

System.out.println("Job running in thread");

}

}

Page 25: Graphics, Threads and HTTPConnections in MIDLets

2) Implementing java.lang.Runnable

• Implement the Runnable interfaceclass MyRunnable implements Runnable{

public void run(){

System.out.println("Job running in Thread");

}

}

Page 26: Graphics, Threads and HTTPConnections in MIDLets

Two choices..

• Why two choices?• Use choice 1 (extending Thread) if possible..

(thread is easier to start)• If your class extends already some other class,

use choice 2 (implement Runnable)

Page 27: Graphics, Threads and HTTPConnections in MIDLets

Instantiating a Thread

• Every thread of execution begins as an instance of class Thread.

• If you extended the Thread-class:– MyThread t = new MyThread();

• If you implemented Runnable:– MyRunnable r = new MyRunnable();

Thread t = new Thread(r);

Page 28: Graphics, Threads and HTTPConnections in MIDLets

Starting a Thread

• Once the Thread is created, you have to start it.• Starting the thread is done by using start()-method:

– t.start();

• What happens after you call start()?– A new thread of execution starts– The thread moves from new state to runnable state.– When the thread gets a chance to execute, its target run()

method will run.

Page 29: Graphics, Threads and HTTPConnections in MIDLets

Choice 1 (Extending Thread)class FooThread extends Thread{

public void run(){for(int x=1; x<6; x++){

System.out.println("runnable running!");}

}}class Test{

public static void main(String [] args){FooThread foo = new FooThread();foo.start();

}}

Page 30: Graphics, Threads and HTTPConnections in MIDLets

Choice 2 (implement Runnable)class FooRunnable extends SomeOtherClass implements Runnable{

public void run(){for(int x=1; x<6; x++){

System.out.println("runnable running!");}

}}class Test{

public static void main(String [] args){FooRunnable foo = new FooRunnable();Thread t = new Thread(foo);t.start();

}}

Page 31: Graphics, Threads and HTTPConnections in MIDLets

Thread States

New Runnable Running Dead

Waiting/Blocking/Sleeping

Page 32: Graphics, Threads and HTTPConnections in MIDLets

Preventing Thread Execution: Sleep

• Sleep() method is a static method of class Thread

• Exampleclass NameRunnable implements Runnable{

public void run(){for(int i=0; i<5; i++){

System.out.println("thread");try{Thread.sleep(6*1000); // Sleep 6 seconds}catch(InterruptedException ex){}

}}

}

Page 33: Graphics, Threads and HTTPConnections in MIDLets

Easiest way to do threads in midlets

class MyMidlet extends Midlet implements Runnable {public MyMidlet() {

Thread x = new Thread(this);x.start();

}public void run() {

}}

Page 34: Graphics, Threads and HTTPConnections in MIDLets

Example: Midlets and threads

Page 35: Graphics, Threads and HTTPConnections in MIDLets

HTTP CONNECTION IN MIDP

Page 36: Graphics, Threads and HTTPConnections in MIDLets

Basic Architecture

Internet MIDPDevice

Web Server

User requests information from an Application (e.g. MyServlet)

Web server passes output from MyServlet back to the MIDlet

Web Server launches MyServlet program and sends it parameters the MIDlet requested

Web Server retrieves output fromthe MyServlet

Page 37: Graphics, Threads and HTTPConnections in MIDLets

HttpConnection

• Request-response application protocol• Parameters of request must be set before the request is sent• Connection states:

– Setup– Connected– Closed

• Setup state methods:– setRequestMethod (POST or GET)– setRequestProperty– With these methods you can cover the HTTP headers that are typically seen in

an HTTP request

Page 38: Graphics, Threads and HTTPConnections in MIDLets

What is needed ?

• MIDlet & MIDP Device• Servlet & Web Server• Connection between MIDP Device & Web

Server• Common Protocol between MIDlet and

Servlet

Page 39: Graphics, Threads and HTTPConnections in MIDLets

Using HTTP Connection

HttpConnection hc = (HttpConnection)Connector.open(URL);DataInputStream in = new DataInputStream(hc.openInputStream());int ch;String alltext = new String("");while ((ch = in.read()) != -1)

alltext += ((char) ch);hc.close();in.close();

Page 40: Graphics, Threads and HTTPConnections in MIDLets

Example: Midlet and HTTP-Connection