You are on page 1of 158

JAVA

-1854


&

2012
r
s.g
ek
re
4G
ks
oo
eB


"
J A V A "


-1854

: .

2012


.
/

, 18/05/2011


Java.

Java.
.
, .
, Java,
, , ,
. ,
/ (input/output) Java. URL
URL . ,

. ,
.
,
. ,
Java.

2011-

2012
.

. ,

. ,

1: JAVA

1.1 Java............................................................................................1
1.2 Java.............................................................................1-4
1.2.1 ................................................................................................1-2
1.2.2 ........................................................................2
1.2.3 ...............................................................................3
1.2.4 ...........................................................................................3
1.2.5 ..........................................................................................3
1.2.6 -..................................................................3
1.2.7 .......................................................................4
1.2.8 ................................................................................4
1.2.9 ...................................................................................4
1.3 Java.......................................................................................4-9
1.3.1 ...................................................4-5
1.3.2 ................................................................................5-6
1.3.3 JAR (Java Archive Files)......................................................6-7
1.1:
jar.exe.....................................................................................7
1.3.4 ....................................................................7
1.3.5 - (Applet Viewer)...................8
1.3.6 ..............................................................................8
1.3.7 (JavaDoc)...................................................8-9
1.3.8 Diassembler .........................................................9
1.3.9 H Java ...................................................9
1.3.10 WWW ..................9
1.3.11 WWW

.................................................................................................10
1.4 Java.....................................................................................10
1.1: Java
.........................................................................................10
2:

2.1 ...................................................................11-13
2.1.1 ........................................11-12
2.1.2 ........................................................................................12
2.1.3 ...................................................................................12
2.1.4 ......................................................13
2.1.5 ......................................................................................13
2.2 ..............................................................................13-14
2.3 ...................................................14
2.3.1 ............................................................................................14
2.3.2 .....................................................................................15
2.4 ...............................................................15-17
2.4.1 this.......................................................................16
2.4.2 ........................................................17
2.4.3 .......................................................................17
2.5 ..........................................................................................18
2.5.1 ................................................18
2.5.2 ......................................19
2.5.3 .............................................19
2.6 (Encapsulation).......................................................................19
2.7 ( Interfaces )............................................................................20
2.7.1 .................................................................20
2.8 ......................................20-22
2.8.1 .....................................20
2.8.2 .........................21
2.8.3 .....................21
2.8.4 .....................................22
2.8.5 ........................................22
2.9 (Packages).........................................................................................22-24
2.9.1 ;...........................................................................22
2.9.2 ...................................................................22
2.9.3 ........................................................................23
2.9.4 .................................................................24
2.10 (Modifiers)...........................................................................24-26
2.10.1 ..............................................................25
2.10.2 static.......................................................................25
2.10.3 final........................................................................25
2.10.4 synchronized..........................................................25
2.10.5 abstract...................................................................26

3 : JAVA

3.1 .....................................................................................................28
3.2 ............................................................................................28-33
3.2.1 .........................................................................28
3.1: o Java...........................28
3.2.2 - ................................29
3.2:
Java.....................................................................................29
3.2.3 ..........................................................................................30
3.3: ......30
3.4:
..................................................................................30-31
3.2.4 ...........................................................32
3.5:
...................................................................................32
3.2.5 ................................................33
3.3 (Casting).....................................................33-35
3.6:
.....................................................................33
3.3.1 ...................................................................................33
3.3.1.1 ....................................34
3.3.1.2 ................................................34
3.3.1.3 ...................................................34
3.7: .....................34
3.3.1.4 ..............................................35
3.4 ...........................................................................................................36-38
3.4.1 ..................................................................................36
3.4.2 ...........................................36
3.4.3 ......................................................................37
3.4.4 ..................................................................................38
3.4.5 ....................................................................38
3.5 (String)................................................................................38
3.5.1 ..............................................................39
3.6 ...............................................................................39-44
3.6.1 .......................................................39-40
3.8: ....................39
3.6.2 /................................................................40
3.6.3 ...........................................................................41
3.9: .......................................41
3.6.4 ................................................................................41-42
3.6.5 Bitwise .................................................................................42
3.10: Bitwise ..........................................42
3.6.6 ..................................................................43
3.11: ............................43
3.6.7 ...................................................................43-44
3.12: .............................44
3.7 ..................................................................45-48
3.7.1 If (If Statement ).................................................................45
3.7.2 ( Contitional Operator)............45-46

3.7.3 Switch..............................................................................46

3.7.4 .........................................................................47-48
3.7.4.1 for...............................................................................47
3.7.4.2 while...........................................................................48
3.7.4.3 do.......while...............................................................48

4:

4.1 Java.............................................................49-53
4.1 System.............................................52

4.2 .........................................................53-54

4.3 ................................................54

4.3.1 Java................54-58

4.4 .....................................................................58

4.4.1 -.................58

4..4.2 -........................58-59

4.5 .................................................................59
4.6 -.................................60-63

4.7 - .........................................63-64

4.7.1 - Web......................................64

4.7.2 < Applet >...........................................................................64-65

4.7.2.1 < Applet >....................65

4.7.2.2.1 ALIGN.....................................................66

4.7.2.2.2 VSPACE HSPACE..........................66

4.7.3 ..................................67-69

5: /

5.1 (Streams)................................................................................................70
5.2 java.io...........................................................................................71
5.3 / ....................................................................................71-74
5.3.1 Steams (Input Streams).........................................................71
5.3.2 Streams (Output Steams)........................................................74
5.4 ....................................................................................75
5.5 ........................................................................76-77
5.6 ........................................................................................78-80

6: URL

6.1 ........................................................................................................81
6.2 H URL.................................................................................................82-84
6.3 URL........................................................................................84-85
6.4 URL ...................................................................85
6.4.1 openConnection........................................................................86
7:

7.1 ........................................................................................................87
7.2 (Loading Images)......................................88
7.2.1 getImage().......................................................88-90
7.2.2 ..........................................................................90-92
7.3 ..................................................................................92-93
7.4 .....................................................................................93-95
7.4.1 ..............................................................................94
7.4.2 ...........................................................95

7.5 .............................................................................................96
7.5.1 .....................................................................96-97

8:

8.1 (nimation)..................................................98

8.2

.......................................................................................................98-99

8.2.1 ..................................99

8.3 .............................................................99-100

8.4 ................................100-102

8.5 ...........................................................102-105

8.6 ...........................................................105-108

8.6.1 Sprite..............................................................................105

8.6.2 SpriteVector...................................................................106-108

8.7 (Playing Sounds)..................................................108-110

8.7.1 ........................................109

8.7.2 ...................................................110

8.7.3 , ...................................................110
.......................................................................................111-122

2........................................................................................111-112
3........................................................................................113-115
4........................................................................................116-117
5........................................................................................118-119
6........................................................................................119-121
7........................................................................................121-122
8........................................................................................122

...........................................................................................122

...................................................................123
.............................................................................124
.........................................................................124
................................................................................................125
..................................................................................125

.......................................................................................................126-140

...................................................................................................141
1 : JAVA

Java
Java Sun Microsystems
James Gosling Bill Joy,
(. video,).
,
WWW ( )

.
Sun ( )
,
(browser) HotJava.
, -
(WWW).
Java Netscape HotJava
- (browser).
, Java -

.
Java. ,
, ""
. Java .
Java

C++, SmallTalk Lisp,
.

Java
Java ", ,
, , , - , ,
".
.

JAR (Java Archive Files)

Java C++ . , Java


C++.
C++,
Java.
Java -
, (Boolean) . Java
. ,
.
Java C++,
goto, (
C++), , #define
Typedef. C++
C
.
C++ Java
, .
C C++,
, .
,
.
. ,
. -
.
, Bounds Checker,
.
Java ,
.
Java C++,
,
. , Java
, " " (garbage
collector). -
Java (Virtual Machine VMJ)
.
, .
,
.


Java
SmallTalk C++. ,
Java . Java
: . ,
Java ,
.
Java .
.
.
2.

Java
. URL
Java, .


Java
. ,

.
Java
.
Java : ,
Java
. , Java Lisp
SmallTalk
.


Java
. , Java
Java
. , bytecode
,
. ,
. ,
API -
.
5.

-
1980 .
Apple, Commodone, Radio Shack, Atari IBM.

. , ,

.
Windows, Macintosh Apple
Unix. ,
Windows NT, Unix Macintosh .
Java
. Java
byte Java
Java . , Java
.


Java
( ,
), Java .
Java (. ,
)
.
C++, .


Java
C++. ,
, Java .

Java C++.
, Java
C++.



. Java .
.
.

, .
,
.
Java
- Java,
,
.
Java. JDK
Sun Microsystems
http://www.java.sun.com.


(Interpreter), Java,
Java. -
Java Web
.
, Netscape. ,
(
bytecode).
Java .
,
Java , - .
:
java _
_ .
, .
, Solvent
Equations, :
java Equations.Solvelt
Java ,
main() . main()
.
main()
.

Java
main() ,
-
.

Java (javac)
( bytecode). Java,
.java ASCII,
C++.
.class Java .
bytecode Java ,
. ,
, bytecode.
Java.
public .
.
:
javac

. bytecode
.
,
. ,
javac BigBird BigBird.class
.

Java .
-d
.
. -d,
.
-g
Java.
Java .


.
. Java
.
JAR (Java Archive Files)
JAR

, .
JAR, jar.exe. -
canvaser.java :canvaser.class
BoxCanvas.class. (Zip)
JAR .
Jar.exe canvaser.jar :
C:\java-1\canvaser>jar cvf canvaser.jar*
" * "
JAR, cvf
jar.exe.
.

1.1: jar.exe

,
x [] . , ,
( ).

.
, JAR
f [jar - ] ( ). ,
JAR
.

v ( ) stderr.


Java ,
. , . Java
:
CLASSPATH.
,
java.applet.Appplet java.
java ,
CLASSPATH. java
,

.

- (Applet Viewer)
Java -
, Web .
- (applet viewer) java,
-
java.
- Web
, web .
HTML ,
<Applet> .
- <Applet> HTML
, , - .
-
.
:
appletviewer URL
URL URL HTML
- Java.


java (jdb)
java. :
,
,
.

.
:
jdb < >

.

(JavaDoc)
Java (javadoc)
. HTML
.
:
javadoc
,
Java.

HTML . @
.
:

/**
* @see Object
* @see gemology.Rock
*@version 2.0 Dec 5,1996
* @author Brett Weir
*/

Diassembler
diassembler java (javap)
"" ,
, bytecode.
diassembler ,
.

H Java
Java
,

.
WWW
WWW ,
, ()
().
, Web
.

. ,
.
Web
. , Java
.
.
Java
.

WWW
.
Java,
(browser) .

.
Java . Java

Java .

Java
1.1
Java (Java source code) . (host)

Java.
" Java"
(Java- enabledbrowser),
.
Java
Java (Virual Machine),
.

1.1: Java .
K 2:

:
,
.


,
, .
.
, ,
.
. ,
Screen.
,

.
, Screen
,
. ,
o
, ,
.
Z T A

Java :




:
, . Java
(handles).


.
. :

className handleName;

className handleName
.

: .
new(). H
:

new className (Arguments);

className Arguments
.

:
. .
(constructor) ,

. ,

.

. ,
.
:

import java.awt.* ;
public class text extends java.applet.Applet
{ TextField text1;
public void init()
{
text1= new TextField("Hello Java")
.
.

}
}

:
.
,
. :

import java.awt.Point ;
class Reference
{
public static void main(String args[])
{
Point pt1, pt2;
pt1=new Point(50,50);
pt2=pt1;
pt1.x=100;
pt1.y=100;
System.out.println("Point:" + pt1.x + " , " + pt1.y);
System.out.println("Point:" + pt2.x + " , " + pt2.y);
}
}
: pt1: 50,100
pt2: 50,100
pt1, pt2. , pt1,
. pt2.
pt2=pt1;

: .
,
. Java,
. Java
.
(garbage collector).


.
.
:
.
,
.

,
.
. graphicsClass
Screen :

graphicsClass Screen;

(class)
.
(instance) .
() ,
.
; . ,
(instances) (Objects),
. ,
.
Java .
,
.
'
. Java
Java.


Java :
.

(attributes)
,
.
Car.
:

Color () : , , ,
.
Style () : , , .
Maker () : BMW, Citroen,
Honda, Mercendes.


. .

.

(behavior)
. ,
, .

.
Car
Car :




(methods),
Java .


.
:



""
(signature) .
.
,
.
. Java :

class Bird {
int xPos, yPos ;
double fly (int x, int y) {
flap(distance);
xPos=x;
yPos=y;
return distance ;
}
..............
}

fly.
:

ObjectName.methodName;


.
.
, , void
.

Java ,
.
(method overloading). Java

.

,
.

this

,
.
,
this. this
-
, ,
. :

t = this.x;
this.myMethod(this);
return this;


this.
.
static this.


,
.
, .
,
, . :
class arrayDemo
{//
static int [ ] arrayOfIntegers()
{....
}
}



. , .
,
.
.
, .
.
,
.
,
.


. - (base class),
- (subclass). H
-,
.
,

. - (
), - ( ).
,
- . ,
.
Java Object.
-.
.

.

.

Subclassing
.
Subclassing
.
.


,
,
. ,
:

,
- ,
.

( )
- .
, .


,
""
- . ,

.
.
- ,
.
-
- .
.
: -,
-.
-.

Java .
-.

-
. .

.

(Encapsulation)
.
,
.
. ;
,
( ) .

. , ,
,

.
(encapsulation) .
,

.
:

:
.
( Interfaces )
, ,
. ,
Java
.


. , ,
. , ,
Java, .class . ,
( ,
, .. )
.
,
.
Java ""
"". .
.

: new
.


:
.


, implements
. :
public class Aplo extends java.applet.Applet implements Runnable.


,
.
.
, -
. -
, implements
.

,

.
,
:

public class Aplo extends java.applet.Applet implements Runnable, Eatable, Sortable {...}


,
:

interface Driveable {
boolean startEngine();
void stopEngine();
float accelerate (float acc);
boolean turn (Direction dir); }

O , interface
class.
.

. public abstract .
private protected.

.
.

public, static i final.



.
interface .
,

.

, .
,
-.
extends
:

public interface Fruit extends Food {.....}

, ,
.
.

(Packages)
.
.
,
. Java ,
.
.

;
Java.
.
,
.
Java.lang.
.
default .


,
.
.
:

.
, .
.

. .
.
cs.technion.ac.il il.ac.technion.cs.

java sun
.

:
. :

MD SomePath\src\YRODN\NameOfNewPackage;

:
. :

copy SomePath\*.java SomePath\src\YRODN\NameOfNewPackage;

package YRODN NmaeOfPackage;

:
,
.
.

java c -d SomePath\classes SomePath\src\YRODN\NOPN\*.java


NONP=Name Of New Package

:
,
.

,
.
import . :

Import com.Hyperg.InerG.Hyperspace.contol.MogmaFlow


(*) .

(Modifiers)
,
. ,
, ,
Java.


, .

Java :

, : public,
protected private.
O static .
O abstract .
O final , .
O synchronized volatile, .
O native .

.
. .


public .
public, protected private. public

. private
. protected,

.

static
.

.

.
static :

static int sum;


static final int Max =10;

final
final

.

Java
final. #define C++ const
Java.

synchronized
O Synchronized (
). Synchronized Java
.
,

.
. ,

(CPU)
,
.

volatile (),
.
abstract
,

. ,
"-". -
.
, -.
abstract.
,
.
.
. -
.


-. ,
.
, .
-
.

abstract
.

private static
abstract.
K 3: JAVA


.
. Java
:

/* */:
.
:

/*
*/

//:

.

.
:

Date today = new Date(); //


System.out.println(today); //

/** */:
/** */.
javadoc,
. javadoc
.


. ,
(compiler)
.
,
Java.
.
.
Java .


.
,
.
, C C++,
, .
Java,

.
.

.
. :
int too;
double d1,d2;
boolean isFun;

Java case sensitive.


. .
($).
, Unicode .
,
this.



. .
, (Boolean)
Java. .
.

3.1: o Java

Type Bit Size

byte 8-bit signed integer

short 16-bit signed integer

int 32-bit signed integer

long 64-bit signed integer

float 32-bit floating - point number

double 64 -bit floating - point number

char 16-bit Unicode

boolean true false

o ,
. .
= .
:

:
int age;
age=15;

.
:
int too=42;
double d1=3.14,d2=2.314;
boolean isFun=true;


Java .

3.2: Java

byte 0

short 0

int 0

long 0L

float 0.0f

double 0.0d

char null

boolean false

null


: byte, short, int long, 8-,
16-, 32- 64- bit .

3.3:

Type Bit Size Minimum Value Maximum Value

byte 8 -256 255

short 16 -32,768 32767

int 32 -2,147,483,648 2,147,483,647


-
long 64 9,223,372,036,854,775,807
9,223,372,036,854,775,808

3.4:

!=

>

<

>=

<=

++ 1

-- 1

~ Bitwise

& Bitwise AND

| Bitwise OR

^ Bitwise XOR

<<

>>
>>>

2 long
long .
long. long
int .


: float double
32- 64-Bit .
.

3.5:

!=

>

<

>=

<=

++ 1
-- 1

float double
Boolean.

float :
- +, -
- +0,-0
- + -


. (char)
16384 (Boolean)
true false.

Boolean 0
1, C++.

(Casting)

.
. ,
.
.

3.6:

byte short,int,long,float,double
short int,long,float,double

char int,long,float,double

int long,float,double

long float,double

float double


. , 4
java 4. ,
.


. 4
int.
int long.
(. -45).
, .
1-
9: : int i=1230; 0 (. 0777 0004),
0x .
(0-9) a-f A-F.
: int i=0xFFFF;
,
(. 5.77777).
double, .
float f
(. 2.56f).


true () false
().
, boolean.



(. 'a', '#', '3'). 16-bit
Unicode. 3.7
,
Unicode.

3.7:

\n

\t (Tab)


\b
(Backspace)

\r (Carrige return)

\f

\\ Backslash

\'

\"

\ddd

\xdd

\udddd Unicode


. Java
String.
.
Java, ,
.

:
"Hi, I am a string literal"
" " //

,
, Unicode.
:

"A string with a \t tab in it"


"Nested strings are \"strings inside of\" other strings".


java, java String .

.


.
( ),
. Java
.
new
.
. Java .
C++.
Java :
Java .
[] ,
.
new
[].


Java [].
:

int intArray[];
float floatArray[];
double [] doubleArray;
char [] charArray;

[] .

. .
:

int [] intArray,SecondArray;
int thirdArray[],simpleIntArray;

.
thirdArray simpleIntArray
.


. Java
new()
. :

int intArray[]=new int[100];


float floatArray[];
floatArray=new float[100];
double [][] doubleArray=new double[10][10];

Java :
int intArray[10];

:
0.
'\0'.
false.
null.

. :

int intArray[]={1,2,3,4,5};
char chararray[]={'a','b','c','d'};
string [] stringarray={"A","four","element","Array"};

intarray 5 1
5. charArray 'a','b','c','d'
.


C C++.
.
:

int intArray[]={100,200,300,400,500};

int a=intarray[0]; // a 100


int b=intarray[1]; // b 200
int c=intarray[2]; // c 300
int d=intarray[3]; // d 400
int e=intarray[4]; // e 500

Java C++ 0 n-1


( n ).

ArrayIndexOutOfBoundException, .

int intArray[]={1,2,3,4,5};
int lengtharray=intArray.length;

lengthArray 5.

Java ,
.

.
Java .
.

.
. :

int twoDimensions[][]=new int[10][5];


char threeDimensions[][][]=new char[5][3][3];

,
.

(String)


. Java.
String Java .

String :
String str = new String();



. :
System.out.println(name + "is a" + color + " beetle");

. +,
,
.
.


toString().

+ = . ,
myName += "Jr.";
:
myname= myname + " Jr."; .


Java.
.
.
Java .
, ,
.


Java :

3.8:

% (Modulus)

.
, . ,
31/9 3 ( 9 31 3 ).

" "
.
/
++ --
1. x++ 1,
x = x+1., x-- x
1.

C++, java x
.

,
.
:

y = x++;
y= ++x;


.
(x++ x--), y x x.
, x y
. :


Java 3.9

3.9:

==

!=

>

<

>=
<=

(boolean true false).

boolean
= = =,
.

:
boolean b;
int t =5;
int k=12;
int i;
i= t +k;
if ( i = =17) b=true;
,
, "=" .



AND, OR, NOT XOR (, ,
).
& &&.
.
. &
&& &
. &&
false

OR (| ||)
OR true ( )
true.

XOR true ( true
false ) false (
true).
! .
x !x.
, && ||
. &, | ^
(bitwise).

Bitwise
java
bits ( ) . 3.10
bitwise .

3.10: Bitwise

& Bitwise AND

| Bitwise OR

^ Bitwise XOR

<<

>>

>>>

~ Bitwise

(x = x <<
<<=
y)

>>= (x = x >> y)

,
>>>=
(x = x >> y)

x & =y AND (x = x & y)

x | =y OR (x = x | y)

x ^ =y XOR (x = x ^ y)

. ,
, " = ".
:

3.11:


.
=
x=y=x=0
+= (x=1;x+=2; //x=3
-=
/=
*=
%=
^= Bitwise XOR
&= Bitwise AND
|= Bitwise OR
<<= Shift
>>= Shift



.
java. .

. ,
:

y=6+4/2;

3.12 .

.

3.12:


. [] ()
.

instanceof true false



++ -- ! ~ instanceof

- .

new
. ()
new (type)

.

,
* / %

+ - ,

<< >> >>> Bitwise

< > <= >=

= = !=

& AND

^ XOR

| OR

&& AND

|| OR

?: If ....then .... else

= += -= *= /= %= ^=

&= |= <<= >>= >>>= .

If (If Statement ): Java :


if ()
{
[ 1]
}
else
{
[ 2]
}

Java, if
. ( true),
.

Java
true false.
, :

int val=0;
int val=2;
if (val) {}

Java.

boolean. :
if (val!=0){}

( Contitional Operator)
if else
(contitional operator). ,
.
:

? __ : __
int smaller = x < y ? x : y;
:

int smaller;
if (x<y) smaller=x
else smaller=y;

true false.
__.
__. x y,
smaller.

.
.
" "
.

Switch
if switch.
switch
.
. (case)
default.
.
switch
. switch
:

//
string type=" ";
int n;
switch (n)
{case 0:
type="none"
break;
case 1:
type="red";
break;
default:
type"?"; }

case switch ,
case. break
case.
case
.


(loop) for, while do:

while ()
do () while ();
for (---for; ; )

( )
,
.

for
for ( )
. for :
: .(. i=0;)
: .(. i<=10;)
: .
. (. i++).
:

// (null
Strings).
String strArray[]= new String[10]; //
int i; //

for (i=0; i<strArray.length; i++)


strArray[i]=" ";

for "" .

for .
Java C++
for:

for (i=0; i<10; i++);


System.out.println("Loop");


. println()
.

while
while
. while
. :

while (count < 6 && i>5)


{ system.out.println ("OK");
}

count 6 i
5 while.
.

while false
while .
do.

do.......while
do.....while
false. while do ....while

, do..while

. :

int x=1;
do{
System.out.prinln(" x :" + x);
x++;
} while ( x <=3);

:
x : 1
x : 2
x : 3

4: -

Java
Java .
,
, - .
Java
.
.
: main().
Java ( Java) main()

. :

public class TheClass {


/* TheClass
. */
class ABClass {
/*
ABClass */
}

public static void main( String argv[ ] ) {


/*
. **/
}
}

main()
. main() ,
: In the class
TheClass: void main ( String argves ) is undefined.

main()- public, static, void


main().
public: .
static: ,
.
void:
.

main() : .
.
.

Java

1: public class First {


2:
3: / / First
4: Sring name ;
5: int accountNumber ;
6: float balance ;
7:
8: / /
9: void Printout( ) {
10: System.out.println( "Name: " + name ) ;
11: System.out.println( "Account Number: " + accountNumber ) ;
12: System.out.println( "Balance: $XXXXXX, Sorry that is classified" ) ;
13: }
14:
15: public static void main( String argv[ ] ) {
16: / / First
17: First f = new First( ) ;
18:
19: / / First
20: f.name = "John Smith" ;
21: f.accountNumber = 235412345 ;
22: f.balance = 25 ;
23:
24: / /
25: for ( int i = 0 ; i <40 ; i++ )
26: System.out.print( "--" ) ;
27:
28: / /
29: System.out.println( "INFORMATION" ) ;
30:
31: / / print
32: f.Printout( ) ;
33:
34: / /
35: for ( int i = 0 ; i <40 ; i++ )
36: System.out.print("--");
37:
38: / /
39: System.out.println( "End of printout" );
40:
41: }
42: }

Java First
First : name, accountNumber balance (
4,5 6 ) Printout ( ) ( 9 13).
15,
main(). 17, First .
25 26 for "--"
. 32 Printout ( )
System.out.println( ) ( 10, 11 12). 35
36 "--",
39 .
System System.out.println( ) java.lang
Java ( -
). 4.1
System.

4.1 System

Variable index
Variable Name Usage

public static PrintStream err

public static InputStream in

public static PrintStream out

Method Index
Method Name Usage

arraycopy ( Object src, int src Position, Object



dst, dstPosition, int len

long
currentTimeMillis( ) milisecond 1
1970


exit( int status )
0.


gc( )
.

getProperties( ) .


getProperty ( String key, String default )
.


setProperties ( Properties props )
.

System.out.println( ) out
System. out PrintStream.
First.java
javac.exe .
javac.exe
: javac First.java.

.

,

.class .java.
.

Java ( java.exe
) . : java First.

,
, .class. Java
(
Java .class) .



. :
currentTimeMillis( ). 64-bit long,
1
1970. :

1: public class DisplayDate {


2:
3: public static void main( String argv[ ] ) {
4:
5: / /
6: for ( int i = 0 ; i < 40; i++ )
7: System.out.print("--") ;
8:
9: / /
10: System.out.println( "Milliseconds since January 1, 1970: " +
System.cuurentTimeMillis( ) ) ;
11:
12: / /
13: for ( int i = 0 ; i <40 ; i++ )
14: System.out.print( "--" ) ;
15:
16: / /
17: System.out.println( "End of printing" ) ;
18:
19: }
20:}

6 7
"--". currentTimeMillis( ) System.out.Println(
) 10 . 13
14 17.
: javac DisplayDate.java
:
java DisplayDate

currentTimeMillis( ) long

.


Java ,
,

.
Java
Java. Windows UNIX
Java .

Java
: java ABClass test , test
main()
. .
: java ABClass test 2 test
argv[0] 2 argv[1].
argv .
. argv[ ]
arg[ ] C.

Java argv C
UNIX. , argv[0], ,
-
C.


. :

java ABClass "Ford Mustang" "Honda Civic"

argv[0] Ford
Mustang, argv[1] Honda Civic.
.
, .

1: public class SayHello {


2:
3: public static void main( String argv[ ] ) {
4:
5: / /
6: for ( int i = 0 ; i < 40; i++ )
7: System.out.print("--") ;
8:
9: / /
10: if (argv.length == 0)
11: { System.out.println( "Please enter your name!" ) ;
12: System.out.exit(0);
13: }
14:
15: / /

16: for (int i=0; i<argv.length; i++)
17: System.out.ptintln ("Hello " + argv[i]);
18:
19: / /
20: for ( int i = 0 ; i <40 ; i++ )
21: System.out.print( "--" ) ;
22:
23: / /
24: System.out.println( "End of printing" ) ;
25: }
26: }

10 argv.length.

, argv[]. System.out.exit( )
. 10 13 0,
.

: java SayHello.java
: java SayHello Dan Bill "John Smith"
:

----------------------------------------------------------------------------------------------------
Hello Dan
Hello Bill
Hello John Smith
----------------------------------------------------------------------------------------------------
End of printing

Hello .
,
: java SayHello,
:

-----------------------------------------------------------------------------------------------------
Please enter your name!
-----------------------------------------------------------------------------------------------------
End of printing

.
Java
. ,
.

SumAverage
.
:

1: class SumAverage {
2:
3: public static void main( String argv[ ] ) {
4: int sum = 0;
5:
6: for ( int i = 0 ; i < argv.length; i++ )
7: sum += argv[i] ;
8:
9: System.out.println("Sum is : " + sum);
10: System.out.println("Average is : " + (float)sum/ args.length);
11: }
12: }

. for
.
, :

Sumaverage.java : 6: Incompatible type for +=.


Can' t convert java.lang.String to int.
sum += argv[i];

.
,
.
, .
parseInt Integer.
7 :
sum += Integer.parseInt(argv[i]);


. :

java SumAverage 1 2 3
:
Sum is: 6
Average is: 2.

-
- Java
Web.
Web -
, Java.
Java ,
Java Java
Developer's Kit. Java

. - ,
, .
Java,
- .

(animation) .

Java Applet Viewer


. ,
, "
Java" (Java-enabled browsers). :
Hot Java Sun
Netscape Navigator
Microsoft Internet Explorer

-
, (hst)
, -
. Java

.
-.
:

"",
.
,
, -
.
.
UNIX, .
, -
DLLs.

- Java Netscape
Navigator Microsoft Internet Explorer.
Java
.


- , -
Applet. Applet, java.applet,
-
Java. -
Abstract Window Toolkit (AWT) Java,
-
(GUI): , ,
, ,
. AWT java.awt.
- ,
, :

public class myClass extends java.applet.Applet


{............}

Java - public.
- .
public.
, Java
- , -
,
- .

-
- main().
-
, ,
.
.
- -
.
.
- :





Applet: init(),
start(), stop(), destroy() paint().

public void init() ( )


init() - ,
main().
o, ,
, -
(host).
- ,
init() - :

public void init()


{...........}

public void start() ( )


start() -,
- Java . -
.
-,
. start()
-, init()
.
start() .
-
start():

public void start()


{...........}

public void stop() ( )


stop()
-
. ' , ,
. stop()
-
:
public void stop()
{...........}
public void destroy() ( )

- ,
- -
.
, destroy() -
(host server).
-
destroy():

public void destriy()


{...........}


-
,
, .
,
(animation).
paint():

public void paint(Graphics g)


{...........}
, Graphics.
Graphics java.awt
- , import java,
:

import java.awt.Graphics;

: init(), start(),
stop(), destroy() paint(). -

.
.

LifeCycleApplet.java

import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;

public class LifeCycleApplet extends java.applet.Applet


{
Font theFont = new Font ("Helectiva", Font.BOLD, 20);
int i;
String String1,String2;

public void init()


{
i=0;
String1 = " ";
String2 = "The applet is initializing!";
repaint();
}

public void start()


{
i=1;
String1 = String2;
String2 = "The applet is starting!";
repaint();
}

public void Stop()


{
i=2;
String1 = String2;
String2 = "The applet is stopping!";
repaint();
}

public void destroy()


{
i=3;
String1 = String2;
String2 = "The applet is being destroyed!";
repaint();
}

public void paint( Graphics g)


{
g.setFont (theFont);
g.setColor(Color.blue);
g.drawString (String1+ String2,5,30);
}
}

-
- html
. bytecode
html .
, html - Java
(tag). Java

- .
HTML LifeCycleApplet.java
:

1. <HTML>
2. <! - Sun Applet Viewer>
<HEAD>
<TITLE> - </TITLE>
</HEAD>
<BODY>
<H1> </H1>
<BR>
<APPLET CODE = LifeCycleApplet.class WIDTH = 600 HEIGHT = 50>

- Java.
</APPLET>
</BODY>
</HTML>


1. <HTML>: html
HTML.
2. : html
(!) : <! .>.

Applet Viewer.
3. <HEAD>: .
4. < TITLE >: .
, html "
- ", - .

(Web browser).
5. <BODY>: .
.
6. < H1 >: .
7. < APPLET >: - Java .
code -
LifeCycleApplet.
600x50 pixels ( ).

java.applet.Applet.resize()

-.

- Web

- HTML
-
(www) HTML
,
.
< Applet >

,
<Applet>. <Applet> HTML
- .
Tag.

CODE
, .class.
HTML .
-
CODEBASE. CODE , CODEBASE
( URL
) .

classes, HTML
, CODEBASE :

<Applet CODE= "myclass.class" CODEBASE = "classes" WIDTH = 100 HEIGHT = 100>


</Applet>


URL
CODEBASE:

<Applet CODE= "myclass.class" CODEBASE = "http:// myserver.com/javaclasses"


WIDTH = 100 HEIGHT = 100> </Applet>

WIDTH HEIGHT
-
.
-
-

.
<Applet> </Applet>

<Applet>.
Java
.
<Applet> <IMG>, ,
, <p> ()
(<H1>, <H2> , ..).

< Applet >

<Applet>
-
. , <Applet>
.

ALIGN
ALIGN - .
: LEFT, RIGHT, TOP, TEXTTOP,
MIDDLE, ABSMIDDLE, BASELINE, BOTTOM, ABSBOTTOM.
ALIGN = LEFT: -
-
.
ALIGN = RIGHT: -
-
.
ALIGN = TEXTTOP: -
.
ALIGN = TOP: -
( - ).
ALIGN = ABSMIDDLE: -
.
ALIGN = MIDDLE: -
.
ALIGN = BASELINE: -
.
ALIGN = ABSBOTTOM: -
.

VSPACE HSPACE
, pixels
- . HSPACE
( - ),
VSPACE .
:

<p> <Applet CODE = "HelloAgainApplet.class" WIDTH = 300 HEIGHT = 200 ALIGN


= LEFT
VSPACE = 50 HSPACE = 10>
</Applet>

-
Java. , -
HTML <Applet>
. - ,
:
HTML .
- .
- :
. HTML
- ,
<PARAM>, , NAME VALUE
. <PARAM> <Applet>.
, HTML LifeCycleApple

HTML - .
HTML :

1. <HTML>
2. <HEAD>
3. <TITLE> - !</TITLE>
4. </HEAD>
5. <BODY>
6. <H1> !</H1>
7. <BR>
8. <APPLET CODE="LifeCycleApplet.class" WIDTH=600 HEIGHT=50>
9. <PARAM NAME=color VALUE="blue">
10.
- Java.
11. </APPLET>
12. </BODY>
13. </HTML>

- html ;
- . -
getParameter() java.applet.Applet.
LifeCycleApplet.java : getParameter ( "Color" )
init(),
"blue". -
.

<PARAM>
getParameter() ,
. ,
<PARAM NAME = ""> <PARAM NAME = " ">.

HTML
getParameter() null.

.

getParameter() .
,
.

, HTMl .
12. 12
theSize int Integer.parseInt(),
:

int theSize;
String s = getParameter("Size");

if (s = = null)
theSize = 12;
else theSize = Integer.parseInt(s);
NewPalindrome
- .

NewPalindrome.java

import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;

public class NewPalindrome extends java.applet.Applet


{
Font f = new Font ("Times New Roman", Font.BOLD, 36);
String Palindrome;

public void init()


{ Palindrome = getParameter("Palindrome");
if ( Palindrome = = null)
{
Palindrome = "Dennis and Edna signed";
}
}

public void paint (Graphics Screen)


{
screen.setfont(f);
screen.setColor(Color.red);
screen.drawstring(Palindrome, 5 , 40);
}
}

HTML - NewPalindrome

<HTML>
<! - Sun Applet Viewer>
<HEAD>
<TITLE> NewPalindrome </TITLE>
</HEAD>
<BODY>
<BR>
<APPLET CODE = "NewPalindrome.class" WIDTH = 600 HEIGHT = 50>
<PARAM NAME = Palindrome VALUE = "No, sir prefer Prison!">

- Java.
</APPLET>
</BODY>
</HTML>

null.
"Dennis and Edna
signed". Palindrome HTML
"No, sir prefer Prison!.

5: /

Java, C C++,
.
. java.io ,

.
streams (, ) pipes ()
.

(Streams)
/ (Input/ Output) Java .

.
, .
,
.

, :
,
:

java.io
-
java.io. ,
java.io .
import.
import java.io.*;
import java.io.InputStream ;

IOException.

Steams (Input Streams)


.
InputStream, ,
(buffer).

InputStream

InputStream Object
.
.

read( ). ,
. ,
.
, -1.
.
read :

public abstact int read ( ) , .


public abstact int read (byte b[] ), byte .
public abstact int read ( byte b[], int off, int len), len bytes
.


(buffer). :

s.read(buffer, t, x); bytes t x.

byte . :

int waiting.in.available ;
byte [] data = newbyte;
System.in.read (data);

available( )
bytes
available().
:

public int available ( ) throws IOException ;

skip( )
bytes bytes
skip( ).

public int skip ( ) throws IOException ;


:

if (s.skip( 256) ! = 256 )


System.out.println("Skipped loss") ;

bytes .
bytes
.

close( )
.
.
.
, .
.
close( ) :

public void close () throws IOException ;


, ,
.

.
:
InputStream s= alwaysMakeNewInputStream ( );
try {
............
} fimnally {
s.close( ) ;
}

mark( ) reset( )
"" .
bytes. ,
bytes
. reset( )
. :

InputStream s = getAnInputStreamFromSomeWhere( );
if (s.markSupported ( ) ) {
........ // stream
s.mark(1024);
........ // 1024 Bytes
s.reset( );
.........
} else {
.........
}

Streams (Output Steams)

.
. OutputStream Object
.
bytes .

write( )
.
bytes .
.
:

public abstract void write ( int b) throws IOException ; byte stream


public abstract void write (byte b[] ) throws IOException ; byte
stream
public abstract void write ( byte b[], int off, int len) throws IOException ; len bytes
stream
.
:
s.write (buffer, t, x); t x.

flush( )
,
, " ".
flush( ). :

public void flush( ) throws IOException;

close( )

. close( ) :

public void close( ) throws IOException;

A
bytes bytes
read (byte b[]). ByteArrayInputStream
: bytes . :

public ByteArrayInputStream (byte buf[]);



public ByteArrayInputStream (byte buf[], int offset, int len );

,
bytes. .
reset( )
, available( )
bytes . :

InputStream s= new ByteArrayInputStream (buffer);


:
bytes .

StringBufferInputStream
ByteArrayInputStream, string.
:

public StringBufferInputStream (String s);


ByteArrayOutputStream , bytes.
:

public ByteArrayOutputStream ( );
public ByteArrayInputStream (int size);

, bytes
bytes. 32 bytes
. size
bytes .
ByteArrayOutputStream
:

OutputStream anotherOutputStream = getTheOtherOutputStream( );


ByteArrayOutputStream = new ByteArrayOutputStream();
fillwithUseful( );
s.writeTo( anotherOutputStream);

StringBufferOutputStream
ByteArrayOutputStream, string.
:

public StringBufferOutputStream (String s);


.
.

:

public FileInputStream (File file);


public FileInputStream (FileDescriptor fdObj);
public FileInputStream (String name);


Object, String, FileDescriptor.
,
FileOutputStream :

public FileOutputStream (File file);


public FileOutputStream (FileDescriptor fdObj);
public FileOutputStream (String name);

FileDescriptor
"" . getFD( ).
:

FileDescriprtor fd = someFileStream.getFd( );
InputStream s1= new FileInputStream(fd);
OutputStream s2= new FileOutputStream(fd);

,
.
.


FileReader FileWriter farrago.txt
outagain.txt:

import java.io.*;

public class Copy {


public static void main(String[] args) throws IOException {
File inputFile = new File("farrago.txt");
File outputFile = new File("outagain.txt");

FileReader in = new FileReader(inputFile);


FileWriter out = new FileWriter(outputFile);
int c;

while ((c = in.read()) != -1)


out.write(c);

in.close();
out.close();
}
}
. : farrago.txt
File Reader outagain.txt
FileWriter. :

File inputFile = new File("farrago.txt");


FileReader in = new FileReader(inputFile);

File
.



. ( ) ( ).
.: .
byte byte :

import java.io.* ;
class ByteCopy
{
static public void main (String args [ ]) {
FileInputStream in = null;
FileOutputStream out = null;
int inByte;

//
if ) args.lenght !=2 ) {
System.out.println ("Usage : java ByteCopy ");
System.exit(0);
}
try {
/
in= new FileInputStream ( args[0]) ;
//
out= new FileOutputStream ( args[1]) ;

// byte
inByte= in.read( );
while (inByte != -1 ) {
out.write((byte) inByte) ;
// byte
inByte= in.read( );
}

}catch (EOFException eofe) {


//

} catch (FileNotFoundEcxeption fnfe) {


//
System.out.println ("File not found : " + args[0]) ;
} catch (IOEcxeption ioe) {
System.err.println (ioe.getMessage ( ) );
{ finally {
//
try {
if (in 1= null in.close( );
if (out != null out.close( );
} catch (IOException e) { }
}
}
}

import java.io.* ;
class LineCopy
{
static public void main (String args [ ]) {
FileInputStream fis = null;
DataInputStream in= null;
FileOutputStream fos = null;
DataOutputStream out = null;
String line ;
int linesRead = 1;

if () args.length != 2 ) {
System.out.println ("Usage : java LineCopy ");
System.exit(0);
}
try {
/
fis= new FileInputStream ( args[0]) ;
in = new DataInputStream (fis);

//
fos= new FileOutputStream ( args[1]) ;
out = new DataOutputStream (fos);

//
Line= in.readLine( );

//
String lineSep = System.getProperty ("line.Seperator") ;
while ( line != null) {
//
System.out.println(linesRead++ + " : " + line);
out.writeBytes(line _lineSep);
line =in.readLine( );
}
}

}catch (EOFException eofe) {


//
} catch (FileNotFoundEcxeption fnfe) {
//
System.out.println ("File not found : " + args[0]) ;
} catch (IOEcxeption ioe) {
System.err.println (ioe.getMessage ( ) );
{ finally {
//
try {
if (fis ! = null) fis.close( );
if (in != null in.close( );
if (fos != null) fos.close( );
if (out != null out.close( );
} catch (IOException e) { }
}
}
}

6: URL

"" World Wide Web, URL


HTML. URL
Uniform Resource Locator,
Internet. ,

. URL
. ,
, FTP,
, , . URL
,
.
URL Java
, .

URL.
URL, - ,
.

.
URL ,
. URL :

http://java.sun.com/

URL, Java
Sun Microsystems. URL :
(protocol identifier).
(resource name).
http,
java.sun.com/ .

. http.
ftp, gopher, file new.
.
,
:
host name : .
filename: .
portnumber: .
reference: -
.
hostname filename,
. URL
:

protocol :// hostname/location/item

H URL
java.net URL
Java, URL. Java
URL,
. URL
URL .
:
URL aDoc = new URL ("http://java.sun.com/j2ee/webservices/index.html");

URL URL
. (Relative) URL
.
URL
URL . URL
HTML. ,
HTML : JHomePage.html.
: Picture.html MKinds.html.,
Jhomepage.html.
:

< a href = "Pictures.html" > Pictures </ a>

URL . URL
. URL
Java. URL
"http://www.gamelan.com"/ .
URL
URL.
URLs URL. :

URL gamelan = new URL ("http://www.gamelan.com/");


URL gamelanNetwork = new URL ( gamelan, "Gamelan.network.html");

URL
URL URL URL.
URL
.
Gamelan.network.html Bottom.
URL
URL :

URL gamelan = new URL ("http://www.gamelan.com/Gamelan.network.html");


URL gamelanNetworkBottom = new URL (gamelan, "# Bottom");
URL :

URL (URL baseURL, String relativeURL)

URL URL

. URL ,
URL URL.
URL.
URL
hostname, filename, port number URL.

. ,
,
hostname, filename port number URL
. URL Gamelan.network.html
site Gamelan :

URL gamelan = new URL ("http", "www.gamelan.com", "Gamelan.network.html");

, hostname
.
(port number)
. ,
:

URL gamelan = new URL ("http", "www.gamelan.com", 80, "Gamelan.network.html");

URL .

Exceptions
URL
MalformedURLException
. Java
.
URL
try..catch. :

try {
URL myURL = new URL(..........)
} catch (MalformedURLException e ) {
................
//
...............
}

URL
.

URL
URL
URL. ,
hostname, port name filename URL
:
getProtocol( )
getHost( )
getPort( )
getFile( )
getRef ( )

URL
. URL HTTP Urls
URL. URL
HTTP Url.


URL
URL.
URL
URL.
URL
.
URL :
import java.net.*;
import java.io.*;
class ParseURL {
public static void main(String args[]) {
URL aURL = null;
try {
aURL = new URL("http://java.sun.com:80/tutorial/intro.html#DOWNLOADING");
} catch (MalformedURLException e) {
;
}
System.out.println("protocol = " + aURL.getProtocol());
System.out.println("host = " + aURL.getHost());
System.out.println("filename = " + aURL.getFile());
System.out.println("port = " + aURL.getPort());
System.out.println("ref = " + aURL.getRef());
}

M URL
URL openConnection( )
. URL
Java URL
. ,
Yahoo :

try {
URL yahoo = new URL("http://www.yahoo.com/");
yahoo.openConnection();
} catch (MalformedURLException e) { // URL() . . . }
catch (IOException e) { // openConnection() . . . }

, openConnection(), URLConnection
, URL URLConnection.
IOException.
URLConnection
.

openConnection

openConnection()
URL,
URLConnection. URLConnection
URL . HTTP
URL HTTP URL.
, URL
URLConnection
. :
. URL :

URL url = new URL ("http://java.sun.com/j2ee/webservices");

URLConnection
. PrintStream.

URLConnection connection = url.openConnection();


PrintStream outputStream = new PrintStream (connection.getOutputStream());

getOutputStream() . URL
,
UnknownServiceException. URL ,
URL.
.

outputStream.println ("string= " + stringToReverse);


outStream.close();

7:


. Java
(animation),
.
(GIF ) .
Java,
.
.
Java
. , ,
Java . Java
.

, .

.
Java ,
.
: GIF JPEG.
. AWT
,
. GIF JPEG Java,
bitmap.
Java
Image .
, Applet
Graphics
- .
Image ,
bitmap .
,
.

Java " "


" ". " "
Image " ",
. H Java ImageProducer
, URL, Image, CropImageFilter,
RGBImageFilter PixelGrabber ImageConsumer.
(Loading Images)
Image new( ).
-
.

Java,
Java .


getImage() Applet.
Image.
Image :

import java.awt.Image;

getImage()

getImage() URL
. :
getImage() ,
URL.
URL.
- Duke
" http:///T1.gif"

getImage()
URL :

Image pic = getImage(new URL("http://T1.gif"));


- :
public void init()
{
URL url;
try {
pic = getImage(new URL("http://T1.gif"));
} catch (MalformedURLException e) {}
}

getImage() : URL
URL,
.
URL Applet.
getDocumentBase(): HTMl
- .
getCodeBase():
- .

CODEBASE HTML .

HTML
Java.

- getDocument Base()
Duke.


.

Image dukeImage[] = new Image[11];

for
:

dukeImage[i]= getImage(getDocumentBase(),"T"+i+".gif");}

getImage()
.


.
Java
.

getImage()
. ,

. ,

. -
.
- (
)
ImageObserver.


.
drawImage() Graphics.
drawImage( ):

drawImage (Image img, int x, int y, ImageObserver observer);


drawImage (Image img, int x, int y, int w, int h, ImageObserver observer);
dawImage (Image img, int x, int y, Color c, ImageObserver observer);
drawImage (Image img, int x, int y,int w, int h, Color c, ImageObserver observer);

:
x, y :
w, h:
c:
observer:
, : -
"Loading"
.
, .
UrlGif .
x,y .
(0,0),
Java.
ImageObserver. -
- this:

public void paint( Graphics g )


{
if (pic != null)
{
g.drawImage(pic,0,0,this);
}
}

Applet -

Component.
this drawImage(), getWidth() getHight().

getImage() , null.
drawImage() (null) .
.

ImageObserver ,
imageUpdate( ), :
Image, ,
x, y , width height .
:
ABORT = .

ALLBITS =

ERROR =
FRAMEBITS =
HEIGHT =
PROPERTIES =
SOMEBIT =

WIDTH =

UrlGif
- :
public boolean imageUpdate (Image img, int infoflags, int x, int y, int width, int height )
{
if ((infoflags & ERROR) != 0) return false;
if ((infoflags & ALLBITS) != 0)
{
paint( getGraphics() );
return false;
}
return true;
}


Image ,
.


drawImage() :

screen.drawImage(whig, xPos, 10, this);


.
getWidht() getHeight() Image
. ,
ImageObserver .

int iWidth = whig.getWidth(this);


int iHeight = whig.getHeight(this);

getWidth() getHeight()
-1. ImageObserver
.


4. 25%.
drawImage()
.

screen.drawImage(whig, xPos, 10,iWidth / 4, iHeight / 4, this);

: ,
x,y ,

ImageObserver.


,
.



, Java .



. :

,
.
.
,
.
: , .
.
,
. RGB
Java.
.
HSB Hue, Saturation,
Brightness (, ).
, .


pixel .
Java
pixel , .
Java (direct color
models ) (indexed color models).

Java.
IndexColorMdel
.

:

Color Red Green Blue

black 0x00 0x00 0x00

blue 0x00 0x00 0xFF

cyan 0x00 0xFF 0xFF

darkGray 0x40 0x40 0x40


gray 0x80 0x80 0x80

green 0x00 0xFF 0x00

lightGray 0xC0 0xC0 0xC0

magneta 0xFF 0x00 0xFF

orange 0xFF 0xC8 0x00

pink 0xFF 0xAF 0xAF

red 0xFF 0x00 0x00

white 0xFF 0xFF 0xFF

yellow 0xFF 0xFF 0x00

DirectColorModel 32bit
.
32 bit.

Color Java. ColorModel


.
pixel .
DirectColorModel IndexColorModel.

.


unpack_color

unpack_colors.java
import java.awt.*;
import java.applet.Applet ;

public class unpack_color extends Applet


{
public void init( )
{
int temp=Color.ewhite.getRGB();
unpack_colors(temp);
}

public void unpack_colors(int the_color)


{
int red, green, blue;
red= the_color & 0x00FF0000;
red= red >> 16;
green = the_color & 0x0000FF00;
green =green >>8 ;
blue=the_color & 0x000000FF;
System.ou.println("red= " +red + "green = " + "blue=" = blue);
}
}


,
. Java

.
pixel .
, pixels
. :
,

.

Java :

ImageProducer
FilteredImageSource
MemoryImageSource
ImageConsumer
PixelGrabber
ImageFilter
RGBImageFilter
CropImageFilter

ImageProducer
pixels .
.
FilteredImageSource ImageProducer
.
: '

new FilterImageSource(ImageProducer, ImageFilter);

ImageProducer
getSource().
ImageFilter .

createImage.
.
MemoryImageSource ImageProducer
pixels .

.
ImageConsumer
.

.
PixelGrabber ImageConsumer
pixels .
Image
ImageProducer.
.
PixelGrabber.


.
H ImageFilter
.
FilteredImageSource.
.
.
.
RGBImageFilter
pixels RGB.
canFilterIndexColorModel.
boolean
filterRGB pixel .
CropImageFilter ImageFilter
.

8:

(nimation)
.
.
(animation)
. animation , 10
20 . ,
(animation) 8 12 .
(animation):

(frame) : ,
web.
- ().
,
.

.
.

GIF .

: , sprite
animation,
.
.

.


:
,

.

MediaTracker. Media Track
.
.

MediaTracker
Java ,
.
.

.
,
,
.
.



. HTTP
.
.

.
ClipRect().
-
. .

.


- (frames)
.

sleep() Thread
-
. sleep() ,
. 10
100 millisecond , .
-

.

try
{
tm += delay;
Thread.sleep(Math.max(0, tm - System.currentTimeMillis()));
}
catch (InterruptedException e)
{
break;
}

repaint() paint()
:

public void paint(Graphics g)


{
g.setColor(Color.black);
g.drawString("Frame " + frame, 0, 30);
}
,
100 , run() 100
repaint(). ,
paint() 100 . repaint()
,
. frame run()
paint().


- world.gif
car.gif.
(frame)
.


"" (Frames).

Image world;
Image car;

public void init()


{
String str = getParameter("fps");
int fps = (str != null) ? Integer.parseInt(str) : 10;
delay = (fps > 0) ? (1000 / fps) : 100;

world = getImage(getDocumentBase(), "world.gif");


car = getImage(getDocumentBase(), "car.gif");
}

paint()
.

public void paint(Graphics g)


{
update(g);
}

paintFrame():

public void paintFrame(Graphics g)


{ Dimension d = size();
int w = world.getWidth(this);
int h = world.getHeight(this);
if ((w > 0) && (h > 0))
{ // world.gif .
g.drawImage(world, (d.width - w)/2, (d.height - h)/2, this);
}
w = car.getWidth(this);
h = car.getHeight(this);
if ((w > 0) && (h > 0))
{// car.gif :
w += d.width;
//
g.drawImage(car, d.width - ((frame * 5) % w), (d.height - h)/3, this);
//
g.drawImage(car, d.width - ((frame * 7) % w), (d.height - h)/2, this);
}
}

K

web Neko.
- , , ,

- .


Image,
Image
:

Image nekoPics[] = new Image[9];


Image currentImg;


. y
x.

int x;
int y = 50;

Thread
- .

Thread runner;

-
:

public void init()


{
String nekoSrc[] = { "right1.gif", "right2.gif",
"stop.gif", "yawn.gif", "scratch1.gif",
"scratch2.gif","sleep1.gif", "sleep2.gif",
"awake.gif" };

for (int i=0; i < nekoPics.length; i++)


{
nekoPics[i] = getImage(getDocumentBase(),nekoSrc[i]);
}
}

getImage()
.

run(). :
, ,
, -

.
.
x,y
.

.
for x
y 10 pixels x.

10 pixels .

.


repaint(). sleep()
.

void nekoRun(int start, int end)


{
for (int i = start; i < end; i += 10)
{
x = i;
if (currentImg == nekoPics[0])
currentImg = nekoPics[1];
else currentImg = nekoPics[0];
repaint();
pause(150);
}
}

repaint() paint()
x y .

currentImg (null)
drawImage().

public void paint(Graphics screen)


{
if (currentImg != null)
screen.drawImage(currentImg, x, y, this);
}

pause() .
.

.

void pause(int time)


{
try {Thread.sleep(time);}
catch (InterruptedException e) { }
}

-.
- run()
.

nekoRun(0, getSize().width / 2);

nekorun().
.

. -

. x,y .

(Sprite Animation)

sprite ,
, sprite.
. sprite,
. sprite
. sprite Sprite SpriteVector.
Sprite

Sprite sprite
.

.
.

Sprite :

:
.

: .

,Y : sprite. sprite
.

: sprite.

: sprite sprite
.

:
sprite.

SpriteVector

SpriteVector sprite
.

SpriteVector Vector,
java.util. H Vector
.
SpriteVector
Sprite.

SpriteVector , background
Background.
sprite.
- :
loadImage()
MediaTracker:

public Image loadImage(String imagename)


{
Image image=getImage(getDocumentBase(),imagename);
MediaTracker tracker=new MediaTracker(this);
tracker.addImage(image,0);
try
{
tracker.waitForID(0);
}
catch(InterruptedException e){}

return image;
}

waitForID()
.
MediaTracker
addImage().

extractImages()
.
CropImageFilter.
"CropImageFilter "

public void extractImages(Image imagestrip, Image images[],int num,int width, int height)
{
ImageProducer source = imagestrip.getSource();

for(int i=0; i<num; i++)


{
ImageFilter extractFilter=new CropImageFilter(0,i*height,width,height);
ImageProducer producer = new FilteredImageSource(source,extractFilter);
images[i] = createImage(producer);
}
}

run() ,
15 -
.
.

.
.

public void run()


{
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);

while(true)
{
try {runner.sleep(15);}
catch (Exception e) { }

for(int i=0;i<getSize().width;i+=80)
for(int j=0;j<getSize().width;j+=80)
gBuffer.drawImage(background,i,j,this);

x+=3;
if(x>300)
x=-100;

ticker++;

if(ticker>3)
{
if(frame<5)
frame++;
else
frame=0;

ticker=0;
}

gBuffer.drawImage(dog[frame],x,45,this);

repaint();
}
}

paint() (Buffer) .

(Playing Sounds)

Java
. 64bit
MIDI :

: AIFF, AUU, WAV, MIDI, RMF


play() Applet. play() detImage()
:
play(url): , URL.
, URL
.

-
"http://pdplab.it.uom.gr/java2001/copy_java1"

URL
play(). try...catch

.

public void init()

{
URL url;
try {
url = new URL("http://ermioni.uom.gr/copy_java1/cuckoo.au");
} catch (MalformedURLException e) {
}
play(url);
}

java.net.*
URL.

play(getDocumentBase(), String):
getDocumentBase() getCodeBase().
.

- LoopSound
getDocumentBase()


AWT, .
MouseOver
-
.
,


.

, Image&Sound.
Duke . , .
,
.

2:

1) ;
(A) ,
(B)
(C)
(D)

2) ;
(A)
(B)

(C)
(D)

3) private;
(A)
(B)
.
(C) private

(D) ,
.

4) ;
(A)
(B)

(C)
(D)

5) o
;
(A) private
(B) public
(C) default
(D) static

6)
. ;
(A) (1) (2)
(B) (1) catch{} (2)
(C) (1) catch{} (2)

(D) (1) catch{} (2)
try{}

7) :
(A) Java
(B) Java
(C) Java
(D) Java
1: A. ,
2: A.

3: B.
.
4: D.

5: B. public
6: C. (1) catch{} (2)

7: A. Java

1. ,
;

1


.
Java
. .

.

3:

1) Java ;
(A)
(B)
(C) Java ;
(D)

2) ;
(A) -
(B) -
(C) -
(D) -

3) ;
(A) double duty;
(B) floatloan= 84.6;
(C) boolean value=12;
(D) int sort=34, end=99;

4)
;
(A) - .
(B) -
.
(C) - .
(D) -

5) Java (call by value)


routine();
double[] rats={1.2,3.4,5.6}
routine(rats);
(A) rats
(B) rats
(C) rats
(D) 1.2

6) img;
(A) int img;
(B) int [] img;
(C) new int img[];
(D) int img = int[];

1: A.

2: D. -

3: C. boolean value=12;
4: B. -

5: C. rats
6: B. int [] img;

1. :
//
//
//

public class WhatHappens {


public static void main(String[] args) {
StringBuffer[] stringBuffers = new StringBuffer[10];

for (int i = 0; i < stringBuffers.length; i ++) {


stringBuffers[i].append("StringBuffer at index " + i);
}
}
}

. ; ;

:
1. length - ,
Unicode
.
2. append
. str
. ,
.

2. :
if (aNumber >= 0)
if (aNumber == 0) System.out.println("first string");
else System.out.println("second string");
System.out.println("third string");
a.
aNumber 3;
1

. .
, " NullpointerException".
10
for StringBuffer():

public class ThisHappens {


public static void main(String[] args) {
StringBuffer[] stringBuffers = new StringBuffer[10];

for (int i = 0; i < stringBuffers.length; i ++) {


stringBuffers[i] = new StringBuffer();
stringBuffers[i].append("StringBuffer at index " + i);
}
}
}

2
a. :
second string
third string
3 0,
if. if 3 0. ,
else
if. Second . println
if .
third .

4:

1) (Application);
(A)
(B)

(C) ,

(D)

2) main() Java;
(A) main()
(B)
Java
(C) main()
(D) main() static

3)
main();
(A) public
(B) static
(C) void
(D)

4) argv[] arg[] main() :


(A)
(B)
(C) Java
(D)

5) - main();
(A) main()
(B) paint() main()
(C) main()
(D) main()

6) HTML ;
(A) <applet> </applet>
(B) <body> </body>
(C) <h1> </h1>
(D) <html> </html>

7) - ;
(A) applet
(B) Applet
(C) Apple
(D) AWT
8) -
;
(A) -
(B) - -
(C) - <applet>
</applet>
(D) - <applet> </applet>

1: A.
2: B.
Java
3: D.
4: A.
5: A. main()
6: D. <html> </html>
7: B. Applet
8: D. - <applet> </applet>

1. import " * "


;

2.
- ;

1
public

, -.

2
, pixels -
HSPACE VSPACE
HTML . HSPACE (
- ), VSPACE .
5:
1) Java;
(A) java.input
(B) java.io
(C) java.inout
(D) java.file

2) ;
(A) -
(B) -
(C) -
(D) -

3)
;
(A) InputStream
(B) OutputStrream
(C) Reader
(D) Writer

4)
write() ;
(A) flush()
(B) close
(C) write()
(D) print()

5)
;
(A) InputStream
(B) BufferInputStream
(C) DataInputStream
(D) LineNuumberInputStream

6) ;
(A) - ,
(B) -
(C) -
(D) -
1: B. java.io
2: C. -
3: C. Reader
4: A. flush()
5: A. InputStream
6: A. - ,

6:

1) URL;

(A)

(B)

(C)

(D)

2) URl:

(A)

(B)
URL

(C)

(D) a b

3) URL :

(A)

(B)

(C)

(D)

4) URL ;

(A)
(B)

(C)

(D)

1: D.
2: A.

3: B.
4: A.

1. - .
.
- :



-
getDocumentBase():

public void init()


{
Button button = new Button("Bring up URL in frame");
add(button);
button.addActionListener (this);
validate();

try
{
myURL= new URL(getDocumentBase(), "k121.html");
}
catch (MalformedURLException e) {}


actionPerformed() showDocument().
AppletContext URL :
public void actionPerformed(ActionEvent e)
{
getAppletContext().showDocument(myURL, "frame1");
}

7:
1) ;
(A) getCodeBase()
(B) getDocumentBase()
(C) a b
(D)

2) Image:
(A)
(B)
(C)
(D)

3) ;
(A) CropImage()
(B) setClip()
(C) setSize()
(D) getImage()

4) - :
(A) new
(B) getImage()
(C) a b
(D)

5) MemoryImageSource;
(A) ImageProducer
(B) ImageConsumer
(C) Image
(D)

6) CropImageFilter:
(A)
(B)
(C)
(D) x

1: C. a b
2: A.
3: B. setClip()
4: B. getImage()
5: A. ImageProducer
6: C.

8:

1. (animation) Sprite;
(A) animation
(B) animation
(C) a b
(D)

2. ;
(A) play()
(B) getAudioClip()
(C) play() getAudioClip()
(D)

1: C. a b
2: C. play() getAudioClip()

Java .
Java (Javac)
class . Java (Java)
.
Java (global) ,

.
. Java
.
Java
final .
Java.util (HahTable)
. ,
.
Object Java -
Java , .
,
.

Random Java.util.
netx( ).
<param name="VERTICAL TEXT 7" value="
. .
, MemoryImageSource
createImage.

update(),
.


Java . ,
.
String .
'+' .

toString.
, toString
.
null Java,
.
' \ ' C.
' = =' ,
equals( ).

.
.
.
.

Vector .



. ,
.

Listener .
: , , . ,
, .

.
.
:
.


Java ,
Font.Undrerline.
Graphics.drawLine(), .

Color
(,, )
0-255.
Graphics
create.
, ,
.
,
,
Fontmetrics.

. , Font.BOLD +
Font.ITALIC .

Java .
.
.
Runnable .
. Runnable ,
Java.


.
join(), .

animation.
stop , . stop(),
.
.
stop().



HTML
Applet .

, (browser)
.
. ,
(browser),
HTML
.

getComponents( ).

' \ n ' .

Java: Runtime (
Exception) ( Error).
Throwable.

NullPointeException
. null.
Java ,

try-catch.

abstract ( )


. .
.
.. ,
.

API (Application Programming Interface )

, Java.

Applet ( )

Java
( Web)
(Web browser).

appletviewer ( )

Sun
Java.

application ()

Java appletviewer
AWT (Abstract Windowing Toolkit)

Java ,
.

base class ( )

boolean ()

. boolean ( )
true false.

byte

Java 8-bit
.

Bytecodes ( bytes)

Java - . byte
.
.class.

callback ()


.
cast ( )

catch

try.
catch
.

char

Java. char 16-bit


Unicode.

check box group ( )

. (check box)
CheckBoxGroup Java,
(radio button).

Class ()

(template)
(. integer).

Class library ( )

Java.
Java.

Class Loader ( )

Java
.
.

Class method ( )
static.
this
.

Class Path ( )

Class Variable ( )

static.
.

Client ( )

client/server.
(server) .

Client / server model ( / )

,
, client / server.
/ .

Clipping Rectangle ( )

Compiler ()

Java byte.
.java .class .

Compiler ()

Component ()

GUI.
Constructor ( )

,
.
.

Container

container Java. container


.

Content Handler ( )

control ( )

, ,
, .

Critical section ( )


. Java,
synchronized().

datagram

,
.

debugger ( )

Java
. debugger
.
double

Java. double 16-bit


.

Double buffering ( buffer)

, buffer
.

encapsulation ()

Event ()

, , ,
.

event model ( )

Exception ()

,
. ,
.

extends

extends
- .

final
final ,
.
.
.

finalize

,
.

finally

finally try/ catch/ finally.


try catch. .

float

Java. 32- bit


.

frame ()

garbage collector ( )

graphics content ( )

java.awt.Graphics.
.

GUI (Graphical User Interface)

,
, , . Java GUI
java.awt.
H

hashtable

.
.

hostname

Internet.

HotJava

www Java,
Java.

HTML (Hypertext Markup Language)

, .

ImageConsumer ( )

.
awt.peer.

ImageObserver ( )


ImageConsumer.
awt.peer.

implements


. .
extends.
import

Java .
Java.

image map ( )

, .

Inheritance ()

Inner Class ( )

.
.

instance ()

. ,
.

instanceof

Java true,
.

int

Java 32 bit
.

interface

. ,
.

ISP (Internet Service Provider Internet)

,
Internet .
J

Java 2D

rendering Java.

JavaBeans

Java.
.

Java Beans

Java Beans Java


.

Java Virtual Machine

byte Java.

JDK (Java Development Kit)

Sun
Java.

layout ()

Java.
Java.

local variable ( )

.
.

long
Java 64-bit
.

member ()

, ,
.

method ()

method overloading ( )

,
.

method overriding ( )

-.

.

modifier ()

,
, .

Multi-threading ()

.
.

N
native

.
C, . native
. ,
native.

new

null

, 0 Java.

Object ()

. .

Overloading ( )

,

.

Overriding ()

,
.

package

Java.
Panel ( )

peer

GUI .
peer Toolikit.

private


. private .

protocol handler ( )

.
: StreamHandler URLConnection.

public


. public
. public .

reference type ( )

. ,
"".
.
S

server ()

short

Java 16- bit


.

Source file ( )

Java -
.

static

.
static
.

stream

. / Java
streams.

Subclass ( )

super

this :
.

Superclass ( )

.
T

this

Java .
this .

Thread ()

.
.

throw

throw
.
catch.

throws


.
Error RuntimeException
throws .

Thumb ( )

Toolkit

Java API
.

try

try catch
finally . .
U

Unicode

16-bit
, . ASCII
Latin-1 Unicode. Java
Unicode char String.

Vector

Web browser ( )

Internet Explorer,
Internet.

Web server ( Web)

Internet .

World Wide Web

HTTP, FTP
Internet.

1. "Java Developer's Reference", Mike Cohn, Bryan Morgan, Micheal Morrison, Micheal t.
Nygard, Dan Sashi, Tom Trinko, SamNet, 1996, ISBN 1-57521-129-7.
2. "Professional Java Fundamentals", Shy Cohen, Tom Mitchell, Adres Gonzalez, Lary
Rodriges, Kerry Hammil, WROX PRESS Ltd, 1996, ISBN 1-861000-38-3.
3. "Exploring Java", Patrick Niemeyer & Joshua, O' REILLY, 1997, ISBN 1-
56592-271-9.
4. " Java", Laura Lemay, Charles L Perkins, Michael Morrison, .
, 1996, ISBN 960-512-083-6.

1. http://www.halcyon.com/mclain/java.htm
2. http://www.javacoffeebreak.com/tutorials/index.html
3. http://journals.ecs.soton.ac.uk/java/tutorial/
4. http://www.theparticle.com/
5. http://www.oreilly.com/
6. http://www.devx.com/
7. http://java.sun.com/
8. http://softwaredev.earthweb.com/java
9. http://www.dgp.toronto.edu/~mjmcguff/learn/java
10. http://www.javaboutique.internet.com/
11. http://www.javapplets.com/
12. http://www.freewarejava.com/
13. http://www.javapowered.com/
14. http://www.zdnet.com/devhead/resources/scriptlibrary/applets
15. http://www.coffeecup.com/java/
16. http://www.apl.jhu.edu/~hall/java/CGI-with-Java.html
17. http://www.epm.ornl.gov/java/
18. http://www.cs.washington.edu/
19. http://www.telecom.ntua.gr/HTML.Tutorials/java
20. http://www.cs.aston.ac.uk/~beaumoaj
21. http://www.essi.fr/~sander/courses/Java/Examples
22. http://laurel.actlab.utexas.edu/~cynbe/java/classes/applets.html
23. http://www.programming.de/

You might also like