820 likes | 1.17k Views
AWT. Window Fundamentals. Window Fundamentals. Component encapsulates all of the attributes of a visual component. All user interface elements that are displayed on the screen and that interact with the user are subclasses of Component .
E N D
Window Fundamentals • Component encapsulates all of the attributes of a visual component. • All user interface elements that are displayed on the screen and that interact with the user are subclasses of Component. • A Component object is responsible for remembering the current foreground and background colors and the currently selected text font.
Window Fundamentals Container • A container is responsible for laying out (that is, positioning) any components that it contains. • It does this through the use of various layout managers
Window Fundamentals Panel • A Panel may be thought of as a recursively nestable, concrete screen component. • Panel is the superclass for Applet. When screen output is directed to an applet, it is drawn on the surface of a Panel object. • In essence, a Panel is a window that does not contain a title bar, menu bar, or border.
Window Fundamentals Window • The Window class creates a top-level window • Generally, Window objects are not created directly. • Instead, a subclass of Window called Frame.
Window Fundamentals Frame • Frame is a subclass of Window • It has a title bar, menu bar, borders, and resizing corners. • If you create a Frame object from within an applet, it will contain a warning message, such as “Java Applet Window,” to the user that an applet window has been created. • This message warns users that the window was started by an applet.
Frames • Frame( ) • Frame(String title)
Frames import java.awt.*; import java.awt.event.*; class MyFrame extends Frame { MyFrame(String s) { super(s) } public void paint(Graphics g) { g.drawString("MCA Department",100,50); }
Frames public static void main(String args[]) { MyFrame f = new MyFrame(“VESIT”); f.setSize(200,100); f.setVisible(true); } }
Frames // Create a child frame window from within an applet. import java.awt.*; import java.awt.event.*; import java.applet.*; /* <applet code="AppletFrame" width=300 height=50> </applet> */ class MyFrame extends Frame { MyFrame() { addWindowListener(new MyWindowAdapter(this)); }
Frames public void paint(Graphics g) { g.drawString("Hello world",100,50); } } class MyWindowAdapter extends WindowAdapter { MyFrame f1; public MyWindowAdapter(MyFrame f1) { this.f1 = f1; } public void windowClosing(WindowEvent we) { f1.setVisible(false); } }
Frames public class AppletFrame1 extends Applet { MyFrame f; public void init() { f = new MyFrame(); f.setSize(250, 250); f.setVisible(true); } public void start() { f.setVisible(true); } public void stop() { f.setVisible(false); } public void paint(Graphics g) { g.drawString("This is in applet window", 10, 20); }
Color class Color(intred, intgreen, intblue) • void setColor(Color newColor) • Color getColor( ) import java.awt.*; import java.applet.*; /* <applet code=“ColorDemo" width=300 height=200> </applet> */ public class ColorDemo extends Applet { public void paint(Graphics g) { g.setColor(Color.green); g.drawString(“Hello World”, 50,50); } }
Graphics Class • All graphics are drawn relative to a window. • This can be the main window of an applet, a child window of an applet, or a stand-alone application window. • The origin of each window is at the top-left corner and is 0,0. • Coordinates are specified in pixels.
Graphics Primitives • For drawing geometric shapes, texts • An abstract class • the extended class must override paint() Line RoundRectangle Polygon Oval Rectangle Arc
Drawing Lines void drawLine(int startX, int startY, int endX, int endY) public void paint(Graphics g) { g.setColor(Color.blue); g.drawLine(0, 0, 100, 100); g.drawLine(0, 100, 100, 0); } (x1,y1) (x2,y2)
Drawing Rectangles void drawRect(int top, int left, int width, int height) void fillRect(int top, int left, int width, int height) public void paint(Graphics g) { g.setColor(Color.pink); g.drawRect(10, 10, 60, 50); g.fillRect(100, 10, 60, 50); }
Drawing Rounded Rectangles void drawRoundRect(int top, int left, int width, int height, int xDiam, int yDiam) void fillRoundRect(int top, int left, int width, int height, int xDiam, int yDiam) xDiam: diameter of the rounding arc along the X axis yDiam: diameter of the rounding arc along the Y axis public void paint(Graphics g) { g.setColor(Color.yellow); g.drawRoundRect(190, 10, 60, 50, 15, 15); g.fillRoundRect(70, 90, 140, 100, 30, 40); }
Drawing Ellipses and Circles • void drawOval(int top, int left, int width, int height) • void fillOval(int top, int left, int width, int height) • ellipse is drawn within a bounding rectangle specified by top,left , width • and height. • To draw a circle, specify a square as the bounding rectangle public void paint(Graphics g) { g.setColor(Color.blue); g.drawOval(10, 10, 75, 50); g.fillOval(100, 10, 50, 50); }
Drawing Arcs • void drawArc(inttop, intleft, intwidth, intheight, • intstartAngle, intsweepAngle) • void fillArc(inttop, intleft, intwidth, intheight, • intstartAngle, intsweepAngle) • arc is bounded by the rectangle specified by top, left , width • and height. • The arc is drawn from startAnglethrough the angular distance • specified by sweepAngle.
Drawing Arcs public void paint(Graphics g) { g.setColor(Color.orange); g.drawArc(10, 40, 70, 70, 0, 75); g.fillArc(100, 40, 70, 70, 0, 75); }
Drawing Polygons void drawPolygon(int x[ ], int y[ ], int numPoints) void fillPolygon(int x[ ], int y[ ], int numPoints) public void paint(Graphics g) { g.setColor(Color.green); int xs[] = {161,161,185,209,185,161}; int ys[] = {310,334,358,334,310,310}; g.fillPolygon(xs,ys,6); }
Font class Font(String fontName, int fontStyle, int pointSize) fontStyl: Font.PLAIN, Font.BOLD, and Font.ITALIC. To combine styles: Font.BOLD | Font.ITALIC void setFont(Font fontObj)
Font class public void paint(Graphics g) { g.setColor(Color.blue); Font f = new Font(“TimesRoman”,Font.BOLD,20); g.setFont(f); g.drawString(“Hello World”, 50,50); }
Component Canvas Scrollbar Button Checkbox Label List Choice TextComponent TextArea TextField Component Container Panel Window Dialog FileDialog Frame MenuComponent MenuItem Menu Using AWT Components
Using AWT Components • Component add(Component compObj) • void remove(Component obj)
Labels • Label( ) • Label(String str) • void setText(String str) • String getText( )
Labels public void init() { Label one = new Label("One"); Label two = new Label("Two"); Label three = new Label("Three"); add(one); add(two); add(three); }
Buttons • Button( ) • Button(String str) • void setLabel(String str) • String getLabel( )
Buttons public void init() { Button yes = new Button("Yes"); Button no = new Button("No"); Button maybe = new Button("Undecided"); add(yes); add(no); add(maybe); }
Buttons : Event Handling import java.awt.*; import java.awt.event.*; import java.applet.*; /* <applet code="ButtonDemo" width=250 height=150> </applet> */ public class ButtonDemo extends Applet implements ActionListener { String msg = ""; Button yes, no, maybe;
Buttons : Event Handling public void init() { yes = new Button("Yes"); no = new Button("No"); maybe = new Button("Undecided"); add(yes); add(no); add(maybe); yes.addActionListener(this); no.addActionListener(this); maybe.addActionListener(this); }
Buttons : Event Handling public void actionPerformed(ActionEvent ae) { msg = ae.getActionCommand(); repaint(); } public void paint(Graphics g) { g.drawString("Pressed: "+msg, 6, 100); } }
Checkboxes • Checkbox( ) • Checkbox(String str) • Checkbox(String str, boolean on) • Checkbox(String str, CheckboxGroup cbGroup, boolean on) • boolean getState( ) • void setState(boolean on) • String getLabel( ) • void setLabel(String str)
Checkboxes: Event Handling public class CheckboxDemo extends Applet implements ItemListener { String msg = ""; Checkbox c, cpp, java; public void init() { c = new Checkbox("C", null, true); cpp = new Checkbox("C++"); java = new Checkbox("JAVA"); add(c); add(cpp); add(java);
Checkboxes: Event Handling c.addItemListener(this); cpp.addItemListener(this); java.addItemListener(this); } public void itemStateChanged(ItemEvent ie) { repaint(); }
Checkboxes: Event Handling public void paint(Graphics g) { msg = c.getLabel()+ " : " + c.getState(); g.drawString(msg, 6, 100); msg = cpp.getLabel()+ " : " + cpp.getState(); g.drawString(msg, 6, 120); msg = java.getLabel()+ " : " + java.getState(); g.drawString(msg, 6, 140); } }
CheckboxGroup • Checkbox getSelectedCheckbox( ) • void setSelectedCheckbox(Checkbox which)
CheckboxGroup public class CBGroup extends Frame { public CBGroup() { CheckboxGroup cbg = new CheckboxGroup(); Checkbox cb1 = new Checkbox(“C”, cbg,false); Checkbox cb2 = new Checkbox(“JAVA”, cbg, true); add(cb1); add(cb2); add(cb3); } … }
Choices void add(String name) : To add a selection to the list String getSelectedItem( ) int getSelectedIndex( )
Choices public class ColorChoice extends Applet implements ItemListener { Choice c1; Color color; public void init() { c1 = new Choice(); c1.addItem("Red"); c1.addItem("Green"); c1.addItem("Blue"); c1.addItemListener(this); add(c1); }
Choices public void itemStateChanged(ItemEvent ie) { if(c1.getSelectedItem().equals("Red")) color = Color.red; else if(c1.getSelectedItem().equals("Green")) color = Color.green; else color = Color.blue; repaint(); } public void paint(Graphics g) { setBackground(color); } }
Lists • List( ) • List(int numRows) • List(int numRows, boolean multipleSelect) • void add(String name) • void add(String name, int index) • String getSelectedItem( ) • int getSelectedIndex( )
Lists public void init() { List list = new List(); list.add("Hydrogen"); list.add("Helium"); list.add("Carbon"); list.add("Oxygen"); add(list); }
TextField • TextField( ) • TextField(int numChars) • TextField(String str) • TextField(String str, int numChars) • String getSelectedText( )
TextArea • TextArea( ) • TextArea(int numLines, int numChars) • TextArea(String str) • TextArea(String str, int numLines, int numChars) • void append(String str)