COP3252 Advanced Java Programming - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

COP3252 Advanced Java Programming

Description:

Java includes a JMenuBar to allow us to create application menus ... We then create our menus. ... Creates an unselected radio button with the specified text. ... – PowerPoint PPT presentation

Number of Views:83
Avg rating:3.0/5.0
Slides: 27
Provided by: UoD
Category:

less

Transcript and Presenter's Notes

Title: COP3252 Advanced Java Programming


1
COP3252Advanced Java Programming
  • 18-Mar-08
  • Lecture Set 16
  • GridLayout, Menus, JRadioButtons, Graphics and
    Java2D

2
Gridlayout
  • Gridlayout positions a containers components in
    a rectangular grid
  • Divided into equal sized rectangles
  • One component is placed in each rectangle
  • Can be added to a JToolBar, JFrame, JPanel, etc.

3
GridLayout Example
  • The following example code will produce a 2
    column, three row grid.
  • A picture of the result is included on the next
    slide.
  • getContentPane().setLayout(new GridLayout(3,2))
  • getContentPane().add(new Button("1"))
  • getContentPane().add(new Button("2"))
  • getContentPane().add(new Button("3"))
  • getContentPane().add(new Button("4"))
  • getContentPane().add(new Button("5"))
  • getContentPane().add(new Button("6"))

4
GridLayout
  • Using the example code, the program will produce
    the following display
  • http//java.sun.com/j2se/1.5.0/docs/api/java/awt/G
    ridLayout.html

5
Menus
  • The File, Edit, Help, etc bar at the top of most
    programs is called a MenuBar.
  • Java includes a JMenuBar to allow us to create
    application menus
  • The elements of a JMenuBar are called JMenus
  • The items (or choices) in a JMenu are called
    JMenuItems

6
Menus
  • We first create the JMenuBar
  • We must then tell our JFrame to use the MenuBar
  • Use the command setJMenuBar(menuBar)
  • menubar is whatever you called your JMenuBar
  • We then create our menus.
  • We then add our MenuItems to the menus (and add
    actions to the MenuItems)
  • Finally, we add our menus to the MenuBar.

7
Menu example
  • //Create the JMenuBar
  • JMenuBar menuBar new JMenuBar()
  • //Set the created bar to be the menu bar for this
    frame
  • setJMenuBar(menuBar)
  • //Create a JMenu called Test
  • JMenu menu new JMenu(Test)
  • //Add a JMenuItem called item1 to the JMenu
  • menu.add(new JMenuItem(item1))
  • //Add the menu to the JMenuBar
  • menuBar.add(menu)

8
Making an interactive menu
  • By default, clicking on a menu item will not do
    anything
  • You must assign an action to each button
    (JMenuItem)
  • Menu actions can be assigned the same way as any
    other action in JAVA

9
Example
  • See JFrameMenuAction.java for a live code example

10
JRadioButtons
  • Radio Button An item that can be selected or
    deselected, and which displays its state to the
    user.
  • Can be grouped together so that only one button
    at a time can be selected.
  • Often used to allow the user to easily choose
    between a set of options.

11
JRadioButton Constructors
  • JRadioButton() Creates an initially unselected
    radio button with no set text.
  • JRadioButton(String text) Creates an unselected
    radio button with the specified text.
  • JRadioButton(String text, boolean selected)
    Creates a radio button with the specified text
    and selection state.

12
Grouping JRadioButtons
  • You must group related buttons so that the user
    can only select one at a time
  • Use a ButtonGroup to setup a grouping
  • Example
  • ButtonGroup group1 new ButtonGroup()
  • group1.add(radioButton1)
  • group1.add(radioButton2)
  • etc

13
Which button is selected?
  • You will need a way to figure out which button is
    selected.
  • JRadioButton has a method called isSelected()
    that returns a boolean value to let you know
    whether the button is selected or not.
  • See the example for usage.

14
JRadioButton Example
  • File JRadioButtonExample.java
  • You will see
  • Hitting the go button will cause the program to
    inform the user which button is selected (using a
    JTextField).

15
Basic Graphics and Java2D
  • We will discuss the following
  • Controlling fonts
  • Class Font, FontMetrics
  • Controlling colors
  • Class Color
  • Drawing two-dimensional shapes
  • Class Graphics2D, Polygoc, BasicStroke,
    GradientPaint, TexturePaint

16
Javas Coordinate System
  • Scheme for identifying every point on the screen
  • Standard x-y style coordinate pair
  • 0,0 starts are upper-left of GUI component
  • Measured in pixels (or Picture Elements)

17
Class Graphics
  • Abstract class
  • Graphics objects can not be instantiated
  • Portability drawing is performed differently on
    different platforms
  • When Java is implemented, subclass of graphics is
    created that implements that drawing
    capabilities.
  • Implementation is hidden by class Graphics
  • Graphics supplies interface so that the
    programmer sees it at platform-independent.

18
paintComponent method
  • Class Component is superclass of JComponent
  • Contains method paintComponent that takes a
    Graphics object as an argument
  • Object is passed to the paintComponent method by
    the system when a LW Swing component needs to be
    repainted.
  • paintComponent is seldom called directly by the
    programmer . Usually event-driven
  • When GUI app executes, paintComponent is called
    for each LW component as the GUI is displayed.
  • To call it again, event must occur (cover/uncover
    components)
  • To force execution, call repaint method

19
Drawing lines, rectangles and ovals
  • Graphics class includes several methods for
    drawing basic 2D shapes
  • Each method requires a non-negative width and
    height value
  • See http//java.sun.com/javase/6/docs/api/java/awt
    /Graphics.html

20
PaintComponent example
  • See JPanelGraphics.java and JPanelGraphicsTest.jav
    a

21
Working with Fonts
  • Class Fonts constructor takes three arguments
  • Font name
  • Any font currently supported by the system on
    which the program is running (ex Monospaced,
    Serif, etc).
  • Font style
  • One of the static fields of class Font
  • Font.PLAIN, Font.BOLD, Font.ITALIC
  • Can be used in combination
  • Font.BOLDFont.ITALIC
  • Font Size
  • int representing point size

22
Working with Fonts
  • See FontJPanel.java and FontTest.java

23
The Java 2D API
  • Provides advanced 2D graphics capabilities over
    that of the Graphics class
  • Graphics2D is an abstract subclass of class
    Graphics
  • Includes all capabilities of Graphics class
    discussed earlier
  • To access, we must cast the Graphics reference
    (g) passed to paintComponent into a Graphics 2D
    reference
  • Graphics2D g2d (Graphics2D)g

24
Java2D Basic Examples
  • Refer to pages 636-638 of the course textbook for
    very basic examples
  • hopefully on Thursday I can show you some
    advanced examples

25
Java2D Paths
  • General Path shape constructed from straight
    lines and complex curves.
  • Represented with an object of class GeneralPath
  • Allows you to define your own shapes

26
Java2D Paths
  • See Path.java and PathTest.java
Write a Comment
User Comments (0)
About PowerShow.com