1 / 110

자바 애플릿 프로그래밍

자바 애플릿 프로그래밍. 9.1 간단한 자바 애플릿. 9.1.1 애플릿 소개 자바 애플릿 웹브라우저 내에서 수행되는 자바 프로그램 자바 애플릿은 main() 함수가 없다 . cf) 일반 자바 어플리케이션은 main() 함수부터 수행 애플릿 프로그램을 작성하려면 Applet 클래스로부터 상속받아야 애플릿뷰어 (appletviewer) 애플릿 프로그램을 작성하고 테스트하기 위해서 웹브라우저 대신 애플릿 클래스는 항상 public 으로 선언되어야

garret
Download Presentation

자바 애플릿 프로그래밍

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 자바 애플릿 프로그래밍

  2. 9.1 간단한 자바 애플릿 9.1.1 애플릿 소개 • 자바 애플릿 • 웹브라우저 내에서 수행되는 자바 프로그램 • 자바 애플릿은 main() 함수가 없다. cf) 일반 자바 어플리케이션은 main() 함수부터 수행 • 애플릿 프로그램을 작성하려면 Applet 클래스로부터 상속받아야 • 애플릿뷰어(appletviewer) • 애플릿 프로그램을 작성하고 테스트하기 위해서 웹브라우저 대신 • 애플릿 클래스는 항상 public으로 선언되어야 • 자바 애플릿에는 다음과 같은 기본적인 함수가 정의 • init(), start(), paint(), stop(), destroy(), update() • 애플릿 프로그램을 개발하는 프로그래머는 이 함수들을 오버라이드해서 사용

  3. Applet 클래스의 계층구조 Java.lang.Object Java.awt.Component Java.lang.Container Java.lang.Panel Java.lang.Applet

  4. 9.1 간단한 자바 애플릿 예제 : Hello.html 1 <applet code=Hello.class width=200 height=150> 2 </applet> 예제 : Hello.java 1 import java.awt.*; 2 import java.applet.*; 3 4 public class Hello extends Applet { 5 private String Greeting[] = { 6 "Hello, world" 7 }; 8 9 public void paint(Graphics g){ 10 g.drawString(Greeting[0], 25, 25); 11 } 12 } % appletviewer Hello.html

  5. 9.1 간단한 자바 애플릿 1. 자바 애플릿을 웹 홈페이지에 등록하기 • HTML에서 applet 태그는 웹에서 자바 애플릿을 등록하기 위한 태그 • 실제 애플릿은 .class 파일에 저장되어 있고, applet 태그는 애플릿 클래스 파일을 기술 <applet code = classfile.class codebase = directory archive = archivefile.zip align = aligndirection name = appletname vspace = 20 hspace = 30 width = 200 height = 100 > </applet> • applet 태그에서 code, width, height 어트리뷰트는 항상 존재해야

  6. 9.1 간단한 자바 애플릿 • 어트리뷰트 code -컴파일된 자바 애플릿 클래스가 저장된 파일 이름 codebase -자바 클래스 파일들이 존재하는 디렉토리 이름codebase는 HTML 파일과 자바 클래스 파일이 다른 디렉토리에 저장되어 있는 경우에 사용 archive -자바의 .class 파일들을 zip이나 jar 파일로 묶어서 파일의 다운로드 시간을 줄이기 위해 사용Netscape 3.0이상에서 지원 align -애플릿 정렬 위치를 결정하는데 사용 name -애플릿의 이름을 지정 애플릿 이름은 한 웹 페이지에서 여러 개의 애플릿이 있을 때 애플릿 간에 통신하는데 사용 vspace -애플릿 주위에서 세로 방향 마진 (단위: 픽셀) hspace -애플릿 주위에서 가로 방향 마진 (단위: 픽셀) width -애플릿의 가로 방향 크기 (단위: 픽셀) height -애플릿의 세로 방향 크기 (단위: 픽셀)

  7. 9.1 그래픽 좌표계 (0, 0) X 축 X값 증가 Y 값 증 가 Y 축

  8. 9.1 색에 대한 정의 • 빛의 3원색 • Red, Green, Blue • 컴퓨터에서 사용 • 모니터 속의 전자총과 동일 • 참고: RGB 모니터 • 색의 3원색 • Red, Blue, Yellow

  9. 9.1 간단한 자바 애플릿 2. Graphics 클래스의 drawXXX() 메소드들 • drawString(String msg, int x, int y) • drawLine(int x1, int y1, int x2, int y2)

  10. Line.java import java.awt.*; import java.applet.*; public class Line extends java.applet.Applet { public void paint (Graphics g){ g.drawLine(50, 50, 100, 150); } }

  11. 9.1 간단한 자바 애플릿 • drawRect(int x, int y, int w, int h) • drawOval(int x, int y, int w, int h)

  12. Rect.java import java.awt.*; import java.applet.*; public class Rect extends java.applet.Applet { public void paint (Graphics g){ g.drawRect(50, 50, 100, 150); } }

  13. DrawOval.java import java.awt.*; import java.applet.*; public class DrawOval extends java.applet.Applet { public void paint (Graphics g){ g.drawOval(50, 50, 100, 150); } }

  14. 9.1 간단한 자바 애플릿 • drawRoundRect(int x, int y, int w, int h, int rw, int rh) • drawArc(int x, int y, int w, int h, int a, int b)

  15. RoundRect.java import java.awt.*; import java.applet.*; public class RoundRect extends java.applet.Applet { public void paint (Graphics g){ g.drawRoundRect(50, 50, 100, 150, 20, 20); } }

  16. DrawArc.java import java.awt.*; import java.applet.*; public class DrawArc extends java.applet.Applet { public void paint (Graphics g){ g.drawArc(50, 50, 100, 150, 95, 115); } }

  17. 실행 결과

  18. 9.1 간단한 자바 애플릿 • draw3DRect(int x, int y, int width, int height, boolean raised) • fill3DRect(int x, int y, int widthm int height, boolean raised)

  19. Draw3DRect.java import java.awt.*; import java.applet.*; public class Draw3DRect extends java.applet.Applet { public void paint (Graphics g){ g.draw3DRect(50, 50, 100, 150, true); } }

  20. Fill3DRect.java import java.awt.*; import java.applet.*; public class Fill3DRect extends java.applet.Applet { public void paint (Graphics g){ g.setColor(Color.red); g.fill3DRect(50, 50, 100, 150, true); } }

  21. 실행 결과

  22. 다각형 그리기 • drawPolygon(int xPoints[], int yPoints[], int nPoints) • drawPloygon(Polygon p) • Polygon(int xPoints[], int yPoints[], int nPoints) • fillPolygon(int xPoints[], int yPoints[], int nPoints) • drawPolyline(int xPoints[], int yPoints[], int nPoints) • drawPloyline(Polygon p)

  23. DrawPolygon.java import java.awt.*; import java.applet.*; public class DrawPolygon extends java.applet.Applet { int xPts[ ] = {25, 55, 150, 110, 65}; int yPts[ ] = {30, 125, 50, 155, 100}; public void paint (Graphics g){ g.drawPolygon(xPts, yPts, xPts.length); } }

  24. DrawPolyLine.java import java.awt.*; import java.applet.*; public class DrawPolyLine extends java.applet.Applet { int xPts[ ] = {25, 55, 150, 110, 65}; int yPts[ ] = {30, 125, 50, 155, 100}; public void paint (Graphics g){ g.drawPolyline(xPts, yPts, xPts.length); } }

  25. 실행 결과

  26. 9.1 간단한 자바 애플릿 • fillXXX() 메소드 • 도형을 그리고 내부를 색으로 채우는 메소드 • fillRect(int x, int y, int w, int h) • fillOval(int x, int y, int w, int h) • fillRoundRect(int x, int y, int w, int h, int rw, int rh) • fillArc(int x, int y, int w, int h, int a, int b) • fillPolygon(int xPoints[], int yPoints[], int nPoints)

  27. FillRect.java import java.awt.*; import java.applet.*; public class FillRect extends java.applet.Applet { public void paint (Graphics g){ g.setColor(Color.blue); g.fillRect(50, 50, 100, 150); } }

  28. FillOval.java import java.awt.*; import java.applet.*; public class FillOval extends java.applet.Applet { public void paint (Graphics g){ g.fillOval(50, 50, 100, 150); } }

  29. FillRoundRect.java import java.awt.*; import java.applet.*; public class FillRoundRect extends java.applet.Applet { public void paint (Graphics g){ g.setColor(Color.red); g.fillRoundRect(50, 50, 100, 150, 20, 20); } }

  30. FillArc.java import java.awt.*; import java.applet.*; public class FillArc extends java.applet.Applet { public void paint (Graphics g){ g.fillArc(50, 50, 100, 150, 95, 115); } }

  31. FillPolygon.java import java.awt.*; import java.applet.*; public class FillPolygon extends java.applet.Applet { int xPts[] = {25, 55, 150, 110, 65}; int yPts[] = {30, 125, 50, 155, 100}; public void paint (Graphics g){ g.fillPolygon(xPts, yPts, xPts.length); } }

  32. FillPolygon2.java import java.awt.*; import java.applet.*; public class FillPolygon2 extends java.applet.Applet { int xPts[ ] = {25, 55, 150, 110, 65}; int yPts[ ] = {30, 125, 50, 155, 100}; Polygon poly = new Polygon(xPts, yPts, xPts.length); public void paint (Graphics g){ g.fillPolygon(poly); } }

  33. 실행 결과

  34. 9.1 간단한 자바 애플릿 3. HTML에서 애플릿으로 파라메터 전달하기 • 애플릿 getParameter() 메소드 • 애플릿이 HTML 파일로부터 값을 전달받기 위해서 이용 • HTML에서는 파라메타를 전달하기 위해 <param name="변수이름" value="변수값"> 문장을 기술 • 자바 프로그램 내에서 사용되는 변수의 이름과 HTML의 <param name=.. >에서 name 부분에 사용된 변수 이름이 동일한 것에 주의 !!

  35. 9.1 간단한 자바 애플릿 예제 : HelloParam.java 1 import java.awt.*; 2 import java.applet.*; 3 4 public class HelloParam extends Applet { 5 String message = ""; 6 7 public void init() { 8 message = getParameter("msg"); 9 if(message == null) 10 message = "Hello"; 11 } 12 13 public void paint(Graphics g) { 14 g.drawString(message, 25, 25); 15 } 16 } HelloParam.html 파일 1 <applet code=HelloParam.class width=300 height=200> 2 <param name="msg" value="안녕하세요"> 3 </applet>

  36. 9.1 애플릿의 실행 흐름 관련 메소드 init() 브라우저에 처음 불려졌을 때, 클래스 변수의 초기화 start() 프로그램의 주요한 흐름 시작 stop() 브라우저의 이동시 destory() 애플릿의 실행이 완전히 종료 paint(Graphics g) 애플릿 상의 노출이 있을 때마다 update(Graphics g) repaint() 메소드 호출시

  37. 9.1 간단한 자바 애플릿 9.1.2 애플릿 메소드 • init() 함수 • 웹브라우저가 자바 애플릿이 있는 홈페이지를 처음 방문했을 때 • 애플릿에서 사용되는 자료들을 초기화하는데 이용 • start() 함수 • 애플릿이 다시 살아날 때 자동적으로 호출된다. (ex) 웹브라우저가 아이콘에서 원래 상태로 돌아올 때, 혹은 다른 페이지로 이동했다가 다시 돌아올 때 자동적으로 호출 • 오디오 파일을 플레이(play)하거나, 애니메이션을 시작시킬 수 있다. • stop() 함수 • start() 함수와 짝을 이루어 사용 • 웹브라우저가 아이콘으로 되거나, 다른 홈페이지로 이동할 때 자동적으로 시작된다. • 오디오 파일 연주나 애니메이션을 중지시킬 수 있다. • stop() 함수가 호출된다고 해서 애플릿이 죽는 것은 아니다.

  38. 9.1 간단한 자바 애플릿 • paint(Graphics g) 함수 • 애플릿의 그림이 다시 그려져야 된다고 판단될 때 자동적으로 호출 (ex) 애플릿이 처음 시작될 때, 애플릿의 크기가 변경될 때, 애플릿이 다른 윈도우에 가려졌다가 다시 보여질 때 • 그래픽스 클래스는 그림을 그리는데 필요한 정보를 가지고 있다 • update(Graphics g) 함수 • 애플릿에 그려진 그림을 모두 지우고 paint() 함수를 호출 • 그림이 많은 경우에 그림을 지우고 새로 그리면 깜빡임(flickering)이 발생해서 보기 안 좋은 경우에, update() 함수를 오버라이딩해서 깜빡임을 줄일 수 있다. • repaint() 함수 • 프로그래머가 그림을 다시 그려야 할 필요성이 있는 경우에 호출 • repaint() 함수를 호출하면 repaint()는 다시 update()를 호출한다.

  39. 9.1 간단한 자바 애플릿 • destory() 함수 • 웹브라우저가 종료될 때 애플릿의 호출 따라서 웹브라우저가 종료되기 전까지는 애플릿이 종료되지 않음 • 프로그래머는 destory() 함수를 오버라이드 해서 애플릿이 적절하게 종료될 수 있도록 종료에 필요한 코드를 넣을 수 있다. • 애플릿의 함수들이 호출되는 순서

  40. 9.2 이미지 그리기 • 이미지 그리기 • Graphics 메소드 • drawImage(Image im, int x, int y, ImageObserver ob) • Applet 메소드 • getImage(URL url, String file_name) • ImageObserver는 이미지를 관리하기 위한 인터페이스 • AWT의 컴포넌트는 ImageObserver 인터페이스를 임플리멘츠하였기 때문에 ImageObserver로 AWT 컴포넌트를 사용 • 일반적으로 getImage()는 애플릿의 init() 함수에서 수행되고, 이미지를 화면에 출력하는 것은 paint() 함수에서 수행

  41. 이미지 그리기 • Image getImage(URL url) • Image getImage(URL url, String name) • boolean drawImage(Image img, int x, int y, ImageObserver observer) • boolean drawImage(Image img, int x, int y, int width, int height, ImageObserver observer) • boolean drawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer) • boolean drawImage(Image img, int x, int y, int width, int height, Color bgcolor ImageObserver observer)

  42. JDK1.2 demo <applet code=GraphApplet.class width=300 height=120>

  43. import java.awt.Graphics; public class GraphApplet extends java.applet.Applet { double f(double x) { return (Math.cos(x/5) + Math.sin(x/7) + 2) * getSize().height / 4; } public void paint(Graphics g) { for (int x = 0 ; x < getSize().width ; x++) { g.drawLine(x, (int)f(x), x + 1, (int)f(x + 1)); } } public String getAppletInfo() { return "Draws a sin graph."; } }

  44. 9.2 이미지 그리기 예제 : ImgTest.java 1 import java.awt.*; 2 3 import java.applet.Applet; 4 5 public class ImgTest extends Applet { 6 7 Image duke; 8 9 public void init() { 10 duke = getImage(getDocumentBase(),"duke.gif"); 11 } 12 13 public void paint(Graphics g) { 14 g.drawImage(duke, 25, 25, this); 15 } 16 }

  45. 9.3 오디오 파일 • 오디오 클립의 재생 • 자바 언어는 확장자가 .au 파일인 오디오 클립을 플레이할 수 있는 방법을 제공 • 오디오 클립에 관련된 자바 클래스는 java.applet.AudioClip 클래스이다. • Applet 메소드 • getAudioClip(URL url, String file_name) • 오디오 클립 메소드 • play() 함수 - AudioClip 클래스를 플레이 • loop() 함수 - 반복적으로 플레이 • stop() 함수 - 플레이를 중지

  46. 9.3 오디오 파일 예제 : AudioTestTwo.java 1 import java.awt.*; 2 import java.applet.*; 3 4 public class AudioTestTwo extends Applet { 5 AudioClip sound; 6 7 public void init() { 8 sound=getAudioClip(getCodeBase(), "bark.au"); 9 } 10 11 public void paint(Graphics g) { 12 g.drawString("Audio Test", 25, 25); 13 } 14 public void start() { 15 sound.loop(); 16 } 17 public void stop() { 18 sound.stop(); 19 } 20 }

  47. 9.4 자바 에니메이션 • - 주의사항 • 1. 얼마나 많은 양의 정지영상 사용 ? • 2. 애니메이션 화면의 크기를 결정 • 3. 이미지의 해상도와 사용된 색상의 수 결정 • 4. 사용자에 대한 배려

  48. 9.4 자바 에니메이션 • 자바 애니메이션 • 보통 여러 장의 그림은 일정시간 간격을 두고 화면에 디스플레이(display)시켜서 그림이 움직이는 듯하게 보여주는 방식 • 시계 프로그램은 매초마다 시간을 그래픽스 클래스의 drawString() 메소드를 이용해서 새로 그려서 작성 예제 : FClock.java …….. 5 public class FClock extends Applet implements Runnable { …….. 26 while(!stop) { 27 day = new Date(); 28 repaint(); 29 try { 30 Thread.sleep(1000); 31 }catch(Exception ex) { 32 System.out.println("Exception:" + ex.toString()); 33 } ……...

  49. 원을 움직이는 예

  50. RollingCircle.java import java.awt.*; import java.applet.*; public class RollingCircle extends Applet implements Runnable{ int x, y; int width, height; int nSpeed; Color c; Thread tThread; }

More Related