Advanced Java Programming. Agenda. AWT & Swing. GUI Programming Part I AWT & Basics. Shmulik London

Advanced Java Programming Shmulik London Lecture #5 GUI Programming Part I – AWT & Basics Advanced Java Programming / © Shmulik London 2006 Interdisc...
13 downloads 2 Views 1MB Size
Advanced Java Programming Shmulik London Lecture #5

GUI Programming Part I – AWT & Basics Advanced Java Programming / © Shmulik London 2006 Interdisciplinary Center Herzeliza Israel

1

Agenda • • • • • •

AWT & Swing AWT components Containers Layout Managers Event Handling Painting

Advanced Java Programming / © Shmulik London 2006

2

AWT & Swing • Originally Java came with the AWT (Abstract Window Toolkit) • AWT was very basic. Developers wanted more. • Swing is a much richer API for GUI. • They are built around different design concept • AWT and Swing co-exist now, but you normally use Swing. Advanced Java Programming / © Shmulik London 2006

3

AWT • AWT meant to be small and simple – Allow easy porting to many platforms – Developing GUI platform is a lot of effort JDK1.1 AWT – 141 files, 13262 lines of code JDK1.5 AWT+Swing – 1017 files, 209,908 lines!! JDK1.1 was light – 8Mb compared to 55Mb today

• It was built around the concept that the GUI should have the look & feel of underlying platform – A problematic concept for complex thing as GUI Advanced Java Programming / © Shmulik London 2006

4

Concept behind AWT • Use underlying widgets of platform • Each component in AWT is just a wrapper for an underlying native widget (its peer) • The component request the windowing system to create the underlying widget and customize it according to its properties • Can support only lowest common denominator of all platforms  Good performance  Easy to port JDK  Native look & feel

Too elementary Heavy weight components Difficult to write apps that will look good on different platforms

Advanced Java Programming / © Shmulik London 2006

5

Some AWT components Button

Frame

TextField Label Scrollbar Choice

others ....

Advanced Java Programming / © Shmulik London 2006

6

Class component • All components are derived from the class java.awt.Component Component

Button

TextComponent

TextArea

...

TextField

Advanced Java Programming / © Shmulik London 2006

7

Class Component • Class component defines the properties common to all components: location, size, background color, foreground color, visibility, ... public public public public public public public public ...

Dimension getSize() void setSize(Dimension d) void setSize(int x, int y) Point getLocation() void setLocation(Point p) void setLocation(int x, int y) Color getBackground() void setBackground(Color c)

Advanced Java Programming / © Shmulik London 2006

8

Container • Container is a subclass of Component that is a superclass for all Components that can contain other components. • It adds to Component the functionality of adding/removing components public public public public public ...

void add(Component c) void remove(Component c) Component[] getComponents() int getComponentCount() void setLayout(...) Advanced Java Programming / © Shmulik London 2006

9

Opening a Frame // A sample program that opens a yellow frame class FrameExample { public static void main(String[] args) { Frame frame = new Frame(“Example”); frame.setSize(400,300); frame.setBackground(Color.yellow); frame.setVisible(true); } }

Advanced Java Programming / © Shmulik London 2006

10

Opening a Frame // A sample program that opens a yellow frame class FrameExample { public static void main(String[] args) { new SampleFrame().setVisible(true); } } class SampleFrame extends Frame { public SampleFrame() { super(“Example”); setSize(400,300); setBackground(Color.yellow); } }

Advanced Java Programming / © Shmulik London 2006

11

Adding components // A sample program that opens a frame with // a button on it class FrameExample extends Frame{ public FrameExample () { setSize(400,300); Button okButton = new Button(“OK”); add(okButton); } public static void main(String[] args) { new FrameExample().setVisible(true); } }

Advanced Java Programming / © Shmulik London 2006

12

Layout Managers • Were are the components added? • There are two ways to layout components on a container: – Set the exact size and location of every component – Use a LayoutManager

• Every container has its own LayoutManager object • The LayoutManager is responsible for the the layout of the component inside the container • The LayoutManager is consulted whenever there is a need to rearrange the components inside the container (container size changed, component added.. ) Advanced Java Programming / © Shmulik London 2006

13

Layout Managers add(new Button(“Ok”))

Frame

layoutContainer(this)

FlowLayout

Advanced Java Programming / © Shmulik London 2006

14

Layout Managers • There a various types of Layout Managers. Each has its strategy for arranging the components. • Layout managers given with java.awt: – FlowLayout, BorderLayout, GridLayout, CardLayout, GridBagLayout

• You can define your own layout managers by implementing the interface java.awt.LayoutManager • LayoutManager is an example of the Strategy pattern

Advanced Java Programming / © Shmulik London 2006

15

FlowLayout

setLayout(new FlowLayout()); add(new Label(“Name:”)); add(new TextField(10)); add(new Button(“Ok”));

Advanced Java Programming / © Shmulik London 2006

16

GridLayout

setLayout(new GridLayout(2,2)); add(new Button(“A”)); add(new Button(“B”)); add(new Button(“C”)); add(new Button(“D”));

Advanced Java Programming / © Shmulik London 2006

17

GridLayout

setLayout(new BorderLaout()); add(new Button(“North”), BorderLayout.NORTH); add(new Button(“East”), BorderLayout.EAST); add(new Button(“South”), BorderLayout.SOUTH); add(new Button(“West”), BorderLayout.WEST); add(new Button(“Center”), BorderLayout.CENTER); Advanced Java Programming / © Shmulik London 2006

18

Combination of layouts Frame with BorderLayout Panel with GridLayout setLayout(new BorderLaout()); TextField display = new TextField(); add(display, BorderLayout.NORTH); Panel buttonsPanel = new Panel(); buttonsPanel.setLayout(new GridLayout(4,4)); String[] labels = {“7”,”8”,”9”,”+”,”4”,”5”, ... }; for (int i=0; iMINIMAL_DISTANCE || Math.abs(mouse.y-ym)>MINIMAL_DISTANCE) { return; } // .. decide for the direction of the movement figure.setLocation( figure.getLocation().x+dx, figure.getLocation().y+dy); repaint(); } } Advanced Java Programming / © Shmulik London 2006

34

Adapters (the need) import java.awt.*; import java.awt.event.*; /** * A panel that lets the user draw freehand lines */ public class DrawingPanel extends Panel { // The points composing the path drawn by the user java.util.List path; public DrawingPanel() { addMouseListener(new StartDrawingAdapter(this)); addMouseMotionListener(new DrawingAdapter(this)); }

Advanced Java Programming / © Shmulik London 2006

35

Adapters (the need) /** * Paints this component */ public void paint(Graphics g) { if (path==null) { return; } for (int i=0; i