Let's consider three basic abstractions of the Abstract Windowing Toolkit.
Color Dimension Font FontMetrics Graphics Image Insets MediaTracker Point Polygon Rectangle Toolkit
An applet can draw items on the screen with the paint(Graphics) method. Images, lines, circles, polygons or any other graphical items are drawn on a Graphics instance. The browser passes the graphics to the paint(Graphics) method. paint(Graphics) is called several times throughout the life of an applet. If an applet changes location on the screen or any time it needs to be refreshed(redrawn), paint(Graphics) is called. Programmers do not directly call paint(Graphics), to have your applet paint itself call repaint(); repaint() will call update(Graphics) that in its turn calls paint(Graphics). Be aware that update(Graphics) assumes that the background is not cleared and clears background first. If you don't want to waste time to clear the background each time you want to paint, it's a good idea to override update(Graphics) with your own code that only calls for paint.
The next example displays a Java related image and some Java poetry below. All this is placed into a blue filled rectangle drawn inside an applet.
|
A layout manager is used to format the location of components within a container. A container (for example the Panel) is a component that can include other components or containers.
Layout of a container can be defined as null. In such a case the size and exact location of each component has to be defined by the programmer. This practice is not recommended because the cross platform portability will be lost.
Most commonly used Layout Managers: FlowLayout, BorderLayout, GridLayout
The GridBagLayout is the most flexible layout manager. Any layout created with the other layout managers can be done with the GridBagLayout. This kind of flexibility comes with more complication and more code to write.
Using the GridBagLayout also requires the use of GridBagConstraints. GridBagConstraints has several public constants that are used for positioning components.
The example creates several buttons with the following properties:
/* We can simplify this process by creating a method to * perform steps 4 and 5 of the GridBagLayout Procedures * Set constraints to component and add component to container. **/ protected void setButton(String iName, GridBagLayout iGridBag, GridBagConstraints iConstraints) { Button button = new Button(iName); iGridBag.setConstraints(button, iConstraints); add (button); }
The rest of this example performs steps 1,2 and 3 of the GridBagLayout Procedure.
The last step is also performed before each call to setButton
6. GoTo 3 and repeat for each Component
|
List of common Components
Lets walk through Sun's example written in Java 1.0 style with old event handling and deprecation methods..
public class ComponentTest extends Applet { Panel center;The following code does
public void init() { setBackground(Color.lightGray); setFont(new Font("Helvetica", Font.PLAIN, 12)); setLayout(new BorderLayout()); Label l = new Label("North"); l.setAlignment(Label.CENTER);
add("South", new TextField("South")); add("North", l); center = new ScrollPanel(); center.setLayout(new BorderLayout()); add("Center", center);
Panel innerPanel = new ScrollPanel(); center.add("Center", innerPanel); innerPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); innerPanel.add(new Label("List of Widgets")); innerPanel.add(new Button("Arthur")); innerPanel.add(new Button("Sami")); innerPanel.add(new SillyWidget()); innerPanel.add(new TextField("quite random")); innerPanel.add(new Scrollbar(Scrollbar.VERTICAL)); innerPanel.add(new Scrollbar(Scrollbar.HORIZONTAL));
List li = new List(4, false); // 4 visible lines, // no multi-line selection li.reshape(0, 0, 100, 100); // move to 0,0 and set size=100x100 li.addItem("Arthur"); li.addItem("Sami"); li.addItem("James"); li.addItem("Chris"); li.addItem("Mary"); li.addItem("Lisa"); li.addItem("Kathy"); li.select(1);
innerPanel.add(li); TextArea tx = new TextArea(10, 5); // 10 cols, 5 rows tx.setText("this is some random text"); tx.setForeground(Color.red); innerPanel.add(tx);
Choice opt = new Choice(); opt.addItem("Arthur"); opt.addItem("Sami"); opt.addItem("Chris"); opt.addItem("Jim"); innerPanel.add(opt);
CheckboxGroup g = new CheckboxGroup(); innerPanel.add(new Checkbox("one", g, false)); innerPanel.add(new Checkbox("two", g, true)); // two=selected innerPanel.add(new Checkbox("three", g, false)); center.add("East", new Scrollbar(Scrollbar.VERTICAL)); center.add("South", new Scrollbar(Scrollbar.HORIZONTAL)); add("East", new Button("East")); add("West", new Button("West")); }
public synchronized boolean handleEvent(Event e) { return false; } }Definition of the ScrollPanel used above
class ScrollPanel extends Panel { public ScrollPanel() { reshape(0, 0, 100, 100); } // Overriden paint method that draws a rectange around the panel public void paint(Graphics g) { Rectangle r = bounds(); g.drawRect(0, 0, r.width, r.height); } }
The SillyWidget should look like this:
class SillyWidget extends Canvas { Color c1 = Color.pink; Color c2 = Color.lightGray; long entered = 0; long inComponent = 0; Font font = new Font("Courier", Font.BOLD, 12); FontMetrics fm; public SillyWidget() { reshape(0, 0, 100, 100); } // Called when the mouse enters the Canvas // This widget is just to change the colors and forcing a repaint // when the mouse enters public boolean mouseEnter(Event e, int x, int y) { Color c = c1; entered = e.when; c1 = c2; c2 = c; repaint(); return true; } // Called when the mouse exits the Canvas // This widget is just change the colors and forcing a repaint when the // mouse exits public boolean mouseExit(Event e, int x, int y) { Color c = c1; inComponent = e.when - entered; c1 = c2; c2 = c; repaint(); return true; } // Called when the Canvas needs repaint and merely draws a bunch of rectangles public void paint(Graphics g) { Rectangle r = bounds(); g.setColor(c1); g.fillRoundRect(0, 0, r.width, r.height, 20, 20); g.setColor(c2); g.fillRoundRect(5, 5, r.width - 10, r.height - 10, 20, 20); g.setColor(c1); g.fillRoundRect(15, 15, r.width - 30, r.height - 30, 20, 20); g.setColor(Color.darkGray); g.setFont(font); fm = g.getFontMetrics(font); String s = ""+inComponent+" ms."; int width = fm.stringWidth(s); System.out.println("width = " + width); g.drawString(s, (r.width - width) / 2, (r.height - fm.getHeight()) / 2); } }