PowerPoint Presentation

Similar documents
Microsoft PowerPoint - java1-lecture11.ppt [호환 모드]

<4D F736F F F696E74202D20C1A63233C0E520B1D7B7A1C7C820C7C1B7CEB1D7B7A1B9D628B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

8장.그래픽 프로그래밍

PowerPoint Presentation

<4D F736F F F696E74202D20C1A63139C0E520B9E8C4A120B0FCB8AEC0DA28B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

자바GUI실전프로그래밍2_장대원.PDF

PowerPoint 프레젠테이션

gnu-lee-oop-kor-lec10-1-chap10

PowerPoint 프레젠테이션

public class FlowLayoutPractice extends JFrame { public FlowLayoutPractice() { super("flowlayout Practice"); this. Container contentpane = getcontentp

<4D F736F F F696E74202D20C1A63236C0E520BED6C7C3B8B428B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

Microsoft PowerPoint - ÀÚ¹Ù08Àå-1.ppt

9장.key

11장.key

제8장 자바 GUI 프로그래밍 II

Microsoft PowerPoint - 04-UDP Programming.ppt

<4D F736F F F696E74202D20C1A63138C0E520C0CCBAA5C6AE20C3B3B8AE28B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

PowerPoint Presentation

10 이벤트 처리와 그래픽 프로그래밍.key

05-class.key

<4D F736F F F696E74202D20C1A63230C0E520BDBAC0AE20C4C4C6F7B3CDC6AE203128B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

PowerPoint Presentation

슬라이드 1

Microsoft PowerPoint - java1-lab5-ImageProcessorTestOOP.pptx

No Slide Title

PowerPoint Presentation

Microsoft PowerPoint - Java7.pptx

Microsoft PowerPoint - 14주차 강의자료

gnu-lee-oop-kor-lec06-3-chap7

Microsoft PowerPoint - 12장

Chap12

Microsoft PowerPoint - Java-03.pptx

Cluster management software

그래픽 chapter.1 스윙컴포넌트그리기 컴포넌트는자신의모양을스스로그린다 X-window, MFC, C# GUI, Android 등 GUI 플랫폼에서 GUI 컴포넌트는스스로자신의모양을그린다. 자바의스윙도예외는아니다. JButton은버튼모양을그리는코드를내장하고, JCo

PowerPoint Presentation

10장.key

쉽게 풀어쓴 C 프로그래밍

10-Java Applet

Microsoft PowerPoint - EEL2 Lecture10 -Swing and Event Handling.pptx

Java Programing Environment

강의자료

PowerPoint Presentation

JMF3_심빈구.PDF

rmi_박준용_final.PDF

JAVA 프로그래밍실습 실습 1) 실습목표 - 메소드개념이해하기 - 매개변수이해하기 - 새메소드만들기 - Math 클래스의기존메소드이용하기 ( ) 문제 - 직사각형모양의땅이있다. 이땅의둘레, 면적과대각

5장.key

PowerPoint Presentation

쉽게 풀어쓴 C 프로그래밍

쉽게 풀어쓴 C 프로그래밍

쉽게 풀어쓴 C 프로그래밍

<4D F736F F F696E74202D20C1A63038C0E520C5ACB7A1BDBABFCD20B0B4C3BC4928B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

중간고사

PowerPoint Presentation

슬라이드 1

JAVA PROGRAMMING 실습 08.다형성

JTable과 MVC(Model-View-Controller) 구조 - 모델-뷰-컨트롤러구조는데이터의저장과접근에대한제공은모델이담당하고, 화면표시는뷰, 이벤트의처리는컨트롤러가하도록각역할을구분한구조이다. 즉, 역할의분담을통하여상호간의영향을최소화하고각요소의독립성을보장하여독자

JavaPrintingModel2_JunoYoon.PDF

No Slide Title

PowerPoint Presentation

(Microsoft PowerPoint - java1-lecture7.ppt [\310\243\310\257 \270\360\265\345])

q 이장에서다룰내용 1 객체지향프로그래밍의이해 2 객체지향언어 : 자바 2

(Microsoft PowerPoint - ETRSRHFRXNAM.ppt [\310\243\310\257 \270\360\265\345])

Microsoft PowerPoint - lec04_05.ppt [호환 모드]

PowerPoint Presentation

Microsoft Word - java18-1-final-answer.doc

PowerPoint 프레젠테이션

11-GUI.key

Design Issues

PowerPoint 프레젠테이션

12-file.key

PowerPoint Presentation

PowerPoint Presentation

Microsoft PowerPoint - 2강

PowerPoint Presentation

JAVA PROGRAMMING 실습 09. 예외처리

Network Programming

ch09

모든스윙컴포넌트에는텍스트옆에이미지를추가로표시할수있다. ImageIcon image = new ImageIcon("image.gif"); JLabel label = new JLabel(" 이미지레이블 "); label.seticon(image);

쉽게 풀어쓴 C 프로그래밍

Microsoft PowerPoint - java1-lecture10.ppt [호환 모드]

(Microsoft PowerPoint - LZVNQBAJWGTC.ppt [\310\243\310\257 \270\360\265\345])

(8) getpi() 함수는정적함수이므로 main() 에서호출할수있다. (9) class Circle private double radius; static final double PI= ; // PI 이름으로 로초기화된정적상수 public

Microsoft PowerPoint - java1-lecture11.ppt [호환 모드]

신림프로그래머_클린코드.key

슬라이드 1

Microsoft PowerPoint - Supplement-03-TCP Programming.ppt [호환 모드]

Spring Boot/JDBC JdbcTemplate/CRUD 예제

2) 활동하기 활동개요 활동과정 [ 예제 10-1]main.xml 1 <LinearLayout xmlns:android=" 2 xmlns:tools="

02 C h a p t e r Java

Microsoft PowerPoint - 06-Chapter09-Event.ppt

Microsoft PowerPoint - [JAVA프로그래밍]9장GUI

쉽게 풀어쓴 C 프로그래밊

Microsoft PowerPoint - 03-TCP Programming.ppt

PowerPoint 프레젠테이션

PowerPoint Presentation

Cluster management software

슬라이드 1

PowerPoint 프레젠테이션

PowerPoint Presentation

자바 프로그래밍

Transcription:

객체지향프로그래밍 그래픽프로그래밍 손시운 ssw5176@kangwon.ac.kr

자바에서의그래픽 2

자바그래픽의두가지방법 3

간단한예제 4

(1) 프레임생성하기 public class BasicPaint { public static void main(string[] args) { JFrame f = new JFrame(" 그래픽기초프로그램 "); f.setdefaultcloseoperation(jframe.exit_on_close); f.setsize(300, 200); f.setvisible(true); 5

(2) 어디에그릴것인가? JFrame 을생성하고여기에 JPanel 을추가한후에 JPanel 위에그림을그려보 자. public class BasicPaint { public static void main(string[] args) { JFrame f = new JFrame(" 그래픽기초프로그램 "); f.setdefaultcloseoperation(jframe.exit_on_close); f.add(new MyPanel()); f.setsize(300, 200); f.setvisible(true); class MyPanel extends JPanel { public MyPanel() {... 6

(3) 어떻게그릴것인가? 컴포넌트에무언가를그리려면 paintcomponent() 메소드를중복정의한다. paintcomponent() 메소드는컴포넌트가화면에그려질때호출된다. 7

일반적인코드의형태 class MyPanel extends JPanel { public void paintcomponent(graphics g) { super.paintcomponent(g); // 여기에그림을그리는코드를넣는다. 8

(4) 그래픽좌표계 y 좌표는아래로갈수록증가한다. 9

(5) 그림을그리는메소드 사각형을그리려면 Graphics 객체가가지고있는 drawrect() 을호출하면된 다. g.drawrect(50, 50, 50, 50); g.drawoval(200, 50, 50, 50); 10

전체소스 public class BasicPaint { public static void main(string[] args) { JFrame f = new JFrame(" 그래픽기초프로그램 "); f.setdefaultcloseoperation(jframe.exit_on_close); f.add(new MyPanel()); f.setsize(300, 200); f.setvisible(true); class MyPanel extends JPanel { public MyPanel() { setborder(borderfactory.createlineborder(color.black)); protected void paintcomponent(graphics g) { super.paintcomponent(g); g.drawrect(50, 50, 50, 50); g.drawoval(200, 50, 50, 50); 11

실행결과 12

기초도형그리기 13

직선그리기 14

사각형그리기 width height 15

drawroundrect() 16

타원그리기 17

호그리기 18

예제 아래그림과비슷한얼굴을그려보자 19

예제 import javax.swing.*; import java.awt.event.*; import java.awt.*; class MyPanel extends JPanel { public void paintcomponent(graphics g) { super.paintcomponent(g); g.setcolor(color.yellow); g.filloval(20, 30, 200, 200); g.setcolor(color.black); // 왼쪽눈을그린다. g.drawarc(60, 80, 50, 50, 180, -180); // 오른쪽눈을그린다. g.drawarc(150, 80, 50, 50, 180, -180); // 입을그린다. g.drawarc(70, 130, 100, 70, 180, 180); 20

예제 public class SnowManFace extends JFrame { public SnowManFace() { setsize(280, 300); setdefaultcloseoperation(jframe.exit_on_close); settitle(" 눈사람얼굴 "); setvisible(true); add(new MyPanel()); public static void main(string[] args) { SnowManFace s=new SnowManFace(); 21

Lab: 프랙탈로나무그리기 프랙탈 (fractal) 은자기유사성을가지는기하학적구조를프랙털구조를말 한다. 22

프랙탈트리를그리는알고리즘 나무줄기를그린다. 줄기의끝에서특정한각도로 2개의가지를그린다. 동일한과정을가지의끝에서반복한다. 충분한가지가생성될때까지이과정을반복한다. 23

SOLUTION import java.awt.color; import java.awt.graphics; import javax.swing.jframe; public class DrawTreeFrame extends JFrame { public DrawTreeFrame() { setsize(800, 700); setdefaultcloseoperation(exit_on_close); setvisible(true); private void drawtree(graphics g, int x1, int y1, double angle, int depth) { if (depth == 0) return; int x2 = x1 + (int) (Math.cos(Math.toRadians(angle)) * depth * 10.0); int y2 = y1 + (int) (Math.sin(Math.toRadians(angle)) * depth * 10.0); g.drawline(x1, y1, x2, y2); drawtree(g, x2, y2, angle - 20, depth - 1); drawtree(g, x2, y2, angle + 20, depth - 1); 24

SOLUTION @Override public void paint(graphics g) { g.setcolor(color.black); drawtree(g, 400, 600, -90, 10); public static void main(string[] args) { new DrawTreeFrame(); 25

색상 java.awt 패키지의일부인 Color 클래스를사용 빛의 3 원색인 Red 성분, Green 성분, Blue 성분이얼마나함유되어있는지를 0 에서 255 까지의수를사용하여나타낸다. 26

색상 클래스변수이름 색상 RGB 값 Color.black black (0,0,0) Color.blue blue (0,0,255) Color.cyan cyan (0,255,255) Color.gray gray (128,128,128) Color.darkGray dark gray (64,64,64) Color.lightGray light gray (192,192,192) Color.green green (0,255,0) Color.magenta magenta (255,0,255) Color.orange orange (255,200,0) Color.pink pink (255,175,175) Color.red red (255,0,0) Color.white white (255,255,255) Color.yellow yellow (255,255,0) 27

색상설정 마젠타색상을얻는방법 Color c = Color.magenta; Color c = new Color (255,0,255); Color 는알파값 (alpha) 을가질수있다. 알파값이란색상의투명도를나타낸 다. ( 예 ) Color c = new Color (255, 0, 0, 128); 28

컴포넌트색상설정메소드 생성자 설명 setbackground(color c) 컴포넌트객체에서배경색을설정한다. setcolor(color c) 전경색을설정한다. Color getcolor() 현재의전경색을반환한다. 29

예제 import javax.swing.*; import java.awt.event.*; import java.awt.*; class MyPanel extends JPanel implements ActionListener { JButton button; Color color = new Color(0, 0, 0); public MyPanel() { setlayout(new BorderLayout()); button = new JButton(" 색상변경 "); button.addactionlistener(this); add(button, BorderLayout.SOUTH); public void paintcomponent(graphics g) { super.paintcomponent(g); g.setcolor(color); g.fillrect(10, 10, 210, 220); 30

예제 public void actionperformed(actionevent e) { color = new Color((int) (Math.random()*255.0), (int) (Math.random()*255.0),(int) (Math.random()*255.0)); repaintcomponent(); public class ColorTest extends JFrame { public ColorTest() { setsize(240, 300); setdefaultcloseoperation(jframe.exit_on_close); settitle("color Test"); setvisible(true); JPanel panel = new MyPanel(); add(panel); public static void main(string[] args) { ColorTest s = new ColorTest(); 31

실행결과 32

색상선택기 33

예제 import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.colorchooser.*; public class ColorChooserTest extends JFrame implements ChangeListener { protected JColorChooser color; public ColorChooserTest() { settitle(" 색상선택기테스트 "); setdefaultcloseoperation(jframe.exit_on_close); color = new JColorChooser(); // 생성자호출 color.getselectionmodel().addchangelistener(this); // 리스너등록 color.setborder(borderfactory.createtitledborder(" 색상선택 ")); 34

예제 JPanel panel = new JPanel(); panel.add(color); add(panel); pack(); this.setvisible(true); public void statechanged(changeevent e) { Color newcolor = color.getcolor(); System.out.println(newColor); public static void main(string[] args) { new ColorChooserTest(); 35

문자열출력과폰트 문자열출력방법 // (x, y) 위치에문자열을출력하려면 g.drawstring("hello World!", x, y); 폰트를지정하기위해서는 Font 클래스를사용 Font 객체는폰트이름 (Courier, Helvetica,..) 과스타일 (plain, bold, italic,...), 크기 (12포인트,...) 의 3가지속성 // plain 형식이고크기는 10포인트 Font font = new Font("Courier", Font.PLAIN, 10); 36

폰트의종류 논리적인폰트 설명 "Serif" 삐침 (serif) 를갖는가변폭글꼴, 대표적으로 TimesRoman 이있다. "SansSerif" 삐침 (serif) 를갖지않는가변폭글꼴, 대표적으로 Helvetica 가있다. "Monospaced" 고정폭을가지는글꼴, 대표적으로 Courier 가있다. "Dialog" "DialogInput" 대화상자에서텍스트출력을위하여사용되는글꼴 대화상자에서텍스트입력을위하여사용되는글꼴 37

폰트의지정 public void paint(graphics g) { Font f = new Font("Serif", Font.BOLD Font.ITALIC, 12); g.setfont(f);... JLabel mylabel = new JLabel(" 폰트색상 ); Font f = new Font("Dialog", Font.ITALIC, 10); // 1 mylabel.setfont(f); 38

예제 class MyPanel extends JPanel { Font f1, f2, f3, f4, f5; public MyPanel() { f1 = new Font("Serif", Font.PLAIN, 20); f2 = new Font("San Serif", Font.BOLD, 20); f3 = new Font("Monospaced", Font.ITALIC, 20); f4 = new Font("Dialog", Font.BOLD Font.ITALIC, 20); f5 = new Font("DialogInput", Font.BOLD, 20); public void paintcomponent(graphics g) { super.paintcomponent(g); g.setfont(f1); g.drawstring("serif 20 points PLAIN", 10, 50); g.setfont(f2); g.drawstring("sanserif 20 points BOLD", 10, 70); g.setfont(f3); g.drawstring("monospaced 20 points ITALIC", 10, 90); g.setfont(f4); g.drawstring("dialog 20 points BOLD + ITALIC", 10, 110); g.setfont(f5); g.drawstring("dialoginput 20 points BOLD", 10, 130); 39

예제 public class FontTest extends JFrame { public FontTest() { setsize(500, 200); setdefaultcloseoperation(jframe.exit_on_close); settitle("font Test"); setvisible(true); JPanel panel = new MyPanel(); add(panel); public static void main(string[] args) { FontTest s = new FontTest(); 40

이미지출력 자바는 GIF, PNG JPEG 타입의이미지를화면에그릴수있다. BufferedImage img = null; try { img = ImageIO.read(new File("strawberry.jpg")); catch (IOException e) { 41

예제 // 소스를입력하고 Ctrl+Shift+O 를눌러서필요한파일을포함한다. public class LoadImageApp extends JPanel { BufferedImage img; public void paint(graphics g) { g.drawimage(img, 0, 0, null); public LoadImageApp() { try { img = ImageIO.read(new File("dog.png")); catch (IOException e) { 42

예제 public Dimension getpreferredsize() { if (img == null) { return new Dimension(100, 100); else { return new Dimension(img.getWidth(null), img.getheight(null)); public static void main(string[] args) { JFrame f = new JFrame(" 이미지표시예제 "); f.add(new LoadImageApp()); f.pack(); f.setvisible(true); f.setdefaultcloseoperation(jframe.exit_on_close); 43

실행결과 44

LAB: 이미지나누어서그리기 drawimage() 를이용하여이미지를그릴때, 일부만그릴수있고또크기를 변경할수있다. 이러한기능을이용하여서이미지를 16 조각으로나누어서 그리는프로그램을작성하여보자. 45

drawimage() 메소드 46

예제 public class MyImageFrame extends JFrame implements ActionListener { private int pieces = 4; private int totalpieces = pieces * pieces; private int[] piecenumber; private BufferedImage img; public MyImageFrame() { settitle("image Draw Test"); try { img = ImageIO.read(new File("hubble.jpg")); catch (IOException e) { System.out.println(e.getMessage()); System.exit(0); piecenumber = new int[totalpieces]; for (int i = 0; i < totalpieces; i++) { piecenumber[i] = i; add(new MyPanel(), BorderLayout.CENTER); JButton button = new JButton("DIVIDE"); button.addactionlistener(this); add(button, BorderLayout.SOUTH); setsize(img.getwidth(null), img.getheight(null)); setvisible(true); 47

예제 void divide() { Random rand = new Random(); int ri; for (int i = 0; i < totalpieces; i++) { ri = rand.nextint(totalpieces); int tmp = piecenumber[i]; piecenumber[i] = piecenumber[ri]; piecenumber[ri] = tmp; class MyPanel extends JPanel { public void paintcomponent(graphics g) { super.paintcomponent(g); int piecewidth = img.getwidth(null) / pieces; int pieceheight = img.getheight(null) / pieces; 48

예제 for (int x = 0; x < pieces; x++) { int sx = x * piecewidth; for (int y = 0; y < pieces; y++) { int sy = y * pieceheight; int number = piecenumber[x * pieces + y]; int dx = (number / pieces) * piecewidth; int dy = (number % pieces) * pieceheight; g.drawimage(img, dx, dy, dx + piecewidth, dy + pieceheight, sx, sy, sx + piecewidth, sy + pieceheight, null); public static void main(string[] args) { new MyImageFrame(); public void actionperformed(actionevent e) { divide(); repaint(); 49

영상처리 영상처리 (image processing) 은이미지를읽어서여러가지처리를하는학 문분야이다. 예를들어서화질이나쁜이미지의화질을향상시키는것도영 상처리의일종이다. 50

예제 public class GrayScaleImage extends JFrame { BufferedImage image; int width; int height; public GrayScaleImage() { try { File input = new File("Lenna.png"); image = ImageIO.read(input); width = image.getwidth(); height = image.getheight(); for (int r = 0; r < height; r++) { for (int c = 0; c < width; c++) { Color color = new Color(image.getRGB(r, c)); 51

예제 int red = (int) (color.getred()); int green = (int) (color.getgreen()); int blue = (int) (color.getblue()); int avg = (red + green + blue) / 3; Color newcolor = new Color(avg, avg, avg); image.setrgb(r, c, newcolor.getrgb()); File ouptut = new File("output.png"); ImageIO.write(image, "png", ouptut); add(new MyPanel()); pack(); setvisible(true); catch (Exception e) { System.out.println(" 이미지읽기실패!"); 52

예제 class MyPanel extends JPanel { public void paintcomponent(graphics g) { g.drawimage(image, 0, 0, null); public Dimension getpreferredsize() { if (image == null) { return new Dimension(100, 100); else { return new Dimension(image.getWidth(null), image.getheight(null)); static public void main(string args[]) throws Exception { GrayScaleImage obj = new GrayScaleImage(); 53

Java 2D 광범위한그래픽객체를그릴수있다. 도형의내부를그라디언트 (gradient) 나무늬로채울수있다. 이미지를그릴수있고필터링연산을적용할수있다. 그래픽객체들의충돌을감지할수있는메커니즘을제공한다. 54

Java 2D 를이용한그리기 public void paintcomponent(graphics g) { Graphics2D g2 = (Graphics2D) g; g2.drawline(100, 100, 300, 300); g2.drawrect(10, 10, 100, 100);... 55

Java 2D 를이용한그리기 56

사각형그리기 Shape r1 = new Rectangle2D.Float(10, 10, 50, 60); g2.draw(r1); 57

타원그리기 // 타원객체를생성하고타원을그린다. g2.draw(new Ellipse2D.Double(x, y, rectwidth, rectheight)); 58

원호생성 Shape arc1 = new Arc2D.Float(10, 10, 90, 90, 90, 60, Arc2D.OPEN); 59

예제 import java.util.*; import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.awt.geom.*; public class MoreShapes extends JFrame { public MoreShapes() { setsize(600, 130); settitle("java 2D Shapes"); setdefaultcloseoperation(jframe.exit_on_close); JPanel panel = new MyPanel(); add(panel); setvisible(true); public static void main(string[] args) { new MoreShapes(); 60

예제 class MyPanel extends JPanel { ArrayList<Shape> shapearray = new ArrayList<Shape>(); public MyPanel() { Shape s; // 사각형 s = new Rectangle2D.Float(10, 10, 70, 80); shapearray.add(s); // 둥근사각형 s = new RoundRectangle2D.Float(110, 10, 70, 80, 20, 20); shapearray.add(s); // 타원 s = new Ellipse2D.Float(210, 10, 80, 80); shapearray.add(s); // 원호 : Arc2D.OPEN s = new Arc2D.Float(310, 10, 80, 80, 90, 90, Arc2D.OPEN); shapearray.add(s); 61

예제 // 원호 Arc2D.CHORD s = new Arc2D.Float(410, 10, 80, 80, 0, 180, Arc2D.CHORD); shapearray.add(s); // 원호 Arc2D.PIE s = new Arc2D.Float(510, 10, 80, 80, 45, 90, Arc2D.PIE); shapearray.add(s); public void paintcomponent(graphics g) { super.paintcomponent(g); Graphics2D g2 = (Graphics2D) g; // 앤티에일리어싱을설정한다. g2.setrenderinghint(renderinghints.key_antialiasing, RenderingHints.VALUE_ANTIALIAS_ON); g2.setcolor(color.black); g2.setstroke(new BasicStroke(3)); for (Shape s : shapearray) g2.draw(s); 62

예제 63

도형채우기 단일색으로채우기 g2.setcolor(color.blue); g2.fill(ellipse); 투명하게채우기 g2.setcomposite(alphacomposite.getinstance(alphacomposite.src_over, 0.50F)); 그라디언트로채우기 GradientPaint gp = new GradientPaint(0, 0, Color.WHITE, 0, 100, Color.RED); 64

예제 class MyComponent extends JComponent { public void paint(graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setrenderinghint(renderinghints.key_antialiasing, RenderingHints.VALUE_ANTIALIAS_ON); g2.setstroke(new BasicStroke(3)); GradientPaint gp = new GradientPaint(0, 10, Color.WHITE, 0, 70, Color.RED); // 사각형 g2.setpaint(color.red); g2.fill(new Rectangle2D.Float(10, 10, 70, 80)); // 둥근사각형 g2.setpaint(gp); g2.fill(new RoundRectangle2D.Float(110, 10, 70, 80, 20, 20));... 65

실행결과 66

LAB: 간단한애니메이션 67

예제 class MyPanel extends JPanel implements ActionListener { private final int WIDTH = 500; private final int HEIGHT = 300; private final int START_X = 0; private final int START_Y = 250; private BufferedImage image; private Timer timer; private int x, y; public MyPanel() { setbackground(color.black); setpreferredsize(new Dimension(WIDTH, HEIGHT)); setdoublebuffered(true); File input = new File("ship.jpg"); try { image = ImageIO.read(input); catch (IOException e) { e.printstacktrace(); 68

예제 x = START_X; y = START_Y; timer = new Timer(20, this); timer.start(); @Override public void paintcomponent(graphics g) { super.paintcomponent(g); g.drawimage(image, x, y, this); @Override public void actionperformed(actionevent e) { x += 1; y -= 1; if (x > WIDTH) { x = START_X; y = START_Y; repaint(); 69

예제 public class MyFrame extends JFrame { public MyFrame() { add(new MyPanel()); settitle(" 애니메이션테스트 "); setdefaultcloseoperation(jframe.exit_on_close); setsize(500, 300); setvisible(true); public static void main(string[] args) { new MyFrame(); 70