CIS 469/569 Java Programming

Some Highlights

Swing GUI Highlights

  • Swing GUI components are lightweight components, except for top-level containers JFrame, JApplet, JDialog, and JWindow; containment hierarchy and painting (see Java Tutorial from java.sun.com/docs/books for good reference);Object<--Component<--Container<--JComponent; JComponent superclass to most Swing components, e.g. JButton, JLabel, JList, JPanel
  • Seven steps in creating Swing GUI application (i.e. using a JFrame) or applet (i.e. using a JApplet); see JFCIAN, pgs. 6-7
  • JLabel methods setText, setIcon, setToolTipText; JTextField properties; columns, editable, selectedText, Font; JPasswordField-->JTextField-->JTextComponent-->JComponent; setEchoChar(char c) method of JPasswordField
  • JButton-->AbstractButton-->JComponent; JCheckBox-->ToggleButton-->AbstractButton;JRadioButton-->ToggleButton-->AbstractButton; JButton is a "command button"; generate ActionEvent; JCheckBox and JRadioButton are "state" buttons they generate ItemEvent; JRadioButtons often added to a ButtonGroup; properties of AbstractButton; BorderPainted, Text, Icon, RolloverIcon, HorizontalAllignment, VerticalAllignment, HorizontalTextPosition (relative to the icon);
  • JComboBox --> JComponent; JComboBox generates ItemEvent; constructor JComboBox(Object[] items);getSelectedIndex and getSelectedItem methods;
  • JList --> JComponent; setVisibleRowCount and setSelectionMode methods of JList; SINGLE_SELECTION, SINGLE_INTERVAL_SELECTION, MULTIPLE_INTERVAL_SELECTION constants of ListSelectionModel class, used as argument to setSelectionMode method; by default, no scrolling with a JList--call JScrollPane constructor with JList argument to provide for scrolling; JList generate ListSelectionEvent (direct subclass of EventObject)
  • Event Handling model changed after Java 1.0; in Java 1.0 model all events represented by single Event class (ch. 7 JFCIAN); Java 1.1 event handling model used in AWT GUI and Swing GUI: Object<--java.util.EventObject<--java.awt.AWTEvent; direct event subclasses of AWTEvent include ActionEvent , ItemEvent, and ComponentEvent; important getSource() method inherited from EventObject;
  • One Listener interface for each Event class; e.g. ActionListener, ItemListener, ListSelectionListener, ComponentListener; 2 programming steps in order to process an event: i) register event listener; an event listener is an object that implements a listener interface; this is done with the method addxxxListener; e.g. addActionListener; ii) implement event handler, i.e., give code for the method(s) in the listener interface; note frequent use of inner classes for doing this;
  • Every JComponent has listenerList field of type EventListenerList; each element in the list has a reference to Listener object and type of listener; when event is generated, the component is given a unique eventID; using this eventID, the event is dispatched to the correct event listener by calling the event handling method; note that each event source can have multiple listeners registered on it and a single listener can register with multiple event sources;
  • MouseEvent-->InputEvent-->ComponentEvent; MouseListener (5 methods); MouseMotionListener(2 methods); each of these 7 methods takes a MouseEvent argument; MouseAdapter and MouseMotionAdapter classes--why are they useful?; methods in MouseListener: mousePressed, mouseClicked, mouseReleased, mouseEntered, mouseExited; methods in MouseMotionListener: mouseDragged, mouseMoved; --- addMouseListener, addMouseMotionListener are event registration methods in class Component;
  • Methods in class MouseEvent: getX(), getY(), getClickCount();
  • Layout managers: FlowLayout: constructors: no arg., one-arg: specify alignment either FlowLayout.CENTER, FlowLayout.RIGHT or FlowLayout.LEFT; 3 arg constructor for specifiying alignment, horizontal gap between components, and vertical gap between components; FlowLayout is the default for Applets, Panels, and JPanels;
  • BorderLayout: no arg. constructor gives no gaps; 2 arg. constructor for specifying gaps; BorderLayout is the default for JFrames and JApplets; North, South, East, West, and Center (constants in BorderLayout class)
  • GridLayout : 2 arg constructor; 4 arg. constructor for specifying horizontal and vertical gaps; every Component in a GridLayout has same width and height;
  • methods in class Container: setLayout(LayoutManager mgr), validate(): causes a container to lay out its components again after the components have been added or modified; method in class Component setVisible(boolean b): used instead of the deprecated show() method, it can be used to show or hide the component;
  • Other Swing components (see Chap. 14, D. and D., 5th ed.): JTextArea, JScrollPane, JPanel, JSlider;
  • If creating a Swing GUI with Swing GUI components: if you are also doing painting, create a customized subclass of JPanel and override the paintComponent(Graphics g) method (what should be the first line in this method?); see Section 14.3 and Fig. 14.4; also read section 14.4;
  • Using menus with Frames: JMenuBar-->JComponent; setJMenuBar(JMenuBar menubar) method of JFrame and JApplet; JMenu-->JMenuItem-->AbstractButton; JCheckBoxMenuItem and JRadioButtonMenuItem are direct subclasses of JMenuItem; Fig. 14.9 example; JPopupMenus (fig. 14.10 e.g.)
  • Pluggable Look-And-Feel (section 14.9 -- Fig. 14.11, 5th ed. D. and D.)

Some Graphics/drawing Highlights

  • java.awt.Graphics is an abstract class; Object is its direct superclass; a derived class of Graphics is provided for each different implementation of Java on different platforms, since drawing is performed differently on each platform that support Java; a Graphics object manages a "graphics context" that encapsulates state information that includes the font, color, and Component on which to draw;
  • GUI--chapters 12-14 (in Deitel and Deitel, 5th ed.); java.awt.Color class, Color constructors, Color constants; getColor(), setColor(Color c) methods of Graphics class; JColorChooser class, method showDialog();
  • Coordinate system used for drawing; methods in Graphics class for drawing: drawString, setColor, setFont, drawLine, drawRect, fillRect, drawOval, fillOval, drawRoundRect, fillRoundRect, drawArc, fillArc, drawPolygon, fillPolygon; see examples in Deitel and Deitel; setBackground(Color c) method in Component class;
  • javax.swing.JFrame-->Frame-->Window-->Container-->Component-->Object; Typical constructor in class that extends JFrame includes super(" "); setSize(...,...); setVisible(true): makes the window (i.e. JFrame) visible; if you are only doing painting, you can paint directly on the JFrame; if you are adding components to the JFrame, add them to the content pane of its JRootPane; to do this use method getContentPane() that returns a Container to which you can add components; setDefaultCloseOperation method in JFrame -- argument can be an int constant in JFrame or WindowConstants interface, e.g. JFrame.EXIT_ON_CLOSE
  • java.awt.Font-->Object; Font constructor, 3 args., name, style, size (pitch); style constants: BOLD, ITALIC, PLAIN; java.awt.FontMetrics-->Object; FontMetrics (Font font) constructor; methods of FontMetrics:getAscent, getDescent, getLeading, getHeight;
  • Java2D API; java.awt.Graphics2D --> Graphics; using 2D graphics in paint method: cast the Graphics object g to Graphics2D; Java2D Shapes : in java.awt.geom: Line2D, Ellipse2D, Rectangle2D, RoundRectangle2D, Arc2D, Line2D; static inner classes of these classes: xxx.Double and xxx.Float;
  • Graphics2D methods setPaint(Paint paint) and setStroke(Stroke s); BasicStroke isa Stroke; Color, GradientPaint, TexturePaint all implement java.awt.Paint; TexturePaint constructor that takes a BufferedImage argument and a Shape argument can be used for drawing a "wallpaper";