Bibi's DevLog ๐Ÿค“๐ŸŽ

[Java - AWT ํ•™์Šต์ •๋ฆฌ] ์ž๋ฐ”์˜ GUIํ”„๋กœ๊ทธ๋ž˜๋ฐ - (2) ์ด๋ฒคํŠธ์ฒ˜๋ฆฌ, Adapterํด๋ž˜์Šค, paint()์™€ Graphics, Image ๋ณธ๋ฌธ

๐Ÿ–ฅ BE ๋ฐฑ์—”๋“œ/Java ์ž๋ฐ”

[Java - AWT ํ•™์Šต์ •๋ฆฌ] ์ž๋ฐ”์˜ GUIํ”„๋กœ๊ทธ๋ž˜๋ฐ - (2) ์ด๋ฒคํŠธ์ฒ˜๋ฆฌ, Adapterํด๋ž˜์Šค, paint()์™€ Graphics, Image

๋น„๋น„ bibi 2020. 11. 25. 17:54

์ด ๊ธ€์€ ์•„๋ž˜ ๋งํฌ ๋ฌธ์„œ๋ฅผ ๋ณด๋ฉฐ ๊ณต๋ถ€ํ•œ ๊ฒƒ์„ ์š”์•ฝ์ •๋ฆฌํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค๐Ÿ™‡‍โ™€๏ธ

'์ž๋ฐ”์˜ ์ •์„' ๋‚จ๊ถ์„ฑ ๋‹˜์˜ ๋ธ”๋กœ๊ทธ ์ž๋ฃŒ - AWT

 

[์ž๋ฐ”์˜ ์ •์„ 2ํŒ] 13์žฅ AWT์ž…๋‹ˆ๋‹ค.(PDF)

java์˜ ์ •์„ 3ํŒ์—์„œ AWT๋ฅผ ๋บ๋”๋‹ˆ ํ•„์š”ํ•˜๋‹ค๋Š” ๋ถ„๋“ค์ด ์žˆ์–ด์„œ ๋”ฐ๋กœ ๋ฝ‘์•„์„œ ์˜ฌ๋ฆฝ๋‹ˆ๋‹ค. ํ•„์š”ํ•˜์‹  ๋ถ„๋“ค ๋งˆ์Œ๊ป ๊ฐ€์ ธ๋‹ค ์“ฐ์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค.

codechobo.tistory.com

 

์•„๋ž˜ ๊นƒํ—ˆ๋ธŒ ๋งํฌ์—์„œ ๊ฐ™์€ ๋‚ด์šฉ์„ ๋” ํŽธํ•˜๊ฒŒ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

github.com/bibi6666667/Codesquad-Cocoa-JAVA/blob/main/mission/src/mission6/AWT.md

 

bibi6666667/Codesquad-Cocoa-JAVA

์ฝ”๋“œ์Šค์ฟผ๋“œ - ๋งˆ์Šคํ„ฐ์ฆˆ ์ฝ”์ฝ”์•„ ๊ณผ์ •(JAVA)์ €์žฅ์†Œ์ž…๋‹ˆ๋‹ค. Contribute to bibi6666667/Codesquad-Cocoa-JAVA development by creating an account on GitHub.

github.com

 



์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ event handling

์ด๋ฒคํŠธ event ๋ž€?

  • ์ด๋ฒคํŠธ : ์‚ฌ์šฉ์ž ๋˜๋Š” ํ”„๋กœ๊ทธ๋žจ ์ฝ”๋“œ์— ์˜ํ•ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ์‚ฌ๊ฑด.

    • (์‚ฌ์šฉ์ž๊ฐ€ ๋งˆ์šฐ์Šค๋ฅผ ์›€์ง์ด๊ฑฐ๋‚˜ ํด๋ฆญํ•จ / ํ‚ค๋ณด๋“œ๋ฅผ ๋ˆ„๋ฆ„ / Frame์˜ ํฌ๊ธฐ๋ฅผ ๋ณ€๊ฒฝ ...)
  • ์ด๋ฒคํŠธ ์†Œ์Šค(event source, ์ด๋ฒคํŠธ ๋ฐœ์ƒ์ง€)

    • ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•œ ์ปดํฌ๋„ŒํŠธ. (์˜ˆ์‹œ : ์‚ฌ์šฉ์ž๊ฐ€ ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ ์ด๋ฒคํŠธ ๋ฐœ์ƒ -> button์ด ์ด๋ฒคํŠธ ์†Œ์Šค.)
  • ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ(event handler, ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ๊ธฐ)

    • ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ์‹คํ–‰ํ•  ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•ด ๋†“์€ ํด๋ž˜์Šค. ์ฃผ๋กœ ํ•จ์ˆ˜๋‚˜ ๋ฉ”์†Œ๋“œ์ด๋‹ค.
  • ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ(event listener, ์ด๋ฒคํŠธ ๊ฐ์ง€๊ธฐ)

    • ์ด๋ฒคํŠธ๋ฅผ ๊ฐ์ง€ํ•˜๊ณ  ์ฒ˜๋ฆฌํ•˜๋Š” ํด๋ž˜์Šค.
  • ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ(event handling, ์ด๋ฒคํŠธ ํ•ธ๋“ค๋ง)

    • (์ด๋ฒคํŠธ ์†Œ์Šค์™€ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๋กœ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฒƒ?)
    • ์ด๋ฒคํŠธ์— ๋Œ€ํ•œ ์ˆ˜ํ–‰์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์—ฌ ์ด๋ฒคํŠธ ์†Œ์Šค์— ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๋กœ ๋“ฑ๋กํ•˜๋Š” ๊ฒƒ.
    • ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋”๋ผ๋„, ๊ทธ์— ๋Œ€ํ•œ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜์ง€ ์•Š์œผ๋ฉด ์•„๋ฌด ์ผ๋„ ์ผ์–ด๋‚˜์ง€ ์•Š์Œ.
  • ์‚ฌ์‹ค ์‚ฌ์šฉ์ž๊ฐ€ AWTํ”„๋กœ๊ทธ๋žจ์„ ์‚ฌ์šฉํ•˜๋ฉด์„œ ํ•˜๋Š” ๋ชจ๋“  ๋™์ž‘์ด ์ด๋ฒคํŠธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค.
    ๋‹จ์ง€ ์ด๋ฒคํŠธ์— ๋Œ€ํ•œ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ๊ฐ€ ๋˜์–ด์žˆ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์•„๋ฌด ์ผ๋„ ์ผ์–ด๋‚˜์ง€ ์•Š๋Š” ๊ฒƒ.

์ด๋ฒคํŠธ์˜ ๋ฐœ์ƒ๊ณผ ์ฒ˜๋ฆฌ

  1. ์ด๋ฒคํŠธ ๋ฐœ์ƒ
    • ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ํ•ด๋‹น ์ด๋ฒคํŠธ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋จ.
  2. ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ๊ฐ€ ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธ
    • = ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๊ฐ€ ์ด๋ฒคํŠธ ์†Œ์Šค์— ๋“ฑ๋ก๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธ.
      3-1. (์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ๊ฐ€ ๋˜์–ด ์žˆ๋‹ค๋ฉด) ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๊ฐ€ ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•จ.
      3-2. (์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ๊ฐ€ ๋˜์–ด ์žˆ์ง€ ์•Š๋‹ค๋ฉด) ์•„๋ฌด ์ผ๋„ ์ผ์–ด๋‚˜์ง€ ์•Š์Œ.
  • ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ๋Š” "์˜ˆ์™ธ์ฒ˜๋ฆฌ"์™€ ์œ ์‚ฌํ•˜๋‹ค.
    • ๋‘˜ ๋‹ค ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋˜๊ณ , ์ผ์น˜ํ•˜๋Š” catch๋ธ”๋Ÿญ/Listener๊ฐ€ ์˜ˆ์™ธ/์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•จ.

์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ๋ฐฉ๋ฒ•

  • ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ(event handling)

    • (AWTํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ ์ค‘) ์‚ฌ์šฉ์ž์˜ ์–ด๋–ค ๋™์ž‘์— ์˜ํ•ด ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ, ์ด์— ๋Œ€ํ•œ ์–ด๋–ค ์ž‘์—…์ด ์ˆ˜ํ–‰๋˜๋„๋ก ํ•˜๋Š” ๊ฒƒ.
  • ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ ๋ฐฉ๋ฒ•

    1. (์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ ์„ ํƒ) โ‘ ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ ์ถ”๊ฐ€/์ œ๊ฑฐ ๋ฉ”์„œ๋“œ๋“ค ์ค‘์—์„œ ํ•„์š”ํ•œ ๊ฒƒ์„ ์ฐพ๋Š”๋‹ค.
      *(์˜ˆ์‹œ)์ฐฝ๋‹ซ๊ธฐ ๋ฒ„ํŠผ ๋ˆŒ๋ฆผ. ๋งˆ์šฐ์Šค ์™ผ์ชฝ ํด๋ฆญ ..
    2. (์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ์ž‘์„ฑ) ์„ ํƒํ•œ ๋ฉ”์„œ๋“œ๊ฐ€ ์†ํ•ด์žˆ๋Š” โ‘ก์ด๋ฒคํŠธ ๊ด€๋ จ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ํด๋ž˜์Šค๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.
    3. (์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ ์ž‘์„ฑ) ์œ„์—์„œ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•ด ์ด๋ฒคํŠธ ์†Œ์Šค์— Listener๋กœ ๋“ฑ๋กํ•œ๋‹ค.
    • โ— EventHandlerํด๋ž˜์Šค๋Š” WindowListener์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•จ.
      • ๋”ฐ๋ผ์„œ WindowListener์ธํ„ฐํŽ˜์ด์Šค์— ์ •์˜๋˜์–ด ์žˆ๋Š” ๋ชจ๋“  ์ถ”์ƒ ๋ฉ”์„œ๋“œ์˜ body๋ถ€๋ถ„์„ ๋งŒ๋“ค์–ด ์ฃผ์–ด์•ผ ํ•จ.
      • ๊ทธ๋ž˜์„œ ์•„๋ฌด ๋‚ด์šฉ๋„ ์—†๋Š” ๋ฉ”์„œ๋“œ๋“ค๋„ ๋งŒ๋“  ๊ฒƒ. (๋‚ด์šฉ์€ ์—†์ง€๋งŒ ๋ฉ”์„œ๋“œ body๋“ค์ด ์žˆ์–ด์•ผ ํ•œ๋‹ค)
        • ์ด๋ ‡๊ฒŒ body(ํ‹€)๋งŒ ์žˆ๋Š” ๋ฉ”์„œ๋“œ๋“ค์€ ํ˜ธ์ถœ๋˜์–ด๋„ ์‹คํ–‰ํ•  ๋ฌธ์žฅ์ด ์—†์œผ๋ฏ€๋กœ ๋ฐ”๋กœ ์ข…๋ฃŒ๋œ๋‹ค.

โ‘ก์ด๋ฒคํŠธ ๊ด€๋ จ ์ธํ„ฐํŽ˜์ด์Šค ๋ฐ AWT์ด๋ฒคํŠธ์™€ ์ฃผ์š” ๋ฉ”์„œ๋“œ

  • โ€ป AWT์ด๋ฒคํŠธ๋“ค์˜ ์ตœ๊ณ  ์กฐ์ƒ์€ AWTEvent์ด๋‹ค.

  • โ€ป ์ด๋ฒคํŠธ ๊ด€๋ จ ํด๋ž˜์Šค๋“ค์€ java.awt.event ํŒจํ‚ค์ง€์— ์†ํ•œ๋‹ค.

  • ๊ฐ Listener๋“ค์„ ์ถ”๊ฐ€/์ œ๊ฑฐํ•˜๋Š” ๋ฉ”์„œ๋“œ(๊ณตํ†ต)

    • ์ถ”๊ฐ€ : void add๋ฆฌ์Šค๋„ˆ์ด๋ฆ„(์ด๋ฒคํŠธํ•ธ๋“ค๋Ÿฌ์ด๋ฆ„)
    • ์ œ๊ฑฐ : void remove๋ฆฌ์Šค๋„ˆ์ด๋ฆ„(์ด๋ฒคํŠธํ•ธ๋“ค๋Ÿฌ์ด๋ฆ„)
  • โœ… ๊ฐ ๋ฉ”์„œ๋“œ์—์„œ๋Š” ๋ฐœ์ƒํ•œ ์ด๋ฒคํŠธ์˜ ์ธ์Šคํ„ด์Šค์— ๋Œ€ํ•œ ์ฐธ์กฐ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

  • ๋”ฐ๋ผ์„œ ์ด๋ฒคํŠธ ๋ฐœ์ƒ ์‹œ ์ƒ์„ฑ๋œ ์ด๋ฒคํŠธ ์ธ์Šคํ„ด์Šค๋“ค์˜ ๋ฉ”์„œ๋“œ ์—ญ์‹œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค!

  • ์•„๋ž˜๋Š” AWT์ด๋ฒคํŠธ ์ข…๋ฅ˜, ์ด๋ฒคํŠธ๋ณ„ ์ฃผ์š” ๋ฉ”์„œ๋“œ ๋ฐ ๊ด€๋ จ ์ธํ„ฐํŽ˜์ด์Šค(~Listener) ์ด๋‹ค.

    โ€ป ๊ตฌ๋ถ„

    • ~Event : AWT์ด๋ฒคํŠธ
    • ~Listener : ๊ฐ ์ด๋ฒคํŠธ์˜ ์ด๋ฒคํŠธ๋ฆฌ์Šค๋„ˆ = ์ธํ„ฐํŽ˜์ด์Šค
    • ~Listener ํ•˜์œ„ ๋ฉ”์„œ๋“œ : ๊ฐ ์ด๋ฒคํŠธ ์ธํ„ฐํŽ˜์ด์Šค์— ์†ํ•˜๋Š” ๋ฉ”์„œ๋“œ.
    • ๊ตต์€๊ธ€์”จ ๋ฉ”์„œ๋“œ : ๊ฐ ์ด๋ฒคํŠธ์˜ ์ฃผ์š” ๋ฉ”์„œ๋“œ.
  • ๋ชจ๋“  ์ด๋ฒคํŠธ ๊ณตํ†ต ์ฃผ์š” ๋ฉ”์„œ๋“œ

    • Object getSource() : ์ด๋ฒคํŠธ ์†Œ์Šค๋ฅผ ๋ฐ˜ํ™˜.
      • java.utilํŒจํ‚ค์ง€์˜ EventObject์— ์ •์˜๋˜์–ด ์žˆ๋Š” ๋ฉ”์„œ๋“œ
    • String paramString() : ์ด๋ฒคํŠธ์™€ ๊ด€๋ จ๋œ ์ •๋ณด๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜.
      • java.awtํŒจํ‚ค์ง€์˜ AWTEvent์— ์ •์˜๋˜์–ด ์žˆ๋Š” ๋ฉ”์„œ๋“œ
  • ActionEvent

    • ActionListener
      • actionPeformed(ActonEvent ae)
    • String getActionCommand() : ์ด๋ฒคํŠธ์˜ ActionCommand๋ฅผ ๋ฐ˜ํ™˜.
    • int getModifiers() : ์ด๋ฒคํŠธ ๋ฐœ์ƒ ์‹œ ๋ˆ„๋ฅด๊ณ  ์žˆ๋˜ ํŠน์ˆ˜ํ‚ค๋ฅผ ๋ฐ˜ํ™˜.
    • long getWhen() : ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•œ ์‹œ๊ฐ„์„ long๊ฐ’์œผ๋กœ ๋ฐ˜ํ™˜.
  • ComponentEvent

    • ComponentListener
      • componentMoved(ActonEvent ae)
      • componentHidden(ActonEvent ae)
      • componentResized(ActonEvent ae)
      • componentShown(ActonEvent ae)
    • Component getComponent() : ์ด๋ฒคํŠธ ์†Œ์Šค(์ปดํฌ๋„ŒํŠธ)๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • MouseEvent

    • MouseMotionListener
      • mouseDragged(MouseEvent me)
      • mouseMoved(MouseEvent me)
    • MouseListener
      • mousePressed(MouseEvent me)
      • mouseReleased(MouseEvent me)
      • mouseEntered(MouseEvent me)
      • mouseExited(MouseEvent me)
      • mouseClicked(MouseEvent me)
    • int getClickCount() : ๋งˆ์šฐ์Šค๋ฅผ ํด๋ฆญํ•œ ํšŸ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜.
    • Point getPoint() : ์ด๋ฒคํŠธ๊ฐ€ ์ผ์–ด๋‚œ ์œ„์น˜๋ฅผ ๋ฐ˜ํ™˜.
    • int getX() : ์ด๋ฒคํŠธ๊ฐ€ ์ผ์–ด๋‚œ ์œ„์น˜์˜ x์ขŒํ‘œ๋ฅผ ๋ฐ˜ํ™˜.
    • int getY() : ์ด๋ฒคํŠธ๊ฐ€ ์ผ์–ด๋‚œ ์œ„์น˜์˜ y์ขŒํ‘œ๋ฅผ ๋ฐ˜ํ™˜.
  • MouseWheelEvent

    • MouseWheelListener
      • mouseWheelMoved(MouseWheelEvent e)
    • int getScrollAmount() : ์Šคํฌ๋กค๋˜์–ด์•ผ ํ•˜๋Š” ์–‘(amount)์„ ๋ฐ˜ํ™˜.
    • int getScrollType() : ์–ด๋–ค ์Šคํฌ๋กคํƒ€์ž…์— ์˜ํ•ด ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋Š”์ง€ ์•Œ๋ ค์คŒ.
    • int getUnitsToScroll() : ๋‹จ์œ„๋‹น ์Šคํฌ๋กคํ•ด์•ผ ํ•˜๋Š” ์–‘์„ ๋ฐ˜ํ™˜.
    • int getWheelRotation() : ๋งˆ์šฐ์Šค ํœ ์ด ์ด๋™ํ•œ ๋ฐฉํ–ฅ์„ ์•Œ๋ ค์คŒ.
  • KeyEvent

    • KeyListener
      • keyPressed(KeyEvent ke)
      • keyReleased(KeyEvent ke)
      • keyTyped(KeyEvent ke)
    • char getKeyChar() : ๋ˆŒ๋Ÿฌ์ง„ ํ‚ค๋ฅผ ๋ฐ˜ํ™˜.
    • int getKeyCode() : ๋ˆŒ๋Ÿฌ์ง„ ํ‚ค์˜ ์ฝ”๋“œ๋ฅผ ๋ฐ˜ํ™˜.
    • String getKeyModifiersText(int modifiers) : ๋ˆŒ๋Ÿฌ์ง„ ํŠน์ˆ˜ํ‚ค(Alt, Ctrl) ๋˜๋Š” ํŠน์ˆ˜ํ‚ค ์กฐํ•ฉ์„ ์„ค๋ช…ํ•˜๋Š” ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜.
    • String getKeyText(int keyCode) : ๋ˆŒ๋Ÿฌ์ง„ ํ‚ค๋ฅผ HOME, F1๊ณผ ๊ฐ™์ด ํ‚ค๋ฅผ ์„ค๋ช…ํ•œ๋Š ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜.
  • TextEvent

    • TextListener
      • textValueChanged(TextEvent te)
  • FocusEvent

    • FocusListener
      • focusGained(FocusEvent fe)
      • focusLost(FocusEvent fe)
    • Component getOppositeComponent() : ํฌ์ปค์Šค๋ฅผ ์žƒ์€ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ฐ˜ํ™˜.
  • ItemEvent

    • ItemListener
      • itemStateChanged(ItemEvent ie)
    • Object getItem() : ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•œ item์„ ๋ฐ˜ํ™˜.
    • ItemSelectable getItemSelectable() : ์ด๋ฒคํŠธ ์†Œ์Šค๋ฅผ ๋ฐ˜ํ™˜.
    • int getStateChange() : item์˜ ์ƒํƒœ(์„ ํƒ/์„ ํƒํ•ด์ œ)๋ฅผ ๋ฐ˜ํ™˜.
  • AdjustmentEvent

    • AdjustmentListener
      • adjustmentValueChanged(AdjustmentEvent ae)
    • Adjustable getAdjustable() : ์ด๋ฒคํŠธ ์†Œ์Šค๋ฅผ ๋ฐ˜ํ™˜.
    • int getAdjustmentType() : ๊ฐ’์„ ๋ณ€๊ฒฝ์‹œํ‚จ ๋ฐฉ๋ฒ•์„ ์•Œ๋ ค์คŒ.
    • int getValue() : ์ด๋ฒคํŠธ ๋ฐœ์ƒ ์‹œ์˜ (์Šคํฌ๋กค๋ฐ”์˜)๊ฐ’์„ ๋ฐ˜ํ™˜.
    • boolean getValueIsAdjusting() : ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜๊ณ  ์žˆ๋Š” ์ค‘์ธ์ง€ ์•Œ๋ ค์คŒ.
  • WindowEvent

    • WindowListener
      • windowClosing(WindowEvent we)
      • windowOpened(WindowEvent we)
      • windowIconified(WindowEvent we)
      • windowDeiconified(WindowEvent we)
      • windowClosed(WindowEvent we)
      • windowActivated(WindowEvent we)
      • windowDeactivated(WindowEvent we)
    • WindowFocusListener
      • windowGainedFocus(WindowEvent e)
      • windowLostFocus(WindowEvent e)
    • WindowStateListener
      • windowStateChanged(WindowEvent e)
    • int getNewState() : ์œˆ๋„์šฐ์˜ ํ˜„์žฌ ์ƒํƒœ๋ฅผ ๋ฐ˜ํ™˜.
    • int getOldState() : ์œˆ๋„์šฐ์˜ ์ด์ „ ์ƒํƒœ๋ฅผ ๋ฐ˜ํ™˜.
    • Window getOppositeWindow() : ์ด๋ฒคํŠธ ๋ฐœ์ƒ ์ „ ํฌ์ปค์Šค๋ฅผ ๊ฐ€์กŒ๋˜ ์œˆ๋„์šฐ๋ฅผ ๋ฐ˜ํ™˜.
    • Window getWindow() : ์ด๋ฒคํŠธ ์†Œ์Šค๋ฅผ ๋ฐ˜ํ™˜.
  • ContainerEvent

    • ContainerListener
      • componentAdded(ContainerEvent ce)
      • componentRemoved(ContainerEvent ce)
    • Component getChild() : ์ปจํ…Œ์ด๋„ˆ์— ์ถ”๊ฐ€/์‚ญ์ œ๋œ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ฐ˜ํ™˜.
    • Container getContainer() : ์ด๋ฒคํŠธ ์†Œ์Šค๋ฅผ ๋ฐ˜ํ™˜.

โ‘ ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ ์ถ”๊ฐ€/์ œ๊ฑฐ ๋ฉ”์„œ๋“œ๋“ค

  • ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๋ฅผ ์ปดํฌ๋„ŒํŠธ์— ์ถ”๊ฐ€/์ œ๊ฑฐํ•˜๋Š” ๋ฉ”์„œ๋“œ๋“ค์ด๋‹ค.

  • โ‘ก์ด๋ฒคํŠธ ๊ด€๋ จ ์ธํ„ฐํŽ˜์ด์Šค๋“ค ์˜ ๋ฉ”์„œ๋“œ๋“ค์— ๋Œ€ํ•œ ์„ค๋ช…์— ํ•ด๋‹นํ•œ๋‹ค!

  • โ€ป ์œˆ๋„์šฐ = ์ปจํ…Œ์ด๋„ˆ์ธ Window์™€ ๊ทธ ์ž์†๋“ค์ธ Frame, Dialog, FileDialog๋ฅผ ๋œปํ•œ๋‹ค.

    • ํ™œ์„ฑ์œˆ๋„์šฐ active window : ํ˜„์žฌ ์ž‘์—… ์ค‘์ธ ์œˆ๋„์šฐ = ๊ฐ€์žฅ ์œ„์— ๋‚˜ํƒ€๋‚˜๋Š” ์œˆ๋„์šฐ
    • ๋น„ํ™œ์„ฑํ™” deactivated : ์‚ฌ์šฉ์ž๊ฐ€ ๋‹ค๋ฅธ ์ž‘์—…์„ ์œ„ํ•ด ๋‹ค๋ฅธ ์œˆ๋„์šฐ๋ฅผ ์„ ํƒ์‹œ, ์ด์ „์— ์ž‘์—…ํ•˜๋˜ ์œˆ๋„์šฐ๋Š” ๋น„ํ™œ์„ฑํ™”๋จ.
  • โ€ป ActionEvent ae

    • ๊ณ ์ˆ˜์ค€ ์ด๋ฒคํŠธ๋กœ, ์ปดํฌ๋„ŒํŠธ์— ์ •์˜๋œ ๋™์ž‘์ด ์ˆ˜ํ–‰๋˜์—ˆ์„ ๋•Œ ๋ฐœ์ƒํ•จ.
    • ActionEvent๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ
      • Button์ด ๋ˆŒ๋Ÿฌ์กŒ์„ ๋•Œ(ํด๋ฆญ/์ŠคํŽ˜์ด์Šค๋ฐ”)
      • Menu๋ฅผ ํด๋ฆญํ–ˆ์„ ๋•Œ
      • TextField ์—์„œ Enterํ‚ค๋ฅผ ๋ˆŒ๋ €์„ ๋•Œ
      • List์˜ item ํ•˜๋‚˜๋ฅผ ์„ ํƒํ•˜์—ฌ ๋”๋ธ”ํด๋ฆญํ–ˆ์„ ๋•Œ
    • ์ด ๋•Œ๋Š” MouseEvent๋‚˜ KeyEvent๊ฐ€ ๋ฐœ์ƒํ•ด๋„, ์ตœ์ข…์ ์œผ๋กœ๋Š” ActionEvent๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒƒ์œผ๋กœ ์ฒ˜๋ฆฌ๋จ.
    • ๋”ฐ๋ผ์„œ ActionListener์˜ actionPerformed(ActionEvent ae)์— ์ด๋ฒคํŠธ ๋ฐœ์ƒ์‹œ ์ˆ˜ํ–‰ํ•  ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค.
    • ActionEvent์˜ ์žฅ์  : ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋Š” ์—ฌ๋Ÿฌ ๋ฐฉ๋ฒ•์ด ์กด์žฌํ•˜๋”๋ผ๋„ ์ตœ์ข…์ ์œผ๋กœ๋Š” ActionEvent๋งŒ ๋ฐœ์ƒํ•˜๋ฏ€๋กœ, ์ด์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค.
  • ์•„๋ž˜๋Š” ๋ฉ”์„œ๋“œ์™€, ๊ฐ ๋ฉ”์„œ๋“œ์˜ ํ˜ธ์ถœ์‹œ๊ธฐ์ด๋‹ค.

  • actionPeformed(ActonEvent ae)

    • Button์ด ๋ˆŒ๋Ÿฌ์กŒ์„ ๋•Œ(ํด๋ฆญ/์ŠคํŽ˜์ด์Šค๋ฐ”)
    • Menu๋ฅผ ํด๋ฆญํ–ˆ์„ ๋•Œ
    • TextField ์—์„œ Enterํ‚ค๋ฅผ ๋ˆŒ๋ €์„ ๋•Œ
    • List์˜ item ํ•˜๋‚˜๋ฅผ ์„ ํƒํ•˜์—ฌ ๋”๋ธ”ํด๋ฆญํ–ˆ์„ ๋•Œ
  • componentMoved(ActonEvent ae)

  • componentShown(ActonEvent ae)

  • componentHidden(ActonEvent ae)

  • componentResized(ActonEvent ae)

    • ๊ฐ๊ฐ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ด๋™๋˜์—ˆ์„ ๋•Œ / ํ™”๋ฉด์— ๋ณด์ผ ๋–„ / ํ™”๋ฉด์— ๋ณด์ด์ง€ ์•Š์„ ๋•Œ / ํฌ๊ธฐ๊ฐ€ ๋ณ€๊ฒฝ๋˜์—ˆ์„ ๋•Œ.
  • mouseDragged(MouseEvent me) : ๋งˆ์šฐ์Šค ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅธ ์ฑ„๋กœ ๋งˆ์šฐ์Šค๋ฅผ ์›€์ง์˜€์„ ๋•Œ

  • mouseMoved(MouseEvent me) : ๋งˆ์šฐ์Šค ํฌ์ธํ„ฐ๋ฅผ ์ด๋™์‹œํ‚ฌ ๋•Œ

  • mousePressed(MouseEvent me) : ๋งˆ์šฐ์Šค ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ

  • mouseReleased(MouseEvent me) : ๋งˆ์šฐ์Šค ๋ฒ„ํŠผ์„ ๋—ด์—ˆ์„ ๋•Œ

  • mouseEntered(MouseEvent me) : ๋งˆ์šฐ์Šคํฌ์ธํ„ฐ๊ฐ€ ์ด๋ฒคํŠธ์†Œ์Šค ์˜์—ญ ๋‚ด๋กœ ๋“ค์–ด์™”์„ ๋–„

  • mouseExited(MouseEvent me) : ๋งˆ์šฐ์Šคํฌ์ธํ„ฐ๊ฐ€ ์ด๋ฒคํŠธ์†Œ์Šค ์˜์—ญ ๋ฐ–์œผ๋กœ ๋‚˜๊ฐˆ ๋•Œ

  • mouseClicked(MouseEvent me) : ๋งˆ์šฐ์Šค ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €๋‹ค ๋–ผ์—ˆ์„ ๋•Œ

  • mouseWheelMoved(MouseWheelEvent e) : ๋งˆ์šฐ์Šค ํœ ์„ ์›€์ง์˜€์„ ๋•Œ

  • keyPressed(KeyEvent ke) : ํ‚ค๋ณด๋“œ ํ‚ค๋ฅผ ๋ˆŒ๋ €์„ ๋•Œ

  • keyReleased(KeyEvent ke) : ํ‚ค๋ณด๋“œ ํ‚ค๋ฅผ ๋–ผ์—ˆ์„ ๋•Œ

  • keyTyped(KeyEvent ke) : ํ‚ค๋ณด๋“œ ํ‚ค๋ฅผ ๋ˆŒ๋ €๋‹ค ๋–ผ์—ˆ์„ ๋•Œ

  • textValueChanged(TextEvent te) : TextField/TextArea์˜ ๋‚ด์šฉ์ด ๋ณ€๊ฒฝ๋˜์—ˆ์„ ๋•Œ

  • focusGained(FocusEvent fe) : ์ด๋ฒคํŠธ ์†Œ์Šค๋กœ focus๊ฐ€ ์ด๋™ํ–ˆ์„ ๋•Œ

  • focusLost(FocusEvent fe) : ์ด๋ฒคํŠธ ์†Œ์Šค๊ฐ€ ๊ฐ–๊ณ  ์žˆ๋˜ focus๊ฐ€ ๋‹ค๋ฅธ ์ปดํฌ๋„ŒํŠธ๋กœ ์ด๋™ํ–ˆ์„ ๋•Œ

  • itemStateChanged(ItemEvent ie) : Checkbox, CheckboxItem, List, Choice ์˜ status๊ฐ€ ๋ณ€๊ฒฝ๋˜์—ˆ์„ ๋•Œ.

    • (selected <-> unselected ๋กœ status๊ฐ€ ์ „ํ™˜๋  ๋•Œ)
  • adjustmentValueChanged(AdjustmentEvent ae) : Scrollbar์˜ ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜์—ˆ์„ ๋•Œ

  • windowOpened(WindowEvent we) : ์œˆ๋„์šฐ๊ฐ€ ์—ด๋ ธ์„ ๋•Œ

  • windowClosing(WindowEvent we) : ์œˆ๋„์šฐ๊ฐ€ ๋‹ซํž ๋•Œ(๋‹ซ๊ธฐ ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ)

  • windowClosed(WindowEvent we) : ์œˆ๋„์šฐ๊ฐ€ ๋‹ซํ˜”์„ ๋•Œ(dispose()๊ฐ€ ํ˜ธ์ถœ๋˜์—ˆ์„ ๋–„)

  • windowIconified(WindowEvent we) : ์œˆ๋„์šฐ๊ฐ€ ์ตœ์†Œํ™”(์•„์ด์ฝ˜ํ™”)๋˜์—ˆ์„ ๋•Œ

  • windowDeiconified(WindowEvent we) : ์œˆ๋„์šฐ๊ฐ€ ์ตœ์†Œํ™” ์ƒํƒœ์—์„œ ์›๋ž˜ ํฌ๊ธฐ๋กœ ๋Œ์•„์™”์„ ๋•Œ

  • windowActivated(WindowEvent we) : ์œˆ๋„์šฐ๊ฐ€ ํ™œ์„ฑํ™” ๋˜์—ˆ์„ ๋–„

  • windowDeactivated(WindowEvent we) : ์œˆ๋„์šฐ๊ฐ€ ๋น„ํ™œ์„ฑํ™” ๋˜์—ˆ์„ ๋•Œ

  • windowGainedFocus(WindowEvent e) : ์œˆ๋„์šฐ๊ฐ€ ํฌ์ปค์Šค๋ฅผ ์–ป์„ ๋•Œ

  • windowLostFocus(WindowEvent e) : ์œˆ๋„์šฐ๊ฐ€ ํฌ์ปค์Šค๋ฅผ ์žƒ์—ˆ์„ ๋•Œ

  • windowStateChanged(WindowEvent e) : ์œˆ๋„์šฐ์˜ ์ƒํƒœ๊ฐ€ ๋ณ€ํ–ˆ์„ ๋•Œ

  • componentAdded(ContainerEvent ce) : ์ปจํ…Œ์ด๋„ˆ์— ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ถ”๊ฐ€๋˜์—ˆ์„ ๋•Œ

  • componentRemoved(ContainerEvent ce) : ์ปจํ…Œ์ด๋„ˆ์— ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ œ๊ฑฐ๋˜์—ˆ์„ ๋•Œ


๊ฐ ์ด๋ฒคํŠธ์˜ ์ฃผ์š” ๋ฉ”์„œ๋“œ


์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ ์˜ˆ์‹œ - ๋‹ซ๊ธฐ ๋ฒ„ํŠผ ๋™์ž‘์‹œํ‚ค๊ธฐ

package mission6.event;

import java.awt.*;
import java.awt.event.*;

public class FrameTest3 {
    public static void main(String[] args) {
        Frame f = new Frame("Login"); // Frame๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค
        f.setSize(300, 200); // Frame์˜ ํฌ๊ธฐ๋ฅผ ์„ค์ •ํ•œ๋‹ค

        // EventHandlerํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด Frame์˜ WindowListener๋กœ ๋“ฑ๋กํ•œ๋‹ค
        f.addWindowListener(new EventHandler());
        f.setVisible(true); // Frame .
    }
}

// ํด๋ž˜์Šค EventHandler์—์„œ,
// ์ด๋ฏธ ๋งŒ๋“ค์–ด์ ธ ์žˆ๋Š” WindowListener๋ผ๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๊ฒŒ ํ•จ.
class EventHandler implements WindowListener
{
    public void windowOpened(WindowEvent e) {}
    // ↓ windowClosing๋ฉ”์„œ๋“œ : Frame์˜ ๋‹ซ๊ธฐ ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ ํ˜ธ์ถœ๋œ๋‹ค.
    // ๋”ฐ๋ผ์„œ ํ•ด๋‹น ๋ฉ”์„œ๋“œ์— ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์ข…๋ฃŒํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค.
    public void windowClosing(WindowEvent e) { // ์‚ฌ์šฉ์ž๊ฐ€ ๋‹ซ๊ธฐ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋Š” ์ด๋ฒคํŠธ
        e.getWindow().setVisible(false); // Frame์„ ํ™”๋ฉด์—์„œ ๋ณด์ด์ง€ ์•Š๋„๋ก ํ•˜๊ณ 
        e.getWindow().dispose(); // ๋ฉ”๋ชจ๋ฆฌ์—์„œ ์ œ๊ฑฐํ•œ๋‹ค
        System.exit(0); // ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•œ๋‹ค
        // โ€ป ์ด ๋ฉ”์„œ๋“œ ๋‚ด์—์„œ๋Š” ์ด๋ฒคํŠธ ๋ฐœ์ƒ์‹œ ์ƒ์„ฑ๋œ WindowEvent์ธ์Šคํ„ด์Šค์˜ ์ฐธ์กฐ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
        // ๊ทธ๋ž˜์„œ WindowEvent์ธ์Šคํ„ด์Šค์˜ ๋ฉ”์„œ๋“œ๋“ค์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
    }
    public void windowClosed(WindowEvent e) {} // ์•„๋ฌด๋‚ด์šฉ๋„ ์—†๋Š” ๋ฉ”์„œ๋“œ๋“ค ๊ตฌํ˜„
    public void windowIconified(WindowEvent e) {}
    public void windowDeiconified(WindowEvent e) {}
    public void windowActivated(WindowEvent e) {}
    public void windowDeactivated(WindowEvent e) {}
}

Adapterํด๋ž˜์Šค

  • Adapterํด๋ž˜์Šค

    • ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๋ฅผ ์ง์ ‘ ๊ตฌํ˜„ํ•˜๋Š” ๋Œ€์‹ , Adapterํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์•„ ์›ํ•˜๋Š” ๋ฉ”์„œ๋“œ๋งŒ ์ž‘์„ฑ(์˜ค๋ฒ„๋ผ์ด๋”ฉ)ํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ณ ์•ˆ๋˜์—ˆ๋‹ค.
    • ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์ž‘์„ฑํ•  ๋•Œ, ํ•ด๋‹น ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ์— ์ •์˜๋œ ๋ชจ๋“  ์ถ”์ƒ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•˜๋Š” ๋ถˆํŽธํ•จ์„ ๊ฐœ์„ ํ•˜๊ธฐ ์œ„ํ•ด ๊ณ ์•ˆ๋จ.
  • Adapter๋ฅผ ์ƒ์†๋ฐ›์œผ๋ฉด, ์ด๋ฒคํŠธ๋ฆฌ์Šค๋„ˆ(์ธํ„ฐํŽ˜์ด์Šค)์˜ ๋ชจ๋“  ์ถ”์ƒ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜์ง€ ์•Š๊ณ  ์›ํ•˜๋Š” ๋ฉ”์„œ๋“œ๋งŒ ์ž‘์„ฑํ•˜๋ฉด ๋œ๋‹ค.

    • (Adapterํด๋ž˜์Šค๋Š” ๊ทธ์ € ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ์— ์ •์˜๋œ ๋ชจ๋“  ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”์ƒ๋ฉ”์„œ๋“œ๋กœ(์•„๋ฌด ๋‚ด์šฉ ์—†์ด) ๊ตฌํ˜„ํ•ด ๋†“์€ ๋ฉ”์„œ๋“œ์ผ ๋ฟ์ด๋‹ค.)
  • ๋”ฐ๋ผ์„œ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์ž‘์„ฑํ•  ๋•Œ..

    • ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๋ฅผ ๊ตฌํ˜„ํ•˜๊ฑฐ๋‚˜ : implements ์ด๋ฒคํŠธ๋ฆฌ์Šค๋„ˆ
    • Adapterํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์œผ๋ฉด ๋œ๋‹ค : extends Adapterํด๋ž˜์Šค
    • (๋‘˜์˜ ์ฐจ์ด๋Š” ์ฝ”๋“œ ์ž‘์„ฑ์˜ ํŽธ๋ฆฌํ•จ ์™ธ์—๋Š” ์•„๋ฌด ์ฐจ์ด๊ฐ€ ์—†๋‹ค)

Adapterํด๋ž˜์Šค๋กœ ๊ตฌํ˜„๋˜์–ด ์žˆ๋Š” ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ

  • ์•„๋ž˜๋Š” Adapterํด๋ž˜์Šค๋กœ ๊ตฌํ˜„๋˜์–ด ์žˆ๋Š” ์ด๋ฒคํฌ ๋ฆฌ์Šค๋„ˆ์˜ ๋ชฉ๋ก์ด๋‹ค.
  • (์ขŒ)์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ(์ธํ„ฐํŽ˜์ด์Šค) - (์šฐ)Adapter ํด๋ž˜์Šค
  • ComponentListener - ComponentAdapter
  • ContainerListener - ContainerAdapter
  • FocusListener - FocusAdapter
  • KeyListener - KeyAdapter
  • MouseListener - MouseAdapter
  • MouseMotionListener - MouseMotionAdapter
  • WindowListener - WindowAdapter

์ด๋ฒคํŠธ ์˜ˆ์ œ (1) - ๋กœ๊ทธ์ธ ๊ตฌํ˜„ํ•˜๊ธฐ

  • ๊ฐ์ฒด์ง€ํ–ฅ์ ์œผ๋กœ ์ •๋ฆฌ๋œ ์ฝ”๋“œ์˜ ์˜ˆ์‹œ.
    • Frame์„ ์ƒ์†๋ฐ›๋Š” ํด๋ž˜์Šค ๊ตฌํ˜„
    • Frame์— ํฌํ•จ๋˜๋Š” ์ปดํฌ๋„ŒํŠธ๋“ค์„ ์ธ์Šคํ„ด์Šค๋ฉค๋ฒ„๋กœ ์„ ์–ธ
    • Frame์˜ ์ƒ์„ฑ์ž์—์„œ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ƒ์„ฑ ๋ฐ ์„ค์ •ํ•˜๊ฒŒ ํ•จ
  • TextField์ธ tfId, tfPwd์—๋„ EventHandler์ธ์Šคํ„ด์Šค๋ฅผ ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๋กœ ๋“ฑ๋กํ•œ ์ด์œ 
    • TextField์—์„œ ์—”ํ„ฐ ํ‚ค๋ฅผ ๋‘˜๋Ÿฌ๋„ ok๋ฒ„ํŠผ์„ ๋ˆ„๋ฅธ ๊ฒƒ๊ณผ ๊ฐ™๊ฒŒ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด.
    • ActionEvent์™€ ๊ฐ™์€ ๊ณ ์ˆ˜์ค€ ์ด๋ฒคํŠธ์˜ ํ•„์š”์„ฑ.
  • TextField/TextArea/TextComponent์˜ ๋ฉ”์„œ๋“œ๋“ค ํ™œ์šฉ
    • requestFocus() : ์›ํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ๊ฐ€ ํฌ์ปค์Šค๋ฅผ ๊ฐ–๊ฒŒ ํ•œ๋‹ค.
    • selectAll() : TextField ๋˜๋Š” TextArea์— ์ž…๋ ฅ๋œ text์ „์ฒด๋ฅผ ์„ ํƒ๋œ ์ƒํƒœ๋กœ ๋งŒ๋“ ๋‹ค
    • setEchoChar(char c) : - ์‹ค์ œ๋กœ ์ž…๋ ฅ๋œ ๊ฐ’์„ ๊ฐ์ถ”๊ธฐ ์œ„ํ•ด ํ™”๋ฉด์— ๋ณด์—ฌ์งˆ ๋ฌธ์ž๋ฅผ ์ง€์ •ํ•œ๋‹ค
import java.awt.*;
import java.awt.event.*;

public class TextFieldTest2 extends Frame {
    // Frameํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›๋Š”๋‹ค(=Frameํด๋ž˜์Šค์˜ public/protected ๋ณ€์ˆ˜ ๋ฐ ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ๊ฐ€๋Šฅ)
    Label lid;
    Label lpwd;
    TextField tfId;
    TextField tfPwd;
    Button ok;

    TextFieldTest2(String title) { // ์ƒ์„ฑ์ž
        super(title); // Frameํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

        lid = new Label("ID :", Label.RIGHT); // Label์˜ text์ •๋ ฌ์„ ์˜ค๋ฅธ์ชฝ์œผ๋กœ
        lpwd = new Label("Password :", Label.RIGHT);

        // ์•ฝ 10๊ฐœ์˜ ๊ธ€์ž๋ฅผ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” TextField์ƒ์„ฑ
        tfId = new TextField(10);
        tfPwd = new TextField(10);
        tfPwd.setEchoChar('*'); // ์ž…๋ ฅํ•œ ๊ฐ’ ๋Œ€์‹  '*'์ด ๋ณด์ด๊ฒŒ ํ•œ๋‹ค

        ok = new Button("OK");
        // OK๋ฒ„ํŠผ๊ณผ TextField์— ์ด๋ฒคํŠธ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ Listener๋ฅผ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค
        // TextField์— ์ด๋ฒคํŠธ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋Š” ์ด์œ ? ์—”ํ„ฐํ‚ค๋ฅผ ๋ˆŒ๋ €์„ ๋•Œ๋„ ์ฒ˜๋ฆฌ๋˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด.
        tfId.addActionListener(new EventHandler());
        tfPwd.addActionListener(new EventHandler());
        ok.addActionListener(new EventHandler());

        setLayout(new FlowLayout()); // LayoutManager๋ฅผ FlowLayout์œผ๋กœ
        add(lid); // ์ƒ์„ฑํ•œ Component๋“ค์„ Frame์— ํฌํ•จ์‹œํ‚จ๋‹ค
        add(tfId);
        add(lpwd);
        add(tfPwd);
        add(ok);
        setSize(450, 65);
        setVisible(true); // Frame์ด ํ™”๋ฉด์— ๋ณด์ด๊ฒŒ ํ•œ๋‹ค
    }
    public static void main(String args[]) {
        TextFieldTest2 f = new TextFieldTest2("Login");
    }
    class EventHandler implements ActionListener {
        public void actionPerformed(ActionEvent e){
            String id = tfId.getText(); // tfId์— ์ž…๋ ฅ๋˜์–ด์žˆ๋Š” text๋ฅผ ์–ป์–ด์˜จ๋‹ค
            String password = tfPwd.getText();
            if (!id.equals("javachobo")){
                System.out.println("์ž…๋ ฅํ•˜์‹  id๊ฐ€ ์œ ํšจํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.");
                // id๋ฅผ ๋‹ค์‹œ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋„๋ก, focus๋ฅผ tfId๋กœ ์˜ฎ๊ธด๋‹ค
                tfId.requestFocus();
                tfId.selectAll(); // tfId์— ์ž…๋ ฅ๋œ  text๊ฐ€ ์„ ํƒ๋˜๊ฒŒ ํ•œ๋‹ค
            } else if (!password.equals("asdf")) {
                System.out.println("์ž…๋ ฅํ•˜์‹  ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.");
                // pwd๋ฅผ ๋‹ค์‹œ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋„๋ก  focus๋ฅผ tfPwd๋กœ ์˜ฎ๊ธด๋‹ค
                tfPwd.requestFocus();
                tfPwd.selectAll();
            } else {
                System.out.println( id + "๋‹˜, ์„ฑ๊ณต์ ์œผ๋กœ ๋กœ๊ทธ์ธ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
            }
        }
    } // class EventHandler
}

์ด๋ฒคํŠธ ์˜ˆ์ œ (2) - ๋งˆ์šฐ์Šคํฌ์ธํ„ฐ ์ขŒํ‘œ ์ฐ๊ธฐ

  • Frame ์œ„์—์„œ ๋งˆ์šฐ์Šค ํฌ์ธํ„ฐ๋ฅผ ์›€์ง์ด๋ฉด ํ™”๋ฉด์— ํฌ์ธํ„ฐ์˜ ์ขŒํ‘œ๊ฐ€ ๋‚˜ํƒ€๋‚œ๋‹ค.
  • ์›๋ฆฌ
    • Frame์œ„์—์„œ ๋งˆ์šฐ์Šคํฌ์ธํ„ฐ๋ฅผ ์›€์ง์ด๋ฉด MouseEvent๊ฐ€ ๋ฐœ์ƒํ•˜๊ณ  mouseMoved๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋จ.
    • MouseEvent์—๋Š” getX()์™€ getY() ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ์–ด ์ด ๋ฉ”์„œ๋“œ๋“ค์„ ํ™œ์šฉํ•ด ๋งˆ์šฐ์Šคํฌ์ธํ„ฐ์˜ ์ขŒํ‘œ๋ฅผ ์•Œ ์ˆ˜ ์žˆ๋‹ค.
  • ์ด๋ฒคํŠธ ์†Œ์Šค : Frame
  • ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ : MouseMotionListener
  • ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ : EventHandler ํด๋ž˜์Šค
import java.awt.*;
import java.awt.event.*;


public class MouseEventTest extends Frame{ // Frameํด๋ž˜์Šค ์ƒ์†๋ฐ›๊ธฐ
    Label location;

    MouseEventTest (String title) { // ์ƒ์„ฑ์ž
        super(title); // ๋ถ€๋ชจ ํด๋ž˜์Šค์ธ Frame(String title)ํ˜ธ์ถœ
        location = new Label("Mouse Pointer Location : ");
        location.setSize(195,15);
        location.setLocation(5,30);
        location.setBackground(Color.yellow);
        add(location);

        //EventHandler์˜ ์ธ์Šคํ„ด์Šค๋ฅผ Frame์˜ Listener๋กœ ๋“ฑ๋ก.
        // ๋งˆ์šฐ์Šค ๋ชจ์…˜ ์ด๋ฒคํŠธ ๋ฐœ์ƒ์‹œ addMouseMotionListener๊ฐ€ ๊ฐ์ง€ํ•˜๊ณ , EventHandler๊ฐ€ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ.
        addMouseMotionListener(new EventHandler());

        setSize(300,200);
        setLayout(null);
        setVisible(true);
    }

    public static void main(String[] args) {
        MouseEventTest mainWin = new MouseEventTest("MouseEventTest");
    }

    class EventHandler implements MouseMotionListener {
        public void mouseDragged(MouseEvent e) {}
        public void mouseMoved(MouseEvent e) {
            // .setText() Label ๋‚ด ํ…์ŠคํŠธ๋ฅผ ()๋กœ ๋ฐ”๊พธ๊ธฐ
            location.setText("Mouse Pointer Location : (" + e.getX()
                                                    + "," + e.getY() + ")");
        }
    } // EventHandler
}

์ด๋ฒคํŠธ ์˜ˆ์ œ (3) - .getSource(), ItemListener ํ™œ์šฉ

  • Checkbox(radio button) ์ค‘ ํ•˜๋‚˜๋ฅผ ํด๋ฆญํ•˜๋ฉด Frame์˜ ๋ฐฐ๊ฒฝ์ƒ‰์ด ๋ณ€๊ฒฝ๋œ๋‹ค.

  • ์›๋ฆฌ

    • Checkbox๋ฅผ ํด๋ฆญํ•˜๋ฉด ItemEvent๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.
    • itemStateChanged๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค.
      • (itemStateChanged๋Š” Checkbox, CheckboxItem, List, Choice ์˜ status๊ฐ€ ๋ณ€๊ฒฝ๋˜์—ˆ์„ ๋•Œ ํ˜ธ์ถœ๋œ๋‹ค.
        • (selected <-> unselected ๋กœ status๊ฐ€ ์ „ํ™˜๋  ๋•Œ)
    • ์ด ๋•Œ itemStateChanged๋ฉ”์„œ๋“œ ๋‚ด์—์„œ getSource()๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ์ด๋ฒคํŠธ ์†Œ์Šค(ํด๋ฆญํ•œ Checkbox)๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.
    • getSource()๋กœ ์–ป์€ ์ด๋ฒคํŠธ ์†Œ์Šค์—์„œ getLabel()๋กœ Label์„ ์–ป๋Š”๋‹ค.
    • if๋ฌธ์œผ๋กœ ์–ด๋–ค ์ฒดํฌ๋ฐ•์Šค์— ์ฒดํฌํ–ˆ๋Š”์ง€ ํ™•์ธํ•œ ๋’ค, ๊ฐ ์ฒดํฌ๋ฐ•์Šค Label์— ๋งž๋Š” ๋ฐฐ๊ฒฝ์ƒ‰์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.
  • ์ด๋ฒคํŠธ ์†Œ์Šค : ํด๋ฆญํ•œ Checkbox

  • ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ : ItemListener

  • ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ : EventHandlerํด๋ž˜์Šค (implements ItemListener)

import java.awt.*;
import java.awt.event.*;

public class CheckboxEventTest2 extends Frame { // Frameํด๋ž˜์Šค ์ƒ์†๋ฐ›์•„์˜ค๊ธฐ
    CheckboxGroup group;
    Checkbox cb1;
    Checkbox cb2;
    Checkbox cb3;

    CheckboxEventTest2 (String title) { // ์ƒ์„ฑ์ž
        super(title); // ๋ถ€๋ชจ ํด๋ž˜์Šค์ธ Frame์˜ ์ƒ์„ฑ์ž ํ˜ธ์ถœ
        group = new CheckboxGroup();
        cb1 = new Checkbox("red", group, true); // ๊ธฐ๋ณธ ์ฒดํฌ ์ƒํƒœ
        cb2 = new Checkbox("green", group, false);
        cb3 = new Checkbox("blue", group, false);

        cb1.addItemListener(new EventHandler());
        cb2.addItemListener(new EventHandler());
        cb3.addItemListener(new EventHandler());

        setLayout(new FlowLayout());
        add(cb1);
        add(cb2);
        add(cb3);
        setBackground(Color.red);
        setSize(300,200);
        setVisible(true);
    }

    public static void main(String[] args) {
        CheckboxEventTest2 mainWin = new CheckboxEventTest2("CheckboxEventTest2");
    }

    class EventHandler implements ItemListener {
        public void itemStateChanged(ItemEvent e) {
            Checkbox cb = (Checkbox)e.getSource();
            String color = cb.getLabel();
            System.out.println(color); // ์ž˜ ์ฐํžˆ๋Š”์ง€ ํ™•์ธ์šฉ.
            if(color.equals("red")) {
                setBackground(Color.red);
            } else if (color.equals("green")) {
                setBackground(Color.green);
            } else {
                setBackground(Color.blue);
            }
        }
    }
}

์ด๋ฒคํŠธ ์˜ˆ์ œ (4) - ๊ฐ„๋‹จ ๋ฉ”๋ชจ์žฅ(ํŒŒ์ผ ํŽธ์ง‘๊ธฐ) ๋งŒ๋“ค๊ธฐ (FileDialog, Menu, TextArea)

  • ํŒŒ์ผ ์ฝ๊ธฐ, ์“ฐ๊ธฐ, ์ €์žฅ์ด ๊ฐ€๋Šฅํ•œ ๋ฉ”๋ชจ์žฅ ๋งŒ๋“ค๊ธฐ

    • fileOpen(), saveAs()๋ฉ”์„œ๋“œ ๋‚ด์šฉ๋“ค์€ ๋‚˜์ค‘์— ๋”ฐ๋กœ ๊ณต๋ถ€ํ•ด ์ดํ•ดํ•˜๊ธฐ.
    • java.io.*; ํŒจํ‚ค์ง€์˜ FileReader, BufferedReader, StringWriter / FileWriter, BuffredWriter
  • ๋ฉ”๋‰ด ๊ตฌ์„ฑํ•˜๊ธฐ (MenuBar, Menu, MenuItem)

  • TextEditor ์ƒ์„ฑ์ž

    • Frame์— MenuComponent๋“ค๊ณผ TextArea ๊ตฌ์„ฑ
    • ๋ฉ”๋‰ด ๊ฐ ๊ธฐ๋Šฅ์— EventHandler(ActionListener) ๋“ฑ๋ก
    • ActionListener๊ฐ€ ํ˜ธ์ถœ๋˜๋Š” ๊ฒฝ์šฐ -(2) Menu๋ฅผ ํด๋ฆญํ–ˆ์„ ๋•Œ ์— ํ•ด๋‹นํ•˜๋ฏ€๋กœ ๋ชจ๋‘ ActionEvent์— ํ•ด๋‹น.
  • MyHandler ํด๋ž˜์Šค : ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ

    • New - .setText("")๊ณต๋ฐฑ ์ถœ๋ ฅ
    • Open - FileDialog ํ†ตํ•ด ํŒŒ์ผ์„ ์„ ํƒํ•œ ๋’ค, fileOpen() ๋ฉ”์„œ๋“œ ํ˜ธ์ถœํ•ด ํ™”๋ฉด์— ๋ณด์—ฌ์คŒ.
    • Save As.. - FileDialog ํ†ตํ•ด ํ˜„์žฌ ์ž‘์„ฑ๋œ ๋‚ด์šฉ์„ ์„ ํƒํ•œ ๋’ค saveAs() ๋ฉ”์„œ๋“œ ํ˜ธ์ถœํ•ด ํŒŒ์ผ๋กœ ์ €์žฅํ•จ.
    • Exit - System.exit(0); ์œผ๋กœ ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ
import java.awt.*;
import java.awt.event.*;
import java.io.*; // ํŒŒ์ผIO ํŒจํ‚ค์ง€?

public class TextEditor extends Frame { // Frameํด๋ž˜์Šค ์ƒ์†๋ฐ›์•„์˜ค๊ธฐ
    String fileName;
    TextArea content;
    MenuBar  mb;
    Menu     mFile;
    MenuItem miNew, miOpen, miSaveAs, miExit;

    TextEditor (String title) {
        super(title); // ๋ถ€๋ชจํด๋ž˜์Šค Frame์˜ ์ƒ์„ฑ์ž ํ˜ธ์ถœ
        content = new TextArea();
        add(content);

        mb = new MenuBar();
        mFile = new Menu("File");
        miNew = new MenuItem("New");
        miOpen = new MenuItem("Open");
        miSaveAs = new MenuItem("Save As...");
        miExit = new MenuItem("Exit");

        mFile.add(miNew); // Menu์— MenuItem๋“ค ์ถ”๊ฐ€
        mFile.add(miOpen);
        mFile.add(miSaveAs);
        mFile.addSeparator(); // ๋ฉ”๋‰ด ๋ถ„๋ฆฌ์„ (separator) ์ถ”๊ฐ€
        mFile.add(miExit);

        mb.add(mFile); // MenuBar์— Menu ์ถ”๊ฐ€
        setMenuBar(mb); // (setMenuBar๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด) Frame์— MenuBar ์ถ”๊ฐ€

        // ๋ฉ”๋‰ด์— ์ด๋ฒคํŠธํ•ธ๋“ค๋Ÿฌ ๋“ฑ๋ก
        MyHandler handler = new MyHandler();
        miNew.addActionListener(handler);
        miOpen.addActionListener(handler);
        miSaveAs.addActionListener(handler);
        miExit.addActionListener(handler);

        setSize(300,200);
        setVisible(true);
    }

    // ์„ ํƒ๋œ ํŒŒ์ผ ๋‚ด์šฉ์„ ์ฝ์–ด์„œ TextArea์— ๋ณด์—ฌ์ฃผ๋Š” ๋ฉ”์„œ๋“œ
    void fileOpen(String fileName) {
        // java.io.*;์˜ ํด๋ž˜์Šค๋“ค
        FileReader fr;
        BufferedReader br;
        StringWriter sw;

        try {
            fr = new FileReader(fileName);
            br = new BufferedReader(fr);
            sw = new StringWriter();

            int ch = 0;
            while ((ch=br.read()) != -1) {
                sw.write(ch);
            }

            br.close();
            content.setText(sw.toString()); // TextArea์— ๋ถˆ๋Ÿฌ์˜จ ๋‚ด์šฉ์„ ์ ์–ด์ค€๋‹ค
        } catch (IOException e) {
            e.printStackTrace();
        }
    } // fileOpen()

    // TextArea์˜ ๋‚ด์šฉ์„ ์ง€์ •๋œ ํŒŒ์ผ์— ์ €์žฅํ•˜๋Š” ๋ฉ”์„œ๋“œ
    void saveAs(String fileName) {
        FileWriter fw;
        BufferedWriter bw;
        try {
            fw = new FileWriter(fileName);
            bw= new BufferedWriter(fw);
            bw.write(content.getText()); // TextArea ์˜ ๋‚ด์šฉ์„ ํŒŒ์ผ์— ์ €์žฅํ•œ๋‹ค.
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        } // try
    } // saveAs๋ฉ”์„œ๋“œ์˜ ๋

    public static void main(String args[]) {
        TextEditor mainWin = new TextEditor("Text Editor");
    } // main๋ฉ”์„œ๋“œ์˜ ๋

    // ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ : ๋ฉ”๋‰ด ํด๋ฆญํ–ˆ์„ ๋•Œ ๋ฉ”๋‰ด๋ณ„ ์ฒ˜๋ฆฌ์ฝ”๋“œ
    class MyHandler implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            String command = e.getActionCommand(); // ActionEvent์—์„œ ๋ฐœ์ƒํ•œ ๋ช…๋ น ๋ฐ›์•„์˜ค๊ธฐ?

            if (command.equals("New")) {
                content.setText(""); // ๊ณต๋ฐฑ ์ถœ๋ ฅ (๋นˆ ํŽ˜์ด์ง€)
            } else if(command.equals("Open")) {
                FileDialog fileOpen =
                        new FileDialog(TextEditor.this, "ํŒŒ์ผ์—ด๊ธฐ");
                fileOpen.setVisible(true);
                fileName = fileOpen.getDirectory() + fileOpen.getFile();
                System.out.println(fileName);
                // fileOpen() ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ - ์„ ํƒ๋œ ํŒŒ์ผ์˜ ๋‚ด์šฉ์„ TextArea์— ๋ณด์—ฌ์ค€๋‹ค
                fileOpen(fileName);
            } else if(command.equals("Save As...")) {
                FileDialog fileSave =
                        new FileDialog(TextEditor.this,"ํŒŒ์ผ์ €์žฅ ",FileDialog.SAVE);
                fileSave.setVisible(true);
                fileName = fileSave.getDirectory() + fileSave.getFile();
                System.out.println(fileName);
                // saveAs() ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ - ํ˜„์žฌ TextArea์˜ ๋‚ด์šฉ์„ ์„ ํƒ๋œ ํŒŒ์ผ์— ์ €์žฅํ•œ๋‹ค
                saveAs(fileName);
            } else if(command.equals("Exit")) {
                System.exit(0); // ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ.
            }
        }
    } // class MyHandler
} // class TextEditor


AWT์˜ ๊ทธ๋ž˜ํ”ฝ - AWT๋กœ ๊ทธ๋ฆผ ๊ทธ๋ฆฌ๊ธฐ

  • ์ด์ œ AWT์—์„œ ์„ ๊ณผ ๋„ํ˜•์ด ์–ด๋–ป๊ฒŒ, ์–ด๋–ค ๊ณผ์ •์œผ๋กœ ๊ทธ๋ ค์ง€๋Š”์ง€ ๋ฐฐ์›Œ๋ณผ ๊ฒƒ์ด๋‹ค.
  • ์ž๋ฐ”๋ฅผ ์ด์šฉํ•ด์„œ ์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ, ๊ฒŒ์ž„ ๋“ฑ ๋†’์€ ์ˆ˜์ค€์˜ ๊ทธ๋ž˜ํ”ฝ๊ธฐ๋Šฅ์ด ํ•„์š”ํ•œ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋งŒ๋“œ๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.
    • ์ž๋ฐ”์˜ ๊ทธ๋ž˜ํ”ฝ์ฒ˜๋ฆฌ API - Java2D. JDK API์˜ ์ผ๋ถ€๋กœ java.awtํŒจํ‚ค์ง€์— ํฌํ•จ๋˜์–ด ์žˆ๋‹ค
  • AWT๋Š” ์ž๋ฐ” ์ดˆ๊ธฐ์˜ GUIํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋„๊ตฌ๋กœ, JDK๋ฒ„์ „์ด ๋†’์•„์ง€๋ฉฐ ๋” ๋†’์€ ๊ธฐ๋Šฅ์˜ API์ธ Swing, Java2D, ์ตœ๊ทผ์—” JavaFX๊นŒ์ง€ ๊ฐœ๋ฐœ๋˜์—ˆ๋‹ค.
    • ๊ทธ๋Ÿผ์—๋„ AWT๊ฐ€ Swing๊ณผ Java2D์˜ ๋ฟŒ๋ฆฌ์ด๊ธฐ ๋•Œ๋ฌธ์—, AWT๋ฅผ ํ†ตํ•ด ์ž๋ฐ” GUIํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ๋ฐฐ์šฐ๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

paint()์™€ Graphics

  • paint() : ์ปดํฌ๋„ŒํŠธ์— ๊ทธ๋ฆผ์„ ๊ทธ๋ฆฌ๊ธฐ ์œ„ํ•œ ๋ฉ”์„œ๋“œ.

    • Componentํด๋ž˜์Šค์— ์ •์˜๋˜์–ด ์žˆ์–ด, ๋ชจ๋“  ์ปดํฌ๋„ŒํŠธ์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค.
    • Frame, Canvas, Panel ๋“ฑ ๊ทธ๋ฆผ์„ ๊ทธ๋ฆฌ๋ ค๋Š” ์ปดํฌ๋„ŒํŠธ์˜ paint()๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉ ํ•˜๋ฉด ๋œ๋‹ค.
  • paint() ์„ ์–ธ

    • public void paint (Graphics g) { ... }
    • Graphics๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ •์˜ํ•จ.
    • paint()๋ฅผ ์ž‘์„ฑํ•˜๋ฉด Graphics๊ฐ€ ๊ฐ€์ง„ ๋‹ค์–‘ํ•œ ๊ทธ๋ž˜ํ”ฝ ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด ๊ทธ๋ฆผ์„ ๊ทธ๋ฆฐ๋‹ค.
  • Graphics

    • ๋ชจ๋“  ์ปดํฌ๋„ŒํŠธ๋Š” Graphics ๊ฐ์ฒด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.
    • Graphics๊ฐ์ฒด์—๋Š” ๊ฐ ์ปดํฌ๋„ŒํŠธ์˜ ๊ทธ๋ž˜ํ”ฝ ๊ด€๋ จ์ •๋ณด๊ฐ€ ๋‹ด๊ฒจ์ ธ ์žˆ๋‹ค.
    • ๋”ฐ๋ผ์„œ ์ปดํฌ๋„ŒํŠธ์— ๊ทธ๋ฆผ์„ ๊ทธ๋ฆฌ๋ ค๋ฉด..
    1. getGraphics()๋ฅผ ํ˜ธ์ถœํ•ด ์ปดํฌ๋„ŒํŠธ์˜ Graphics๊ฐ์ฒด์— ๋Œ€ํ•œ ์ฐธ์กฐ๋ฅผ ์–ป์–ด๋‚ธ๋‹ค
    2. Graphics๊ฐ์ฒด๊ฐ€ ๊ฐ€์ง„ ๋‹ค์–‘ํ•œ ๊ทธ๋ž˜ํ”ฝ ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด ๊ทธ๋ฆผ์„ ๊ทธ๋ฆฐ๋‹ค.
    • Graphics๊ฐ์ฒด = ์ถ”์ƒํด๋ž˜์Šค์ธ Graphics๋ฅผ ์™„์ „ํžˆ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ๋งํ•จ.

Graphics๊ฐ€ ์ œ๊ณตํ•˜๋Š” ์ฃผ์š” ๋ฉ”์„œ๋“œ

  1. ๋ฌธ์ž์ถœ๋ ฅ ๊ด€๋ จ ๋ฉ”์„œ๋“œ
    • ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ถœ๋ ฅํ•  ๋ฌธ์ž์—ด์˜ font๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
    • void drawString(String str, int x, int y) : x, y ์œ„์น˜์— ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅํ•œ๋‹ค.
    • Font getFont() : ํ˜„์žฌ ์„ค์ •๋˜์–ด ์žˆ๋Š” Font๋ฅผ ์–ป๋Š”๋‹ค.
    • void setFont(Font font) : ์ถœ๋ ฅํ•  ๋ฌธ์ž์—ด์˜ ํฐํŠธ๋ฅผ ์ง€์ •ํ•œ๋‹ค.
    • FontMetrics getFontMetrics() : FontMetrics๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
      • FontMetrics๊ฐ์ฒด๋Š” ์ง€์ •๋œ ํฐํŠธ์— ๋Œ€ํ•œ ์ƒ์„ธํ•œ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•œ๋‹ค.
  2. ์ƒ‰ ๊ด€๋ จ ๋ฉ”์„œ๋“œ
    • ์ถœ๋ ฅํ•  ๊ทธ๋ฆผ์˜ ์ƒ‰์„ ์ง€์ •ํ•˜๊ฑฐ๋‚˜, ์ง€์ •๋œ ์ƒ‰์˜ ์ •๋ณด๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.
    • Color getColor() : ํ˜„์žฌ ์„ค์ •๋˜์–ด ์žˆ๋Š” Color๋ฅผ ์–ป๋Š”๋‹ค.
    • void setColor(Color c) : ์ถœ๋ ฅํ•  ์ƒ‰์„ ์ง€์ •ํ•œ๋‹ค.
  3. ๋„ํ˜• ๊ด€๋ จ ๋ฉ”์„œ๋“œ
    • ๋‹ค์–‘ํ•œ ๋„ํ˜•์„ ๊ทธ๋ฆฌ๋Š” ๋ฉ”์„œ๋“œ.
    • draw~() : ๋„ํ˜•์˜ ์„ ๋งŒ ๊ทธ๋ฆฐ๋‹ค.
    • fill~() : ๋„ํ˜• ๋‚ด๋ถ€๋ฅผ ์„ ์˜ ์ƒ‰์œผ๋กœ ์ฑ„์šด๋‹ค.
    • ์„ (line)์„ ๊ทธ๋ฆฌ๋Š” ๋ฉ”์„œ๋“œ
      • void drawLine(int x1, int y1, int x2, int y2)
    • ํƒ€์›(oval)์„ ๊ทธ๋ฆฌ๋Š” ๋ฉ”์„œ๋“œ
      • void drawOval(int x, int y, int width, int height)
      • void fillOval(int x, int y, int width, int height)
    • ์‚ฌ๊ฐํ˜•(rectangle)์„ ๊ทธ๋ฆฌ๋Š” ๋ฉ”์„œ๋“œ
      • void drawRect(int x, int y, int width, int height)
      • void fillRect(int x, int y, int width, int height)
    • ๋‘ฅ๊ทผ ์‚ฌ๊ฐํ˜•(roundrectangle)์„ ๊ทธ๋ฆฌ๋Š” ๋ฉ”์„œ๋“œ
      • void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
      • void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
    • ์ž…์ฒด ์‚ฌ๊ฐํ˜•(3Drectangle)์„ ๊ทธ๋ฆฌ๋Š” ๋ฉ”์„œ๋“œ
      • void draw3DRect(int x, int y, int width, int height, boolean raised)
      • void fill3DRect(int x, int y, int width, int height, boolean raised)
    • ํ˜ธ(arc, ๋ถ€์ฑ„๊ผด)๋ฅผ ๊ทธ๋ฆฌ๋Š” ๋ฉ”์„œ๋“œ
      • void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)
      • void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)
        • startAngle : ๋ถ€์ฑ„๊ผด์„ ๊ทธ๋ฆฌ๊ธฐ ์‹œ์ž‘ํ•˜๋Š” ๊ฐ๋„
        • arcAngle : ๋ถ€์ฑ„๊ผด์„ ํ‘œํ˜„ํ•˜๋Š” ๊ฐ๋„
    • ๋‹ค๊ฐํ˜•(polygon)์„ ๊ทธ๋ฆฌ๋Š” ๋ฉ”์„œ๋“œ
      • void drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
      • void fillPolygon(int[] xPoints, int[] yPoints, int nPoints)
      • void drawPolyline(int[] xPoints, int[] yPoints, int nPoints)
        • int[] xPoints : ์ ๋“ค์˜ x์ขŒํ‘œ ๋ชจ์Œ ๋ฐฐ์—ด
        • int[] yPoints : ์ ๋“ค์˜ y์ขŒํ‘œ ๋ชจ์Œ ๋ฐฐ์—ด
        • int nPoints : ์ ๋“ค์˜ ๊ฐฏ์ˆ˜
  4. ๊ธฐํƒ€ ๋ฉ”์„œ๋“œ
    • void clearRect(int x, int y, int width, int height) : ์ง€์ •๋œ ์˜์—ญ์„ ์ง€์šด๋‹ค.
    • boolean drawImage(Image img, int x, int y, ImageObserver observer) : x,y์˜ ์œ„์น˜์— ์ด๋ฏธ์ง€๋ฅผ ๊ทธ๋ฆฐ๋‹ค.

Graphics๋ฉ”์„œ๋“œ๋กœ ๋„ํ˜• ๊ทธ๋ฆฌ๊ธฐ

  • ์„ , ์›, ๋‘ฅ๊ทผ ์‚ฌ๊ฐํ˜•, ๋‹ค๊ฐํ˜•, ํ˜ธ ๊ทธ๋ฆฌ๊ธฐ
import java.awt.*;
import java.awt.event.*;

public class GraphicsEx1 extends Frame{ // Frameํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์Œ
    public static void main(String[] args) {
        new GraphicsEx1("GraphicsEx1");
    }

    public GraphicsEx1 (String title) { // ์ƒ์„ฑ์ž
        super(title); // Frame์˜ ์ƒ์„ฑ์ž ํ˜ธ์ถœ
        addWindowListener(new WindowAdapter() {
            // ์ฐฝ๋‹ซ๊ธฐ ๋ฒ„ํŠผ ๊ตฌํ˜„
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        setBounds(100, 100, 400, 300);
        setVisible(true);
    }

    public void paint (Graphics g) {
        g.setFont(new Font("Serif", Font.PLAIN, 15));
        g.drawString("Graphics๋ฅผ ์ด์šฉํ•ด ๊ทธ๋ฆผ์„ ๊ทธ๋ฆฝ๋‹ˆ๋‹ค.", 10, 50);

        // ์› ๊ทธ๋ฆฌ๊ธฐ
        g.drawOval(50,100, 50,50); // ์ผ๋ฐ˜ ์›
        // ํŒŒ๋ž€์ƒ‰ ์› ๊ทธ๋ฆฌ๊ธฐ
        g.setColor(Color.BLUE);//( ์ƒ‰ ์„ค์ • -> ๊ทธ๋ฆฌ๊ธฐ : ๊ทธ ์ƒ‰๊น”๋กœ ๊ทธ๋ ค์ง )
        g.fillOval(100,100, 50,50); // ํŒŒ๋ž€์ƒ‰ ์›

        // ์„  ๊ทธ๋ฆฌ๊ธฐ
        g.setColor(Color.red);
        g.drawLine(100,100, 150,150);

        // ๋‘ฅ๊ทผ ์‚ฌ๊ฐํ˜• ๊ทธ๋ฆฌ๊ธฐ
        g.fillRoundRect(200, 100, 120, 80, 30, 30);
        // ๋‹ค๊ฐํ˜• ๊ทธ๋ฆฌ๊ธฐ
        g.setColor(Color.orange);
        g.fillPolygon(new int[]{50, 100, 150, 200}, new int[]{250, 200, 200, 250}, 4);

        // ํ˜ธ ๊ทธ๋ฆฌ๊ธฐ
        g.setColor(Color.cyan);
        g.fillArc(250, 200, 100, 100, 0, 120);
        // startAngle : ๋ถ€์ฑ„๊ผด์„ ๊ทธ๋ฆฌ๊ธฐ ์‹œ์ž‘ํ•˜๋Š” ๊ฐ๋„
        // arcAngle : ๋ถ€์ฑ„๊ผด์„ ํ‘œํ˜„ํ•  ๊ฐ๋„
    }
}

AWT์“ฐ๋ ˆ๋“œ์™€ repaint()

โ“ paint()๋ฅผ ํ˜ธ์ถœํ•œ ์ ์ด ์—†๋Š”๋ฐ ์–ด๋–ป๊ฒŒ ๊ทธ๋ฆผ์ด ๊ทธ๋ ค์กŒ์„๊นŒ?
โ— paint()๋Š” ์ง์ ‘ ํ˜ธ์ถœํ•˜์ง€ ์•Š์•„๋„, AWT์“ฐ๋ ˆ๋“œ์— ์˜ํ•ด ์ž๋™์œผ๋กœ ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์„œ๋“œ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

  • AWT์“ฐ๋ ˆ๋“œ
    • GUI์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹คํ–‰ํ•˜๋ฉด ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋˜๋Š” ๋ฐ๋ชฌ ์“ฐ๋ ˆ๋“œ์ด๋‹ค.
    • ํ‰์†Œ์—๋Š” ๋Œ€๊ธฐ์ƒํƒœ์— ์žˆ๋‹ค.
    • ํ™”๋ฉด์ด ๊ฐฑ์‹ ๋˜์–ด์•ผ ํ•  ๋•Œ๊ฐ€ ๋˜๋ฉด, paint() ๋˜๋Š” update()๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ํ™”๋ฉด์„ ๋‹ค์‹œ ๊ทธ๋ฆฐ๋‹ค.
      • (AWT์“ฐ๋ ˆ๋“œ์— ์˜ํ•ด) ์ž๋™์œผ๋กœ ํ™”๋ฉด์ด ๊ฐฑ์‹ ๋˜๋Š” ์ƒํ™ฉ
        • ์ฒ˜์Œ ํ™”๋ฉด์— ๋‚˜ํƒ€๋‚  ๋•Œ
        • ๋‹ค๋ฅธ ํ™”๋ฉด์— ๊ฐ€๋ ค์ ธ ์žˆ๋˜ ๋ถ€๋ถ„์ด ๋‹ค์‹œ ํ™”๋ฉด์— ๋‚˜ํƒ€๋‚  ๋•Œ
        • ์•„์ด์ฝ˜ํ™”(์ตœ์†Œํ™”) ๋˜์–ด ์žˆ๋‹ค๊ฐ€, ์›๋ž˜ ํฌ๊ธฐ๋กœ ํ™”๋ฉด์— ๋‚˜ํƒ€๋‚  ๋•Œ
    • ์œ„์˜ ์ƒํ™ฉ ์™ธ์—๋„ ํ™”๋ฉด์„ ๊ฐฑ์‹ ํ•˜๋ ค๋ฉด repaint()๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋œ๋‹ค.
      • repaint() -> update(Graphics g) -> paint(Graphics g)
      • repaint()๋Š” AWT์“ฐ๋ ˆ๋“œ์—๊ฒŒ ํ™”๋ฉด์„ ๊ฐฑ์‹ ํ•  ๊ฒƒ์„ ์š”์ฒญํ•˜๊ณ ,
      • AWT์“ฐ๋ ˆ๋“œ๋Š” 0.1์ดˆ๋งˆ๋‹ค ํ™•์ธํ•ด ์š”์ฒญ์ด ์žˆ์œผ๋ฉด update()๋ฅผ ํ˜ธ์ถœํ•˜๊ณ ,
      • update()๋Š” ํ™”๋ฉด์„ ์ง€์šด ๋’ค paint()๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

repaint() ์˜ˆ์ œ - ๋งˆ์šฐ์Šค ํฌ์ธํ„ฐ ์œ„์น˜์— * ์ถœ๋ ฅํ•˜๊ธฐ

  • Frame ๋‚ด์˜ ๋งˆ์šฐ์Šค ํฌ์ธํ„ฐ์˜ ์œ„์น˜์— "*"์„ ์ถœ๋ ฅํ•˜๋Š” ์˜ˆ์ œ.

  • Frame์˜์—ญ ๋‚ด์—์„œ ๋งˆ์šฐ์Šคํฌ์ธํ„ฐ๋ฅผ ์›€์ง์ด๋ฉด "*"๋„ ๋”ฐ๋ผ ์›€์ง์ธ๋‹ค.

    • repaint()๋ฅผ ์ด์šฉํ•ด, ๋งˆ์šฐ์Šคํฌ์ธํ„ฐ๊ฐ€ ์›€์ง์ผ ๋–„๋งˆ๋‹ค xy์ขŒํ‘œ๋ฅผ ๋ฐ›์•„ "*" ์ƒˆ๋กœ ๊ทธ๋ฆฌ๋Š” ๊ฒƒ์ด๋‹ค.
  • ์›๋ฆฌ

    • ๋งˆ์šฐ์Šค๋ฅผ ์›€์ง์ด๋ฉด MouseEvent๊ฐ€ ๋ฐœ์ƒํ•˜๊ณ , EventHandler์˜ mouseMoved()๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค.
    • MouseEvent์˜ getX(), getY()๋ฅผ ํ†ตํ•ด ํ˜„์žฌ ๋งˆ์šฐ์Šคํฌ์ธํ„ฐ์˜ ์œ„์น˜๋ฅผ ์–ป์–ด ์ธ์Šคํ„ด์Šค๋ณ€์ˆ˜ x,y์— ์ €์žฅํ•œ๋‹ค.
    • repaint()๋ฅผ ํ†ตํ•ด AWT์“ฐ๋ ˆ๋“œ์—๊ฒŒ Frame์„ ํ™”๋ฉด์„ ๊ฐฑ์‹ ํ•˜๋„๋ก ์š”์ฒญํ•œ๋‹ค.
    • AWT์“ฐ๋ ˆ๋“œ๋Š” 0.1์ดˆ๋งˆ๋‹ค ํ™•์ธํ•ด ์š”์ฒญ์ด ์žˆ์œผ๋ฉด update()๋ฅผ ํ˜ธ์ถœํ•ด ํ™”๋ฉด์„ ์ง€์šฐ๊ณ , paint()๋กœ "*"๋ฅผ ์ƒˆ๋กœ ๊ทธ๋ฆฐ๋‹ค.
  • ๋งŒ์•ฝ repaint()๊ฐ€ ์—†์œผ๋ฉด ๋งˆ์šฐ์Šค๋ฅผ ์›€์ง์—ฌ๋„ "*"์ด ์ด๋™ํ•˜์ง€ ์•Š๋Š”๋‹ค!

import java.awt.*;
import java.awt.event.*;

public class GraphicsEx2 extends Frame implements MouseMotionListener {
 // Frameํด๋ž˜์Šค ์ƒ์†๋ฐ›๊ณ  MouseMotionListener๊ตฌํ˜„.
    int x = 0;
    int y = 0;

    public static void main(String[] args) {
        new GraphicsEx2("GraphicsEx2");
    }

    public GraphicsEx2(String title) { // ์ƒ์„ฑ์ž
        super(title); // ๋ถ€๋ชจํด๋ž˜์Šค Frame์˜ ์ƒ์„ฑ์ž ํ˜ธ์ถœ
        addMouseMotionListener(this);//GraphicsEx2 ๊ฐ์ฒด์— MouseMotionListener ์—ฐ๊ฒฐ
        addWindowListener(new WindowAdapter() { // ์ข…๋ฃŒ๊ธฐ๋Šฅ - Adapterํด๋ž˜์Šค ํ™œ์šฉ
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        setBounds(100, 100, 500, 500);
        setVisible(true);
    }

    public void paint(Graphics g) {
        g.drawString("๋งˆ์šฐ์Šค๋ฅผ ์›€์ง์—ฌ๋ณด์„ธ์š”.", 10, 50);
        g.drawString("*", x, y);
    }

    // MouseMotionListener ์ด๋ฒคํŠธ์ฒ˜๋ฆฌ ๋ฉ”์„œ๋“œ
    public void mouseMoved(MouseEvent me) {
        x = me.getX(); // MouseEvent์˜ getX()๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด ํ˜„์žฌ ๋งˆ์šฐ์Šคํฌ์ธํ„ฐ์˜ x์ขŒํ‘œ๋ฅผ ์–ป์–ด์˜ด
        y = me.getY(); // MouseEvent์˜ getY()๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด ํ˜„์žฌ ๋งˆ์šฐ์Šคํฌ์ธํ„ฐ์˜ y์ขŒํ‘œ๋ฅผ ์–ป์–ด์˜ด
        repaint(); // ๋งˆ์šฐ์Šค ์›€์ง์ผ ๋•Œ๋งˆ๋‹ค x,y์ขŒํ‘œ์— "*"๊ฐ€ ์žˆ๋„๋ก ์ƒˆ๋กœ ๊ทธ๋ฆฌ๊ธฐ.
    }
    public void mouseDragged(MouseEvent me){}
}
  • โ€ป ์œ„์˜ ์†Œ์Šค์ฝ”๋“œ์— ์•„๋ž˜ ์ฝ”๋“œ๋ธ”๋Ÿญ์„ ์ถ”๊ฐ€ํ•˜๋ฉด ๋งˆ์šฐ์Šคํฌ์ธํ„ฐ์˜ ์ž์ทจ๊ฐ€ ๋‚จ๊ฒŒ ๋œ๋‹ค.
    • update()๋Š” ์›๋ž˜ ์ปดํฌ๋„ŒํŠธ์˜์—ญ์„ ์ง€์šฐ๊ณ (๋ฐฐ๊ฒฝ์ƒ‰์œผ๋กœ ์ฑ„์šฐ๊ณ ) paint()๋ฅผ ํ˜ธ์ถœํ•˜๋Š”๋ฐ,
    • ์•„๋ž˜ ์ฝ”๋“œ๋ธ”๋Ÿญ์œผ๋กœ update()๋ฅผ ์žฌ์ •์˜ํ•ด ์ปดํฌ๋„ŒํŠธ์˜์—ญ์„ ์ง€์šฐ์ง€ ์•Š๊ณ  paint()๋งŒ ํ˜ธ์ถœํ•˜๋„๋ก ์˜ค๋ฒ„๋ผ์ด๋”ฉํ–ˆ๊ธฐ ๋•Œ๋ฌธ.
/* 
    public void update(Graphics g){
        paint(g);
    }
    */

Image๋ฅผ ์ด์šฉํ•ด์„œ ์ด๋ฏธ์ง€ ์ถœ๋ ฅํ•˜๊ธฐ

  • Image ํด๋ž˜์Šค

    • gif, jpg์™€ ๊ฐ™์€ ํ˜•์‹์˜ ์ด๋ฏธ์ง€ ํŒŒ์ผ์„ ๋ณด์—ฌ์ฃผ๊ฑฐ๋‚˜ ๊ฐ€์ƒํ™”๋ฉด(off-screen)์„ ์ƒ์„ฑํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋œ๋‹ค.
  • GUIํ”„๋กœ๊ทธ๋žจ์—์„œ ์ด๋ฏธ์ง€ ํŒŒ์ผ์„ ๋ณด์—ฌ์ฃผ๋ ค๋ฉด Toolkit, .getImage(File img), .drawImage() ๋“ฑ์„ ํ™œ์šฉํ•œ๋‹ค.

    • drawImage(Image img, int x, int y, ImageObserver obj)
      • Graphics๊ฐ€ ์ œ๊ณตํ•˜๋Š” ๋ฉ”์„œ๋“œ๋กœ, ์ด๋ฏธ์ง€(img)๋ฅผ ์ง€์ •๋œ ์œ„์น˜(x,y)์— ์ถœ๋ ฅํ•œ๋‹ค.
      • ImageObserver obj
        • ์ด๋ฏธ์ง€๊ฐ€ ๋กœ๋”ฉ๋˜๋Š” ๋ฐ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฌ๊ธฐ ๋–„๋ฌธ์—, ๋กœ๋”ฉ์ด ์ง„ํ–‰๋˜๋Š” ์ƒํƒœ์— ๋”ฐ๋ผ ํ™”๋ฉด์— ๋‹ค์‹œ ๊ทธ๋ ค์ค˜์•ผ ํ•œ๋‹ค.
        • "์ด๋ฏธ์ง€๊ฐ€ ๋กœ๋”ฉ๋˜๊ณ  ์žˆ๋Š” ์ƒํƒœ๋ฅผ ์•Œ๋ ค์ค„ ๋Œ€์ƒ"์„ ImageObserver๋กœ ์ง€์ •ํ•œ๋‹ค.
        • ๋ณดํ†ต์€ ์ปดํฌ๋„ŒํŠธ ์ž์‹ (this)์„ ImageObserver๋กœ ์ง€์ •ํ•œ๋‹ค.
          • ์™œ? ๋ชจ๋“  AWT์ปดํฌ๋„ŒํŠธ๋Š” ImageObserver์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ–ˆ๊ธฐ ๋–„๋ฌธ์ด๋‹ค.
  • GUIํ”„๋กœ๊ทธ๋žจ์—์„œ ๊ทธ๋ฆผ์„ ๊ทธ๋ฆด ๋•Œ๋Š” ๋ณดํ†ต..

    • ์‹ค์ œ ํ™”๋ฉด์— ์ง์ ‘ ๊ทธ๋ฆผ์„ ๊ทธ๋ฆฌ๋Š” ๋Œ€์‹ ,
    • ๊ฐ€์ƒํ™”๋ฉด์— ๊ทธ๋ฆผ์„ ๊ทธ๋ฆฐ ๋‹ค์Œ → ์‹ค์ œํ™”๋ฉด์œผ๋กœ ๋ณต์‚ฌํ•ด ๋ณด์—ฌ์ค€๋‹ค.
    • ์™œ? ์‹ค์ œํ™”๋ฉด์— ์ง์ ‘ ๊ทธ๋ฆผ์„ ๊ทธ๋ฆฌ๋ฉด ๊นœ๋นก๊ฑฐ๋ฆผ์ด ๋ฐœ์ƒํ•˜๊ณ , ์‹คํ–‰ ์‹œ ๊ทธ๋ฆฐ ๊ทธ๋ฆผ์ด ๋ณด์กด๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ.

Image๋กœ ๊ฐ€์ƒํ™”๋ฉด ์‚ฌ์šฉํ•˜๊ธฐ ์˜ˆ์ œ - ๋งˆ์šฐ์Šค๋กœ ๊ทธ๋ฆผ ๊ทธ๋ฆฌ๊ธฐ

๋งˆ์šฐ์Šค๋กœ ๊ทธ๋ฆผ ๊ทธ๋ฆฌ๊ธฐ (1) - drawString()์œผ๋กœ ๊ทธ๋ฆฌ๊ธฐ
  • Image(img)์™€ Image์˜ Graphics(gImg)๋ฅผ ๋ฉค๋ฒ„๋ณ€์ˆ˜๋กœ ์ •์˜
  • ์ƒ์„ฑ์ž์—์„œ createImage()๋ฅผ ํ†ตํ•ด Frame๊ณผ ๊ฐ™์€ ํฌ๊ธฐ์˜ Image๋ฅผ ์ƒ์„ฑํ•ด img์— ์ €์žฅ.
  • ์ƒ์„ฑ๋œ Image์—์„œ getGraphics()๋ฅผ ํ†ตํ•ด Image์— ๋Œ€ํ•œ Graphics๋ฅผ ์–ป๋Š”๋‹ค.
    • ์ด๋ ‡๊ฒŒ ์–ป์–ด์ง„ Graphics์— drawString()๊ณผ ๊ฐ™์€ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด ์ž‘์—….
    • ์ž‘์—…ํ•œ ๋‚ด์šฉ์€ Image์— ๊ทธ๋ ค์ง€๊ฒŒ ๋œ๋‹ค.
  • paint()์—์„œ๋Š” ๋‹จ์ˆœํžˆ ๊ฐ€์ƒํ™”๋ฉด(Image img)์— ๊ทธ๋ ค์ง„ ๋‚ด์šฉ์„ Frame์— ๋ณต์‚ฌํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค.
  • int getModifiers() : ์ด๋ฒคํŠธ ๋ฐœ์ƒ ์‹œ ๋ˆ„๋ฅด๊ณ  ์žˆ๋˜ ํŠน์ˆ˜ํ‚ค๋ฅผ ๋ฐ˜ํ™˜.
import java.awt.*;
import java.awt.event.*;

public class GraphicsEx4 extends Frame implements MouseMotionListener{
    // Frameํด๋ž˜์Šค ์ƒ์†๋ฐ›๊ณ  MouseMotionListener๊ตฌํ˜„.
    int x = 0;
    int y = 0;

    Image img = null; // Image๊ฐ์ฒด ์„ ์–ธ
    Graphics gImg = null; //

    public static void main(String[] args) {
        new GraphicsEx4("GraphicsEx4");
    }

    public GraphicsEx4(String title) { // ์ƒ์„ฑ์ž
        super(title); // ๋ถ€๋ชจํด๋ž˜์Šค Frame์˜ ์ƒ์„ฑ์ž ํ˜ธ์ถœ
        addMouseMotionListener(this);//GraphicsEx2 ๊ฐ์ฒด์— MouseMotionListener ์—ฐ๊ฒฐ
        addWindowListener(new WindowAdapter() { // ์ข…๋ฃŒ๊ธฐ๋Šฅ - Adapterํด๋ž˜์Šค ํ™œ์šฉ
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        setBounds(100, 100, 500, 500);
        setVisible(true);

        img = createImage(500, 500); //createImage()๋กœ Image๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ
        gImg = img.getGraphics(); //getGraphics()๋ฅผ ํ†ตํ•ด Image๊ฐ์ฒด์˜ Graphics๊ฐ์ฒด๋ฅผ ์–ป์Œ
        gImg.drawString("์™ผ์ชฝ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅธ ์ฑ„๋กœ ๋งˆ์šฐ์Šค๋ฅผ ์›€์ง์—ฌ ๋ณด์„ธ์š”.", 10, 50);
        repaint();
    }

    public void paint(Graphics g) {
        if(img != null) {
            g.drawImage(img,0,0,this);
            // ๊ฐ€์ƒํ™”๋ฉด์— ๊ทธ๋ ค์ง„ ๊ทธ๋ฆผ์„ Frame์— ๋ณต์‚ฌ
        }
    }

    // MouseMotionListener ์ด๋ฒคํŠธ์ฒ˜๋ฆฌ ๋ฉ”์„œ๋“œ
    public void mouseMoved(MouseEvent me) {}
    // ๋งˆ์šฐ์Šค๋ฅผ ํด๋ฆญํ•ด์„œ ์›€์ง์ผ(=๋“œ๋ž˜๊ทธ) ๋•Œ๋งŒ ๊ทธ๋ ค์ง€๋ฏ€๋กœ mouseDragged()์— ์ด๋ฒคํŠธ์ฒ˜๋ฆฌ๋ฅผ ํ•ด ์ค€๋‹ค.
    public void mouseDragged(MouseEvent me){
        if (me.getModifiersEx() == MouseEvent.BUTTON1_DOWN_MASK) {
            // MouseEvent๋ฐœ์ƒ ์‹œ ๋ˆ„๋ฅด๊ณ  ์žˆ๋˜ ํ‚ค ๊ฐ’ == ๋งˆ์šฐ์Šค ์™ผ์ชฝ ๋ฒ„ํŠผ ์ด๋ฉด.
            x = me.getX();
            y = me.getY();
            gImg.drawString("*", x, y);
            repaint();
        }
    }
} // class GraphicsEx4
๋งˆ์šฐ์Šค๋กœ ๊ทธ๋ฆผ ๊ทธ๋ฆฌ๊ธฐ (2) - drawLine()์œผ๋กœ ๊ทธ๋ฆฌ๊ธฐ
  • ์œ„ ์˜ˆ์ œ์˜ drawString() ๋Œ€์‹  drawLine()์„ ์‚ฌ์šฉํ•ด ๋” ์ž์œ ๋กญ๊ฒŒ ์„ ์„ ๊ทธ๋ฆด ์ˆ˜ ์žˆ๋‹ค.
  • ์ฐจ์ด์  : paint(), mouseMoved(), mouseDragged() ๋ถ€๋ถ„์ด ๋‹ค๋ฅด๋‹ค.
    • โ“ ์™œ ๋‹ค๋ฅผ๊นŒ?..
import java.awt.*;
import java.awt.event.*;

public class GraphicsEx5 extends Frame implements MouseMotionListener {
    // Frameํด๋ž˜์Šค ์ƒ์†๋ฐ›๊ณ  MouseMotionListener๊ตฌํ˜„.
    int x = 0;
    int y = 0;

    Image img = null; // Image๊ฐ์ฒด ์„ ์–ธ(๊ฐ€์ƒํ™”๋ฉด)
    Graphics gImg = null; // img์˜ Graphics๊ฐ์ฒด๋ฅผ ๋‹ด์„ ๋ณ€์ˆ˜ ์„ ์–ธ 

    public static void main(String[] args) {
        new GraphicsEx5("GraphicsEx5");
    }

    public GraphicsEx5(String title) { // ์ƒ์„ฑ์ž
        super(title); // ๋ถ€๋ชจํด๋ž˜์Šค Frame์˜ ์ƒ์„ฑ์ž ํ˜ธ์ถœ
        addMouseMotionListener(this);//GraphicsEx2 ๊ฐ์ฒด์— MouseMotionListener ์—ฐ๊ฒฐ
        addWindowListener(new WindowAdapter() { // ์ข…๋ฃŒ๊ธฐ๋Šฅ - Adapterํด๋ž˜์Šค ํ™œ์šฉ
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        setBounds(100, 100, 500, 500);
        setVisible(true);

        img = createImage(500, 500); // (๊ฐ€์ƒํ™”๋ฉด) createImage()๋กœ Image๊ฐ์ฒด ์ƒ์„ฑ
        gImg = img.getGraphics(); //getGraphics()๋ฅผ ํ†ตํ•ด Image๊ฐ์ฒด์˜ Graphics๊ฐ์ฒด๋ฅผ ์–ป์Œ
        gImg.drawString("์™ผ์ชฝ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅธ ์ฑ„๋กœ ๋งˆ์šฐ์Šค๋ฅผ ์›€์ง์—ฌ ๋ณด์„ธ์š”.", 10, 50);
        repaint();
    }

    public void paint(Graphics g) {
        if (img == null) return; // ๊ฐ€์ƒํ™”๋ฉด์— ๊ทธ๋ ค์ง„ ๊ฒŒ ์—†์œผ๋ฉด paint() ์ข…๋ฃŒ
        g.drawImage(img, 0, 0, this);
        // ๊ฐ€์ƒํ™”๋ฉด์— ๊ทธ๋ ค์ง„ ๊ทธ๋ฆผ์„ Frame์— ๋ณต์‚ฌ

    }

    // MouseMotionListener ์ด๋ฒคํŠธ์ฒ˜๋ฆฌ ๋ฉ”์„œ๋“œ
    public void mouseMoved(MouseEvent me) {
        x = me.getX(); //MouseEvent ๋ฐœ์ƒํ–ˆ์„ ๋•Œ, ๋งˆ์šฐ์Šคํฌ์ธํ„ฐ์˜ x์ขŒํ‘œ
        y = me.getY();
    }

    public void mouseDragged(MouseEvent me) {
        if (me.getModifiersEx() != MouseEvent.BUTTON1_DOWN_MASK) return;
        // MouseEvent๋ฐœ์ƒํ–ˆ์„ ๋•Œ ๋ˆ„๋ฅด๊ณ  ์žˆ๋˜ ํ‚ค ๊ฐ’์ด ๋งˆ์šฐ์Šค ์™ผ์ชฝ ๋ฒ„ํŠผ์ด ์•„๋‹ˆ๋ฉด ์ข…๋ฃŒ.
        gImg.drawLine(x, y, me.getX(), me.getY());
        x = me.getX();
        y = me.getY();

        repaint();
    }
} // class GraphicsEx5

์ด๋ฏธ์ง€ ์‚ฝ์ž…ํ•˜๊ธฐ ์˜ˆ์ œ

  • ์ด๋ฏธ์ง€(img)๋ฅผ ํ™”๋ฉด ์ค‘์•™์— ๋ณด์—ฌ์ฃผ๋Š” ์˜ˆ์ œ.
  • ํŒŒ์ผ๋ช…์€ ๊ฒฝ๋กœ๋ฅผ ํ•จ๊ป˜ ์ง€์ •ํ•ด์ฃผ๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.
    • ์ด๋ฏธ์ง€ํŒŒ์ผ์ด ์˜ˆ์ œ์˜ ํด๋ž˜์ŠคํŒŒ์ผ๊ณผ ๊ฐ™์€ ๊ฒฝ๋กœ์— ์žˆ๋Š” ๊ฒฝ์šฐ, ๊ฒฝ๋กœ๋ช…์€ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ๋‹ค.
import java.awt.*;
import java.awt.event.*;

class GraphicsEx6 extends Frame {
    Image img = null;

    public GraphicsEx6(String title) {//์ƒ์„ฑ์ž
        super(title);
        addWindowListener(new WindowAdapter() {// ์ด๋ฒคํŠธ - ์ฐฝ๋‹ซ๊ธฐ ๋ฒ„ํŠผ
            public void windowClosing(WindowEvent we) {
                System.exit(0);
            }
        });

        Toolkit tk = Toolkit.getDefaultToolkit();
        img = tk.getImage("sampleBibiImg.jpg");

        setBounds(100, 100, 400, 300);
        setVisible(true);
    }

    public static void main(String[] args) {
        new GraphicsEx6("GraphicsEx6");
    }

    public void paint(Graphics g) {
        if (img == null) return;

        // Image์˜ ํฌ๊ธฐ๋ฅผ ์–ป๋Š”๋‹ค
        int imgWidth = img.getWidth(this); // getWidth(ImageObserver obj)
        int imgHeight = img.getHeight(this); // getHeight(ImageObserver obj)

        //์ด๋ฏธ์ง€๋ฅผ  Frame์˜ ์ค‘์•™์— ์ถœ๋ ฅํ•œ๋‹ค
        g.drawImage(img, (getWidth() - imgWidth) / 2,
                (getHeight() - imgHeight) / 2, this);
    }
} // class