(2) 18 6 26 Thread Thread Runnable ( ) Java 1 Sample91 Thread (Thread Runnable Thread ) Thread Runnable run() / Runnable run() run() Runnnale ( ) MyRun class MyRun implements Runnable Runnable (implements) run() import java.awt.*; import javax.swing.*; public class Sample91 extends JApplet { Image buf; Circle c1 = new Circle(Color.blue, 0, 100, 20); public void update(graphics g) { if(buf == null) { buf = createimage(getwidth(), getheight()); Graphics2D g2 = (Graphics2D)buf.getGraphics(); g2.clearrect(0, 0, getwidth(), getheight()); paint(g2); g.drawimage(buf, 0, 0, this); public void paint(graphics g) { Graphics2D g2 = (Graphics2D)g; c1.draw(g2); public void start() { (new Thread(new MyRun())).start(); class MyRun implements Runnable { public void run() { for(int i = 0; i < 200; ++i) { try { Thread.sleep(50); catch(exception e) { c1.moveto(c1.getx()+1, c1.gety()); 1
repaint(); static class Circle { double gx, gy, rad; public Circle(Paint c, double x, double y, double r) { col = c; gx = x; gy = y; rad = r; g.filloval((int)(gx-rad),(int)(gy-rad),(int)rad*2,(int)rad*2); public void moveto(double x, double y) { gx = x; gy = y; double getx() { return gx; double gety() { return gy; 2 Sample91 run()? ( )? interface Animation { public void addtime(double dt); public void draw(graphics2d g); Animation addtime() draw() go time Animation 20 a ( count ) init() 2 paint() import java.awt.*; import javax.swing.*; public class SampleA1 extends JApplet { Image buf; 2
boolean go; double time; Animation[] a = new Animation[20]; int count = 0; public void init() { a[count++] = new MovingCircle(Color.blue, 50, 50, 30); a[count++] = new MovingCircle(Color.red, 100, 100, 40); a[count++] = new MovingCircle(Color.orange, 100, 100, 30); public void update(graphics g) { if(buf == null) { buf = createimage(getwidth(), getheight()); Graphics2D g2 = (Graphics2D)buf.getGraphics(); g2.clearrect(0, 0, getwidth(), getheight()); paint(g2); g.drawimage(buf, 0, 0, this); public void paint(graphics g) { Graphics2D g2 = (Graphics2D)g; for(int i = 0; i < count; ++i) a[i].draw(g2); public void start() { go = true; (new Thread(new MyRun())).start(); public void stop() { go = false; class MyRun implements Runnable { public void run() { time = 0.001 * System.currentTimeMillis(); while(go) { try { Thread.sleep(100); catch(exception e) { double dt = 0.001 * System.currentTimeMillis() - time; for(int i = 0; i < count; ++i) a[i].addtime(dt); time += dt; repaint(); interface Animation { public void addtime(double dt); public void draw(graphics2d g); static class MovingCircle implements Animation { Color col; double gx, gy, rad; public MovingCircle(Color c, double x, double y, double r) { col = c; gx = x; gy = y; rad = r; public void addtime(double dt) { gx = gx +1 ; gy = gy; g.setcolor(col); g.filloval((int)(gx-rad), (int)(gy-rad), (int)rad*2, (int)rad*2); 3
start() go true stop() go false run() MyRun time go true 100 dt dt time time + dt go true Animation MovingCircle static class MovingCircle implements Animation { Color col; double gx, gy, rad; public MovingCircle(Color c, double x, double y, double r) { col = c; gx = x; gy = y; rad = r; public void addtime(double dt) { gx = gx +1 ; gy = gy; g.setcolor(col); g.filloval((int)(gx-rad), (int)(gy-rad), (int)rad*2, (int)rad*2); Sample91 Circle SampleA1 MovingCircle MovingCircle ( ) MovingCircle paint draw MovingCircle 10-1: (1) MovingCircle (2) MovinvCircle 100 x y 1 45 (3) MovingSquare (4) ChangeColorCircle ChangeColorSquare 4
! 2 import java.awt.*; import javax.swing.*; public class SampleA2 extends JApplet { Image buf; boolean go; double time; Animation[] a = new Animation[20]; int count = 0; public void init() { a[count++] = new FlyingCircle(Color.red, 100, 100, 40, 30, -40); a[count++] = new FlyingCircle(Color.orange, 100, 100, 30, 40, 50); a[count++] = new ResizingSquare(Color.green, 100, 100, 100); public void update(graphics g) { if(buf == null) { buf = createimage(getwidth(), getheight()); Graphics2D g2 = (Graphics2D)buf.getGraphics(); g2.clearrect(0, 0, getwidth(), getheight()); paint(g2); g.drawimage(buf, 0, 0, this); public void paint(graphics g) { Graphics2D g2 = (Graphics2D)g; for(int i = 0; i < count; ++i) a[i].draw(g2); public void start() { go = true; (new Thread(new MyRun())).start(); public void stop() { go = false; class MyRun implements Runnable { public void run() { time = 0.001 * System.currentTimeMillis(); while(go) { try { Thread.sleep(100); catch(exception e) { double dt = 0.001 * System.currentTimeMillis() - time; for(int i = 0; i < count; ++i) a[i].addtime(dt); time += dt; repaint(); interface Animation { public void addtime(double dt); public void draw(graphics2d g); static class FlyingCircle implements Animation { double gx, gy, rad, vx, vy; public FlyingCircle(Paint c, double x, double y, double r, double vx1, double vy1) { col = c; gx = x; gy = y; rad = r; vx = vx1; vy = vy1; public void addtime(double dt) { gx += vx*dt; gy += vy*dt; if(gx < 0 && vx < 0 gx > 300 && vx > 0) vx = -vx; if(gy < 0 && vy < 0 gy > 200 && vy > 0) vy = -vy; g.filloval((int)(gx-rad), (int)(gy-rad), (int)rad*2, (int)rad*2); static class ResizingSquare implements Animation { double gx, gy, len, time; 5
public ResizingSquare(Paint c, double x, double y, double l) { col = c; gx = x; gy = y; len = l; time = 0.0; public void addtime(double dt) { time += dt; int l = (int)(2.0*len + len*math.sin(time)) / 4; g.fillrect((int)(gx-l), (int)(gy-l), (int)(l*2), (int)(l*2)); FlyingCircle FlyingCircle (gx,gy) (x vx y vy) rad interface Animation { public void addtime(double dt); public void draw(graphics2d g); static class FlyingCircle implements Animation { static final int MAX_X = 300, MAX_Y = 200; double gx, gy, rad, vx, vy; public FlyingCircle(Paint c, double x, double y, double r, double vx1, double vy1) { col = c; gx = x; gy = y; rad = r; vx = vx1; vy = vy1; public void addtime(double dt) { gx += vx*dt; gy += vy*dt; if(gx < 0 && vx < 0 gx > MAX_X && vx > 0) vx = -vx; if(gy < 0 && vy < 0 gy > MAX_Y && vy > 0) vy = -vy; g.filloval((int)(gx-rad), (int)(gy-rad), (int)rad*2, (int)rad*2); (x, y) dt (x, y ) ( ) x = x + v x dt, y = y + v y dt. X /Y X Y MAX X MAX Y static final ResizingSquare static class ResizingSquare implements Animation { 6
double gx, gy, len, time; public ResizingSquare(Paint c, double x, double y, double l) { col = c; gx = x; gy = y; len = l; time = 0.0; public void addtime(double dt) { time += dt; int l = (int)(2.0*len + len*math.sin(time)) / 4; g.fillrect((int)(gx-l), (int)(gy-l), (int)(l*2), (int)(l*2)); ResizingSquare addtime() time paint() sin 1 10-2 a. ( ) ( : v y = v y +αdt α ) b. ( ) ( c. ( ) d. N ( : (x, y) (x + r cos(θ + 2πi N ), y + r sin(θ + 2πi N )) i = 0, 1,..., N 1 θ ) 10-3 / 3? Paint Color Paint Color? API Paint Paint (API implements ) 1 Color 1 Paint 2 GradientPaint ( ) TexturePaint 2 ( float ) new GradientPaint(x 1, y 1, c 1, x 2, y 2, c 2 ) 7
true new GradientPaint(x 1, y 1, c 1, x 2, y 2, c 2, true) ( 1) (x,y) w h 1: TexturePaint (x, y), w, h Rectangle2D.Float API new TexturePaint(img, new Rectangle2D.Float(x,y,w,h)) import java.awt.*; import java.awt.geom.*; import java.awt.image.*; import javax.swing.*; public class SampleA3 extends JApplet { Rect r0, r1, r2; public void init() { r0 = new Rect(Color.green, 80, 80, 100, 40); BufferedImage im = new BufferedImage(20, 20, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = (Graphics2D)im.getGraphics(); g2.setpaint(new Color(1f, 0f, 0f, 0.7f)); g2.filloval(0, 0, 20, 20); for(int x = 0; x < 20; ++x) for(int y = 0; y < x; ++y) im.setrgb(x, y, 0x00FFFFFF); TexturePaint tp = new TexturePaint(im, new Rectangle2D.Float(0, 0, 10, 20)); r1 = new Rect(tp, 100, 100, 80, 160); GradientPaint gp = new GradientPaint(90f, 90f, new Color(1f,1f,1f,0.1f), 180f, 180f, Color.blue); r2 = new Rect(gp, 150, 120, 100, 90); public void paint(graphics g) { Graphics2D g2 = (Graphics2D)g; r0.draw(g2); r1.draw(g2); r2.draw(g2); 8
class Rect { Paint pat; int xpos, ypos, width, height; public Rect(Paint p, int x, int y, int w, int h) { pat = p; xpos = x; ypos = y; width = w; height = h; g.setpaint(pat); g.fillrect(xpos-width/2, ypos-height/2, width, height); r0 r1 r2 9