강의 10

eyelashesnectarineSoftware and s/w Development

Nov 3, 2013 (3 years and 11 months ago)

103 views



10



그래픽프로그래밍
2




10


강의

목표


Graphics


1à7ÝFŒ7

8¸8Œ8Œ

Shape
8x

?,*à#È

-É8x

8©HÌ


*ü7ä3Ø8¨-ØCì

8¨7Ý-É8x

8©HÌ


Aô8¨+l

8¨-ØCì

=Ì*à

-C

6”#ü+ˆ8¨2Œ

GÜ?0*°

? G8FŒ#È

-É8x

8©HÌ

자바프로그래밍

강원대학교

1

자바프로그래밍

강원대학교

Frame

2

자바프로그래밍

강원대학교

import javax.swing.JFrame;


public class EmptyFrameViewer

{


public static void main(String[] args)


{


JFrame frame = new JFrame();



final int FRAME_WIDTH = 300;


final int FRAME_HEIGHT = 400;



frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);


frame.setTitle("An Empty Frame");


frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



frame.setVisible(true);


}

}

3

자바프로그래밍

강원대학교

도형

그리기


프레임

6|7?

$øGI8x

?,*à?d

88F¨2P#È

?,*ð

?,*à#È

?d#Ù8x

?4:ø

.´E¼
(JComponent)
8x

F8)<8¸7?

"W6è:06è6°




JColorChooser, JComboBox, JFileChooser, JLabel, JList,
JMenuBar, JOptionPane, JPanel, JScrollBar, JSpinner,
JTextComponent
등이

모두

JComponent


서브클래스임


JComponent


paintComponent
+ˆ2À&?*°

?4;?


paintComponent
+ˆ2À&?#È

Fx7ÈF”

&€*ü$?

4?3ØB•7?

8ŒF¨

8Ä% 8p)•

호출됨


필요한

&€



F8)•?,(Ü8¨

=Ì8€

4?F½%T

&€


프레임을

6x8¨@Œ8p)•

>•2ÀGˆF¼$??4

$?4?

=q8p)•

.)8?4?Aà

&€


프레임이

$?*¬

8<$ø7ä7?

8ŒF¨

?4)X9€$??4

!ÌAô!Ô

&€


*ü7ä3Ø

&?(Ì?,*°

8¨7ÝF¨

F8)<8¸8x

8¨% 4?Aà

&€

&%

4

자바프로그래밍

강원대학교

import javax.swing.*;


public class EmptyFrameViewer

{


public static void main(String[] args)


{


JFrame frame = new JFrame();



final int FRAME_WIDTH = 300;


final int FRAME_HEIGHT = 400;



frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);


frame.setTitle("An Empty Frame");


frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



JComponent component = new JComponent(); //
부품을

구성


frame.add(component); //
부품을

프레임에

넣음



frame.setVisible(true);


}

}

5

자바프로그래밍

강원대학교

도형

그리기


JComponent

?Q=è?4

?4:ô??

8¼#È

paintComponent

+ˆ2À&?#È

Gˆ+¨7?

6x,h?∙


?,*à:ô

6~8€


Gˆ+¨7?

$øGI8x

?,*à)X+¨

$øGI

?,*à#È

?d#Ù8x

?J#È

Jcomponent


2P/@A¨(Ì
스를

만들어

프레임에

넣어

줘야





Jcomponent


2P/@A¨(Ì3Ø

public class
RectangleComponent

extends
JComponent

{


public void
paintComponent
(Graphics g)


{


// Recover Graphics2D


Graphics2D g2 = (Graphics2D) g;


...g2


이용하여

그림을

그림
⸮.


}



6

자바프로그래밍

강원대학교

public class RectangleComponent extends JComponent

{


public void paintComponent(Graphics g)


{


// Recover Graphics2D


Graphics2D g2 = (Graphics2D) g;



Rectangle box = new Rectangle(5, 10, 20, 30);


g2.draw(box);



}

}


RectanlgeComponent
A¨(Ì3Ø#È

3LD°A¨(Ì3Ø8¬

JComponent


paintComponent
+ˆ2À&?*°


8à9I8Œ
(override)



JComponent


paintComponent
+ˆ2À&?#È

6x,h?∙$ø

?,*à:ô

6~8€


8à9I8Œ%P

paintComponent
+ˆ2À&?#È

1à?5GI

8¬3ØBh3Ø*°

FŒ!Ì

? 2eFŒ
??

8¨?∙8x

Gˆ+¨
(JComponent)
7?

?,*ð

RectangleComponent.java

7

자바프로그래밍

강원대학교

import javax.swing.*;


public class RectangleViewer

{


public static void main(String[] args)


{


JFrame frame = new JFrame();



final int FRAME_WIDTH = 300;


final int FRAME_HEIGHT = 400;



frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);


frame.setTitle("A Rectangle Frame");


frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



RectangleComponent component = new RectangleComponent();


frame.add(component); //
부품을

프레임에

넣음



frame.setVisible(true);


}

}

RectangleViewer.java

8

자바프로그래밍

강원대학교

import javax.swing.JComponent;

import java.awt.Rectangle;

import java.awt.Graphics;

import java.awt.Graphics2D;


public class RectangleComponent extends JComponent

{


public void paintComponent(Graphics g)


{


// Recover Graphics2D


Graphics2D g2 = (Graphics2D) g;



Rectangle box = new Rectangle(5, 10, 20, 30);


g2.draw(box);




box.translate(15, 25);


g2.draw(box);


}

}

RectangleComponent.java

9

자바프로그래밍

강원대학교

컴파일할

때의

파일간

종속성


(
?M8t

E($ˆ7?
) RectanlgeComponent.java
D@8°

6ú8¨

RectanlgeViewer.java


@(D@8°FŒ)X??

FŒ+¨

6|%\


RectangleViewer
?4

RectangleComponet


1à7ÝFŒ?d

&€,l



반면

RectanlgeComponent.java


RectanlgeViewer.java
D@8°

6ú8¨


@(D@8°

?4#Ù

10

자바프로그래밍

강원대학교

Graphical Shapes


java.awt.Graphics2D


abstract void draw(Shape s)



java.awt.Shape
-

8¬BdDÌ8¨3Ø


Shape
8¬BdDÌ8¨3Ø*°

? G8F•

A¨(Ì3Ø

7<
: Rectangle
, Ellipse2D.Double,
Line2D.Double
&%


API
,l2P7?2P

java.awt.Shape
8x

=r6x.(+¨

Shape


속하는

도형들을







있음

11

자바프로그래밍

강원대학교

Graphical Shapes


Ellipse2D.Double
8t

Ellipse2D


!è.´A¨(Ì3Ø
(innerclass)



Ellipse2D.Double


1à7ÝFŒ?d

88F¨2P#È


import java.awt.geom.Ellipse2D

Ellipse2D.Double ellipse = new Ellipse2D.Double(x, y, width, height);

g2.draw(ellipse);

Ellipse

12

자바프로그래밍

강원대학교

Drawing Lines


To construct a line:



or,

Line2D.Double segment = new Line2D.Double(x1, y1, x2, y2);

Point2D.Double from = new Point2D.Double(x1, y1);

Point2D.Double to = new Point2D.Double(x2, y2);

Line2D.Double segment = new Line2D.Double(from, to);


To draw the line:

g.draw(segment);

13

자바프로그래밍

강원대학교

Drawing Strings

g2.drawString("Message", 50, 100);

14

g2.setColor(Color c)

g2.setFont(Font font)

자바프로그래밍

강원대학교

Colors


Standard colors: Color
A¨(Ì3Ø7?

static
Fx&?)•

9I8Œ%L6è

있음


Color.BLUE
,

Color.RED
,

Color.PINK

etc.



static
Fx&?
:
?P-ø

8¬3ØBh3Ø7?

2ÁFŒ:ô

6~??

A¨(Ì3Ø7?

FŒ!Ì+

정의되는

필드


Fx&?
:
+ˆ2À&?

7¬.´7?

2T6ì%P

-ô3L7t

1õ3L

15

자바프로그래밍

강원대학교

칼라

사각형

그리기

public void paintComponent(Graphics g)

{


Graphics2D g2 = (Graphics2D) g;




g2.setColor(Color.RED);


g2.fill(new Rectangle(0, 0, 200, 200));



g2.setColor(Color.YELLOW);


g2.fill(new Rectangle(50, 50, 100, 100));

}

16

자바프로그래밍

강원대학교

임의

칼라

사각형

그리기

public void paintComponent(Graphics g)

{


Graphics2D g2 = (Graphics2D) g;




g2.setColor(new Color(0.5f, 0.6f, 0.3f));


g2.fill(new Rectangle(0, 0, 200, 200));



g2.setColor(new Color(25, 200, 224));


g2.fill(new Rectangle(50, 50, 100, 100));

}

Color(float r, float g, float b)


r, g, b


0.0
-
1.0

Color(int r, int g, int b)
-

r, g, b


0
-
255

17

자바프로그래밍

강원대학교

복잡한

모양

그리기

18

자바프로그래밍

강원대학교

import javax.swing.JFrame;


public class CarViewer

{


public static void main(String[] args)


{


JFrame frame = new JFrame();



final int FRAME_WIDTH = 300;


final int FRAME_HEIGHT = 400;



frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);


frame.setTitle("Two cars");


frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



CarComponent component = new CarComponent();


frame.add(component);



frame.setVisible(true);


}

}

19

자바프로그래밍

강원대학교

import java.awt.Graphics;

import java.awt.Graphics2D;

import javax.swing.JComponent;


public class CarComponent extends JComponent

{


public void paintComponent(Graphics g)


{


Graphics2D g2 = (Graphics2D) g;



Car car1 = new Car(0, 0);




int x =
getWidth()

-

Car.WIDTH;


int y =
getHeight()

-

Car.HEIGHT;



Car car2 = new Car(x, y);



car1.render(g2);


car2.render(g2);


}

}

위치

WIDTH

HEIGHT

20

자바프로그래밍

강원대학교

import java.awt.Graphics;

import java.awt.Graphics2D;

import javax.swing.JComponent;


public class CarComponent extends JComponent

{


public void paintComponent(Graphics g)


{


Graphics2D g2 = (Graphics2D) g;



Car car1 = new Car(0, 0);




int x =
getWidth()

-

Car.WIDTH;


int y =
getHeight()

-

Car.HEIGHT;



Car car2 = new Car(x, y);



car1.render(g2);


car2.render(g2);


}

}


getWidth, getHeight


paintComponent


4?F½FŒ??

8¼#È

CarComponent
?Q=è

8Ä4?7??À

Gl>Ð%\



8<$ø7ä

A ?d*°

9¤9IFŒ+¨

paintComponent
?4

Gl>Ð%L??

8Ä% =\8Œ

88?Œ?4

8à?ø1ä%\

(
F8)•?,(Ü8x

4?F½F•



*ü7ä3Ø*°

&?(Ì?,FŒ7

8<$ø7ä

A ?d*°

-H!?
.(2l7È
!)

21

자바프로그래밍

강원대학교

복잡한

모양

그리기

22

자바프로그래밍

강원대학교

public class Car

{


public Car(int x, int y)


{


xLeft = x;


yTop = y;


}


public void render(Graphics2D g2)


{


g2.draw(body);


g2.draw(frontTire);


g2.draw(rearTire);


g2.draw(frontWindshield);


g2.draw(roofTop);


g2.draw(rearWindshield);


}



public static int WIDTH = 60;

// static field


public static int HEIGHT = 30;

// static field



private int xLeft; //
인스턴스

필드


private int yTop; //
인스턴스

필드


}

미완성
!

23

자바프로그래밍

강원대학교

import java.awt.Graphics2D;

import java.awt.Rectangle;

import java.awt.geom.Ellipse2D;

import java.awt.geom.Line2D;

import java.awt.geom.Point2D;


/**


A car shape that can be positioned anywhere on the screen.

*/

public class Car

{


/**


Constructs a car with a given top left corner


@param x the x coordinate of the top left corner


@param y the y coordinate of the top left corner


*/


public Car(int x, int y)


{


xLeft = x;


yTop = y;


}


24

자바프로그래밍

강원대학교

/**


Draws the car.


@param g2 the graphics context


*/


public void render(Graphics2D g2)


{


Rectangle body


= new Rectangle(xLeft, yTop + 10, 60, 10);


Ellipse2D.Double frontTire


= new Ellipse2D.Double(xLeft + 10, yTop + 20, 10, 10);


Ellipse2D.Double rearTire


= new Ellipse2D.Double(xLeft + 40, yTop + 20, 10, 10);



// The bottom of the front windshield


Point2D.Double r1


= new Point2D.Double(xLeft + 10, yTop + 10);


// The front of the roof


Point2D.Double r2


= new Point2D.Double(xLeft + 20, yTop);


// The rear of the roof


Point2D.Double r3


= new Point2D.Double(xLeft + 40, yTop);


// The bottom of the rear windshield


Point2D.Double r4

25

자바프로그래밍

강원대학교

Line2D.Double frontWindshield


= new Line2D.Double(r1, r2);


Line2D.Double roofTop


= new Line2D.Double(r2, r3);


Line2D.Double rearWindshield


= new Line2D.Double(r3, r4);




g2.draw(body);


g2.draw(frontTire);


g2.draw(rearTire);


g2.draw(frontWindshield);


g2.draw(roofTop);


g2.draw(rearWindshield);


}



public static int WIDTH = 60;


public static int HEIGHT = 30;


private int xLeft;


private int yTop;

}

26

자바프로그래밍

강원대학교

Reading
Text

Input

String input = JOptionPane.showInputDialog("Enter x");

double x = Double.parseDouble(input);

27

마우스

이벤트

처리

자바프로그래밍

강원대학교

28

자바프로그래밍

강원대학교

Mouse Events


마우스

8¨-ØCì*°

=Ì*àFŒ?d

88F¨2P#È

*ü7ä3Ø*à3Ø"<*°

1à7Ý


MouseListener
인터페이스

public interface MouseListener

{


void mousePressed(MouseEvent event);


// Called when a mouse button has been pressed on a component


void mouseReleased(MouseEvent event);


// Called when a mouse button has been released on a component


void mouseClicked(MouseEvent event);


// Called when the mouse has been clicked on a component


void mouseEntered(MouseEvent event);


// Called when the mouse enters a component


void mouseExited(MouseEvent event);


// Called when the mouse exits a component

}

29

자바프로그래밍

강원대학교

마우스

리스너

구현과

등록

public class MyMouseListener implements MouseListener

{


// Implements five methods

}

MouseListener listener = new MyMouseListener();

component.addMouseListener(listener);


30

자바프로그래밍

강원대학교

Mouse Event
처리

프로그램




RectangleComponentViewer


마우스를

클릭하면



위치로

사각형이

이동

31

자바프로그래밍

강원대학교

public class RectangleComponent extends JComponent

{


public RectangleComponent() {


box = new Rectangle(BOX_X, BOX_Y, BOX_WIDTH,


BOX_HEIGHT);


}


public void paintComponent(Graphics g) {


Graphics2D g2 = (Graphics2D) g;


g2.draw(box);


}


public void moveTo(int x, int y) {


box.setLocation(x, y);


repaint();
// RectangleComponent


새로

그려지게




}




private Rectangle box;

}


32


repaint


시스템이

컴포넌트

자신에게

paint
메소드를

호출하도록


.


paint
메소드는

paintComponent,
paintBorder, paintChildren
메소드를


례로

호출함

자바프로그래밍

강원대학교

public class RectangleComponentViewer

{


public static void main(String[] args) {


final RectangleComponent component = new RectangleComponent();


class MousePressListener implements MouseListener {


public void mousePressed(MouseEvent event) {


int x = event.getX();


int y = event.getY();


component.moveTo(x, y);


}


public void mouseReleased(MouseEvent event) {}


public void mouseClicked(MouseEvent event) {}


public void mouseEntered(MouseEvent event) {}


public void mouseExited(MouseEvent event) {}


}


MouseListener listener = new MousePressListener();


component.addMouseListener(listener);


JFrame frame = new JFrame();


frame.add(component);


frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);


frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


frame.setVisible(true);


}

}

33

자바프로그래밍

강원대학교

File
RectangleComponent.java

01:

import java.awt.Graphics;

02:

import java.awt.Graphics2D;

03:

import java.awt.Rectangle;

04:

import javax.swing.JComponent;

05:

06: /**

07: This component lets the user move a rectangle by

08: clicking the mouse.

09: */

10:

public class

RectangleComponent
extends

JComponent

11:

{

12:

public

RectangleComponent()

13:

{

14: // The rectangle that the paint method draws

15:

box =
new

Rectangle(BOX_X, BOX_Y,

16:

BOX_WIDTH, BOX_HEIGHT);

17:

}

18:


34

자바프로그래밍

강원대학교

File
RectangleComponent.java

19:

public void

paintComponent(Graphics g)

20:

{

21:

super.paintComponent(g);

22:

Graphics2D g2 = (Graphics2D) g;

23:


24:

g2.draw(box);

25:

}

26:


27: /**

28: Moves the rectangle to the given location.

29: @param x the x
-
position of the new location

30: @param y the y
-
position of the new location

31: */

32:

public void

moveTo(
int

x,
int

y)

33:

{

34:

box.setLocation(x, y);

35:

repaint();

36:

}

35

자바프로그래밍

강원대학교

File
RectangleComponent.java

37:


38:

private Rectangle box;

39:


40:

private static final int

BOX_X =
100
;

41:

private static final int

BOX_Y =
100
;

42:

private static final int

BOX_WIDTH =
20
;

43:

private static final int

BOX_HEIGHT =
30
;

44:

}

36

자바프로그래밍

강원대학교

마우스

리스너

구현

class MousePressListener implements MouseListener

{


public void mousePressed(MouseEvent event)


{


int x = event.getX();


int y = event.getY();


component.moveTo(x, y);


}


// Do
-
nothing methods


public void mouseReleased(MouseEvent event) {}


public void mouseClicked(MouseEvent event) {}


public void mouseEntered(MouseEvent event) {}


public void mouseExited(MouseEvent event) {}

}


MouseListener
인터페이스에

정해져

있는

다섯개의

메소드를

모두

구현해야



37

자바프로그래밍

강원대학교

RectangleComponentViewer2.java

01:

import java.awt.event.MouseListener;

02:

import java.awt.event.MouseEvent;

03:

import javax.swing.JFrame;

04:

05: /**

06: This program displays a RectangleComponent.

07: */

08:

public class

RectangleComponentViewer

09:

{

10:

public static void

main(String[] args)

11:

{

12:

final
RectangleComponent component


=
new

RectangleComponent();

13:


14: // Add mouse press listener

15:


16:

class MousePressListener implements MouseListener

17:

{

38

자바프로그래밍

강원대학교

RectangleComponentViewer2.java

18:

public void

mousePressed(MouseEvent event)

19:

{

20:

int

x = event.getX();

21:

int
y = event.getY();

22:

component.moveTo(x, y);

23:

}

24:


25:

// Do
-
nothing methods

26:

public void

mouseReleased(MouseEvent event) {}

27:

public void

mouseClicked(MouseEvent event) {}

28:

public void

mouseEntered(MouseEvent event) {}

29:

public void

mouseExited(MouseEvent event) {}

30:

}

31:


32:

MouseListener listener =
new

MousePressListener();

33:

component.addMouseListener(listener);

34:

39

자바프로그래밍

강원대학교

RectangleComponentViewer2.java

35:

JFrame frame =
new

JFrame();

36:

frame.add(component);

37:


38:

frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);

39:

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

40:

frame.setVisible(true);

41:

}

42:


43:

private static final int
FRAME_WIDTH =
300
;

44:

private static final int

FRAME_HEIGHT =
400
;

45:

}

40

타이머

이벤트

처리


애니메이션

자바프로그래밍

강원대학교

41

Processing Timer Events



javax.swing.Timer


일정

시간

간격으로

액션이
벤트를

발생시킨다
.



객체

1õB?*°

8°9I

4??8

?8?Ý8p)•

?e4?FŒ??8Ä

F”

&€

1à7Ý


타이머에게

6•2Œ8¨-ØCì

*à3Ø"<*°

&%)‘F¨

"Ç8p+¨

6•2Œ8¨-Ø
Cì?4

-P2?%T

&€*ü$?

6•2Œ8¨-ØCì

*à3Ø"<8Œ

actionPerformed

+ˆ2À&??4

Gl>Ð%\

-

callback!


class MyListener implements ActionListener

{





void actionPerformed(ActionEvent event)




{







//
타이머에서

액션이벤트가

발생할

때마다


†††
⼯/
이곳에

적어주는

액션이

실행됨




}

}

자바프로그래밍

강원대학교

42

Processing Timer Events


액션

리스너를

타이머에

등록함


MyListener listener = new MyListener();

Timer t = new Timer(interval, listener);

t.start();

//
타이머

쓰레드

시작

Timer

MyListener

actionPerformed

등록

주기적으로

이벤트

발생

actionPerformed
메소드

실행

(callback)

자바프로그래밍

강원대학교

43

Example: Countdown


Example: a timer that counts down to zero

자바프로그래밍

강원대학교

44

File
TimeTester.java

01:

import java.awt.event.ActionEvent;

02:

import java.awt.event.ActionListener;

03:

import javax.swing.JOptionPane;

04:

import javax.swing.Timer;

05:

06: /**

07: This program tests the Timer class.

08: */

09:

public class

TimerTester

10:

{

11:

public static void

main(String[] args)

12:

{

13:

class

CountDown

implements

ActionListener

14:

{

15:

public

CountDown(
int

initialCount)

16:

{

17:

count = initialCount;

18:

}

자바프로그래밍

강원대학교

45

File
TimeTester.java

19:


20:

public void

actionPerformed(ActionEvent event)

21:

{

22:

if
(count >=
0
)

23:

System.out.println(count);

24:

if
(count ==
0
)

25:

System.out.println(
"Liftoff!"
);

26:

count
--
;

27:

}

28:

29:

private int

count;

30:

}

31:


32:

CountDown listener =
new
CountDown(10);

33:


34:

final int

DELAY =
1000
;
// Milliseconds between


// timer ticks

자바프로그래밍

강원대학교

46

File
TimeTester.java

35:

Timer t =
new

Timer(DELAY, listener);

36:

t.start();

37:


38:

JOptionPane.showMessageDialog(
null
,
"Quit?"
);

39:

System.exit(
0
);

40:

}

41:

}

자바프로그래밍

강원대학교

47

자바프로그래밍

강원대학교

애니메이션

프로그램

기본

형식

class Mover implements ActionListener

{


public void actionPerformed(ActionEvent event)


{


// Move the rectangle


}

}


ActionListener listener = new Mover();

final int DELAY = 100; // Milliseconds between timer ticks

Timer t = new Timer(DELAY, listener);

t.start();

48

자바프로그래밍

강원대학교

Accessing Surrounding Variables


Inner
A¨(Ì3Ø8Œ

+ˆ2À&?7?2P#È

-H?™

A¨(Ì3Ø8Œ

-ô3L7?

9E?0F”

3L

8¼8€

public static void main(String[] args)

{


. . .


final Rectangle
box

= new Rectangle(5, 10, 20, 30);



class Mover implements ActionListener


{


public void actionPerformed(ActionEvent event)


{


// Move the rectangle


box.translate(1, 1);



}


}


. . .

}

49

자바프로그래밍

강원대학교

File
TimeTester2.java

01:

import java.awt.Rectangle;

02:

import java.awt.event.ActionEvent;

03:

import java.awt.event.ActionListener;

04:

import javax.swing.JOptionPane;

05:

import javax.swing.Timer;

06:

07: /**

08: This program uses a timer to move a rectangle once per second.

09: */

10:

public class

TimerTester2

11:

{

12:

public static void

main(String[] args)

13:

{

14:

final

Rectangle box =
new

Rectangle(
5
,
10
,
20
,
30
);

15:


16:

class

Mover
implements

ActionListener

17:

{

50

자바프로그래밍

강원대학교

File
TimeTester2.java

18:

public void

actionPerformed(ActionEvent event)

19:

{

20:

box.translate(
1
,
1
);

21:

System.out.println(box);

22:

}

23:

}

24:


25:

ActionListener listener =
new
Mover();

26:


27:

final int

DELAY =
100
;
// Milliseconds between timer ticks

28:

Timer t =
new

Timer(DELAY, listener);

29:

t.start();

30:


31:

JOptionPane.showMessageDialog(
null
,
"Quit?"
);

32:

System.out.println(
"Last box position: "

+ box);

33:

System.exit(
0
);

34:

}

35:

}

51

자바프로그래밍

강원대학교

File
TimeTester2.java

java.awt.Rectangle[x=6,y=11,width=20,height=30]

java.awt.Rectangle[x=7,y=12,width=20,height=30]

java.awt.Rectangle[x=8,y=13,width=20,height=30] . . .

java.awt.Rectangle[x=28,y=33,width=20,height=30]

java.awt.Rectangle[x=29,y=34,width=20,height=30]

Last box position: java.awt.Rectangle[x=29,y=34,width=20,height=30]

Output:

52

Animation Program


자바프로그래밍

강원대학교

53

public class RectangleComponent extends JComponent{


private Rectangle box;


public RectangleComponent(){



box = new Rectangle(BOX_X, BOX_Y, BOX_WIDTH,







BOX_HEIGHT);


}


public void paintComponent(Graphics g){



Graphics2D g2 = (Graphics2D) g;



g2.draw(box);


}


public void moveBy(int dx, int dy){



box.translate(dx, dy);



repaint();


}

}

자바프로그래밍

강원대학교

54

public class RectangleMover{


public static void main(String[] args){



JFrame frame = new JFrame();



frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);



frame.setTitle("An animated rectangle");



frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



final RectangleComponent component = new RectangleComponent();



frame.add(component);



frame.setVisible(true);



class TimerListener implements ActionListener{




public void actionPerformed(ActionEvent event){





component.moveBy(1, 1);




}



}



ActionListener listener = new TimerListener();



final int DELAY = 100; // Milliseconds between timer ticks



Timer t = new Timer(DELAY, listener);



t.start();


}

}

자바프로그래밍

강원대학교

55