tcu cosc 10403 introduction programming with java n.
Skip this Video
Loading SlideShow in 5 Seconds..
TCU CoSc 10403 Introduction Programming (with Java) PowerPoint Presentation
Download Presentation
TCU CoSc 10403 Introduction Programming (with Java)

Loading in 2 Seconds...

play fullscreen
1 / 30

TCU CoSc 10403 Introduction Programming (with Java) - PowerPoint PPT Presentation

  • Uploaded on

TCU CoSc 10403 Introduction Programming (with Java). Program Development Environment. Reference. Lab and Eclipse Introduction On course web page Help sessions - taking place Tuesday (TTC 353)!!

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'TCU CoSc 10403 Introduction Programming (with Java)' - westbrook

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
  • Lab and Eclipse Introduction
    • On course web page
  • Help sessions - taking place Tuesday (TTC 353)!!
    • To work through the introduction to Eclipse, TURNIN, and the TCU web environment
  • Work through introduction carefully
    • Avoid headaches later!
basic steps
Basic Steps
  • Setting up your environment
    • Tools: IE, Eclipse, TURNIN
    • U: drive
      • Where your applets will be developed
    • wwwpub
      • Object code and associated html file will be posted here

*** Watch your disk space quota!

required software development environment

You must have a “wwwpub” folder in your main “U” drive directory. (May incur a 1-hour delay)

  • Java programs are developed and reside at the top-level of your “U” drive (folder names should be chosen as: “Lab0Project”, “Lab1Project”, … , “Lab8Project”)
  • Your “wwwpub” sub-directory should have:
    • A “10403Labs.html” file - will be provided!
    • Ten folders (named “Lab0”, “Lab1”, … “Lab9”) - will be provided!
  • When a program is finished (say Lab3). From your “Lab3Project” folder:
    • Submit your “” to your instructor using TURNIN.
    • Move your “Lab3.bin” folder and associated “ Lab3.class” file into the correspondingly named folder in your “wwwpub” directory.
    • Construct a Lab3.html file.
  • 6. Use Internet Explorer to verify that your program runs on the web.
    • Point your browser to:

Required Software Development Environment

basic steps1
Basic Steps
  • Publishing your applet (class, html files)
    • COPY files into your wwwpub folder
      • Labx.html file
      • bin/Labx.class file(s)
    • Test using IE
turning in your java program to your instructor
Turning in your Java program to your instructor
  • Submitting your Java source (*.java)
    • TURNIN
      • Select Course (e.g., 10403-0xx)
      • Select Labx (e.g., Lab0)
      • Enter your TCU email (e.g.
      • Navigate to your file and select it.
      • Let your instructor know if you do not get your graded lab returned to you.
      • TURNIN may be obtained by following links off of the course website,

java overview
Java Overview
  • Applets
    • A Java applet is a program intended to be transported over the Web and executed using a web browser
    • html file + java class files (bytecodes)
    • Note that Java byte code (not source code) is linked to an HTML document and sent across the web. Thus, a version of the Java interpreter must be embedded in the web browser to execute the applet once it reaches its destination.
    • An applet also can be executed using the appletviewer tool of the Java Software Development Kit
  • Applications
    • Executes outside a browser
    • Covered towards the end of this course
a compiler
A Compiler
  • A compiler is a program that translates code from one language to an equivalent code in another language. (either to assembler language or to ultimate machine language, which can be executed)
    • The original code is called source-code.
    • The language into which it is translated is called the target language.
    • For many traditional compilers, the source code is translated directly into a particular machine language.
      • In that case, the translation process occurs once and the resulting executable program can be run whenever needed.
    • In other cases, assembler code is produced and the assembler is called to translate this lower level language code into machine language.
an interpreter
An Interpreter
  • An interpreter is similar to a compiler but has important differences…
    • An interpreter interleaves the translation and execution activities.
      • A small part of the source code, such as one statement, is translated and executed.
      • Then another part is translated and executed, and so on.
    • This eliminates the need for a separate compilation phase; however, the program runs more slowly because the translation process occurs during each execution.
      • Each statement is translated, then executed immediately, rather than translating the entire program and then executing the translated code..
java uses both a compiler and interpreter
Java Uses Both a Compiler and Interpreter
  • The Java compilertranslates Java source code into Java ‘bytecode.’
    • Bytecode is a representation of the program in a low-level code similar to machine language code.
  • The Java interpreterthen reads a short segment of Java bytecode,translatesthat segment into machine languageand executes it. This process is repeated as the program executes.
the advantage of java bytecode
The Advantage of Java Bytecode
  • The difference between Java bytecode and true machine language code is that Java bytecode is not tied to any particular processor type. Machine Independent!
  • Most compilers generate machine code (or assembler code) that can only be executed on a particular machine / machine series….
    • This makes Java “architecture neutral” Thus this feature makes Java easily portable from one machine to another.
      • All you need is a Java interpreter or bytecode compiler for each processor type on which the bytecode is to be executed.

Basic Java Applet Structure

An applet is a small program that is intended to be embedded inside another application such as a browser.

The JApplet class provides a standard interface between applets and their environment. The JApplet hierarchy is as shown to the right:

import package1 ;

import package2 ;


public class name extends JApplet


variables and methods


anatomy of a java applet
Anatomy of a Java Applet

Comments for programmers

are preceded by // or are

surrounded by /* …*/

Note: ALL Java programs begin life as a class - more about this later!

/* Trivial applet that displays a string */

import javax.swing.*;

import java.awt.*;

public class HelloWorld extends JApplet


JLabel helloL = new JLabel(“Hello World”);

public void init()





Informs the Java compiler that the program will be using

already existing code found in the java.swing and java.awt packages. The java.swing class contains the code that is used to implement java applets.

Defines a new JApplet, HelloWorld, that,

by extending the JApplet class, inherits all

methods defined in the Applet class.

Tells the applet to display the string, “Hello World”, on the

applet’s display area

One of the methods defined in the Applet class is an init() method. By including an init() method

here, the programmer is indicating that he wishes to override the inherited init() method and to

substitute his/her own in its place - to be executed when the applet is “initialized” to be run by the browser.

a note about the applet s html file
A Note about the Applet’s html file

The program that will actually run via the browser - created as a consequence of compiling the “HelloWorld” program into JavaByte code using Eclipse.

200 x 200

250 x 100

Note: the width and height parameters are VERY important. If set too small, the applet

will not display properly on the web when the html program is executed!!! May appear

to run OK when viewed with Eclipse (applet viewer).

applets from source code to display


- - - - - - ----

- - - - -

- - - -

- - - ---- -- -





- - - -

- - - ------ -

- - - -

- -- -- -

- - - -----




Applet in web page

Applets - from source code to display
  • To use an applet in a web page - two tasks to perform:
  • Compile the text-based Java source code into Java bytecode.

2. Prepare an html document which describes the web page in which the applet will run. Done with an <applet …> tag.

what does the browser do for us



Useful methods

for parameters,

images, audio,





What Does the Browser Do For Us?

An applet is started up by the Java runtime system, which then looks to call one of the following methods:

init() - called once to provide the initial setting up of the applet

start() - the applet (re)starts to execute when it becomes visible on the current browser page.

paint() - called after init() and as part of an update() when the browser window needs to be re(drawn)

stop() - the applet is suspended when it is no longer visible on the current browser page). Start() is called when it becomes visible again.

destroy() - called once so that the applet can release any resources it may have before the viewer ends or the browser moves to a different page.



system in

the browser

Relationship; between an applet

and the Applet class.

Other methods include: size(),


play(), loop(), etc. (22 methods

in all)


Other methods

& fields

applet anatomy 101
Applet Anatomy 101

We will want to override some of these with our own methods:

  • init()
    • instantiate GUI components
    • Register them with listeners
    • Add components to applet
      • Simple AWT - add components to Applet directly
      • Swing - getContentPane of JApplet and add to it
  • start()
    • Called automatically after init
    • Called anytime a user returns to the page containing the applet
    • Typical use – reactivation of a thread
  • stop()
    • Override if you want to stop time-consuming activity when a user is off the page (animation, audio, threads)
    • Typical use - suspend a thread, suspend playing of audio
anatomy of an applet continued
Anatomy of an Applet (continued)
  • destroy()
    • called automatically when browser shuts down
    • typically do not need to override
  • paint()
    • used to display graphics
    • called in special ways:
      • Automatically called by browser
      • User can invoke by calling repaint()

Example of Use

  • Say we have an applet that plays a video clip:
    • The init() method might draw the controls and start loading the video file.
    • The start() method would wait until the file was loaded, and then start playing it.
    • The stop() method would pause the video, but not rewind it.
    • If the start() method were called again, the video would pick up where it left off; it would not start over from the beginning. However, if destroy() were called and then init(), the video would start over from the beginning.
  • You’ll most often write an init() method
  • Normally, you will not call any of these directly (the browser (e.g., Internet Explorer) will when it needs to)

Applet lifecycle: The browser

1. reads the linked to HTML page and looks for any <APPLET> tags.

2. parses the <APPLET> tag to find the CODE and possibly CODEBASE attribute.

3. downloads the .class file for the applet from the URL found in the last step.

4. converts the raw bytes downloaded into a Java class, that is a java.lang.Class object.

5. instantiates the applet class to form an applet object.

6. calls the applet's init() method.

7. calls the applet's start() method.

8. While the applet is running, the browser passes any events intended for the applet, e.g. mouse clicks, key presses, etc., to the applet's handleEvent() method. Update events are used to tell the applet that it needs to repaint itself.

9. calls the applet's stop() method.

10. calls the applet's destroy() method.

an applet at work
An Applet at Work

1st run:

import java.applet.Applet;

import java.awt.*;

import javax.swing.*;

public class Simple extends JApplet


int x = 15, y = 15;

public void init()

{ System.out.println("initializing."); }

public void start()

{ System.out.println("starting....."); }

public void stop()

{ System.out.println("stoping......"); }

public void destroy()

{ System.out.println("destroying.....");}

public void paint(Graphics g)

{ System.out.println("repainting......");

//Draw a string on the applet's display

g.drawString("COSC 10403",x,y);

x = x + 30;

y = y + 30;



1st resize:


2nd resize:

After more

Resizing &

Clicking on the

“go away” box:


What An Applet Can Do

  • Draw pictures on a web page
  • Create a new window and draw in it.
  • Play sounds.
  • Receive input from the user through the keyboard or the mouse.
  • Make a network connection to the server from which it came and can send to and receive arbitrary data from that server.

Overriding methods

  • All applets, inheriting from the Applet class have init(), start(), stop(), paint(), and destroy() methods (and others)
  • Subclasses (yours!) may override these methods to accomplish certain tasks at certain times
    • We have already written Java code to override the paint() method.
    • We will almost always do so with the init() method

What an applet can’t do

  • Write data on any of the host's disks.
  • Read data from the host's disks without the user's permission.
    • In some environments, notably Netscape, an applet cannot read data from the user's disks even with permission.
  • Delete files
  • Read from or write to arbitrary blocks of memory, even on a non-memory-protected operating system like the MacOS. All memory access is strictly controlled.
  • Make a network connection to a host on the Internet other than the one from which it was downloaded.
  • Call the native API directly (though Java API calls may eventually lead back to native API calls).
  • Introduce a virus or trojan horse into the host system.
  • An applet is not supposed to be able to crash the host system.
    • However in practice Java isn't quite stable enough to make this claim yet.
importing classes and packages
Importing Classes and Packages
  • A Java program is never entirely self-contained but instead must execute in the “universe” provided by the Java runtime environment.
  • Java contains many predefined pieces called classes that are grouped together into categories of related classes called packages.
  • A package is really a directory of subdirectories (where

each subdirectory contains all the information relating

to a particular class).

  • The 1st connection with the Java world is accomplished through the two import statements appearing at the start of the program.

import java.awt.Graphics;

import javax.swing.*;;

  • The statements direct Java to make the Graphics class and the Applet class visible to the HelloWorld program. It is important to note that the statements don’t actually cause code to be imported - but rather tell the compiler where to go to locate the code.
more about importing classes and packages
More about Importing Classes and Packages
  • If you removed the first two lines, the applet could still compile and run, but only if you changed the rest of the code like this:

public class HelloWorld extends java.applet.Applet


public void paint(java.awt.Graphics g)


g.drawString("Hello world!", 30, 30);



  • Importing the Applet and Graphics classes lets the program refer to them later without the need for prefixes.
  • The java.applet package contains classes that are essential to Java applets. The java.awt package contains the most frequently used classes in the Abstract Window Toolkit (AWT), which provides the Java graphical user interface (GUI).
still more about importing classes and packages
Still More about Importing Classes and Packages
  • Besides importing individual classes, you can also import entire packages by writing your import statements like:

import java.applet.*;

import java.awt.*;

import javax.swing.*;

  • In this case, the (“*”) is treated as a wildcard character.
  • To Java, the overall effect is the same.
  • Does not add any overhead to the running of the applet and minimizes the chance of making mistakes by not importing a needed class.
the java applet extends clause


Builtin classes

Your program

The Java Applet extends Clause

Builtin classes

Made visible by import statements

Your program

Builtin classes

Applet class

inherited by extends


Your program

*** More about inheritance later.