overview
play

Overview Roadmap Today: introduction to Java Windows and graphical - PDF document

Overview Roadmap Today: introduction to Java Windows and graphical output Future: event-driven programs and user interaction Topics CSE 143 Java A bit of history: AWT and Swing Some basic Swing components: JFrame and JPanel


  1. Overview • Roadmap • Today: introduction to Java Windows and graphical output • Future: event-driven programs and user interaction • Topics CSE 143 Java • A bit of history: AWT and Swing • Some basic Swing components: JFrame and JPanel • Java graphics Introduction to Graphical Interfaces in Java: • Reading: AWT and Swing • Textbook: Ch. 19 • Online: Sun Java Swing tutorial (particularly good for picking up details of particular parts of Swing/AWT as needed); Swing API javadoc web pages Reading: Sec. 19.1-19.3, 19.5 http://java.sun.com/docs/books/tutorial/uiswing/index.html 10/9/2003 (c) 2001-3, University of Washington 06-1 10/9/2003 (c) 2001-3, University of Washington 06-2 Graphical User Interfaces Opposing Styles of Interaction • GUIs are a hallmark of modern software • “Algorithm-Driven” • “Event Driven” • When program needs • Hardly existed outside research labs until Mac's came along • When user wants to do something, information from user, it he/she signals to the program • Picked up by PC's later asks for it Moves or clicks mouse, types, etc. • User sees and interacts with “controls” or “components” • Program is in control • These signals come to the program as (sometimes called “widgets”) • Typical in non-GUI “events” • menus environments (examples: • Program is interrupted to deal with the payroll, batch simulations) • scrollbars events • text boxes • User has more control • check boxes • Typical in GUI environments • buttons • radio button groups • graphics panels • etc. etc. 10/9/2003 (c) 2001-3, University of Washington 06-3 10/9/2003 (c) 2001-3, University of Washington 06-4 A Bit of Java History Bit o' Advice • Java 1.0: AWT (Abstract Windowing Toolkit) • Use Swing whenever you can • Java 1.1: AWT with new event handling model • Use AWT whenever you have to • Java 1.2 (aka Java 2): Swing (mostly to support older windows browsers • Greatly enhanced user interface toolkit built on top of AWT that don’t have the current Sun Java plugin) • Same basic event handling model as in Java 1.1 AWT • Java 1.3, 1.4 • Bug fixes and significant performance improvements; no major revolution • Naming • Most Swing components start with J. • No such standard for AWT components 10/9/2003 (c) 2001-3, University of Washington 06-5 10/9/2003 (c) 2001-3, University of Washington 06-6 CSE143 Au03 06-1

  2. Components & Containers Types of Containers • Every GUI-related component • Top-level containers: JFrame,JDialog, JApplet descends from Component, which • Often correspond to OS Windows contains dozens of basic methods • Mid-level containers: panels, scroll panes, tool bars, … and fields common to all AWT/Swing component • can contain certain other components Component • "Atomic" components: labels, text • JPanel is best for general use fields, buttons, check boxes, icons, • An Applet is a special kind of container Lots Container menu items, … of AW componjents • Specialized containers: menus, list boxes, combo • Some components are Containers boxes... JComponent various AWT containers – components like panels that can • Technically, all J components are containers contain other nested tons JPanel JFileChooser subcomponents of J components 10/9/2003 (c) 2001-3, University of Washington 06-7 10/9/2003 (c) 2001-3, University of Washington 06-8 JFrame – A Top-Level Window JPanel – A General Purpose Container • Top-level application window • Commonly added to a window to provide a space for graphics, or collections of buttons, labels, etc. JFrame win = new JFrame(“Optional Window Title”); • Some common methods • JPanels can be nested to any depth setSize(int width, int height); // frame width and height • Many methods in common with JFrame (since both are setBackground(Color c); // background color ultimately instances of Component) show( ); //make visible (for the first time) setSize(int width, int height); repaint( ); // request repaint after content change setBackground(Color c); setPreferredSize(Dimension d); // default size for window; also can set min setPreferredSize(Dimension d); // and max sizes • Bit o' advice: Can't find the method you're looking for? dispose( ); // get rid of the window when done Check the superclass. 10/9/2003 (c) 2001-3, University of Washington 06-9 10/9/2003 (c) 2001-3, University of Washington 06-10 Adding Components to Containers Non-Component Classes • Not all classes are GUI components • Swing containers have a “content pane” that manages the components in that container • AWT [Differs from original AWT containers, which managed their components directly] • Color, Dimension, Font, layout managers • Shape and subclasses like Rectangle, Point, etc. • To add a component to a container, get the content • Graphics pane, and use its add method • Swing JFrame jf = new JFrame( ); JPanel panel = new JPanel( ); • Borders jf.getContentPane( ).add(panel); • Further geometric classes or • Graphics2D Container cp = jf.getContentPane( ); • Neither AWT nor Swing cp.add(panel); • Images, Icons 10/9/2003 (c) 2001-3, University of Washington 06-11 10/9/2003 (c) 2001-3, University of Washington 06-12 CSE143 Au03 06-2

  3. Layout Managers pack and validate • What happens if we add several components to a • Container state is “valid” or “invalid” depending on whether container? layout manager has arranged components since last change • What are their relative positions? • When a container is altered, either by adding components or changes to components (resized, contents change, etc.), the • Answer: each container has a layout manager. Some layout needs to be updated (i.e., the container state needs to be kinds: set to valid) • FlowLayout (left to right, top to bottom) • Swing does this automatically more often than AWT, but not always • BorderLayout(“center”, “north”, “south”, “east”, “west”) • Common methods after changing layout • GridLayout (2-D grid) • validate( ) – redo the layout to take into account new or changed • GridBagLayout (makes HTML tables look simple); others components • Default LayoutManager for JFrame is BorderLayout • pack( ) – redo the layout using the preferred size of each component • Default for JPanel is FlowLayout 10/9/2003 (c) 2001-3, University of Washington 06-13 10/9/2003 (c) 2001-3, University of Washington 06-14 Layout Example Graphics and Drawing • Simple things like labels have suitable default code to paint • Create a JFrame with a button at the bottom and a panel themselves in the center • For more complex graphics, extend a suitable class and override JFrame frame = new JFrame(“Trivial Window”); //default layout: Border the (empty) inherited method paintcomponent to draws its contents JPanel panel = new JPanel( ); • (Different from AWT, where you overrode paint – don’t do that in swing!) JLabel label = new JLabel(“Smile!”); public class Drawing extends JPanel { label.setHorizontalAlighment(SwingConstants.CENTER); ... Container cp = frame.getContentPane( ); /** Repaint this Drawing whenever requested by the system */ public void paintComponent (Graphics g) { cp.add(panel, BorderLayout.CENTER); super.paintComponent(g); cp.add(label, BorderLayout.SOUTH); g.setColor(Color.green); g.drawOval(40,30,100,100); g.setColor(Color.red); g.fillRect(60, 50, 60, 60); } 10/9/2003 (c) 2001-3, University of Washington 06-15 10/9/2003 (c) 2001-3, University of Washington 06-16 paintComponent Painter's Rules • Method paintComponent is called by the underlying • Always override paintComponent( ) of any component you will be drawing on system whenever it needs the window to be repainted • Not necessary if you make simple changes, like changing background color, • Triggered by window being move, resized, uncovered, title, etc. that don't require a graphics object expanded from icon, etc. • Always call super.paintComponent(g) to paint the background • Can happen anytime – you don’t control when • Never call paint( ) or paintComponent( ). Never means never! • If your code does something that requires repainting, • This is a hard rule to understand. Follow it anyway. call method repaint( ) • Always paint the entire picture, from scratch • Requests that paintComponent be called sometime in the • Don't create a Graphics object to draw with future, when convenient for underlying system window • only use the one given to you as a parameter of paintComponent( ) manager • and, don't save that object to reuse later! • This rule is bent in advanced graphics applications 10/9/2003 (c) 2001-3, University of Washington 06-17 10/9/2003 (c) 2001-3, University of Washington 06-18 CSE143 Au03 06-3

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend