Title: TCU CoSc 10403 Introduction Programming with Java
1TCU CoSc 10403 Introduction Programming(with
Java)
- Program Development Environment
2Reference
- Lab and Eclipse Introduction
- On course web page
- http//www.cs.tcu.edu/10403/
- Help sessions - taking place Thursday (TTC 353)!!
- To work through the introduction to Eclipse,
TURNIN, and the TCU web environment - Work through introduction carefully
- Avoid headaches later!
3Basic 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!
4Software Development Environment
COSC10403
bin
5Required 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 Lab3.java to your instructor using
TURNIN. - Move your Lab3.html file and associated
Lab3.class file into the correspondingly named
folder in your wwwpub directory. - Use Internet Explorer to verify that your program
runs on the web. - Point your browser to
- http//stuwww.tcu.edu/yourname/10403labs.html
6Basic Steps
- Publishing your applet (class, html files)
- COPY files into your wwwpub folder
- Labx.html file
- bin/Labx.class file(s)
- Test using IE
- http//stuwww.tcu.edu/yourname/10403labs.html
7Turning 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. j.r.smith_at_tcu.edu)
- Navigate to your Labx.java file and select it.
- KEEP YOUR RECEIPT NUMBER!
- 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, - www.cs.tcu.edu/classinfo
8Java 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
9The Programming Process
10A 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.
11An 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..
12Java Uses Both a Compiler and Interpreter
- The Java compiler translates 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 interpreter then reads a short segment
of Java bytecode, translates that segment into
machine language and executes it. This process
is repeated as the program executes.
13The 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.
14Basic 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
15Anatomy 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()
add(helloL)
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 applets 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.
16A Note about the Applets 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).
17Applets - 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 ltapplet gt tag.
18What 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. -
Javas runtime system in the browser
Relationship between an applet and the Applet
class. Other methods include size(),
getImage(),getAudioClip(), play(), loop(), etc.
(22 methods in all)
Applet
Other methods fields
19Applet 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
20Anatomy 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()
21 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. - Youll 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)
22Applet lifecycle The browser
1. reads the linked to HTML page and looks for
any ltAPPLETgt tags. 2. parses the ltAPPLETgt 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.
23An Applet at Work
- 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.....")
24What 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.
25Overriding 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
26What an applet cant 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.
27Importing 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 java.applet.Applet
- 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 dont actually cause code to be
imported - but rather tell the compiler where to
go to locate the code.
28More 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).
29Still 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.
30The Java Applet extends Clause
Builtin classes
Made visible by import statements
Your program
Builtin classes
Applet class inherited by extends statement
Your program
More about inheritance later.