Java AWT
AWT Intro
- Abstract Windowing Toolkit
- Lib to create GUIs
- Features
- Graphical primitives
- draw line, fill area, text rendering
- Event based programming (mouse/keyboard)
- Dialogboxes
- Advanced features
- Bitmap rendering
- Sound
- Graphical primitives
- Portable
Swing vs. AWT
- Swing as AWT addon
- Swing has more features
- AWT easier to learn, less features
- Swing components entirely written in Java
- AWT components need native implementation
GUI Basics
- import java.awt.*
- App needs a window to display components
- Inherit from frame or dialog (subclasses of window)
- Or create a frame
- No events attached initially
- Window has standard features
Frame wnd = new Frame("My frame");
wnd.setSize(400,300);
wnd.setVisible(true);
Paint Method
- Called to draw the window
- Graphics provides a graphics device context
- Universal graphics/text output
- Provides all features (draw line, render text)
- Rendering based on 2d coord system
- (0,0) is left top corner
- unit is pixel (device dependent)
- getInsets provides current painting scope
public class AWTApp extends Frame {
public void paint(Graphics g) {
g.drawString("Hi!", 100, 100);
}
}
Close a Window
- Register WindowListener
- WindowListener interface specifies a windowClosing method
- setVisible(false), System.exit(0)
- Or implement as WindowClosingAdapter (recurring task)
- And register adapter as WindowListener
public class WindowClosingAdapter extends WindowAdapter {
@Override
public void windowClosing(WindowEvent event) {
event.getWindow().setVisible(false);
event.getWindow().dispose();
System.exit(0);
}
}
public class Main {
public static void main(String[] args) {
Frame f = new Frame("WindowClosingAdapter test");
f.addWindowListener(new WindowClosingAdapter());
f.setSize(400,300);
f.setLocation(100,100);
f.setVisible(true);
}
}
Basic Graphic Tasks
// Line
g.drawLine(x1, y1, x2, y2);
// Rectangle
// x,y is top left corner
g.drawRect(x,y, width, height);
// Polygon
// arx ... array of x coords
// ary ... array of y coords
// both arrays need to be synced
// int cnt .. num of coord pairs
g.drawPolygon(int[] arx, int[] ary, int cnt);
//Circle
// in the rect with widht and height
// with top left corner in x,y
// draw biggest ellipse
g.drawOval(int x, int y, int width, int height);
Basic Graphics Tasks #2
// Arc
// 0° - 3:00 pos
// positive angles ccw
g.drawArc(int x, int y, int width, int height,
int startAngle, int arcAnlge);
// Fillmodes
g.fillRect(int x, int y, int w, int h);
g.fillPolygon(int[] arx, int[] ary, int cnt);
g.fillOval(int x, int y, int width, int height);
g.fillArc(int x, int y, int width, int height,
int startAngle, int arcAngle);
// Copy/Clear areas
g.clearRect(int x, int y, int width, int height);
g.copyArea(int x, int y, int width, int height, int dx, int dy);
Clipping Region & Text Output
// Output to certain area
// Define smaller rects to limit "canvas"
g.clipRect(int x, int y, int width, int height);
// Define arb clip
g.setClip(int x, int y, int width, int height);
// Text output
// x,y defines basisline
g.drawString(String str, int x, int y);
g.drawChars(char[] data, int offset, int length; int x, int y);
g.drawBytes(byte[] data, int offset, int length, int x, int y),
// define different font objects
// no rotation etc. provided
// toolkit provides std fontlist
Toolkit.getDefaultToolkit().getFontList();
// Methods to get Font information
AWT Components
- Component is base class
- abstract class, that represents a GUI element with certain size, and pos, with gui events in mind
- Container derives from Component
- container is also component, but may hold other container
- methods to add/remove components
- work with layoutmanager
- Panel and Window derive from Container
- Panel used to define a set of dialog elements
- Window top-level window mit border, title and menü
AWT Frame & Visual Properties
Frame frame = new Frame("Frame");
frame.setSize(300,200);
frame.setVisible(true);
// ...
frame.setVisible(false);
frame.dispose();
System.exit(0);
- Type of border, position, size
- Relation to other windows
- Window class has no border
- Frame has border, title and menü
- AWT provides methods to set all this data
Event Handling
- Communication between OS and app through messages
- E.g. mouseclicks, mouse movement, etc.
- Event sources: send messages
- EventListener: receive messages
- Delegation Based Event Handling
- Window class implements EventListener
- Provide callbacks
- Local anonymous EventListener classes
- Adapter class specializations
Event Handling #2
- Event types, Event objects
- Lowlevel Event: ComponentEvent subclasses
- Transfer of basic messages between windows/dialogs
- Event listener types
- Adapter classes
- for every low level event source
References
Handbuch der Java-Programmierung
(5. Auflage)
Thank you for your attention!
Java AWT
By dinony
Java AWT
Java GUI Programming
- 234