lecture25-applets - Lecture 25 Graphics in Java Lecture 25...

This preview shows page 1 - 11 out of 30 pages.

Lecture 25: Graphics in JavaLecture 25: Graphics in JavaCS 170, Section 0003 December 20093 December 200912/3/2009CS170, Section 000, Fall 20091
Lecture PlanHomework 8 details review¾More on GUIs¾More on Java GUI API (Ch. 13): ¾Essential Swing components and layouts¾Putting it all together: TicTacToe¾GUIiilHW8¾GUI very similar to HW8Hk 8tiHomework 8 questionsCS170, Section 000, Fall 20092
HW8: Game of LifeA mathematical game invented by mathematician John Conway in 1970Game rulesA dead cell with exactly three live neighbors becomes a live cell (birth).A live cell with two or three live neighbors stays alive (survival).In all other cases, a cell dies or remains dead (overcrowding or loneliness).Resources and demosResources and demosImplementationImplementationUse Timer class for animationUse two dimensional arrays for cell updates, then drawImage!¾Even better: draw each cell separately (as JPanel like TicTacToe example¾Even better: draw each cell separately (as JPanel like TicTacToe example – stay tuned)!
GUI Class Hierarchy (Swing)Dimension HeavyweightClasses in the java.awt package LayoutManagerFont FontMetrics Heavyweight 1CGraphics Object Color CiPanel Applet FWi dJApplet JFComponent ContainerFrameDialog Window JFrameJDialog * JComponent Swing Components in the javax.swing package 4Lightweight
Container ClassesDimension FontHeavyweight Classes in the java.awt package 1LayoutManagerFont FontMetrics ObjectColorPanelAppletJApplet1Component Graphics Object Color ContainerPanelAppletFrameWindow JAppletJFrameDialog JDialog * JComponent Swing Components in the javax.swing package JPanel Container classes can contain other GUI components.5Lightweight
Swing GUI Components JMenuItem JCheckBoxMenuItemAbJMenu AbstractButtonJRadioButtonMenuItemJToggleButton JCheckBox JButtonJComponent JRadioButtonJTextFieldJTextComponent JPasswordField JEditorPane JComboBoxJListJOptionPaneJScrollBarJSliderJTextArea JPanelJLabelJComboBoxJLayeredPane JListJMenuBarJOptionPaneJPopupMenuJFileChooserJScrollBarJScrollPaneJSeparatorJSplitPaneJSliderJTabbedPaneJToolBarJToolTipJRootPane JPanelJColorChooserJLabel6JInternalFrameJProgressBarJTable JTableHeaderJTree JSpinner
FramesFrame is a window that is not contained inside another window. Frame is the basis to contain other user interface components in Java GUI applications.The JFrame class can be used to createThe JFrame class can be used to create windows. For Swing GUI programs, use JFrame class to create widows.7
Creating Framesimport javax.swing.*;public class MyFrame {public static void main(String[] args) {JFrame frame = new JFrame("Test Frame");frame.setSize(400, 300);frame.setVisible(true);frame setDefaultCloseOperation(frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}}8
Adding Components into a Frame// Add a button into the frameframe.getContentPane().add(new JButton("OK"));Title barnew JButton( OK ));Title barContent pane9
Layout Managers

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture