java -2012
TRANSCRIPT
JAVA
"
JAVA"
-1854
: .
2012
. /
, 18/05/2011
Java. Java. . , . , Java, , , , . , / (input/output) Java. URL URL . , . , . , . , Java.
...
2011-
2012
.
.
. ,
.
,
. ,
'
.
+ 2:
4- 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.8Diassembler .9
1.3.9 Java .9
1.3.10 WWW .9
1.3.11 WWW . 10
1.4 Java.10
1.1: Java . 10
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:
2.10.5 abstract.
. 26
+ 2:
t 3: JAVA
3.1.28
3.2 .28-33
3.2.1 .28
3.1: 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.7
3.8: .39
3.6.2 /.40
3.6.3 .41
3.9: .41
3.6.4 .41-42
3.6.5Bitwise .42
3.10: Bitwise .42
3.6.6 .43
3.11: .43
3.6.7 .43-44
3.12: .44
.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 dowhile.48
ft 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
i- 7:
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
4- 5: /
5.1 (Streams).70
5.2 java.io.71
5.3 / .71-74
5.3.1Steams (Input Streams).71
5.3.2Streams (Output Steams).74
5.4 .75
5.5 .76-77
5.6 .78-80
* 6: IIRT,
6.1.81
6.2.82-84
6.3 URL.84-85
6.4 URL .85
6.4.1 openConnection.86
7.1.87
7.2 (Loading Images).88
7.2.1 getlmagef).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
i- 8:
8.1 (3^).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, (. ,). , 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.iava.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 .
/V JAR (Java Archive Files)
JAR , . JAR, jar.exe. - canvaser.java xanvaser.class BoxCanvas.class. (Zip) JAR . Jar.exe canvaser.jar :
C:\java-l\canvaser>jar cvf canvaser.jar*
" * " JAR, cvf jar.exe. .
1.1: oac jar.exe
c
t
x []
, . , , ( ).
f [jar -]
. , JAR ( ). , JAR
.
( ) stderr.
/*
Java , . , . Java : CLASSPATH.
, java.applet.Appplet java.
java , CLASSPATH. java , .
/V uivi- (Applet Viewer)
Java -, Web . - (applet viewer) java, - java.
- Web , web . ' HTML , . - 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 , .
Java cog
Java , .
/V 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 .
Java
Java
java
- 2:
: , .
, , .
. , , . . , Screen. , .
, Screen , . , , , .
Java :
: , . Java (handles).
. . :
className handleName;
className handleName .
: . new(). :
new className (Arguments);
className Arguments .
: . . (constructor) , . ,
. . , . :
import java.awt. * ;
public class text extends java.applet.Applet { TextFieldtextl;
public void initQ
{
textl= new TextFieldf'Hello Java")
}
}
.
: . , . :
import java.awt.Point ; class Reference
{
public static void main(String argsfj) {
Point ptl, pt2; ptl=new Point(50,50); pt2=ptl; ptl.x=100; ptl.y=100;
System.out.println('Point:" + ptl. + ","+ ptl.y); System.out.println('Point:" + pt2.x + ","+ pt2.y);
}
}
: ptl: 50,100
pt2: 50,100
ptl, pt2. , ptl, . pt2. pt2=ptl;
: . , . 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 (intx, inty) { 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 [] arrayOflntegersQ
{....
}
}
. , . , . . , . .
, . , .
.
. - (base class), - (subclass). -, .
, . - ( ), - ( ).
, - . , .
Java Object. -. . . ' .
Subclassing
. Subclassing . .
mac
, , . , :
, - , ' .
( ) - . , .
, "" - . ' , .
. - , .
- - . .
: -, -. -.
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 (...) AnwoOpyia
, :
interface Driveable {.
boolean startEngineQ;
voidstopEngineQ; float accelerate (float acc); boolean turn (Direction dir);}
, interface class.
. ' . public abstract . private protected.
MmA-Wm . .
public, static final.
. interface . , .
, . , -. extends :
public interface Fruit extends Food {.....}
" , , . .
(Packages)
. . , . Java , . .
;
Java. .
, .
Java.lang.
. default .
, . . :
OTo .
, .
.
. . . cs.technion.ac.il il.ac.technion.cs.
** java sun .
:
: . :
MDSomePath\src\YRODNNameOJNewPackage;
: . :
copy SomePath\*.java SomePath\src\YRODNNameOJNewPackage;
:
package YRODN NmaeOjPackage;
: , . .
javac -d SomePaMclasses SomePath\src\YRODNNOPN\*.java
NONP=Name Of New Package
: , .
, . import . :
Import com.Hyperg.InerG.Hyperspace.contol.MogmaFlow
(*) .
(Modifiers)
, . , , , Java.
, .
Java :
, : public,
protected private. static . abstract . final , . synchronized volatile, . native .
. . .
public . public, protected private. public
. private . protected, .
static
. . . static :
static int sum;
static final intMax =10;
final
final .
^""^ Java final. #define C++ const Java.
synchronized
Synchronized ( ). Synchronized Java . , .
. , (CPU) , . volatile (),
.
abstract
, . , "-". - . , -. abstract.
, . . . - .
^
^-"* - -. , .
- , . - .
abstract .
k\mm\ private static abstract.
4- 3: JAVA
. . Java :
- /* */: . :
/*
*/
/*
=
=
i- 5: /
Java, C C++, . . java.io , . streams (, ) pipes () .
(Streams)
/ (Input/ Output) Java . . , . , .
, :
A stream
dest
, :
mva.io
- java.io. , java.io . import.
IOException.
/
Steams (Input Streams)
import java.io. *; importjava.io.InputStream ;
. InputStream. , (buffer).
InputStream
InputStream Object . .
read( ). , . , . , -1. ' .
read :
public abstact int read () , .
public abstact int read (byte bfj ), byte .
public abstact int read (byte bfj, int off int len), len bytes
.
(buffer). :
s.read(buffer, t, x); bytes t .
byte . :
intwaiting.in.available ; byte [] data = newbyte; System.in.read (data);
available^)
bytes available(). :
public int available () throws IOException ;
skipf)
bytes bytes skip( ).
public int skip () throws IOException ;
:
if (s.skip( 256) 1 =256) System.outprintlnC'Skippedloss") ;
^^ bytes . bytes .
closed)
. . . , . .
close( ) :
public void close () throws IOException ;
, , .
.
:
InputStream s= alwaysMakeNewInputStream (); try{
} fimnally {
s.close() ;
}
markf) resetf)
"" . bytes. , bytes . reset( ) . :
InputStream s = getAnInputStreamFromSomeWhere(); if (s.markSupported () ) {
// stream
s.mark(1024);
// 1024 Bytes
s.reset();
} else {
}
.
Streams (Output Steams)
. . OutputStream Object . bytes .
writef )
. bytes . . :
public abstract void write (int b) throws IOException ; byte stream
public abstract void write (byte bfj ) throws IOException ; byte
stream
public abstract void write ( byte bfj, int off, int len) throws IOException ; len bytes
stream
. :
s.write (buffer, t, x); t .
flushi )
, , " ". flush( ). :
public voidflush() throws IOException;
closed)
. close( ) :
public voidclose() throws IOException;
bytes bytes read (byte b[]). ByteArraylnputStream : bytes . :
public ByteArraylnputStream (byte buff]);
public ByteArraylnputStream (byte buff], int offset, int len);
, bytes. . reset( ) , available( ) bytes . :
InputStream s= new ByteArraylnputStream (buffer);
: bytes .
StringBufferlnputStream ByteArraylnputStream, string. :
public StringBufferlnputStream (Strings);
BvteArravOutputStream , bytes. :
public ByteArrayOutputStream (); public ByteArraylnputStream (int size);
, bytes bytes. 32 bytes . size bytes .
ByteArrayOutputStream :
OutputStream anotherOutputStream = getTheOtherOutputStream(); ByteArrayOutputStream = new ByteArrayOutputStream(); fillwithUseful();
s.writeTo( anotherOutputStream);
StringBufferOutputStream ByteArrayOutputStream, string. :
public StringBufferOutputStream (Strings);
. .
:
public FilelnputStream (File file); public FilelnputStream (FileDescriptor fidObj); public FilelnputStream (String name);
Object, String, FileDescriptor.
, FileOutputStream :
public FileOutputStream (File file); public FileOutputStream (FileDescriptor fidObj); public FileOutputStream (String name);
FileDescriptor "" . getFD( ). :
FileDescriprtor fid = someFileStream.getFd();
InputStream sl= new FilelnputStreamffd); OutputStream s2= new FileOutputStream(fd);
^4**^ , . .
FileReader FileWriter farrago.txt outagain.txt:
import java.io. *;
public class Copy {
public static voidmain(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 = inreadQ) != -1) outwrite(c);
incloseQ; out.close();
}
}
To . : 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 []){ FilelnputStream in = null; FileOutputStream out = null; int inByte;
// if ) args.lenght 1=2) {
System.out.println ("Usage : java ByteCopy "); System.exit(O);
}
try{
/
in= new FilelnputStream (argsfOJ) ; //
out= new FileOutputStream (argsfJJ) ;
// byte inByte=in.read(); while (inByte !=-!){ out.write((byte) inByte) ; // byte inByte=in.read();
}
jcatch (EOFException eofe) {
//
} catch (FileNotFoundEcxeption jhfe) { //
System.outprintln ("File not found: " + argsfOJ) ; } catch (IOEcxeption ioe) {
System.errprintln (ioe.getMessage () ); {finally!
// try{
if(inl=nullin.close(); if(out!=nullout.close(); } catch (IOException e) {}
}
}
}
:
import java. .*;
class LineCopy {
static public void main (String args []){
FilelnputStream fis = null;
DatalnputStream in= null;
FileOutputStream fos = null;
DataOutputStream out = null;
String line;
int HnesRead = 1;
if() args. length != 2) {
System.outprintln ("Usage : java LineCopy ");
System.exit(O); }
try {
/
fis= new FilelnputStream (argsfOJ) ; in = new DatalnputStream (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();
} }
jcatch (EOFException eofe) {
//
} catch (FileNotFoundEcxeption fnfe) { //
System.outprintln ('File not found: " + argsfOJ) ; } catch (IOEcxeption ioe) {
System.errprintln (ioe.getMessage () ); {finally {
// try{
if (fis ! = null) fis.close(); if(in!=nullin.close(); if (fos ! = null) fbs.close(); if(out!=nullout.close(); } catch (IOException e) {}
}
}
}
4- 6: URL
"" World Wide Web, URL HTML. To URL Uniform Resource Locator, Internet. , . URL . , , FTP, , , . URL
, .
URL Java , . URL. URL, - , . .
URL , . URL :
http://iava.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
URL
iava.net URL Java, URL. Java
URL, . URL URL . :
URL aDoc = new URL ("http://iava.sun.com/i2ee/webservices/index.html");
To URL URL . (Relative) URL .
URL URL . URL HTML. , HTML : JHomePage.html. : Picture.html MKinds.html., Jhomepage.html. :
< a href= "Pictures.html" > Pictures
URL . URL . URL Java. URL "http://www.gamelan.com7 . URL URL. URLs URL. :
URL gamelan = new URL ("http://www.samelan.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.samelan.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()
.getRefQ
URL . URL HTTP Urls URL. URL HTTP Url.
URL URL.
URL URL. URL .
URL :
importjava.net.*; import java.io. *;
class ParselJRL { 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.getProtocolQ); System.out.println("host = " + aURL.getHostQ); System.out.printlnC'filename = " + aURL.getFile()); System.out.println("port = " + aURL.getPortQ); System.out.println("ref= " + aURL.getRefQ);
}
URL
URL openConnection( ) . URL Java URL . , Yahoo :
try {
URL yahoo = new URL("http://www.yahoo, com/"); yahoo. openConnectionQ;
} catch (MalformedURLException e) {// URLQ ...} catch (IOException e) {// openConnectionQ ...}
, openConnection(), URLConnection , URL URLConnection. IOException. URLConnection .
openConnection
openConnection() URL, URLConnection. URLConnection
URL . HTTP URL HTTP URL.
, URL URLConnection . : . URL :
URL url = new URL riittp://iava.sun.com/i2ee/webservices"):
URLConnection . PrintStream.
URLConnection connection = url.openConnectionQ; PrintStream outputStream = new PrintStream (connectiongetOutputStreamQ);
getOutputStream() . URL , UnknownServiceException. URL , URL. .
outputStream.println ("string= " + stringToReverse); outStream.closeQ;
i- 7:
. Java (animation), . (GIF ) . Java, .
. Java . , , Java . Java . , . .
Java , . : GIF JPEG. . AWT , . GIF JPEG Java, bitmap.
Java Image . , Applet Graphics - .
Image , bitmap . , .
Java " "
" ". " " Image " ", . Java ImageProducer , URL. Image, CropImageFilter. RGBImageFilter PixelGrabber ImageConsumer.
(Loading Images)
Image new( ). - .
Java .
Java,
getlmage() Applet. Image. Image :
import java.awt.Image;
getlmageO
getlmage() URL . :
** getlmage() , URL. URL.
- Duke " http:///Tl.gif'
getlmage() URL :
Image pic = getlmage(new URL("htty://Tl.sif'));
- :
public void init()
{
URL url; try{
pic = getlmage(new URL("htty://Tl.sif')); } catch (MalformedURLException e) {}
}
To .
* getlmage() : URL URL, .
URL Applet.
. getDocumentBaseO: 1 - .
. getCodeBase(): - . CODEBASE HTML .
HTML Java.
- getDocument Base() Duke.
Image dukelmagef] = new ImagefllJ;
for :
dukelmage[i]= getImage(getDocumentBase(), "T"+i+ ".gif');}
To getlmage() .
. Java .
getlmage() . , . , . - . - ( ) ImageObserver.
. drawlmage() Graphics. drawlmage( ):
drawlmage (Image img int , inty, ImageObserver observer); drawlmage (Image img intx, inty, intw, int h, ImageObserver observer); dawlmage (Image img, intx, inty, Color c, ImageObserver observer);
. drawlmage (Image img int x, inty, int w, int h, Color c, ImageObserver observer);
:
, y : w, h: c:
observer: , : - "Loading" . , .
UrlGif . x,y . (0,0), Java, ImageObserver. - - this:
public voidpaint( Graphics g) {
if (pic != null) {
g drawlmage(pic, 0,0, this);
}
}
Applet -
Component. this drawImageQ, getWidthQ getHightQ.
getlmage() , null. drawlmage() (null) . .
ImageObserver , imageUpdate( ), : Image, , , y , width height . :
ABORT = .
ALLBITS =
ERROR =
FRAMEBITS = HEIGHT = PROPERTIES =
SOMEBIT =
WIDTH =
UrlGif - :
public boolean imageUpdate (Image img int infoflags, intx, inty, int width, int height )
{
if ((infoflags & ERROR) != 0) return false; if ((infoflags &AIIBITS) != 0)
{
paint( getGraphicsQ ); return false;
}
return true;
}
To Image , .
drawImageQ :
screen.drawlmage(whig, xPos, 10, this);
. getWidhtQ getHeightQ Image
. , ImageObserver .
int iWidth = whig.getWidth(this); int iHeight = whiggetHeightfthis);
mmrnrn. getWidth() getHeight() -1. ImageObserver .
4. 25%. drawlmage() .
screen.drawlmage(whig xPos, lOJWidth /'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.
IndexColorModel . :
Color
Red
Green
Blue
black
0x00
0x00
0x00
blue
0x00
0x00
OxFF
cyan
0x00
OxFF
OxFF
darkGray
0x40
0x40
0x40
gray
0x80
0x80
0x80
green
0x00
OxFF
0x00
lightGray
OxCO
OxCO
OxCO
magneta
OxFF
0x00
OxFF
orange
OxFF
OxC8
0x00
pink
OxFF
OxAF
OxAF
red
OxFF
0x00
0x00
white
OxFF
OxFF
OxFF
yellow
OxFF
OxFF
0x00
To DirectColorModel 32bit . 32 bit.
Color Java. ColorModel . pixel . DirectColorModel IndexColorModel. .
unpack color
2^" unpack colors.java
importjava.awt.*; import java. applet. Applet ;
public class unpack color extends Applet {
public void init() {
int temp=Color. ewhite.getRGBQ; unpack colors(temp);
}
public void unpack colors(int the color) {
int red, green, blue; red= the color & OxOOFFOOOO; red=red 16;
green = the color & OxOOOOFFOO;
green =green >>8 ;
blue=the color & OxOOOOOOFF;
System.ou.println("red= " +red + "green = " + "blue=" = blue); }
}
, . Java . pixel . , pixels . : , .
Java :
. ImageProducer
. FilteredlmageSource
. MemorylmageSource
. ImageConsumer . PixelGrabber . ImageFilter . RGBImageFilter . CropImageFilter
ImageProducer pixels . .
FilteredlmageSource ImageProducer . :
new FilterImageSource(ImageProducer, ImageFilter);
ImageProducer getSource(). ImageFilter .
createlmage. .
MemorvImageSource ImageProducer pixels . .
ImageConsumer . .
PixelGrabber ImageConsumer pixels . Image ImageProducer. . PixelGrabber.
.
ImageFilter . FilteredlmageSource. .
. .
RGBImageFilter pixels RGB. canFilterlndexColorModel. boolean filterRGB pixel .
CropImageFilter ImageFilter .
4- 8:
Kivnonc(Animation)
. . (animation) . To animation , 10 20 . , (animation) 8 12 .
^^):
/VI (frame) : , web. - (). , .
. .
GIF .
> : , sprite animation, . .
.
(
:
. ,
. .
MediaTracker. Media Track . .
MediaTracker
Java , . . .
, , . ' .
. HTTP . .
. ClipRect().
- . .
.
Evoc
- (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 voidpaint(Graphicsg) {
g. setColor (Color, black); g.drawString("Frame " + frame, 0, 30);
}
, 100 , run() 100 repaint(). , paint() 100 . repaint() , . frame run() paintQ.
Miac
- world.gif car.gif. (frame) .
"" (Frames).
Image world; Image car;
public void init() {
String sir = getParameter("fps");
intfps = (str != null) ? Integer.parselnt(str) : 10;
delay = (fps > 0) ? (1000 /fps) : 100;
world = getImage(getDocumentBase(), "world.gif); car = getImage(getDocumentBase(), "car.gif);
}
paint() .
public voidpaint(Graphics g) {
update(g);
}
paintFrame():
public voidpaintFrame(Graphics g) { Dimension d = sizeQ;
intw =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;
//
gdrawlmagefcar, d.width - ((frame * 5) % w), (d.height - h)/3, this); // g.drawlmage(car, d.width - ((frame * 7) %w), (d.height - h)/2, this);
web Neko. - , , , - .
Image,
Image :
Image nekoPics[] = new Image[9J; Image currentlmg;
. y .
intx;
inty = 50;
Thread - .
Thread runner;
- :
public void initQ
{
String nekoSrcfJ = { "rightl.gif', "right2.gif, "stop.gif, "yawngif, "scratchl.gif, "scratch2.gif, "sleepl.gif, "sleep2.gif, "awake.gif"};
for (int i=0; i < nekoPicsJength; {
nekoPicsfi] = getImage(getDocumentBase(),nekoSrc[i]);
}
}
getlmage() .
run(). : , , , - .
. x,y . . for x y 10 pixels .
10 pixels . .
repaint(). sleep() .
voidnekoRun(int start, intend) {
for (int i = start; i < end; i += 10) {
= i;
if (currentlmg == nekoPics[0]) currentlmg = nekoPicsflJ;
else currentlmg = nekoPics[0]; repaintQ; pause(150);
}
}
repaint() paint() x y . currentlmg (null) drawImageQ.
public void paint(Graphics screen)
{
if (currentlmg != null)
screen.drawlmage(currentlmg, x, y, this);
}
pause() . . .
void pause (int time) {
try {Threadsleep(time);} catch (InterruptedException e) {}
}
-. - run() .
nekoRun(0, getSizeQ.width / 2);
nekorun(). . . - . x,y .
( (Sprite Animation)
sprite , , sprite. . sprite, . sprite . sprite Sprite Sprite Vector.
Sprite
Sprite sprite . . .
Sprite :
: .
: .
, : sprite. sprite .
: sprite.
: sprite sprite .
: sprite.
SpriteVector
SpriteVector sprite .
SpriteVector Vector, java.util. 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.addlmage(image,0); try
{
tracker.waitForlD(O);
}
catch(InterruptedException e){} return image;
}
waitForID() . MediaTracker addlmageQ.
extractlmages() . CropImageFilter. "CropImageFilter "
public void extractlmages(lmage imagestrip, Image images[],int num,int width, int height)
{
ImageProducer source = imagestrip.getSource();
for(int i=0; i