Java

Compiled versus interpreted?

Java is object-oriented

Java has several standard libraries

What is the JDK?

Java Application Programmer's Interface (API) Documentation

Compiling and running your first Java applet

Your first look at an Applet!

Your first look at a different Applet!

Your last look before creating a different Applet!

Creating a different Applet!

Variables (objects) declared via:

Class varname = new Constructor;

Button pressme = new Button ("Press here");

Data Types

Expressions and Operators...

Java has if and if...else just like JavaScript

JAVA programming basics...

Even more JAVA programming basics...

Yet even more JAVA programming basics...

Still Expressing and operating...

Characters

char middy; middy = 'k'; char quit = 'Q'; Notice: Single quotes used for character Double quotes used for strings Arrays

int [] grade = new int[10]; grade[0]=...; ... grade[9]=...; for (i=0; i<10; i++) { grade[i]=....; } int [] floor = {17, 24, 33, 19, 26}; same as... floor[0]=17; floor[1]=24; floor[2]=33; floor[3]=19; floor[4]=26; Button [] stuff = new Button[5]; stuff[0] = new Button ("balance"); ... stuff[4] = new Button ("deposit"); for (i=0; i<5; i++) { stuff[i]= new Button ("push me"); } Character Strings

In Java, array of characters is NOT a character string!

char [] name = {'E','l','w','o','o','d'};

CANNOT!
add(new Label(name));

String class in java.lang

Constructors

String(); String firstname; String("string"); String firstName = new String ("Elwood"); ...same as... String firstName = "Elwood"; String (String); String newName = new String (firstName); String (char []); String newName = new String (name); add(new Label(newName)); Methods

char charAt(int) firstName.charAt(2) is 'w' String concat(String) String lastName = "Scuggins"; String fullname; fullName = firstName.concat(lastName); ...same as... fullName = firstName + lastName; fullName is "ElwoodScuggins" boolean equals(String) equals compares contents == compares location firstName.equals(newName) is true firstName == newName is false boolean equalsIgnoreCase(String) int length() firstName.length() is 6 char[] toCharArray() firstName.toCharArray() is {'E','l','w','o','o','d'} String toLowerCase() String toUpperCase() String trim() removes leading and trailing whitespace inText = " new balance please " trimText = inText.trim(); trimText is "new balance please" Applet methods:

init ()
called only when applet first created

start ()
called after init and whenever applet is stopped and re-started

paint (Graphics g)
called after init and start and whenever screen needs to be redrawn

The HTML file can supply one or more parameters to the Applet

HTML...
<PARAM NAME="frog" VALUE="ribbet">
Java...
String getParameter (String);
getParameter ("frog");
returns the string "ribbet"

HTML...
<PARAM NAME="buttonwords" VALUE="Press here">
Java...
Button pushy = new Button (getParameter ("buttonwords"));

And with the use of JavaScript these parameters can even be dynamic!

Applets can contain Panels

All components that can contain other components have a LayoutManager

FlowLayout

default
arranges components in rows left-to-right top-to-bottom
each component is its natural size
Each row is centered
Five pixels between components and between rows

setLayout(new FlowLayout());
setLayout(new FlowLayout(int alignment);
alignment may be FlowLayout.LEFT, FlowLayout.RIGHT, or FlowLayout.CENTER (default)
setLayout(new FlowLayout(int alignment, int hGap, int vGap);

BorderLayout

divides window into North, South, East, West, Center sections

North, South -- component resized to full window width
East, West -- component resized to full window height
Center -- component resized to take remaining space

setLayout(new BorderLayout());
setLayout(new BorderLayout(int hGap, int vGap));
gap size defaults are zero

add(section, component);
add("South", balButton);

GridLayout

divides window into equal-sized rectangles of rows and columns
arranges components in rectangles left-to-right top-to-bottom
components resized to fit into rectangles

setLayout(new GridLayout(int rows, int cols));

Introduction to Buttons, Layout Managers, and Action Events

All Kinds of AWTful things going on here...

Events, (Camera), Action!

Images, Lines, Shapes

Threads

This document was last modified