AWT Components Using AWT Components 4 Component Canvas

  • Slides: 28
Download presentation
AWT Components

AWT Components

Using AWT Components 4 Component – Canvas – Scrollbar – Button – Checkbox –

Using AWT Components 4 Component – Canvas – Scrollbar – Button – Checkbox – Label – List – Choice – Text. Component – Text. Area – Text. Field 4 Component – Container • Panel • Window – Dialog » File. Dialog – Frame 4 Menu. Component – Menu. Item • Menu 2

Frame import java. awt. *; public class Test. Frame extends Frame { public Test.

Frame import java. awt. *; public class Test. Frame extends Frame { public Test. Frame(String title){ super(title); } public static void main(String[] args){ Frame f = new Test. Frame("Test. Frame"); f. set. Size(400, 400); f. set. Location(100, 100); f. show(); } }

How to Use Buttons? public class Test. Button extends Frame { public Test. Button(String

How to Use Buttons? public class Test. Button extends Frame { public Test. Button(String title){ super(title); Button hw = new Button("Hello World!"); add(hw); } …. } 5

How to Use Labels? public class Test. Label extends Frame { public Test. Label(String

How to Use Labels? public class Test. Label extends Frame { public Test. Label(String title){ super(title); Label label 1 = new Label(); label 1. set. Text("Label 1"); Label label 2 = new Label("Label 2"); label 2. set. Alignment(Label. CENTER); Label label 3 = new Label("Label 3"); add(label 1, "North"); add(label 2, "Center"); add(label 3, "South"); } }

How to Use Checkboxes? public class Test. Checkbox extends Frame { public Test. Checkbox(String

How to Use Checkboxes? public class Test. Checkbox extends Frame { public Test. Checkbox(String title){ super(title); Checkbox. Group cbg = new Checkbox. Group(); Checkbox cb 1 = new Checkbox("American Express", cbg, false); Checkbox cb 2 = new Checkbox("Visa", cbg, false); Checkbox cb 3 = new Checkbox("Mastercard", cbg, true); add(cb 1, "North"); add(cb 2, "Center"); add(cb 3, "South"); } … } 7

How to Use Choices? public class Test. Choice extends Frame { public Test. Choice(String

How to Use Choices? public class Test. Choice extends Frame { public Test. Choice(String title){ super(title); Choice choice = new Choice(); choice. add("ichi"); choice. add("ni"); choice. add("san"); add(choice); } 8

How to Use Text. Field & Text. Area public class Test. Text extends Frame

How to Use Text. Field & Text. Area public class Test. Text extends Frame { public Test. Text(String title){ super(title); Text. Field text. Field = new Text. Field(20); Text. Area text. Area = new Text. Area(5, 20); text. Area. set. Editable(false); text. Field. set. Text("Text. Field"); text. Area. set. Text("Text. Area Line 1 n Text. Area Line 2"); add(text. Field, "North"); add(text. Area, "South"); } … }

How to Use Lists? public class Test. List extends Frame { public Test. List(String

How to Use Lists? public class Test. List extends Frame { public Test. List(String title){ super(title); List l = new List(2, true); //prefer 2 items visible l. add("zero"); l. add("uno"); l. add("dos"); l. add("tres"); l. add("cuatro"); l. add("cinco"); l. add("seis"); l. add("siete"); l. add("ocho"); l. add("nueve"); add(l); } 10

How to Use Menus? public class Test. Menu extends Frame { public Test. Menu(String

How to Use Menus? public class Test. Menu extends Frame { public Test. Menu(String title){ super(title); Menu. Bar mb = new Menu. Bar(); set. Menu. Bar(mb); Menu m 1 = new Menu("Menu 1"); mb. add(m 1); Menu. Item mi 1_1 = new Menu. Item("Menu Item 1_1"); m 1. add(mi 1_1); m 1. add. Separator(); Menu. Item mi 1_2 = new Menu. Item("Menu Item 1_2"); m 1. add(mi 1_2); Menu m 2 = new Menu("Menu 2"); // mb. add(m 2); m 1. add(m 2); Menu. Item mi 2_1 = new Checkbox. Menu. Item("Menu Item 2_1"); m 2. add(mi 2_1); } 11 …

How to Use Canvases and Graphics Primitives? 4 For drawing geometric shapes, texts, and

How to Use Canvases and Graphics Primitives? 4 For drawing geometric shapes, texts, and images 4 An abstract class – the extended class must override paint() Line Round. Rectangle Polygon Oval Rectangle Arc 12

draw. Line(x 1, y 1, x 2, y 2) class My. Canvas extends Canvas

draw. Line(x 1, y 1, x 2, y 2) class My. Canvas extends Canvas { public void paint(Graphics g){ g. set. Color(Color. blue); int x 1 = 161, (x 1, y 1) y 1 = 186, x 2 = 181, (x 2, y 2) y 2 = 206; g. draw. Line(x 1, y 1, x 2, y 2); } 13

fill. Oval(x, y, w, h) draw. Oval(x, y, w, h) g. set. Color(Color. blue);

fill. Oval(x, y, w, h) draw. Oval(x, y, w, h) g. set. Color(Color. blue); { int x = 239, y = 186, w = 48, h = 32; g. fill. Oval(x, y, w, h); } 14

fill. Polygon(int[] xs, int[] ys) draw. Polygon(int[] xs, int[] ys) g. set. Color(Color. green);

fill. Polygon(int[] xs, int[] ys) draw. Polygon(int[] xs, int[] ys) g. set. Color(Color. green); { int xs[] = {161, 185, 209, 185, 161}; int ys[] = {310, 334, 358, 334, 310}; g. fill. Polygon(xs, ys, 6); }

fill. Rect(x, y, w, h) draw. Rect(x, y, w, h) g. set. Color(Color. pink);

fill. Rect(x, y, w, h) draw. Rect(x, y, w, h) g. set. Color(Color. pink); { int x = 239, y = 248, w = 48, h = 32; g. fill. Rect(x, y, w, h); }

fill. Round. Rect(x, y, w, h, rw, rh) draw. Round. Rect(x, y, w, h,

fill. Round. Rect(x, y, w, h, rw, rh) draw. Round. Rect(x, y, w, h, rw, rh) g. set. Color(Color. yellow); { int x = 161, y = 248, w = 48, h = 32, round. W = 25, round. H = 25; g. fill. Round. Rect(x, y, w, h, round. W, round. H); }

fill. Arc(x, y, w, h, sa, a) draw. Arc(x, y, w, h, sa, a)

fill. Arc(x, y, w, h, sa, a) draw. Arc(x, y, w, h, sa, a) g. set. Color(Color. orange); { int x = 239, y = 310, w = 48, h = 48, start. Angle = 20, angle = 90; g. fill. Arc(x, y, w, h, start. Angle, angle); }

draw. String(s, x, y) Font. Metrics (x, y)

draw. String(s, x, y) Font. Metrics (x, y)

draw. String, Font, & Font. Metrics class My. Canvas extends Canvas { public void

draw. String, Font, & Font. Metrics class My. Canvas extends Canvas { public void paint(Graphics g){ g. set. Font(new Font("Dialog", 0, 20)); Font. Metrics fm = g. get. Font. Metrics(); int x = 100; int y = 100; g. draw. String("Hello", x, y); y = y+fm. get. Height(); g. draw. String("World", x, y); } }

draw. Image(image, x, y, w, h, ob) Image im = Toolkit. get. Default. Toolkit().

draw. Image(image, x, y, w, h, ob) Image im = Toolkit. get. Default. Toolkit(). get. Image(name); g. draw. Image(im, x, y, w, h, observer);

draw. Image public class Test. Image extends Frame { Image im; public Test. Image(String

draw. Image public class Test. Image extends Frame { Image im; public Test. Image(String title){ super(title); im = Toolkit. get. Default. Toolkit(). get. Image("jp 2. jpg"); if (im==null){ System. out. println("No image"); System. exit(0); } } public void paint(Graphics g){ g. draw. Image(im, 0, 0, im. get. Height(this), im. get. Width(this), this); } … }

Layout of Components 4 Border. Layout – north, south, west, east & center 4

Layout of Components 4 Border. Layout – north, south, west, east & center 4 Grid. Layout – tabular form (rows & columns) 4 Flow. Layout 4 Grid. Bag. Layout – left to right & top down – tabular form(variable 4 Card. Layout – stack of panels row heights and column widths)

Use Layout Managers set. Layout(new Border. Layout()); set. Layout(new Card. Layout(()); set. Layout(new Flow.

Use Layout Managers set. Layout(new Border. Layout()); set. Layout(new Card. Layout(()); set. Layout(new Flow. Layout()); set. Layout(new Grid. Layout(rows, columns, xgap, ygap)); 4 Default layout managers – Windows (Frames & Dialogs) • Border. Layout – Panels (Applets) • Flow. Layout

How to Use Border. Layout? import java. awt. *; public class Test. Border. Layout

How to Use Border. Layout? import java. awt. *; public class Test. Border. Layout { public static void main(String[] args){ Frame f = new Frame("Test. Border. Layout"); f. set. Size(200, 200); f. add("North", new Button("North")); f. add("South", new Button("South")); f. add("East", new Button("East")); f. add("West", new Button("West")); f. add("Center", new Button("Center")); f. set. Visible(true); }

How to Use Flow. Layout? import java. awt. *; public class Test. Flow. Layout

How to Use Flow. Layout? import java. awt. *; public class Test. Flow. Layout { public static void main(String[] args){ Frame f = new Frame("Test. Flow. Layout"); f. set. Size(200, 200); f. set. Layout(new Flow. Layout()); f. add(new Button("Button 1")); f. add(new Button("Button 2")); f. add(new Button("Button 3")); f. add(new Button("Button 4")); f. add(new Button("Button 5")); f. set. Visible(true);

How to Use Card. Layout? import java. awt. *; public class Test. Card. Layout

How to Use Card. Layout? import java. awt. *; public class Test. Card. Layout { public static void main(String[] args){ Frame f = new Frame("Test. Card Layout"); f. set. Size(200, 200); f. set. Layout(new Card. Layout()); f. add("Graphics. Panel", new Graphics. Panel()); f. add("Label. Panel", new Label. Panel()); f. add("Button. Panel", new Button. Panel()); f. set. Visible(true); } }

How to Use Grid. Layout? import java. awt. *; public class Test. Grid. Layout

How to Use Grid. Layout? import java. awt. *; public class Test. Grid. Layout { public static void main(String[] args){ Frame f = new Frame("Test. Grid. Layout"); f. set. Size(200, 200); f. set. Layout(new Grid. Layout(2, 3)); f. add(new Button("Button 1")); f. add(new Button("Button 2")); f. add(new Button("Button 3")); f. add(new Button("Button 4")); f. add(new Button("Button 5")); f. set. Visible(true); }