Вы находитесь на странице: 1из 49

1

" "

" "

2004 .
2
3

.............................................................................................................3
........................................................................3
..............................................................................3
..................................................................................................4
.............................................................................................4
...............................................................................................................6
1. ..............................................................7
Graphics..........................................................................................7
...................................................................................21
Java2D..................................................................25
2. .........................................................31
Cursor...........................................................................................................31
3. ................................35
...................................................................................35
.........................................................................36
4. ...........................................................................38
Thread Runnable....................................................................39
5. ...............................43
6. .....................................44
4

1. java.applet java.applet.Applet.
2. .
3. java.awt.geom, java.awt
java.awt.Graphics java.awt.Graphics2D.
4. .
5. .
6.

,
.
,
() ,
, .

, ,
:
o ,
java.awt, java.awt.geom
java.awt.Graphics java.awt.Graphics2D.
o ,
. ,
(,
Thread.sleep()).
/ .
5
, ,
, ( ,
, ).
o ( )
.
, ,
.

:
1. , .
2. ,
.
3. .

1. ,
*.class,
<applet></applet> html ?
2. java.applet?
3. ?
4. ?
5. ?
6. Java (
, main()).
7. ,
?
8.
6
,
.
7

:
1. , , .
Java.
2. Java http://java.sun.com/ (
).
3. Java 2 SDK, Standard Edition Documentation
http://java.sun.com/products/jdk/1.5/index.html.
4. , , . Java (The
Java Language Specification http://www.javasoft.com/docs/books/jls/).
http://www.uni-vologda.ac.ru/java/jls/index.html
5. Eclipse http://www.eclipse.org/.

:
1. . Java.
2. . Java 2: .
3. . . Java.
4. .. Java.
5. .
Eclipse http://lib.juga.ru/article/articleview/174/1/69/.
8
1.


.
.
Color,
Font.
,
(0, 0) ,
, .
.
Graphics Graphics2D,
Java 2.
, .
Graphics
Graphics2D.
Java
,
Graphics getGraphics() Component
paint() update().
,
Graphics.
Graphics
,
, .
setColor (Color newColor),
newColor Color.
getColor (),
Color.
9

, Java, , ,
Color. .
:
Color(int red, int green, int blue)
, red, green
blue . RGB.
0 ( ) 255 (
). :
Color pureRed = new Color(255, 0, 0);
Color pureGreen = new Color(0, 255, 0);
- pureRed -
pureGreen .

0.0 ( )
1.0 ( ):
Color(float red, float green, float blue)
, Color someColor = new Color(O.OSf, 0.4f, 0.95f);

Color(int rgb)
. 1623
, 815 , 07
. , Color = new Color(OXFF8F48FF);
0x8F,
0x48, 0xFF.

Color(int red, int green, int blue, int alpha)
Color(float red, float green, float blue, float alpha)
Color(int rgb, boolean hasAlpha)
, "",
10
.
. 255 1.0,
, .
0 0.0, ,
.
,
2431, hasAlpha true.
hasAlpha false, 255,
, rgb.
,
255 1.0.

Color(ColorSpace cspace, float[] components, float alpha)
(color model)
RGB, : CMYK, HSB, CIEXYZ,
ColorSpace.
HSB

getHSBColor(float hue, float saturation, float brightness).


,
Color,
Color. "Code Conventions"
: black, blue, cyan, darkGray, gray, green,
lightGray, magenta, orange, pink, red, white, yellow.
Color
: getRed(), getGreen(), getBlue(), getAlpha(), getRGB(), getColorSpace(),
getComponents().
brighter() darker()
. ,
, ,
11
.
,
RGB HSB :
float[] RGBtoHSB(int red, int green, int blue, float[] hsb)
int HSBtoRGB(int hue, int saturation, int brightness)
, .


drawLine(int x1, int y1, int 2, int y2)

(x1, y1) (2, 2).
, ,
, (, ) drawLine (x,
, , ) . , ,
.
:
drawRect(int x, int , int width, int height)
, ,
(, ), width height ;
draw3DRect(int x, int , int width, int height, boolean raised)
, ,
raised true, ,
raised false;
drawOval(int x, int , int width, int height) ,
, . width == height,
;
drawArc(int x, int , int width, int height, int startAngle, int arc)
, ,
. arc
startAngle. .
12
,
;
drawRoundRect (int x, int , int width, int height, int arcWidth, int
arcHeight) .
,
arcwidth arcHeight,
;
drawPolyline(int[] xPoints, int[] yPoints, int nPoints)
(xPoints[i], ypoints[i])
nPoints;
drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
,
;
drawFoiygon(Polygon p) ,
Polygon.
Polygon
, ,
.
:
Polygon () ;

Polygon(int[] xPoints, int[] yPoints, int nPoints)

(xPoints[i], yPoints[i]) nPoints



addPoint(int x, int )
contains() ,
,
, .
intersects() ,
,
, , .
13
getBounds() getBounds2D() ,
.
Graphics.
, : fillRect(), fill3DRect(), fillArc(), fillOval(),
fillPolygon(), fillRoundRect(). ,
, .
, ,

:
public void paint(Graphics g)(
Color initColor = g.getColor(); //
g.setColor(new Color(0, 0, 255)); //
//
g.fillRect(0, 0, getSize().width-1, getSize().height-1);
g.setColor(initColor); //
//
}
, Graphics
. , (
setBackground() omponent).
,
Graphics2D,
Java 2D. ,
setBackground(Color ).
Graphics2D,
Graphics .

,
(, ), , Graphics :
drawString (String s, int x, int y) s;
14
drawBytes(byte[] b, int offset, int length, int x, int ) length
b, offset;
drawChars(char[] ch, int offset, int length, int x, int ) length
ch, offset.
, ,
AttributedCharacterIterator.
:
drawString(AttributedCharacterIterator iter, int x, int y). (, )
(baseline)
.

setFont(Font newFont) Graphics
.
getFont () .
Java, Font. ,
.

Font (glyphs) ,
. :
Font (Map attributes)
attributes .
TextAttnbute java.awt.font.
Java 2D .
Font (String name, int style, int size) name,
style size .
JDK 1.1, Java 2D
.

0,376 , , 1/72 . -
1/72 , 0,351 . -
15
.
name ,
, "Courier New", "Dialog", "Dialoglnput",'
"Monospaced", "Serif", "SansSerif", "Symbol".
(logical font names). name == null,
.
style Font:
BOLD ;
ITALIC ;
PLAIN .
(bolditalic)
, Font. BOLD | Font. ITALIC.

(font face name)
(font name). ,
.
, "Serif"
(family) Times New Roman,
Times New Roman Bold, Times New Roman
Italic.
, .

:
Font[] fnt = Toolkit.getGraphicsEnvironment.getAHFonts();
for (int i = 0; i< fnt.length; i++)
System.out.println(fnt[i].getFontName());
SUN J2SDK Lucida.
SDK, , .


16
font.properties;
font.properties.ar;
font.properties.ja;
font.properties.ru.
. . JDK
jdkl.3\jre\lib - lib JDK
, .
Java
. ,
user.language.
"ru",
font.properties.ru.
, font.properties,
.
, str
drawString(), Font,
, , "Serif".
Java font.properties,
,
, , Times New Roman.
Unicode-, str
Unicode .


: ,
. :
.
FontMetrics.
Java 2D FontMetrics TextLayout.
, ,
17
FontMetrics.
FontMetrics
FontMetrics ,
.
FontMetrics, f,
getFontMetrics (f) Graphics Component.
FontMetrics ch
charWidth(ch),
getChars() getBytes(),
str stringWidth(str).

.
(leading)
, ,
, , getLeading().

(ascent)
getAscent(), getMaxAscent().

(descent) getDescent(),
getMaxDescent().
, (height) ascent + descent + leading
getHeight().
.
. 1.

. 1.
18
19

LineMetrics java.awt.font.
getLineMetrics () FontMetrics.
1 ,
. 2 .
1.
import java.awt.*;
import j ava.awt.event.*;
class GraphTest extends Frame{
GraphTest(String s) {
super(s);
setBounds(0, 0, 500, 300);
setVisible(true);
}
public void paint(Graphics g){
Dimension d = getSize();
int dx=d.width/20, dy=d.height/20;
g.drawRect(dx, dy+20, d.width-2*dx, d.height-2*dy-20);
g.drawRoundRect(2*dx, 2*dy+20, d.width-4*dx, d.height-4*dy-20, dx, dy);
g.fillArctd.width (2-dx, d.height-2*dy+1, 2*dx, dy-1, 0, 360);
g.drawArctd.width (2-3*dx, d.height-3*dy/2-5, dx, dy/2, 0, 360);
g.drawArctd.width (2+2*dx, d.height-3*dy/2 - 5, dx, dy/2, 0, 360);
Font fl = new Font("Serif", Font.BOLD(Font.ITALIC, 2*dy);
Font f2 = new Font ("Serif", Font.BOLD, 5*dy/2);
FontMetrics fml = getFontMetrics(fl);
FontMetrics fm2 = getFontMetrics(f2);
String s1 = " ";
String s2 = " .";
String s3 = " ";
int firstLine = d.height/3;
20
int nextLine = fml.getHeight();
int secondLine = firstLine+nextLine/2;
g.setFont(f2);
g.drawstring(s3, (d.width-fm2.stringWidth(s3))/2, firstLine);
g.drawLine(d.width/4, secondLine-2, 3*d.width/4, secondLine-2);
g.drawLine(d.width/4, secondLine1, 3*d.width/4, secondLine-1);
g.drawLine(d.width/4, secondLine, 3*d.width/4, secondLine);
g.setFont(fl);
g.drawstring(s1,(d.width-fml.stringWidth(s1))/2, firstLine+2*nextLine);
g.drawString(s2,(d.width-fml.stringWidth(s2))/2, firstLine+3*nextLine);
}
public static void main(String[] args){
GraphTest f = new GraphTest(" ");
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent ev){
System.exit(0);
}
});
}
}
1 Dimension,

width height. getSize() component
Dimension. 1
f GrapTest setBounds()
500x300 .
1 ,
,
.
21

. 2. Graphics

Graphics
,
.
, ,
, , .
:
, , 1,
, ,
getScreenSize() Java.awt.Toolkit
. ,
.
Java 2 Graphics, Java 2D,
Graphics2D.
Java 2D
Java 2D,
Graphics2D java.awt,
:
, Graphics
(User Space),
(Device Space): ,
. Graphics2D (transform)

.
22

"",
, ,
/ /.
AffineTransform.
setTransform(). " "
transform() translate()
concatenate().
,
, .
: , , .,
shape java.awt.
draw(),
, shape.
fill(), ,
shape.
(Stroke) (n).
Stroke. BasicStroke
. :
o (width) ( )
;
o (end cap)
CAP_ROUND, CAP_SQUARE (
),
CAP_BUTT;
o (line joins)
JOIN_ROOND, JOIN_BEVEL,
JOIN_MITER ( );
o (dash)
-, ,
23
,
.
Paint.
. Color (solid)
, GradientPaint (gradient) ,

, TexturePaint
(pattern fill).
, . . ,
shape, draw()
. ,
.
, ,
, , ,
, .
, .
,
.
TextAttribute.
(rendering) (hints),
RenderingHints.
Java 2D
, . ,
, .


(transform)
, .
setTransform() , .
AffineTransform
24
java.awt.geom, olor Font
.
AffineTransform.
AffineTransform

AffineTransform:
AffineTransform(double a, double b, double , double d, double e, double f)
AffineTransform (float a, float b, float c, float d, float e, float f)
(, )
(*+*+, b*x+d*y+f)
.

, .

, , , .

{, b, , d, e, f} {a, b, c, d}, e = f = 0,
:
AffineTransform(double[] arr)
AffineTransform(float[] arr)
,
, :
AffineTransform(AffineTransform at)

:
AffineTransform()
,
, ,
57 (20, 40).

25
, AffineTransform.
getRotateInstance (double angle) angle,
, .
, .
,
angle .
getRotateInstance(double angle, double x, double )
(, ).
getScalelnstance (double sx, double sy)
sx , sy .
getSharelnstance(double shx, double shy) (x,
) (x+shx*y, shy*x+y).
getTranslateInstance (double tx, double ty) (, )
(x+tx, y+ty).
createInverse() ,
.
:
setTransform(AffineTransform at)
setTransform(double a, double b, double c, double d, double e, double f)
setToIdentity()
setToRotation(double angle)
setToRotation(double angle, double x, double y)
setToScale(double sx, double sy)
setToShare(double shx, double shy)
setToTranslate(double tx, double ty)
, .
, :
concatenate(AffineTransform at)
rotate(double angle)
rotate(double angle, double x, double y)
26
scale(double sx, double sy)
shear(double shx, double shy)
translate(double tx, double ty)
,
.
, preConcatenate(AffineTransform at),
, .
AffineTransform
.
. paint() 1
, 2.
2.
// 1...
public void paint(Graphics gr){
Graphics2D g = (Graphics2D)gr;
AffineTransform at =
AffineTransform.getRotatelnstance(-Math.PI/4.0, 250.0,150.0);
at.concatenate(new AffineTransform(0.5, 0.0, 0.0, 0.5, 100.0, 60.0));
g.setTransform(at);
Dimension d = getSize();
// 1...
paint() gr Graphics2D
gr Graphics2D. ,
getRotatelnstance() 45
(250.0, 150.0). at
AffineTransform. concatenate(), at,

(100.0, 60.0). ,
g
setTransform().
27
.
,

(100.0, 60.0) .
45 (250.0,
150.0).
. 3.

. 3.

Java2D

Stroke. Java 2D ,
BasicStroke.
BasicStroke
BasicStroke .

BasicStroke(float width, int cap, int join, float miter, float[] dash, float
dashBegin)
:
width ;
cap; :
o CAP_ROUND ;
o CAP_SQUARE ;
28
o CAP_BUTT ;
join; :
o JOIN_ROUND ;
o JOIN_BEVEL ,
;
o JOIN_MITER ;
miter,
JOIN_MITER;
dash;
,
;
;
dashBegin,
dash.

:
BasicStroke (float width, int cap, int join, float miter) ;
BasicStroke (float width, int cap, int join)
JOIN_ROUND JOIN_BEVEL;
JOIN_MITER miter = 10.0f;
BasicStroke (float width) CAP_SQUARE
JOIN_MITER miter = 10.0f;
BasicStroke () 1. 0f.
3 ,
4 , .
3.
import java.awt.*;
import java.awt.geom. *;
import java.awt.event.*;
class StrokeTest extends Frame{
29
StrokeTest(String s) {
super(s) ;
setSize(500, 400);
setvisible(true);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent ev)(
System.exit(0);
}
});
}
public void paint(Graphics gr){
Graphics2D g = (Graphics2D)gr;
g.setFont(new Font("Serif", Font.PLAIN, 15));
BasicStroke pen1 = new BasicStroke(20, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER,30);
BasicStroke pen2 = new BasicStroke(20, BasicStroke.CAP_ROUND,
BasicStroke.JOIN_ROUND);
BasicStroke n = new BasicStroke(20, BasicStroke.CAP_SQUARE,
BasicStroke.JOIN_BEVEL);
floatf] dash1 = {5, 20};
BasicStroke pen4 = new BasicStroke(10, BasicStroke.CAP_ROUND,
BasicStroke.JOIN_BEVEL, 10, dashl, 0);
float[] dash2 = (10, 5, 5, 5};
BasicStroke pen5 = new BasicStroke(10, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_BEVEL, 10, dash2, 0);
g.setStroke(pen1);
g.draw(new Rectangle2D.Double(50, 50, 50, 50));
g.draw(new Line2D.Double(50, 180, 150, 180));
g.setStroke(pen2);
g.draw(new Rectangle2D.Double(200, 50, 50, 50));
30
g.draw(new Line2D.Double(50, 230, 150, 230));
g.setStroke(n3);
g.draw(new Rectangle2D.Double(350, 50, 50, 50));
g.draw(new Line2D.Double(50, 280, 150, 280));
g.drawstring("JOIN_MITER", 40, 130);
g.drawstring("JOIN_ROUND", 180, 130);
g.drawstring("JOINJBEVEL", 330, 130);
g.drawstring("CAP_BUTT", 170, 190);
g.drawstring("CAP_ROUND", 170, 240);
g.drawstring("CAP_SQUARE", 170, 290);
g.setStroke(pen5);
g.drawfnew Line2D.Double(50, 330, 250, 330));
g.setStroke(pen4);
g.draw(new Line2D.Double(50, 360, 250, 360));
g.drawString("{10, 5, 5, 5,...}", 260, 335);
g.drawstring("(5, 10,...)", 260, 365);
}
public static void main(String[] args){
new StrokeTest(" ");
}
}
31

. 4. .


setStroke() draw() fill().
, draw()
Graphics2D, shape.
, , ,
, Rectangle2D, RoundRectangle2D, Line2D, Ellipse2D,
Arc2D, Point2D java.awt.geom.
CubicCurve2D QuadCurve2D
.
,
Double Float
. 3
Rectangle2D.Double Line2D.Double
.
GeneralPath
java.awt.geom.GeneralPath
,
, . ,
shape,
GeneralPath.
GeneralPath
GeneralPath() , ,
32
GeneralPath (Shape sh).
append (Shape sh,
boolean connect)
connect true,
.
. (0, 0),
(, ) moveTo (float x, float ).
(, ) :
lineTo(float x, float );
quadTo (float xi, float yl, float x,
float y),
curveTo(float xl, float yl, float x2, float y2, float x,
float y).
(, ).
closePath().
:
GeneralPath p = new GeneralPath();
p.moveTo(xl, yl); // ,
p.lineTo(x2, y2); // ,
p.lineTo(x3, ); // ,
p.closePath(); //
Paint.
Java 2D
Color, GradientPaint TexturePamt.
33
2.

Cursor
,
:
CROSSHAIR_CURSOR ,
- ;
DEFAULT_CURSOR ;
HAND_CURSOR " ",
- ;
MOVE_CURSOR ,
;
TEXT_CURSOR , ;
WAIT_CURSOR , .

:
E_RESIZE_CURSOR ;
N_RESIZE_CURSOR ;
NE_RESIZE_CURSOR , ;
NW_RESIZE_CURSOR , ;
S_RESIZE_CURSOR ;
SE_RESIZE_CURSOR , ;
SW_RESIZE_CURSOR , ;
W_RESIZE_CURSOR .
type
Cursor(int type).

getPredefinedCursor(int type), Cursor
.

34
getDefauitcursor(). .
, :
Cursor curs = new Cursor(Cursor.WAIT_CURSOR);
someComp.setCursor(curs);
somecomp
.


. CUSTOM_CURSOR.

createCustomCursor(Image cursor, Point hotspot, String name)
Cursor .
cursor
Image. name , null.
hotspot .
, , ,
getBestCursorSize(int desiredWidth, int desiredHeight)
Dimension.
.
, (0, 0).
, ,
, (32, 32). cursor ,
.
getMaximumCursorColors()
, , 256,
.
java.awt.Toolkit, .
Toolkit , Java
, .
Toolkit ,
35
Toolkit.getDefaultToolkit().
Window ,
, Frame, Toolkit getToolkit()
Window.
:
Toolkit tk = Toolkit.getDefaultToolkit();
int colorMax = tk.getMaximumCursorColors(); //

Dimension d = tk.getBestCursorSize(50, 50); // d


int w = d.width, h = d.height, k = 0;
Point p = new Point(0, 0); //
//
int[] pix = new int[w * h]; //
//
for(int i = 0; i < w; i++)
for(int j = 0; j < h; j++)
if (j < i) pix[k++] = 0xFFFF0000; //
//
else pix[k++] = 0; //
//
// (w, h),
// pix, w
Image im = createImage(new MemoryImageSource(w, h, pix, 0, w));
Cursor curs = tk.createCustomCursor(im, p, null);
someComp.setCursor(curs);

32 -
someComp.

ComponentEvent ,
36
, .
FocusEvent .
KeyEvent
, .
MouseEvent
.
37
3.


MouseEvent
:
MOUSE_PRESSED;
MOUSE_RELEASED;
MOUSE_CLICKED (
);
MOUSE_MOVED;

MOUSE_DRAGGED;

MOUSE_ENTERED;

MOUSE_EXITED.
:
public interface MouseListener extends EventListener{
public void mouseClicked(MouseEvent e);
public void mousePressed(MouseEvent e) ;
public void mouseReleased(MouseEvent e);
public void mouseEntered(MouseEvent e);
public void mouseExited(MouseEvent e);
}
public interface MouseMotionListener extends EventListener{
public void mouseDragged(MouseEvent e);
public void mouseMoved(MouseEvent e);
}

e.getx(), e.getv(),
38
e.getPoint(), Point.

e.getClickCount(), .
0.
, ,
e.getModifiers() inputEvent
inputEvent:
BUTTON1_MASK , ;
BUTTON2_MASK , ,
;
BUTTON3_MASK , .

KeyEvent :
KEY_PRESSED;
KEY_RELEASED;
KEY_TYPED.
- ,
, ,
<Shift>+<>. <lt>- MS
Windows. , <F1>,
KEY_TYPED.
,
:
public interface KeyListener extends EventListener{
public void keyTyped(KeyEvent e);
public void keyPressed(KeyEvent e);
public void keyReleased(KeyEvent e);
}
.
e.getKeyChar() Unicode char,
39
. ,
CHAR_UNDEFINED.
e.getKeyCode ()
int. KeyEvent ,
(virtual key codes), ,
VK_FI, VK_SHIFT, VK_A, VK_B, VK_PLUS.
KeyEvent.
. ,
, getKeyCode()
. ,
KEY_TYPED, VK_UNDEFINED.
, -
<Alt>, <Ctrl>, <Meta>, <Shift>,
inputEvent getModifiers()
ALT_MASK, CTRL_MASK, META_MASK, SHIFT_MASK.
isAltDown(),
isControlDown(), isMetaDown(), isShiftDown().
40
4.

,

. threads.
.
"". thread "" "".

, Java .
, Java,
,
.
Java
, .
(main) - , ,
main() .
, , , . .
,
. ,
.
Java Thread.

start().

Thread.currentThread();
Thread Runnable.
run(). ,
. , Thread
run(), Thread ,
. run() .
41
run() , . .
. , .
run() ,
Java
start ().
, :
Thread Runnable.
Thread .
,
run(), , , -
.
, Thread.
Thread Runnable.
Thread :
Thread(ThreadGroup group, Runnable target, String name)
name, group
run() target. ,
, null;
Thread() run();
Thread(Runnable target);
Thread(Runnable target, String name);
Thread(String name);
Thread(ThreadGroup group, Runnable target);
Thread(ThreadGroup group, String name).
name , ,
Java
.
start().
Java run() -.
run().
42
-
null.

sleep(long ms) ms .
,
sleep(long ms, int nanosec).
4 TwoThreads Runnable.
Thread, TwoThreads3
. , ,
Applet JApplet.
4. Runnable
class TwoThreads3 implements Runnable{
private String msg;
TwoThreads3(String s){ msg = s; }
public void run(){
for(int i = 0; i < 20; i++){
try{
Thread.sleep(100);
}
catch(InterruptedException ie){}
System.out.print(msg + " ");
}
System.out.println("End of thread.");
}
public static void main (String[] args){
new Thread(new TwoThreads3("PING"), "Thread 1").start ();
new Thread(new TwoThreads3("pong"), "Thread 2").start ();
System.out.println();
}
}
43
,
: ,
, .
run() ,
, - null.
5 ,
run() , - th
go, .
stop(),
. , J2SDK.
-,
.
5.
class TwoThreads implements Runnable{
private String msg;
private Thread go;
TwoThreads(String s){
msg = s;
go = new Thread(this);
go.start();
}
public void run(){
Thread th = Thread.currentThread();
while(go == th){
try{
Thread.sleep(100);
}
catch(InterruptedException ie){}
System.out.print(msg + " ");
}
44
System.out.println("End of thread.");
}
public void stop(){go = null;}
public static void main(String[] args){
TwoThreads thl = new TwoThreads ("PING");
TwoThreads th2 = new TwoThreads ("pong");
try{
Thread.sleep(1000);
}
catch(InterruptedException ie){}
thl.stop(); th2.stop();
System.out.printlnf);
}
}
45
5.

Java
. wait()
notify(), ,
, (notify)
.
Object .
,
(wait set). " " .
wait(long millisec)
this, , millisec "
", .
,
, .
millisec , notify(),
. millisec 0,

, notify().
sleep() , wait()
.
" ", , ,
JLS.
wait() wait(0). wait (long
millisec, int nanosec) nanosec ,
.
notify() " " ,
. notifyAll()
.
.
46
47
6.

, .
Microsoft DHTML marquee, Java
Web .
HTML.

// AppletThread.java ,

import java.awt.*;

// ,
public class AppletThread extends java.applet.Applet implements Runnable{
String s; //
Thread thread; //

public void init(){


//
thread = new Thread(this);
thread.start();

// HTML
s = getParameter("Marquee");
}

public void run(){


//
for (;;){
char c; //
48
try{
// paint
repaint();

// 0.3
Thread.sleep(300);

// :
//1.
c = s.charAt(0);
//2.
s = s.substring(1, s.length());
//3.

s += c;
}
catch (InterruptedException e){}
}// for (;;)
}// run()

public void destroy(){


//
if (thread != null){thread = null;}
}

public void paint(Graphics g){


//
g.drawString(s, 10, 20);
}
}
49

HTML .
<!-- AppletThread.html >
<!-- , >

<applet code=AppletThread width=100 height=40>


<param name="Marquee" value="Hello, Thread!">
</applet>

.
paint() ,

paint()
, .
Component repaint().
repaint() ,
, update(Graphics g).
repaint() Java
.