User interfaces and Swing Overview, applets, drawing, action listening, layout managers. APIs: java.awt.*, javax.swing.*, classes names start with a J. Java Lectures 1
2 Applets public class Simple extends Applet { public void init() {... public void start() {... public void stop() {... public void destroy() {... init() to initialize the applet each time it's loaded (or reloaded). start() to start the applet's execution, such as when the applet's loaded or when the user revisits a page that contains the applet. stop() to stop the applet's execution, such as when the user leaves the applet's page or quits the browser. destroy() to perform a final cleanup in preparation for unloading.
3 Run an applet <HTML> <BODY> <APPLET CODE=SimpleApplet.class WIDTH=200 HEIGHT=100> </APPLET> </BODY> </HTML> file simpleapplet.html appletviewer simpleapplet.html
Applets and standalone applications The user interface is attached to a panel object nested for applets, in a top-level browser, for a swing application: in a top-level frame object. A frame object is a top-level window that provides a title, banner, and methods to manage the appearance and behavior of the window. Java Lectures 4
A simple window the whole is an instance of javax.swing.jframe. container of the window upper left corner, coordinates (200, 100) from the upper left corner of the screen. width: 300 pixels; height: 350 pixels. Java Lectures 5
6 Code of the simple window import javax.swing.jframe; class Window { public static void main(string[] args) { JFrame cadre = new JFrame("Empty"); cadre.setsize(300, 350); cadre.setlocation(200, 100); cadre.show();
7 A small application JButton JPanel JPanel JPanel JFrame
import javax.swing.*; import java.awt.event.*; import java.awt.*; class Container extends JPanel implements ActionListener { JButton show = new JButton("show"); JButton erase = new JButton("erase"); JPanel board = new JPanel(); Container() { setlayout(new BorderLayout(5, 5)); JPanel thebuttons = new Jpanel(); // for the buttons thebuttons.add(show); thebuttons.add(erase); add(thebuttons, BorderLayout.NORTH); board.setpreferredsize(new Dimension(200, 150)); board.setbackground(color.white); add(board, BorderLayout.CENTER); show.addactionlistener(this); erase.addactionlistener(this); Java Lectures... 8
public void actionperformed(actionevent e) { Object source = e.getsource(); if (source == show) { Graphics g = board.getgraphics(); g.setcolor(color.red); g.drawoval(50,20,100,100); g.dispose(); else if (source == erase) { board.repaint(); public static void main(string[] argv) { JFrame myframe = new JFrame(); myframe.setcontentpane(new Container()); myframe.pack(); myframe.show(); // or myframe.setvisible(true); Java Lectures 9
Drawing Every JComponent object contains: a background and a foreground color, public Color getforeground(); public void setforeground(color c); a font, public Font getfont(); public void setfont(font f); a method, public Graphics getgraphics(); Java Lectures 10
11 The Graphics class Magic pencil for a graphical component. It holds a graphic context: the painted component, a color for drawing: setcolor(color c); a font: setfont(font f); a "clipping" rectangle (render area), draw methods, drawrect(), drawline()...
12 Paint repaint() calls the paint() method, which calls the method paintcomponent(), then a method to repaint the borders, then the paint() methods of the subcomponents, down the containment hierarchy.
13 Button: Action listening The interface ActionListener contains only one method declaration public void actionperformed(actionevent e); How button-generated actions are passed to the actionperformed method? trace.addactionlistener(delegate); the delegate object must implement ActionListener, and thus has a method actionperformed.
14 Action listening means: when the button show is clicked, the method actionperformed of delegate is invoked. an ActionEvent object is created when the button is clicked, and passed to actionperformed. Event handling in the actionperformed method: for example, drawing.
15 Events Click a button, Chose an item in a menu, Push the button of the mouse/move the mouse while the pointer is on a component, Type "return" when working in a text area, Close the window of the application, Press any key....
Action listening and event handling Events are "perceived" by the computer passed to the Java program, treating the event, according to the type of event and the concerned component. ActionEvent objects are created, representing the event. Listeners are objects which treat some kind of events, implementing the right interface. they are linked to a certain source (component), they have methods to handle the events. Java Lectures 16
17 Other ActionListeners Many... An adapter class implements empty versions of all its interface's methods. use inner classes. listener1 Source listener2 listener3
18 The event-dispatching thread It does event handling and the different painting operations on the screen, It treats everything sequentially: while an event is treated, the UI is suspended.
19 Another example: Mouse clicks
20 The MouseListener interface This listener allows to treat 5 types of events, public void mousepressed(mouseevent evt); public void mousereleased(mouseevent evt); public void mouseentered(mouseevent evt); public void mouseexited(mouseevent evt); public void mouseclicked(mouseevent evt);
A class for the circles class GraphicCircle { int x,y, radius; GraphicCircle(int radius, int x, int y) { this.radius = radius; this.x = x; this.y = y; public void draw(graphics g) { g.filloval(x - radius, y - radius, 2*radius, 2*radius); Java Lectures 21
22 class TestClicks extends JPanel implements MouseListener { Vector memory = new Vector(); TestClicks() { setpreferredsize(new Dimension(250, 250)); addmouselistener(this); public void mouseclicked(mouseevent evt) { memory.add(new GraphicCircle(15, evt.getx(), evt.gety())); repaint(); public void mousepressed(mouseevent evt) { public void mousereleased(mouseevent evt) { public void mouseentered(mouseevent evt) { public void mouseexited(mouseevent evt) {...
23 public void paintcomponent(graphics g) { super.paintcomponent(g); for (int i = 0; i < memory.size(); i++) ((GraphicCircle)memory.get(i)).draw(g); public static void main(string[] args) { JFrame frame = new JFrame(); frame.setcontentpane(new TestClicks()); frame.setlocation(50, 50); frame.pack(); frame.setvisible(true);
Layout managers java.awt.flowlayout (default for JPanel): from left to right, starting new rows if necessary. Java Lectures 24
25 Layout managers (2) java.awt.borderlayout (default for content pane): five areas available to hold components, north, south, east, west, and center. All extra space is placed in the center area. javax.swing.boxlayout: puts components in a single row or column. It respects the components' requested maximum sizes, and also lets you align components.
26 Layout managers (3) javax.swing.cardlayout: implements an area that contains different components at different times. javax.swing.gridlayout: displays components in a grid. javax.swing.gridbaglayout: aligns components by placing them within a grid of cells, allowing some components to span more than one cell.
27 Some design solution Main advice: separate the user interface from the application. 2 classes, the user interface, the application, implementing the ActionListener.
28 class Delegate implements MouseListener { Jpanel panel; Delegate(JPanel _panel) { this.panel = _panel; public void mouseclicked(mouseevent evt) { panel.addcircle(new GraphicCircle(15, evt.getx(), evt.gety())); repaint(); public void mousepressed(mouseevent evt) { public void mousereleased(mouseevent evt) { public void mouseentered(mouseevent evt) { public void mouseexited(mouseevent evt) {
29 class TestClicks extends JPanel { Vector memory = new Vector(); TestClicks() { setpreferredsize(new Dimension(250, 250)); addmouselistener(new Delegate(this)); public void addcircle(graphiccircle c) { memory.add(c); public void paintcomponent(graphics g) { super.paintcomponent(g); for (int i = 0; i < memory.size(); i++) ((GraphicCircle)memory.get(i)).draw(g);...
30 public static void main(string[] args) { JFrame frame = new JFrame(); frame.setcontentpane(new TestClicks()); frame.setlocation(50, 50); frame.pack(); frame.setvisible(true);