Minule bolo Applety java awt button text label

Minule bolo… Applety • java. awt. *; • button, text, label, combo-box, …. • events, • layouts, …

Applety Obmedzenia • applet nemôže pristupovať na lokálny disk • applet nemá menu • dialog box je mätúci Pamätajme • applet sa naťahuje vždy znova do browsera • kvôli bezpečnosti, obmedzený prístup k lokálnym zdrojom • applet a aplikácia majú odlišné riadenie Výhody • nepotrebuje inštaláciu … len runtime, pluggin, etc. • applet vám nezničí systém • idea nezávislej platformy

Spracovanie udalosti • action (Event evt, Object what) stlačenie buttonu, check-box, vybratie položky z combo-boxu, . . . • key. Down (Event evt, int key) stlačenie klávesu, key predstavuje kód klávesu z evt. key • key. Up(Event evt, int key) uvoľnenie klávesu • lost. Focus(Event evt, Object what) strata fokusu objeku • got. Focus(Event evt, Object what) získanie fokusu • mouse. Down(Event evt, int x, int y) stlačenie myši na súradniciach x, y • mouse. Up(Event evt, int x, int y) pustenie myši na súradniciach x, y • mouse. Move(Event evt, int x, int y) presun myši • mouse. Drag(Event evt, int x, int y) public boolean handle. Event(Event evt) dragovanie myši, od mouse. Down po mouse. Up • mouse. Enter(Event evt, int x, int y) myš vliezla do komponentu • mouse. Exit(Event evt, int x, int y) myš ušla z komponentu

import java. awt. *; import java. applet. *; import java. util. *; Spracovanie udalostí public class Auto. Event extends Applet { Hashtable h = new Hashtable(); String[] event = { "key. Down", "key. Up", "lost. Focus", "got. Focus", "mouse. Down", "mouse. Up", "mouse. Move", "mouse. Drag", "mouse. Enter", "mouse. Exit“ }; My. Button b 1 = new My. Button(this, Color. blue, "test 1"), b 2 = new My. Button(this, Color. red, "test 2"); public void init() { set. Layout(new Grid. Layout(event. length+1, 2)); for(int i = 0; i < event. length; i++) { add(new Label(event[i], Label. CENTER)); Text. Field t = new Text. Field(); t. set. Editable(false); add(t); h. put(event[i], t); } add(b 1); add(b 2); } }

import java. awt. *; import java. applet. *; import java. util. *; My. Button class My. Button extends Canvas { Auto. Event parent; Color color; String label; public void paint(Graphics g) { g. set. Color(color); int rnd = 30; g. fill. Round. Rect(0, 0, size(). width, size(). height, rnd); g. set. Color(Color. black); g. draw. Round. Rect(0, 0, size(). width, size(). height, rnd); My. Button(Auto. Event parent, Color color, String label) { this. label = label; this. parent = parent; this. color = color; } public boolean key. Down(Event evt, int key) { Text. Field t = (Text. Field)parent. h. get("key. Down"); t. set. Text(evt. to. String()); return true; } public boolean key. Up(Event evt, int key) { Text. Field t = (Text. Field)parent. h. get("key. Up"); t. set. Text(evt. to. String()); return true; }. . . Font. Metrics fm = g. get. Font. Metrics(); int width = fm. string. Width(label); int height = fm. get. Height(); int ascent = fm. get. Ascent(); int leading = fm. get. Leading(); int horiz. Margin = (size(). width - width)/2; int ver. Margin = (size(). height - height)/2; g. set. Color(Color. white); g. draw. String(label, horiz. Margin, ver. Margin + ascent + leading); }

import java. awt. *; Oknové aplikácie public class Toe. Test extends Frame { Text. Field rows = new Text. Field("3"); Text. Field cols = new Text. Field("3"); public boolean handle. Event(Event evt) { if(evt. id == Event. WINDOW_DESTROY) System. exit(0); else return super. handle. Event(evt); return true; } public Toe. Test() { set. Title("Toe Test"); Panel p = new Panel(); p. set. Layout(new Grid. Layout(2, 2)); p. add(new Label("Rows", Label. CENTER)); public boolean action(Event evt, Object arg){ p. add(rows); if (arg. equals("go")) { p. add(new Label("Columns", Label. CENTER)); Dialog d = new Toe. Dialog(this, p. add(cols); Integer. parse. Int(rows. get. Text()), add("North", p); Integer. parse. Int(cols. get. Text())); add("South", new Button("go")); d. show(); } } else public static void main(String[] args) { return super. action(evt, arg); Frame f = new Toe. Test(); return true; f. resize(200, 100); } f. show(); } }

Dialog class Toe. Dialog extends Dialog { public static final int BLANK = 0; public static final int XX = 1; public static final int OO = 2; public int turn = XX; public Toe. Dialog(Frame parent, int w, int h) { super(parent, "The game itself", false); set. Layout(new Grid. Layout(w, h)); for(int i = 0; i < w * h; i++) add(new Toe. Button(this)); resize(w * 50, h * 50); } public boolean handle. Event(Event evt) { if(evt. id == Event. WINDOW_DESTROY) dispose(); else return super. handle. Event(evt); return true; } }

Canvas class Toe. Button extends Canvas { int state = Toe. Dialog. BLANK; Toe. Dialog parent; Toe. Button(Toe. Dialog parent) { this. parent = parent; public boolean mouse. Down(Event evt, int x, int y) { } if (state == Toe. Dialog. BLANK) { public void paint(Graphics g) { state = parent. turn; int x 2 = size(). width - 1; parent. turn= (parent. turn == Toe. Dialog. XX ? int y 2 = size(). height - 1; Toe. Dialog. OO : Toe. Dialog. XX); g. draw. Rect(0, 0, x 2, y 2); } else int x 1 = x 2/4; int y 1 = y 2/4; state = (state == Toe. Dialog. XX ? int wide = x 2/2; int high = y 2/2; Toe. Dialog. OO : Toe. Dialog. XX); if(state == Toe. Dialog. XX) { repaint(); g. draw. Line(x 1, y 1, x 1 + wide, y 1 + high); return true; g. draw. Line(x 1, y 1 + high, x 1 + wide, y 1); } } } if(state == Toe. Dialog. OO) g. draw. Oval(x 1, y 1, x 1+wide/2, y 1+high/2); }

import java. awt. *; import java. lang. *; import java. applet. *; Menu public class Frame. Menu. Crusro. Applet extends Applet { public void init() { new Frame. Menu. Cursor("Menu Based Cursors"); } } class Frame. Menu. Cursor extends Frame { public Frame. Menu. Cursor(String title) { super(title); Menu. Bar mbar = new Menu. Bar(); set. Menu. Bar(mbar); Menu m = new Menu("File"); mbar. add(m); m. add(new Menu. Item("Quit")); m = new Menu("Cursor"); mbar. add(m); m. add(new Menu. Item("Default")); m. add(new Menu. Item("Wait")); m. add(new Menu. Item("Hand")); m. add(new Menu. Item("Move")); . . . resize(300, 200); show(); } public boolean handle. Event(Event e) { if (e. id == e. WINDOW_DESTROY) { dispose(); // Erase frame return true; } else if (e. id == e. ACTION_EVENT) { if (e. target instanceof Menu. Item) { String menu. Name = e. arg. to. String(); if (menu. Name. equals("Quit")) dispose(); if (menu. Name. equals("Default")) set. Cursor(Frame. DEFAULT_CURSOR); if (menu. Name. equals("Wait")) set. Cursor(Frame. WAIT_CURSOR); if (menu. Name. equals("Hand")) set. Cursor(Frame. HAND_CURSOR); if (menu. Name. equals("Move")) set. Cursor(Frame. MOVE_CURSOR); . . . . return true; } else { return false; } } }

import java. awt. *; import java. awt. event. *; import java. applet. *; Listener model public class Button 2 extends Applet { Button b 1 = new Button("Button 1"), b 2 = new Button("Button 2"); public void init() { b 1. add. Action. Listener(new B 1()); b 2. add. Action. Listener(new B 2()); add(b 1); add(b 2); } class B 1 implements Action. Listener { public void action. Performed(Action. Event e) { get. Applet. Context(). show. Status("Button 1"); } } class B 2 implements Action. Listener { public void action. Performed(Action. Event e) { get. Applet. Context(). show. Status("Button 2"); } } }

Udalosti (eventy) Komponent Udalosť Adjustable Applet Button Canvas Checkbox. Menu. Item Choice Component Container Dialog File. Dialog Frame Label List Menu. Item Panel Popup. Menu Scrollbar Scroll. Pane Text. Area Text. Component Text. Field Window Adjustment. Event Container. Event, Focus. Event, Key. Event, Mouse. Event, Component. Event Action. Event, Focus. Event, Key. Event, Mouse. Event, Component. Event Item. Event, Focus. Event, Key. Event, Mouse. Event, Component. Event Action. Event, Item. Event, Focus. Event, Key. Event, Mouse. Event, Component. Event Container. Event, Window. Event, Focus. Event, Key. Event, Mouse. Event, Component. Event Action. Event, Focus. Event, Key. Event, Mouse. Event, Item. Event, Component. Event Action. Event Container. Event, Focus. Event, Key. Event, Mouse. Event, Component. Event Action. Event Adjustment. Event, Focus. Event, Key. Event, Mouse. Event, Component. Event Container. Event, Focus. Event, Key. Event, Mouse. Event, Component. Event Text. Event, Focus. Event, Key. Event, Mouse. Event, Component. Event Action. Event, Text. Event, Focus. Event, Key. Event, Mouse. Event, Component. Event Container. Event, Window. Event, Focus. Event, Key. Event, Mouse. Event, Component. Event

Listeners

import java. awt. *; import java. awt. event. *; import java. applet. *; Check box class ILCheck implements Item. Listener { public void item. State. Changed(Item. Event e) { public class Radio. Check. New extends Applet { for(int i = 0; i < cb. length; i++) { Text. Field t = new Text. Field(30); if(e. get. Source(). equals(cb[i])) { Checkbox[] cb = { t. set. Text("Check box " + (i + 1)); new Checkbox("Check Box 1"), return; new Checkbox("Check Box 2"), } new Checkbox("Check Box 3") }; } Checkbox. Group g = new Checkbox. Group(); } Checkbox } cb 4 = new Checkbox("four", g, false), class IL 4 implements Item. Listener { cb 5 = new Checkbox("five", g, true), public void item. State. Changed(Item. Event e) { cb 6 = new Checkbox("six", g, false); t. set. Text("Radio button four"); } public void init() { } t. set. Editable(false); class IL 5 implements Item. Listener { add(t); public void item. State. Changed(Item. Event e) { ILCheck il = new ILCheck(); t. set. Text("Radio button five"); for(int i = 0; i < cb. length; i++) { } cb[i]. add. Item. Listener(il); } add(cb[i]); } cb 4. add. Item. Listener(new IL 4()); cb 5. add. Item. Listener(new IL 5()); cb 6. add. Item. Listener(new IL 6()); add(cb 4); add(cb 5); add(cb 6); }

Myš public class Mouse extends Applet implements Mouse. Motion. Listener { int width, height; Vector list. Of. Positions; public void init() { width = get. Size(). width; height = get. Size(). height; set. Background( Color. black ); list. Of. Positions = new Vector(); add. Mouse. Motion. Listener( this ); } public void mouse. Moved( Mouse. Event e ) { if ( list. Of. Positions. size() >= 50 ) list. Of. Positions. remove. Element. At( 0 ); list. Of. Positions. add. Element( new Point( e. get. X(), e. get. Y() ) ); repaint(); } public void mouse. Dragged( Mouse. Event e ) { } public void paint( Graphics g ) { g. set. Color( Color. white ); for ( int j = 1; j < list. Of. Positions. size(); ++j ) { Point A = Point)(list. Of. Positions. element. At(j-1)); Point B = (Point)(list. Of. Positions. element. At(j)); g. draw. Line( A. x, A. y, B. x, B. y ); } } }

import java. awt. *; import java. util. *; java. awt. event. *; public class Good. Idea extends Frame { Button b 1 = new Button("Button 1"), b 2 = new Button("Button 2"); Štýl public Good. Idea() { set. Layout(new Flow. Layout()); b 1. add. Action. Listener(new B 1 L()); b 2. add. Action. Listener(new B 2 L()); add(b 1); add(b 2); } public class B 1 L implements Action. Listener { public void action. Performed(Action. Event e) { System. out. println("Button 1 pressed"); } } public class B 2 L implements Action. Listener { public void action. Performed(Action. Event e) { System. out. println("Button 2 pressed"); } } public static void main(String[] args) { Frame f = new Good. Idea(); f. add. Window. Listener( new Window. Adapter() { public void window. Closing(Window. Event e){ System. out. println("Window Closing"); System. exit(0); } }); f. set. Size(300, 200); f. set. Visible(true); } }

import java. awt. *; import java. awt. event. *; import java. util. *; ne-Štýl public class Bad. Idea 1 extends Frame implements Action. Listener, Window. Listener { Button b 1 = new Button("Button 1"), b 2 = new Button("Button 2"); public void window. Closing(Window. Event e) { public Bad. Idea 1() { System. out. println("Window Closing"); set. Layout(new Flow. Layout()); System. exit(0); add. Window. Listener(this); } b 1. add. Action. Listener(this); public void window. Closed(Window. Event e) {} b 2. add. Action. Listener(this); public void window. Deiconified(Window. Event e) {} add(b 1); add(b 2); public void window. Iconified(Window. Event e) {} } public void window. Activated(Window. Event e) {} public void action. Performed(Action. Event e) { public void window. Deactivated(Window. Event e) {} public void window. Opened(Window. Event e) {} Object source = e. get. Source(); if(source == b 1) public static void main(String[] args) { System. out. println("Button 1 pressed"); Frame f = new Bad. Idea 1(); else if(source == b 2) f. set. Size(300, 200); System. out. println("Button 2 pressed"); f. set. Visible(true); else } } System. out. println("Something else"); }

import java. awt. *; import java. awt. event. *; import java. util. *; Štýl-mix public class Bad. Idea 2 extends Frame implements Action. Listener { Button b 1 = new Button("Button 1"), b 2 = new Button("Button 2"); class WL extends Window. Adapter { public Bad. Idea 2() { public void window. Closing(Window. Event e) { set. Layout(new Flow. Layout()); System. out. println("Window Closing"); add. Window. Listener(new WL()); System. exit(0); b 1. add. Action. Listener(this); b 2. add. Action. Listener(this); } add(b 1); } add(b 2); public static void main(String[] args) { } Frame f = new Bad. Idea 2(); public void action. Performed(Action. Event e) { f. set. Size(300, 200); Object source = e. get. Source(); if(source == b 1) f. set. Visible(true); System. out. println("Button 1 pressed"); } else if(source == b 2) } System. out. println("Button 2 pressed"); else System. out. println("Something else"); }

Oddelenie BL a GUI import java. awt. *; import java. awt. event. *; import java. applet. *; class Business. Logic { private int modifier; Business. Logic(int mod) { modifier = mod; } public void set. Modifier(int mod) { modifier = mod; } public int get. Modifier() { return modifier; } public int calculation 1(int arg) { return arg * modifier; } public int calculation 2(int arg) { return arg + modifier; } } public class Separation extends Applet { Text. Field t = new Text. Field(20), mod = new Text. Field(20); Business. Logic bl = new Business. Logic(3); Button calc 1 = new Button("Calculation 1"), calc 2 = new Button("Calculation 2"); public void init() { add(t); calc 1. add. Action. Listener(new Calc 1 L()); calc 2. add. Action. Listener(new Calc 2 L()); add(calc 1); add(calc 2); mod. add. Text. Listener(new Mod. L()); add(new Label("Modifier: ")); add(mod); } static int get. Value(Text. Field tf) { … } class Calc 1 L implements Action. Listener { public void action. Performed(Action. Event e) { t. set. Text(Integer. to. String( bl. calculation 1(get. Value(t)))); } } class Calc 2 L implements Action. Listener {…} class Mod. L implements Text. Listener { public void text. Value. Changed(Text. Event e) { bl. set. Modifier(get. Value(mod)); } }

import java. awt. *; import java. awt. event. *; import java. util. *; Dynamic Events public class Dynamic. Events extends Frame { Vector v = new Vector(); int i = 0; Button b 1 = new Button("Button 1"), b 2 = new Button("Button 2"); public Dynamic. Events() { set. Layout(new Flow. Layout()); b 1. add. Action. Listener(new B 1()); b 2. add. Action. Listener(new B 2()); add(b 1); add(b 2); } class B implements Action. Listener { public void action. Performed(Action. Event e) { System. out. println("A button was pressed"); } } class Count. Listener implements Action. Listener { int index; public Count. Listener(int i) { index = i; } public void action. Performed(Action. Event e) { System. out. println( "Counted Listener " + index); } } class B 1 implements Action. Listener { public void action. Performed(Action. Event e) { System. out. println("Button 1 pressed"); Action. Listener a = new Count. Listener(i++); v. add. Element(a); b 2. add. Action. Listener(a); } } class B 2 implements Action. Listener { public void action. Performed(Action. Event e) { System. out. println("Button 2 pressed"); int end = v. size() -1; if(end >= 0) { b 2. remove. Action. Listener( (Action. Listener)v. element. At(end)); v. remove. Element. At(end); } } } ….

Od appletu k aplikácii . . static class WL extends Window. Adapter { public void window. Closing(Window. Event e) { System. exit(0); } } // A main() for the application: public static void main(String[] args) { Button 2 New. B applet = new Button 2 New. B(); Frame a. Frame = new Frame("Button 2 New. B"); a. Frame. add. Window. Listener(new WL()); a. Frame. add(applet, Border. Layout. CENTER); a. Frame. set. Size(300, 200); applet. init(); applet. start(); a. Frame. set. Visible(true); } }

Pakovanie appletu do JARu jar cf Button 2 New. B. jar *. class <head> <title> Button 2 New. B Example Applet </title> </head> <body> <applet code="Button 2 New. B. class" archive="Button 2 New. B. jar" width=200 height=150> </applet> </body>

case study private void paint. Face(Graphics g) { g. draw. Image(sadness==sad? sad. Smiley: sadness==happy? happy. Smiley: bored. Smiley, (width*edge-face. Size)/2, (score. Height-face. Size)/2, this); } private Image init. One. Smiley(int the. Sadness) { Image off = create. Image(face. Size, face. Size); Graphics g = off. get. Graphics(); g. set. Color(Color. black); g. fill. Rect(0, 0, face. Size); g. set. Color(base. Color); g. fill 3 DRect(1, 1, face. Size-2, true); g. fill 3 DRect(2, 2, face. Size-4, true); g. set. Color(Color. yellow); g. fill. Oval(6, 6, face. Size-12); g. set. Color(Color. black); g. draw. Oval(6, 6, face. Size-12); if (the. Sadness==sad) { g. draw. Arc(10, face. Size-13, face. Size-20, 135, -100); } else if (the. Sadness==happy) { g. draw. Arc(10, face. Size-20, -35, -100); } else { g. fill. Rect(12, face. Size-23, 1); } g. fill. Oval(13, 2, 2); g. fill. Oval(face. Size-12 -2, 13, 2, 2); return off; }

Minesweeper private Image init. One. Offscreen(int i) { Image off = create. Image(edge, edge); Graphics g = off. get. Graphics(); g. set. Color(i==exploded ? dangerous. Color : base. Color); if (i > unexposed) { g. fill. Rect(0, 0, edge); g. set. Color(base. Shadow); } else { g. fill 3 DRect(0, 0, edge-1, true); g. set. Color(Color. black); }; g. draw. Line(edge-1, 0, edge-1); g. draw. Line(0, edge-1, edge-1); int half. Width = edge/2; int quarter. Pos = (edge-1)/4; if (i==unexposed || i==0 ) { } else if (i==mine || i==exploded) { /* A circle with four lines through it, and a highlight */ g. set. Color(mine. Color); g. draw. Line(2, 2, edge-4); g. draw. Line(edge-4, 2, 2, edge-4); g. draw. Line(half. Width-1, 1, half. Width-1, edge-3); g. draw. Line(1, half. Width-1, edge-3, half. Width-1); g. fill. Oval(quarter. Pos, half. Width+1, half. Width+1); g. set. Color(Color. white); g. fill. Oval(half. Width-3, edge/8, edge/8); } else if (i==incorrect) { /* A diagonal cross, 3 pixels wide */ g. set. Color(dangerous. Color); g. draw. Line(2, 2, edge-4); g. draw. Line(2, 3, edge-5, edge-4); g. draw. Line(3, 2, edge-4, edge-5); g. draw. Line(edge-4, 2, 2, edge-4); g. draw. Line(edge-4, 3, 3, edge-4); g. draw. Line(edge-5, 2, 2, edge-5); } else if (i==flagged) { /* A flag on a pole with a base */ g. set. Color(dangerous. Color); g. fill. Rect(half. Width-4, half. Width-5, half. Width-4); g. set. Color(mine. Color); g. draw. Line(half. Width, 3, half. Width, edge-4); g. draw. Line(5, edge-4, edge-5, edge-4); } else { /* A question mark or the adjacency count */ Font. Metrics fm = this. get. Font. Metrics(the. Main. Font); int font. Ascent = fm. get. Ascent(); String s = i==queried ? "? " : ""+i; g. set. Color(i==queried ? new Color(0, 0, 255) : colors[i]); g. set. Font(the. Main. Font); g. draw. String(s, (edge-fm. string. Width(s))/2, font. Ascent); }; return off; }

Be carefull

java/doc/demo/applet public void init() { not. Image = get. Image(get. Code. Base(), "images/o. gif"); cross. Image = get. Image(get. Code. Base(), "images/x. gif"); add. Mouse. Listener(this); }

java/doc/demo/applet
- Slides: 26