1 / 54

Advanced GUIs and Graphics in Java

Learn about creating applets, exploring GUI components, handling key and mouse events, and using various layout managers in Java.

Download Presentation

Advanced GUIs and Graphics in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 13 Advanced GUIs and Graphics

  2. Chapter Objectives • Learn about applets • Explore the class Graphics • Learn about the class Font • Explore the class Color

  3. Chapter Objectives • Learn to use additional Layout managers • Become familiar with more GUI components • Learn how to create menu-based programs • Explore how to handle key and mouse events

  4. Inheritance Hierarchy of GUI Classes

  5. Constructors and Methods of the class Component

  6. Constructors and Methods of the class Component

  7. Constructors and Methods of the class Component

  8. Constructors and Methods of the class Container

  9. Applets • Applet: a Java program that is embedded within a Web page and executed by a Web browser • Create an applet by extending the class JApplet • class JApplet contained in package javax.swing

  10. Members of class JApplet

  11. Members of class JApplet

  12. Applets • No main method • Methods init, start, and paint guaranteed to be invoked in sequence • To develop an applet • Override any/all of the methods above

  13. Applet Methods • init Method • Initializes variables • Gets data from user • Places various GUI components • paint Method • Performs output

  14. Skeleton of a Java Applet import java.awt.Graphics; import javax.swing.JApplet; public classWelcomeApplet extends JApplet { }

  15. Applet Displaying Welcome Message //Welcome Applet import java.awt.Graphics; import javax.swing.JApplet; public class WelcomeApplet extends JApplet { public void paint(Graphics g) { super.paint(g); //Line 1 g.drawString(“Welcome to Java Programming”, 30, 30); //Lineƒ2 } }

  16. HTML to Run Applet

  17. class Font • Shows text in different fonts • Contained in package java.awt • Available fonts • Serif/SanSerif • Monospaced • Dialog/DialogInput • Arguments for constructor • String specifying the Font face name • int value specifying Font style • int value specifying Font size • Expressed in points (72 points = 1 inch)

  18. Constructors and Methods of the class Font

  19. class Color • Shows text in different colors • Changes background color of component • Contained in package java.awt

  20. Constructors of the class Color

  21. Methods of the class Color

  22. Methods of the class Color

  23. Constants Defined in the class Color

  24. class Graphics • Provides methods for drawing items such as lines, ovals, and rectangles on the screen • Contains methods to set the properties of graphic elements including clipping area, fonts, and colors • Contained in the package java.awt

  25. Constructors and Methods of the class Graphics

  26. Constructors and Methods for the class Graphics

  27. Constructors and Methods for the class Graphics

  28. Constructors and Methods for the class Graphics

  29. Applets Derived from JApplet No main method Uses init method Displayed by HTML Sets title in HTML Size set in HTML Applet closes when HTML doc closes GUI applications class extends JFrame Invokes main method Uses constructors Uses method setVisible Uses setTitle method Uses method setSize Closes with Exit button Differences Between Applets and GUI Applications

  30. Converting a GUI Application to an Applet • Change JFrame to JApplet • Change constructor to method init • Remove method calls such as setVisible, setTitle, setSize • Remove the method main • If applicable, remove Exit button/all code associated with it (e.g. action listener)

  31. Additional GUI Components • JTextArea • JCheckBox • JRadioButton • JComboBox • JList

  32. JTextArea • Can collect multiple lines of input from user • Can display multiple lines of output • Pressing Enter key separates lines of text • Each line ends with newline character ‘\n’ • Derived from class JTextComponent

  33. Constructors and Methods of class JTextArea

  34. Methods Inherited by class JTextArea from Parent class JTextComponent

  35. JTextArea Example

  36. JCheckBox • User selects from predefined values • Example of a toggle button • Clicking JCheckBox generates item event • Use interface ItemListener and its abstract method itemStateChanged to handle event

  37. Constructors and Methods of class JCheckBox

  38. Constructors and Methods of class JCheckBox

  39. Constructors and Methods of class JCheckBox

  40. JRadioButton • Created same way as check boxes • Placed in content pane of applet • Forces user to select only one radion button at a time • You create a button group to group radio buttons • Generates an ItemEvent • interface ItemListener and method itemStateChanged used to handle events

  41. Constructors and Methods of class JRadioButton

  42. Constructors and Methods of class JRadioButton

  43. Constructors and Methods of class JRadioButton

  44. JComboBox • Commonly known as a drop-down list • Used to select an item from a list of possibilities • Generates an ItemEvent • Event monitored by ItemListener • ItemListener invokes method itemStateChanged

  45. Constructors of class JComboBox

  46. Applet with JCheckBox, JComboBox, and JRadioButton

  47. Constructors of class JList

  48. Methods of class JList

  49. Layout Managers • FlowLayout • Default layout manager • Places components from left to right until no more items can be placed • Can align each line left, center, or right • Default alignment: LEFT • GridLayout • Similar to FlowLayout • All rows (columns) have same number of components • All components have the same size

  50. Layout Managers • BorderLayout • Items placed into one of 5 specific regions • NORTH/SOUTH • EAST/WEST • CENTER • NORTH and SOUTH components extend horizontally (completely span one edge to the other) • EAST and WEST components extend vertically between components in NORTH and SOUTH regions • CENTER component expands to occupy any unused regions

More Related