Docsity

Applets and Advanced Graphics - Java Programming Language - Lecture Slides, Slides of Computer Science

Toggle sidebar
bg1
Chapter 10: Applets and
Advanced Graphics
• The Applet Class
• The <applet> HTML Tag
•Passing Parameters to Applets
•Conversions Between Applications and Applets
•Running a Program as an Applet and as an
Application
•Handling the Mouse Event
•Handling the Keyboard Event
•Model dynamic behavior using sequence
diagrams and statecharts diagrams
•Advanced Layout (CardLayout and GridBagLayout
and using No Layout Manager) (Optional)
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24

Partial preview of the text

Download Applets and Advanced Graphics - Java Programming Language - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Chapter 10: Applets and

Advanced Graphics

  • The Applet Class
  • The HTML Tag
  • Passing Parameters to Applets
  • Conversions Between Applications and Applets
  • Running a Program as an Applet and as an Application
  • Handling the Mouse Event
  • Handling the Keyboard Event
  • Model dynamic behavior using sequence diagrams and statecharts diagrams
  • Advanced Layout (CardLayout and GridBagLayout and using No Layout Manager) (Optional)
  • The Applet Class

    public class MyApplet extends Applet { public void init() { ... } public void start() { ... } public void stop() { ... } public void destroy() { ... } //your other methods }

    The init() Method

    • Invoked when the applet is first loaded and again if the applet is reloaded.
    • Common functions implemented in this method include
      • creating threads,
      • loading images,
      • setting up user-interface components,
      • getting parameters from the tag in the HTML page.

        The start() Method

        • Invoked after the init() method is executed; also called whenever the applet becomes active again after a period of inactivity
        • For example, when the user returns to the page containing the applet after surfing other Web pages.
        • Functionality might include restarting threads (for example, to resume an animation) or simply telling the applet to run

        again.

        The destroy() Method

        • Invoked when the browser exits normally to inform the applet that it is no longer needed and that it should release any resources it has allocated.
        • Usually, you will not need to override this method unless you need to release specific resources, such as threads that the applet created.

        Example 10.1 Using Applets

        • Objective: Compute mortgages. The applet enables the user to enter the annual interest rate, the number of years, and the loan amount. Click the Compute Mortgage button, and the applet displays the monthly payment and the total payment.

        MortgageApplet

        Run Applet Viewer

        The HTML Tag

        <applet code=classfilename.class width=applet_viewing_width_in_pixels height=applet_viewing_height_in_pixels [archive=archivefile] [codebase=applet_url] [vspace=vertical_margin] [hspace=horizontal_margin] [align=applet_alignment] [alt=alternative_text]

        Running Applets in Java Plug-In

        Why to Use Java Plug-In?

        Java Plug-in enables Web browsers

        to run Java applets consistently on

        all the platforms.

        Passing Parameters to

        Applets

        <applet code = "DisplayMessage.class" width = 200 height = 50 alt="You must have a Java-enabled browser to view the applet"

        Example 10.2 Passing

        Parameters to Java Applets

        • Objective: Display a message at a specified location. The message and the location (x, y) are obtained from the HTML source.

        Run Applet Viewer

        DisplayMessage

        Example 10.3 Converting

        Applets into Applications

        • Objective: Convert MortgageApplet (from

        Example 10.1) to a Java application.

        Run

        MortgageApplication

        Example 10.4 Converting

        Applications into Applets

        • Objective: Convert a Java application that performs simple numeric calculations ( MenuDemo, from Example 9.11) into a Java applet.

        Run Applet Viewer

        AppletMenuDemo

        Handling Mouse Events

        • Java provides two listener interfaces,

        MouseListener and MouseMotionListener, to

        handle mouse events.

        • The MouseListener listens for actions such as when the mouse is pressed, released, entered, exited, or clicked.
        • The MouseMotionListener listens for actions such as dragging or moving the mouse.

        Example 10.

        Moving Message Using Mouse

        • Objective: Create a program to display a message in a panel. You can use the mouse to move the message. The message moves as the mouse drags and is always displayed at the mouse point.

        Run as Application

        MoveMessageDemo

        Run as Applet