Fejlett Webes Technolgik II Bilicki Vilmos 2002 10

Fejlett Webes Technológiák II. Bilicki Vilmos 2002. 10. 14

A mai előadás tartalma: l MIDP (Mobile Information Device Profile) • Magas szintű eseménykezelés • Alacsony szintű eseménykezelés, grafika

Eseménykezelés l Felhasználói műveletek kezelése: • Absztrakt parancsok (magas szintű grafika • • • esetén) Alacsony szintű események (alacsony szintű grafika esetén) paint() metódus run() metódus

Absztrakt parancsok l l Parancsokat (Commands) definiálunk és ezeket absztrakt elemekhez rendeljük Displayable objektumhoz rendelhetünk ilyen parancsokat az add. Command() metódus segítségével A készülék stílusa meghatározhatja az ilyen absztrakt elemek helyét (pl. : vissza a bal oldalon) A Command objektum paraméterei: • • • Label (a felirat) Command. Type (a parancs jelentése, ha saját akkor SCREEN, egyébként megváltozhat) Priority (a parancs prioritása)

Command. Listener l l A magas szintű eseménykezelés a Listener modellen alapul. Ahhoz, hogy egy objektum Listener legyen meg kell valósítania a Command. Listener interfészt • l void command. Action(Command c, Displayable d); Akkor kapja meg az eseményeket, ha a Displayable objektumhoz: • • Vannak parancsok csatolva (add. Command()) Van Listener regisztrálva set. Command. Listener()

Példa alkalmazás Választó ablak (Choice) other. Screen() MIDlet (Rec. Set ) other. Screen() Bevitel Ablak (Screen) out. Put()

Osztályok

Rec. Set start. App() protected void start. App() throws MIDlet. State. Change. Exception { try { record. Store = Record. Store. open. Record. Store(RECORD_STORE_NAME, true); } catch (Record. Store. Exception rse) { } screen = new Screen(this, "Bevitel", "", 255, 0); choice. Sc = new Choice(this, "Bevitel", 255); Display. get. Display(this). set. Current(screen); }

Rec. Set other. Screen(String str) void other. Screen(String str) { if(Display. get. Display(this). get. Current() == choice. Sc) { if(str=="") { Display. get. Display(this). set. Current(screen); } else { Alert alert; Image source = null; try{ source = Image. create. Image("/kep 1. png"); } catch(Exception e){} alert = new Alert(str, "Ez egy Alert", source, null); Display. get. Display(this). set. Current(alert); } } else { Display. get. Display(this). set. Current(choice. Sc); }

Rec. Set out. Put(String str)/I void out. Put(String str) { String input; Alert alert; alert = new Alert("A beirt szoveg", str, null); Display. get. Display(this). set. Current(alert); Byte. Array. Output. Stream bout = null; Data. Output. Stream dout = null; try { bout = new Byte. Array. Output. Stream(); dout = new Data. Output. Stream(bout); dout. write. UTF(str); byte[] record = bout. to. Byte. Array(); if (record. Store. get. Num. Records() > 0) { record. Store. set. Record(1, record, 0, record. length); } else { record. Store. add. Record(record, 0, record. length); } } catch (Record. Store. Exception rse) {} catch (IOException ioe) {}

Rec. Set out. Put(String str)/II finally { if (dout != null) { try { dout. close(); catch (IOException ioe) { } if (bout != null) { try { bout. close(); catch (IOException ioe) { } } }

Rec. Set out. Put(String str)/III Data. Input. Stream din = null; try { if (record. Store. get. Num. Records() > 0) { byte[] record = record. Store. get. Record(1); din = new Data. Input. Stream(new Byte. Array. Input. Stream(record)); input = din. read. UTF(); alert = new Alert("A beolvasott", input, null); Display. get. Display(this). set. Current(alert); } else { } }

Rec. Set out. Put(String str)/IV catch (Record. Store. Exception rse) { } catch (IOException ioe) { } finally { if (din != null) { try { din. close(); } catch (IOException ioe) { } } }

Screen/I public class Screen extends Text. Box implements Command. Listener { private Rec. Set midlet; private Command exit. Command; private Command other. Screen; private Command ok. Command; public Screen(Rec. Set midlet, String p 0, String p 1, int p 2, int p 3) { super( p 0, p 1, p 2, p 3); this. midlet = midlet; exit. Command = new Command("Kilep", Command. EXIT, 1); other. Screen = new Command("Masik", Command. SCREEN, 1); ok. Command = new Command("Kiir", Command. OK, 1); add. Command(exit. Command); add. Command(other. Screen); add. Command(ok. Command); set. Command. Listener(this); }

Screen/II public void command. Action(Command c, Displayable p 1) { if (c == ok. Command) { midlet. out. Put(this. get. String()); } if (c == exit. Command) { midlet. exit. Requested(); } if (c == other. Screen) { midlet. other. Screen(""); } }}

Choice/I public class Choice extends List implements Command. Listener { private Rec. Set midlet; private Command other. Screen; private Command command. OK; public Choice(Rec. Set midlet, String p 0, int p 1) { super( "Lista", List. IMPLICIT); this. midlet = midlet; append("Elso", load. Image("/kep. png")); append("Masodik", load. Image("/kep 1. png")); command. OK = new Command("OK", Command. SCREEN, 1); other. Screen = new Command("Masik", Command. SCREEN, 1); add. Command(command. OK); add. Command(other. Screen); set. Command. Listener(this); }

Choice/II public void command. Action(Command c, Displayable p 1) { if (c == other. Screen) { midlet. other. Screen(""); } if (c == command. OK) { midlet. other. Screen(get. String(get. Selected. Index())); } }

Choice III. private Image load. Image(String s) { Image image = null; try{ image = Image. create. Image(s); } catch(Exception e) { } return image; }

Alacsony szintű események, grafika l l l A következő billentyűzet eseményeket kezeli: • • • public void key. Pressed(int key. Code); public void key. Released(int key. Code); public void key. Repeated(int key. Code); Game. Action: • • public void key. Pressed(int key. Code) int action = get. Game. Action(key. Code); A key. Released nincs feltétlenül megvalósítva

Példa class Tetris. Canvas extends Canvas { int left. Key, right. Key, down. Key, rotate. Key; void init () { left. Key = get. Key. Code(LEFT); right. Key = get. Key. Code(RIGHT); down. Key = get. Key. Code(DOWN); rotate. Key = get. Key. Code(FIRE); } public void key. Pressed(int key. Code) { if (key. Code == left. Key) { move. Block. Left(); } else if (key. Code = right. Key) {. . . }}}

Példa Game. Action public void key. Pressed(int key. Code) { int action = get. Game. Action(key. Code); if (action == LEFT) { move. Block. Left(); } else if (action == RIGHT) {. . . } }

Rajzolás l Canvas osztályból kell származtatni • protected abstract void paint(Graphics g) • Nincs automaikus újrarajzolás: • repaint(x 2, y 2, wid, ht) • service. Repaints(); • Egyetlen rajzolási mód a pixel felülírás • 24 bit színmélység (8 piros, 8 kék, 8 zöld) • • • g. set. Color(WHITE); g. fill. Rect(0, 0, 95, 50); g. set. Color(BLACK);

Koordináta rendszer l A képernyő bal felső sarkában 0, 0 • public static final int get. Width(); • public static final int get. Height();

Példa Grafikus megjelenítő (Screen) MIDlet (Graphics. Sample )

Graphics. Sample public class Graphics. Sample extends MIDlet { Screen scr; public Graphics. Sample() { } protected void start. App() throws MIDlet. State. Change. Exception { scr = new Screen(this); Display. get. Display(this). set. Current(scr); } protected void pause. App() { } protected void destroy. App(boolean p 0) throws MIDlet. State. Change. Exception { }}

Screen/I import javax. microedition. lcdui. *; import javax. microedition. midlet. *; public class Screen extends Canvas { Graphics. Sample midlet; private static final int BLACK = 0 x 0000; private static final int WHITE = 0 x 00 FFFFFF; private static final int RED = 0 x 00 FF 0000; private static final int BLUE = 0 x 000000 FF; int px, py; public Screen(Graphics. Sample midlet) { this. midlet = midlet; px = 25; py = 25; }

Screen/II protected void paint(Graphics g) { g. set. Color(WHITE); g. fill. Rect(0, 0, g. get. Clip. Width(), g. get. Clip. Height ()); g. set. Color(BLACK); g. draw. Arc(px, py, 20, 0, 360); }

Screen/III protected void key. Pressed(int key. Code) { int game. Action = get. Game. Action(key. Code); switch (game. Action) { case FIRE: break; case RIGHT: px++; repaint(); break; case DOWN: py++; repaint(); break; case LEFT: px--; repaint(); break; case UP: py--; repaint(); break; default: break; } }

Rajzolás l l l A Graphics osztály metódusai segítségével rajzolhatunk: • • • Szöveget Képeket Vonalakat Négyszögeket (telítettek is lehetnek) Szögeket (telítettek is lehetnek) • • Folyamatos (solid) Szaggatott (dotted) A vonal stílusa lehet: public void set. Stroke. Style(int style)

Szöveg kiíratása l l l public void draw. Char(character, int x, int y, int anchor) public void draw. Chars(char[] data, int offset, int length, int x, int y, int anchor) public void draw. String(String str, int x, int y, int anchor) public void draw. Substring(String str, int offset, int len, int x, int y, int anchor) Szöveg irány: • • LEFT, HCENTER, RIGHT TOP, BASELINE, BOTTOM

Példa g. draw. String(str, x, y, TOP|LEFT); g. draw. String(str, x + f. string. Width(str)/2, y, TOP|HCENTER); g. draw. String(str, x + f. string. Width(str), y, TOP|RIGHT); g. draw. String(str, x, y + f. get. Baseline. Position(), BASELINE|LEFT); g. draw. String(str, x + f. string. Width(str)/2, y + f. get. Baseline. Position(), BASELINE|HCENTER); g. draw. String(str, x + f. string. Width(str), y + f. get. Baseline. Position(), BASELINE|RIGHT); draw. String(str, x, y + f. get. Height(), BOTTOM|LEFT); draw. String(str, x + f. string. Width(str)/2, y + f. get. Height(), BOTTOM|HCENTER); draw. String(str, x + f. string. Width(str), y + f. get. Height(), BOTTOM|RIGHT);

Graphics metódusok l l l l public void draw. Line(int x 1, int y 1, int x 2, int y 2) public void draw. Rect(int x, int y, int width, int height) public void fill. Rect(int x, int y, int width, int height) public void draw. Round. Rect(int x, int y, int width, int height, int arc. Width, int arc. Height) public void fill. Round. Rect(int x, int y, int width, int height, int arc. Width, int arc. Height) public void draw. Arc(int x, int y, int width, int height, int start. Angle, int arc. Angle) public void fill. Arc(int x, int y, int width, int height, int start. Angle, int arc. Angle) public void draw. Image(Image img, int x, int y, int anchor)
- Slides: 32