Two Java libraries java.applet and java.awt provide the mechanisms for building graphical user interfaces (GUI) and applets. GUI's allow programs to communicate with users using bitmap displays and gestures with a mouse (as opposed to a textual, command-line approach). Applets are GUI applications that are part of HTML documents and are delivered over the internet using WWW browsers.
The java.applet library is small; to build applets requires not only the applet library, but also the awt library--the abstract window toolkit. The java.awt is large, with over 42 classes.
import java.applet.Applet; import java.awt.Graphics; import java.awt.Color; public class HelloApplet extends Applet { public void init() { setBackground (Color.white); // Make applet window stand out } public void paint(Graphics g) { g.drawString ("Hello world!", 40, 20); } }
<applet code="ConicApplet" width=400 height=400></applet>
import java.applet.Applet; import java.awt.*; public class Text extends Applet { private String text; public void init() { setBackground (Color.white); // Make applet window stand out text = getParameter("text"); if (text == null) { text = "Hello world!"; } } public void paint (Graphics g) { // Put text in the center of the window Dimension d = this.size(); // Get the size of the window FontMetrics m = g.getFontMetrics(); int a = m.getAscent(); int baseline = a + (d.height - (a + m.getDescent()))/2; g.drawString (text, (d.width-m.stringWidth(text))/2, baseline); } }
Default value in java.awt.Component
public void update(Graphics g) { g.setColor(getBackground()); g.fillRect(0, 0, width, height); g.setColor(getForeground()); paint(g); }Don't change backround in paint or default or you will get a flash of color.
Dialog Helvitica TimesRoman Courier DialogInput ZapfDingbats
style: PLAIN, BOLD, ITALIC, Font.BOLD|Font.ITALIC
Color white = new Color(255, 255, 255); Color lightGray = new Color(192, 192, 192); Color gray = new Color(128, 128, 128); Color darkGray = new Color(64, 64, 64); Color black = new Color(0, 0, 0); Color red = new Color(255, 0, 0); Color pink = new Color(255, 175, 175); Color orange = new Color(255, 200, 0); Color yellow = new Color(255, 255, 0); Color green = new Color(0, 255, 0); Color magenta = new Color(255, 0, 255); Color cyan = new Color(0, 255, 255); Color blue = new Color(0, 0, 255);
brighter is RGB / 0.7 (ie., 1.42 * RGB) darker is RGB * 0.7
java.awt.Graphics public abstract class Graphics extends Object { public void draw3DRect (int x, int y, int width, int height, boolean raised) public void drawArc (int x, int y, int width, int height, int startAngle, int arcAngle) public void drawLine (int x1, int y1, int x2, int y2) public void drawOval (int x, int y, int width, int height) public void drawPolygon (int[] xs, int[] ys, int n) public void drawRect (int x, in y, int width, int height) public void drawString (string str, int x, in y) public void drawImage (image img, int x, int y, int width, int height, ImageObserver obs
It is usually better to call repaint() then to call paint() directly, because if you call repaint() 10 times before the screen updater actually gets around to doing the painting, your paint method will only get called once, which is more efficient.
Button, Checkbox, Choice, Label, List, TextComponent, Scrollbar, Canvas MenuComponent: MenuBar, MenuItem Container: Panel, Window
Classes BorderLayout, CardLayout, FlowLayout, GridBagLayout, GridLayout implement interface LayoutManager.
Called by default event handler in the class java.awt.Component.
public boolean action (Event e, Object arg)List of all ways "action" event generate and meaning of the argument.
Component Class Argument Type argument value Button String button label Checkbox Boolean checkbox state (after) Choice String menu choice List String list item TextField String field contents