疯狂Java实战演义


/Download at http://www.pin5i.com ಴ℸা䳔㽕Փ⫼ System.out.println ᴹ䖯㸠ᠧॄेৃˈབᵰ䳔㽕ᅲ⦄᳝⬠䴶ⱘѨᄤẟ␌៣ˈ՟བՓ⫼ ˄Āकā˅ˈ⥽ᆊϟẟৢˈᇚẟᄤ᳓ᤶॳᴹⱘᷛᖫˈ᳔ৢݡᠻ㸠䕧ܹDŽ⬅Ѣᴀゴᰃ೼᥻ࠊৄЁ䖯㸠ᠧॄˈ ⴔẟⲬᓣⱘᷛᖫܙ฿ˈ⥽ᆊ᠔ϟⱘതᷛˈݡএᇚẟᄤᬒ㕂ࠄẟⲬЁDŽ៥ӀৃҹᇚẟⲬⳟ԰ϔϾѠ㓈᭄㒘 Āƽāҷ㸼咥ᄤˈ Āƻāҷ㸼ⱑᄤDŽᔧ⥽ᆊҹ˄ x,y˅ⱘᔶᓣ䕧ܹϟẟⱘതᷛৢˈ␌៣Ёህৃҹḍ᥂ ㋏㒳䆶䯂⥽ᆊᰃ৺㒻㓁␌៣ᯊˈ⥽ᆊ䕧ܹ y ᰃҷ㸼㒻㓁ˈ䕧ܹ݊ᅗ߭ҷ㸼䗔ߎ␌៣DŽ ᰃ 1 ࠄ N˄ẟⲬⱘ໻ᇣ˅ⱘℷ᭄DŽ ⥽ᆊᖙ乏ҹ˄x,y˅ⱘᮍᓣ䕧ܹẟⲬⱘതᷛˈ݊Ёˈ x ҷ㸼ẟതᷛˈy ҷ㸼オതᷛDŽx Ϣ y ⱘؐᖙ乏 1.1.2 䕧ܹ䕧ߎ㑺ᅮ ᴹ䖯㸠␌៣DŽ ԡ㕂DŽᴀゴЏ㽕Ⳃⱘᰃሩ⦄Ѩᄤẟⱘᅲ⦄ॳ⧚ˈབᵰ䇏㗙᳝݈䍷ˈৃҹ㞾㸠ᓔথᔎ໻ⱘĀҎᎹᱎ㛑⬉㛥ā ⿟ᑣᴹᅲ⦄ˈᔧ៥Ӏϟẟⱘᯊ׭ˈ䖭ϾĀ⬉㛥āህᇍ៥Ӏ᠔ϟⱘẟᄤ䖯㸠Ẕ⌟ˈᑊᇚẟᄤϟࠄ᳔ᙄᔧⱘ ᳈ᔎ໻ⱘĀ⬉㛥ā៥Ӏৃҹ㓪ݭخẟৢˈ䖭ϾㅔऩⱘĀ⬉㛥āህ䱣֓೼ẟⲬЁϟϔϾẟˈᔧ✊ˈབᵰᛇ ៥Ӏⱘᇍ᠟ˈᔧ៥Ӏϟᅠخࠄᇍ៬ᔶᓣⱘˈ៥Ӏৃҹ䆒䅵ϔϾㅔऩⱘĀ⬉㛥āᴹخゴЁˈ៥Ӏᑊϡ䳔㽕 Ѩᄤẟᖙ乏⬅ঠᮍ䖯㸠␌៣ˈᔧᶤϔᮍᣝ✻ϔᅮ㾘߭䖲៤ѨϾẟᄤⱘᯊ׭ˈ䆹␌៣ᮍህ㚰߽ˈ೼ᴀ ᔶ៤Ѩ䖲ᄤⱘϔᮍЎ䌶DŽܜ‰ 佪 ‰ ೼ẟⲬϞҹᇍሔঠᮍഛϡৃ㛑ᔶ៤Ѩ䖲Ў੠ẟDŽ ‰ Ѩᄤẟϧ⫼ⲬЎ 15×15 ˈѨ䖲ᄤⱘᮍ৥Ў῾ǃオǃ᭰DŽ ϟ˅DŽܜⱘ␌៣ˈ咥ẟϢⱑẟⱘ㾘߭ϔḋˈԚϔḋ⬅咥ẟ 㸠˄ᴀゴ㡖䆒䅵ܜ‰ ѨᄤẟᰃϸϾҎП䯈䖯㸠ⱘゲᡔ⌏ࡼˈ⬅Ѣᇍ咥ᮍⱑᮍ㾘߭ϡৠˈ咥ẟᖙ乏 ␌៣ˈѨᄤẟⱘ⥽⊩Ϣ㾘߭བϟ˖ Ⳃāǃ ĀѨⳂ⺄āǃĀѨḐāㄝ໮⾡⿄䇧DŽѨᄤẟ␌៣ᰃϔϾ↨䕗໻ӫⱘẟ㉏␌៣ˈ໻໮᭄Ҏ䛑Ӯ⥽䖭Ͼ 㣅᭛⿄ПЎ”Gobang”៪”FIR”˄Five in a Row ⱘ㓽ݭ˅ ˈѺ᳝Ā䖲Ѩᄤā ǃĀѨᄤ䖲āǃ ĀІ⦴ā ǃĀѨ Ѩᄤẟᰃ䍋⑤ѢЁ೑সҷⱘӴ㒳咥ⱑẟ⾡ПϔDŽ⦄ҷѨᄤẟ᮹᭛⿄ПЎĀ䖲⦴āˈ㣅䆥Ў ”Renju”ˈ 1.1.1 Ѩᄤẟҟ㒡 ೼ᴀゴЁᇚՓ⫼᳔ㅔऩⱘᮍᓣᴹᅲ⦄ϔϾ᥻ࠊৄѨᄤẟ␌៣DŽ ℸˈᥠᦵᴀゴѨᄤẟⱘᅲ⦄ॳ⧚ˈᇍѢᄺдҹৢⱘゴ㡖ᇚӮ䴲ᐌ᳝ᐂࡽDŽ԰Ўᴀкⱘ㄀ϔゴݙᆍˈ៥Ӏ ᑣгӮবᕫ᳈ࡴᆍᯧˈ᳈ࡴ䱣ᖗᑨ᠟DŽ㗠ᴀゴⱘЏ㽕Ⳃⱘ䅽䇏㗙ᥠᦵϢ⧚㾷 Java 㓪⿟ⱘ෎⸔ⶹ䆚ˈ಴ ᦵ෎⸔ⱘⶹ䆚ˈ֓᳈ᆍᯧᄺд awt,swing ㄝ೒ᔶ⫼᠋⬠䴶ⱘ㓪ݭˈϛবϡ⾏݊ᅫˈݭ䍋᳝᪡԰⬠䴶ⱘ⿟ ϞˈϔϾ⿟ᑣ᳔䞡㽕ⱘᑊϡᰃ⬠䴶ˈ㗠ᰃ໘⧚৘⾡Ϯࡵ䘏䕥Ϣ᭄᥂ⱘᮍ⊩ˈা㽕ᥠᦵњḌᖗⱘᮍ⊩ˈᥠ 咥㡆᥻ࠊৄ⦃๗ϟ䖯㸠␌៣ˈ␌៣ⱘৃ⥽ᗻᑊϡ催ˈԐТ䖭ᑊϡᰃϔϾᅠᭈⱘ␌៣DŽ㱑✊བℸˈԚџᅲ ⬅Ѣᰃ೼᥻ࠊৄϟ䴶䖤㸠ⱘ⿟ᑣˈ᠔ҹᑊ≵᳝ⓖ҂ⱘ␌៣⬠䴶ˈϢঞ哴ᷛ᪡԰ㄝϰ㽓ˈাᰃ೼ϔ⠛ 㕂ᴹ䖯㸠␌៣DŽ ᥻ࠊৄѨᄤẟˈ乒ৡᗱНˈህᰃ೼ Java ᥻ࠊৄ䖤㸠ⱘѨᄤẟ␌៣ˈ䳔㽕⫼᠋⫼䬂Ⲭ䕧ܹẟᄤⱘԡ 1.1 ᓩ㿔 ㄀  ゴ᥻ࠊৄѨᄤẟ /Download at http://www.pin5i.com ᄬ೼њⳌᑨⱘẟᄤˈབᵰᄬ೼ⱘ䆱ˈ䳔㽕ݡ⃵ᦤ⼎⥽ᆊˈ䞡ᮄ䕧ܹDŽ ㋏㒳䇏পњ⥽ᆊ䕧ܹⱘതᷛৢˈ䰸њ߸ᮁ␌៣ᰃ৺㚰߽໪ˈ䖬䳔㽕߸ᮁ⥽ᆊ䕧ܹⱘതᷛЁᰃ৺Ꮖ㒣 ߭㋏㒳䆶䯂ᰃ৺㒻㓁␌៣DŽ 䕧ܹⳌᑨⱘതᷛˈ㋏㒳䇏প⥽ᆊ᠔䕧ܹⱘതᷛᑊ䖯㸠Ⳍᑨⱘߚᵤˈབᵰ⥽ᆊ᠔ϟⱘẟՓᕫ⥽ᆊ␌៣㚰߽ˈ ␌៣ᓔྟˈ㋏㒳೼᥻ࠊৄЁᠧॄߎẟⲬˈ⥽ᆊḍ᥂䖭ϾẟⲬˈ䗝ᅮϟẟⱘԡ㕂തᷛৢˈ೼᥻ࠊৄЁ 1.2.1 ⥽ᆊ䕧ܹതᷛ ೒ 1.1 Ѩᄤẟ␌៣⌕⿟ 䖛⿟Ёৃҹ⏙᱄ⱘᥠᦵ⿟ᑣ㒧ᵘˈᇍѢᅲ⦄ࡳ㛑᳝㥿໻ⱘᐂࡽˈѨᄤẟⱘ݋ԧ⌕⿟བ೒ 1.1 ᠔⼎DŽ њ㾷ϔϟ␌៣ⱘᭈϾ␌៣⌕⿟ˈњ㾷␌៣ⱘ⌕⿟ˈ᳝ࡽѢ៥Ӏ೼ᓔথⱘܜ೼ᓔথѨᄤẟПࠡˈ៥Ӏ 1.2 њ㾷␌៣⌕⿟ᦣ䗄 ៪㗙݊Ҫ⬠䴶ᡔᴃˈѨᄤẟⱘᅲ⦄ॳ⧚޴Т໻ৠᇣᓖDŽ swing ៪㗙 awtˈৃҹՓ⫼Ⳍᑨⱘᮍ⊩ˈᇚѠ㓈᭄㒘Ā⬏āࠄ⬠䴶ЁDŽ಴ℸˈϡㅵᰃՓ⫼ swingǃawt /Download at http://www.pin5i.com ೒ 1.2 Ѩᄤẟ㉏೒ ݇㋏DŽϟ䴶ϔϔҟ㒡DŽ gobangGame ⱘᬍবˈӮᕅડࠄ Chessboard ⢊ᗕⱘᬍবˈ㗠 Chessman Ϣ GobangGame ᰃϔϾ㘮ড় ˄Chessman˅˄ᵮВ㉏˅ˈ㉏ⱘ݇㋏བ೒ 1.2 ᠔⼎ˈҢ೒ЁৃҹⳟߎˈChessboard ձ䌪Ѣ GobangGameˈ 䖭䞠䆒䅵ϝϾ㉏ᴹᅠ៤␌៣ⱘࡳ㛑ˈẟⲬ㉏˄Chessboard˅ˈ␌៣㉏˄GobangGame˅Ϣẟᄤ㉏ 1.3 ߯ᓎ␌៣ⱘ৘Ͼᇍ䈵 ೼ᴀゴ៥Ӏᑊϡ䳔㽕ᅲ⦄ᔎ໻ⱘҎᎹᱎ㛑Ā⬉㛥āˈা䳔㽕ㅔऩⱘ䱣ᴎѻ⫳തᷛेৃDŽ 㒣㚰߽њDŽ Ⳍ䖲˅ᴹ߸ᮁĀ⬉㛥ā᠔ϟⱘẟᄤᰃ৺Ꮖ㒣Փᕫ␌៣㚰߽ˈབᵰ␌៣㚰߽ˈৠḋഄএᦤ⼎⥽ᆊˈ⬉㛥Ꮖ ࠄѻ⫳ⱘതᷛϞ≵᳝ᄬ೼ẟᄤЎℶDŽᔧĀ⬉㛥āϟᅠẟৢˈህৃҹՓ⫼ৠḋⱘ߸ᮁᮍᓣ˄߸ᮁᰃ৺Ѩᄤ ೼ⳌᑨⱘതᷛЁϟẟDŽབᵰ⬉㛥䱣ᴎѻ⫳ⱘതᷛЁᏆ㒣ᄬ೼ẟᄤˈ䙷М៥Ӏৃҹ䞡ᮄ䱣ᴎѻ⫳തᷛˈⳈ Ꮖ㒣䆆ࠄˈᴀゴৃҹᅲ⦄ϔϾㅔऩⱘ⬉㛥ᴹ䖯㸠␌៣ˈা䳔㽕䱣֓ⱘѻ⫳ẟⲬതᷛˈህৃҹ䅽Ā⬉㛥ā ⥽ᆊ䕧ܹњതᷛˈ㋏㒳߸ᮁ⥽ᆊ≵᳝␌៣㚰߽ৢˈህᑨ䆹䕂ࠄĀ⬉㛥āϟẟˈ೼ᴀゴⱘᓔ༈Ё៥Ӏ Ā⬉㛥āϟẟ 1.2.2 /Download at http://www.pin5i.com Chessman.BLACK.getChessman(); ҷⷕ⏙ऩ˖code\gobang\src\org\crazyit\gobang\Chessman.java བᵰ៥Ӏ䳔㽕ᕫࠄẟᄤⱘᄫヺІ˄Āƽā៪㗙Āƻā˅ˈৃҹՓ⫼ҹϟⱘҷⷕDŽ ‰ String getChessman()ˈ䖨ಲ String ㉏ൟⱘẟᄤᅲ՟ˈĀƽā៪㗙ĀƻāDŽ Ͼሲᗻ䇗⫼ҹϟⱘᮍ⊩㦋পẟᄤ˖ Ͼ䴭ᗕሲᗻˈҷ㸼咥ᄤϢⱑᄤᵮВ㉏ˈϸϾ㸼ᗕሲᗻ䛑ᰃ Chessman ㉏ൟⱘˈ㽕㦋পẟᄤˈ߭䗮䖛䖭ϸ Chessman ㉏ᰃϔϾᵮВ㉏ˈℸ㉏ᰃᵘ䗴఼⾕᳝ⱘˈϡ㛑Ⳉ᥹߯ᓎˈ䞠䴶᳝ BLACK Ϣ WHITE ϸ 1.3.2 Chessman㉏ ਞ䆝 Chessboard ⥽ᆊ៪㗙Ā⬉㛥ā೼ᶤϾԡ㕂ϟњᗢḋⱘẟᄤDŽ ᔧ໪䚼䇗⫼ Chessboard ⱘ setBoard ᮍ⊩ᯊˈህৃҹᇚᶤϾؐ䆒㕂ࠄ Chessboard ЁⱘѠ㓈᭄㒘䞠ˈ ֱ᳝ᄬϢ䆹ᇍ䈵Ⳍ݇ⱘ⢊ᗕሲᗻˈ䙷М៥Ӏৃҹᇚ䖭Ͼᇍ䈵ⳟ៤ϔϾ᮴⢊ᗕⱘ Java ᇍ䈵DŽ ᇍ䈵ˈ᳝⢊ᗕⱘ Java ᇍ䈵ৃҹ⧚㾷៤ϔϾ Java ᇍ䈵ֱᄬϔѯϢ䆹ᇍ䈵Ⳍ݇ⱘ⢊ᗕሲᗻˈབᵰ䆹ᇍ䈵≵ printBoard ᮍ⊩ⱘᯊ׭ˈᇚᇍ䈵ݙⱘѠ㓈᭄㒘ᠧॄˈ៥Ӏৃҹᇚ Chessboard ⳟ԰ϔϾ᳝⢊ᗕⱘ Java Ѣ printBoard ᮍ⊩ᰃ≵᳝খ᭄ⱘˈ಴ℸ៥Ӏ䳔㽕Ў Chessboard ᦤկϔϾѠ㓈᭄㒘ব䞣ˈᔧ䇗⫼ ॄࠄ᥻ࠊৄЁˈ಴ℸ䆹ᮍ⊩ৃҹㅔऩⱘ䇗⫼ System.out.print এᠧॄⳌ݇ⱘᄫヺІDŽ䳔㽕⊼ᛣⱘᰃˈ⬅ Chessboard ЁᦤկњϔϾ printBoard ⱘᮍ⊩⫼ѢᠧॄẟⲬˈ೼ᴀゴЁˈ៥Ӏህ䳔㽕ᇚẟⲬ᭄㒘ᠧ ϡⶹབԩ䆒䅵ሲᗻ៪㗙㸠Ўˈৃҹᇚ݊䆒䅵៤᥹ষ៪㗙ᢑ䈵㉏DŽˈޚ⠽ᣓᤣϡ Ӏ䛑ৃҹᇚ݊ᢑ䈵៤݋ԧⱘᶤϾᇍ䈵ˈᑊ䌟ќϔᅮⱘሲᗻϢ㸠ЎDŽ೼䆒䅵ᇍ䈵ⱘ䖛⿟Ёˈབᵰ᳝ᶤѯџ Chessboard ㉏ᯊˈ៥Ӏህ䳔㽕থ᣹䴶৥ᇍ䈵ⱘᗱ㓈ˈ೼៥Ӏⱘ⿟ᑣЁˈ᠔᳝ⳟࠄⱘ៪㗙ᛇⱘџ⠽ˈ៥ ㋴ؐDŽ೼߯ᓎܗ㋴䖯㸠䌟ؐDŽ೼␌៣Ёབᵰ៥Ӏ䖯㸠њϟẟⱘ᪡԰ˈৃҹⳈ᥹ᬍব䖭Ͼ᭄㒘ⱘᶤϔϾ ܗҹϞҷⷕ߯ᓎϔϾ೎ᅮ໻ᇣ˄ϔ㓈ϢѠ㓈໻ᇣ˅ⱘѠ㓈᭄㒘ˈݡ䗮䖛ጠ༫ᕾ⦃Ў᭄㒘Ёⱘ↣ϔϾ } } array[i][j] = new Object(); for (int j = 0; j < array[i].length; j++) { for (int i = 0; i < array.length; i++) { Object[][] array = new Object[size][size]; ҷⷕ⏙ऩ˖code\gobang\src\org\crazyit\gobang\Chessboard.java এ߯ᓎϔϾѠ㓈᭄㒘ˈ߯ᓎѠ㓈᭄㒘ৃҹՓ⫼ҹϟҷⷕDŽ ⊩䳔㽕ᅲ⦄ⱘࡳ㛑˖߱ྟ࣪ẟⲬDŽ⬅Ѣ៥ӀᇚẟⲬⳟ԰ᰃϔϾѠ㓈᭄㒘ˈ಴ℸ initBoard ህ䳔㽕ᐂ៥Ӏ ᔧ៥Ӏ䳔㽕߱ྟ࣪ẟⲬⱘᯊ׭ˈৃҹⳈ᥹䇗⫼ Chessboard ⱘ initBoard ᮍ⊩ˈ៥Ӏ䳔㽕㗗㰥䆹ᮍ ‰ String[][] getBoard()ˈ䖨ಲẟⲬˈ䖨ಲ㉏ൟᰃֱᄬẟⲬⱘѠ㓈᭄㒘DŽ ᄤ䆒㕂ࠄẟⲬϞDŽ ᷛ,ˈchessman ᰃᮄϟẟᄤⱘ㉏ൟ˄咥ᄤϢⱑᄤ˅ˈ↣ϟᅠϔ乫ẟᄤৢˈ䗮䖛䇗⫼ℸᮍ⊩ᡞẟ ‰ void setBoard( int posX , int posY , String chessman )ˈposX Ϣ posY ᰃᮄϟẟᄤⱘ x Ϣ y ത ⢊ᗕ᳝ᬍবˈ᠔ҹᖙ乏䇗⫼ℸᮍ⊩䞡ᮄ䕧ܹẟⲬDŽ ‰ void printBoard()ˈℸᮍ⊩⫼Ѣ೼᥻ࠊৄ䕧ߎẟⲬˈ৘ᮍ↣ϔᅠϔ乫ẟᄤৢˈ⬅ѢẟⲬϞẟᄤⱘ ϾᮄⱘぎẟⲬDŽ ‰ void initBoard()ˈ䖭Ͼᮍ⊩⫼Ѣ߱ྟ࣪ẟⲬˈᓔྟᮄⱘ␌៣ᯊˈᑨ䆹䇗⫼ℸᮍ⊩ˈ߱ྟ࣪ߎϔ 䖭Ͼ㉏ࣙ৿ҹϟᮍ⊩˖ 㽕䖯㸠Ѩᄤẟ␌៣ˈᖙ乏᳝᳝ϔϾẟⲬˈ㗠䖭Ͼ㉏Џ㽕᥻ࠊẟⲬⱘ߱ྟ࣪ˈ䕧ߎϢঞ๲ࡴᮄⱘẟᄤDŽ Chessboard㉏ 1.3.1 /Download at http://www.pin5i.com Ϣ㸼⦄䖭ϾẟⲬDŽټ ϔϾ᭄㒘ˈ䙷М N ߫ⱘẟⲬህᰃϔϾѠ㓈᭄㒘ˈ⫼᭄㒘㛑ᕜདⱘᄬخ㸼ϔϾẟⲬˈབᵰᡞẟⲬⱘϔ߫ᔧ ⱘᰃϔϾ BOARD_SIZE*BOARD_SIZE ⱘѠ㓈᭄㒘DŽ೒ 1.3 ᦣ䗄њЎҔМ䳔㽕Փ⫼ϔϾѠ㓈᭄㒘ᴹҷ ؐЎĀƽā៪㗙ĀƻāDŽ⫼ϔϾϡৃᬍবⱘᐌ䞣 BOARD_SIZE ᴹ㸼⼎ẟⲬⱘ໻ᇣˈ᠔ҹֱᄬ䖭ϾẟⲬ ԡ㕂(i ҷ㸼 x തᷛˈj ҷ㸼 y തᷛ)ˈབᵰℸԡ㕂≵᳝ẟᄤˈ咬䅸ؐЎĀकāˈབᵰ᳝ẟᄤˈboard [i][j]ⱘ ೼ℸ㉏ЁˈЏ㽕ᰃ⫼ϔϾ String[][]㉏ൟⱘѠ㓈᭄㒘 board এֱᄬẟⲬˈboard [i][j]ҷ㸼ẟⲬⱘᶤϾ 1.4 ẟⲬ㉏ᅲ⦄ Џ㽕Ⳃⱘᰃሩ⦄ϔϾ᳔ㅔऩⱘѨᄤẟˈ಴ℸᴀゴЁᑊϡ⍝ঞӏԩ݋ԧⱘ䆒䅵῵ᓣDŽ ⫼ㄪ⬹῵ᓣˈབᵰ䳔㽕ѻ⫳ߎϡৠⱘẟᄤ˄ᇚ᥻ࠊৄᤶ៤݊Ҫ⬠䴶˅ˈৃҹ㓪ݭẟᄤᎹॖㄝDŽԚᰃᴀゴ ࠄ᳈དⱘ⿟ᑣ㾷㗺ˈ៥ӀৃҹՓ⫼ϔѯ䆒䅵῵ᓣˈ՟བᇚ␌៣㾘߭ݭ៤ϔϾ݋ԧⱘㅫ⊩ˈৃҹՓخ䳔㽕 㒚ᦣ䗄њ৘Ͼᮍ⊩ⱘ԰⫼ˈ೼ϟ䴶ゴ㡖Ё៥Ӏᇚᓔྟᇍ䖭ϝϾᇍ䈵᠔ᅮНⱘᮍ⊩䖯㸠ᅲ⦄DŽᔧ✊ˈབᵰ ࠄℸˈ␌៣ЁⱘϝϾᇍ䈵Ꮖ㒣䆒䅵ᅠ៤ˈ䖭ϝϾᇍ䈵ЁᏆ㒣ᅮНདњ৘⾡ᮍ⊩ˈᑊ೼ࠡ䴶ゴ㡖Ё䆺 ẟৢ˄⥽ᆊϟẟ៪㗙Ā⬉㛥āϟẟ˅䇗⫼DŽ ᄤẟЁ᳔Џ㽕ⱘᮍ⊩ˈѨᄤẟᰃ৺ৃҹⳌ䖲ⱘ᠔᳝䘏䕥ˈ䛑Ӯ೼䆹ᮍ⊩Ёᅲ⦄DŽisWon ᮍ⊩Ӯ೼↣⃵ϟ ߸ᮁϔሔ␌៣ᰃ৺㚰߽ˈৃҹ䇗⫼ GobangGame ⱘ isWon ᮍ⊩ˈ䆹ᮍ⊩߸ᮁ␌៣ᰃ৺㚰߽ˈᰃѨ ᮍ⊩⫼Ѣ䱣ᴎѻ⫳തᷛDŽ ᔧ䕂ࠄĀ⬉㛥āϟẟⱘᯊ׭ˈ៥Ӏ䳔㽕䱣ᴎ⫳៤⬉㛥ⱘϟẟതᷛˈGobangGame Ёⱘ computerDo 䆕᥻ࠊৄ䕧ܹⱘֵᙃㄝˈ䖭ѯࡳ㛑៥Ӏᇚ೼ϟ䴶ⱘゴ㡖Ёࡴҹᦣ䗄DŽ 㽕߱ྟ࣪ẟⲬ˄䇗⫼ Chessboard ⱘ init ᮍ⊩˅ˈ䳔㽕ᓔྟҢ᥻ࠊৄ䇏প⥽ᆊⱘ䕧ֵܹᙃǃᠧॄẟⲬˈ偠 GobangGame ЁᦤկњϔϾ start ᮍ⊩ˈ⫼Ѣ␌៣ⱘᓔྟˈ៥Ӏ䳔㽕㗗㰥␌៣ᓔྟⱘ㸠Ўˈ՟བ䳔 ᮍ⊩䖨ಲ falseˈা᳝ᔧ䆹ᮍ⊩䖨ಲ true ⱘᯊ׭ˈᠡӮএׂᬍ Chessboard ⱘѠ㓈᭄㒘DŽ ᓣЎ˖x,yˈ៥Ӏ䳔㽕ᇍᄫヺІ䖯㸠໘⧚ᕫࠄx੠yⱘؐˈབᵰ⥽ᆊ䕧ܹⱘᄫヺІϡヺড়㋏㒳㽕∖ˈ߭isValid Ёⱘ isValid ᮍ⊩⫼Ѣ偠䆕᥻ࠊৄⱘ䕧ܹˈ⥽ᆊЏ㽕೼᥻ࠊৄ䕧ܹϟẟⱘതᷛˈϟẟⱘതᷛⱘᄫヺІᔶ GobangGame ᰃ៥ӀѨᄤẟ␌៣ⱘЏԧ㉏ˈ␌៣䞠䴶᠔᳝ⱘ໘⧚䛑೼䆹㉏Ёᅲ⦄DŽGobangGame ⱘ x Ϣ y തᷛˈico ҷ㸼ᮄϟⱘẟᄤ㉏ൟˈབᵰ䌶њˈ䖨ಲ trueˈ৺߭䖨ಲ falseDŽ ‰ boolean isWon( int posX , int posY , String ico )ˈ߸ᮁ䕧䌶ˈখ᭄ posX Ϣ posY ҷ㸼ᮄϟẟᄤ y തᷛⱘ int[]᭄㒘㉏ൟDŽ ‰ int[] computerDo()ˈ䅵ㅫᴎ䱣ᴎϟẟˈ⬅䅵ㅫᴎ㞾ࡼ䆒㕂ẟⲬˈᑊ䖨ಲࣙ৿ᮄϟẟᄤԡ㕂 x Ϣ খ᭄ chessman ҷ㸼咥ᄤ៪ⱑᄤDŽ ‰ boolean isReplay( String chessman )ˈᰃ৺䞡ᮄᓔྟ␌៣ˈབᵰᰃˈ䖨ಲ trueˈ৺߭䖨ಲ falseˈ ‰ void start()ˈᓔྟ␌៣DŽℸᮍ⊩ᡯߎ Exception ᓖᐌDŽ trueˈབᵰϡড়⊩ˈ߭䖨ಲ falseˈℸᮍ⊩ᡯߎ Exception ᓖᐌDŽ ‰ boolean isValid( String inputStr )ˈℸᮍ⊩偠䆕᥻ࠊৄⱘ䕧ܹᄫヺІᰃ৺ড়⊩ˈབᵰড়⊩ˈ䖨ಲ 䖭Ͼ㉏DŽ ‰ GobangGame( Chessboard chessboard )ˈ᳝খ᭄ᵘ䗴఼ˈҹϔϾ Chessboard ᅲ՟এ߱ྟ࣪ ‰ GobangGame()ˈ咬䅸᮴খ᭄ᵘ䗴఼DŽ ᑊᕅડ Chessboard ㉏DŽЏ㽕ࣙ৿ҹϟᵘ䗴఼Ϣᮍ⊩˖ GobangGame ㉏ᰃ䖯㸠␌៣ⱘ㉏ˈChessboard ձ䌪Ѣℸ㉏ˈℸ㉏᥻ࠊ␌៣ⱘᓔྟˈ䞡⥽Ϣ㒧ᴳˈ GobangGame㉏ 1.3.3 /Download at http://www.pin5i.com for( int i = 0 ; i < BOARD_SIZE ; i++ ) { ㋴ܗ//ᠧॄ↣Ͼ᭄㒘 public void printBoard() { ҷⷕ⏙ऩ˖code\gobang\src\org\crazyit\gobang\Chessboard.java ㋴ˈᇚ݊ᠧॄेৃDŽܗࠄ݋ԧⱘᶤϾѠ㓈᭄㒘ⱘ ᠽሩᗻDŽᔧ✊ˈ៥Ӏ೼ᴀゴЎњㅔऩ䍋㾕ˈা೼䆹Ѡ㓈᭄㒘Ёᄬᬒ䳔㽕ᠧॄⱘᄫヺІˈᠧॄᯊা䳔㽕ᕫ ࠄ␌៣ⱘخ㒘Ёⱘ↣ϔϾẟᄤᇍ䈵ˈ䛑ৃҹᅲ⦄ᶤϾẟᄤ᥹ষ៪㗙㒻ᡓẟᄤⱘᢑ䈵㉏ˈ䖭ḋৃҹ᳈དⱘ ࠄ᳈དⱘᠽሩᗻˈ៥Ӏৃҹ೼Ѡ㓈᭄㒘Ёᄬᬒẟᄤᇍ䈵ˈ㗠ϡᰃㅔऩⱘᄫヺІˈ䙷Мᄬᬒ೼Ѡ㓈᭄خҹ 䕧ߎẟⲬˈা䳔㽕 Chessboard ⱘ board ሲᗻ˄Ѡ㓈᭄㒘˅ⱘ↣ϔϾؐˈᠧॄࠄ᥻ࠊৄЁDŽབᵰৃ 1.4.2 䕧ߎẟⲬ ҹᇚ᭄㒘Ё݋ԧⱘᶤϾĀकā᳓ᤶ៤݋ԧⱘẟᄤᄫヺІDŽ ԡࠄẟⲬⱘᶤϾԡ㕂ˈা䳔㽕ᕫࠄẟⲬ᭄㒘ⱘϔ㓈ؐϢѠ㓈ؐेৃˈ՟བ໘⧚⥽ᆊϟẟࡼ԰ⱘᯊ׭ˈৃ ㋴ⱘؐ䌟ЎĀकāᴹ߱ྟ࣪ẟⲬDŽ߯ᓎњẟⲬ᭄㒘ৢˈབᵰ䳔㽕ᅮܗᓎℸ᭄㒘ৢˈ䗮䖛䗁ҷЎЎϾ᭄㒘 Ϟ䴶ҷⷕЁˈBOARD_SIZE ᰃҷ㸼ẟⲬⱘ໻ᇣˈ⫼ϔϾ String[][]㉏ൟⱘѠ㓈᭄㒘ᴹҷ㸼ẟⲬˈ߯ } } } board[i][j] = "क"; for( int j = 0 ; j < BOARD_SIZE ; j++ ) { for( int i = 0 ; i < BOARD_SIZE ; i++ ) { ㋴䌟ؐЎĀकāˈ⫼Ѣ᥻ࠊৄ䕧ߎẟⲬܗ //ᡞ↣Ͼ board = new String[BOARD_SIZE][BOARD_SIZE]; //߱ྟ࣪ẟⲬ᭄㒘 public void initBoard() { ҷⷕ⏙ऩ˖code\gobang\src\org\crazyit\gobang\Chessboard.java ៥Ӏ䳔㽕߱ྟ࣪ẟⲬⱘᅲ⦄ˈⳟҹϟҷⷕ⠛↉DŽܜ৘Ͼࡳ㛑DŽ佪 ೼ 1.3 㡖ҟ㒡䖛ˈℸ㉏Џ㽕ᰃᅲ⦄ẟⲬ߱ྟྟ࣪ǃ䕧ߎǃϢ᳈ᮄˈ೼䖭㡖֓⫼ҷⷕϔℹϔℹഄᅲ⦄ 1.4.1 ߱ྟ࣪ẟⲬ ೒ 1.3 ẟⲬϢ᭄㒘ⱘ݇㋏ /Download at http://www.pin5i.com ៣ⱘḌᖗ䚼ߚDŽ ঞњϔѯ Java 䇁㿔ⱘ෎ᴀ᪡԰ˈ՟བጠ༫ᕾ⦃ǃ߯ᓎѠ㓈᭄㒘ㄝDŽ೼ϟ䴶ⱘᇣ㡖Ёˈ៥Ӏᇚএᅲ⦄␌ ࠄℸˈẟⲬ㉏ⱘ޴Ͼᮍ⊩䛑Ꮖ㒣ᅲ⦄ˈ䆹㉏ⱘЏ㽕ࡳ㛑ᰃ߯ᓎẟⲬǃᠧॄẟⲬㄝˈᅲ⦄ⱘ䖛⿟Ё⍝ } return this.board; public String[][] getBoard() { */ * @return 䖨ಲẟⲬ * 䖨ಲẟⲬ /** ҷⷕ⏙ऩ˖code\gobang\src\org\crazyit\gobang\Chessboard.java getBoard ᮍ⊩া䳔㽕ᇚᴀᇍ䈵Ёⱘ board˄Ѡ㓈᭄㒘˅䖨ಲेৃˈҷⷕབϟDŽ ẟᄤ៪㗙䅵ㅫ␌៣ᰃ৺㚰߽DŽ ␌៣㉏ GobangGame Ё䇗⫼ˈ␌៣㉏ᕫࠄẟⲬⱘѠ㓈᭄㒘ˈৃҹ⫼Ѣ߸ᮁẟⲬЁⱘᶤϔϾԡ㕂ᰃ৺᳝ ೼ Chessboard ЁᦤկњϔϾ getBoard ⱘᮍ⊩ˈ⫼Ѣ䖨ಲᴀᇍ䈵ⱘẟⲬѠ㓈᭄㒘ˈ䆹ᮍ⊩ϔ㠀೼ 1.4.3 㦋পẟⲬ ೼᥻ࠊৄЁߎ⦄њϔϾẟⲬDŽڣᠧॄߎᴹⱘᬜᵰˈህད ೒ 1.4 ᥻ࠊৄѨᄤẟⱘẟⲬ ߎᬜᵰབ೒ 1.4DŽ System.out.println()ᮍ⊩ᰃ䕧ߎৢ㞾ࡼᤶ㸠ⱘˈབᵰՓ⫼ℸᮍ⊩ˈ֓䖒ϡࠄ៥Ӏ䳔㽕ⱘᬜᵰˈẟⲬⱘ䕧 䕧ߎᴹˈ⊼ᛣࠄ䖭䞠ⱘ䕧ߎᮍ⊩⫼ⱘᰃ System.out.print()㗠ϡᰃᐌ⫼ⱘ System.out.println()ˈ䖭䞠಴Ў ㋴䛑㽕ܗẟⲬⱘ䕧ߎϢẟⲬⱘ߱ྟ࣪Ⳍ㉏Ԑˈ䛑ᰃ㽕䘡ग़ֱᄬẟⲬⱘ᭄㒘ˈাϡ䖛ᰃ↣䘡ग़ࠄϔϾ } } System.out.print("\n"); ㋴ህᤶ㸠ϔ⃵ܗ //↣ᠧॄᅠϔ㸠᭄㒘 } System.out.print( board[i][j] ); //ᠧॄৢϡᤶ㸠 for( int j = 0 ; j < BOARD_SIZE ; j++ ) { /Download at http://www.pin5i.com ᆊ⦄೼䕧ܹⱘ x Ϣ y തᷛ˄x ੠ y തᷛ⥽ټℸ㉏Ё᳝ಯϾሲᗻˈϸϾ int ㉏ൟⱘ posX Ϣ poxYˈ⫼ᴹᄬ 䕧ܹⱘড়⊩ᗻˈ߸ᮁ␌៣ⱘ䕧Ϣ䌶ˈ䇗⫼ẟⲬ㉏ᴹ߱ྟ࣪ẟⲬˈᠧॄẟⲬˈՓ⫼ẟᄤ㉏এ䆒㕂ẟⲬㄝDŽ ᴀゴЁⱘ␌៣㉏ᰃ GogangGameˈ೼䆹㉏ЁˈЏ㽕᥻ࠊ␌៣ⱘᓔྟˈ䞡ᮄᓔྟϢ㒧ᴳˈ偠䆕⥽ᆊ 1.6 ␌៣㉏ᅲ⦄ ϔϾ䕗ЎㅔऩⱘѨᄤẟˈԚ᳈໮ⱘᛇ৥໻ᆊሩ⦄䴶৥ᇍ䈵ⱘᗱ㓈DŽخ㱑✊ᴀゴᰃЎњ ᴀゴЁⱘѨᄤẟ䖕⿏ࠄ swing ⬠䴶Ёˈ䙷М䆹ẟᄤᵮВ㉏ህϡᕫϡ᳈ᬍњDŽ ӕϮᑨ⫼˅ˈ䱣ⴔϮࡵⱘϡᮁব࣪ˈ㉏ⱘϡৃব޴Тᰃϡৃ㛑ⱘDŽВϾ՟ᄤˈབᵰ䳔㽕ᇚخЁ˄⡍߿ᰃ މϟˈϔϾ㉏Ёⱘሲᗻ᳝䰤ᑊϨᰃ೎ᅮⱘDŽԚᰃ೼៥Ӏᓔথⱘᅲ䰙ᚙމ೼ᴀᇣ㡖ⱘᓔ༈ᦤࠄ˖೼ᶤѯᚙ ᅲ⦄㉏ˈ៥Ӏ೼ẟⲬѠ㓈᭄㒘ЁᄬᬒⱘাᰃᶤϾ᥹ষˈ㗠ϡᰃ݋ԧⱘ㉏ˈ䖭ḋˈᦤ催њ⿟ᑣⱘৃᠽሩᗻˈ ᓎゟẟᄤ᥹ষˈᑊᦤկⱑẟϢ咥ẟⱘމབᵰԴᏠᳯԴⱘ⿟㛑᳝᳈དⱘᠽሩᗻˈヨ㗙ᓎ䆂ৃҹḍ᥂ᚙ ᄫヺІˈ಴ℸৃҹⳈ᥹ݭ៤ᵮВᇍ䈵ेৃDŽ ሩ⼎ϔϾẟᄤˈৃ㛑䳔㽕Ў݋ԧⱘᶤϾẟᄤֱᄬⳌᑨⱘẟᄤ೒⠛ˈ೼ᴀゴЁˈ⬅Ѣẟᄤাᰃ᱂䗮ⱘϸϾ ೼᥻ࠊৄЁˈ៥ӀৃҹՓ⫼䖭⾡ᮍᓣᴹ⹂ᅮẟᄤⱘᄫヺІˈབᵰ៥Ӏ䳔㽕೼ swing ៪㗙݊Ҫ⬠䴶Ё Chessman.WHITE.getChessman(); Chessman.BLACK.getChessman(); ⬅Ѣ BLACK Ϣ WHITE ϸϾሲᗻᰃ䴭ᗕⱘˈ᠔ҹ㽕㦋প咥ᄤ៪㗙ⱑᄤˈৃҹ䗮䖛ҹϟҷⷕᴹ㦋ᕫ˖ public static final Chessman WHITE = new Chessman(“ƻ”); public static final Chessman BLACK = new Chessman(“ƽ”); ㄝৠҹϟҷⷕ˖ ֱ䆕ℸᇍ䈵া᳝咥ᄤϢⱑᄤϸ⾡㉏ൟDŽ咥ԧҷⷕᰃ߫ߎᵮВؐˈᅲ䰙Ϟህᰃ䇗⫼⾕⫼ᵘ䗴఼߯ᓎℸᇍ䈵ˈ ఼ⱘᴗ䰤ׂ佄ヺᰃ privateˈгᰃ㸼ᯢℸ㉏ᰃϡৃҹ䗮䖛໪䚼߯ᓎⱘˈা㛑೼ℸ㉏ⱘݙ䚼߯ᓎˈ䖭ᰃЎњ ೼Ϟ䴶ⱘҷⷕЁˈৃҹⳟࠄˈᵮВ㉏ᰃ⫼ enum ݇䬂ᄫҷ᳓њ class ݇䬂ᄫˈⳟࠄℸᵮВ㉏ⱘᵘ䗴 } } return this.chessman; public String getChessman() { */ * @return 咥ẟ៪㗙ⱑẟ /** } this.chessman = chessman; private Chessman(String chessman) { */ * ⾕᳝ᵘ䗴఼ /** private String chessman; BLACK("ƽ"),WHITE("ƻ"); public enum Chessman { ҷⷕ⏙ऩ˖code\gobang\src\org\crazyit\gobang\Chessman.java 㗠ϡᰃ classˈℸ㉏Ё᳝ϸϾᵮВሲᗻ BLACK ੠ WHITEˈҷ㸼咥ᄤϢⱑᄤˈҷⷕᅲ⦄བϟ˖ ᄫ⫼ enumعϸϾᇍ䈵ˈ咥ẟ੠ⱑẟDŽ䖭⾡ᅲ՟᳝䰤㗠Ϩ೎ᅮⱘ㉏ˈ೼ Java 䞠䴶⿄ЎᵮВ㉏ˈᵮВ㉏ⱘ݇ ϟ˅ˈ՟བᴀゴЁⱘẟᄤ㉏ˈᅗা᳝މϟˈϔϾ㉏ⱘሲᗻᰃ᳝䰤㗠Ϩ೎ᅮⱘ˄೼ᶤѯᚙމ೼ᶤѯᚙ ẟᄤᵮВ㉏ᅲ⦄ 1.5 /Download at http://www.pin5i.com ҷⷕ⏙ऩ˖code\gobang\src\org\crazyit\gobang\GobangGame.java x Ϣ y ᖙ乏ᰃϔϾ᭄ᄫˈ⬅ҹϟҷⷕ偠䆕DŽˈܜ佪 ೒ 1.5 偠䆕⌕⿟ 㽕߸ᮁ䆹ԡ㕂ᰃ৺Ꮖ㒣ᄬ೼ẟᄤˈ݋ԧ߸ᮁ⌕⿟བ೒ 1.5 ᠔⼎DŽ ܹⱘᄫヺІᰃ৺㛑䕀ᤶЎ᭄ᄫˈᰃ৺䍙䍞ẟⲬⱘ䖍⬠˄ᇣѢㄝѢ 1ˈ໻ѢㄝѢẟⲬ᭄㒘ⱘ䭓ᑺ˅ˈᑊϨ䳔 ḍ᥂ᓩ㿔Ёᦤࠄⱘ䕧ܹ㑺ᅮˈ⥽ᆊ೼᥻ࠊৄ䕧ܹⱘᄫヺІᖙ乏ᰃҹ(x,y)ⱘᮍᓣ䕧ܹˈ䖬䳔㽕偠䆕䕧 1.6.2 偠䆕⥽ᆊ䕧ܹᄫヺІⱘড়⊩ᗻ ҹᶹⳟĀӓ QQ ␌៣໻ख़āϔゴDŽ ᴹᠻ㸠DŽ⬅ѢᴀゴЁⱘҷⷕϢࡼ԰Ⳍᇍ䕗ᇥˈ಴ℸᑊϡ⍝ঞབԩᅲ⦄ҹϞ᠔䇈ⱘ໘⧚῵ᓣˈ᳈⏅ܹⱘৃ ᱄ˈ⿟ᑣⱘ㗺ড়ᑺ᳈Ԣˈwhile ᕾ⦃ԧЁা䋳䋷䇏প⥽ᆊ䕧ܹⱘᄫヺІˈ㗠݋ԧⱘ໘⧚߭ϡᖙ⬅䆹ᮍ⊩ ᇥ while ᕾ⦃ԧЁⱘҷⷕˈ㗠ϨৃҹՓᕫ⿟ᑣ᳈ࡴ⏙ޣϡҙخᢑপߎᴹˈ԰Ў݋ԧⱘϔϾ໘⧚㉏DŽ䖭ḋ ⱘ if 䇁হˈ䖭ѯ if 䇁হӮᕅડ⿟ᑣⱘৃ䇏ᗻˈབᵰ䳔㽕ᇚ䖭ѯ if 䇁হএᥝˈ៥Ӏৃҹᇚ↣Ͼ if Ёⱘҷⷕ М៥Ӏህ䳔㽕߸ᮁ⥽ᆊ䕧ܹњ y ৢ⿟ᑣ᠔䳔㽕ᠻ㸠ાѯ᪡԰ˈ಴ℸˈ䖭ḋӮЎ while ᕾ⦃ԧЁ๲ࡴ䆌໮ བᵰ䳔㽕䇏প䕧ܹˈ៥Ӏህ䳔㽕Ў䖭ѯ䕧ܹ԰ߎϡৠⱘ߸ᮁˈ՟བˈ⥽ᆊ䕧ܹњ y˄㒻㓁␌៣˅ˈ䙷 㒳㽕∖ˈৃҹ೼偠䆕໘Փ⫼ continue 䏇ߎᴀ⃵ᕾ⦃DŽ ߭䖨ಲ nullDŽᔧᕫࠄ⥽ᆊ䕧ܹⱘᄫヺৢˈ៥Ӏৃҹ䖯䖭ѯᄫヺ䖯㸠偠䆕ˈ偠䆕ᅠৢˈབᵰᄫヺІヺড়㋏ BufferedReader Ё᳝ϔϾ readLine()ᮍ⊩ˈℸᮍ⊩ᘏᰃ䇏পϟϔ㸠ⱘ䕧ܹᄫヺІˈབᵰ≵᳝ϟϔ㸠ˈ } */ * ໘⧚䬂Ⲭ䕧ܹ /** while( (inputStr = br.readLine()) != null ) { //br.readLine:↣ᔧ䬂Ⲭ䕧ܹϔ㸠ݙᆍᣝಲ䔺䬂ˈ߭䕧ܹⱘݙᆍ㹿 br 䇏পࠄ String inputStr = null; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //㦋প䬂Ⲭⱘ䕧ܹ ᇚ݊ࣙ㺙៤ᄫヺ⌕DŽ᠔ҹ⿟ᑣЁ⫼Ѣ㦋প䬂Ⲭⱘ䕧ܹ䞛⫼ҹϟҷⷕ߯ᓎDŽ 䕧ܹ˖System.in ᰃᄫ㡖⌕ˈ᠔ҹ⿟ᑣ䳔㽕Փ⫼䕀ᤶ⌕ InputStreamReaderޚҢ䕧ܹ⌕Ё䇏প᭛ᴀˈԚᷛ BufferedReader ᰃ Java IO ⌕ЁⱘϔϾᄫヺࣙ㺙⌕ˈᅗᖙ乏ᓎゟ೼ᄫヺ⌕ⱘ෎⸔ПϞDŽ䆹ᇍ䈵ৃҹ 1.6.1 Փ⫼BufferedReader㦋প䬂Ⲭ䕧ܹ ⫼߱ྟ࣪ẟⲬǃᠧॄẟⲬǃ㦋ᕫẟⲬ˄᭄㒘˅ㄝᮍ⊩DŽ ᳝ϔϾ Chessboard ㉏ൟⱘব䞣 chessboardˈ⫼ᴹ㸼⼎ẟⲬˈ␌៣Ёህা⫼ࠄϔϾẟⲬˈ䆹ᇍ䈵ৃҹՓ ␌៣㚰߽䳔㽕䖲᥹ⱘẟᄤ䖒ࠄⱘ䖲ᄤ᭄Ⳃˈ⬅ѢᰃѨᄤẟˈ಴ℸা䳔㽕 5 ϾẟᄤⳌ䖲ˈ␌៣ህ㚰߽DŽ䖬 ᰃᣛ⥽ᆊ䕧ܹⱘ᭄ᄫᇍᑨẟⲬ᭄㒘Ёⱘϔ㓈ؐϢѠ㓈ؐ˅ˈϔϾ咬䅸ؐЎ5ⱘint㉏ൟᐌ䞣WIN_COUNTˈ /Download at http://www.pin5i.com ߸ᮁ䕧䌶೼ᴀゴⱘ⿟ᑣЁ⿡ᖂ໡ᴖˈ᳝ϸ⾡ᮍ⊩ᴹ߸ᮁ䕧䌶˖ ⱘ㸠Ўˈ⣀ゟ៤ϔϾ isWon ᮍ⊩˄䆺㒚䇋ⳟ 1.3.3 Ёⱘ GobangGame ㉏˅DŽ ৠḋഄˈབᵰᰃĀ⬉㛥ā䱣ᴎ⫳៤ⱘതᷛˈ៥Ӏৠḋⱘ䳔㽕䖯㸠䕧䌶偠䆕ˈ಴ℸˈ៥ӀᏆ㒣ᇚ߸ᮁ䕧䌶 ߸ᮁ␌៣䕧䌶ˈ䳔㽕೼⥽ᆊ䕧ܹњതᷛᑊ䗮䖛њড়⊩ᗻ偠䆕ৢ˄䕧ܹⱘതᷛ˅ˈݡᠻ㸠䕧䌶ⱘ偠䆕ˈ 1.6.3 ߸ᮁ䕧䌶 ㋴ᯊˈህӮᡯߎ ArrayIndexOutOfBoundsException ᓖᐌDŽܗ䙷М䗮䖛ẟⲬ᭄㒘㦋পᶤϾ 䳔㽕ᦤ⼎⥽ᆊ䞡ᮄ䕧ܹDŽ䖭䞠䳔㽕⊼ᛣⱘᰃˈབᵰ≵᳝ࠡϔϾ߸ᮁ˄߸ᮁ䕧ܹⱘതᷛᰃ৺䍙䖛ẟⲬ㣗ೈ˅ˈ ҹϞҷⷕЁˈབᵰ board[i][j]ϡㄝѢĀकā(ĀकāᰃẟⲬ↣Ͼԡ㕂ⱘ咬䅸ؐ)ˈ߭䆕ᯢℸԡ㕂᳝ẟᄤˈ } return false; System.out.println( "ℸԡ㕂Ꮖ㒣᳝ẟᄤˈ䇋䞡ᮄ䕧ܹ˖" ); chessboard.printBoard(); if( board[posX][posY] != "क" ) { String[][] board = chessboard.getBoard(); //Ẕᶹ䕧ܹⱘԡ㕂ᰃ৺Ꮖ㒣᳝ẟᄤ ҷⷕ⏙ऩ˖code\gobang\src\org\crazyit\gobang\GobangGame.java ߭ᦤ⼎⥽ᆊ䞡ᮄ䕧ܹˈ⬅ҹϟҷⷕ偠䆕DŽ ㋴ؐЎĀकā˅ˈܗ˄㋴ᰃ৺Ꮖ㒣ᰃϔϾẟᄤ˄Āƽā៪㗙Āƻā˅ˈབᵰ䆹തᷛЁᏆ㒣᳝ẟᄤܗ㋴ˈݡ߸ᮁ ܗChessboard ᇍ䈵Ёⱘ getBoard ᮍ⊩ৃҹᕫࠄẟⲬⱘѠ㓈᭄㒘ˈḍ᥂⥽ᆊ䕧ܹⱘതᷛᕫࠄ᭄㒘Ёⱘ 偠䆕њ䕧ܹതᷛⱘড়⊩ᗻৢˈ䖬䳔㽕偠䆕⥽ᆊ䕧ܹⱘതᷛЁᰃ৺Ꮖ㒣ᄬ೼ẟᄤˈ៥Ӏ䗮䖛 } return false; + Chessboard.BOARD_SIZE + ",䇋䞡ᮄ䕧ܹ˖" ); System.out.println( "X Ϣ Y തᷛা㛑໻ѢㄝѢ 1,ϢᇣѢㄝѢ" chessboard.printBoard(); || posY >= Chessboard.BOARD_SIZE ) { if( posX < 0 || posX >= Chessboard.BOARD_SIZE || posY < 0 //Ẕᶹ䕧᭄ܹؐᰃ৺೼㣗ೈПݙ ҷⷕ⏙ऩ˖code\gobang\src\org\crazyit\gobang\GobangGame.java ⷕ偠䆕DŽ 㣗ೈা㛑ᰃ໻Ѣ 1 ϢᇣѢ N˄ẟⲬⱘ໻ᇣ˅ˈབᵰ䍙ߎ䖭Ͼ㣗ೈˈ߭䳔㽕ᦤ⼎⥽ᆊ䞡ᮄ䕧ܹˈ⬅ҹϟҷ 10 Ь 10ˈԚ⥽ᆊ䕧ܹњ 11ˈ䙷МᇚӮᡯߎ ArrayIndexOutOfBoundsException ᓖᐌˈ಴ℸˈx Ϣ y ⱘ ໪ˈ䖬䳔㽕߸ᮁ⥽ᆊ䕧ܹⱘതᷛ㣗ೈˈे䆹㣗ೈϡৃᇣѢ 1 ᑊϡৃ໻ѢẟⲬ᭄㒘ⱘ᳔໻ؐˈ՟བẟⲬᰃ ᐌᤩ㦋ˈᦤ⼎⥽ᆊ䳔㽕䞡ᮄ䕧ܹẟᄤതᷛDŽ䰸њ߸ᮁ⥽ᆊ䕧ܹⱘᄫヺІᰃ৺ヺড়៥Ӏ␌៣᠔㽕∖ⱘḐᓣ ϡ㛑䕀ᤶ៤ᶤϔϾ᭄ᄫˈ䆹ᮍ⊩ᇚӮᡯߎ NumberFormatException ᓖᐌˈ៥ӀৃҹՓ⫼ catch ᇚ䆹ᓖ ᔧ៥Ӏ䇗⫼ Integer.parseInt ᮍ⊩ᇚᄫヺІ䕀ᤶ៤ϔϾ Integer ㉏ൟⱘᯊ׭ˈབᵰ䳔㽕䕀ᤶⱘᄫヺІ } return false; System.out.println("䇋ҹ(᭄ᄫ,᭄ᄫ)ⱘḐᓣ䕧ܹ˖"); chessboard.printBoard(); } catch (NumberFormatException e) { posY = Integer.parseInt( posStrArr[1] ) - 1; posX = Integer.parseInt( posStrArr[0] ) - 1; try { String[] posStrArr = inputStr.split(","); ᇚ⫼᠋䕧ܹⱘᄫヺІҹ䗫ো(,)԰Ўߚ䱨ˈߚ䱨៤ϸϾᄫヺІ// /Download at http://www.pin5i.com temp = posX + WIN_COUNT - 1; //䅵ㅫ㒜⚍ⱘ᳔໻ X തᷛϢ Y തᷛ startY = temp < 0 ? 0 : temp; temp = posY - WIN_COUNT + 1; startX = temp < 0 ? 0 : temp; temp = posX - WIN_COUNT + 1; //䅵ㅫ䍋⚍ⱘ᳔ᇣ X തᷛϢ Y തᷛ int temp = 0; int sameCount = 0; //ৠᴵⳈ㒓ϞⳌ䚏ẟᄤ㌃⿃᭄ int endY = endX; //Ⳉ㒓㒧ᴳ Y തᷛ int endX = Chessboard.BOARD_SIZE - 1; //Ⳉ㒓㒧ᴳ X തᷛ int startY = 0; //Ⳉ㒓䍋⚍ Y തᷛ int startX = 0; //Ⳉ㒓䍋⚍ⱘ X തᷛ ҷⷕ⏙ऩ˖code\gobang\src\org\crazyit\gobang\GobangGame.java ᮁ␌៣ⱘ䕧䌶ˈᑊϨᰃҹ䆹ẟᄤЎЁᖗ৥਼ೈ䖯㸠䘡ग़DŽҹϟᰃ߸ᮁ䕧䌶ⱘҷⷕᅲ⦄DŽ Ѩ乫ẟᄤˈབᵰ≵᳝ˈህাᓊԌࠄ䖍⬠DŽ᠔ҹˈা㽕㛑䅵ㅫߎӏᛣϔ乫ẟᄤⱘ䖭ѯԡ㕂ˈ៥Ӏህৃҹ߸ ѯതᷛ䛑ᰃℸ咥ẟ㛑ᔶ៤Ѩ䖲ᄤⱘ᳔ᇣ៪㗙᳔໻ԡ㕂ⱘẟᄤˈབᵰ৘Ͼᮍ৥᳝䎇໳ⱘぎ䯈ˈህᓊԌࠄ㄀ ೼೒ 1.6 Ёৃҹⳟߎˈ˄0ˈ0˅ˈ˄0ˈ3˅ˈ˄0ˈ6˅ˈ˄3ˈ0˅ˈ˄6ˈ0˅ˈ˄3ˈ7˅ˈ˄7ˈ3˅ˈ˄7ˈ7˅䖭 ೒ 1.6 Ѩ䖲ᄤ ᠔⼎DŽ ಴Ўϡ䳔㽕ᠿᦣᭈϾẟⲬˈ᠔ҹ᳈ࡴᖿ䗳ˈᴀゴ⿟ᑣՓ⫼ⱘᰃℸᮍ⊩ˈ䆹ᮍ⊩ⱘॳ⧚བ೒ 1.6 ℸẟᄤ᳝≵᳝ᔶ៤Ѩ䖲ᄤˈབᵰᔶ៤Ѩ䖲ᄤˈ߭ϟẟᄤⱘ⥽ᆊЎ䌶DŽℸᮍ⊩Ϣࠡ䴶ⱘᮍ⊩↨䕗ˈ ‰ ↣⃵ϟᅠϔ乫ẟᄤˈҹ䆹ẟᄤЎЁᖗˈᠿᦣ೼ℸẟᄤ᠔೼㣗ೈݙⱘ῾ǃオǃ᭰ᮍ৥ˈ偠䆕ࡴϞ ᇍẟⲬ䖯㸠䘡ग़ˈ䖭ḋᇍ⿟ᑣⱘᗻ㛑Ӯ䗴៤ϔᅮⱘᕅડDŽ ␌៣DŽ䆹߸ᮁᮍ⊩䳔㽕䘡ग़ᭈϾẟⲬˈгህᰃᛣੇⴔ↣⃵ϟẟৢ˄⥽ᆊ៪㗙Ā⬉㛥ā˅䛑䳔㽕 买㡆ⱘẟᄤ䖒ࠄѨ䖲ᄤˈ߭ℸ买㡆ⱘ⥽ᆊЎ䌶DŽབᵰ≵᳝Ⳍৠ买㡆ⱘẟᄤ䖒ࠄѨ䖲ᄤˈ߭㒻㓁 ⃵ϟᅠϔ乫ẟᄤˈህ䗮䖛⿟ᑣҢ῾ǃオǃ᭰৘Ͼᮍ৥ᠿᦣẟⲬˈབᵰ೼ᶤϾᮍ৥Ёˈ᳝ৠ⾡↣ ‰ /Download at http://www.pin5i.com തᷛˈⳈࠄ䖭ϾതᷛЁ≵᳝ẟᄤDŽ Ⲭ໻ᇣ)㣗ೈПݙⱘ x Ϣ y ℷ᭄തᷛˈབᵰ䖭ϾതᷛЁᏆ㒣᳝ẟᄤˈ߭㒻㓁Փ⫼ Math.radom()ᮍ⊩㦋প Math.random()ᮍ⊩㦋পϔϾ೼ BOARD_SIZE(ẟ ⫼ܜϞ䴶ҷⷕЁˈ䱣ᴎ⫳៤ x Ϣ y തᷛⱘ䖛⿟ᰃ ୤ॴēᄷ໭ࢗಳဓຄdۤޥē࡜ࢶྻඔವ෷ܥ୤૭ᄯྸ࠼՛Ӊၽ୤ᆐωயਠပ೛࿳ԅڴᆐēఢ ēൎྻแਬ༓ྑ૱ծ୤૭ᄯಾ׮ൎပԅสᄢ՛ပ୤ܬക༶އēუّ while ߜစၙϢݖණѻēܥၽ୤ᆐԅ ჼّ୤૭ᄯ՛Ӊڴ࠼Ӊၽ୤ᆐē႕༓ྑᄷ໭഻ݯಓюᆵγdઊਙ࡜ݖСಓუྂྡྷᄵெࣣēఢྸڴ༎ēఢ ඨߑಾ૱ծ୤૭೴ᆦᄯಾ׮ྸ࠼Ӊၽ୤ܬēୣᄯ༶ܬუऺ༓ྑᅟ࿉ԅಾēဎဟแਬರဈॴ while ༶ } posY = (int)(Math.random() * ( Chessboard.BOARD_SIZE - 1 )); posX = (int)(Math.random() * ( Chessboard.BOARD_SIZE - 1 ) ); while( board[posX][posY] != "क" ) { //ᔧẟⲬЁⱘԡ㕂ϡᰃĀकāⱘᯊ׭˄Ꮖ㒣᳝ẟᄤ˅ˈ߭ݡ⃵⫳៤ᮄⱘതᷛ String[][] board = chessboard.getBoard(); int posY = (int)(Math.random() * ( Chessboard.BOARD_SIZE - 1 ) ); //䱣ᴎ⫳៤ y തᷛˈेѠ㓈᭄㒘݋ԧѠ㓈ⱘؐ int posX = (int)(Math.random() * ( Chessboard.BOARD_SIZE - 1 ) ); //䱣ᴎ⫳៤ x തᷛˈेѠ㓈᭄㒘݋ԧϔ㓈ⱘؐ ҷⷕ⏙ऩ˖code\gobang\src\org\crazyit\gobang\GobangGame.java ϟẟⱘԡ㕂ˈᑊᇍ䖭ѯԡ㕂ⱘതᷛ䖯㸠ⳌᑨⱘߚᵤDŽҹϟᰃ䱣ᴎ⫳៤തᷛⱘҷⷕᅲ⦄DŽ ᵰᛇ䅽䅵ㅫᴎবĀ㘾ᯢā䍋ᴹˈৃҹࡴϞҎᎹᱎ㛑Ā⬉㛥āˈ䆹ҎᎹᱎ㛑Ā⬉㛥ā䳔㽕ߚᵤ⥽ᆊⱘ᠔᳝ ៥ӀՓ⫼䖭Ͼᮍᓣᴹѻ⫳䱣ᴎതᷛˈ಴Ўᰃ䱣ᴎ⫳៤ⱘԡ㕂ˈ᠔ҹ䖭Ͼ䅵ㅫᴎᰃ↨䕗ĀーāⱘDŽབ ᭄㒘ˈݡՓ⫼䆹ؐᴹЬҹẟⲬⱘ໻ᇣˈेৃѻ⫳䱣ᴎⱘതᷛDŽ ϟẟⱘࡳ㛑ˈ᳔Џ㽕ᰃѻ⫳䱣ᴎⱘതᷛDŽ៥ӀৃҹՓ⫼ Math.random ᮍ⊩ᴹѻ⫳ 0.0 ࠄ 1.0 П䯈ⱘ double ᕫࠄ䱣֓ⱘതᷛؐˈᑊϨ೼䆹തᷛЁ䖯㸠ϟẟ᪡԰ˈህৃҹᅲ⦄Ā⬉㛥āⱘϟẟˈ಴ℸˈᅲ⦄䖭Ͼ䱣ᴎ ៥Ӏ೼ࠡ䴶ゴ㡖Ё䇈ࠄˈՓ⫼ϔϾㅔऩⱘᮍᓣᴹѻ⫳ϔϾĀ⬉㛥āϢ៥Ӏ䖯㸠ᇍ៬DŽ៥Ӏা䳔㽕Փ 1.6.4 䅵ㅫᴎ䱣ᴎϟẟ ग़ⱘᅲ⦄෎ᴀ㉏ԐDŽ䖭䞠䳔㽕⊼ᛣⱘᰃˈᔧ䘛ࠄϔϾৃҹⳌ䖍ⱘẟᄤˈህ䳔㽕Ў sameCount ؐࡴ 1DŽ ᭄Ⳃ䖒ࠄѨ䖲ᄤˈ߭Ў䌶DŽҹϞҷⷕাᰃᅲ⦄῾৥䘡ग़߸ᮁˈオ৥䘡ग़Ϣ᭰৥䘡ग़ⱘ߸ᮁᮍ⊩Ϣ῾৥䘡 xǃy തᷛϢ᳔໻ xǃy തᷛˈ✊ৢҢ᳔ᇣ xǃy തᷛ䆓䯂ࠄ᳔໻ xǃy തᷛˈབᵰℸ买㡆ẟᄤⱘⳌ䖲㌃⿃ ᰃ䅵ㅫߎ೼䖭乫ẟᄤⱘⳈ㒓Ϟ(῾ǃオǃ᭰ᮍ৥)㛑䖒ࠄѨ䖲ᄤⱘ᳔ᇣܜҢϞ䴶ҷⷕЁৃҹⳟࠄˈ佪 } } sameCount = 0; } else if( sameCount != WIN_COUNT - 1 ) { sameCount++; if( board[posX][i] == ico && board[posX][i+1] == ico ) { for( int i = startY; i < endY; i++) { String[][] board = chessboard.getBoard(); //ҢᎺࠄেᮍ৥䅵ㅫⳌৠⳌ䚏ẟᄤⱘ᭄Ⳃ Chessboard.BOARD_SIZE - 1 : temp; endY = temp > Chessboard.BOARD_SIZE - 1 ? temp = posY + WIN_COUNT - 1; Chessboard.BOARD_SIZE - 1 : temp; endX = temp > Chessboard.BOARD_SIZE - 1 ? /Download at http://www.pin5i.com chessboard.printBoard(); chessboard.initBoard(); boolean isOver = false; //true Ў␌៣㒧ᴳ ҷⷕ⏙ऩ˖code\gobang\src\org\crazyit\gobang\GobangGame.java ҹϟⱘҷⷕᅲ⦄ҹϞⱘ⌕⿟DŽ ಲ㄀ 1 ℹ䞡ᮄᓔྟˈ৺߭䖨ಲ㄀ 2 ℹ䕂ࠄ⫼᠋䕧ܹDŽ 䅵ㅫᴎ䌶њˈ߭䇗⫼ isReply()ᮍ⊩䕧ߎⱘֵᙃ䆶䯂⥽ᆊᰃ৺䞡ᮄ␌៣ˈབᵰ⥽ᆊ䕧ܹ yˈ߭䖨 ‰ ˄6˅䇗⫼ computerDo()ᮍ⊩䱣ᴎ⫳៤䅵ㅫᴎⱘ x,y തᷛˈᑊᡞ board[x][y] 䌟ؐЎ"ƻ"DŽབᵰ ᓔྟDŽ ߭䇗⫼ isReply()ᮍ⊩䕧ߎⱘֵᙃ䆶䯂⥽ᆊᰃ৺䞡ᮄ␌៣ˈབᵰ⥽ᆊ䕧ܹ yˈ߭䖨ಲ㄀ 1 ℹ䞡ᮄ ‰ ˄5˅䇗⫼ isWon( int posX , int posY , String chessman )߸ᮁ⥽ᆊᰃ৺䌶њDŽབᵰ⥽ᆊ䌶њˈ ‰ ˄4˅ᡞ⥽ᆊϟⱘẟᄤԡ㕂䌟ؐЎ"ƽ"DŽ ৺߭ࠄ㄀ 4 ℹDŽ ‰ ˄3˅ݡ䇗⫼ᴀ㉏ⱘ isValid()ᮍ⊩এ偠䆕⥽ᆊ䕧ܹⱘড়⊩ᗻˈབᵰ䕧ܹϡড়⊩ˈ䖨ಲ㄀ 2 ℹ㒻㓁ˈ ‰ ˄2˅Ң᥻ࠊৄ㦋প⫼᠋ⱘ䕧ܹDŽ ࣪ϢᠧॄẟⲬDŽ 䇗⫼ Chessboard ㉏ൟⱘ chessboard ሲᗻЁⱘ initBoard()Ϣ printBoard()ᮍ⊩এ߱ྟܜ‰ ˄1˅佪 ҹϟᰃ␌៣ⱘ⌕⿟䇈ᯢˈ݋ԧгৃҹⳟ೒ 1.2˖ 1.6.6 ␌៣䖛⿟ᅲ⦄ } return false; } return true; //ᓔྟᮄϔሔ if( br.readLine().equals("y") ) { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println( message + "ݡϟϔሔ˛(y/n)" ); ? "ᙁ୰ᙼˈᙼ䌶њˈ" : "ᕜ䘫ធˈᙼ䕧њˈ"; String message = chessman.equals(Chessman.BLACK.getChessman()) chessboard.printBoard(); throws Exception { public boolean isReplay( String chessman ) */ * @return ᓔྟ䖨ಲ trueˈড߭䖨ಲ falseDŽ * @param chessman "ƽ"Ў⫼᠋ˈ"ƻ"Ў䅵ㅫᴎDŽ * ᰃ৺䞡ᮄᓔྟϟẟDŽ /** ҷⷕ⏙ऩ˖code\gobang\src\org\crazyit\gobang\GobangGame.java Ͼ⿟ᑣˈᅲ⦄ҷⷕབϟDŽ ৄ䕧ߎ䆶䯂⥽ᆊᰃ৺䞡ᮄᓔྟ␌៣ⱘֵᙃˈབᵰ⥽ᆊ䕧ܹ”y”ᄫヺІˈ߭䞡ᮄᓔྟ␌៣ˈ৺߭Ⳉ᥹䗔ߎᭈ ᅲ⦄ᰃ৺䞡ᮄᓔྟ␌៣ⱘࡳ㛑ˈ䖭೼䖭ᮍ⊩Ёˈ⿟ᑣⱘ⌕⿟ᰃ˖བᵰ⥽ᆊ៪㗙⬉㛥䌶њˈ߭೼᥻ࠊ ᰃ৺䞡ᮄ␌៣ 1.6.5 /Download at http://www.pin5i.com ख़āϔゴDŽᴀゴⱘⳂⱘЏ㽕ҟ㒡ϔϾㅔऩѨᄤẟⱘᅲ⦄DŽ 䖭ѯ if 䇁হˈ䆺㒚ৃҹⳟĀӓ QQ ␌៣໻އ៥Ӏ೼䆹ゴ㡖Փ⫼њ݊Ҫᮍᓣএ㾷ˈމІ݇԰Ⳍᑨ߸ᮁⱘᚙ ⬅Ѣ䖭ѯ if ⱘᄬ೼ˈӮՓᕫ៥Ӏ⿟ᑣⱘৃ䇏ᗻবᏂˈ೼Āӓ QQ ␌៣໻ख़āゴ㡖ˈৠḋߎ⦄њ䇏পᄫヺ 䖭ѯ if 䯂乬˄ৃҹՓ⫼䆒䅵῵ᓣЁⱘㄪ⬹῵ᓣ˅ˈᔧ✊໻ᆊгৃ㛑㾝ᕫ䖭ѯ if ≵᳝ҔМ݇㋏ˈԚᰃˈއ 㓁ˈ៥ӀৃҹⳟࠄˈҹϞⱘҷⷕЁ៥Ӏݭњ໮Ͼ if 䇁হˈ݊ᅲ៥ӀৃҹՓ⫼ϔѯ Java ⱘ෎⸔ⶹ䆚ᴹ㾷 ҹϞⱘҷⷕЁˈ៥ӀՓ⫼њϔϾ isOver ᴹᷛ䆚␌៣ᰃ৺㚰߽ˈᔧ␌៣㚰߽ᯊˈህ䆶䯂⥽ᆊᰃ৺㒻 } System.out.println("䇋䕧ܹᙼϟẟⱘതᷛˈᑨҹ x,y ⱘḐᓣ䕧ܹ˖"); chessboard.printBoard(); } break; //བᵰϡ㒻㓁ˈ䗔ߎ⿟ᑣ } continue; chessboard.printBoard(); chessboard.initBoard(); if( isReplay( chessman ) ) { //བᵰ㒻㓁ˈ䞡ᮄ߱ྟ࣪ẟⲬˈ㒻㓁␌៣ if( isOver ) { //བᵰѻ⫳㚰㗙ˈ䆶䯂⫼᠋ᰃ৺㒻㓁␌៣ } } isOver = true; if( isWon( computerPosArr[0] , computerPosArr[1] , chessman ) ) { //߸ᮁ䅵ㅫᴎᰃ৺䌶њ chessboard.setBoard( computerPosArr[0] , computerPosArr[1] , chessman ); chessman = Chessman.WHITE.getChessman(); int[] computerPosArr = computerDo(); //䅵ㅫᴎ䱣ᴎ䗝ᢽԡ㕂തᷛ } else { isOver = true; if( isWon( posX , posY , chessman ) ) { //߸ᮁ⫼᠋ᰃ৺䌶њ chessboard.setBoard( posX , posY , chessman ); String chessman = Chessman.BLACK.getChessman(); ㋴䌟Ў"ƽ"ܗ //ᡞᇍᑨⱘ᭄㒘 } continue; //བᵰϡড়⊩ˈ㽕∖䞡ᮄ䕧ܹˈݡ㒻㓁 if( !isValid( inputStr ) ) { isOver = false; while( (inputStr = br.readLine()) != null ) { //br.readLine:↣ᔧ䬂Ⲭ䕧ܹϔ㸠ݙᆍᣝಲ䔺䬂ˈ߭䕧ܹⱘݙᆍ㹿 br 䇏পࠄ String inputStr = null; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 㦋প䬂Ⲭⱘ䕧ܹ// ㄀ 1 ゴ ᥻ࠊৄѨᄤẟ g15g 1.7 ᴀゴᇣ㒧 ᴀゴЏ㽕ᰃҟ㒡ᓔথ᥻ࠊৄѨᄤẟⱘᭈϾ䖛⿟ˈԧ⦄⌕⿟䆒䅵Ϣ㉏䆒䅵ⱘ෎ᴀᮍ⊩ˈ⼎㣗њ᭄㒘ⱘ Փ⫼ˈ㦋প⫼᠋䬂Ⲭⱘ䕧ܹDŽՓ⫼њߚᬃ㒧ᵘϢᕾ⦃㒧ᵘⱘ⌕⿟᥻ࠊˈ䖬ҟ㒡ϢՓ⫼њᵮВ㉏DŽ৥䇏㗙 ☠䕧њ䴶৥ᇍ䈵ⱘϔѯ෎ᴀⶹ䆚ˈ䗮䖛䖭ѯ෎⸔ⱘⶹ䆚䆒䅵Ϣᓔথߎ᳝䍷ੇᗻⱘᇣ␌៣ˈ䅽䇏㗙ࡴ⏅ᇍ 䖭ѯ෎⸔ⶹ䆚ⱘ⧚㾷DŽ Download at http://www.pin5i.com/ /Download at http://www.pin5i.com ‰ ∖㒧ᵰՓ⫼ⱘ᭄ᄺヺোᰃĀ=āDŽ ᭄ˈᇍᑨՓ⫼ⱘヺোᰃĀsqrtā੠Ā1/xāDŽצ‰ ᓔᮍϢ ǃЬǃ䰸ˈᇍᑨՓ⫼ⱘヺোᰃĀ+āǃĀ-āǃĀ*āǃĀ/āDŽޣ‰ ࡴǃ ᭄ˈՓ⫼ࠄⱘ᭄ᄺヺো᳝˖צ∖ᇍϔϾ䴲 0 ⱘ᭄ᄺ ǃЬǃ䰸ಯ߭䖤ㅫˈ៪㗙ᇍϔϾℷ᭄䖯㸠ᓔᮍˈ៪㗙ޣ೼ℸ䅵ㅫ఼ЁˈЏ㽕Փ⫼ⱘ᭄ᄺ䖤ㅫ᳝ࡴǃ 2.1.1 ᭄ᄺϢ݊ᅗヺোҟ㒡 ᦤկ৘Ͼ䅵ㅫ఼ⱘᣝ䪂DŽ Ң೒ 2.1 Ёৃҹⳟࠄˈ៥Ӏᓔথ⬠䴶ⱘᯊ׭ˈ䳔㽕ᦤկϔϾ᭛ᴀḚ೼にষⱘ᳔Ϟ䚼ˈ᭛ᴀḚϟ䴶ݡ ೒ 2.1 ⫼ Swing ࠊ԰ⱘ䅵ㅫ఼ ⱘ᳔㒜ᬜᵰབ೒ 2.1 ᠔⼎DŽ Ⲭ䕧ܹؐৢˈህৃҹᇚ݊᠔䕧ܹⱘؐᰒ⼎ࠄϔϾ᭛ᴀḚЁˈ䖤ㅫৢˈݡᇚ㒧ᵰᰒ⼎ࠄ᭛ᴀḚЁDŽ䅵ㅫ఼ ᅲ⦄ϔϾ䅵ㅫ఼ˈ⬠䴶Ё䳔㽕ᦤկ৘⾡䕧ܹⱘᣝ䪂ˈݡҹ䖭ѯᣝ䪂㒘៤䅵ㅫ఼ⱘ䬂Ⲭˈ⫼᠋⚍ߏ䬂 ਀఼੠џӊ䗖䜡఼ㄝDŽ ϸϾᏗሔ఼ˈҹঞՓ⫼ࠄџӊǃџӊⲥخ JButton ϸϾ Swing ᆍ఼ˈՓ⫼ BorderLayout ੠ GridLayout ᅲ⦄ᴀкⱘ乍ⳂDŽ೼ᴀゴЁˈ៥ӀᇚՓ⫼ࠄ JFrame ੠ JPanel ϸϾ Swing ᆍ఼ˈՓ⫼ࠄ JTextField ੠ ៥Ӏ೼㄀ϔゴЁˈ៥Ӏᅲ⦄њϔϾ೼᥻ࠊৄ䖯㸠ⱘѨᄤẟ␌៣ˈ៥ӀҢᴀゴᓔྟᇚ೼ Swing ⬠䴶Ё 䅵ㅫ㒧ᵰ˗䇏প䅵ㅫ㒧ᵰ˗㌃⿃䅵ㅫ㒧ᵰDŽټ᭄˗∖ᓔᮍ˗ᄬצ∖˗Џ㽕ࡳ㛑᳝˖ಯ߭䖤ㅫ ൟ Windows 䅵ㅫ఼ᅲ⦄ⱘޚൟⱘ Java ᅲ⦄ˈᷛޚൟā੠Ā⾥ᄺൟāˈᴀゴⱘӓ Windows 䅵ㅫ఼ᰃᷛޚ Windows 䅵ㅫ఼ˈᰃ Windows ᪡԰㋏㒳㞾ᏺ䅵ㅫ఼,ˈৃҹᐂࡽ⫼᠋ᅠ៤᭄᥂ⱘ䖤ㅫˈᅗৃߚЎĀᷛ 2.1 ӓWindows䅵ㅫ఼ὖ䗄 ㄀  ゴӓ:LQGRZV䅵ㅫ఼ /Download at http://www.pin5i.com ៥Ӏ䳔㽕⊼ᛣ⫼᠋⚍ߏⱘ݋ԧ乎ᑣDŽ ᔧ⫼᠋ݡ⃵⚍ߏ䖤ㅫヺ˄䴲Ā=ā˅ᯊˈ㋏㒳ህ䳔㽕ᇚ㒧ᵰᰒ⼎ࠄ᭛ᴀḚЁDŽ಴ℸ೼ᓔথ䅵ㅫ఼ⱘᯊ׭ˈ ៥Ӏ䳔㽕⊼ᛣⱘᰃˈ՟བᏆ㒣⚍ߏњᶤϾ᭄ᄫˈݡ⚍ߏ䖤ㅫヺˈ䙷М⿟ᑣ䳔㽕䆄ᔩПࠡ䗝⚍ߏⱘ᭄ᄫˈ ᴀゴⱘ䅵ㅫ఼ᑊ≵᳝໡ᴖⱘ⌕⿟ˈা䳔㽕ㅔऩⱘ᪡԰ˈ䖨ಲ䅵ㅫ㒧ᵰㄝDŽ೼ᅲ⦄䅵ㅫ఼ⱘ䖛⿟Ёˈ ᥹ϟᴹݡ㒻㓁ㄝᕙ⫼᠋ⱘ䕧ܹDŽ ĀCEāǃĀCāㄝ᪡԰ᣛҸˈ㗠Ϩ≵᳝݇䯁䅵ㅫ఼ˈህ䖨ಲ䅵ㅫ㒧ᵰᑊᰒ⼎ˈབᵰϡᰃˈ߭ϡ䅵ㅫ㒧ᵰDŽ ህᓔྟⲥ਀⫼᠋ⱘ哴ᷛࡼ԰ˈབᵰ䕧ܹᰃ݇Ѣ䅵ㅫ㒧ᵰ៪㗙ĀMCāǃĀMRāǃĀMSāǃĀM+āǃĀBackspaceāǃ ⼎ⱘ᭄ᄫˈ䖬᳝⏙䰸Ϟ⃵㒧ᵰǃ⏙䰸᠔᳝㒧ᵰǃ䗔Ḑㄝ᪡԰DŽҢ೒ 2.2 Ёৃҹⳟߎˈ䅵ㅫ఼ᠧᓔПৢˈ 㒧ᵰˈ䰸໪ˈ䖬ৃҹ⚍ߏĀMCāǃĀMRāǃĀMSāǃĀM+ā䬂এ⏙䰸ǃ䇏পǃֱᄬǃ㌃ࡴ䅵ㅫᰒ⼎ḚЁᰒ Ā-āǃĀ*āǃĀ/ā䬂এ䕧ܹ㽕䖤ㅫ㒧ᵰⱘㅫᴃᓣˈݡ䗮䖛⚍ߏĀ=āǃĀsqrtāǃĀ1/xāㄝ䬂এⳈ᥹㦋প䅵ㅫ ⫼᠋ᠧᓔ䅵ㅫ఼ৢˈ೼≵᳝݇䯁䅵ㅫ఼Пࠡˈৃҹ䗮䖛哴ᷛ⚍ߏĀ1āࠄĀ9ā᭄ᄫ䬂੠⚍ߏĀ+āǃ 2.2 ⌕⿟ᦣ䗄 GridLayout 䴲ᐌ䗖ড়DŽᴀゴ䅵ㅫ఼ⱘ⬠䴶Ꮧሔᑊϡ໡ᴖˈ಴ℸ೼䖭䞠ϡݡ䆺㒚ᦣ䗄DŽ Ꮧሔ໘⧚఼ᴹ䖯㸠㔥Ḑ⢊Ꮧሔˈ⬅Ѣ৘Ͼ䅵ㅫ఼ᣝ䪂䛑ᰃҹ㔥Ḑ⢊ߚᏗ೼⬠䴶Ёⱘˈ಴ℸՓ⫼ ㅫ఼ᣝ䪂˄JButton ᇍ䈵˅⏏ࡴࠄ䖭Ͼ JPanel ЁDŽ೼⏏ࡴ䅵ㅫ఼ᣝ䪂ⱘᯊ׭ˈ៥ӀৃҹՓ⫼ GridLayout ᄬᬒϔϾ JPanel ᇍ䈵ˈ៥Ӏ䳔㽕Ў䖭Ͼ JPanel ᇍ䈵䖯㸠Ꮧሔˈᇚ᭛ᴀḚ˄JTextField ᇍ䈵˅Ϣ৘Ͼ䅵 ⬠䴶ЁՓ⫼ⱘ Swing 㒘ӊⳌᇍㅔऩˈᭈϾ໻にষৃҹⳟ԰ϔϾ JFrame ᇍ䈵ˈ೼ JFrame ᇍ䈵Ёˈ 2.1.2 ⬠䴶䇈ᯢ ǃ㌃ࡴ៪㗙⏙䰸㒧ᵰˈৃҹ䗮䖛ᬍব៪㗙䇏প៥Ӏ᠔ֱᄬⱘؐᴹᅲ⦄DŽټᑨⱘ㒧ᵰˈབᵰ䳔㽕䅵পǃᄬ ᴀḚЁDŽ݊Ҫⱘ䅵ㅫ఼ࡳ㛑䛑ৃҹ䗮䖛䅵ㅫ఼ݙ䚼ⱘ⿟ᑣᅲ⦄ˈ՟བՓ⫼ᶤϾᄫヺІ៪㗙᭄ᄫᴹֱᄬⳌ ৃҹՓ⫼ 1 ᴹ䰸ҹॳྟⱘ᭄ᄫDŽᔧ⫼᠋䳔⚍ߏĀ=āⱘᯊ׭ˈ䅵ㅫ఼ህ䳔㽕ᇚ᳔㒜ⱘ䅵ㅫ㒧ᵰᰒ⼎ࠄ᭛ ᭄צˈ⫣ಯ߭䖤ㅫ೼⿟ᑣЁৃҹⳈ᥹Փ⫼ Java 䖤ㅫヺᅲ⦄ˈᅲ⦄ᓔᮍৃҹ䇗⫼ Math ㉏ⱘ sqrt ᮍ ‰ Փ⫼ĀCEā੠ĀCāҷ㸼⏙䰸Ϟ⃵䅵ㅫ㒧ᵰ੠⏙䰸᠔᳝䅵ㅫ㒧ᵰDŽ ‰ Փ⫼ĀBackspaceāヺোҷ㸼䗔ḐDŽ 㒧ᵰDŽټ㒧ᵰ੠㌃ࡴᄬ ټ㒧ᵰǃֱᄬᄬټ㒧ᵰǃ䇏পᄬټ‰ Փ⫼ヺোĀMCāǃĀMRāǃĀMSāǃĀM+āҷ㸼⏙䰸ᄬ 䗔Ḑ᪡԰ˈ䖬ৃҹ⏙䰸Ϟ⃵䅵ㅫ㒧ᵰ៪㗙ܼ䚼㒧ᵰ˖خѺ᳝ᇍ᭄ᄫᰒ⼎ḚЁⱘ᭄ᄫ ǃ䇏পǃ㌃ࡴǃ⏙䰸ㄝ᪡԰ˈټᄬخ䰸њ⫼Ѣ᭄ᄺ䖤ㅫⱘヺোˈWindows 䅵ㅫ఼䖬ᦤկᇍ䅵ㅫ㒧ᵰ Ā%āোˈབᵰՓ⫼ℸヺোˈ㄀ѠϾ᪡԰᭄ህㄝѢϸ᭄ⳌЬݡ䰸ҹ 100DŽ ‰ /Download at http://www.pin5i.com ࡳ㛑㉏ˈࡳ㛑㉏Փ⫼њ MyMath Ꮉ݋㉏ˈϟ䴶ゴ㡖ᇚᇍ䖭ѯ䅵ㅫ఼ⱘⳌ݇㉏԰䆺㒚ҟ㒡DŽ Ң೒Ёৃҹⳟߎˈ៥Ӏⱘ⬠䴶㉏㒻ᡓњ java.swing.JFrame ㉏ˈ䅵ㅫ఼㉏Փ⫼њ⬠䴶㉏ˈ⬠䴶㉏Փ⫼њ Ь䰸ˈ䅵ㅫ఼㉏(Cal)ü⫼Ѣᠧᓔ䅵ㅫ఼ˈ䅵ㅫ఼Ё৘Ͼ㉏ⱘ݇㋏བ೒ 2.3 ᠔⼎ˈޣ⫼Ѣ໘⧚໻ൟ᭄ᄫⱘࡴ 㛑㉏˄CalService˅üЏ㽕⫼Ѣᅠ៤䅵ㅫ఼Ёⱘ䘏䕥ࡳ㛑ˈ䅵ㅫᎹ݋㉏˄MyMath˅üℸ㉏ᰃᎹ݋㉏ˈ ᴀゴЏ㽕䆒䅵ಯϾ㉏ᴹᅠ៤䅵ㅫ఼ⱘࡳ㛑ˈ⬠䴶㉏˄CalFrame˅üЏ㽕⫼ᴹᰒ⼎䅵ㅫ఼⬠䴶ˈࡳ 䈵Ёˈ䙷Мৃҹݡᓎゟ䖭ѯᇍ䈵ᴹᅠ៤䳔㽕ᅲ⦄ⱘࡳ㛑៪㗙᪡԰DŽ ᇣ㡖Ёা䆆㾷߯ᓎ䖭ϝϾ෎ᴀⱘ㉏ˈབᵰ೼ᓔথⱘ䖛⿟থ⦄ৃҹᇚϔѯ㸠Ў៪㗙ሲᗻᬒ㕂ࠄϔϾᮄⱘᇍ ǃЬǃ䰸ⱘ෎ᴀ䅵ㅫ㉏ˈ䖬䳔㽕ϔϾ䋳䋷໘⧚䅵ㅫࡳ㛑ⱘϮࡵ㉏DŽᴀޣ䳔㽕ᓎゟϔϾϧ䮼䋳䋷໘⧚ࡴǃ ᅲ⦄ϔϾ䅵ㅫ఼ˈ៥Ӏ䳔㽕ᓎゟϔ㋏߫ⱘᇍ䈵ᴹᅲ⦄ˈ՟བˈ䅵ㅫ⬠䴶៥Ӏ㽕ᓎゟϔϾ⬠䴶㉏ˈ䖬 2.3 ᓎゟ䅵ㅫ఼ᇍ䈵 ೒ 2.2 䅵ㅫ⌕⿟ /Download at http://www.pin5i.com BigDecimal ᇍ䈵˖ 䖤ㅫⱘ䆱ˈ䳔㽕ᇚ double ㉏ൟ䕀ᤶ៤ϔϾ BigDecimal ᇍ䈵ˈ៥ӀৃҹՓ⫼ҹϟҷⷕᴹ߯ᓎϔϾ ⫼DŽ೼ᅲ⦄ MyMath ⱘ䖛⿟Ё䳔㽕⊼ᛣⱘᰃˈ䖭޴Ͼಯ߭䖤ㅫᮍ⊩ˈখ᭄䛑ᰃ double ㉏ൟⱘˈ㽕䖯㸠 MyMath ㉏ᦤկњ෎⸔ⱘಯ߭䖤ㅫᮍ⊩ˈ⬅Ѣ䆹㉏Ё᠔᳝ⱘᮍ⊩䛑ᰃ䴭ᗕⱘˈ಴ℸ໪⬠ৃҹⳈ᥹䇗 num1 ੠ num2ˈᑊ䖨ಲ double ㉏ൟDŽ ‰ static double divide ( double num1, double num2 )ˈ䰸⊩ˈՓ⫼ᴹ䅵ㅫ㒧ᵰⱘ᭄ᄫᰃᇕ㺙ৢⱘ ⱘ num1 ੠ num2ˈᑊ䖨ಲ double ㉏ൟDŽ ‰ static double multiply ( double num1, double num2 )ˈЬ⊩ˈՓ⫼ᴹ䅵ㅫ㒧ᵰⱘ᭄ᄫᰃᇕ㺙ৢ ⱘ num1 ੠ num2ˈᑊ䖨ಲ double ㉏ൟDŽ Փ⫼ᴹ䅵ㅫ㒧ᵰⱘ᭄ᄫᰃᇕ㺙ৢˈ⫣ޣˈ( ‰ static double subtract ( double num1, double num2 num1 ੠ num2ˈᑊ䖨ಲ double ㉏ൟDŽ ‰ static double add( double num1, double num2 )ˈࡴ⊩ˈՓ⫼ᴹ䅵ㅫ㒧ᵰⱘ᭄ᄫᰃᇕ㺙ৢⱘ ⫼ˈ䖭Ͼ㉏ࣙ৿ҹϟᮍ⊩˖ 㸠䍙໻᭄ᄫⱘಯ߭䖤ㅫDŽЎњᮍ֓䇗⫼ˈℸ㉏ⱘᮍ⊩ܼ䚼䛑ᰃ䴭ᗕᮍ⊩ˈৃҹⳈ᥹⫼Ā㉏ৡ.ᮍ⊩ৡā䇗 Ьˈ䰸ᮍ⊩ⱘ㉏ˈℸ㉏Փ⫼ BigDecimal ᴹᇕ㺙෎ᴀ㉏ൟˈ೼ϡᤳ༅㊒ᑺⱘৠᯊˈгৃҹ䖯ˈޣˈњࡴ Փ⫼ floatˈdouble ϸ⾡⍂⚍෎ᴀ㉏ൟᴹ䖯㸠䅵ㅫˈᆍᯧᤳ༅㊒ᑺˈ᠔ҹˈ៥ӀՓ⫼ϔϾ㞾ᏅᅮН 2.3.1 MyMathᎹ݋㉏ ೒ 2.3 䅵ㅫ఼㉏೒ /Download at http://www.pin5i.com web ᑨ⫼Ёⱘ MVC ῵ᓣЁⱘ V˄㾚೒˅ˈᑊϡ໘⧚ӏࡵⱘϮࡵ䘏䕥ˈЏ㽕㘠䋷 ڣ⚍ߚথ໘⧚DŽ䖭ህ᳝ ⬅Ѣ CalFrame ᰃ⬠䴶㉏ˈ಴ℸ᠔䳔㽕䖯㸠ⱘϮࡵ໘⧚ᑊϡ໮ˈ᳈໮ⱘᰃⲥ਀⫼᠋ⱘ᪡԰ˈᑊ䖯㸠 ‰ JButton[] getNButton()ˈℸᮍ⊩㦋ᕫ䅵ㅫ఼ⱘ݊ᅗ᪡԰䬂DŽ ‰ JButton[] getRButton()ˈℸᮍ⊩㦋ᕫ䅵ㅫ఼ⱘ㒧ᵰ᪡԰䬂DŽ ᪡԰䬂DŽټ‰ JButton[] getMButton()ˈℸᮍ⊩㦋ᕫ䅵ㅫ఼ⱘᄬ ‰ JTextField getTextField()ˈ䖭Ͼᮍ⊩߱ྟ࣪䕧ܹḚDŽ Ⳉ᥹䖨ಲDŽ ‰ ActionListener getActionListener()ˈབᵰࡼ԰ⲥ਀఼Ўぎˈ߭߯ᓎϔϾˈᑊ䖨ಲˈབᵰϡЎぎˈ ‰ void initialize()ˈ߱ྟ࣪䅵ㅫ఼⬠䴶DŽ ϟᮍ⊩˖ 䇗⫼Ⳍᑨⱘᮍ⊩ˈЏ㽕ࣙ৿ҹخЁ৘Ͼ㒘ӊⱘԡ㕂ˈЎ㒘ӊ๲ࡴџӊⲥ਀఼ˈ⫼ᴹⲥ਀⫼᠋ⱘ᪡԰ˈᑊ CalFrame ㉏㒻ᡓ javax.swing.Jframe ㉏ˈЏ㽕ᰃ⫼Ѣ䅵ㅫ఼⬠䴶ⱘᅲ⦄ˈℸ㉏Ёˈᥦ⠜њ䅵ㅫ఼ 2.3.3 CalFrame㉏ ⱘᅲ⦄㉏ˈⳈ᥹䇗⫼᥹ষᮍ⊩DŽ ᭄ㄝᅲ⦄ˈ✊ৢ৥ callMethod ӴܹϡৠצЎ䆹᥹ষᦤկϡৠⱘᅲ⦄ˈ՟བ䅵ㅫᓔᮍǃ䅵ㅫˈމԧⱘᚙ ៤ϔϾ⢊ᗕ῵ᓣˈᇚ৘Ͼ䅵ㅫⱘᮍ⊩䛑ᢑ䈵៤ϔϾ䅵ㅫ᥹ষˈ䆹᥹ষᦤկϔϾ䅵ㅫⱘᮍ⊩ˈ✊ৢᣝ✻݋ خ᳈དⱘ⿟ᑣ㾷㗺ˈ៥Ӏৃҹᇚ䖭ѯخ⊩ህḍ᥂䖭ϾᄫヺІݡ䇗⫼䳔㽕ᠻ㸠ĀCEāⱘᮍ⊩DŽབᵰ䳔㽕 ⱘᮍ⊩ˈḍ᥂খ᭄Ёⱘ cmd ؐ䖯㸠ߚথ໘⧚ˈ՟བ䇗⫼䆹ᮍ⊩ᯊᇚĀCEāᄫヺІ԰Ў cmdˈ䙷М䆹ᮍ CalService ㉏Ёⱘ৘Ͼᮍ⊩䛑ᰃ⫼Ѣ໘⧚䅵ㅫⱘ䘏䕥ˈ݊Ё callMethod ᮍ⊩ৃҹⳟ԰ЁϔϾЁ䕀 ‰ String clear( String text)ˈ⏙䰸Ϟ⃵䅵ㅫ㒧ᵰDŽ ‰ String clearAll()ˈ⏙䰸᠔᳝䅵ㅫ㒧ᵰDŽ ᳝݇ⱘࡳ㛑DŽټ ‰ String mCmd( String cmd, String text )ˈ⫼ᴹᅲ⦄ĀM+āǃĀMCāǃĀMRāǃĀMSāϢᄬ ‰ String backSpace( String text )ˈߴ䰸᳔ৢϔϾᄫヺˈᑊ䖨ಲ㒧ᵰDŽ 䗑ࡴࠄৢ䴶ˈᑊᇕ㺙៤ᄫヺІ䖨ಲDŽ ‰ String catNum( String cmd, String text )ˈ䖲᥹䕧ܹⱘ᭄ᄫˈ↣⃵⚍ߏ᭄ᄫˈህᡞᡞᮄࡴⱘ᭄ᄫ Пˈ߭䖨ಲℷ᭄ⱘ᭄ᄫᄫヺІDŽ ‰ String setNegative( String text )ˈ䆒㕂ℷ䋳᭄ˈᔧ text ᰃℷ᭄ᯊˈ䖨ಲ䋳᭄ⱘ᭄ᄫᄫヺІˈড ‰ String setOp( String cmd , String text )ˈ䆒㕂᪡԰ヺোDŽ ‰ String sqrt( String text )ˈ⫼ᴹ䅵ㅫᓔᮍˈᑊ䖨ಲᇕ㺙៤ String ݙൟⱘ㒧ᵰDŽ ᭄ˈᑊ䖨ಲᇕ㺙៤ String ݙൟⱘ㒧ᵰDŽצ‰ String setReciprocal( String text )ˈ⫼ᴹ䅵ㅫ 䖤ㅫˈབᵰ᳝ˈ֓ࡴϞএDŽ ݙൟⱘ㒧ᵰDŽখ᭄ text ᰃᰒ⼎ḚЁⱘ᭄ᄫݙᆍˈboolean ㉏ൟⱘখ᭄ isPercent ҷ㸼ᰃ৺᳝"%" ǃЬǃ䰸⊩ˈᑊ䖨ಲᇕ㺙៤ Stringޣ‰ String cal( String text , boolean isPercent )ˈ⫼ᴹ䅵ㅫࡴǃ ‰ String callMethod( String cmd , String text )ˈ䇗⫼ᮍ⊩ᑊ䖨ಲ䅵ㅫ㒧ᵰDŽ ಲˈᑊϨˈӮ䆄ᔩ䅵ㅫ఼ⱘ⢊ᗕ˄⫼᠋ⱘϞϔℹ᪡԰˅DŽࣙ৿ҹϟᮍ⊩˖ CalService ㉏Џ㽕ᰃ⫼ᴹ໘⧚䅵ㅫ఼ⱘϮࡵ䘏䕥ˈ⫼᠋೼᪡԰䅵ㅫ఼ᯊˈℸ㉏ᇚ䅵ㅫ㒧ᵰˈᑊϨ䖨 2.3.2 CalService㉏ new BigDecimal(String.valueOf(number)); /Download at http://www.pin5i.com public static double divide(double num1, double num2) { 䰸⊩˖ } return first.multiply(second).doubleValue(); BigDecimal second = getBigDecimal(num2); BigDecimal first = getBigDecimal(num1); public static double multiply(double num1, double num2) { Ь⊩˖ } return first.subtract(second).doubleValue(); BigDecimal second = getBigDecimal(num2); BigDecimal first = getBigDecimal(num1); public static double subtract(double num1, double num2) { ˖⫣ޣ { ;() return first.add(second).doubleValue BigDecimal second = getBigDecimal(num2); BigDecimal first = getBigDecimal(num1); //䇗⫼Ꮉ݋ᮍ⊩ᇚ double 䕀ᤶ៤ BigDecimal public static double add(double num1, double num2) { ࡴ⊩˖ ҷⷕ⏙ऩ˖code\cal\src\org\crazyit\cal\MyMath.java ⦄њˈ಴ℸৃҹⳈ᥹䇗⫼䆹㉏ⱘⳌᑨᮍ⊩ेৃᅲ⦄ˈҹϟҷⷕߚ߿ᅲ⦄ಯ߭䖤ㅫDŽ ǃЬǃ䰸↨䕗ㅔऩˈ⬅Ѣ BigDecimal Ꮖ㒣Ў៥Ӏᅲޣ㓪ݭњ double 䕀ᤶⱘᎹ݋ᮍ⊩ৢˈᅲ⦄ࡴǃ 2.4.1 ᅲ⦄ಯ߭䖤ㅫ 䕀ᤶ៤ BigDecimal ᇍ䈵ˈ✊ৢݡ䖯㸠݋ԧⱘ䖤ㅫDŽ ᦤկњ䖭ϾᎹ݋ᮍ⊩ৢˈ៥Ӏৃҹ೼݊Ҫⱘ䅵ㅫᮍ⊩ЁՓ⫼䖭ϾᎹ݋ᮍ⊩ˈ䗝ᢽᇚ double ⱘখ᭄ } return new BigDecimal(number); private static BigDecimal getBigDecimal(double number) { */ * @return * @param number * ЎϔϾ double ㉏ൟⱘ᭄ᄫ߯ᓎ BigDecimal ᇍ䈵 /** ҷⷕ⏙ऩ˖code\cal\src\org\crazyit\cal\MyMath.java ҹϟҷⷕḍ᥂ϔϾ double ㉏ൟ䕀ᤶ៤ϔϾ BigDecimalDŽ double ㉏ߥⱘˈ಴ℸ៥ӀৃҹᦤկϔϾᎹ݋ᮍ⊩ᴹᇚ double 䕀ᤶ៤ BigDecimal ㉏ൟDŽ ⱘᯊ׭ˈ䛑Փ⫼ BigDecimal ᇍ䈵䖯㸠䅵ㅫDŽ⬅Ѣ៥ӀᅮН MyMath ᮍ⊩ⱘᯊ׭ˈ᠔᳝ᮍ⊩ⱘখ᭄䛑ᰃ ǃЬǃ䰸ಯ߭䖤ㅫˈ៥ӀᏆ㒣⹂ᅮњᅲ⦄䖭ಯϾᮍ⊩ޣMyMath ᰃϔϾᎹ݋㉏ˈЏ㽕⫼Ѣ໘⧚ࡴǃ 2.4 MyMathᎹ݋㉏ᅲ⦄ ೼ᅲ䰙ᓔথЁˈ៥Ӏৃҹᇚ䖭ѯⲥ਀఼԰Ў MVC ῵ᓣЁⱘ C˄᥻ࠊ఼˅ᦤপࠄ঺໪ⱘ㉏ЁDŽ ᰃᰒ⼎Ⳍᑨⱘ᭄᥂DŽ೼ᴀゴЁˈCalFrame ࣙᣀњϔѯⲥ਀఼ˈⲥ਀⬠䴶џӊᑊ䇗⫼Ⳍ݇ⱘϮࡵᮍ⊩ˈ /Download at http://www.pin5i.com 2.5 ᠔⼎DŽ ᘏԧᏗሔˈབ೒خ ᗻˈᇍѢ⬠䴶ⱘᏗሔг᳈ࡴഄⳈ㾖Ϣᮍ֓ˈ㾖ᆳℸ೒ˈ៥ӀৃҹՓ⫼ BorderLayout ⬅Ѣᣝ໪㾖Ⳍ䖥ⱘᮍᓣᡞ㒘ӊߚ៤њಯ䚼ߚˈህᮍ֓⿟ᑣЁᇍⳌৠሲᗻⱘ㒘ӊ㒳ϔഄ߯ᓎϢ䆒㕂ሲ ‰ ๲ࡴ᭄ᄫϢ݊ᅗ䖤ㅫヺDŽ ‰ ๲ࡴ㒧ᵰ᪡԰䬂DŽ ᪡԰䬂DŽټ‰ ๲ࡴᎺ䖍ᄬ ‰ ๲ࡴ䕧ܹϢ䅵ㅫ㒧ᵰᰒ⼎ḚDŽᇍᑨ೒ 2.4 ЁⱘᎺϞ㾦䙷䚼ߚDŽ ‰ 䆒㕂⠊にষ JFrame ᷛ乬ǃᏗሔㅵ⧚఼ǃᰃ৺ৃҹᬍবㄝሲᗻ DŽ 䅵ㅫ఼にষⱘ໻ὖ⌕⿟˖ ᅗにষ៪㗙㒘ӊⱘ⠊ᆍ఼ˈ߱ྟ࣪݊خℸ㉏ህᰃϔϾ JFrame˄㒻ᡓњ javax.swing.JFrame˅ˈ⫼ᴹ 2.5.1 ߱ྟ࣪⬠䴶˄initialize()ᮍ⊩˅ ೒ 2.4 Ꮧሔ ⦄ˈݡⳟ৘Ͼᇣ㒘ӊⱘᅲ⦄DŽЎњᮍ֓Ꮧሔˈ៥ӀᣝⳌ䖥ⱘ໪㾖ᡞ䅵ㅫ఼ߚЎಯϾ䚼ߚˈ㾕೒ 2.4˖ ⳟᘏԧⱘᥦ⠜ᅲܜˈjava.awt.GridLayout 䖯㸠ᏗሔDŽ೼䖭䞠ˈ៥ӀՓ⫼Ā㞾ϟ㗠ϟāⱘᮍ⊩এ㾖ᆳℸ㉏ javax.swing.JButton˄ᣝ䪂˅ˈjavax.swing.JTextField˄䕧ܹḚ˅ˈᑊՓ⫼ java.awt.BorderLayout ੠ 䖭䞠ᅲ⦄䅵ㅫ఼ⱘ⬠䴶ˈᰃ⫼ java ⱘ Swing ᅲ⦄ⱘˈЏ㽕⫼ࠄⱘ㉏᳝ javax.swing.JFrame˄にষ˅ˈ 2.5 䅵ㅫ఼Џ⬠䴶 д䆹㉏ⱘ䆺㒚Փ⫼DŽ 䅵ㅫᮍ⊩ˈৃҹ䅽៥Ӏᕜᮍ֓ⱘ䖯㸠᭄ᄺ䖤ㅫˈ䰸ᴀゴҟ㒡ⱘᮍ⊩៥ˈ䇏㗙ৃҹᶹ䯙 Java ⱘ API ᴹᄺ ಯϾᮍ⊩䛑ᰃ䇗⫼њ BigDecimal ⱘᮍ⊩ᴹᅲ⦄ˈJava ⱘ BigDecimal ㉏Ў៥Ӏᦤկњ䆌໮ᔎ໻ⱘ } .doubleValue(); return first.divide(second, DEFAULT_SCALE, BigDecimal.ROUND_HALF_UP) BigDecimal second = getBigDecimal(num2); BigDecimal first = getBigDecimal(num1); /Download at http://www.pin5i.com //๲ࡴ㒧ᵰ᪡԰䬂 ݡᡞ䖭Ͼ JPanel ࡴࠄ JFrame Ⳍᑨⱘԡ㕂ˈ݋ԧᅲ⦄ⱘҷⷕབϟ˖ ㋴ࡴࠄϔϾᮄᓎⱘ JPanel Ёˈ᳔ৢܗᄬ JButton ᇍ䈵ⱘ᭄㒘ˈ㦋প᭄㒘ৢˈ䘡ग़᭄㒘ˈᑊᡞ᭄㒘Ёⱘ ๲ࡴ㒧ᵰ᪡԰䬂ˈ䖭ѯ㒧ᵰ᪡԰䬂ࣙᣀ˖BackˈCEˈCDŽ䗮䖛ᴀ㉏ⱘ getRButton()ᮍ⊩㦋পϔϾֱ for( JButton b : mButton ) panel1.add(b); //䗁ҷ๲ࡴᣝ䪂 panel1.setLayout( new GridLayout( 5, 1, 0, 5 ) ); //䆒㕂Ꮧሔㅵ⧚఼ JPanel panel1 = new JPanel(); //ᮄᓎϔϾ panelˈ⫼Ѣᬒ㕂ᣝ䪂 JButton[] mButton = getMButton(); ᪡԰䬂ټ //๲ࡴᎺ䖍ᄬ JPanel Ёˈ᳔ৢݡᡞ䖭Ͼ JPanel ࡴࠄ JFrame ⱘⳌᑨԡ㕂˖ ㋴ࡴࠄϔϾᮄᓎⱘܗgetMButton ᮍ⊩៥Ӏᇚ೼ 2.5.2 Ёᅲ⦄DŽ㦋প᭄㒘ৢˈ䘡ग़᭄㒘ˈᑊᡞ᭄㒘Ёⱘ ᪡԰䬂ˈᴀ㉏䳔㽕䗮䖛 getMButton()ᮍ⊩㦋পϔϾֱᄬ JButton ᇍ䈵ⱘ᭄㒘ˈټ๲ࡴᎺ䖍ᄬ panel.setPreferredSize( new Dimension( PRE_WIDTH, PRE_HEIGHT ) ); panel.add( getTextField(), BorderLayout.NORTH ); panel.setLayout( new BorderLayout(10,1) ); JPanel panel = new JPanel(); //๲ࡴ䅵ㅫ䕧ܹḚ panel Ёⱘ NORTH ԡ㕂Ё˖ ๲ࡴ䕧ܹϢ㒧ᵰᰒ⼎ⱘ JTextField 䕧ܹḚˈ䖭䞠䇗⫼ᴀ㉏ⱘ getTextField()ᮍ⊩㦋পˈᑊᡞᅗࡴܹ this.setResizable( false ); //䆒㕂Ўϡৃᬍব໻ᇣ this.setTitle("䅵ㅫ఼"); //䆒㕂にষⱘᷛ乬 ҹϟҷⷕ䆒㕂⠊にষ JFrame ᷛ乬੠䆒㕂ᰃ৺ৃҹᬍব໻ᇣⱘሲᗻDŽ ೒ 2.5 Ꮧሔㅵ⧚఼ /Download at http://www.pin5i.com b.setForeground(Color.blue); } else { b.setForeground(Color.red); if (Arrays.binarySearch(redButton, nOp[i]) >= 0) { // བᵰ᪡԰ヺ೼ redButton ߎ⦄ Arrays.sort(redButton); // ᇍ redButton ᥦᑣˈᠡৃҹՓ⫼ binarySearch ᮍ⊩ b.addActionListener(getActionListener()); // Ўᣝ䪂๲ࡴџӊ JButton b = new JButton(this.nOp[i]); // ᮄᓎᣝ䪂 for (int i = 0; i < this.nOp.length; i++) { JButton[] result = new JButton[nOp.length]; String[] redButton = { "/", "*", "-", "+", "=" }; // 䖭Ͼ᭄㒘ֱᄬ䳔㽕䆒㕂Ў㑶㡆ⱘ᪡԰ヺ private JButton[] getNButton() { ҷⷕ⏙ऩ˖code\cal\src\org\crazyit\cal\CalFrame.java Џ㽕ᅲ⦄䅵ㅫ఼⬠䴶ⱘ getNButton ᮍ⊩ेৃDŽҹϟᰃ䆹ᮍ⊩ⱘᅲ⦄DŽ 䖤ㅫ䬂Џ㽕ࣙᣀ᭄ᄫ䬂Ϣ෎ᴀ䖤ㅫ䬂ˈ᭄ᄫ䬂Ң 0 ࠄ 9ˈ෎ᴀ䖤ㅫ䬂ࣙᣀᓔᮍǃℷ䋳ǃᇣ᭄⚍ㄝ䬂ˈ 2.5.2 ߯ᓎ䖤ㅫ䬂 ҹϞ᠔᳝ⱘҷⷕഛ೼ code\cal\src\org\crazyit\cal\CalFrame.java ЁDŽ ᅲ⦄ˈϟ䴶ᇚҟ㒡བԩᅲ⦄䖭ϝϾᮍ⊩DŽ ✊ৢݡᇍ䖭ѯ᭄㒘䖯㸠䘡ग़ˈᇚ↣ϔϾ JButton ࡴܹࠄ⬠䴶ЁDŽ䖭ϔϾ䖨ಲ JButton ᭄㒘ⱘᮍ⊩ᑊ≵᳝ ೼ᴀᇣ㡖Ёˈ៥Ӏ䗮䖛 getMButtonǃgetRButton ੠ getNButton ᮍ⊩ᴹ䖨ಲϡৠⱘ JButton ᭄㒘ˈ this.add(panel); … //ᡞᮄ๲ࡴⱘ䴶ᵓࡴࠄ frame for( JButton b : nButton ) panel22.add(b); //䗁ҷ๲ࡴᣝ䪂 panel22.setLayout( new GridLayout( 4, 5, 3, 5 ) ); //䆒㕂Ꮧሔㅵ⧚఼ JPanel panel22 = new JPanel(); //ᮄᓎϔϾ panelˈ⫼Ѣᬒ㕂ᣝ䪂 JButton[] nButton = getNButton(); //๲ࡴ᭄ᄫϢ݊ᅗ䖤ㅫヺ ᇍ䈵ࡴܹࠄⳌᑨⱘ JPanel Ёˈࡴܹࠄ JPanel ᑊ䆒㕂ⳌᑨᏗሔⱘҷⷕབϟ˖ 䬂ˈ៥Ӏৠḋⱘ䗮䖛ϔϾ getNButton ᮍ⊩ᴹ䖨ಲ䖭ѯ᪡԰䬂ᇍᑨⱘ JButton ᇍ䈵ˈ᳔ৢᇚ䖭ѯ JButton ᥹ϟᴹᇚ݊Ҫⱘᣝ䬂ࡴܹࠄ⬠䴶ⱘ JPanel ᇍ䈵Ёˈ䖭ѯ᪡԰䬂Џ㽕ࣙᣀ᭄ᄫ䬂੠݊Ҫⱘϔѯ䖤ㅫ for( JButton b : rButton ) panel21.add(b); //䗁ҷ๲ࡴᣝ䪂 panel21.setLayout( new GridLayout( 1, 3, 3, 3 ) ); //䆒㕂Ꮧሔㅵ⧚఼ JPanel panel21 = new JPanel(); //ᮄᓎϔϾ panelˈ⫼Ѣᬒ㕂ᣝ䪂 panel2.setLayout( new BorderLayout(1, 5) ); JPanel panel2 = new JPanel(); JButton[] rButton = getRButton(); /Download at http://www.pin5i.com //getRButton private String[] mOp = { "MC", "MR", "MS", "M+" }; //getMButton ੠ getRButton ߯ᓎⱘ᪡԰䬂བϟ˖ getMButton ߯ᓎⱘᰃ⬠䴶Ꮊջⱘ᪡԰䬂ˈgetRButton ߯ᓎⱘᰃ䖤԰䬂Ϟ䴶ⱘ᪡԰䬂ˈgetMButton } return result; } result[i] = b; b.setForeground(Color.red); // 䆒㕂ᣝ䪂买㡆 b.addActionListener(getActionListener()); // Ўᣝ䪂๲ࡴџӊ JButton b = new JButton(this.rOp[i]); // ᮄᓎᣝ䪂 for (int i = 0; i < this.rOp.length; i++) { JButton[] result = new JButton[rOp.length]; private JButton[] getRButton() { } return result; } result[i + 1] = b; b.setForeground(Color.red); // 䆒㕂ᣝ䪂买㡆 b.addActionListener(getActionListener()); // Ўᣝ䪂๲ࡴџӊ JButton b = new JButton(this.mOp[i]); // ᮄᓎᣝ䪂 for (int i = 0; i < this.mOp.length; i++) { result[0] = getButton(); JButton[] result = new JButton[mOp.length + 1]; private JButton[] getMButton() { ҷⷕ⏙ऩ˖code\cal\src\org\crazyit\cal\CalFrame.java ៥Ӏ䳔㽕ᅲ⦄ getMButton ੠ getRButton ᮍ⊩ˈҹϟᰃ䖭ϸϾᮍ⊩ⱘ݋ԧᅲ⦄DŽ ᪡԰ᣝ䬂ⱘ߯ᓎϢ䖤ㅫ䬂ⱘ߯ᓎ෎ᴀϔ㟈ˈাᰃ᠔᳝ⱘᣝ䬂ⱘᄫԧ䛑ᖙ乏ᰃ㑶㡆ⱘˈ߯ᓎ᪡԰ᣝ䪂ˈ 2.5.3 ߯ᓎ᪡԰ᣝ䬂 "%", "1", "2", "3", "-", "1/x", "0", "+/-", ".", "+", "=" }; private String[] nOp = { "7", "8", "9", "/", "sqrt", "4", "5", "6", "*", ҷⷕЁՓ⫼њϔϾ nOp ⱘᄫヺІ᭄㒘ᴹֱᄬ䳔㽕߯ᓎⱘᣝ䬂ˈ䆹᭄㒘ࣙ৿ⱘݙᆍབϟ˖ setForeground ᮍ⊩ᴹ䆒㕂䆹ᣝ䪂ⱘᄫԧ买㡆DŽ೼ҷⷕЁ៥Ӏϡ㛑ⳟࠄ䆹ᮍ⊩ᐂ៥Ӏ߯ᓎњાѯᣝ䬂ˈ ㋴ˈህ䳔㽕䇗⫼ JButton ⱘܗ᭄㒘ᯊˈህ䳔㽕԰߸ᮁˈབᵰᣝ䬂᭄㒘䞠䴶ᄬ೼㑶㡆ᣝ䬂᭄㒘ⱘᶤϾ ҹϞҷⷕ䳔㽕⊼ᛣⱘᰃˈ៥Ӏ䳔㽕ᦤկϔϾ㑶㡆ᣝ䬂ⱘᄫヺІ᭄㒘ˈ೼䘡ग़᠔᳝ⱘ䳔㽕߯ᓎⱘᣝ䬂 } return result; } result[i] = b; { /Download at http://www.pin5i.com } textField.setText( result ); if( result != null ) { ᳔ৢᡞ䅵ㅫ㒧ᵰ䆒㕂ࠄ㒧ᵰ᭛ᴀᰒ⼎ḚЁˈᰒ⼎㒭Փ⫼㗙DŽ } button.setText("M"); } else if( cmd.indexOf("M") == 0 && service.getStore() > 0 ) { button.setText(""); if( cmd.indexOf("MC") == 0 ) { ⴔՓ⫼ϔϟ windows 䅵ㅫ఼ⱘ䖭޴Ͼᣝ䪂ˈݡⳟ䖭䞠ህᕜᆍᯧ⧚㾷њDŽ 䆩ܜ㒧ᵰ໻Ѣ 0ˈህᡞᷛ䆄䆒㕂ЎĀMāˈ䖭䞠ᓘϡᯢⱑⱘ䇏㗙ˈৃҹټĀMSāˈĀMRāˈĀM+āˈᑊϨᄬ ᷛ䆄ˈབᵰᰃ⚍ߏĀMCāᣝ䪂ˈህᡞᷛ䆄䆒㕂Ўぎˈབᵰᰃ⚍ߏټ㒧ᵰ㉏ൟⱘᄬټݡ䆒㕂ᷛᖫᄬ result = service.callMethod( cmd, textField.getText() ); 䇗⫼ CalService ᅲ՟Ёⱘ callMethod ᮍ⊩এ໘⧚䅵ㅫˈᑊᡞ㒧ᵰ䖨ಲDŽˈܜ佪 actionPerformed ᮍ⊩Ё໘⧚Ϯࡵ䘏䕥DŽ actionPerformed( ActionEvent e )ᮍ⊩এ߯ᓎϔϾ java.awt.event.ActionListener ㉏ൟⱘݙ䚼㉏ˈᑊ೼ ҢϞ䴶ҷⷕЁৃҹⳟࠄˈ䖭䞠ᰃ䗮䖛ᅲ⦄ java.awt.event.ActionListener ᥹ষЁⱘ }; } } textField.setText( result ); if( result != null ) { //䆒㕂䅵ㅫ㒧ᵰ } button.setText("M"); } else if( cmd.indexOf("M") == 0 && service.getStore() > 0 ) { button.setText(""); if( cmd.indexOf("MC") == 0 ) { //໘⧚ button ⱘᷛ䆄 } System.out.println( e1.getMessage() ); } catch( Exception e1 ) { result = service.callMethod( cmd, textField.getText() ); //䅵ㅫ᪡԰㒧ᵰ try { String result = null; String cmd = e.getActionCommand(); public void actionPerformed( ActionEvent e ) { actionListener = new ActionListener(){ ҷⷕ⏙ऩ˖code\cal\src\org\crazyit\cal\CalFrame.java ᑊ೼ℸᮍ⊩Ё໘⧚Ϯࡵ䘏䕥ˈݡᡞ᭄᥂䖨ಲᰒ⼎㒭⫼᠋DŽ㾕ҹϟҷⷕDŽ ᥹ষЁⱘ actionPerformed( ActionEvent e )ᮍ⊩ˈᔧⲥ਀఼ⲥ਀ࠄ⫼᠋ⱘ᪡԰ᯊˈӮ㞾ࡼ䇗⫼ℸᮍ⊩ˈ ᑨ⫼᠋ⱘ哴ᷛ᪡԰DŽ៥ӀՓ⫼ java.awt.event.ActionListener ᥹ষᴹ߯ᓎϔϾџӊⲥ਀఼ˈЏ㽕ᰃᅲ⦄ ೼Ϟϔ㡖Ёˈ៥Ӏ⊼ᛣࠄ⿟ᑣЎ JButton ㉏ൟⱘ㒘ӊ๲ࡴњџӊⲥ਀఼ˈ䖭Ͼџӊⲥ਀఼ᰃ⫼ᴹડ 2.5.4 ๲ࡴџӊⲥ਀఼ ㋴ৢˈ៥Ӏৃҹ䖤㸠䅵ㅫ఼ˈ݋ԧⱘᬜᵰབ೒ 2.4 ᠔⼎DŽܗ߯ᓎᅠ⬠䴶 private String[] rOp = { "Back", "CE", "C" }; /Download at http://www.pin5i.com this.firstNum = text; //䆒㕂㄀ϔϾ᪡԰᭄ⱘؐ this.lastOp = cmd; //ᇚℸ᪡԰ヺো䆒㕂ЎϞ⃵ⱘ᪡԰ public String setOp( String cmd , String text ) { ҷⷕ⏙ऩ˖code\cal\src\org\crazyit\cal\CalService.java ᠋ℷ㽕䖯㸠䅵ㅫⱘ㉏ൟˈ㾕ҹϟҷⷕDŽ ᔧ⫼᠋⚍ߏĀ+-*/ā˄ಯ߭䖤ㅫ˅ⱘᯊ׭ˈህᡞ lastOp 䆒㕂Ў݊ЁϔϾヺোˈ䖭Ͼব䞣⫼ᴹ䆄ᔩ⫼ } return result; } result = "0" + result; if( result.indexOf(".") == 0 ) { //བᵰ᳝.ᓔ༈ˈ߮೼ࠡ䴶㸹 0 } } result = text + cmd; //߮䖨ಲ㒧ᵰЎⳂࠡⱘ text ࡴϞᮄ⚍ߏⱘ᭄ᄫ } else { isSecondNum = false; //ᇚ isSecondNum ᷛᖫЎ false if( isSecondNum ) { if( !text.equals("0") ) { //བᵰⳂࠡⱘ text ϡㄝѢ 0 String result = cmd; public String catNum( String cmd, String text ) { ҷⷕ⏙ऩ˖code\cal\src\org\crazyit\cal\CalService.java 䖨ಲˈ㾕ҹϟҷⷕDŽ Ў㒧ᵰخᑊᡞ isSecondNum ᷛᖫЎ falseˈ⫼᠋㒻㓁䕧᭄ܹᄫⱘᯊ׭ˈህᡞ᭄ᄫ䗑ࡴࠄॳᴹ᭄ᄫⱘৢ䴶 Ў㒧ᵰ䖨ಲ˗བᵰᰃ㄀ѠϾˈⳈ᥹䖨ಲ㒧ᵰˈخϔϾˈህᡞ⫼᠋ᮄ䕧ܹⱘ᭄ᄫ䗑ࡴࠄॳᴹ᭄ᄫⱘৢ䴶ˈᑊ ߸ᮁᰃ㄀ϔϾ᪡԰᭄䖬ᰃ㄀ѠϾˈབᵰᰃ㄀ܜ೼⫼᠋䕧᭄ܹᄫⱘᯊ׭˄ࣙᣀĀ0123456789.ā˅ˈ佪 ᪡԰ˈisSecondNum ⫼ᴹ߸ᮁ⫼᠋ᰃ೼䕧ܹ㄀޴Ͼ᪡԰᭄DŽ এֱᄬ⫼᠋䕧ܹⱘ㄀ϔϾ᪡԰᭄ˈsecondNum এֱᄬ㄀ѠϾ᪡԰᭄ˈlastOp এֱᄬ䅵ㅫヺো៪㗙݊ᅗ ǃЬǃ䰸䅵ㅫヺোˈݡ䕧ܹ㄀ѠϾ᪡԰᭄ˈ᳔ৢ⚍Ā=āো䅵ㅫߎ㒧ᵰˈ᠔ҹ䖭ᯊ⫼ firstNumޣߏࡴǃ 䕧ܹ㄀ϔϾ᪡԰᭄ˈݡ⚍ܜ᠋⫼ᑨ䆹ᰃމǃЬǃ䰸⊩ⱘ䖛⿟Ёˈℷᐌⱘᚙޣ೼Փ⫼䅵ㅫ఼䅵ㅫࡴǃ 2.6.1 䅵ㅫಯ߭䖤ㅫ㒧ᵰ ҷ㸼ᰃ৺㄀ѠϾ᪡԰᭄DŽ ⱘ᪡԰, isSecondNumخҷ㸼㄀ϔϾ᪡԰᭄ˈsecondNum ҷ㸼㄀ѠϾ᪡԰᭄ˈlastOp ҷ㸼Ϟ⃵⫼᠋᠔ 䖤ㅫˈ᪡԰㒧ᵰㄝϮࡵDŽ᳝ಯϾ䞡㽕ⱘሲᗻ˖firstNumټᑨ⫼Ёⱘ໻䚼ߚϮࡵˈ݊Ёࣙᣀ᭄ᄫ䖤ㅫˈᄬ ೼ 2.3 ゴ㡖Ёˈ៥ӀᓎゟњϔϾ㉏ৡЎ CalService ⱘ㉏ᴹ໘⧚䅵ㅫ఼ⱘ䅵ㅫϮࡵˈ䆹㉏໘⧚њᭈϾ 2.6 䅵ㅫϮࡵ໘⧚ ⚍ߏ䛑Ӯᠻ㸠䆹ᮍ⊩ˈcallMethod ៥Ӏᑊ≵᳝ᦤկӏԩᅲ⦄ˈ೼ϟϔᇣ㡖ˈ៥Ӏᇚᅲ⦄䆹ᮍ⊩DŽ ೼ⲥ਀఼Ёˈ៥Ӏ䇗⫼њ CalServer ⱘ callMethod ᮍ⊩ᴹপᕫ᪡԰ⱘ㒧ᵰˈᤶ㿔Пˈ⬠䴶Ёⱘ↣⃵ /Download at http://www.pin5i.com } return firstNum; this.isSecondNum = true; //ᡞ isSecondNum ᷛᖫЎ true secondNum = secondNum == null ? text : secondNum; //㒭㄀ѠϾ᪡԰᭄䞡ᮄ䌟ؐ } MyMath.divide( Double.valueOf( firstNum ), secondResult ) ); firstNum = String.valueOf( else if( this.lastOp.equals("/") ) { } MyMath.multiply( Double.valueOf( firstNum ), secondResult ) ); firstNum = String.valueOf( } else if( this.lastOp.equals("*") ) { MyMath.subtract( Double.valueOf( firstNum ), secondResult ) ); firstNum = String.valueOf( else if( this.lastOp.equals("-") ) { } MyMath.add( Double.valueOf( firstNum ), secondResult ) ); firstNum = String.valueOf( if( this.lastOp.equals("+") ) { //ಯ߭䖤ㅫˈ䖨ಲ㒧ᵰ䌟㒭㄀ϔϾ᪡԰᭄ } , MyMath.divide( secondResult, 100 ) ); secondResult = MyMath.multiply( Double.valueOf( firstNum ) if( isPercent ) { //བᵰ᳝"%"᪡԰ˈ߭㄀ѠϾ᪡԰᭄ㄝѢϸ᭄ⳌЬݡ䰸ҹ 100 } return "0"; if( secondResult == 0 && this.lastOp.equals("/") ) { //བᵰ䰸᭄Ў 0ˈϡ໘⧚ : Double.valueOf( secondNum ).doubleValue(); ? Double.valueOf( text ).doubleValue() double secondResult = secondNum == null //߱ྟ࣪㄀ѠϾ᪡԰᭄ throws Exception { public String cal( String text , boolean isPercent ) ҷⷕ⏙ऩ˖code\cal\src\org\crazyit\cal\CalService.java བᵰ᳝䖭⾡᪡԰ˈ㄀ѠϾ᪡԰᭄ህㄝѢϸ᭄ⳌЬݡ䰸ҹ 100ˈ䇋ⳟҹϟҷⷕDŽ isPercent )ᮍ⊩ᅲ⦄њℸ䅵ㅫˈ⊼ᛣࠄ䖭Ͼᮍ⊩ⱘ㄀ѠϾখ᭄ isPercentˈ䖭ᰃ䅵ㅫ఼ⱘĀ%āো᪡԰ˈ ᳔ৢ⫼᠋⚍ߏĀ=āোᯊˈህᰃ⿟ᑣ䅵ㅫߎ᳔ৢ㒧ᵰⱘᯊ׭ˈℸ㉏ⱘ String cal( String text , boolean 䆒㕂Ўぎˈᑊᡞ isSecondNum 䆒㕂Ў trueˈҷ㸼ϟ⃵䕧᭄ܹᄫᯊˈ㽕⏙ぎ䕧ܹḚᑊ䞡ᮄ䕧ܹDŽ ೼Ϟ䴶ⱘҷⷕЁˈৃҹⳟࠄˈ䰸њ䆒㕂 lastOp ໪ˈ䖬ᡞ䕧ܹⱘ᭄ᄫ䆒㕂㒭 firstNumˈᡞ secondNum } return null; //䖨ಲぎؐ this.isSecondNum = true; //ᇚ isSecondNum ᷛᖫЎ true this.secondNum = null; ᇚ㄀ѠϾ᪡԰᭄䌟ؐЎぎ// /Download at http://www.pin5i.com if (text.equals("0")) { ᭄צ∖ // བᵰ text Ў 0ˈ߭ϡ public String setReciprocal(String text) { ҷⷕ⏙ऩ˖code\cal\src\org\crazyit\cal\CalService.java ᭄ᰃ䇗⫼ MyMath ⱘ divide ᮍ⊩এ䅵ㅫ 1 Ϣ᥹ᬊࠄⱘ᭄ؐⳌ䰸ⱘؐDŽצ { ;((( return String.valueOf(Math.sqrt(Double.valueOf(text // 䅵ㅫ㒧ᵰᑊ䖨ಲ this.isSecondNum = true; // ᇚ isSecondNum ᷛᖫЎ true public String sqrt(String text) { Ϩ䖨ಲ㒧ᵰ˖ ᭄ⱘ䅵ㅫᅲ⦄䛑↨䕗ㅔऩˈᓔᮍᰃⳈ᥹Փ⫼ Math ㉏ⱘ sqrt ᮍ⊩এ䅵ㅫ᥹ᬊࠄⱘ᭄ؐˈᑊצᓔᮍϢ ᭄ㄝצ2.6.3 ᅲ⦄ᓔᮍǃ ᇚ store 䆒㕂Ўᔧࠡⱘ㒧ᵰDŽ Ёˈ⚍ߏњĀMCāᯊˈህᇚ store 䆒㕂Ў 0ˈ⚍ߏњĀMRāˈ߭ᇚ store ⱘؐ䇏পˈ⚍ߏњĀMSāˈ߭ ⿟ᑣЁᦤկњϔϾ store ⱘሲᗻ⫼ᴹֱᄬ䅵ㅫ㒧ᵰˈᔧ⫼᠋⚍ߏњĀM+āᯊˈህᇚ㒧ᵰࡴࠄ store } return null; } store = Double.valueOf( text ).doubleValue(); //བᵰᰃ"MS"᪡԰ˈ߭ᡞ䅵ㅫ㒧ᵰֱᄬࠄ store } else if( cmd.equals( "MS" ) ) { return String.valueOf( store ); isSecondNum = true; //བᵰᰃ"MR"᪡԰ˈ߭ᡞ store ⱘؐ䇏ߎᴹ } else if( cmd.equals( "MR" ) ) { store = 0; //བᵰᰃ"MC"᪡԰ˈ߭⏙䰸 store } else if( cmd.equals( "MC" ) ) { store = MyMath.add( store, Double.valueOf( text ) ); //བᵰᰃ"M+"᪡԰,߮ᡞ䅵ㅫ㒧ᵰ㌃⿃ࠄ store Ё if( cmd.equals( "M+" ) ) { public String mCmd( String cmd, String text ) { ҷⷕ⏙ऩ˖code\cal\src\org\crazyit\cal\CalService.java ᄬ᪡԰ˈ㾕ҹϟҷⷕDŽ ˄䇏প˅āǃĀMS˄ֱᄬ˅ā᪡԰ᯊˈህ䇗⫼ℸᮍ⊩ˈݡḍ᥂ᕫࠄⱘᄫヺІএ䖯㸠⏙䰸ǃ㌃ࡴǃ䇏পǃֱ ఼ˈ೼⫼᠋⚍ߏĀMC˄⏙䰸˅āǃĀM+˄㌃ࡴ˅āǃĀMRټᔧᄬܙᅮНϔϾ double ㉏ൟⱘሲᗻ store ᴹ ᪡԰ټ2.6.2 ᄬ 㒧ᵰ䖨ಲDŽخ multiplyǃdivide ᮍ⊩ˈᑊᡞ䖨ಲⱘ㒧ᵰֱᄬࠄ firstNum˗᳔ৢᡞ secondNum 䆒㕂Ўぎˈᡞ firstNum ᔧ ݡㄝѢϸ᭄ⳌЬ䰸ҹ 100 ⱘ㒧ᵰ˗✊ৢḍ᥂ lastOp ⱘؐ˄+ǃ-ǃ*ǃ/˅এ䇗⫼ MyMath ㉏Ёⱘ addǃsubtractǃ secondNum ህㄝѢ᳔ৢ䕧ܹⱘ᭄ᄫˈབᵰϡЎぎˈ߭ㄝѢॳᴹⱘؐˈབᵰ᳝Ā%āো᪡԰ˈ߭ secondNum ⹂ᅮ secondNum ⱘؐˈབᵰ secondNum ЎぎˈˈܜϞ䴶䅵ㅫ㒧ᵰЁˈ㒣ग़њ޴Ͼℹ偸ˈ佪 /Download at http://www.pin5i.com } else if (opString.indexOf(cmd) != -1) { return catNum(cmd, text); } else if (numString.indexOf(cmd) != -1) { return backSpace(text); } else if (cmd.equals("Back")) { return clear(text); } else if (cmd.equals("CE")) { return clearAll(); if (cmd.equals("C")) { public String callMethod(String cmd, String text) throws Exception { ҷⷕ⏙ऩ˖code\cal\src\org\crazyit\cal\CalService.java ݇ⱘᅲ⦄DŽ 䴶ⱘⲥ਀఼ЁˈাӮ䇗⫼ CalService ⱘ callMethod ᮍ⊩䖯㸠䖤ㅫˈ಴ℸ៥Ӏ䳔㽕ᇍ callMethod 䖯㸠Ⳍ ᭄ǃ⏙䰸䅵ㅫㄝˈԚᰃ೼⬠צ೼ࠡ䴶ⱘゴ㡖Ёˈ៥ӀᏆ㒣ᅲ⦄њ৘Ͼᮍ⊩ˈ՟བಯ߭䖤ㅫǃᓔᮍǃ 2.6.6 ᅲ⦄Ё䕀ᮍ⊩˄callMethod˅ } return this.firstNum; this.secondNum = null; this.firstNum = "0"; // ᇚ㄀ϔ㄀Ѡ᪡԰᭄ᘶ໡Ў咬䅸ؐ public String clearAll() { ҷⷕ⏙ऩ˖code\cal\src\org\crazyit\cal\CalService.java ЁᦤկњϔϾ clearAll ᮍ⊩ˈ⫼Ѣ⏙䰸᠔᳝ⱘ䅵ㅫ㒧ᵰDŽ ⏙䰸᠔᳝䅵ㅫ㒧ᵰˈᡞ firstNum Ϣ secondNum 䛑䆒㕂Ўॳྟؐˈᑊ䖨ಲ firstNumˈ೼ CalService 2.6.5 ⏙䰸䅵ㅫ㒧ᵰ } text.length() - 1); return text.equals("0") || text.equals("") ? "0" : text.substring(0, public String backSpace(String text) { ҷⷕ⏙ऩ˖code\cal\src\org\crazyit\cal\CalService.java 䗔᪡԰DŽצ⦃ӀᇚՓ⫼ String ⱘ substring ᮍ⊩䖯㸠໘⧚ˈᇚ䕧ܹᄫヺІⱘ᳔ৢϔԡ៾পDŽҹϟᮍ⊩ᅲ ܹⱘ᭄ᄫ䖯㸠៾পˈབᵰ᥹ᬊࠄⱘᄫヺІᰃĀ0ā៪㗙Ў nullˈ߭ϡ԰ӏԩ᪡԰ˈⳈ᥹䖨ಲˈ৺߭ˈ៥ 䗔᪡԰ˈ៥Ӏৃҹᇚ⫼᠋䕧צᔧ៥Ӏⱘ⿟ᑣЁᕫࠄ⫼᠋೼⬠䴶䕧ܹⱘⳌ᭄݇ᄫᯊˈབᵰ⫼᠋䖯㸠њ 䗔᪡԰צ⦃2.6.4 ᅲ } } return String.valueOf(MyMath.divide(1, Double.valueOf(text))); // 䅵ㅫ㒧ᵰᑊ䖨ಲ this.isSecondNum = true; // ᇚ isSecondNum ᷛᖫЎ true } else { return text; /Download at http://www.pin5i.com 䰸䖭ѯ if…else…DŽ೼ϟ䴶ⱘゴ㡖Ёˈ៥ӀӮ೼㓪ݭⱘ䖛⿟Ёˈሩ⼎ϔѯ䆒䅵῵ᓣⱘὖᗉDŽ if…else…䇁হˈᇍ䆒䅵῵ᓣ᳝ϔᅮњ㾷៪㗙Ꮰᳯᇍℸ᳝њ㾷ⱘ䇏㗙ˈৃҹᇱ䆩এ䞡ᵘᴀゴⱘҷⷕˈ⍜ Ϟᅲ⦄᳈ᔎ໻ⱘ䅵ㅫ఼˄⾥ᄺൟ䅵ㅫ఼˅DŽ঺໪䳔㽕⊼ᛣⱘᰃˈᴀゴ⿟ᑣ㓪ݭⱘ䖛⿟ЁˈՓ⫼њ䆌໮ ⫼᠋ˈᑊϨ৥⫼᠋䖨ಲ᳝⫼ⱘֵᙃDŽᴀゴЁᅲ⦄ⱘ䅵ㅫⳌᇍ䕗Ўㅔऩˈ᳝݈䍷ⱘ䇏㗙ৃҹ೼ᴀ᭛ⱘ෎⸔ Ꮧሔㅵ⧚఼এᏗሔDŽᑊϨ৥䇏㗙ҟ㒡њ ActionLisner џӊⲥ਀఼ⱘՓ⫼ˈҟ㒡བԩⲥ਀⫼᠋ⱘࡼ԰ડᑨ JPanelˈJTextFieldˈJButton ⱘՓ⫼DŽ⬠䴶Ꮧሔᮍ䴶ˈՓ⫼ࠄњ awt ⱘ BorderLayout Ϣ GridLayourt ᴀゴЏ㽕ᰃ䗮䖛ϔϾӓ Windows 䅵ㅫ఼ⱘ෎ᴀᅲ⦄ˈ৥䇏㗙䆆㾷 Java swing 㓪⿟ˈ⼎㣗њ JFrameˈ 2.7 ᴀゴᇣ㒧 䖭ѯ䯂乬DŽއDŽ᳝݈䍷ⱘ䇏㗙ৃҹњ㾷Ⳍ݇ⱘ䆒䅵῵ᓣˈ㗗㰥བԩ㾷އᓣᴹ㾷 ᮍḜˈ៥ӀৃҹՓ⫼ϔѯⱘ䆒䅵῵އbackSpace ᮍ⊩DŽᔧ✊ˈՓ⫼䖭М໮ⱘ if…else…ᑊϡᰃ᳔Շⱘ㾷 䗔њᣝ䬂ˈ䙷М callMethod ᮍ⊩ህӮḍ᥂⚍ߏⱘᣝ䬂᭛ᴀᴹᡒࠄצབⲥ਀఼ⲥ਀ࠄ⫼᠋⚍ߏњ ᅮ䇗⫼݋ԧⱘાϾᮍ⊩໘⧚䅵ㅫDŽ՟އCalService Ёⱘ callMethod ᮍ⊩ˈাᰃ߸ᮁ䕧ܹੑҸˈݡ } } return mCmd(cmd, text); } else { return cal(text, true); } else if (cmd.equals("%")) { return sqrt(text); } else if (cmd.equals("sqrt")) { return setReciprocal(text); } else if (cmd.equals("1/x")) { return setNegative(text); } else if (cmd.equals("+/-")) { return cal(text, false); } else if (cmd.equals("=")) { return setOp(cmd, text); ㄀ 2 ゴӓWindows 䅵ㅫ఼g16g /Download at http://www.pin5i.com ೒ 3.1 ೒⠛⌣㾜఼ ⠛ㄝࡳ㛑ˈ೒⠛⌣㾜఼ⱘ᳔㒜ᬜᵰབ೒ 3.1 ᠔⼎DŽ ᴀゴᇚᅲ⦄ϔϾ᳔ㅔऩⱘ೒⠛⌣㾜఼ˈࣙᣀњᠧᓔ೒⠛ǃᬒ໻Ϣ㓽ᇣ೒⠛ǃᶹⳟϞϔᓴ੠ϟϔᓴ೒ ԰ˈ೼䖭䞠ˈᅲ⦄њ೒⠛ⱘ⌣㾜ࡳ㛑ˈᇐ㟾ࡳ㛑˄ϟϔᓴǃϞϔᓴ˅ˈᬒ໻㓽ᇣࡳ㛑DŽ ⱘ᠔᳝೒⠛ˈᑊৃҹ⚍ߏ⌣㾜Ϟϔᓴ೒⠛៪㗙ϟϔᓴ೒⠛ˈ䖬᳝ᇍ೒⠛ᬒ໻Ϣ㓽ᇣˈ៪㗙㗏䕀೒⠛ㄝ᪡ Ϣ໡ᴖⱘ ACDSee ೒⠛⌣㾜఼˄䖭Ͼ䖬ᬃᣕ㓪䕥೒⠛˅ˈ೒⠛⌣㾜᳔఼෎ᴀⱘࡳ㛑ᰃ㛑⌣㾜ϔϾⳂᔩЁ ⳌֵՓ⫼ Window ᪡԰㋏㒳ⱘ໻໮᭄⫼᠋ˈ䛑Փ⫼䖛 Windows ⱘ೒⠛⌣㾜఼ˈ៪㗙ᰃࡳ㛑᳈ᔎ໻ 3.1 ೒⠛⌣㾜఼ὖ䗄 ㄀  ゴ೒⠛⌣㾜఼ 3.2 ߯ᓎ೒⠛⌣㾜఼ⱘⳌ݇ᇍ䈵 ៥Ӏ佪ܜ䳔㽕߯ᓎ೒⠛⌣㾜఼ⱘⳌ݇ᇍ䈵DŽ៥Ӏܜ߯ᓎ೒⠛⌣㾜఼ⱘ⬠䴶ᇍ䈵 ViewerFrameˈ✊ ৢ ೼䆹㉏Ёˈ៥ӀЎ㦰ऩǃᣝ䪂ࡴњџӊⲥ਀఼ˈ᠔ҹᅮНњϔϾ㒻ᡓ AbstractAction ⱘ㉏ ViewerAction ᴹડᑨ䖭ѯࡼ԰DŽ೼ Action Ёડᑨࡼ԰ˈህࠄ໘⧚݋ԧ䘏䕥ⱘℹ偸ˈ៥Ӏᡞ᠔᳝ⱘ䘏䕥໘⧚ᬒࠄ ViewerService ㉏ЁˈViewerService Ёࣙᣀᠧᓔ೒⠛ǃϞϔᓴǃϟϔᓴǃᬒ໻੠㓽ᇣ೒⠛ㄝࡳ㛑ˈЎњ ⿟ᑣ᳈དⱘ㾷㗺ড়ˈ៥Ӏৃҹᡞ݋ԧⱘᶤѯϮࡵ໘⧚ᬒ㕂ࠄ⣀ゟⱘ㉏Ё䖯㸠໘⧚DŽ 䰸њҹϞ᠔䇈ⱘ޴Ͼ㉏ˈ⬅Ѣ៥Ӏ䖭Ͼ⿟ᑣ᳝ᠧᓔ೒⠛ⱘ᪡԰ˈ᠔ҹ䳔㽕ϔϾ᭛ӊ䖛Ⓒ఼˄া㛑䗝 ᢽ೒⠛㉏ൟⱘ᭛ӊ˅ˈ᠔ҹᅮНњϔϾ㒻ᡓ JFileChooser ⱘ㉏ ViewChooserˈ䖭Ͼ㉏䞠䴶ᅮНњ㞾Ꮕⱘ ᭛ӊ䖛Ⓒ఼DŽᴀゴЁ᠔⍝ঞⱘᇍ䈵ঞᅗӀП䯈ⱘ݇㋏བ೒ 3.2 ᠔⼎DŽ ೒ 3.2 ೒⠛⌣㾜఼㉏೒ ᴀゴ⿟ᑣⱘࡳ㛑䕗Ўㅔऩˈ಴ℸ᠔⍝ঞⱘᇍ䈵гᑊϡ໡ᴖˈা᳝ㅔऩⱘѨϾᇍ䈵DŽ Download at http://www.pin5i.com/ /Download at http://www.pin5i.com ೒ 3.3 ᭛ӊ䖛Ⓒ఼ⱘ԰⫼ ViewerFileChooser ㉏DŽ ӊ㉏ൟāⱘϟᢝЁгা㛑䗝ᢽ.bmpˈᬜᵰབ೒ 3.3 ᠔⼎DŽ೼ᴀゴЁˈ᭛ӊᇍ䆱Ḛᇍᑨⱘᰃ ೼᭛ӊᇍ䆱Ḛⱘ addFilter ᮍ⊩ࡴܹҹϞⱘҷⷕৢˈ䙷М⫼᠋ᇚϡ㛑ⳟࠄ.bmp ⱘ᭛ӊˈᑊϨ೼Ā᭛ "BMP (*.BMP)")); this.addChoosableFileFilter(new MyFileFilter(new String[] { ".BMP" }, ӊˈ䙷Мৃҹ೼ addFilter ᮍ⊩ЁՓ⫼ҹϟҷⷕᅲ⦄˖ 䆹ᇍ䈵Ёⱘ addFilter ᮍ⊩Џ㽕⫼Ѣ৥᭛ӊᇍ䆱Ḛࡴܹ᭛ӊ䖛Ⓒ఼ˈ՟བ៥Ӏ䳔㽕াᰒ⼎.bmp ⱘ᭛ ‰ void addFilter()ˈЎ䖭Ͼ᭛ӊᇍ䆱Ḛ๲ࡴ䖛Ⓒ఼DŽ ⱘ᭛ӊᇍ䆱Ḛᇍ䈵ᰃ JFileChooser ㉏ⱘᄤ㉏ˈⳂⱘᰃЎњࡴܹ೼ 3.2.1 ЁᅮНⱘ᭛ӊ䖛Ⓒ఼˖ Java ᭛ӊᇍ䆱Ḛⱘᅲ⦄↨䕗ㅔऩˈা㽕Փ⫼ JFileChooser ㉏ᑊᦤկϔϾ㞾Ꮕⱘᵘ䗴఼ेৃDŽ䖭䞠 3.2.2 ᭛ӊᇍ䆱Ḛ ˄ViewerFileChooser˅ⱘϔϾݙ䚼㉏˄MyFileFilter˅DŽ Ӏ᠔ᅮНⱘ೒⠛᭛ӊˈ䙷М݊Ҫⱘ᭛ӊᇚϡӮ㹿ᰒ⼎DŽ೼ᴀゴЁˈ᭛ӊ䖛Ⓒ఼ᰃ᭛ӊᇍ䆱Ḛ㉏ 㸠偠䆕DŽᔧ⫼᠋ᠧᓔ᭛ӊ䗝ᢽᯊˈ៥Ӏህৃҹᇍ᠔᳝ⱘ᭛ӊ䖯㸠ϔ⃵䖛Ⓒˈ᭛ӊ䗝ᢽ఼Ёাৃҹ䗝ᢽ៥ ᭛ӊ䖛Ⓒ఼Џ㽕೼⫼᠋ᠧᓔ೒⠛ᯊՓ⫼ˈᔧ⫼᠋䖯㸠њ೒⠛䗝ᢽৢˈህৃҹᇍ⫼᠋᠔䗝ᢽⱘ᭛ӊ䖯 ‰ String getDescription()ˈ㦋প䖛Ⓒ఼ⱘᦣ䗄DŽ ‰ boolean accept( File f )ˈ߸ᮁ᭛ӊᰃ৺ሲѢ೒⠛㉏ൟDŽ ⊩˖ FileFilter ⱘϔϾ㒻ᡓˈгᰃ᭛ӊᇍ䆱Ḛⱘݙ䚼㉏ˈ䞠䴶䞡ݭњ FileFilter ⱘ accept Ϣ getDescription ᮍ བᵰ㽕Փ᭛ӊᇍ䆱Ḛᅲ⦄᭛ӊ䖛Ⓒࡳ㛑ˈህ䳔㽕㒧ড় FileFilter ㉏ᴹ䖯㸠᭛ӊ᪡԰ˈ᭛ӊ䖛Ⓒ఼ᰃ ᭛ӊ䖛Ⓒ఼ 3.2.1 /Download at http://www.pin5i.com 䖭Ͼ೒⠛⌣㾜఼ⱘ⬠䴶ᥦ⠜↨䕗ㅔऩˈা᳝㦰ऩ˄ϡ䳔㽕ᥦ⠜˅ǃᎹ݋ᷣǃ೒⠛ᰒ⼎ऎˈ៥ӀՓ⫼ 3.3 ߯ᓎЏ⬠䴶 Ёˈབᵰথ⦄ৃҹᇍ⿟ᑣ䖯㸠䞡ᵘˈ䙷Мгৃҹ೼䞡ᵘⱘ䖛⿟Ёˈ߯ᓎⳌ݇ⱘ㉏DŽ ࠄℸˈ೒⠛⌣㾜఼ⱘⳌ݇ᇍ䈵䛑Ꮖ㒣ᓎゟˈᑊϨ⹂ᅮњ៥Ӏ䳔㽕ᅲ⦄ાѯᮍ⊩ˈ៥Ӏ೼ᅲ⦄ⱘ䖛⿟ ᮁˈ䅽⿟ᑣⶹ䘧⫼᠋䖯㸠њԩ⾡᪡԰ˈݡ䇗⫼Ϯࡵ໘⧚㉏ЁⱘⳌᑨᮍ⊩DŽ ⬅Ѣ៥Ӏা᳝ϔϾ᪡԰໘⧚㉏ˈ಴ℸ೼ᅲ⦄ actionPerformed ᮍ⊩ᯊˈ៥Ӏህ䳔㽕䖯㸠ϔ㋏߫ⱘ߸ ‰ void actionPerformed( ActionEvent e )ˈ䞡ݭ AbstractAction ⱘᮍ⊩ˈડᑨџӊDŽ ݡ⫼䖭Ͼ Action ᴹ߯ᓎᣝ䪂ˈ⚍ߏᣝ䪂ⱘᯊ׭ˈᇚ䇗⫼ℸ㉏ⱘ actionPerformed ᮍ⊩˖ ᪡԰ˈᴀ՟Ёⱘ᪡԰໘⧚㉏ᰃ AbstractAction ⱘϔϾᄤ㉏ˈ㛑⫼ ImageIcon˄೒ᷛ˅ᴹ߯ᓎϔϾ Actionˈ ೼ᴀ՟Ёˈ⬅Ѣ⫼᠋ৃҹᠻ㸠ⱘ᪡԰䕗ᇥˈ಴ℸˈ៥ӀৃҹᦤկϔϾ᪡԰໘⧚㉏ᴹ᥹ᬊ⫼᠋᠔᳝ⱘ 3.2.5 ᪡԰໘⧚㉏ DŽ೼ᴀゴЁˈϮࡵ໘⧚㉏ᇍᑨⱘᰃ ViewerService ㉏DŽމ೒⠛⌣㾜఼ⱘᚙ ݊Ҫⱘ䆓䯂㗙ᇚӮফࠄᕅડDŽᔧ✊ˈ៥ӀᴀゴাᰃϔϾ᱂䗮ⱘ೒⠛⌣㾜఼ˈϡᄬ೼໮Ͼ⫼᠋Փ⫼ৠϔϾ ᇍ䈵ⱘ䖭ѯሲᗻᇚӮ㹿᠔᳝ⱘ䆓䯂㗙݅ѿˈབᵰ݊ЁⱘϔϾ䆓䯂㗙ᬍবњ݊ЁϔϾ៪㗙໮Ͼሲᗻˈ䙷М ↨՟ㄝሲᗻDŽ⬅Ѣ៥Ӏ䖭Ͼᰃ᳝⢊ᗕⱘ Java ᇍ䈵ˈ䙷Мህᛣੇⴔˈབᵰ䆓䯂ⱘᰃৠϔϾᅲ՟ˈ䙷М䆹 ϔѯϮࡵ⢊ᗕˈ䖭ѯϮࡵ⢊ᗕࣙᣀ˖ᔧࠡ⌣㾜ⱘ᭛ӊⳂᔩǃ᭛ӊⳂᔩⱘ᭛ӊ䲚ড়ǃ೒⠛ᬒ໻៪㗙㓽ᇣⱘ ೼ᴀゴЁˈ䖭ϾϮࡵ໘⧚㉏ᑊϡᰃ᮴⢊ᗕⱘ Java ᇍ䈵ˈгህᰃᛣੇⴔᴀゴⱘϮࡵ໘⧚㉏ᇚҎֱᄬ ‰ void menuDo( ViewerFrame frame, String cmd )ˈડᑨ㦰ऩⱘࡼ԰DŽ ‰ next( ViewerFrame frame )ˈ⌣㾜ϟϔᓴ೒⠛DŽ ‰ void last( ViewerFrame frame )ˈ⌣㾜Ϟϔᓴ೒⠛DŽ ԰ˈ䖭䞠ৃ㛑Ӯ϶༅೒⠛㊒ᑺDŽ ᬒ໻៪㗙㓽ᇣ᪡خ⠛‰ void zoom( ViewerFrame frame, boolean isEnlarge )ˈᇍℷ೼⌣㾜ࠄⱘ೒ ‰ void open( ViewerFrame frame )ˈᔍߎ᭛ӊ䗝ᢽḚˈᑊ䇏প㹿䗝ᢽࠄⱘ೒⠛DŽ ‰ static ViewerService getInstance()ˈ㦋প ViewerService ㉏ⱘϔϾऩᗕᅲ՟DŽ ᇣ೒⠛ǃ⌣㾜Ϟϔᓴ೒⠛ǃ⌣㾜ϟϔᓴ೒⠛ㄝࡳ㛑ˈབϟ˖ Ϯࡵ໘⧚㉏Џ㽕ᰃ໘⧚೒⠛⌣㾜఼ⱘ໻䚼ߚϮࡵ䘏䕥ˈࣙᣀᠧᓔ೒⠛ǃ݇䯁⌣㾜఼ǃᬒ໻೒⠛ǃ㓽 3.2.4 Ϯࡵ໘⧚㉏ViewerService ೼ᴀゴЁˈЏ⬠䴶ᇍᑨⱘᰃ ViewerFrame ㉏DŽ ೼䖭䞠䳔㽕⊼ᛣⱘᰃˈ⬅Ѣᠧᓔⱘ೒⠛໻ᇣᑊϡ㛑⹂ᅮˈ಴ℸ೒⠛ᰒ⼎ऎᖙ乏Փ⫼ JScrollPaneDŽ ‰ void createMenuBar()ˈ߯ᓎ᭛ӊǃᎹ݋ǃᐂࡽㄝ㦰ऩDŽ ‰ createToolPanel()ˈ߯ᓎᬒ໻ǃ㓽ᇣǃϞϔᓴǃϟϔᓴㄝᎹ݋ᣝ䪂DŽ ‰ JLabel getLabel()ˈ㦋পᰒ⼎೒⠛ⱘ JLabelDŽ ‰ void init()ˈ߱ྟ࣪೒⠛⌣㾜఼ⱘ⬠䴶DŽ ᷣϢ㦰ऩᷣࡴϞџӊⲥ਀఼ˈབϟ˖ ៥ӀᓎゟϔϾ⬠䴶㉏԰Ў೒⠛⌣㾜఼ⱘЏ⬠䴶ˈ䆹㉏ࣙᣀ೒⠛ᰒ⼎ऎǃ㦰ऩᷣǃᎹ݋ᷣˈᑊЎᎹ݋ Џ⬠䴶㉏ 3.2.3 /Download at http://www.pin5i.com //߯ᓎϔϾ JMenuBar ᬒ㕂㦰ऩ public void createMenuBar() { ҷⷕ⏙ऩ˖code\viewer\src\org\crazyit\viewer\ViewerFrame.java ৃҹϔ⃵ᗻ߯ᓎߎ᠔᳝ⱘ㦰ऩ˄⫼᭄㒘ᅮНད㦰ऩ᭛ᄫㄝϰ㽓ⱘᔶᓣ˅ˈ㾕ҹϟᮍ⊩DŽ 䞠䴶ⱘ service ㉏ህᰃ៥ӀⱘϮࡵ䘏䕥໘⧚㉏ ImageService ⱘϔϾऩᗕᅲ՟DŽ᳝њ䖭Ͼџӊⲥ਀఼ˈህ 䖭Ͼџӊⲥ਀఼ᅲ⦄њ ActionListener Ёⱘ actionPerformed ᮍ⊩ˈᰃડᑨ⫼᠋᪡԰ⱘᮍ⊩ˈᮍ⊩ }; } service.menuDo( ImageFrame.this, e.getActionCommand() ); public void actionPerformed(ActionEvent e) { ActionListener menuListener = new ActionListener(){ //ࡴ㒭㦰ऩⱘџӊⲥ਀఼ ҷⷕ⏙ऩ˖code\viewer\src\org\crazyit\viewer\ViewerFrame.java Ў㦰ऩᅮНϔϾџӊⲥ਀఼ˈ㾕ҹϟҷⷕDŽܜˈ㦰ऩᷣˈᖙ乏᳝џӊડᑨˈ᠔ҹ 3.3.2 ߯ᓎ㦰ऩᷣ ᇚ೼ϟ䴶ゴ㡖Ёᅲ⦄DŽ ऎাᰃϔϾ JLabel˅DŽҹϞҷⷕЁⱘ咥ԧ䚼ߚˈՓ⫼ϔϾ createToolPanel ⱘᮍ⊩ᴹ߯ᓎ㦰ऩˈ䆹ᮍ⊩ ᷣǃ䇗⫼ createToolPanel()ᮍ⊩এ߯ᓎᎹ݋ᷣˈ᳔ৢᡞ㦰ऩᷣ੠೒⠛ᰒ⼎ऎࡴࠄ JFrame Ё˄೒⠛ᰒ⼎ ᰃЎ JFrame 䆒㕂ᷛ乬ˈ᥹ϟᴹ䆒㕂໻ᇣˈ✊ৢ䇗⫼ᴀ㉏ⱘ createMenuBar()ᮍ⊩এ߯ᓎ㦰ऩܜ佪 } this.pack(); this.setVisible( true ); //䆒㕂Ўৃ㾕 this.add( new JScrollPane(label), BorderLayout.CENTER ); this.add( toolBar, BorderLayout.NORTH ); //ᡞᎹ݋ᷣ੠䇏೒ऎࡴࠄ JFrame 䞠䴶 JPanel toolBar = createToolPanel(); //߯ᓎᎹ݋ᷣ createMenuBar(); //߯ᓎ㦰ऩ this.setPreferredSize( new Dimension( width, height ) ); //䆒㕂໻ᇣ this.setTitle( "ⳟ೒⿟ᑣ" ); //䆒㕂ᷛ乬 public void init() { ҷⷕ⏙ऩ˖code\viewer\src\org\crazyit\viewer\ViewerFrame.java ҷⷕDŽ ᳔ৢ㦋পᎺ䖍Ꮉ݋ᷣ䴶ᵓǃϟ䴶㦰ऩᷣ䴶ᵓǃ㦰ऩˈᑊᡞ䖭ѯ䴶ᵓϢ⬏೒㦋পࡴࠄ JFrame Ёˈ㾕ҹϟ PENCIL_TOOL(䪙ヨ)㉏ൟⱘ Toolˈ߯ᓎ৘⾡哴ᷛⲥ਀఼ˈᑊ೼ⲥ਀ⱘᠻ㸠ᮍ⊩Ё䇗⫼ Tool ⱘⳌᑨᮍ⊩ˈ 䆒㕂 JFrame にষⱘᷛ乬ˈ᥹ϟᴹ߱ྟ࣪⬏೒ऎඳˈ߱ྟ࣪Ўⱑ㡆ˈ✊ৢݡ㦋পˈܜ佪 3.3.1 ߱ྟ࣪⬠䴶˄init()ᮍ⊩˅ ᴹ԰Ў೒⠛ᰒ⼎ऎඳDŽ BorderLayout.CENTERDŽ೼ᴀゴЁˈ⬅Ѣᠧᓔ೒⠛ⱘ໻ᇣᑊϡ⹂ᅮˈ಴ℸ៥Ӏ䳔㽕Փ⫼ϔϾ JScrollPane BorderLayout 䖯㸠ᏗሔˈᡞᎹ݋ᷣᬒ೼ BorderLayout.NORTH ˈᡞ೒⠛ᰒ⼎ऎᬒ೼ /Download at http://www.pin5i.com 䖭Ͼ JmenuBar ࡴࠄ JFrame 䞠䴶ˈ߯ᓎ㦰ऩⱘ䖛⿟ህᅠ៤њDŽ ࠄ JMenu ЁDŽ↣⃵䘡ग़ᅠ ㄀ϔϾ᭄㒘ˈ䛑ᡞ䖭Ͼ JMenu ࡴࠄ JMenuBar ЁDŽ䘡ग़ᅠ᠔᭄᳝㒘ˈህᡞ ߯ᓎϔϾ㦰ऩ乍˄JMenuItem˅ˈᑊЎ䖭Ͼ㦰ऩ乍๲ࡴϞࠡ䴶ᅮНⱘџӊⲥ਀఼ˈ✊ৢᡞ䖭Ͼ㦰ऩ乍ࡴ ҢҷⷕЁৃҹⳟࠄˈ⿟ᑣ⫼ϸϾ᭄㒘ᡞ䖭ϸሖ㦰ऩⱘ᭛ᄫֱᄬњ䖯এˈϸϾ᭄㒘ϔ䍋䘡ग़ˈ↣⃵䛑 ݇Ѣ ᐂࡽЏ乬 ᐂࡽ(H) ϟϔϾ(P) ϞϔϾ(X) 㓽ᇣ(O) ᬒ໻(M) Ꮉ݋(T) 䗔ߎ(X) ᠧᓔ(O) ᭛ӊ(F) ೒⠛⌣㾜఼ⱘ㦰ऩᰃ䖭ḋⱘ㒧ᵘ˖ } this.setJMenuBar( menuBar ); //䆒㕂 JMenubar } menuBar.add(menu); //ᡞ㦰ऩࡴࠄ JMenuBar Ϟ } } menu.add( menuItem ); //ᡞ㦰ऩ乍ࡴࠄ JMenu 㦰ऩ䞠䴶 menuItem.addActionListener( menuListener ); JMenuItem menuItem = new JMenuItem( menuItemArr[i][j] ); //ᮄᓎϔϾ JMenuItem 㦰ऩ乍 } else { menu.addSeparator(); //䆒㕂㦰ऩߚ䱨 if ( menuItemArr[i][j].equals( "-" ) ) { //བᵰ menuItemArr[i][j]ㄝѢ"-" for( int j = 0 ; j < menuItemArr[i].length ; j++ ) { JMenu menu = new JMenu( menuArr[i] ); //ᮄᓎϔϾ JMenu 㦰ऩ for( int i = 0 ; i < menuArr.length ; i++ ) { //䘡ग़ menuArr Ϣ menuItemArr এ߯ᓎ㦰ऩ }; { "ᐂࡽЏ乬", "݇Ѣ" } {"ᬒ໻(M)", "㓽ᇣ(O)","-","ϞϔϾ(X)","ϟϔϾ(P)"}, {"ᠧᓔ(O)","-", "䗔ߎ(X)"}, String[][] menuItemArr = { //㦰ऩ乍᭛ᄫ᭄㒘 String[] menuArr = { "᭛ӊ(F)", "Ꮉ݋(T)", "ᐂࡽ(H)" }; //㦰ऩ᭛ᄫ᭄㒘ˈҹϟ䴶ⱘ menuItemArr ϔϔᇍᑨ JMenuBar menuBar = new JMenuBar(); 3.3.3 ߯ᓎᎹ݋ᷣ 䖭䞠ⱘᎹ݋ᣝ䪂ˈЎњ㕢㾖ˈᛇ⫼೒⠛ⱘᮍᓣ߯ᓎ JButtonˈ䖭䞠ህ㽕⫼ࠄ AbstractActionˈгህ ᰃ៥Ӏᠽሩⱘ ViewerAction ㉏ˈ佪ܜᰃ⫼ ViewerAction ⱘ ViewrAction(ImageIcon icon, String name, ViewerFrame frame)এ߯ᓎϔϾ ViewrActionˈখ᭄䞠䴶ⱘ icon ᇍ䈵ህᰃҢᴀഄ䏃ᕘЁ䇏њ೒ᷛⱘ೒ᷛ ㉏ˈ✊ৢҹ䖭Ͼ ViewerAction ᇍ䈵Ўখ᭄এ߯ᓎϔϾ JButtonDŽ㾕ҹϟҷⷕDŽ ҷⷕ⏙ऩ˖code\viewer\src\org\crazyit\viewer\ViewerFrame.java public JPanel createToolPanel() { //߯ᓎϔϾ JPanel JPanel panel = new JPanel(); //߯ᓎϔϾᷛ乬Ў"Ꮉ݋"ⱘᎹ݋ᷣ JToolBar toolBar = new JToolBar( "Ꮉ݋" ); //䆒㕂Ўϡৃᢪࡼ toolBar.setFloatable( false ); //䆒㕂Ꮧሔᮍᓣ panel.setLayout( new FlowLayout( FlowLayout.LEFT ) ); //Ꮉ݋᭄㒘 String[] toolarr = { "open", "last", "next", "big", "small" }; for( int i = 0 ; i < toolarr.length ; i++ ) { ViewerAction action = new ViewerAction( new ImageIcon("img/" + toolarr[i] + ".gif") , toolarr[i], this ); //ҹ೒ᷛ߯ᓎϔϾᮄⱘ button JButton button = new JButton( action ); //ᡞ button ࡴࠄᎹ݋ᷣЁ toolBar.add(button); } panel.add( toolBar ); //䖨ಲ return panel; } ҹϞҷⷕⱘ咥ԧ䚼ߚˈ៥ӀՓ⫼њ JButton ᴹ߯ᓎᎹ݋ᷣⱘ೒ᷛˈ↣ϔϾ JButton ᇍ䈵䛑Փ⫼ ViewerAction ԰Ўᵘ䗴খ᭄ˈԚᰃ䳔㽕⊼ᛣⱘᰃˈ৘Ͼ JButton П䯈ᑊϡᰃ݅ѿϔϾ ViewerAction ⱘᅲ ՟DŽ߯ᓎᅠ㦰ऩϢᎹ݋ᷣৢˈৃҹ䖤㸠ᶹⳟ݋ԧⱘᬜᵰˈЏ⬠䴶ⱘᬜᵰབ೒ 3.4 ᠔⼎DŽ Download at http://www.pin5i.com/ /Download at http://www.pin5i.com JButton button = new JButton( action ); //ҹ೒ᷛ߯ᓎϔϾᮄⱘ button , toolarr[i], this ); new ImageIcon("img/" + toolarr[i] + ".gif") ViewerAction action = new ViewerAction( for( int i = 0 ; i < toolarr.length ; i++ ) { String[] toolarr = { "open", "last", "next", "big", "small" }; ҷⷕ⏙ऩ˖code\viewer\src\org\crazyit\viewer\ViewerFrame.java Փ⫼њҹϟҷⷕDŽ ݋ᷣⱘᶤϾ᪡԰ᯊˈህӮᠻ㸠 ViewerAction ⱘ actionPerformed ⱘᮍ⊩DŽ៥Ӏ೼ 3.3.3 Ё߯ᓎᎹ݋ᷣᯊˈ ៥Ӏ೼ 3.2.5 Ё߯ᓎњϔϾ ViewerAction ⱘ㉏ˈЏ㽕⫼Ѣ໘⧚Ꮉ݋ᷣⱘ⚍ߏџӊˈᔧ⫼᠋⚍ߏњᎹ 3.4.1 ᅲ⦄Ꮉ݋ᷣ⚍ߏ ᰃ᳝⢊ᗕⱘ Java ᇍ䈵DŽ ϔ⃵䇈ᯢˈViewerServiceخ⠛ǃ㓽ᇣ೒⠛ǃ⌣㾜Ϟϔᓴ೒⠛ǃ⌣㾜ϟϔᓴ೒⠛ㄝࡳ㛑ˈ೼䖭䞠䳔㽕ݡ ViewerService ㉏Џ㽕ᰃ໘⧚೒⠛⌣㾜఼ⱘ໻䚼ߚϮࡵ䘏䕥ˈࣙᣀᠧᓔ೒⠛ǃ݇䯁⌣㾜఼ǃᬒ໻೒ 3.4 ᅲ⦄೒⠛⌣㾜ⱘ᪡԰ ఼ˈৃҹখ㗗 ACESee ៪㗙 Windows ೒⠛⌣㾜఼ㄝࡳ㛑DŽ ᳈ᔎ໻ⱘ೒⠛⌣㾜خ೼ᴀ՟Ёˈ೒⠛⌣㾜఼ⱘࡳ㛑Ⳍᇍ䕗Ўㅔऩˈ಴ℸ⬠䴶гᰃ䕗Ўㅔ⋕DŽབᵰᛇ ೒ 3.4 ೒⠛⌣㾜఼Џ⬠䴶 /Download at http://www.pin5i.com ᥹ষⱘᶤϾᅲ⦄㉏DŽ ҹՓ⫼䖭Ͼখ᭄এ߯ᓎ݋ԧⱘᶤϾᅲ⦄㉏DŽЎ ViewerAction 㓪ݭϔϾᎹ݋ᮍ⊩ˈՓ⫼ডᇘᕫࠄ Action ᇚॳᴹⱘᄫヺІ᳈ᤶЎᶤϾ Action ᅲ⦄㉏ⱘܼ䰤ᅮ㉏ৡˈ䙷М೼ᵘ䗴 ViewerAction ⱘᯊ׭ˈህৃ } toolBar.add(button); // ᡞ button ࡴࠄᎹ݋ᷣЁ JButton button = new JButton(action); // ҹ೒ᷛ߯ᓎϔϾᮄⱘ button + toolarr[i] + ".gif"), toolarr[i], this); ViewerAction action = new ViewerAction(new ImageIcon("img/" for (int i = 0; i < toolarr.length; i++) { "org.crazyit.viewer.action.SmallAction" }; "org.crazyit.viewer.action.BigAction", "org.crazyit.viewer.action.NextAction", "org.crazyit.viewer.action.LastAction", String[] toolarr = { "org.crazyit.viewer.action.OpenAction", // Ꮉ݋᭄㒘 ҷⷕ⏙ऩ˖code\viewer\src\org\crazyit\viewer\ViewerFrame.java ᦤկњ䖭Ͼ OpenAction ৢˈ៥Ӏ䳔㽕ׂᬍ߯ᓎᎹ݋ᷣⱘҷⷕˈᤶϔ⾡ᮍᓣ߯ᓎᎹ݋ᷣDŽ } //ᠧᓔ᭛ӊᇍ䆱Ḛ public void execute(ViewerService service, ViewerFrame frame) { ҷⷕ⏙ऩ˖code\viewer\src\org\crazyit\viewer\action\ OpenAction.java ҷⷕᰃ OpenAction ⱘ݋ԧⱘᅲ⦄DŽ ՟བ᳝ϔϾᠧᓔ᭛ӊᇍ䆱Ḛⱘ Actionˈ䙷М៥ӀህᮄᓎϔϾ OpenActionˈ䆹㉏ᅲ⦄ Action ᥹ষDŽҹϟ 㓪ݭњ᥹ষ Action ৢˈ៥ӀᅮНњϔϾ execute ⱘᮍ⊩ˈ䙷Мˈ៥ӀৃҹЎ䆹 Action ᮄᓎᅲ⦄㉏ˈ } void execute(ViewerService service, ViewerFrame frame); */ * @param frame Џ⬠䴶ᇍ䈵 * @param service ೒⠛⌣㾜఼ⱘϮࡵ໘⧚㉏ * ݋ԧᠻ㸠ⱘᮍ⊩ /** public interface Action { ҷⷕ⏙ऩ˖code\viewer\src\org\crazyit\viewer\action\Action.java DŽ᥹ϟᴹˈ߯ᓎϔϾ Action ⱘ᥹ষˈᦤկϔϾ execute ⱘᮍ⊩DŽއif…elseˈ䙷М៥Ӏህ䳔㽕ᛇࡲ⊩এ㾷 ᮍḜˈԚᰃབᵰ⿟ᑣЁߎ⦄བѯП໮ⱘއWindows 䅵ㅫ఼˅ЁᏆ㒣ߎ⦄ˈᔧࠡᑊ≵᳝ᦤկӏԩⱘ㾷 ᴀゴЁা᳝ 5 Ͼ Actionˈህ䳔㽕ݭ 5 ⃵ⱘ if…elseˈᇍѢ䖭ḋⱘҷⷕˈ៥Ӏ೼ᴀкⱘ㄀Ѡゴ˄ӓ … } //Ϟϔϟ೒⠛ } else if (this.name.equals("last")) { //ᠧᓔ᭛ӊᇍ䆱Ḛ if (this.name.equals("open")) { ህᛣੇⴔ៥Ӏ䳔㽕೼ actionPerformed ᮍ⊩Ё԰ߎ䖭ѯ߸ᮁ˖ ҹϞҷⷕЁՓ⫼њĀopenāǃĀlastāㄝᄫヺІ⫼ᴹᷛ䆚ᑨ䆹Փ⫼ ViewerService ⱘાϾᮍ⊩ˈ䙷М } toolBar.add(button); ᡞ button ࡴࠄᎹ݋ᷣЁ// /Download at http://www.pin5i.com //ϞϔϾ } zoom( frame, false ); if( cmd.equals("㓽ᇣ(O)") ) { //㓽ᇣ } zoom( frame, true ); if( cmd.equals("ᬒ໻(M)") ) { //ᬒ໻ } open( frame ); if( cmd.equals("ᠧᓔ(O)") ) { //ᠧᓔ public void menuDo( ViewerFrame frame, String cmd ) { ҷⷕ⏙ऩ˖code\viewer\src\org\crazyit\viewer\ViewerFrame.java М㉏ൟⱘϮࡵ໘⧚DŽ೼ᮍ⊩Ёˈᰃḍ᥂㦰ऩⱘ᭛ᄫএ߸ᮁϟℹ㽕䇗⫼ⱘᮍ⊩DŽ ѯҔخᅮއ䇗⫼䖭Ͼᮍ⊩ˈ⬅䖭Ͼᮍ⊩এܜ៥ӀЎ㦰ऩ๲ࡴњџӊⲥ਀఼ˈ↣⃵⚍ߏ㦰ऩᯊˈ䛑Ӯ 3.4.2 ᅲ⦄㦰ऩⱘ⚍ߏ Ԣ⿟ᑣⱘ㗺ড়DŽޣᇥ if…else ៪㗙ሑ䞣ޣҎⶹ䘧⿟ᑣᇚӮ᳝໮໡ᴖˈ಴ℸヨ㗙䖬ᰃ᥼ዛՓ⫼݊Ҫᮍ⊩ᴹ Ԛᰃ≵᳝ˈއা䳔㽕এׂᬍՓ⫼㗙˄Џ⬠䴶ᇍ䈵˅DŽᇍѢϔѯㅔऩⱘ⿟ᑣˈ៥ӀৃҹՓ⫼ if…else ᴹ㾷 ᇥ໮ᇥϾ Actionˈ䛑ϡᖙএׂᬍ ViewerAction ㉏ˈޣ᮴䆎⏏ࡴ៪㗙ˈخׂᬍ ViewerActionˈ៥Ӏ䖭ḋ ᳈ⳕџˈԚᰃˈབᵰキ೼⿟ᑣৃᠽሩⱘ㾦ᑺⳟˈᔧ䳔㽕Ў೒⠛⌣㾜఼⏏ࡴ㸠Ўᯊˈ៥Ӏህϡᖙݡخ䖭ḋ ݊ᅲ೼ᴀゴЁˈ៥Ӏᑊϡ䳔㽕བℸ໡ᴖᴹᅲ⦄ˈ៪䆌᳝ѯ䇏㗙Ӯ㾝ᕫˈ㓪ݭ໮޴Ͼ if…else ৃ㛑↨ } action.execute(service, frame); //䇗⫼ Action ⱘ execute ᮍ⊩ Action action = getAction(this.actionName); ViewerService service = ViewerService.getInstance(); public void actionPerformed(ActionEvent e) { ҷⷕ⏙ऩ˖code\viewer\src\org\crazyit\viewer\ViewerAction.java Ⳉ᥹䗮䖛ҹϞⱘᎹ݋ᮍ⊩˄getAction˅ᕫࠄⳌ݇ⱘ Action ᅲ⦄㉏ˈݡ䇗⫼ Action ⱘ execute ᮍ⊩ेৃDŽ ԧⱘᶤϾ Action ᅲ⦄㉏ৢˈ೼ᅲ⦄ ViewerAction ⱘᯊ׭ˈ៥ӀህৃҹϡᖙՓ⫼䙷ේ⚺Ҏⱘ if…else њˈ 䆹ᮍ⊩೼ ViewerAction Ёˈ᠔ҹ៥Ӏ೼ᵘ䗴 ViewerAction ⱘᯊ׭ˈᇚᇍᑨⱘ໘⧚㉏ӴܹेৃDŽᕫࠄ݋ ҹϞⱘ咥ԧҷⷕˈՓ⫼њডᇘᴹ߯ᓎϔϾᅲ՟ˈᑊϨ䆹ᅲ՟೼ ViewerAction Ёা᳝ϔϾᅲ՟ˈ⬅Ѣ } } return null; } catch (Exception e) { return this.action; } this.action = action; Action action = (Action)Class.forName(actionName).newInstance(); //߯ᓎ Action ᅲ՟ if (this.action == null) { try { private Action getAction(String actionName) { ҷⷕ⏙ऩ˖code\viewer\src\org\crazyit\viewer\ViewerAction.java /Download at http://www.pin5i.com ImageIcon icon = new ImageIcon( name ); } } } } this.currentFiles.add( file ); //ᡞ᭛ӊࡴࠄ currentFiles Ё if( filter.accept( file ) ) { //བᵰᰃ೒⠛᭛ӊ for( FileFilter filter : fileFilters ) { for( File file : files ) { this.currentFiles = new ArrayList(); File files[] = cd.listFiles(); .getChoosableFileFilters(); FileFilter[] fileFilters = fileChooser //៪㗙 fileChooser ⱘ᠔᳝ FileFilter || this.currentDirectory == null ) { if( cd != this.currentDirectory //བᵰ᭛ӊ།᳝ᬍব File cd = fileChooser.getCurrentDirectory(); //㦋পⳂࠡ᭛ӊ། String name = this.currentFile.getPath(); //㦋প᭛ӊ䏃ᕘ this.currentFile = fileChooser.getSelectedFile(); //㒭Ⳃࠡᠧᓔⱘ᭛ӊ䌟ؐ == ViewerFileChooser.APPROVE_OPTION ) { if( fileChooser.showOpenDialog( frame ) //བᵰ䗝ᢽᠧᓔ public void open( ViewerFrame frame ) { ҷⷕ⏙ऩ˖code\viewer\src\org\crazyit\viewer\ViewerService.java 㓧ᄬⱘ᭛ӊ䛑ֱᄬ೼ᴀ㉏ⱘ currentFiles ЁˈcurrentFiles ᰃϔϾ List㉏ൟDŽ ᄬ䍋ᴹˈⳂⱘᰃЎњϡ⫼↣⃵䛑এ᧰㋶䖭Ͼ᭛ӊ།ݙⱘ᭛ӊˈгᮍ䴶ĀϞϔᓴā੠Āϟϔᓴāⱘᅮԡˈ 䖭Ͼ೒⠛⌣㾜఼ˈᠧᓔϔϾ೒⠛᭛ӊПৢˈӮᡞ䖭Ͼ᭛ӊ᠔᳝᭛ӊ།㉏ⱘ᠔᳝೒⠛㉏ൟⱘⱘ᭛ӊ㓧 3.4.3 ᠧᓔ೒⠛ ೼ℸˈ៥ӀৠḋৃҹՓ⫼ 3.4.1 Ёⱘᮍ⊩ᴹ⍜䰸䖭ϔේⱘ if…elseˈ೼䖭䞠ϡݡ䆺㒚ᦣ䗄DŽ } } System.exit( 0 ); if( cmd.equals("䗔ߎ(X)") ) { //䗔ߎ } next( frame ); if( cmd.equals("ϟϔϾ(P)") ) { //ϟϔϾ } last( frame ); if( cmd.equals("ϞϔϾ(X)") ){ /Download at http://www.pin5i.com //བᵰ᳝ᠧᓔࣙ৿೒⠛ⱘ᭛ӊ། public void last( ViewerFrame frame ) { ҷⷕ⏙ऩ˖code\viewer\src\org\crazyit\viewer\ViewerService.java 䖭䞠ᰃҹ䇏পϞϔᓴ೒⠛ⱘᮍ⊩Ў՟ᄤ䇈ᯢˈ䇏পϟϔᓴ೒⠛ⱘᅲ⦄ᰃ㉏ԐⱘDŽ ህ㛑Ң currentFiles Ёপࠄ೒⠛DŽ ӊ currentFilesˈ䙷Мˈ䇏পĀϞϔᓴā៪㗙Āϟϔᓴā೒⠛ህবⱘㅔऩњˈা㽕ᕫࠄϔϾ೒⠛ⱘ㋶ᓩˈ ࠡ䴶ⶹ䘧ˈViewerService Ёֱᄬⴔᔧࠡᠧᓔⱘ᭛ӊ currenFileˈ䖬᳝䖭Ͼ᭛ӊ།ϟ䴶ⱘ᠔᳝೒⠛᭛ 3.4.5 ĀϞϔᓴāǃĀϟϔᓴā೒⠛ ⱘ setIcon ᮍ⊩ᡞ䖭೒⠛䆒㕂ࠄ JLabel ᇍ䈵ЁএDŽ ⫼ᮄⱘᆑᑺЎখ᭄এ䇗⫼ getScaledInstance ᮍ⊩ᕫࠄᮄⱘ ImageIcon ᇍ䈵ˈ᳔ৢজ䇗⫼ JLabel ᮍ⊩ᕫࠄᆑᑺˈᑊ⫼䖭Ͼᆑᑺ੠㓽ᬒ↨՟ⳌЬᕫࠄᮄⱘᆑᑺDŽ ⫼ getIcon ᮍ⊩㦋ⱘ ImageIcon ೒⠛ᇍ䈵ˈབᵰ䖭Ͼᇍ䈵ϡЎぎˈህҢ䖭Ͼᇍ䈵Ё䇗⫼ getIconWidth ᰃ䗮䖛 isEnlarge এᕫࠄ㓽ᬒⱘ↨՟˄ᬒ໻ᰃ໻Ѣ 1ˈ㓽ᇣᰃ 0 Ϣ 1 П䯈˅ˈ᥹ϟᴹҢ Jlable Ёܜ佪 } } frame.getLabel().setIcon( newIcon ); //ᬍবᰒ⼎ⱘ೒⠛ .getScaledInstance( width,-1,Image.SCALE_DEFAULT) ); ImageIcon newIcon = new ImageIcon( icon.getImage() //㦋পᬍব໻ᇣৢⱘ೒⠛ int width = (int)(icon.getIconWidth() * enLargeRange); if( icon != null ) { ImageIcon icon = (ImageIcon)frame.getLabel().getIcon(); //㦋পⳂࠡⱘ೒⠛ double enLargeRange = isEnlarge ? 1 + range : 1 - range; //㦋পᬒ໻៪㗙㓽ᇣⱘЬ↨ public void zoom( ViewerFrame frame, boolean isEnlarge ) { ҷⷕ⏙ऩ˖code\viewer\src\org\crazyit\viewer\ViewerService.java ডПᰃ㓽ᇣDŽ ᮍ⊩˄zoom˅Ёˈ⫼খ᭄ isEnlarge ᰃҷ㸼ᬒ໻៪㗙㓽ᇣⱘDŽབᵰ isEnlarge ㄝѢ trueˈህҷ㸼ᰃᬒ໻ˈ Image Ё᳝ϔϾি getScaledInstance ⱘᮍ⊩ˈ㛑ḍ᥂ᆑᑺএᣝ↨՟ᬍব೒⠛ⱘ໻ᇣDŽ೼䖭Ͼ㓽ᬒ 3.4.4 ᬒ໻៪㗙㓽ᇣ೒⠛ ᇍ䈵ⱘ setIcon ᮍ⊩ˈᡞ೒⠛䆒㕂䖯এˈህᅠ៤њᰒ⼎೒⠛ⱘ䖛⿟DŽ ᳔ৢˈ⫼ᔧࠡ䗝ᢽࠄⱘ᭛ӊЎখ᭄ᮄᓎϔϾ ImageIcon ᇍ䈵ˈᑊ䇗⫼ ViewerFrame ᇍ䈵Ё JLabel currentFiles Ё㓧ᄬ䍋ᴹDŽ 䆌ⱘ㉏ൟˈህᡞ䖭Ͼ᭛ӊࡴࠄܕⱘ᭛ӊ䖛Ⓒ఼ˈབᵰ䇏পࠄⱘ᭛ӊ㉏ൟሲѢᔧࠡⱘ᭛ӊ䖛Ⓒ఼Ё 䇗⫼ ViewerFileFilter Ёⱘ getChoosableFileFilters()ᮍ⊩㦋প៥Ӏ㞾ᅮНܜˈ೼䇏প᭛ӊⱘ䖛⿟Ё Ѣ⦄೼ᠧᓔⱘ᭛ӊ།ˈ䙷М䆕ᯢ᭛ӊ།ⱘ䏃ᕘ᳝ᬍবˈህ䇏প䖭Ͼ᭛ӊ།ϟ䴶ⱘ᠔᳝᭛ӊDŽ བᵰ currentDirectory˄ᔧࠡ᭛ӊ།˅Ўぎ˄䆕ᯢᰃ㄀ϔ⃵ᠧᓔ᭛ӊ˅៪㗙ᰃ currentDirectory ϡㄝ ᅗ᪡԰ⱘᯊ׭ˈ䖭䞠ህ㦋পᔧࠡⱘ᭛ӊ䏃ᕘϢᔧࠡⱘ᭛ӊ།DŽ݊خˈПࠡ ViewerFileChooser ᇍ䈵ⱘ showOpenDialog ᮍ⊩ᔍߎϔϾ᭛ӊ䗝ᢽḚˈ೼⫼᠋᳾䗝ᢽ೒⠛ ⫼ܜ佪 } } frame.getLabel().setIcon( icon ); if( this.currentFiles != null && !this.currentFiles.isEmpty() ) { int index = this.currentFiles .indexOf( this.currentFile ) ; //ᠧᓔϞϔϾ if( index > 0 ) { File file = (File)this.currentFiles.get( index - 1); ImageIcon icon = new ImageIcon( file.getPath() ); frame.getLabel().setIcon( icon ); this.currentFile = file; } } } བᵰ currentFile Ϣ currentFiles 䛑ϡЎぎ˄䆕ᯢᔧࠡᰃ᳝ᠧᓔ᭛ӊⱘ˅ˈ䙷ህ⫼ currentFile Ң currentFiles Ёᕫࠄᔧࠡ᭛ӊⱘ㋶ᓩˈᑊᡞ䖭Ͼ㋶ᓩޣ 1ˈএ㦋পϞϔϾ᭛ӊDŽ 㦋পࠄϞϔϾ᭛ӊৢˈ䇗⫼ File ㉏ⱘ getPath()ᮍ⊩ᕫࠄ᭛ӊⱘ䏃ᕘˈ✊ৢҹ䖭ϾЎখ᭄ᴹ߯ᓎϔ Ͼ ImageIconˈ᳔ৢᡞᅗ䆒㕂ࠄ JLabel ЁDŽ 3.5 ᭛ӊ䗝ᢽϢ䖛Ⓒ Փ⫼ JFileChooser ߯ᓎ᭛ӊᇍ䆱Ḛ⌕⿟ᰃܜՓ⫼ᵘ䗴఼߯ᓎϔϾ JFileChooser ᇍ䈵ˈ✊ৢ䇗⫼ JFileChooser ᇍ䈵ⱘ showXXXDialog ⱘᮍ⊩ᰒ⼎᭛ӊᇍ䆱Ḛˈབᵰ䳔㽕ᇍ᭛ӊ䖯㸠䖛Ⓒˈህ䳔㽕䇗⫼ addChoosableFileFilter(FileFilter filter)ᮍ⊩⏏ࡴ᭛ӊ䖛Ⓒ఼ˈ㾕ҹϟҷⷕDŽ ҷⷕ⏙ऩ˖code\viewer\src\org\crazyit\viewer\ViewerFileChooser.java private void addFilter() { this.addChoosableFileFilter( new MyFileFilter( new String[]{".BMP"}, "BMP (*.BMP)" ) ); this.addChoosableFileFilter( new MyFileFilter( new String[]{".JPG",".JPEG",".JPE",".JFIF"}, "JPEG (*.JPG;*.JPEG;*.JPE;*.JFIF)") ); this.addChoosableFileFilter( new MyFileFilter( new String[]{".GIF"}, "GIF (*.GIF)" ) ); this.addChoosableFileFilter( new MyFileFilter( new String[]{".TIF",".TIFF"}, "TIFF (*.TIF;*.TIFF)" ) ); this.addChoosableFileFilter( new MyFileFilter( new String[]{".PNG"}, "PNG (*.PNG)" ) ); this.addChoosableFileFilter( new MyFileFilter( new String[]{".ICO"}, "ICO (*.ICO)" ) ); this.addChoosableFileFilter( new MyFileFilter( new String[]{".BMP",".JPG",".JPEG",".JPE",".JFIF", ".GIF",".TIF",".TIFF",".PNG",".ICO"}, "᠔᳝೒ᔶ᭛ӊ") ); } 䖭䞠ᰃᡞ bmpˈjpgˈgif ㄝ㉏ൟⱘ᭛ӊ䖛Ⓒ఼䛑ࡴࠄ JFileChooser Ёˈ⬭ᛣࠄ䖭䞠ᰃ䇗⫼ MyFileFilter( String[] suffarr,String decription )䖭Ͼᵘ䗴఼এ߯ᓎϔϾ FileFilerˈ㄀ϔϾখ᭄ᰃৢ㓔ৡˈ ㄀ѠϾখ᭄ᰃᦣ䗄ˈ㾕ҹϟҷⷕDŽ ҷⷕ⏙ऩ˖code\viewer\src\org\crazyit\viewer\ViewerFileChooser.java public MyFileFilter( String[] suffarr,String decription ) { super(); Download at http://www.pin5i.com/ /Download at http://www.pin5i.com Ԣ⿟ᑣⱘ㗺ড়ˈ㓪ݭ᳈໮ৃᠽሩⱘ⿟ᑣDŽ ޣ೼ᴀゴЁ߱ℹՓ⫼њ Java ⱘডᇘˈ೼ҹৢⱘゴ㡖Ёˈ៥ӀӮ᳈໮ⱘՓ⫼৘⾡ Java ᡔᴃˈⳂⱘᰃЎњ ԰DŽ೼ᴀゴᅲ⦄Ꮉ݋ᷣ⚍ߏᯊˈ៥ӀՓ⫼њ Java ⱘডᇘᴹ߯ᓎ݋ԧ݋ԧⱘᶤϾᎹ݋ᷣ Action ㉏ˈ៥Ӏ ϔᓴāǃĀϟϔᓴā೒⠛ⱘࡳ㛑ˈᑊ䅽䇏㗙ԧӮࠄৃҹᗢḋᄤএ᪡԰ϔᓴ೒⠛ˈ՟བᬍব೒⠛ⱘ໻ᇣㄝ᪡ ⫼њ AbstractAction এ߯ᓎᣝ䪂ˈᑊડᑨᣝ䪂џӊDŽ೼ҷⷕЁˈՓ⫼ List 㓧ᄬⱘᡔᎻㅔऩএᅲ⦄њĀϞ ᴀゴ䗮䖛೒⠛⌣㾜఼ⱘ෎ᴀᅲ⦄ˈ৥䇏㗙ҟ㒡њ JFileChooser ᭛ӊ䗝ᢽḚϢ FileFilter ⱘ⫼⊩ˈՓ 3.6 ᴀゴᇣ㒧 ᵰDŽ ࠄℸˈ៥ӀᭈϾ೒⠛⌣㾜఼Ꮖ㒣ᅲ⦄ˈᴀゴ᠔⍝ঞⱘݙᆍ䕗ᇥˈࠄ⦄೼ৃ㛑䖤㸠⿟ᑣᶹⳟ݋ԧⱘᬜ } return f.isDirectory(); //བᵰᰃⳂᔩˈ䖨ಲ true,៪㗙䖨ಲ false } } return true; if ( f.getName().toUpperCase().endsWith( s ) ) { for ( String s : suffarr ) { //བᵰ᭛ӊⱘৢ㓔ৡড়⊩ˈ䖨ಲ true public boolean accept( File f ) { ҷⷕ⏙ऩ˖code\viewer\src\org\crazyit\viewer\ViewerFileChooser.java MyFileFilter 㒻ᡓњ FileFilterˈ៥Ӏ䖭䞠䞡ݭᅗⱘ accept ᮍ⊩ˈএᅮН䖛Ⓒⱘ㾘߭ˈ㾕ҹϟҷⷕDŽ } this.decription = decription; this.suffarr = suffarr; ㄀ 3 ゴ ೒⠛⌣㾜఼g14g /Download at http://www.pin5i.com 䈵DŽ៥Ӏ೼؛Ͼᯊ䯈ᇥѢҎⱘ㾚乥᱖⬭ᯊ䯈˅ህ䞡ᮄ㒬ࠊϔᐙ⢊ᗕᬍবⱘ೒⠛ˈህ㛑䗴៤䖭⾡Āࡼāⱘ 䈵ˈা㽕↣䱨ϔ↉ᯊ䯈˄䖭؛⾡ㅔऩഄᴹ䇈ˈࡼ⬏ᰃ߽⫼Ҏⱘ㾚㾝᱖⬭ⱘ⫳⧚⡍ᗻˈᅲ⦄ߎᴹⱘϔ 4.1.1 ࡼ⬏ॳ⧚ ೒ 4.1 Ḡ䴶ᔍ⧗ Ḡ䴶ᔍ⧗␌៣ⱘᬜᵰབ೒ 4.1 ᠔⼎DŽ Џ㽕ᰃձ䴴䖭Ͼᮍ⊩Ёⱘ Graphics ㉏ൟⱘখ᭄ˈᇚՓ⫼ Java Ёⱘ Timer এ䞡໡㒬೒ˈѻ⫳ࡼ⬏ᬜᵰˈ ೼ᴀゴЁˈᇚҟ㒡ϢՓ⫼ Java ⱘ㒬೒ࡳ㛑ˈՓ⫼ࠄ JPanel ⱘ paint(Graphics g)ᮍ⊩এ㒬೒ˈ㒬೒ ⱘᅲ⦄ॳ⧚DŽ ᣵᵓব䭓ǃবⷁˈᇣ⧗࿕࡯ࡴᔎㄝㄝDŽᴀゴЏ㽕ҟ㒡བԩᅲ⦄ϔϾㅔऩⱘᔍ⧗␌៣ˈ䅽䇏㗙њ㾷Āࡼ⬏ā 䱰⹡⠽ࠄ䖒ϟϔ݇ˈ೼䱰⹡⠽㹿⍜䰸ⱘ䖛⿟Ёˈৃ㛑Ӯѻ⫳ϔѯ㛑ᬍবᣵᵓ៪㗙ᇣ⧗⢊ᗕⱘ⠽કˈ՟བ˖ ⥽ᆊ᥻ࠊϔϾৃҹᎺে⿏ࡼⱘᣵᵓএᬍব䖤ࡼЁᇣ⧗ⱘ⿏ࡼᮍ৥ˈⳂⱘᰃ⫼ᇣ⧗⍜䰸␌៣ሣᐩЁⱘ᠔᳝ Ḡ䴶ᔍ⧗ᰃ␌៣Ёᐌ㾕ⱘ␌៣ˈҢҹࠡⱘᥠϞ␌៣ᴎࠄབҞⱘ᠟ᴎ␌៣ˈ䛑ᰃϔϾकߚ㒣݌ⱘ␌៣DŽ 4.1 Ḡ䴶ᔍ⧗ὖ䗄 ㄀  ゴḠ䴶ᔍ⧗ /Download at http://www.pin5i.com ᣵᷣ≵᳝ᣵԣᇣ⧗˅DŽ˄ޚ␌៣䕧䌶ⱘᷛ ␌៣Ёᑊϡ⍝ঞ໡ᴖⱘ⌕⿟ˈা䳔㽕໘⧚␌៣ⱘ䕧䌶ेৃˈ಴ℸ೼ᅲ⦄ⱘ䖛⿟Ёˈ݇䬂ᰃབԩ⹂ᅮ ೒ 4.3 ␌៣⌕⿟ ⥽ᆊЎ䕧DŽ݋ԧⱘ␌៣⌕⿟བ೒ 4.3 ᠔⼎DŽ ᬜᵰӮ⫳ᬜDŽབᵰ⍜䰸њ᠔᳝ⱘ䱰⹡⠽ˈህ߸ᮁ⥽ᆊЎ䌶ˈབᵰᣵᵓ≵᳝᥹ԣ৥ϟ⿏ࡼⱘᇣ⧗ˈህ߸ᮁ ᥝ䱰⹡⠽ⱘᯊ׭ˈ᳝ᴎ⥛ѻ⫳ϔѯ⠽કˈѻ⫳ⱘ⠽કӮⳈ㒓৥ϟ⿏ࡼˈ⫼ᣵᵓ᥹ԣ⠽કৢˈ⠽કⱘ⡍⅞ ᇣ⧗ПࠡˈӮϔⳈ໘Ѣ␌៣⢊ᗕˈ೼䖭Ͼ⢊ᗕЁˈᇣ⧗ӮϔⳈ໘ѢⳈ㒓䖤ࡼ៪㗙ᬍবᮍ৥ˈᔧᇣ⧗⍜䰸 ⥽ᆊՓ⫼Ꮊেᮍ৥䬂ᓔྟ␌៣Ϣ᥻ࠊᣵᵓˈ೼᳾⍜䰸ᅠ᠔᳝ⱘ䱰⹡⠽៪㗙ᣵᵓ≵᳝ḷԣ৥ϟ⿏ࡼⱘ 4.2 ⌕⿟ᦣ䗄 ೒ 4.2 ᇣ⧗ⱘডᔍ ᅮ䖭ѯᇣ㒚㡖ˈ䖭ḋ೼ᓔথⱘ䖛⿟Ёˈ៥Ӏህৃҹᣝ✻䖭ѯᇣ㒚㡖এ䗤ℹᅲ⦄៥Ӏⱘ⿟ᑣDŽ 䅽ᇣ⧗ডᔍߎ⦄ᇥ䆌㓪⿏ᰃЎњ䅽␌៣๲ࡴ⚍ϡ⹂ᅮᗻˈ๲ࡴ␌៣ⱘ䍷ੇᗻDŽ៥Ӏ䳔㽕೼㓪ݭ␌៣ࠡ⹂ ⱘᮍᓣডᔍˈབ೒ 4.2 ᠔⼎DŽ⿏أ೼ᴀゴᅲ⦄ⱘ䖛⿟Ёˈ៥ӀӮ䆒㕂ᇣ⧗Ѣᇍ⿄ⱘᮍᓣˈᑊߎ⦄ᇥ䆌 4.1.2 ᇣ⧗ডᔍⱘᮍ৥ ⃵㒬⬏䛑䳔㽕ᬍব㒬⬏ⱘⳌ݇ؐˈህৃҹ䖒ࠄĀࡼ⬏āⱘᬜᵰDŽ ⿟ᑣЁϡᮁⱘ䖯㸠㒬⬏˄Փ⫼ repaint ᮍ⊩˅ˈᇍ⿟ᑣᴹ䆆ˈা䳔㽕೼ⷁᯊ䯈ݙ䖯㸠໮⃵ⱘ㒬⬏ˈᑊϨ↣ /Download at http://www.pin5i.com ⱘ㗺ড়ǃᦤ催ݙ㘮ǃ㓪ݭߎӬ⾔ⱘҷⷕDŽ ࡴܹ៪㗙ᬍব৘Ͼ㉏ⱘ݇㋏៪㗙⿟ᑣⱘ㒧ᵘˈԚ᳔㒜䛑ᰃЎ䰡Ԣ⿟ᑣˈމ䖛⿟Ёˈ៥Ӏৃҹḍ᥂ᅲ䰙ᚙ ヨ㗙೼䖭䞠ᦤկњᴀゴⱘ㉏೒ˈᰃЎњ䅽䇏㗙ৃҹ᳈⏙᱄ⱘњ㾷ᴀゴ⿟ᑣⱘ㒧ᵘˈԚ೼ᅲ⦄ᓔথⱘ ೒ 4.4 Ḡ䴶ᔍ⧗㉏೒ ៥Ӏᬒࠄ BallService ㉏Ёএ໘⧚ˈᴀゴ㉏ⱘ݇㋏བ೒ 4.4 ᠔⼎DŽ ⠛ˈℸ㉏䖬ᅠ៤⬠䴶ⱘ߱ྟ࣪ˈⲥ਀⫼᠋ⱘ䬂Ⲭˈ㗠Ϣ␌៣Ⳍ݇ⱘϮࡵ䘏䕥˄߸ᮁ䕧䌶៪㗙⧗ⱘ䖤ࡼ˅ˈ ⹂ᅮњ៥Ӏ␌៣Ёⱘ᠔⍝ঞⱘᇍ䈵ৢˈ៥Ӏ䖬䳔㽕ϔϾ BallFrame ㉏এ߯ᓎϔϾ⬏ᵓˈ⫼Ѣ㒬ࠊ೒ ៤ᢑ䈵㉏ˈᑊᇚ䖭ѯᮍ⊩ব៤ᢑ䈵ⱘDŽخᵰ䆹⠊㉏Ёᶤѯᮍ⊩ᑊϡ䳔㽕⬅⠊㉏ᅲ⦄ˈ៥Ӏৃҹᇚ⠊㉏ Ϩ㾝ᕫৃҹՓ⫼䖭ѯ⡍ᗻ៪㗙㸠Ўᵘ៤ϔϾᇍ䈵ˈ䙷МৃҹᓎゟϔϾᮄⱘᇍ䈵԰Ў䖭ѯᇍ䈵ⱘ⠊㉏DŽབ Stick ⱘ䭓ᑺব䭓៪㗙বⷁDŽ೼ᑇᯊⱘᓔথЁˈབᵰথ⦄໮Ͼᇍ䈵П䯈᳝ϔѯ݅ৠⱘ⡍ᗻ៪㗙㸠Ўˈᑊ Փ䘧݋ࡳ㛑ᅲ⦄ⱘᢑ䈵ᮍ⊩ˈկ݊ᄤ㉏ᅲ⦄)DŽ䘧݋㉏ⱘᄤ㉏᳝ LongMagic Ϣ ShortMagicˈ԰⫼ᰃՓ ㉏ˈℸ㉏Ё᳝ϔϾ⫼ѢڣBall ㉏˄᥻ࠊᇣ⧗ⱘ⿏ࡼϢ݊ᅗࡼ԰˅ˈBrick ㉏˄ⷪഫ㉏˅ˈMagic ㉏(䘧݋ᢑ ࣙ৿䖭ѯሲᗻϢⳌ݇ⱘᮍ⊩ˈ䅽݊ᄤ㉏㒻ᡓDŽ㒻ᡓℸ㉏ⱘᄤ㉏᳝ Stick ㉏˄⫼ѢᅮНᣵᵓⱘ㸠ЎѢሲᗻ˅ˈ 㞾Ꮕⱘ x Ϣ y തᷛሲᗻˈ᳝೒⠛ሲᗻˈ᳝䗳ᑺሲᗻˈ᠔ҹˈ೼䖭ᯊ೼ˈ䆒䅵ϔϾ෎㉏ BallComponent ೼䖭Ͼ␌៣Ёˈ᳝ᣵᵓˈᇣ⧗ˈⷪഫ˄䱰⹡⠽˅ˈ䘧݋ㄝ⠽કˈ䖭ѯ⠽ક䛑᳝݅ৠⱘ⡍ᗻˈ᳝ሲѢ ߯ᓎ␌៣ᇍ䈵 4.3 /Download at http://www.pin5i.com ৠϔӊᰃˈህᰃᬍবᣵᵓⱘሩ⦄ᔶᓣDŽЎخҹᇚ䘧݋ᢑ䈵៤ব䭓ⱘ䘧݋៪㗙বⷁⱘ䘧݋ˈ㗠ᅗӀ䛑䳔㽕 ೼ᴀ՟Ёˈᣵᵓᰃৃҹব䭓៪㗙বⷁⱘˈ㗠Փᣵᵓব䭓៪㗙বⷁⱘᮍᓣᰃ䗮䖛䘧݋ᴹᅲ⦄ˈ಴ℸৃ ‰ abstract void magicDo( Stick stick )ˈ䘧݋ⱘࡳ㛑ˈ㒭݊ᄤ㉏ᅲ⦄DŽ LongMagic ㉏੠ ShortMagic ㉏ˈϸϾᄤ㉏ⱘ magicDo ᮍ⊩Ёߚ߿ᅲ⦄Փᣵᵓব䭓Ϣবⷁⱘࡳ㛑DŽ magicDo( Stick stick )ˈℸᢑ䈵ᮍ⊩ᰃᅲ⦄䘧݋ⱘᬜᵰࡳ㛑ˈ⫼Ѣ㒭݊ᄤ㉏ᅲ⦄ˈ⦄೼ᅲ⦄ⱘᄤ㉏ⱘ կϔϾ Magic( String path, int x , int y )ᵘ䗴఼এ䇗⫼⠊㉏ⱘᵘ䗴఼ˈᑊᦤկϔϾᢑ䈵ⱘᮍ⊩ Magic ㉏ᰃϔϾ䘧݋㉏ˈ೼␌៣Ё㸼⦄ࣙ৿೼ⷪഫЁˈᰃ BallComponet ⱘϔϾᢑ䈵ᄤ㉏ˈℸ㉏ᦤ 4.3.3 䘧݋㉏ঞ݊ᄤ㉏˄Magic˅ ᇚ䖭ϾѠ㓈᭄㒘Ā⬏āߎᴹDŽ ᯊ׭ˈ៥Ӏ䛑ৃҹᓎゟϔϾѠ㓈᭄㒘ˈᇚⳌᑨⱘᇍ䈵ᬒ㕂ࠄ䆹᭄㒘Ёˈᔧ⬠䴶䖯㸠㒬⬏ⱘᯊ׭ˈህৃҹ ㋴ᑊϡᰃᄫヺІˈ㗠ᰃ݋ԧⱘᶤϾ Brick ᇍ䈵ˈ೼ҹৢⱘゴ㡖Ёˈᔧ䘛ࠄ䳔㽕೼⬠䴶Ё㒬⬏ᶤѯ೒⠛ⱘ ܗ㸼⼎⬠䴶Ё᠔᳝ⱘⷪഫˈᅲ⦄ॳ⧚Ϣ᥻ࠊৄѨᄤẟЁⱘẟⲬϔḋˈԚᰃ೼ᴀゴЁˈѠ㓈᭄㒘ⱘ↣ϔϾ ⹂ᅮњϔϾⷪഫ⬅ϔϾ Brick ᇍ䈵ᴹ㸼⼎ৢˈ೼⬠䴶Ёˈ៥ӀৃҹᦤկϔϾ Brick ⱘѠ㓈᭄㒘ˈᴹ ៪㗙ਃ⫼ℸ㉏ˈdisable ⱘؐЎ true ៪㗙 falseDŽ⫼ذˈ( ‰ void setDisable( boolean disable ‰ boolean isDisable()ˈ⫼ᴹ߸ᮁℸ㉏ᰃ৺᳝ᬜDŽ ‰ Magic getMagic()ˈ㦋প䘧݋DŽ ‰ void setMagic( Magic magic )ˈ䆒㕂䘧݋DŽ ҹϟᮍ⊩˖ Brick ⱘ⢊ᗕˈབᵰ diable Ў trueˈ߭㸼ᯢℸⷪഫᏆ㒣ϡৃ⫼ˈϡӮݡᰒ⼎DŽᑊᦤկ䖭ϸϾሲᗻⳌ݇ⱘ ๲ࡴњ magic Ϣ disable ሲᗻˈmagic ҷ㸼ℸⷪഫЁ᠔ࣙ৿ⱘ䘧݋ˈ߱ྟؐЎ nullˈdisable ᰃ⫼ᴹᷛᖫ ൟ ⱘ䘧݋˗2 ҷ㸼ℸⷪഫ䞠䴶᳝ ShortMagic ㉏ൟⱘ䘧݋˗݊ᅗҷ㸼ℸⷪഫ䞠䴶≵᳝䘧݋DŽ঺໪ˈᴀ㉏ pahǃx Ϣ y খ᭄⫼Ѣ䇗⫼⠊㉏ⱘᵘ䗴఼ˈtype ᰃҷ㸼ⷪഫⱘ㉏ൟ˖1 ҷ㸼ℸⷪഫ䞠䴶᳝ LongMagic ㉏ ℸ㉏ᰃ BallComponet ⱘϔϾᄤ㉏ˈᦤկϔϾ Brick(String path, int type, int x, int y )ᵘ䗴఼ˈ݊Ё 4.3.2 ⷪഫ㉏˄Brick˅ ҹᦤկ˄བᵰ䳔㽕ⱘ䆱˅ϔѯᢑ䈵ᮍ⊩䅽ᄤ㉏এᅲ⦄ˈᑊϨৃҹ೼⠊㉏Ё䇗⫼䖭ѯᢑ䈵ᮍ⊩DŽ ᗻ៪㗙ᮍ⊩ᄬᬒࠄ䆹㉏Ёˈ಴ℸᅗ೼␌៣Ёᑊϡҷ㸼ᶤϾ݋ԧⱘᇍ䈵DŽᇚ䆹㉏߯ᓎЎᢑ䈵㉏ˈ៥Ӏህৃ ⱘⳂⱘᰃˈ೼៥ӀⱘḠ䴶ᔍ⧗␌៣Ёˈ䆹㉏ᑊϡᰃ݋ԧᄬ೼ⱘᶤϔϾᇍ䈵ˈ㗠ᰃ៥Ӏᇚϔѯ݀⫼ⱘሲخ ߯ᓎ BallComponent ⱘᯊ׭ˈ៥Ӏৃҹᇚ䖭Ͼ㉏ব៤ᢑ䈵㉏ˈेՓᅗ≵᳝ӏԩⱘᢑ䈵ᮍ⊩ˈ䖭ḋ ҹᇚ䖭ѯᬒࠄ䖭Ͼ෎㉏ЁDŽ 㗙ᬍবᇍ䈵ⱘതᷛԡ㕂Ϣ೒⠛ሲᗻDŽབᵰ៥Ӏ೼㓪ⷕⱘ䖛⿟Ёথ⦄᳝ϔѯ݅ৠⱘሲᗻ៪㗙ᮍ⊩ˈ៥Ӏৃ 䰸এ䖭ѯᵘ䗴ᮍ⊩ˈℸ㉏ᦤկњ䖭ѯሲᗻⱘ setter Ϣ getter ᮍ⊩ˈ⫼Ѣ㦋পᇍ䈵ⱘതᷛϢ೒⠛ˈ៪ ‰ BallComponent( String path , int x , int y )ˈҹ x തᷛˈy തᷛ੠ path এᵘ䗴ϔϾ BallComponetDŽ Ϣ path এᵘ䗴ϔϾ ComponentDŽ ‰ BallComponent( int panelWidth , int panelHeight, String path )ˈҹ panelWidthˈpanelHeightˈ ೼ℸᵘ䗴ᮍ⊩Ёˈᇚḍ᥂䏃ᕘএ䇏প೒⠛ˈݡ䆒㕂ᇍ䈵Ё image ሲᗻDŽ ‰ BallComponent( String path )ˈpath ᰃ೒⠛ⱘ䏃ᕘˈ⫼೒⠛ⱘ䏃ᕘᴹᵘ䗴ϔϾ BallComponentˈ ߱ྟؐЎ null˗䗳ᑺ speedˈ߱ྟؐЎ 5DŽḍ᥂ϡৠⱘ䳔㽕ˈᦤկҹϟϝϾᵘ䗴ᮍ⊩˖ ⠊㉏ˈᅮНњ䖭ѯᄤ㉏᳝݅ⱘሲᗻϢᮍ⊩ˈሲᗻ᳝˖x തᷛˈ߱ྟؐЎ-1˗y തᷛˈ߱ྟؐЎ-1˗೒⠛ imageˈ Ў Brick˄ⷪഫ˅㉏ǃMagic˄䘧݋˅㉏ǃStick˄ᣵᵓ˅㉏ǃBall˄ᇣ⧗˅㉏ⱘخˈBallComponent ෎㉏BallComponent 4.3.1 /Download at http://www.pin5i.com ㋴᳝⺄ᩲǃ䆒㕂ᣵᵓⱘ䭓ᑺǃ߸ᮁ⫼᠋ᰃ৺䗮݇ǃ߱ܗࡼǃ⌟䆩ᇣ⧗Ϣᣵᵓᰃ৺᳝⺄ᩲ៪㗙ᣵᵓ੠݊ᅗ BallService ໘⧚њ䖭Ͼ␌៣Ёⱘ໻䚼ߚϮࡵࡳ㛑ˈࣙᣀᓔྟ␌៣ǃᇣ⧗⿏ࡼǃ䘧݋⿏ࡼǃᣵᵓ⿏ 4.3.6 Ϯࡵ໘⧚㉏˄BallService˅ 㸔῵ᓣˈৃҹᇚ᠔᳝Ϣᇣ⧗Ⳍ݇ⱘᮍ⊩ࡴܹࠄ Ball ЁDŽ ܙࠄخࡼ˄೼␌៣㒧ᴳ៪㗙ᓔྟᯊ˅ˈ៥ӀህৃҹЎ Ball ㉏⏏ࡴϔϾ stopRun ⱘᮍ⊩ˈᘏПˈབᵰ䳔㽕 ℶ䖤ذⱘ㸠Ўˈ՟བᇣ⧗Ӯ䖤ࡼˈ៥ӀৃҹЎ Ball ㉏ࡴܹϔϾ run ⱘᮍ⊩ˈ㸼⼎⧗ⱘ䖤ࡼˈ՟བᇣ⧗Ӯ 㸔῵ᓣˈৃҹЎᇣ⧗ᇍ䈵ᦤկϔѯϢПⳌ݇ܙ䋷໘⧚ӏԩⱘϮࡵ䘏䕥DŽབᵰ䳔㽕ᇚ䆹ᇣ⧗ᇍ䈵㓪ݭ៤Ў ⱘⳟ԰ϔϾㅔऩⱘᇍ䈵ˈᑊϡ䋳䋷໘⧚ӏԩⱘ㸠Ўˈ䖭ৃҹⳟ԰៥Ӏϔ㠀᠔䇈ⱘ䋿㸔῵ᓣˈᇍ䈵ᑊϡ䋳 ℸˈϮࡵ㉏ህ䳔㽕㓈ᡸϔϾᇣ⧗ⱘᇍ䈵ˈᴹ᥻ࠊᇣ⧗ⱘ䖤ࡼ៪㗙݊Ҫ㸠ЎDŽ೼䖭䞠ˈᇣ⧗ᇍ䈵ৃҹऩ㒃 䈵ᴹᬍবᇣ⧗ⱘⳌ݇ሲᗻˈ៥Ӏ೼ࠡ䴶ⱘゴ㡖ЁᦤࠄˈᦤկϔϾϮࡵ㉏䖯䋳䋷໘⧚␌៣ⱘⳌ݇䘏䕥ˈ಴ Ёԧ⦄˅ˈབᵰ䳔㽕ᬍবᇣ⧗ⱘ䗳ᑺˈৃҹ䇗⫼Ⳍ݇ⱘ setter ᮍ⊩ᴹ䖯㸠ˈԚᰃ៥Ӏ䳔㽕ⶹ䘧⬅ાѯᇍ ೼ᴀ՟Ёˈᇣ⧗ᇍ䈵াֱᄬϔѯⳌ݇ⱘሲᗻˈ՟བ῾৥䗳ᑺϢ㒉৥䗳ᑺ˄೒⠛ǃԡ㕂Ϣ໻ᇣ೼⠊㉏ ‰ int getSpeedY()ˈ㦋পᇣ⧗ⱘオ৥䗳ᑺDŽ ‰ int getSpeedX()ˈ㦋পᇣ⧗ⱘ῾৥䗳ᑺDŽ ‰ void setStarted( boolean b )ˈᡞᇣ⧗⢊ᗕ䆒㕂Ў䖤ࡼ៪㗙䴭ℶDŽ ‰ boolean isStarted()ˈᇣ⧗ᰃ৺Ꮖ㒣೼䖤ࡼDŽ ‰ setSpeedY( int speed )ˈ䆒㕂ᇣ⧗ⱘオ৥䗳ᑺDŽ ‰ setSpeedX( int speed )ˈ䆒㕂ᇣ⧗ⱘ῾৥䗳ᑺDŽ 䖬Ў䖭ѯሲᗻᦤկⳌᑨⱘ setter Ϣ getter ᮍ⊩ˈབϟ˖ ໘Ѣ䴭ℶ⢊ᗕˈԚϡ㛑ݡ⿏ࡼˈৠḋˈ⫼ϔϾ stop ሲᗻᴹᷛᖫᇣ⧗ᰃ৺㛑ݡ⿏ࡼDŽ䰸њᅮН䖭ѯሲᗻˈ ᇣ⧗ᰃ໘Ѣ䴭ℶ⢊ᗕˈ᠔ҹ⫼ϔϾ started ሲᗻᴹᷛᖫᇣ⧗ᰃ৺Ꮖ㒣ᓔྟ䖤ࡼDŽ␌៣㒧ᴳৢˈᇣ⧗гᰃ ᑺⱘ᭰ᮍ৥ˈ᠔ҹ៥Ӏᡞᇣ⧗ⱘ䗳ᑺߚ㾷៤῾৥䗳ᑺϢオ৥䗳ᑺ˄speedX Ϣ speedY˅ˈ␌៣᳾ᓔྟࠡˈ Ball ㉏гᰃ BallComponet ⱘᄤ㉏ˈ⬅Ѣᇣ⧗೼␌៣䴶ᵓЁ䖤ࡼⱘᯊ׭䰸њ῾オᮍ৥ˈ䖬᳝৘⾡㾦 4.3.5 ᇣ⧗㉏˄Ball˅ 䖭ѯሲᗻᏆ㒣೼ BallComponet Ёԧ⦄DŽ 䭓ᑺᴹᅲ⦄ᴀ՟Ё᠔䳔㽕ᅲ⦄ⱘ䭓ⷁᣵᵓࡳ㛑DŽ೼ Stick ㉏Ёᑊϡ䳔㽕݇ᖗᣵᵓⱘ೒⠛ǃԡ㕂Ϣ໻ᇣˈ Ͼᣵᵓᇍ䈵ˈ᠔ҹᣵᵓ㉏ᖙ乏ࣙᣀ䭓ᑺⱘሲᗻˈ䖭ḋˈ೼ᅲ⦄䘧݋㉏ⱘᯊ׭ˈህৃҹ䗮䖛ᬍবᣵᵓ㉏ⱘ ៪㗙বⷁⱘˈᑊϨ೼ᓎゟ䘧݋ᢑ䈵㉏ⱘᯊ׭ˈᏆ㒣ᅮНњϔϾ magicDo ⱘᮍ⊩ˈ䆹ᮍ⊩ⱘখ᭄ህᰃϔ ⬅Ѣ䆹㉏㒻ᡓѢ BallComponet ㉏ˈ಴ℸা䳔㽕ᦤկϔϾᵘ䗴఼ेৃDŽ೼ᴀ՟Ёˈᣵᵓᰃৃҹব䭓 ‰ int getPreWidth()ˈ㦋প߱ྟ䭓ᑺDŽ ‰ void setPreWidth( int preWidth )ˈ䆒㕂߱ྟ䭓ᑺDŽ ℸᮍ⊩ⱘ setter Ϣ getter ᮍ⊩ˈབϟ˖ ᠔ҹˈℸ㉏ЁাᅮНњᣵᵓ x ᮍ৥ⱘ⿏ࡼ䗳ᑺ SPEED ˈ䖬᳝ᅮНᣵᵓⱘ߱ྟ䭓ᑺ preWidth ˈᑊᦤկ ৠḋˈStick ㉏гᰃ BallComponet ⱘᄤ㉏ˈ⫼ᴹҷ㸼␌៣Ёⱘᣵᵓˈ⬅Ѣᣵᵓা᳝Ꮊে⿏ࡼⱘˈ 4.3.4 ᣵᵓ㉏˄Stick˅ կ᳈໮ⱘখ᭄DŽ བᵰҹৢ␌៣Ёߎ⦄঺໪ϔ⾡䘧݋ˈӮᬍব⧗ⱘ䗳ᑺ˄বᖿ៪㗙ব᜶˅ˈ䙷М៥Ӏህ䳔㽕Ў䆹ᢑ䈵㉏ᦤ ᯊ׭ˈህৃҹЎ䆹㉏⏏ࡴᄤ㉏ˈᑊᦤկϡৠⱘᅲ⦄DŽᔧ✊ˈ䖭䞠াᦤկϔϾ Stick ⱘখ᭄ৃ㛑ᑊϡ໳ˈ њ⿟ᑣⱘৃᠽሩᗻˈ៥Ӏ೼䖭䞠ᇚϔϾ䘧݋বЎϔϾᢑ䈵㉏˄Magic˅ˈᔧ៥Ӏ䳔㽕᳝݊Ҫᔶᓣⱘ䘧݋ⱘ /Download at http://www.pin5i.com 䕥ⱘᯊ׭˄⧗ⱘ䖤ࡼǃ䘧݋ⱘϟ㨑ㄝ˅ˈ៥Ӏৃҹ䇗⫼ JPanel ⱘ repaint ᮍ⊩ᇚ JPanel 䖯㸠䞡㒬DŽ Ͼ JPanel ᇍ䈵ेৃˈ಴Ў␌៣ⱘ⬠䴶ᑊ≵᳝໮໡ᴖⱘᏗሔϢ⬠䴶ѸѦDŽᔧ៥Ӏᅲ⦄␌៣ⱘϔѯⳌ݇䘏 ᑨ䆹ᰃϔϾা䆒㕂њにষᷛ乬䖬᳝买㡆ㄝ෎ᴀሲᗻⱘ JFrameˈ೼䖭Ͼ JFrame Ёˈ៥Ӏা䳔㽕ᦤկϔ ㋴䛑ᰃ⫼ Graphics ᇍ䈵⬏ߎᴹⱘˈ᠔ҹˈ៥ӀⱘЏ⬠䴶ܗ೼䖭ϾḠ䴶ᔍ⧗␌៣Ёˈ␌៣Ёⱘ᠔᳝ 4.4 Џ⬠䴶ᅲ⦄ ᇥњҷⷕП䯈ⱘ㗺ড়ˈᑊ䙉ᕾњऩϔ㘠䋷ⱘॳ߭DŽޣˈҢϔᅮ⿟ᑺϞ䆆 㽕এ݇ᖗ䘧݋݋ԧⱘᅲ⦄DŽ೼߯ᓎ␌៣৘Ͼᇍ䈵ⱘ䖛⿟Ёˈ៥Ӏᇚ໘⧚䘏䕥ⱘᮍ⊩ᬒ㕂ࠄϔϾϮࡵ㉏Ёˈ 䴶৥ᇍ䈵ⱘ໮ᗕ⡍ᗻˈՓ⫼ Magic ⱘ magicDo ᮍ⊩ᴹ䖯㸠Ā䘧݋ⱘՓ⫼āˈ೼䖭Ͼ䖛⿟Ёˈ៥Ӏᑊϡ䳔 ᇚϔϾ䘧݋ᢑ䈵ЎϔϾ Magic ᇍ䈵ˈ䆹㉏ৃҹ᳝໮Ͼᅲ⦄ˈ೼Փ⫼ Magic ᇍ䈵ⱘᯊ׭ˈ៥Ӏৃҹ߽⫼ ࠄℸˈᴀゴЁ᠔᳝ⱘᇍ䈵䛑Ꮖ㒣߯ᓎᑊ⹂ᅮњᅗӀⱘ㸠Ўˈ೼ᓎゟ䘧݋㉏˄Magic˅ⱘᯊ׭ˈ៥Ӏ BallSerivce ⱘ draw ᮍ⊩ᇚᇣ⧗ⱘ೒⠛Ā⬏āࠄ⬠䴶ЁDŽ ⧚ˈᑊḍ᥂䖨ಲⱘֵᙃᴹᬍব⬠䴶DŽ՟བᇣ⧗ⱘ䖤ࡼˈ៥Ӏৃҹ䇗⫼ BallService ⱘ run ᮍ⊩ˈݡ䇗⫼ ㉏㓈ᡸϔϾ BallService ⱘᇍ䈵ˈᕫࠄ⬠䴶ЁⳌ݇ᇍ䈵ⱘֵᙃৢˈৃҹ䇗⫼ BallService Ёⱘᮍ⊩䖯㸠໘ ៥ӀՓ⫼њ BallService ㉏ᴹ໘⧚໻䚼ߚⱘ␌៣䘏䕥ˈЏ⬠䴶㉏Ё޴Тϡࣙᣀӏԩⱘ䘏䕥໘⧚ˈ䆹 ЁⱘϔϾݙ䚼㉏DŽ ᔧ⬏ᵓˈBallPanel ᰃ䖭Ͼ㉏ܙ‰ BallPanel getBallPanel()ˈ㦋পϔϾ BallPanel ㉏ൟⱘ JPanel এ ‰ void initialize() throws IOExceptionˈℸᮍ⊩ᡯߎ IO ᓖᐌˈ߱ྟ࣪⬠䴶DŽ བϟ˖ Ⲭџӊⲥ਀఼ҹঞ߯ゟϔϾTimer ↣䱨ϔᇣ↉ᯊ䯈এࠋᮄ⬏ᵓˈЏ㽕᳝߱ྟ࣪⬠䴶Ϣ៪㗙⬏ᵓϸϾᮍ⊩ˈ BallFrame ᰃ߯ᓎϔϾ JFrame Џ⬠䴶ˈ䆒㕂Џ⬠䴶ⱘᷛ乬ǃ䭓Ϣᆑǃ⬏ᵓㄝሲᗻˈᑊϨЎ๲ࡴ䬂 4.3.7 Џ⬠䴶㉏˄BallFrame˅ 㸔῵ᓣ˅ˈ՟བ䘧݋ⱘϟ㨑ǃᣵᵓⱘ⿏ࡼㄝDŽܙⱘ㉏Ё˄ेࠡ䴶ᦤࠄⱘ ␌៣ᰃ৺㚰߽ㄝDŽ೼՟Ё BallService ໘⧚њ໻䚼ߚⱘ␌៣䘏䕥ˈᔧ✊ˈ៥Ӏгৃᇚ䖭ѯ䘏䕥ᬒࠄⳌ݇ ᵓⱘԡ㕂DŽ೼ᴀゴⱘ⿟ᑣЁˈBallService ໘⧚᠔᳝ⱘⳌ݇䘏䕥ˈ՟བ߸ᮁᇣ⧗೼䖤ࡼⱘ䖛⿟Ёᰃ৺䍞⬠ǃ ␌៣Ё䗮䖛ϞǃϟǃᎺǃেⱘ䬂ᴹ᥻ࠊᣵᵓⱘԡ㕂ˈ಴ℸህ䳔㽕ᦤկϔϾ setStickPos ⱘᮍ⊩ᴹᬍবᣵ Ball ⱘ isStarted ᮍ⊩䖨ಲ trueˈे␌៣Ꮖ㒣ᓔྟˈrun ᮍ⊩ⱘЏ㽕ࡳ㛑ህᰃ䇗ᇣ⧗ⱘԡ㕂DŽ៥Ӏ䳔㽕೼ ⱘ䇗⫼ run ᮍ⊩ˈ䅽ᇣ⧗䖯㸠䖤ࡼˈᔧ✊ˈᇣ⧗䖯㸠䖤ࡼⱘࠡᦤᰃذᔧ␌៣ᓔྟᯊˈ⿟ᑣЁ䳔㽕ϡ ‰ void draw( Graphics g )ˈ⬏೒ˈᮍ⊩ЁᰃՓ⫼ Graphics ᇍ䈵 g এ⬏೒DŽ 㒘ˈখ᭄ path ᰃᣛⷪഫⱘ೒⠛ˈxSize Ϣ ySize ᰃ᭄㒘ⱘ䭓ᑺDŽ ‰ Brick[][] createBrickArr( String path, int xSize, int ySize )ˈ߯ᓎⷪഫˈ䖨ಲϔϾ Brick ㉏ൟⱘ᭄ ‰ boolean isWon()ˈ߸ᮁ⥽ᆊᰃ৺Ꮖ㒣䖛݇DŽ ‰ void setStickWidth( Magic magic )ˈḍ᥂䘧݋˄magic˅ⱘ㉏ൟএ䆒㕂ᬍবᣵᵓⱘ䭓ᑺDŽ ‰ void setMagicPos()ˈᬍব䘧݋ⱘതᷛԡ㕂DŽ ㋴Ϣᣵᵓᰃ৺᳝⺄ᩲDŽܗ‰ isHitStick( BallComponent image )ˈ⌟䆩ᶤ ‰ boolean isHitBrick( Brick brick )ˈ⌟䆩ᇣ⧗Ϣⷪഫᰃ৺᳝⺄ᩲˈখ᭄ brikc ᰃᣛⷪഫDŽ ‰ setBallPos()ˈᬍবᇣ⧗ⱘതᷛԡ㕂DŽ ‰ void setStickPos( KeyEvent ke )ˈᬍবᣵᵓⱘതᷛԡ㕂DŽ ‰ void run()ˈᇣ⧗䖯㸠䖤ࡼDŽ ࣪ⷪഫⱘᥦ߫Ϣ䘧݋ǃ⬏೒ㄝࡳ㛑DŽ䖭ѯࡳ㛑ⱘᅲ⦄䛑᳝ᇍᑨⱘᮍ⊩ˈབϟ˖ྟ /Download at http://www.pin5i.com ѯᇍ䈵ᇚӮϹ䞡⍜㗫㋏㒳䌘⑤ˈ䙷М៥Ӏৃҹ㗗㰥ᇚ䆹ᇍ䈵ݭ៤ऩᗕⱘDŽ ऩᗕ῵ᓣDŽབᵰ䘛ࠄϔѯᇍ䈵ᑊϡ䳔㽕໮⃵߯ᓎ៪㗙߯ᓎ䖭خऩᗕⱘDŽ೼ӫ໮ⱘ䆒䅵῵ᓣЁˈ᳝ϔ⾡ি ⱘᮍ⊩ᴹ㦋ᕫ BallPanel ⱘᅲ՟ˈ⬅Ѣ BallPanel ᑊϡ䳔㽕↣⃵এ߯ᓎˈ᠔ҹ៥Ӏৃҹᇚ BallPane ব៤ ೼䖭䞠䳔㽕⊼ᛣⱘᰃˈ៥Ӏ䳔㽕೼ BallFrame Ё㓈ᡸϔϾ BallPanel ⱘᇍ䈵ˈ✊ৢ䗮䖛 getBallPanel } return ballPanel; } new Dimension( BALLPANEL_WIDTH, BALLPANEL_HEIGHT ) ); ballPanel.setPreferredSize( //䆒㕂⬏ᵓⱘ໻ᇣ ballPanel = new BallPanel(); //ᮄᓎϔϾ⬏ᵓ if ( ballPanel == null ) { public BallPanel getBallPanel() { ҷⷕ⏙ऩ˖code\ball\src\org\crazyit\ball\BallFrame.java ↣⃵া᳝ϔϾ BallPanle ᇍ䈵ˈ㾕ҹϟҷⷕDŽ 㗠㦋প䖭Ͼ BallPanel ᅲ⦄ᰃ೼ BallPanel getBallPanel ᮍ⊩Ёˈℸ㉏ֱ䆕䖭Ͼ Panel ᰃऩᗕⱘˈ } } //ৃҹ䇗⫼ BallService ⱘ draw ᮍ⊩䖯㸠㒬ࠊ public void paint( Graphics g ) { */ * @return void * @param g Graphics * * 䞡ݭ void paint( Graphics g )ᮍ⊩ /** public class BallPanel extends JPanel { //ᅮНϔϾ JPanel ݙ䚼㉏ᴹᅠ៤⬏೒ࡳ㛑 ҷⷕ⏙ऩ˖code\ball\src\org\crazyit\ball\BallFrame.java BallPanle ᇍ䈵ˈBallPanel ᰃᴀ㉏ⱘϔϾݙ䚼㉏ˈᑊϨ㒻ᡓ JPanelˈ㾕ҹϟҷⷕDŽ ⳟࡴ㉫ⱘϔ㸠ҷⷕ ballPanel = getBallPanel()ᰃ䇗⫼ᴀ㉏Ёⱘ getBallPanel()ᮍ⊩এ㦋পϔϾ } this.add( ballPanel ); //ᡞ⬏ᵓࡴࠄ JFrame ballPanel = getBallPanel(); //㦋প⬏ᵓ this.setBackground( Color.BLACK ); //䆒㕂㚠᱃Ў咥㡆 this.setResizable( false ); //䆒㕂Ўϡৃᬍব໻ᇣ this.setTitle("ᔍ⧗"); //䆒㕂にষⱘᷛ乬 public void initialize() throws IOException { ҷⷕ⏙ऩ˖code\ball\src\org\crazyit\ball\BallFrame.java JPanel ⬏ᵓࡴࠄ JFrame Ёˈ㾕ҹϟҷⷕDŽ 䆒㕂 JFrame にষⱘᷛ乬ǃ㚠᱃买㡆Ϣᰃ৺ৃҹᬍব໻ᇣˈ✊ৢ㦋প JPanel ᇍ䈵ˈ᳔ৢᡞˈܜ佪 ߱ྟ࣪⬠䴶˄initialize()ᮍ⊩˅ 4.4.1 /Download at http://www.pin5i.com ೒ 4.5 ߱ྟ࣪␌៣ᯊⱘ⬠䴶 ᔧࠡ⿟ᑣⱘᬜᵰབ೒ 4.5 ᠔⼎DŽ ballFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ballFrame.setVisible(true); ballFrame.pack(); BallFrame ballFrame = new BallFrame(); ⱘҷⷕ˖ 㓪ݭњ BallFrame ⱘ߱ྟ࣪ҷⷕৢˈ៥Ӏৃҹ䖤㸠݋ԧᶹⳟⳌ݇ⱘ␌៣ᬜᵰDŽ㓪ݭ߯ᓎ BallFrame 4.4.3 䖤㸠ᬜᵰ new ݇䬂ᄫᴹ߯ᓎˈ䖭ህ᮴⊩ֱ䆕䆹ᇍ䈵ⱘᅲ՟ⱘଃϔᗻDŽ ໪䬂ৃҹՓ⫼ new ݇䬂ᄫᴹ߯ᓎ䆹㉏ⱘᅲ՟ˈ䙷Мህᛣੇⴔ䆹㉏ᇚϡӮᰃऩᗕˈ᳝ৃ㛑໪⬠໮⃵䗮䖛 ˄Ў໪⬠ᦤկଃϔⱘ䆓䯂⚍˅ˈ⾕᳝ᵘ䗴఼ᰃЎњϡ䅽໪⬠এՓ⫼ new ݇䬂ᄫᴹ߯ᓎ䆹㉏ⱘᅲ⦄ˈབᵰ ЎϔϾ㉏ᅲ⦄ऩᗕ῵ᓣˈ䳔㽕Ў䆹㉏ᦤկϔϾ⾕᳝ⱘᵘ䗴఼ˈݡᦤկϔϾৃҹ㦋প䆹㉏ᅲ⦄ⱘᮍ⊩ Ԣњ㋏㒳೼߯ᓎᇍ䈵ᯊⱘᓔ䫔DŽ ޣᓣˈ㋏㒳ৃҹϡᖙ໮⃵߯ᓎ䆹ᇍ䈵ⱘᅲ՟ˈ໪⬠Փ⫼ⱘᯊ׭ৃҹՓ⫼ৠϔϾᅲ՟ˈ಴ℸ೼ϔᅮ⿟ᑣϞ ⱘᇍ䈵ˈ՟བ JDBC ⱘ Connection ᇍ䈵ˈ䙷М៥Ӏህৃҹ߽⫼ऩᗕ῵ᓣᴹ߯ᓎ䖭ѯᇍ䈵DŽ՟བऩᗕ῵ ⬠ৃҹ䗮䖛䖭Ͼ䆓䯂⚍ᴹ䆓䯂䆹㉏ⱘଃϔᅲ՟DŽ೼៥Ӏᑇᯊᓔথⱘ䖛⿟ЁˈӮ䘛ࠄϔѯϡ䳔㽕໮⃵߯ᓎ ऩᗕ῵ᓣгৃҹিऩ՟῵ᓣˈ䆹῵ᓣֱ䆕ϔϾ㉏᳝Ϩҙ᳝ϔϾᅲ՟ˈᑊЎ໪⬠ᦤկϔϾ䆓䯂ˈ䅽໪ ऩᗕ῵ᓣㅔҟ 4.4.2 /Download at http://www.pin5i.com ᮍ⊩߱ྟ࣪␌៣DŽ BallService ᥻ࠊ␌៣ⱘˈгህᛣੇⴔ䖯㸠㄀Ѡ⃵␌៣ⱘᯊ׭ˈህ䳔㽕ݡ⃵䇗⫼ BallFrame ⱘ initialize ℶ˄㚰߽៪㗙༅䋹˅ˈ಴ℸ䳔㽕䇗⫼ restart ᮍ⊩䞡ᮄਃࡼDŽ⬅Ѣ៥Ӏᰃ೼ذtimer 䴲ぎˈ⬅Ѣ␌៣ 䖭䞠៥Ӏ䳔㽕ᯢⱑⱘᰃˈ㄀ϔ⃵䖯㸠␌៣ᯊˈtimer Ў nullˈህ䳔㽕䖯㸠߯ᓎˈᔧ䖯㸠㄀Ѡ⃵␌៣ⱘᯊ׭ˈ ⃵ⱘ Timerˈᑊ⫼䇗⫼ start()ᮍ⊩ਃࡼ Timerˈབᵰ timer ϡЎぎˈⳈ᥹䇗⫼ restart()ᮍ⊩ਃࡼ timerDŽ೼ 㸠ϔ⃵䖤ࡼDŽབᵰℸ㉏ⱘሲᗻ timer Ўぎˈህҹ ActionListern ᇍ䈵Ўখ᭄এ߯ᓎϔϾ↣ 100 ↿⾦ᠻ㸠ϔ ⱘᬍবҹঞ reapint ⬏ᵓˈ䖭Ͼ task ↣ 100 ↿⾦ᠻ㸠ϔ⃵ˈे↣䱨ϔⱒ↿⾦ᇣ⧗˄៪㗙݊Ҫ㒘ӊ˅Ӯᠻ Ў Timer ⱘ taskˈ䖭 Ͼ task Џ㽕ᰃ໘⧚␌៣Ё৘Ͼ㒘ӊԡ㕂خᓎゟϔϾ ActionListener ᇍ䈵ˈܜ佪 } this.addKeyListener( keyAdapter ); }; } service.setStickPos( ke ); //ᬍবᣵᵓⱘതᷛ public void keyPressed( KeyEvent ke ) { KeyListener keyAdapter = new KeyAdapter(){ //ᅮН䬂Ⲭⲥ਀䗖䜡఼ if( klarr.length == 0 ) { KeyListener[] klarr = this.getKeyListeners(); //๲ࡴџӊⲥ਀఼ } timer.start(); //ᓔྟ timer timer = new Timer( 100, task ); //ᮄᓎϔϾ timer } else { timer.restart(); //䞡ᮄᓔྟ timer if( timer != null ) { //བᵰ timer ϡЎぎˈ䇗⫼ timer ⱘ restart ᮍ⊩ }; } ballPanel.repaint(); //ࠋᮄ⬏ᵓ service.run(); //ᓔྟᬍবԡ㕂 public void actionPerformed( ActionEvent e ) { ActionListener task = new ActionListener(){ //ᅮН↣ 0.1 ⾦ᠻ㸠ϔ⃵ⲥ਀఼ ҷⷕ⏙ऩ˖code\ball\src\org\crazyit\ball\BallFrame.java ᠔ҹাⲥ਀䬂Ⲭⱘ᪡԰ˈ⫼ϔϾ KeyListener এⲥ਀䬂Ⲭⱘࡼ԰ˈ䇋ⳟҹϟҷⷕDŽ ӏࡵ៪㗙 Linux ㋏㒳ⱘ crobtabˈᑊ⫼ start()ᮍ⊩এਃ⫼ TimerDŽ೼䖭Ͼᔍ⧗␌៣Ёˈ៥Ӏা᳝䬂Ⲭ᪡԰ˈ javax.swing.Timer ৃҹ䆒ᅮ↣䱨ϔϾᯊ䯈਼ᳳህ䞡໡ᠻ㸠ᶤϾ taskˈ㉏ԐѢ Window ㋏㒳ⱘ䅵ߦ 4.4.4 ⲥ਀఼ϢTimer ē࡜ࢶྻߜ BallPanel ᄯԅ paint ֺ֥ަద BallService.drawd܊ֺ֥ ԅಬຣēแਬಬຣॴ BallService ԅ drawۥᅟġแਬӲ஍ωਠပճ BallService ᄯԅ draw ֺ֥ᆴఉ /Download at http://www.pin5i.com if (isWon()) { // བᵰ䌶њ ҷⷕ⏙ऩ˖code\ball\src\org\crazyit\ball\BallService.java BallPanel Ё䇗⫼ BallService ⱘ draw ᮍ⊩ˈҹϟᰃ BallService ⱘ draw ᮍ⊩ⱘ䚼ߚᅲ⦄DŽ ᇚᣵᵓĀ⬏āࠄ BallPanel Ёˈᑊ೼ܜˈ⫣ᅲ⦄њᣵᵓ㉏ৢˈ៥Ӏৃҹᅲ⦄ BallService ⱘ draw ᮍ SPEED Ͼതᷛԡ㕂ˈ䳔㽕Ў preWidth ሲᗻᦤկ setter Ϣ getter ᮍ⊩DŽ ᅮНϔϾ final int ㉏ൟⱘ SPEED ሲᗻˈҷ㸼ᣵᵓⱘ⿏ࡼ䗳ᑺˈ↣⃵⿏ࡼˈx തᷛ䛑Ӯ৥Ꮊ៪㗙৥ে⿏ࡼ ⬅Ѣᣵᵓⱘ䭓ᑺৃ㛑Ӯᬍবˈ᠔ҹ Stick ㉏᳝ⱘϾ int ㉏ൟⱘ preWidth ሲᗻˈҷ㸼ᣵᵓⱘ䭓ᑺˈᑊ } this.x = (int)( ( panelWidth - image.getWidth( null ) ) / 2 ); //䆒㕂 x തᷛ this.image = ImageIO.read( new File( path ) ); //䇏প೒⠛ super(); public BallComponent( int panelWidth , int panelHeight, String path ) throws IOException { ҷⷕ⏙ऩ˖code\ball\src\org\crazyit\ball\BallComponent.java 䖛⿟ᰃϔϾ IO ᪡԰ˈ᠔ҹӮᡯߎ IOExceptionˈ㾕ҹϟҷⷕDŽ ࠄ⬏ᵓⱘᑩ䚼ˈݡḍ᥂䇏পߎᴹⱘ೒⠛ⱘᆑᑺএ䆒㕂 Stick ᇍ䈵ⱘ߱ྟ䭓ᑺሲᗻDŽ೼Ң⺕Ⲭ䇏প೒⠛ⱘ ⱘԡ㕂ˈᑊϨՓ⫼ javax.imageio.ImageIO ⱘ read ᮍ⊩এ䇏প⺕ⲬЁⱘ೒⠛᭛ӊDŽ᥹ϟᴹᡞ y തᷛ䆒㕂 䇗⫼⠊㉏ⱘ BallComponent(int x, int y, String path)ᵘ䗴఼ˈᡞℸᇍ䈵ⱘ x തᷛ䆒㕂ࠄ⬏ᵓЁ䯈ܜ佪 } this.preWidth = super.getImage().getWidth( null ); //䆒㕂ॳᴀⱘ䭓ᑺ this.setY( panelHeight - super.getImage().getHeight( null ) ); //䆒㕂 y തᷛ super( panelWidth, panelHeight, path ); //䇗⫼⠊ᵘ䗴఼ public Stick( int panelWidth , int panelHeight, String path ) throws IOException { ҷⷕ⏙ऩ˖code\ball\src\org\crazyit\ball\Stick.java ℸ㉏ᦤկϔϾҹ⬏ᵓⱘᆑǃ催੠ᣵᵓⱘ೒⠛䏃ᕘЎখ᭄ⱘᵘ䗴఼ˈ㾕ҹϟҷⷕDŽ 4.5.1 ᣵᵓ˄Stick㉏˅ ᦤկњ޴Ͼϡৠⱘᵘ䗴఼ˈމᮍ⊩㦋প೒⠛ˈᑊϨ⠊㉏ḍ᥂ϡৠⱘᚙ ҹՓ⫼⠊㉏ⱘ setX Ϣ setY ᮍ⊩䆒㕂തᷛˈгৃҹՓ⫼ getX Ϣ getY ᮍ⊩㦋পതᷛˈ䖬ৃҹՓ⫼ getImage ೼䖭Ͼ䆒䅵Ёˈᣵᵓǃᇣ⧗ǃⷪഫϢⷪഫЁ᠔ࣙ৿ⱘ䘧݋䛑᳝ϔϾ݅ৠⱘ⠊㉏ BallComponetˈৃ 4.5 ᣵᵓǃᇣ⧗ǃⷪഫǃ䘧݋ BallService ⱘ run ໘⧚˄run ᮍ⊩ 100 ↿⾦ᠻ㸠ϔ⃵˅DŽ ⱘ⿏ࡼˈҙҙ䆒㕂ᇣ⧗ⱘ䖤ࡼ⢊ᗕˈᤶ㿔ПˈsetStickPos ᮍ⊩াᰃ໘⧚ᣵᵓⱘ⿏ࡼˈᇣ⧗ⱘ䖤ࡼ䅽 㸠䖤ࡼDŽᔧ៥Ӏ೼␌៣ЁᣝϟᎺে䬂ⱘᯊ׭ˈৠᯊ䳔㽕⿏ࡼᣵᵓˈਃࡼ␌៣ৢˈ៥Ӏᑊϡ䳔㽕݇ᖗᇣ⧗ ៥Ӏএᅲ⦄ setStickPos ᮍ⊩ⱘᯊ׭ˈህ䳔㽕䆒㕂ᇣ⧗Ў䖤ࡼ⢊ᗕˈਃࡼᔍ⧗␌៣ህᛣੇⴔᇣ⧗ᓔྟ䖯 Ͼᮍ⊩⫼ᴹⲥ਀䬂Ⲭⱘᣝ䬂ᰃ৺᳝ᣝϟˈབᵰ᳝ⱘ䆱ˈህ䳔㽕䇗⫼ BallService ⱘ setStickPos ᮍ⊩DŽᔧ ˄Ў JFrame ߯ᓎϔϾ䬂Ⲭⲥ਀఼˅ᑊ䞡ݭ KeyAdapter ㉏ⱘ void keyPressed(KeyEvent ke)ᮍ⊩ˈ䖭 བᵰ䖭Ͼ᭄㒘ⱘ䭓ᑺЎぎˈ䇈ᯢᴀにষᑊ≵᳝⏏ࡴࠄӏԩ KeyListenerˈ᠔ҹህ߯ᓎϔϾ KeyAdapter Փ⫼ JFrame ⱘ keyKeyListeners()ᮍ⊩㦋পᴀにষⱘ KeyLister ᭄㒘ˈܜˈ᥹ϟᴹݡ๲ࡴџӊⲥ਀఼ /Download at http://www.pin5i.com throws IOException { public Ball( int panelWidth , int panelHeight , int offset, String path ) ҷⷕ⏙ऩ˖code\ball\src\org\crazyit\ball\Ball.java ℸ㉏ᦤկϔϾҹ⬏ᵓⱘᆑǃ催ǃᣵᵓ催ᑺϢᇣ⧗ⱘ೒⠛䏃ᕘЎখ᭄ⱘᵘ䗴఼ˈ㾕ҹϟҷⷕDŽ 4.5.2 ᇣ⧗˄Ball㉏˅ ೒ 4.6 ߯ᓎᣵᵓ ࠄℸˈ៥Ӏৃҹ䖤㸠⿟ᑣᶹⳟ߯ᓎᣵᵓৢⱘᬜᵰˈ݋ԧⱘᬜᵰབ೒ 4.6 ᠔⼎DŽ } .getPreWidth(), stick.getImage().getHeight(null), null); g.drawImage(stick.getImage(), stick.getX(), stick.getY(), stick ڣ // 㒬ࠊᣵᵓ೒ g.clearRect(0, 0, width, height); ڣ // ⏙䰸ॳᴹⱘ೒ } else { width, height - 10, null); g.drawImage(gameOver.getImage(), gameOver.getX(), gameOver.getY(), ڣ // 㒬ࠊ␌៣㒧ᴳ೒ } else if (ball.isStop()) { height - 10, null); g.drawImage(won.getImage(), won.getX(), won.getY(), width, 㒬ࠊ䌶ⱘ೒⠛ // /Download at http://www.pin5i.com ㉏ˈᅗᰃ BallComponet ⱘᄤ㉏ˈজᰃ LongMagic Ϣ ShortMagic ⱘ⠊㉏ˈℸڣMagic ㉏ᰃϔϾᢑ 4.5.3 䘧݋˄Magicঞ݊ᄤ㉏˅ ೒ 4.7 ⬏ᇣ⧗೒⠛ g.drawImage(ball.getImage(), ball.getX(), ball.getY(), null); ᠔⼎DŽ ৢˈ៥Ӏৃҹ೼ BallService ⱘ draw ᮍ⊩ЁˈᇚϔϾᇣ⧗Ā⬏āࠄ BallPanel ЁDŽ݋ԧⱘᬜᵰབ೒ 4.7 এҷ㸼ᇣ⧗ⱘ῾৥ᮍ৥Ϣオ৥ᮍ৥ˈᑊ๲ࡴⳌᑨⱘ setter Ϣ getter ᮍ⊩DŽЎ Ball ᇍ䈵⏏ࡴњⳌ݇ⱘሲᗻ ៥Ӏᡞᇣ⧗ⱘ䗳ᑺᮍ৥ߚЎ῾ϢオϸϾᮍ৥ˈ᠔ҹ䖭䞠⫼ int ㉏ൟⱘ speedX Ϣ speedY ϸϾሲᗻ Ⳉ᥹䆒㕂 Ball ⱘ started ሲᗻЎ trueDŽ 㸼ℷ೼␌៣ЁDŽ៥Ӏ೼ Ball ЁᦤկϔϾ started ⱘሲᗻᴹᷛ䆚䖭ϸ⾡⢊ᗕDŽ䙷Мᔧ␌៣ᓔྟᯊˈህৃҹ ᴳ䖤ࡼˈབᵰᇣ⧗㒧ᴳ䖤ࡼˈҷ㸼␌៣Ꮖ㒣㒧ᴳˈᇣ⧗ϡ㛑ݡ䖤ࡼˈᣵᵓгϡݡফ⥽ᆊⱘ᥻ࠊˈড߭ҷ ໛ᓔྟ␌៣ˈড߭ҷ㸼␌៣Ꮖ㒣ᓔྟˈ≵␌៣≵㒧ᴳПࠡˈᇣ⧗ህϔⳈ䖤ࡼ˗ϔϾᰃᇣ⧗ᰃ৺㒧ޚҷ㸼 ೼䖭䞠ˈᇣ⧗ᇍ䈵᳝ϸ⾡⢊ᗕˈϔ⾡ᰃᇣ⧗ᰃ৺ᓔྟ䖤ࡼˈ䖭⾡⢊ᗕϟˈབᵰᇣ⧗≵᳝ᓔྟ䖤ࡼˈ ࠄᵓԡϞ䴶ⱘԡ㕂DŽ ⱘԡ㕂ˈᑊϨՓ⫼ javax.imageio.ImageIO ⱘ read ᮍ⊩এ䇏প⺕ⲬЁⱘ೒⠛᭛ӊDŽ᥹ϟᴹᡞ y തᷛ䆒㕂 䇗⫼⠊㉏ⱘ BallComponent(int x, int y, String path)ᵘ䗴఼ˈᡞℸᇍ䈵ⱘ x തᷛ䆒㕂ࠄ⬏ᵓЁ䯈ܜ佪 } this.setY( panelHeight - super.getImage().getHeight( null ) - offset ); //䆒㕂 y തᷛ super( panelWidth, panelHeight, path ); 䇗⫼⠊ᵘ䗴఼// /Download at http://www.pin5i.com if( this.magic != null ) { } this.magic = new ShortMagic( "img/short.gif", x, y ); } else if( type == Brick.MAGIC_SHORT_TYPE ) { this.magic = new LongMagic( "img/long.gif", x, y ); if( type == Brick.MAGIC_LONG_TYPE ) { super(path); public Brick(String path, int type, int x, int y ) throws IOException { ҷⷕ⏙ऩ˖code\ball\src\org\crazyit\ball\Brick.java 䖬ࣙ৿ϔϾ Magic ㉏ൟⱘሲᗻ magicˈ೼ᵘ䗴఼Ё߱ྟ࣪䖭Ͼሲᗻˈ㾕ҹϟҷⷕЁDŽ Brick ㉏ᰃ BallComponet ⱘϔϾᄤ㉏ˈ⫼ϔϾ boolean ㉏ൟⱘሲᗻ disalbe এᷛᖫᇍ䈵ᰃ৺᳝ᬜᵰˈ 4.5.4 ⷪഫ˄Brick㉏˅ ЎϔञDŽ ᇥ䖛ˈህ䇗⫼ Stick ⱘ setPreWidth ᮍ⊩ᡞᣵᵓⱘ䭓ᑺ䆒㕂ޣ㗙ㄝѢ೒⠛ⱘ䭓ᑺˈ䇈ᯢᣵᵓⱘ䭓ᑺ≵᳝ 㦋পᣵᵓ೒⠛ⱘ䭓ᑺˈབᵰ⦄೼ⱘ䭓ᑺ໻Ѣ៪ܜ䖭䞠ⱘ⌕⿟੠ LongMagic Ёᅲ⦄ⱘᮍ⊩ⳌԐˈ佪 } } stick.setPreWidth( (int)(stick.getPreWidth() * 0.5 ) ); //ᇚᣵᵓⱘᆑᑺᬍЎϔञ if( stick.getPreWidth() >= imageWidth ) { //བᵰᣵᵓ≵᳝বⷁ䖛 double imageWidth = stick.getImage().getWidth(null); public void magicDo( Stick stick ) { ҷⷕ⏙ऩ˖code\ball\src\org\crazyit\ball\ShortMagic.java ϟ䴶ݡⳟ ShortMagic ᅲ⦄ⱘ magicDo ᮍ⊩DŽˈס ⠛ⱘ䭓ᑺˈ䇈ᯢᣵᵓⱘ䭓ᑺ≵᳝๲ࡴ䖛ˈ᠔ҹህ䇗⫼ Stick ⱘ setPreWidth ᮍ⊩ᡞᣵᵓⱘ䭓ᑺ䆒㕂Ўজ 㦋পᣵᵓ೒⠛ⱘ䭓ᑺˈݡᣓ䖭Ͼ䭓ᑺ੠ᣵᵓ⦄೼ⱘ䭓ᑺ↨䕗ˈབᵰᣵᵓⱘ䭓ᑺᇣѢ៪㗙ㄝѢ೒ܜ佪 } } stick.setPreWidth( (int)(stick.getPreWidth() * 2 ) ); ס //ᇚᣵᵓⱘ䭓ᑺᬍЎঠ if( stick.getPreWidth() <= imageWidth ) { //བᵰᣵᵓ≵᳝ব䭓䖛 double imageWidth = stick.getImage().getWidth(null); public void magicDo( Stick stick ) { ҷⷕ⏙ऩ˖code\ball\src\org\crazyit\ball\LongMagic.java ⳟ LongMagic ㉏ᅲ⦄ⱘ magicDo ᮍ⊩DŽܜⱘᅲ⦄ᮍ⊩Ёᅲ⦄ˈ佪 ᄤ㉏˖LongMagic Ϣ ShortMagicˈ䖭ϸϾ䘧݋ⱘࡳ㛑ᰃՓ␌៣Ёⱘᣵᵓব䭓੠বⷁˈࡳ㛑䛑೼ magicDo ᮍ⊩ˈ䖭䞠া᳝ᅮНˈ≵᳝ᅲ⦄ˈ䅽݊ᄤ㉏এᅲ⦄DŽMagic ㉏᳝ϸϾڣ㸠ҷⷕᰃ⫼ᴹᅠ៤䘧݋ࡳ㛑ⱘᢑ 䖭Ͼᵘ䗴఼া䇗⫼⠊㉏ BallComponent ⱘᵘ䗴఼ˈএ䆒㕂䘧݋ⱘ㸼⦄೒⠛Ϣ߱ྟതᷛDŽࡴ㉫ⱘϔ public abstract void magicDo( Stick stick ); } super( path, x, y ); public Magic( String path, int x , int y ) throws IOException { ҷⷕ⏙ऩ˖code\ball\src\org\crazyit\ball\Magic.java 䗴఼կ݊ᄤ㉏㒻ᡓˈ㾕ҹϟҷⷕDŽ ㉏া᳝ϔϾᢑ䈵ᮍ⊩ magicDoˈ⫼ᴹᅠ៤䘧݋ⱘࡳ㛑ˈᦤկϔϾՓ⫼೒⠛䏃ᕘϢ xǃy തᷛЎখ᭄ⱘᵘ /Download at http://www.pin5i.com //䆒㕂 x തᷛ brick.setDisable(isDisable); Brick brick = new Brick( path, random, x, y ); } random = 0; if( isDisable ){ isDisable = Math.random() > 0.8 ? true : false; //ϔᅮᴎ⥛≵᳝ⷪഫ y = j * imageSize; x = i * imageSize; random = (int)( Math.random() * 3 ); //߯ᓎϔϾᮄⱘⷪഫ for ( int j = 0 ; j < ySize ; j++) { for ( int i = 0 ; i 0 ) { if( ke.getKeyCode() == KeyEvent.VK_LEFT ) { //བᵰᰃᎺᮍ৥䬂 ball.setStarted( true ); //ᡞᔍ⧗ⱘ䖤ࡼ⢊ᗕ䆒Ў true public void setStickPos( KeyEvent ke ) { ҷⷕ⏙ऩ˖code\ball\src\org\crazyit\ball\BallService.java ⳟҹϟҷⷕDŽܜˈপ⥽ᆊ᠔ᣝϟⱘ䬂Ⲭᣝ䬂 Ў䖭Ͼᮍ⊩ⱘখ᭄ˈ೼ᮍ⊩ݙৃҹ䗮䖛䖭Ͼᇍ䈵ⱘ getKeyCode()ᮍ⊩এ㦋خ䳔㽕ҹϔϾ KeyEvent ᇍ䈵 ߎⳌᑨⱘডᑨˈএᬍবᣵᵓⱘതᷛԡ㕂ˈ᠔ҹخᣵᵓⱘ⿏ࡼЏ㽕ᰃձ䴴ⲥ਀⥽ᆊⱘ䬂Ⲭ᪡԰ˈ✊ৢ 4.6.2 䆒㕂ᣵᵓⱘԡ㕂˄⿏ࡼᣵᵓ˅ ೒ 4.8 ߯ᓎⷪഫ /Download at http://www.pin5i.com double brickX = brick.getX() //brick ⱘЁᖗ x തᷛ + ball.getImage().getHeight(null) / 2; double ballY = ball.getY() //ball ⱘ೚ᖗ y തᷛ + ball.getImage().getWidth(null) / 2; double ballX = ball.getX() //ball ⱘ೚ᖗ x തᷛ } return false; if ( brick.isDisable() ) { public boolean isHitBrick( Brick brick ) { ҷⷕ⏙ऩ˖code\ball\src\org\crazyit\ball\BallService.java ϟҷⷕDŽ rˈ䙷Мˈབᵰ˄x1ˈy1˅Ϣ˄x2ˈy2˅ⱘ䎱⾏ᇣѢ n+rˈⷪഫϢᇣ⧗ህ໘Ѣ⺄ᩲⱘ⢊ᗕˈ㾕೒ 4.9 Ϣҹ 䆒ᇣ⧗೚ᖗⱘതᷛᰃ˄x1ˈy1˅ˈⷪഫЁ䯈ⱘതᷛᰃ˄x2ˈy2˅ˈⷪഫⱘϔञ䖍䭓ᰃ nˈᇣ⧗ⱘञᕘᰃ؛ ˈ೼␌៣Ёˈབᵰ䖤㸠ⱘᇣ⧗⺄ࠄⷪഫˈህ㽕ᡞⷪഫ⍜ᥝˈ᠔ҹ៥Ӏ䳔㽕߸ᮁᇣ⧗Ϣⷪഫᰃ৺᳝⺄ᩲ ೒ 4.9 ᇣ⧗Ϣⷪഫ⺄ᩲ 4.6.3 ᇣ⧗Ϣⷪഫ⺄ᩲ ⊩ᰃ䞡ᮄ߱ྟ࣪⬠䴶DŽᅲ⦄ᣵᵓⱘ⿏ࡼ䕗Ўㅔऩˈা䳔㽕䆒㕂ᣵᵓᇍ䈵ⱘതᷛᑊ߸ᮁᰃ৺䍞⬠䴶ेৃDŽ ডDŽབᵰ⥽ᆊᣝϟⱘᰃ F2 䬂˄䖭䞠ᅮН F2 䬂ᰃ䞡ᮄᓔྟ␌៣˅ˈህ䇗⫼ BallFrame ᇍ䈵ⱘ initialize ᮍ ӏԩ᪡԰DŽབᵰ⥽ᆊᣝϟⱘᰃে䬂ˈ໘⧚ᮍᓣϢᎺ䬂㉏Ԑˈাϡ䖛ᰃᮍ৥Ⳍخᵓⱘ⿏ࡼ䗳ᑺ˅ˈ৺߭ϡ Ꮖ㒣೼␌៣䴶ᵓⱘ᳔Ꮊ䖍ˈབᵰϡᰃˈህᡞᣵᵓⱘԡ㕂৥Ꮊ⿏ࡼ Stick.SPEED Ͼԡ㕂˄SPEED ҷ㸼ᣵ Ẕᶹᣵᵓᰃ৺ܜབᵰ⥽ᆊᣝϟⱘᰃᎺ䬂ˈгህᰃ ke.getKeyCode() ㄝѢ KeyEvent.VK_LEFTˈህ } } } System.out.println( e.getMessage() ); } catch ( IOException e ) { ballFrame.initialize(); try { //߱ྟ࣪ ballFrame if( ke.getKeyCode() == KeyEvent.VK_F2 ) { //བᵰᰃ F2 䬂 } } ballFrame.getBallGame().reStart( ballFrame );// /Download at http://www.pin5i.com ೒ 4.10 ᇣ⧗Ϣⷪഫ⺄ᩲ ᇣ⧗⺄ᩲⷪഫⱘᬜᵰབ೒ 4.10 ᠔⼎DŽ ⧗⺄ᩲˈᑊߎĀ䎠㨑āњⳌᑨⱘ䘧݋ˈ೼ draw ⱘᯊ׭ˈህ䳔㽕ᇚ䘧݋ⱘ೒⠛⬏ࠄ⬠䴶Ё˄BallPanel˅ˈ 䆹ሲᗻЎ trueˈ߭㸼⼎䖭ഫⷪഫҡ✊໘೼ॳᴹⱘԡ㕂ˈབᵰ䆹ሲᗻЎ falseˈ߭㸼⼎䖭ഫⷪഫᏆ㒣㹿ᇣ ࠄ BallPanel Ёⱘᯊ׭˄䘡ग़Ѡ㓈᭄㒘˅ˈᕫࠄ↣ϔϾⷪഫᇍ䈵ˈ䛑䳔߸ᮁ䆹ᇍ䈵ⱘ disable ሲᗻˈབᵰ ህ䇗⫼ Brick ᇍ䈵ⱘ setDisable ᮍ⊩ᡞ Brick ᇍ䈵䆒㕂Ў᮴ᬜˈᑊ䖨ಲ trueDŽ៥ӀᇚⷪഫⱘѠ㓈᭄㒘Ā⬏ā ㉫ԧҷⷕ䚼ߚህᰃҹ˄x1ˈy1˅ˈ˄x2ˈy2˅ϸϾ⚍ⱘ䎱⾏Ϣ nǃr ⱘ੠↨䕗ˈབᵰ䖭Ͼ䎱⾏ᇣѢ੠ˈ } return false; } return true; brick.setDisable( true ); //Փ brick ᮴ᬜ + brick.getImage().getWidth(null) )/ 2) { if( distance < ( ball.getImage().getWidth(null) //བᵰϸϾ೒ᔶ䞡঴ˈ䖨ಲ true; + Math.pow( ballY -brickY, 2 )); Math.pow( ballX - brickX, 2 ) double distance = Math.sqrt( //ϸϾതᷛ⚍ⱘ䎱⾏ + brick.getImage().getHeight(null)/2; double brickY = brick.getY() //brick ⱘЁᖗ y തᷛ brick.getImage().getWidth(null)/2; + /Download at http://www.pin5i.com //䆒㕂ᣵᵓⱘᆑᑺ magic.setY( magic.getY() + magic.getSpeed() ); //䆒㕂 magic ⱘ y തᷛ৥ϟ๲ࡴ if( bricks[i][j].isDisable() && magic.getY() < height ) { //བᵰ䖭Ͼ brick ⱘ⢊ᗕᰃ᮴ᬜⱘ if( magic != null ) { Magic magic = bricks[i][j].getMagic(); //㦋প magic for ( int j = 0 ; j < bricks[i].length ; j++ ) { for ( int i = 0 ; i < bricks.length ; i++ ) { public void setMagicPos() { ҷⷕ⏙ऩ˖code\ball\src\org\crazyit\ball\BallService.java ๲ࡴ magic.getSpeed()Ͼ y തᷛؐˈ䖒ࠄ䘧݋৥ϟ⿏ࡼⱘᬜᵰˈ㾕བϟҷⷕDŽ ⬏ᵓⱘ催ᑺ height˄䖭䞠ᛣᗱᰃ䇈䖭Ͼ䘧݋䖬೼⬏ᵓⱘ㣗ೈПݙ˅ˈ䙷Мˈ֓ҹ Magic ᇍ䈵ⱘ䗳ᑺ↣⃵ ⷪഫ㹿ᇣ⧗⍜ᥝ˅ˈ㗠Ϩ䖭Ͼ Brick ᇍ䈵Ё᳝ Magic ᇍ䈵ϡЎ nullˈᑊϨ Magic ᇍ䈵ⱘ y തᷛᇣѢᇣѢ ᠔ҹ៥Ӏ䳔㽕䘡ग़ bricks ᭄㒘Ёⱘ᠔᳝ Brick ᇍ䈵ˈབᵰ Brick ᇍ䈵ⱘ⢊ᗕᰃ disable Ў true˄гህᰃ䇈 Ͼ䘧݋ˈLongMagic Ϣ ShortMagicˈ԰⫼ߚ߿ᰃՓᣵᵓህ䭓៪㗙বⷁˈ㗠䘧݋ᰃֱᄬ೼ Brick ᇍ䈵Ёˈ ᔧᇣ⧗Ϣⷪഫথ⫳⺄ᩲৢˈⷪഫᇚӮব៤䘧݋˄བ೒ 4.10 ᠔⼎˅DŽࠡ䴶ⱘゴ㡖Ё䆆ࠄ䖭Ͼ␌៣᳝ϸ 4.6.5 䘧݋ⱘ⿏ࡼ bc ⱘ y തᷛᰃ৺໻Ѣᣵᵓⱘ y തᷛDŽ bc ⱘ x തᷛᰃϡᰃ໘Ѣᣵᵓⱘ㣗ೈݙˈbc.getY() + tempImage.getHeight(null) > stick.getY()ᰃ⹂䅸 tempImage.getWidth(null) > stick.getX()&& bc.getX() < stick.getX() + stick.getPreWidth()ᰃ⹂䅸 䖭Ͼᮍ⊩Ёⱘখ᭄ bc ҷ㸼ⱘᰃᇣ⧗៪㗙䘧݋ˈࡴ㉫ҷⷕ䚼ߚᰃ߸ᮁᅗӀᰃ৺᳝⺄ᩲˈbc.getX() + } return false; } return true; && bc.getY() + tempImage.getHeight(null) > stick.getY() ) { && bc.getX() < stick.getX() + stick.getPreWidth() if( bc.getX() + tempImage.getWidth(null) > stick.getX() //བᵰϢᣵᵓ᳝⺄ᩲ Image tempImage = bc.getImage(); //㦋প೒⠛ᇍ䈵 public boolean isHitStick( BallComponent bc ) { ҷⷕ⏙ऩ˖code\ball\src\org\crazyit\ball\BallService.java ⱘ㣗ೈݙ˅ˈy തᷛ໻Ѣ y1ˈ䙷Мህৃҹ߸ᮁᅗӀ೼⺄ᩲˈ㾕ҹϟҷⷕDŽ y1˅㸼⼎ˈᣵᵓⱘ䭓ᑺЎ nˈ䙷Мˈা㽕ᇣ⧗៪㗙䘧݋ⱘ x തᷛ໘Ѣ x1 Ϣ x1+n П䯈˄гህᰃ໘Ѣᣵᵓ 䆒ᣵᵓⱘതᷛᰃᣛ䖭Ͼ䭓ᮍᔶⱘᎺϞ㾦ˈ⫼˄x1ˈ؛᠔ҹৃҹϡՓ⫼Ϟ㡖߸ᮁᇣ⧗Ϣⷪഫ⺄ᩲⱘᮍ⊩DŽ 䳔㽕໘⧚ⱘџᚙˈᑊϡ⬅䆹ᮍ⊩䖯㸠໘⧚DŽ೼䖭䞠ˈ⬅Ѣᣵᵓᰃ䭓ᮍᔶⱘˈ㗠Ϩᣵᵓⱘ y തᷛᰃϡবⱘˈ ᅗӀথ⫳њ⺄ᩲˈ䆹ᮍ⊩ህ䳔㽕䖨ಲ trueDŽisHitStick ᮍ⊩া㽕߸ᮁᰃ৺থ⫳њ⺄ᩲˈ㟇Ѣথ⫳⺄ᩲৢ᠔ ៥Ӏ䳔㽕ᅲ⦄ BallService ⱘ isHitStick ᮍ⊩ˈ䆹ᮍ⊩߸ᮁᇣ⧗ǃ䘧݋Ϣᣵᵓᰃ৺থ⫳њ⺄ᩲˈা㽕 4.6.4 ᇣ⧗ǃ䘧݋Ϣᣵᵓ⺄ᩲ ݋ⱘ⿏ࡼǃ䘧݋Ϣᣵᵓⱘ⺄ᩲ៥Ӏᇚ೼ϟ䴶ⱘゴ㡖Ёᦣ䗄DŽ བ೒ 4.10 ᠔⼎ˈᔧᇣ⧗Ϣⷪഫথ⫳⺄ᩲⱘᯊ׭ˈⷪഫህӮব៤䘧݋ˈᑊϨ䆹䘧݋Ӯ䖯㸠ϟ㨑DŽ䘧 /Download at http://www.pin5i.com } } magic.magicDo( stick ); //䘧݋ⱘ԰⫼ if( isHitStick( magic ) ) { public void setStickWidth( Magic magic ) { ҷⷕ⏙ऩ˖code\ball\src\org\crazyit\ball\BallService.java ݋থ⫳⺄ᩲˈ֓䇗⫼ Magic ᇍ䈵ⱘ magicDo ᮍ⊩ˈmagicDo ᮍ⊩Ӯᇚᣵᵓⱘ䭓ᑺˈ㾕ҹϟҷⷕDŽ ᴹᇚӮकߚㅔऩˈা㽕߸ᮁ䘧݋Ϣᣵᵓᰃ৺᳝⺄ᩲ˄䇗⫼ isHitStick ᮍ⊩˅ˈབᵰᣵᵓϢĀᥝϟᴹāⱘ䘧 ೼ 4.6.4 Ёᅲ⦄њ߸ᮁᇣ⧗Ϣᣵᵓǃⷪഫᰃ৺থ⫳⺄ᩲⱘᮍ⊩ˈ಴ℸ೼䖭䞠ᬍবᣵᵓ䭓ᑺˈᅲ⦄䍋 4.6.6 ᬍবᣵᵓⱘ䭓ᑺ˄䘧݋ⱘ԰⫼˅ ೒ 4.11 䘧݋ⱘϟ㨑 䆱ˈ⬠䴶ЁህӮߎ⦄ϟ㨑ⱘ䘧݋ˈ݋ԧⱘᬜᵰབ೒ 4.11 ᠔⼎DŽ ೼ BallService ⱘ run ᮍ⊩䇗⫼ setMagicPos ᮍ⊩˄run ᮍ⊩↣ 100 ↿⾦ᠻ㸠ϔ⃵˅ˈབᵰⷪഫ㹿⍜䰸ⱘ ҹϞⱘҷⷕᅲ⦄њ setMagicPos ᮍ⊩ˈ䆹ᮍ⊩↣ᠻ㸠ϔ⃵ˈ䛑Ӯᬍব䘧݋ⱘԡ㕂ˈ಴ℸˈ៥Ӏৃҹ } } } } } setStickWidth( magic ); /Download at http://www.pin5i.com } } return false; if(!bricks[i][j].isDisable() ) { for ( int j = 0 ; j < bricks[i].length ; j++ ) { for ( int i = 0 ; i < bricks.length ; i++ ) { //བᵰ⍜њܼ䚼ⷪഫˈ߭Ў䌶 public boolean isWon() { ҷⷕ⏙ऩ˖code\ball\src\org\crazyit\ball\BallService.java ሲᗻЎ false ⱘ䆱ˈ␌៣䳔㽕㒻㓁䖯㸠DŽ true ⱘ䆱ˈህᛣੇⴔ᠔᳝ⱘⷪഫ䛑Ꮖ㒣㹿Āߏ㨑āˈ䖭ϔ݇␌៣䗮䖛˗བᵰ᭄㒘ЁⱘᶤϔϾⷪഫⱘ disable ህ䳔㽕೼ BallService ЁᇍⷪഫⱘѠ㓈᭄䖯㸠䘡ग़ˈབᵰ䆹᭄㒘Ё᠔᳝ⱘⷪഫᇍ䈵ⱘ disable ሲᗻ䛑Ўᕵ ᰃˈᇣ⧗ᰃ৺Ꮖ㒣ᇚ᠔᳝ⱘⷪഫ⏙䰸DŽ಴ℸ៥Ӏᅲ⦄䆹ࡳ㛑ⱘᯊ׭ˈޚ೼ᴀ␌៣Ёˈᰃ৺䗮݇ⱘᷛ 4.6.7 ߸ᮁᰃ৺Ꮖ㒣䗮݇ ᰃ⬅Ѣ៥Ӏ೼ Magic ⱘᄤ㉏˄LongMagic˅Ё䆒㕂њᣵᵓⱘᆑᑺDŽ བ೒ 4.12 ᠔⼎ˈᔧ␌៣Ёⱘᣵᵓ᥹ᬊࠄ㓓㡆ⱘ䘧݋ᯊ˄ࡴ䭓ᣵᵓ˅ˈᣵᵓⱘᆑᑺህথ⫳њᬍবˈ䖭 ೒ 4.12 䘧݋ⱘᬜᵰ ݋ԧᬜᵰབ೒ 4.12 ᠔⼎DŽ ᵓࡴ䭓៪㗙㓽ⷁDŽMagic ⱘϸϾᄤ㉏Ꮖ㒣೼ 4.5.3 Ёᅲ⦄ˈ᥹ϟᴹ៥Ӏৃҹ䖤㸠␌៣ᶹⳟᬜᵰˈ␌៣ⱘ Ⳍᑨⱘᅲ⦄ˈ಴ℸ៥Ӏ೼ҹϞⱘҷⷕЁৃҹⳈ᥹䇗⫼ Magic ⱘ magicDo ᮍ⊩ˈ䖭ḋህৃҹᇚᔧࠡⱘᣵ ೼ᴀゴЁা⍝ঞϸϾ䘧݋˖㓽ⷁᣵᵓ੠ࡴ䭓ᣵᵓˈᑊϨ៥Ӏ೼ Magic ⱘϸϾᄤ㉏ЁᏆ㒣ᇍ݊ᦤկњ /Download at http://www.pin5i.com ៥Ӏৃҹ䆒䅵↣ϔ݇ⷪഫⱘϡৠᥦ߫ᮍᓣˈϢঞ䞠䴶᠔ࣙᣀⱘ䘧݋ˈৃҹᗢМএ䆒㕂䖭Ͼϰ㽓˛དˈ 4.7.1 ݇व ᑣࡼᗕ߯ᓎⷪഫⱘѠ㓈᭄㒘DŽ ᳈໮ⱘ␌៣䘧݋ˈৃҹ㒻ᡓ Magic ᇍ䈵ᑊᅲ⦄ magicDo ᮍ⊩DŽབᵰ䳔㽕᳈ᬍⷪഫⱘᥦ߫ˈৃҹ㓪ݭ⿟ ໾໮ⱘᎹ԰ˈህ㛑ᡞ䖭ѯὖᗉࡴࠄ␌៣ЁএDŽབᵰ៥Ӏ䳔㽕ࡴܹخ៣⥽䍋ᴹӮ↨䕗ᶃ➹ˈ݊ᅲ៥Ӏϡᖙ Ңࠡ䴶ⱘ޴ᇣ㡖ৃҹⳟࠄˈᴀ␌៣ᑊ≵᳝ࡴܹ݇वǃ䅵ߚㄝὖᗉˈ㗠Ϩ䘧݋ⱘ⾡㉏↨䕗ᇥˈᭈϾ␌ 4.7 ࡳ㛑ᬍ䖯䆒䅵 ߽ˈ៥Ӏৃҹ೼ℸ෎⸔Ϟᓔথߎ᳈໮᳝䍷ⱘࡳ㛑ˈ՟བࡴܹߚ᭄ⱘ䅵ㅫǃࡴܹ໮⾡䘧݋ㄝDŽ ӏԩ໡ᴖⱘࡳ㛑ˈাᰃ೼ᇣ⧗䖤ࡼⱘ䖛⿟Ёᇚⷪഫ⍜䰸ᑊ䕀࣪៤Ⳍᑨⱘ䘧݋ˈ᳔ৢ߸ᮁ␌៣ᰃ৺Ꮖ㒣㚰 ࠄℸˈ៥ӀⱘḠ䴶ᔍ⧗␌៣ህᏆ㒣ܼ䚼ᅲ⦄њˈৃҹ䖤㸠␌៣䖯㸠⌟䆩DŽ៥Ӏ䖭ϾḠ䴶ᔍ⧗ᑊ≵᳝ ೒ 4.13 ␌៣㚰߽ Ёˈᔧ␌៣㚰߽ᯊˈ݋ԧⱘᬜᵰབ೒ 4.13 ᠔⼎DŽ ህ䳔㽕䇗⫼ isWon ᮍ⊩ᴹ߸ᮁ␌៣ᰃ৺㚰߽ˈབᵰ㚰߽ⱘ䆱ˈህ䳔㽕ᇚ␌៣㚰߽ⱘ೒⠛Ā⬏Āࠄ BallPanel ᅲ⦄њ isWon ᮍ⊩ৢˈ៥Ӏৃҹਃࡼ␌៣䖯㸠⌟䆩ˈ⬅Ѣ៥Ӏ೼ࠡ䴶ゴ㡖ᅲ⦄ draw ᮍ⊩ⱘᯊ׭ˈ } return true; { /Download at http://www.pin5i.com 㽕䆺㒚ᦣ䗄њ䬂Ⲭџӊⲥ਀఼ǃ೼ Swing 㒘ӊЁ⬏೒ㄝⳌ݇ⶹ䆚⚍DŽ ᥹ᬊࠄᣝ䬂ֵᙃৢˈህӮ䇗⫼Ⳍ݇ⱘᮍ⊩এ᪡԰␌៣Ёⱘ৘Ͼᇍ䈵ˈᑊᇚ䖭ѯᇍ䈵⬏ࠄ⬠䴶ЁDŽᴀゴЏ ᇚ␌៣ЁⱘⳌ݇㒘ӊ˄ᇣ⧗ǃᣵᵓ˅䛑ᢑ䈵៤ЎϔϾᇍ䈵ˈᑊЎ JFrame ᦤկњ䬂Ⲭⲥ਀఼ˈᔧⲥ਀఼ ⱘ drawImage ᮍ⊩এ⬏೒DŽ೼ᓔথḠ䴶ᔍ⧗ⱘ䖛⿟Ёˈ៥Ӏᇚ⬠䴶Ёⱘⷪഫᢑ䈵៤ϔϾѠ㓈᭄㒘ˈڣ ᴀゴЏ㽕ᰃ䗮䖛ϔϾᔍ⧗␌៣ⱘ෎ᴀᅲ⦄ˈ৥䇏㗙䆆㾷 Java ⱘ⬏೒ᮍ⊩ˈЏ㽕ᰃՓ⫼ Graphics ᇍ 4.8 ᴀゴᇣ㒧 ᡞ᠔᳝ⱘⷪഫ䛑䆒㕂Ў᮴ᬜᵰˈህ⏙䰸ᥝ᠔᳝ⱘⷪഫњDŽ } } } bricks.[i][i].setDisable(true); for ( int j = 0 ; j < bricks[i].length ; j++ ) { for ( int i = 0 ; i < bricks.length ; i++ ) { public void magicDo( Stick stick ) { ⦄䞠䴶ⱘ doMagic ᮍ⊩ˈ㾕ҹϟҷⷕ˖ ϔϾϔ⃵㛑ᡞ᠔᳝ⷪഫ⏙ᥝⱘ䘧݋ˈ䙷Мˈ៥ৃҹᮄᓎϔϾি KillAllMagic ⱘ㉏ˈ㒻ᡓ Magic ㉏ˈᑊᅲ ㉏԰Ў᥹ষˈ๲ࡴᮄⱘ䘧݋г↨䕗ㅔऩˈ՟བ៥⦄೼㽕๲ࡴڣ⬅Ѣ៥Ӏⱘ䘧݋Փ⫼њϔϾ Magic ᢑ 4.7.3 䘧݋ BallGame ⱘ䅵ߚ఼ЁDŽ এֱᄬߚ᭄ˈ✊ৢݡⷪഫϢ৘Ͼ䘧݋㉏Ё๲ࡴϔϾߚ᭄ⱘሲᗻˈབᵰ᳝⺄ᩲⱘᯊ׭ˈ֓ᡞ䖭ѯߚ᭄ࡴࠄ ৃҹㅔऩᅲ⦄ϔϾेᯊ䅵ߚⱘࡳ㛑г↨䕗ㅔऩˈৃҹ㒭೼ BallGame ㉏Ё๲ࡴϔϾ䴭ᗕⱘ int ሲᗻ 4.7.2 䅵ߚ ✊ৢ៥Ӏህৃҹ䗮䖛 IO ᪡԰এ䇏পϢߚᵤ䖭Ͼ᭛ӊˈߚᵤৢ֓䆒㕂ࠄ Brick ㉏ൟⱘ᭄㒘䞠䴶DŽ *(0) *(0) *(0) *(0) *(0) *(0) *(0) *(0) *(0) *(0) *(0) *(0) *(3) *(0) *(0) *(0) *(0) *(0) *(0) *(0) *(0) *(2) *(0) *(0) *(0) *(0) *(0) *(0) *(0) *(0) *(0) *(0) *(0) *(0) *(0) *(0) *(0) *(0) *(1) *(0) *(0) *(4) *(0) *(0) *(0) *(0) *(0) *(0) ೼Ā*āোᮕ䖍ࡴϞϔϾᇣᣀোএࡴܹ䘧݋ⱘ㉏ൟˈ㾕ҹϟ᭛ḷ˖ ៥ӀৃҹՓ⫼᳔ㅔऩⱘϔϾ txt ᭛ᴀএ䆒㕂↣݇ⱘⷪഫϢ䘧݋ˈ՟བՓ⫼ϔϾĀ*āোএҷ㸼ⷪഫˈ✊ৢ ㄀ 4 ゴ Ḡ䴶ᔍ⧗ g23g Download at http://www.pin5i.com/ Download at http://www.pin5i.com/ Download at http://www.pin5i.com/ Download at http://www.pin5i.com/ Download at http://www.pin5i.com/ Download at http://www.pin5i.com/ Download at http://www.pin5i.com/ Download at http://www.pin5i.com/ Download at http://www.pin5i.com/ Download at http://www.pin5i.com/ Download at http://www.pin5i.com/ Download at http://www.pin5i.com/ Download at http://www.pin5i.com/ Download at http://www.pin5i.com/ Download at http://www.pin5i.com/ Download at http://www.pin5i.com/ Download at http://www.pin5i.com/ Download at http://www.pin5i.com/ Download at http://www.pin5i.com/ Download at http://www.pin5i.com/ Download at http://www.pin5i.com/ Download at http://www.pin5i.com/ Download at http://www.pin5i.com/ Download at http://www.pin5i.com/ Download at http://www.pin5i.com/ /Download at http://www.pin5i.com ೒ 6.1 ⬏೒ ᳔㒜ᬜᵰབ೒ 6.1 ᠔⼎DŽ ᵾǃ买㡆㓪䕥ㄝࡳ㛑ˈ䰸њ䖭ѯ㒬೒ࡳ㛑ˈ䖬Ӯᅲ⦄ᠧᓔ೒⠛ǃֱᄬ೒⠛ㄝ᭛ӊ᪡԰ࡳ㛑DŽ⬏೒Ꮉ݋ⱘ ᴀ᭛ᇚՓ⫼ Java 䇁㿔এᅲ⦄ Windows ⱘ໻䚼ߚࡳ㛑ˈࣙᣀপ㡆ǃ৘⾡᭄ᄺߑ᭄೒ᔶǃ‵Ⲃ᪺ǃ஋ ࡳ㛑DŽ Ԛ݋᳝໻໮೒⠛໘⧚⿟ᑣ᠔ᖙ䳔ⱘ෎ᴀࡳ㛑˖䪙ヨ⬏೒ǃ৘⾡᭄ᄺߑ᭄೒ᔶǃ฿㡆ǃপ㡆ǃ‵Ⲃ᪺ㄝㄝ ⫼ Windows ㋏㒳ⱘ䇏㗙䛑↨䕗❳ᙝˈᰃϔ⾡↨䕗ㅔऩϢ݋᳝ҷ㸼ᗻⱘ⬏೒Ꮉ݋ˈ㱑✊ࡳ㛑ϡ໳ᔎ໻ˈ ᐌᔎ໻ⱘ೒ᔶ໘⧚Ꮉ݋ˈWindows ⬏೒Ꮉ݋߭ᰃϔℒ䕗Ўㅔऩⱘ⬏೒Ꮉ݋ˈࡳ㛑䕗ЎㅔऩˈⳌֵ㒣ᐌՓ ៥Ӏᑇᯊ᠔Փ⫼ⱘ೒ᔶ໘⧚Ꮉ݋᳝ PhotoShopǃWindows ⬏೒Ꮉ݋ㄝˈ݊Ё PhotoShop ᰃϔℒ䴲 6.1 ⬏೒䕃ӊὖ䗄 ㄀  ゴӓ:LQGRZV⬏೒ /Download at http://www.pin5i.com 䚼ᬒࠄ ImageService ㉏ЁˈᴀゴЁⱘ㉏೒བ೒ 6.2 ᠔⼎DŽ ಴Ў㒬೒ⱘࡳ㛑Ꮖ㒣ܼ䚼⬅ Tool ⱘᅲ⦄㉏এᅲ⦄ˈ᠔ҹ䰸њ㒬೒໪ⱘ݊ᅗࡳ㛑ⱘ䘏䕥ᅲ⦄ˈህܼ 䗝ᢽ᭛ӊᦤկϔ⾡ㅔऩⱘにষ䗝ᢽᴎࠊ˅ⱘ ImageFileChooser ㉏ˈ⫼Ѣ໘⧚䗝ᢽ᭛ӊᯊⱘ䖛Ⓒㄝ᪡԰DŽ ৘⾡㒘ӊDŽ⬅Ѣ៥Ӏ᳝䗝ᢽᠧᓔ೒⠛᭛ӊⱘ᪡԰ˈ᠔ҹӮ᳝ϔϾᠽሩ javax.swing.JFileChooser ㉏˄Ў ೼ᴀゴЁˈ⬏೒䕃ӊⱘЏ⬠䴶Փ⫼ ImageFrameˈ䆹㉏㒻ᡓѢ JFrameˈ䆹㉏Ӯ߱ྟ࣪⬏೒䕃ӊⱘ 䖭Ͼ㾘㣗ˈ䖭гᰃᴀゴᇚᎹ݋԰ЎϔϾ᥹ষⱘॳ಴DŽ 䳔㽕ᅮНᶤѯ㾘㣗ⱘᯊ׭ˈ៥Ӏৃҹᇚ䖭ѯ㾘㣗ݭࠄϔϾ᥹ষЁˈ䙷М䖭Ͼ᥹ষ᠔᳝ⱘᅲ⦄㉏䛑㽕䙉ᅜ ᅲ䰙Ϟ೼䖭Ͼ⬏೒Ꮉ݋Ёˈ᠔᳝ⱘᎹ݋䛑ᖙ乏䙉ᅜϔᅮⱘ㾘㣗ˈेՓ⫼哴ᷛ䖯㸠ᢪࡼǃ⚍ߏㄝࡼ԰ˈᔧ ҹ೼䖭䞠䆒䅵ϔϾੑৡЎ Tool ⱘ᥹ষˈ䖭Ͼ᥹ষᰃ᠔᳝Ꮉ݋ⱘ᥹ষˈ䞠䴶ᅮНњϔ㋏߫ⱘ哴ᷛࡼ԰DŽ ⫼ⱘᎹ݋ˈ✊ৢህ೼⬏ᵓЁ⫼哴ᷛ䖯㸠ᢪࡼǃ⚍ߏㄝࡼ԰ˈ⬏ᵓӮᰒ⼎ߎⳌᑨᎹ݋ⱘ᠔ѻ⫳ⱘᬜᵰˈ᠔ ᰃ⫼哴ᷛ䗝ᢽ䳔㽕ՓܜՓ⫼ Windows ⱘ⬏೒䕃ӊˈথ⦄೼㓪䕥೒⠛ⱘᯊ׭ˈ᳝ϾⳌԐⱘ䖛⿟ˈ佪 6.3 ߯ᓎ⬏೒Ꮉ݋ⱘ৘Ͼᇍ䈵 ᳝ᬜ㣗ೈDŽ া㽕ⶹ䘧Փ⫼ Java ⿟ᑣᴹᅲ⦄⬏೒ⱘॳ⧚ˈᅲ⦄⿟ᑣህकߚⱘㅔऩˈ݇䬂ᰃབԩ䅵ㅫ৘ϾᎹ݋ⱘ ׭ˈ៥Ӏৃҹᇚ䖭Ͼ BufferedImage ᇍ䈵䗮䖛 ImageIO ⱘ writer ᮍ⊩ݭࠄ᭛ӊЁDŽ Graphics ᇍ䈵ˈᕫࠄ䆹ᇍ䈵ˈህৃҹՓ⫼ᅗⱘ drawXXX ⱘᮍ⊩ᴹ䖯㸠⬏೒ˈࠄ᳔㒜䳔㽕䖯㸠ֱᄬⱘᯊ ៥Ӏৃҹ೼⬏೒ⱘ⬠䴶ЁֱᄬϔϾ BufferedImage ⱘᇍ䈵ˈ䙷Мৃҹ䗮䖛䖭Ͼᇍ䈵ᕫࠄϔϾ 6.2.3 ֱᄬ೒⠛ࡳ㛑 ⱘࡳ㛑ˈࠋᄤࡳ㛑ৃҹⳟ԰ᰃϔϾ⬏ヨࡳ㛑ˈাᰃՓ⫼ࠋᄤ⬏ߎᴹⱘ㒓↨⬏ヨ᳈㉫㗠ᏆDŽ ᔧࠡⱘऎඳDŽ䰸䖭ѯ⬏೒ࡳ㛑໪ˈ៥Ӏ䖬䳔㽕ᦤկϔϾࠋᄤܙ฿fillRect ⱘᮍ⊩ˈ៥Ӏৃҹ߽⫼䆹ᮍ⊩এ ⱑ㡆ⱘ㒓DŽᅲ⦄஋⍖ⱘࡳ㛑ˈৃҹ೼ᔧࠡ哴ᷛ⚍ߏⱘऎඳЁ⬏ϞⳌᑨⱘ⚍ˈGraphics ㉏ЁᦤկњϔϾ 䳔㽕⬏ⶽᔶⱘ䆱ˈৃҹ䇗⫼ drawRect ᮍ⊩DŽབᵰ䳔㽕ᅲ⦄‵Ⲃ᪺ⱘࡳ㛑ˈৃҹᇚ哴ᷛ㒣䖛ⱘऎඳ⬏Ϟ ⬏㒓៥Ӏৃҹ䇗⫼ drawLine ᮍ⊩ˈ䙷М⬏ἁ೚ⱘ䆱ৃҹ䇗⫼ Graphics ㉏ⱘ drawOval ᮍ⊩ˈབᵰ 6.2.2 ݊Ҫ⬏೒ࡳ㛑 drawLine ᮍ⊩ৢˈ៥Ӏݡᇍ⬠䴶ⱘ㒘ӊ䖯㸠ϔ⃵ repaint ህৃҹᅲ⦄⬏㒓ⱘࡳ㛑DŽ ⫼䆹ᮍ⊩ᴹ⬏㒓ⱘ䆱ˈ䳔㽕ᤩ㦋⫼᠋೼⬠䴶Ёᣝϟ哴ᷛⱘ⚍തᷛϢᬒᓔ哴ᷛᯊⱘ⚍തᷛDŽᔧ䇗⫼њ ᮍ⊩Ё᳝ಯϾখ᭄ˈߚ߿ᰃ㒓ⱘᓔྟ⚍തᷛ˄xǃy˅Ϣ㒓ⱘ㒧ᴳ⚍ⱘതᷛ˄xǃy˅ˈ಴ℸˈབᵰ䳔㽕䇗 ೼ Graphics Ё䆌໮ᮍ⊩ˈ݊Ё᳝ϔϾ drawLine ⱘᮍ⊩ˈՓ⫼䆹ᮍ⊩៥Ӏৃҹᇚ㒓⬏ࠄ⬠䴶Ёˈ䆹 6.2.1 ⬏㒓 Graphics ㉏ᴹ৥⬠䴶⬏ϞⳌᑨⱘݙᆍˈབᵰ䳔㽕䖯㸠᭛ӊ᪡԰ˈ៥ӀৃҹՓ⫼ Java ⱘ IO ᴹᅲ⦄DŽ ៥Ӏৃҹ㗗㰥ϔϟབԩՓ⫼ Java ⿟ᑣএᅲ⦄䖭ѯࡳ㛑ˈབᵰ䳔㽕䖯㸠⬏೒ˈ䙷М៥Ӏᔧ✊ህ䳔㽕 ⬏೒Ꮉ݋ॳ⧚ 6.2 ೒ 6.2 ⬏೒䕃ӊ㉏೒ 6.3.1 Ꮉ݋᥹ষTool Ң೒ 6.2 ЁৃҹⳟࠄˈᎹ݋᥹ষ Tool ᅮНњ哴ᷛࡼ԰ⱘಯϾᮍ⊩ˈߚ߿ᰃᢪࡼ mouseDrapped()ǃ ⿏ࡼ mouseMoved()ǃᵒ ᓔ mouseReleased()ǃᣝ ϟ mousePressed()ǃ⚍ ߏ mouseClicked()ѨϾࡼ԰ˈ ᑊ⫼ String ㉏ൟⱘᐌ䞣ሲᗻᴹᅮНᎹ݋ⱘ㉏ൟDŽ䖭Ͼ᥹ষা᳝ϔϾᅲ⦄㉏ AbstractToolˈ㗠↣ϾᎹ݋㉏ 䛑ᰃএᠽሩ AbstractTool ㉏ˈ೼೒Ё㸼⦄Ў Tool1ǃTool2…….ToolNDŽҹϟᰃℸ᥹ষᅮНⱘሲᗻϢᮍ⊩˖ ‰ static final String ARROW_TOOLˈㆁ༈Ꮉ݋㉏ൟDŽ ‰ static final String PENCIL_TOOLˈ䪙ヨᎹ݋㉏ൟDŽ ‰ static final String BRUSH_TOOL ˈࠋᄤᎹ݋㉏ൟDŽ ‰ static final String CUT_TOOLˈ࠾ߛᎹ݋㉏ൟDŽ ‰ static final String ERASER_TOOLˈ‵Ⲃ᪺Ꮉ݋㉏ൟDŽ ‰ static final String LINE_TOOL ˈⳈ㒓Ꮉ݋㉏ൟDŽ ‰ static final String RECT_TOOL ˈⶽᔶᎹ݋㉏ൟDŽ ‰ static final String POLYGON_TOOL ˈ໮䖍ᔶᎹ݋㉏ൟDŽ ‰ static final String ROUND_TOOLˈἁ೚ᔶᎹ݋㉏ൟDŽ Download at http://www.pin5i.com/ /Download at http://www.pin5i.com 䚼ߚⱘ㸠Ўˈ݊Ҫⱘ㸠Ў᳈Ꮰᳯ䅽ᅗⱘᄤ㉏এᅲ⦄ˈ䙷М៥Ӏৃҹᇚ䖭ѯᇍ䈵԰ЎϔϾᢑ䈵㉏DŽ ㉏ᇍ䈵䛑ᖙ乏䙉ᅜᶤѯ㸠Ўˈ䙷М៥Ӏৃҹᇚ䖭ѯ㸠Ў䛑ᔧ԰㾘㣗ˈݭࠄ᥹ষЁ˗བᵰ᳝ѯᇍ䈵ᅲ⦄њ ᥹ষ⫼ѢᅮН㾘㣗ˈ䙷Мᢑ䈵㉏ህᰃ⫼Ѣᅲ⦄䚼ߚⱘ㾘㣗DŽᔧ៥Ӏ೼㓪ݭ⿟ᑣⱘ䖛⿟Ёথ⦄ˈ᳝ϔ ‰ void dragBorder( MouseEvent e )ˈᢪࡼ䖍⬠ˈгህᰃᬍব⬏Ꮧⱘ໻ᇣDŽ ᰃ䍋⚍തᷛˈ˄x2ˈy2˅ᰃ㒜⚍തᷛDŽ䖭Ͼᮍ⊩ᰃϔϾぎⱘᮍ⊩ˈЏ㽕ᰃ⬅݊ᄤ㉏ᅲ⦄DŽ ‰ void draw(Graphics g, int x1, int y1, int x2, int y2)ˈ⬏೒ᔶˈg ᰃ⫼ᴹ⬏೒ᔶⱘᇍ䈵ˈ˄x1ˈy1˅ ⫼ Graphics ㉏ൟⱘᇍ䈵 g এ⬏೒ᔶDŽ ‰ void createShape( MouseEvent e ,Graphics g )ˈ⬏೒ᔶˈ䗮䖛খ᭄ e এ㦋প哴ᷛⱘ䔼䗍ˈᑊ ‰ void mouseClicked MouseEvent e)ˈᅲ⦄ᔧᤩᤝࠄ哴ᷛ⚍ߏᯊ䇗⫼ⱘᮍ⊩DŽ ‰ void mousePressed MouseEvent e)ˈᅲ⦄ᔧᤩᤝࠄ哴ᷛᣝϟᯊ䇗⫼ⱘᮍ⊩DŽ ‰ void mouseReleased MouseEvent e)ˈᅲ⦄ᔧᤩᤝࠄ哴ᷛᵒᓔᯊ䇗⫼ⱘᮍ⊩DŽ ‰ void mouseMoved MouseEvent e)ˈᅲ⦄ᔧᤩᤝࠄ哴ᷛ⿏ࡼᯊ䇗⫼ⱘᮍ⊩DŽ ‰ void mouseDragged( MouseEvent e)ˈᅲ⦄ᔧᤩᤝࠄ哴ᷛᢪࡼᯊ䇗⫼ⱘᮍ⊩DŽ ‰ int getPressY()ˈ䖨ಲϞ⃵哴ᷛᣝϟⱘ y തᷛDŽ ‰ int getPressX()ˈ䖨ಲϞ⃵哴ᷛᣝϟⱘ x തᷛDŽ ‰ void setPressY(int y)ˈ䆒㕂哴ᷛᣝϟⱘ y തᷛˈint ㉏ൟⱘ y Ў哴ᷛⱘ y തᷛDŽ ‰ void setPressX(int x)ˈ䆒㕂哴ᷛᣝϟⱘ x തᷛˈint ㉏ൟⱘ x Ў哴ᷛⱘ x തᷛDŽ ‰ void setDefaultCursor( Cursor cursor )ˈ䆒㕂哴ᷛᣛ䩜ᣛ䩜ˈҹ Cursor Ўখ᭄DŽ ‰ Cursor getDefaultCursor()ˈℸᮍ⊩㦋প咬䅸哴ᷛᣛ䩜ⱘᔶ⢊DŽ ᭄ˈpath ᰃᎹ݋ⱘ೒ᷛ䏃ᕘDŽ ‰ AbstractTool( ImageFrame frame, String path )ˈ䅽ᄤ㉏䇗⫼ⱘᵘ䗴఼ˈҹ ImagerFrame Ўখ প⬏ᵓⱘሲᗻDŽ ‰ AbstractTool( ImageFrame frame )ˈ䅽ᄤ㉏䇗⫼ⱘᵘ䗴఼ˈҹ ImagerFrame Ўখ᭄ˈ⫼Ѣ㦋 ᮍ⊩བϟ˖ 䞡໡ⱘᅲ⦄ˈা݇ᖗϢᴀ㉏Ⳍ݇ⱘ䘏䕥ˈAbstractTool ᠔ᅮНⱘخⱘᅲ⦄ˈ䙷Мᅗⱘᄤ㉏ህৃҹϡᖙݡ ЁᅮНⱘ᠔᳝ᮍ⊩ˈᑊᠽሩњ݊ᅗᮍ⊩ˈ䅽݊ᄤ㉏㒻ᡓ៪㗙䞡ݭDŽ䆹㉏ЁЎ݊ҪⱘᎹ݋㉏ᦤկњ໻䚼ߚ ㉏ˈ᠔ҹᑊϡ㛑㹿߯ᓎˈা㛑㹿㒻ᡓDŽℸ㉏ᅲ⦄ ToolڣAbstractTool ᰃ Tool ⱘᅲ⦄㉏ˈгᰃϔϾᢑ 6.3.2 Toolⱘᅲ⦄㉏AbstractTool ᤶ㿔ПˈՓ⫼㗙াϢ ToolFactory 㗺ড়DŽ ᇥҷⷕⱘׂᬍDŽޣ಴ℸՓ⫼㗙ḍᴀϡ䳔㽕݇ᖗՓ⫼ⱘᰃાϔϾᅲ⦄㉏ˈᔧҷⷕথ⫳ᬍবⱘᯊ׭ˈгৃҹ ៥ӀৃҹՓ⫼ϔϾ ToolFactor ⱘ㉏ᴹᕫࠄ݋ԧⱘᶤϾ Tool ᅲ⦄㉏ˈToolFactory 䖨ಲⱘ䛑ᰃ Tool ᥹ষˈ ӏԩⱘџᚙˈ䖭ѯᮍ⊩⬅ᅗⱘᅲ⦄㉏এ݋ԧᅲ⦄DŽ೼ᶤϾᇍ䈵Ё䳔㽕Փ⫼ࠄ Tool ⱘᅲ⦄㉏ᯊˈخህϡݡ Ң᥹ষЁᅮНⱘሲᗻϢᮍ⊩ৃҹⳟߎˈ೼᥹ষЁাᅮНᎹ݋ⱘ㉏ൟˈ䖬᳝ᅮНᎹ݋哴ᷛࡼ԰ⱘᮍ⊩ˈ ‰ void mouseClicked MouseEvent e)ˈᔧᤩᤝࠄ哴ᷛ⚍ߏᯊ䇗⫼ⱘᮍ⊩ᅮНDŽ ‰ void mousePressed MouseEvent e)ˈᔧᤩᤝࠄ哴ᷛᣝϟᯊ䇗⫼ⱘᮍ⊩ᅮНDŽ ‰ void mouseReleased MouseEvent e)ˈᔧᤩᤝࠄ哴ᷛᵒᓔᯊ䇗⫼ⱘᮍ⊩ᅮНDŽ ‰ void mouseMoved MouseEvent e)ˈᔧᤩᤝࠄ哴ᷛ⿏ࡼᯊ䇗⫼ⱘᮍ⊩ᅮНDŽ ‰ void mouseDragged( MouseEvent e)ˈᔧᤩᤝࠄ哴ᷛᢪࡼᯊ䇗⫼ⱘᮍ⊩ᅮНDŽ ‰ static final String COLORPICKED_TOOL ˈ买㡆䗝ᢽᎹ݋㉏ൟDŽ ‰ static final String ATOMIZER_TOOLˈ஋๼Ꮉ݋㉏ൟDŽ static final String ROUNDRECT_TOOL ˈ೚㾦ⶽᔶᎹ݋㉏ൟDŽ ‰ /Download at http://www.pin5i.com ‰ initDrawSpace( ImageFrame frame )ˈ߱ྟ࣪⬏ᵓDŽ њҹϟⱘᮍ⊩˖ ⾡䴶ᵓⱘᰒ⼎Ϣ䱤㮣ǃ买㡆ⱘ㓪䕥ǃᭈϾ⬠䴶ⱘࠋᮄǃ㦰ऩㄝϮࡵ䘏䕥䛑ᬒ೼䖭Ͼ㉏Ёᅲ⦄ˈ䆹㉏ࣙ৿ 䰸哴ᷛⱘ⬏೒ࡳ㛑໪˄⬏೒ࡳ㛑⬅ Tool ⱘᅲ⦄㉏ᅠ៤˅ˈ߱ྟ࣪⬏ᵓǃ೒⠛ⱘᮄᓎᠧᓔϢֱᄬǃ৘ 6.3.5 Ϯࡵ䘏䕥㉏ImageService ⦄㉏DŽ ᭄᥂ˈݡӴ䗦㒭݋ԧⱘϮࡵ㉏ˈ䅽݊䖯㸠Ⳍ݇ⱘ໘⧚DŽ೼ᴀゴˈ䋳䋷໘⧚⬏೒ࡳ㛑ⱘЏ㽕ᰃ Tool ⱘᅲ ⬠䴶㉏㉏ԐѢ៥Ӏ MVC ῵ᓣЁⱘ V˄㾚೒˅ˈ䆹㉏ᑊϡ䋳䋷໘⧚ӏԩⱘ䘏䕥ˈЏ㽕䋳䋷Ң⬠䴶᥹ᬊ ‰ void paint( Graphics g )ˈ⬏೒DŽ Ͼᮍ⊩ˈህᰃϔϾ⫼Ѣ㒬೒ⱘᮍ⊩ˈབϟ˖ ᔧ⬏೒Ꮉ݋ⱘ⬏ᵓˈℸݙ䚼㉏া᳝ϔܙ঺໪ˈℸ㉏᳝ϔϾ㒻ᡓ JPanel ⱘݙ䚼㉏ DrawSpaceˈ⫼Ѣ ‰ JPanel createToolPanel()ˈ߯ᓎ⫼Ѣ⬏೒ⱘᎹ݋ᷣDŽ ‰ JPanel createDrawSpace()ˈ߯ᓎ⬏ᵓDŽ ‰ void createMenuBar()ˈ߯ᓎ᭛ӊǃᶹⳟǃ买㡆ǃᐂࡽㄝ㦰ऩᷣDŽ ᵓⱘ催ϢᆑㄝሲᗻDŽ ‰ Dimension getScreenSize()ˈ㦋প Dimension ㉏ᔶ screenSizeˈscreenSize Џ㽕⫼Ѣ㦋প⬏ ‰ JPanel getCurrentColorPanel()ˈ㦋প买㡆䗝ᢽ䴶ᵓDŽ ‰ JPanel createColorPanel()ˈ߯ᓎϔϾㅔऩⱘ买㡆䗝ᢽ䴶ᵓDŽ ‰ JColorChooser getColorChooser()ˈ㦋প买㡆䗝ᢽ఼DŽ ‰ Tool getTool()ˈ㦋পℷ೼Փ⫼ⱘᎹ݋DŽ ‰ void setTool( Tool tool )ˈ䆒㕂ℷ೼Փ⫼ⱘᎹ݋DŽ ⱘϔϾᠽሩ㉏DŽ ‰ void setBufferedImage( MyImage bufferedImage)ˈ䆒㕂⬏ᵓ೒⠛ˈMyImage ᰃ BufferedImage ‰ MyImage getBufferedImage()ˈ㦋প⬏ᵓЁⱘ೒⠛DŽ ‰ JPanel getColorPanel()ˈ㦋প买㡆䴶ᵓDŽ ‰ JPanel getDrawSpace()ˈ㦋প⬏ᏗDŽ ‰ void init()ˈ䆒㕂࣪Џ⬠䴶DŽ 䖭Ͼ⬏೒Ꮉ݋ⱘ⬠䴶ⱘЏ㽕ᬒ೼䖭Ͼ㉏Ёᅲ⦄ˈℸ㉏᳝ҹϟᮍ⊩˖ 6.3.4 ⬠䴶㉏ImageFrame 䴶ⱘゴ㡖ЁˈᇚӮ䆆㾷བԩᅲ⦄䖭 11 Ͼᄤ㉏DŽ ᓣˈ᠔᳝ⱘᄤ㉏䛑ᦤկњ getInstance ⱘᮍ⊩ᴹ䖨ಲᴀ㉏ⱘᅲ՟ˈᑊϨ᠔᳝ⱘᵘ䗴఼䛑ᰃ⾕᳝ⱘDŽ೼ϟ ⬅Ѣ៥ӀᑊϡᏠᳯ໪⬠ৃҹⳈ᥹Փ⫼ new ݇䬂ᄫᴹ߯ᓎ䖭ѯ㉏ⱘᅲ՟ˈ಴ℸ೼䖭䞠Փ⫼њऩᗕ῵ ‰ static Tool getInstance( ImageFrame frame )ˈ㦋প Tool ㉏ൟⱘᴀ㉏ᅲ՟DŽ ᳯ㹿Ⳉ᥹ᅲ՟࣪ˈ᠔ҹℸ㉏ⱘᵘ䗴఼⾕᳝ˈᑊᦤկϔϾ䴭ᗕⱘᮍ⊩㦋প Tool ㉏ൟⱘℸ㉏ᅲ⦄ˈབϟ˖ 䞡ݭ AbstractTool ⱘ䚼ߚ៪㗙ܼ䚼ᮍ⊩DŽ⬅Ѣ೼ᴀ䆒䅵Ёˈᛇ䖭ѯ㉏೼໪㸼⦄Ў Tool ᥹ষˈϡᏠމᚙ ˄೚ⶽᔶ˅ǃAtomizerTool˄஋๼˅ǃColorPickedTool˄买㡆䗝ᢽ˅11 Ͼᄤ㉏ˈ䖭ѯᄤ㉏䛑ᰃḍ᥂㞾Ꮕⱘ Ⲃ᪺˅ǃLineTool˄Ⳉ㒓˅ǃRectTool˄ⶽᔶ˅ǃPolygonTool˄໮䖍ᔶ˅ǃRoundTool˄ἁ೚ᔶ˅ǃRoundRectTool AbstractTool ϔ᳝݅ ArrowTool˄ㆁ༈˅ǃPencilTool˄䪙ヨ˅ǃBrushTool˄ࠋᄤ˅ǃEraserTool˄‵ AbstractToolⱘᄤ㉏ 6.3.3 /Download at http://www.pin5i.com //㦋পℷ೼Փ⫼ⱘᎹ݋ //䆒㕂ᷛ乬 service.initDrawSpace( this ); //߱ྟ࣪⬏೒ this.setTitle( "᳾ੑৡ - ⬏೒" ); //䆒㕂ᷛ乬 public void init() { ҷⷕ⏙ऩ˖code\image\src\org\crazyit\image\ImageFrame.java ҷⷕDŽ ᳔ৢ㦋পᎺ䖍Ꮉ݋ᷣ䴶ᵓǃϟ䴶㦰ऩᷣ䴶ᵓǃ㦰ऩˈᑊᡞ䖭ѯ䴶ᵓϢ⬏೒㦋পࡴࠄ JFrame ЁDŽ㾕ҹϟ PENCIL_TOOL(䪙ヨ)㉏ൟⱘ Toolˈ߯ᓎ৘⾡哴ᷛⲥ਀఼ˈᑊ೼ⲥ਀ⱘᠻ㸠ᮍ⊩Ё䇗⫼ Tool ⱘⳌᑨᮍ⊩ˈ 䆒㕂 JFrame にষⱘᷛ乬ˈ᥹ϟᴹ߱ྟ࣪⬏೒ऎඳˈ߱ྟ࣪Ўⱑ㡆ˈ✊ৢݡ㦋পˈܜ佪 6.4.1 ߱ྟ࣪⬠䴶˄init()ᮍ⊩˅ ⳟЏ⬠䴶ⱘ߱ྟ࣪˖ܜBorderLayout.CENTER ԡ㕂ˈ买㡆䗝ᢽ䴶ᵓ೼ BorderLayout.SOUTH ԡ㕂DŽ ޴䚼ߚ㒘៤ˈ⫼ BorderLayou ⱘᥦᵓᮍᓣˈᎺ䖍Ꮉ݋ᷣ೼ BorderLayou.WEST ԡ㕂ˈ⬏೒ऎ೼ ೼䖭Ͼ䕃ӊЁˈЏ⬠䴶Џ㽕⬅Ꮊ䖍ⱘᎹ݋ᷣǃϟ䴶ⱘ买㡆䗝ᢽᵓǃऴ໻䚼ߚऎඳⱘ⬏೒ऎǃ㦰ऩㄝ 6.4 Џ⬠䴶ᅲ⦄ 㡖Ёˈ៥Ӏা㽕ᣝ✻䖭ѯᅮНདⱘᮍ⊩ˈ䗤ℹএᅲ⦄៥Ӏⱘ⬏೒Ꮉ݋DŽ ೼ᴀᇣ㡖Ёˈ៥ӀЏ㽕⹂ᅮњ⬏೒Ꮉ݋᠔⍝ঞⱘ޴Ͼᇍ䈵ˈᑊᅮНњҪӀⱘ㸠ЎϢሲᗻˈ೼ϟ䴶ゴ ‰ boolean accept( File f )ˈ߸ᮁᰃ৺ᰃড়⊩ⱘ᭛ӊ㉏ൟDŽ ᮍ⊩ˈ߸ᮁᰃ৺ᰃড়⊩ⱘ᭛ӊ㉏ൟˈབϟ˖ 䖭Ͼ㉏Ё᳝ϔϾ㒻ᡓ FileFilter ㉏ⱘݙ䚼㉏ MyFileFilterˈ䖭Ͼݙ䚼㉏Џ㽕ᰃ䞡ݭ FileFilter ⱘ accept ‰ void addFilter()ˈ๲ࡴ᭛ӊ䖛Ⓒ఼ˈ䖭䞠া䗝ᢽ೒⠛㉏ᔶⱘ᭛ӊDŽ ‰ String getSuf()ˈ㦋প᭛ӊⱘৢ㓔ৡDŽ ࠊˈ៥Ӏ䖭䞠ᠽሩ䖭Ͼ㉏ˈᰃЎњ๲ࡴ៥Ӏ㞾Ꮕⱘ᭛ӊ䖛Ⓒ఼DŽ㾕ҹϟᮍ⊩˖ ImageFileChooser ㉏㒻ᡓњ JFleChooser ㉏ˈJFleChooser ᰃ Java ᦤկⱘϔϾㅔऩⱘ᭛ӊ䗝ᢽᴎ 6.3.6 ᭛ӊ䗝ᢽ㉏ImageFileChooser ᇍ䈵ˈᅗᑊ≵ֱ᳝ᄬϔѯ⢊ᗕሲᗻDŽ 䰸њ⬏೒ࡳ㛑໪ˈImageService 䋳䋷њᭈϾ⬏೒Ꮉ݋ⱘ݊Ҫࡳ㛑ˈ೼ᴀゴЁˈ䆹㉏ᰃ᮴⢊ᗕⱘ Java ‰ void menuDo( ImageFrame frame, String cmd )ˈ໘⧚㦰ऩџӊDŽ ‰ void exit( ImageFrame frame )ˈ䗔ߎ⬏೒䕃ӊDŽ ‰ void editColor( ImageFrame frame )ˈ㓪䕥买㡆DŽ ‰ void createGraphics( ImageFrame frame )ˈ߯ᓎᮄ೒⠛ᑊ߱ྟ࣪DŽ ‰ void open( ImageFrame frame )ˈᠧᓔ೒⠛DŽ ‰ void save( boolean b, ImageFrame frame )ˈֱᄬ೒⠛DŽ ‰ static Cursor createCursor( String path )ˈ߯ᓎ哴ᷛ೒ᔶDŽPath ᰃ哴ᷛ೒ᔶⱘ䏃ᕘDŽ ‰ repaint( Graphics g, BufferedImage bufferedImage )ˈࠋᮄ⬠䴶DŽ Dimension getScreenSize()ˈ㦋পሣᐩⱘߚ䕼⥛DŽ ‰ tool = ToolFactory.getToolInstance( this, PENCIL_TOOL ); //߯ᓎ哴ᷛ䖤ࡼⲥ਀఼ MouseMotionListener motionListener = new MouseMotionAdapter() { //ᢪࡼ哴ᷛ public void mouseDragged(MouseEvent e) { tool.mouseDragged( e ); } //⿏ࡼ哴ᷛ public void mouseMoved(MouseEvent e) { tool.mouseMoved( e ); } }; //߯ᓎ哴ᷛⲥ਀఼ MouseListener mouseListener = new MouseAdapter(){ //ᵒᓔ哴ᷛ public void mouseReleased( MouseEvent e ) { tool.mouseReleased( e ); } //ᣝϟ哴ᷛ public void mousePressed(MouseEvent e) { tool.mousePressed( e ); } //⚍ߏ哴ᷛ public void mouseClicked(MouseEvent e) { tool.mouseClicked( e ); } }; drawSpace.addMouseMotionListener( motionListener ); drawSpace.addMouseListener( mouseListener ); createMenuBar(); //ҹ drawSpace Ў viewport এ߯ᓎϔϾ JScrollPane scroll = new JScrollPane( drawSpace ); //䆒㕂 viewport ImageService.setViewport( scroll, drawSpace , bufferedImage.getWidth(), bufferedImage.getHeight() ); //ᇚ panel ࡴࠄᴀ Frame Ϟ䴶 this.add( scroll, BorderLayout.CENTER ); //this.add( toolPanel, BorderLayout.WEST ); //this.add( colorPanel, BorderLayout.SOUTH ); } ৃҹⳟࠄˈ䖭䞠᳝ϸ⾡哴ᷛⲥ਀఼ˈMouseMotionListener ੠ MouseListenerˈMouseMotionListener Џ㽕ᰃⲥ਀哴ᷛⱘ䖤ࡼࡼ԰ˈ៥Ӏᅲ⦄њᅗⱘ mouseDragger˄哴ᷛᢪࡼ˅Ϣ mouseMoved˄哴ᷛ⿏ࡼ˅ ᮍ⊩ˈMouseListener 䋳䋷ⲥ਀哴ᷛⱘ݊ᅗࡼ԰ˈ៥Ӏᅲ⦄њᅗⱘ mouseReleased˄ᵒᓔ哴ᷛ˅ǃ mousePressed˄ᣝϟ哴ᷛ˅੠ mouseClicked˄⚍ߏ哴ᷛ˅ϝϾᮍ⊩DŽҹϞҷⷕⱘ咥ԧ䚼ߚˈ䖭ϝ㸠ҷ ⷕߚ߿߯ᓎ㦰ऩǃ⬏೒Ꮉ݋ᷣϢ买㡆䗝ᢽ䴶ᵓˈབԩ߯ᓎ៥Ӏᇚ೼ 6.4.3ǃ6.4.4 ੠ 6.4.5 Ё䆺㒚ᦣ䗄DŽ ⦄೼䖤㸠⬏೒Ꮉ݋ˈৃҹⳟࠄᬜᵰབ೒ 6.3 ᠔⼎DŽ Download at http://www.pin5i.com/ /Download at http://www.pin5i.com ҷⷕ⏙ऩ˖code\image\src\org\crazyit\image\ImageFrame.java drwaSpace ⱘ໻ᇣˈᑊϨ䖨ಲˈ㾕ҹϟҷⷕDŽ ᰃ new ϔϾ DrawSpace ᅲ՟ˈݡ䆒㕂䖭Ͼܜҹ䖭Ͼ߯ᓎ⬏ᵓⱘᮍ⊩೼ᴀ㉏Ёা㹿䇗⫼ϔ⃵DŽ佪 㗠㦋প䖭Ͼ⬏ᵓህᰃএ߯ᓎϔϾ䖭Ͼ⬏ᵓ㉏ⱘᅲ՟ˈ⬅Ѣ៥Ӏⱘ⬏೒䕃ӊᰃ↣⃵䛑া㓪䕥ϔᓴ೒⠛ˈ᠔ 䞠䳔㽕⊼ᛣⱘᰃˈ㽕䇗⫼ℸᮍ⊩ˈᑊϡᰃⳈ᥹䇗⫼ paint ᮍ⊩ˈ㗠ᰃ䇗⫼ ImageService ⱘ repaint ᮍ⊩DŽ ҢϞ䴶ҷⷕৃҹⳟࠄˈ䖭Ͼݙ䚼㉏↨䕗ㅔऩˈাᰃ㒻ᡓ JPanelˈᑊ䞡ݭ JPanel ⱘ paint ᮍ⊩ˈ䖭 } } service.repaint( g, bufferedImage ); //draw public void paint( Graphics g ) { */ * @return void * @param g Graphics * * 䞡ݭ void paint( Graphics g )ᮍ⊩ /** public class DrawSpace extends JPanel { / / ⬏೒ऎඳ ҷⷕ⏙ऩ˖code\image\src\org\crazyit\image\ImageFrame.java ᔧ䖭Ͼ㒬೒ऎඳˈ㾕ҹϟҷⷕDŽܙএ 䖭ᰃϔϾ⬏೒Ꮉ݋ˈ᠔ҹ䳔㽕ϔϾৃҹ㒬೒ⱘऎඳˈ೼䖭䞠៥Ӏ⫼㒻ᡓ JPanel ⱘݙ䚼㉏ DrawSpace 6.4.2 㦋প⬏ᵓ ೒ 6.3 Џ⬠䴶 /Download at http://www.pin5i.com JMenu menu = new JMenu( menuArr[i] ); //ᮄᓎϔϾ JMenu 㦰ऩ for( int i = 0 ; i < menuArr.length ; i++ ) { //䘡ग़ menuArr Ϣ menuItemArr এ߯ᓎ㦰ऩ }; { "ᐂࡽЏ乬", "݇Ѣ" } { "㓪䕥买㡆" }, {"Ꮉ݋ㆅ(T)", "买᭭Ⲧ(C)"}, {"ᮄᓎ(N)", "ᠧᓔ(O)", "ֱᄬ(S)", "-", "䗔ߎ(X)"}, String[][] menuItemArr = { //㦰ऩ乍᭛ᄫ᭄㒘 String[] menuArr = { "᭛ӊ(F)", "ᶹⳟ(V)", "买㡆(C)", "ᐂࡽ(H)" }; //㦰ऩ᭛ᄫ᭄㒘ˈϢϟ䴶ⱘ menuItemArr ϔϔᇍᑨ JMenuBar menuBar = new JMenuBar(); //߯ᓎϔϾ JMenuBar ᬒ㕂㦰ऩ public void createMenuBar() { //߯ᓎ㦰ऩ ҷⷕ⏙ऩ˖code\image\src\org\crazyit\image\ImageFrame.java ᅗࡴϞϔϾࡼ԰ⲥ਀఼ˈএⲥ਀䖭Ͼ㦰ऩ乍ᰃ৺㹿⚍ߏDŽ䇋ⳟҹϟҷⷕDŽ гᬒ೼ϔϾ String ㉏ൟⱘѠ㓈᭄㒘䞠䴶ˈএ䗁ҷ߯ᓎ↣Ͼ JmenuItem˄㦰ऩ乍˅ˈ↣߯ᓎᅠϔϾˈህЎ 䞠䴶ˈᑊ䗁ҷ䖭Ͼ᭄㒘এ߯ᓎϔϾ JMenuˈJMenu ህᰃᣛ㦰ऩDŽৠḋˈᡞҪӀϟ䴶ⱘ৘Ͼ㦰ऩ乍᭛ᄫ ⬅Ѣ㦰ऩ↨䕗ㅔऩˈ៥Ӏᡞ᭛ӊǃᶹⳟǃ买㡆ǃᐂࡽಯϾ㦰ऩ᭛ᄫᬒ೼ϔϾString ㉏ൟⱘ᭄㒘menuArr -݇Ѣ -ᐂࡽЏ乬 -ᐂࡽ˄H˅ 㓪䕥买㡆 -买㡆˄C˅ -买᭭Ⲧ˄C˅ -Ꮉ݋ㆅ˄T˅ -ᶹⳟ˄V˅ -䗔ߎ˄X˅ -ֱᄬ˄S˅ -ᠧᓔ˄O˅ -ᮄᓎ˄N˅ -᭛ӊ˄F˅ 䖭Ͼ䕃ӊⱘ㦰ऩ㒘㒛ᔶᓣབϟ˖ 6.4.3 ߯ᓎ㦰ऩ } return drawSpace; , (int)screenSize.getHeight() - 150 ) ); new Dimension( (int)screenSize.getWidth() drawSpace.setPreferredSize( //䆒㕂 drawSpace ⱘ໻ᇣ JPanel drawSpace = new DrawSpace(); public JPanel createDrawSpace() { ߯ᓎ⬏ᵓ// for( int j = 0 ; j < menuItemArr[i].length ; j++ ) { //བᵰ menuItemArr[i][j]ㄝѢ"-" if ( menuItemArr[i][j].equals( "-" ) ) { //䆒㕂㦰ऩߚ䱨 menu.addSeparator(); } else { //ᮄᓎϔϾ JMenuItem 㦰ऩ乍 JMenuItem menuItem = new JMenuItem( menuItemArr[i][j] ); menuItem.addActionListener( menuListener ); //ᡞ㦰ऩ乍ࡴࠄ JMenu 㦰ऩ䞠䴶 menu.add( menuItem ); } } //ᡞ㦰ऩࡴࠄ JMenuBar Ϟ menuBar.add(menu); } //䆒㕂 JMenubar this.setJMenuBar( menuBar ); } ҹϞҷⷕⱘ㉫ԧ䚼ߚˈᰃЎ㦰ऩ⏏ࡴⳌᑨⱘߚ䱨ヺDŽ೼ϔ㠀ⱘϟᢝ㦰ऩЁˈৃҹ䗮䖛ߚ䱨ヺᇚ㦰ऩ ߦߚЎ໮ϾഫDŽࡴܹ㦰ऩৢЏ⬠䴶ⱘ݋ԧᬜᵰབ೒ 6.4 ᠔⼎DŽ ೒ 6.4 ࡴܹ㦰ऩৢⱘ⬠䴶 6.4.4 ߯ᓎ⬏೒Ꮉ݋ᷣ ⬏೒Ꮉ݋ᷣˈ䖭䞠ᅲ⦄ⱘᎹ݋᳝䪙ヨǃࠋᄤǃᣒ㡆఼ǃ஋ᵾǃ‵Ⲃ᪺ǃⳈ㒓ǃ໮䖍ᔶǃⶽᔶǃἁ೚ Download at http://www.pin5i.com/ /Download at http://www.pin5i.com } frame.setTool( tool ); //䆒㕂ℷ೼Փ⫼ⱘ tool if( tool != null ) { : tool; tool = name != "" ? ToolFactory.getToolInstance( frame, name ) //䆒㕂 tool public void actionPerformed( ActionEvent e ) { ҷⷕ⏙ऩ˖code\image\src\org\crazyit\image\ImageAction.java 䞡ݭⱘ actionPerformed ᮍ⊩ˈ䖭Ͼᮍ⊩ህᰃ⫼ᴹᠻ㸠ⲥ਀ࠄᣝ䪂㹿⚍ߏৢⱘᮍ⊩DŽ 䖭ѯᣝ䪂˛೼䖭䞠ˈⲥ਀ⱘᎹ԰ህᬒࠄ㒻ᡓ AbstractAction ⱘ ImageAction এᅲ⦄ˈ㾕 ImageAction Ё ⱘᄤ㉏ˈᑊҹ䖭Ͼ ImageAction এ߯ᓎϔϾ JButtonˈᑊᡞ JButton ࡴࠄ JToolBar ЁDŽ䙷МˈᗢМⲥ਀ ᄬᬒ䖭ѯᎹ݋㉏ᔶⱘ᭄㒘ˈ↣⃵䛑ҹᎹ݋೒ᷛⱘ䏃ᕘǃᎹ݋ৡএ߯ᓎϔϾ ImageAction˄AbstractAction˅ 䆒㕂 JToolBar ⱘᷛ乬ǃᥦ߫ᮍᓣ˄ൖⳈ˅ǃᰃ৺ৃҹᢪࡼǃϢ䖍⬠ⱘ䎱⾏ǃ䖬᳝Ꮧሔᮍᓣˈ᥹ϟᴹ䘡ग़ ៥Ӏ߯ᓎϔϾᬒ㕂䖭ѯᣝ䪂ⱘ JToolBarˈJtoolBar ᰃ Java Swing ᦤկⱘϔϾᎹ݋ᷣ㉏ˈᑊˈܜ佪 } return panel; //䖨ಲ panel.add( toolBar ); } toolBar.add(button); //ᡞ button ࡴࠄᎹ݋ᷣЁ JButton button = new JButton( action ); //ҹ೒ᷛ߯ᓎϔϾᮄⱘ button , toolarr[i], this ); new ImageIcon("img/" + toolarr[i] + ".jpg") ImageAction action = new ImageAction( for( int i = 0 ; i < toolarr.length ; i++ ) { , RECT_TOOL, ROUND_TOOL, ROUNDRECT_TOOL}; , ATOMIZER_TOOL, ERASER_TOOL, LINE_TOOL, POLYGON_TOOL String[] toolarr = { PENCIL_TOOL, BRUSH_TOOL, COLORPICKED_TOOL //Ꮉ݋᭄㒘 toolBar.setLayout( new GridLayout( 5, 2, 2, 2 ) ); //䆒㕂Ꮧሔᮍᓣ toolBar.setMargin( new Insets( 2, 2, 2, 2) ); //䆒㕂Ϣ䖍⬠ⱘ䎱⾏ toolBar.setFloatable( true ); //䆒㕂Ўৃҹᢪࡼ toolBar.setOrientation( toolBar.VERTICAL ); //䆒㕂ЎൖⳈᥦ߫ JToolBar toolBar = new JToolBar( "Ꮉ݋" ); //߯ᓎϔϾᷛ乬Ў"Ꮉ݋"ⱘᎹ݋ᷣ JPanel panel = new JPanel(); //߯ᓎϔϾ JPanel public JPanel createToolPanel() { ҷⷕ⏙ऩ˖code\image\src\org\crazyit\image\ImageFrame.java ҹ೒ᔶⱘᮍᓣ߯ᓎᣝ䪂ˈ঺໪ˈ៥ӀӮ᳝ϔϾ AbstractAction ⱘᅲ⦄㉏DŽ㾕ҹϟҷⷕ˖ action)ᵘ䗴఼এ߯ᓎ JButtonˈ⫼䖭Ͼᵘ䗴఼߯ᓎ JButtonˈৃҹ೼ AbstractAction Ёࡴܹᣝ䬂ⱘ೒ᷛˈ ᔶ੠೚ⶽᔶˈ䖭䞠ˈ↣ϾᎹ݋៥Ӏ⫼ϔϾ JButton এҷ㸼ᅗˈՓ⫼ JButton ⱘ JButton(AbstractActoin /Download at http://www.pin5i.com JPanel panel = new JPanel(); //ᮄᓎϔϾ JPanel public JPanel createColorPanel() { ҷⷕ⏙ऩ˖code\image\src\org\crazyit\image\ImageFrame.java Ͼ JButton ⱘ买㡆ˈ᳔ৢࡴࠄ JToolBar ЁDŽ㾕ҹϟҷⷕDŽ ൟⱘ᭄㒘ˈ↣⃵䛑ᮄ߯ᓎϔϾ ImageActionˈᑊҹ䖭Ͼ ImageAction এ߯ᓎϔϾ JButtonˈৠᯊ䆒㕂䖭 㡆ᣝ䪂ˈᑊ䆒㕂䖭Ͼ JToolBar ⱘᏗሔᮍᓣǃᷛ乬ǃᰃ৺ৃҹᢪࡼㄝሲᗻˈ᳔ৢএ䘡ग़ֱᄬ䖭ѯ买㡆㉏ ߯ᓎϔϾ JToolBar এᬒ㕂䖭ѯ买ܜ߯ᓎᎹ݋ᷣⱘ䖛⿟㉏Ԑˈ⫼䆒㕂њ买㡆ⱘᣝ䪂এҷ㸼䖭ѯ买㡆ˈ佪 GREENǃLIGHT_GRAYǃMAGENTAǃORANGEǃPINKǃREDǃWHITEǃYELLOW˅ˈ߯ᓎ䖛⿟Ϣ 䖭ᰃϔϾㅔऩⱘ买㡆䗝ᢽ䴶ᵓˈা᳔᳝෎ᴀⱘ޴⾡买㡆䗝ᢽ˄BLACKǃBLUEǃCYANǃGRAYǃ 6.4.5 ߯ᓎ买㡆䗝ᢽ䴶ᵓ ೒ 6.5 ࡴܹ⬏೒Ꮉ݋ᷣ ݋ᷣৢˈ݋ԧⱘᬜᵰབ೒ 6.5 ᠔⼎DŽ ᰃ⫼೼ⲥ਀买㡆䗝ᢽᯊ⫼ࠄˈབᵰ color ϡЎぎˈህᡞᔧࠡⱘ买㡆䆒㕂Ў㹿䗝ᢽⱘ买㡆DŽࡴܹњ⬏೒Ꮉ ⦄㉏ᅲ՟ˈབᵰ䖭Ͼᅲ՟ϡЎぎˈህᡞᅗ䆒㕂Ў⦄೼ℷՓ⫼ⱘᎹ݋DŽ㗠䆒㕂ℷ೼Փ⫼ⱘ买㡆䖭↉ҷⷕˈ 㛑䖛Ꮉ݋ⱘৡᄫ name এ㦋পϔϾ Tool ᥹ষⱘᅲܜҷⷕЁࡴ㉫ⱘഄᮍᰃ䖭Ͼᮍ⊩ⱘЏ㽕Ϯࡵˈ佪 } } colorPanel.setBackground( color ); AbstractTool.color = color ; //䆒㕂ℷ೼Փ⫼ⱘ买㡆 if( color != null ) { //䆒㕂Ꮧሔᮍᓣ panel.setLayout( new FlowLayout( FlowLayout.LEFT ) ); //ᮄᓎϔϾ JToolBar JToolBar toolBar = new JToolBar( "买㡆" ); //䆒㕂Ўϡৃᢪࡼ toolBar.setFloatable( false ); //䆒㕂Ϣ䖍⬠ⱘ䎱⾏ toolBar.setMargin( new Insets( 2, 2, 2, 2) ); //䆒㕂Ꮧሔᮍᓣ toolBar.setLayout( new GridLayout( 2, 6, 2, 2 ) ); //Color ㉏ЁⱘᏆ᳝买㡆 Color[] colorArr = { BLACK, BLUE, CYAN, GRAY, GREEN, LIGHT_GRAY , MAGENTA, ORANGE, PINK, RED, WHITE, YELLOW}; JButton[] panelArr = new JButton[ colorArr.length ]; //ℷ೼Փ⫼ⱘ买㡆 currentColorPanel = new JPanel(); currentColorPanel.setBackground( Color.BLACK ); currentColorPanel.setPreferredSize( new Dimension( 20, 20 ) ); //߯ᓎ䖭ѯ买㡆ⱘ button for( int i = 0 ; i < panelArr.length ; i++ ) { //߯ᓎ JButton panelArr[i] = new JButton( new ImageAction( colorArr[i], currentColorPanel ) ); //䆒㕂 button ⱘ买㡆 panelArr[i].setBackground( colorArr[i] ); //ᡞ button ࡴࠄ toobar Ё toolBar.add( panelArr[i] ); } panel.add( currentColorPanel ); panel.add( toolBar); //䖨ಲ return panel; } ҹϞҷⷕЏ㽕⫼Ѣ߯ᓎ买㡆䗝ᢽ䴶ᵓˈҹϞҷⷕⱘ咥ԧ䚼ߚˈՓ⫼њ JButton ᴹ԰Ўϔ⾡买㡆ⱘ䗝 ᢽᣝ䪂ˈ䖭ѯ买㡆䗝ᢽᣝ䬂Փ⫼њ ImageAction ԰Ўᵘ䗴খ᭄ˈImageAction ೼ 6.4.4 Ё߯ᓎˈ䆹 Action ㉏䋳䋷໘⧚⫼᠋⚍ߏᎹ݋ᷣ៪㗙䗝ᢽ买㡆ᯊⱘ㸠Ўˈ⦄೼ৃҹ䖤㸠⬏೒Ꮉ݋ˈ݋ԧⱘᬜᵰབ೒ 6.6 ᠔⼎DŽ Download at http://www.pin5i.com/ /Download at http://www.pin5i.com ೒ 6.7 Tool 㒻ᡓ݇㋏೒ ㉏Ϣᅗⱘᄤ㉏݇㋏ˈབ೒ 6.7 ᠔⼎DŽ AbstractToolˈ䆹㉏ᅲ⦄њ Tool ᥹ষⱘ᠔᳝ᮍ⊩ˈᑊᅮНњϔѯᮍ⊩կᄤ㉏䞡ݭˈTool ᥹ষǃAbstractTool ࠡ䴶䇈ࠄˈ䖭䞠ⱘᎹ݋㉏䆒䅵ᰃ᳝ϔϾᅮНњᎹ݋᠔᳝ᮍ⊩ⱘ᥹ষ Toolˈ䖭Ͼ᥹ষ᳝ϔϾᅲ⦄㉏ 6.5 Ꮉ݋ᅲ⦄ 䗝ᢽ䴶ᵓϢ⬏೒ऎඳㄝDŽ᥹ϟᴹˈ៥Ӏᇚ䗤ℹᅲ⦄⬏೒䞠䴶ⱘࡳ㛑DŽ ೼ᴀᇣ㡖Ёˈ៥ӀՓ⫼ Swing ߯ᓎњ⬏೒Ꮉ݋ⱘЏ⬠䴶ˈЏ⬠䴶Џ㽕ࣙᣀ㦰ऩǃ⬏೒Ꮉ݋ᷣǃ买㡆 ೒ 6.6 Џ⬠䴶 /Download at http://www.pin5i.com ⱘ⬏ᏗϞDŽAbstractTool Ёⱘ mouseDragged ᮍ⊩ᰃᇍ Tool ᥹ষᅮНⱘ mouseDragged ⱘᅲ⦄DŽ ᮄⱘ೒⠛ˈᑊᡞ䖭ᓴ೒⠛ⱘᑩ㡆䆒㕂Ўⱑ㡆ˈݡᡞॳᴹⱘ೒⠛⫼ Graphics ⱘ drawImage ᮍ⊩⬏ࠄ⦄೼ ҹ哴ᷛᔧࠡⱘ x Ϣ y തᷛ߯ᓎϔᓴܜ䚼ߚ˅ˈ䙷ህ䖯ܹᬍব⬏Ꮧ໻ᇣⱘҷⷕ↉DŽᬍব⬏Ꮧ໻ᇣⱘᮍ⊩ᰃ തᷛˈབᵰ哴ᷛⱘ㉏ൟᰃሲѢᬍব໻ᇣ㉏㉏ൟˈ㗠Ϩ哴ᷛⱘԡ㕂തᷛ x Ϣ y 䛑໻Ѣ 0˄㾕ҷⷕЁⱘࡴ㉫ 㦋পপ哴ᷛᔧࠡⱘ x Ϣ yܜϔѯ߸ᮁˈ佪خ໻ᇣПࠡˈ䳔㽕ᇍ㦋哴ᷛⱘᔧࠡԡ㕂ڣ䖭Ͼᮍ⊩DŽ೼ᬍব೒ 䖭Ͼᮍ⊩⬅ AbstractTool ⱘ mouseDragged ᮍ⊩䇗⫼ˈгህᰃ᳝哴ᷛᢪࡼࡼ԰ᯊˈ㚃ᅮӮ䇗⫼ࠄ } } , frame.getDrawSpace(), x, y ); ImageService.setViewport( frame.getScroll() //䆒㕂 viewport AbstractTool.drawHeight = y; AbstractTool.drawWidth = x; //䆒㕂⬏Ꮧⱘ໻ᇣ getFrame().setBufferedImage( img ); , AbstractTool.drawWidth, AbstractTool.drawHeight, null ); g.drawImage( getFrame().getBufferedImage(), 0, 0 g.setColor( Color.WHITE ); Graphics g = img.getGraphics(); img = new MyImage( x, y, BufferedImage.TYPE_INT_RGB ); ໻ᇣڣ //ᬍব೒ && ( x > 0 && y > 0 ) ) { || cursorType == Cursor.S_RESIZE_CURSOR ) || cursorType == Cursor.W_RESIZE_CURSOR if( ( cursorType == Cursor.NW_RESIZE_CURSOR //བᵰ哴ᷛᣛ䩜ᰃᢪࡼ⢊ᗕ MyImage img = null; ? AbstractTool.drawHeight : e.getY(); int y = cursorType == Cursor.W_RESIZE_CURSOR ? AbstractTool.drawWidth : e.getX(); int x = cursorType == Cursor.S_RESIZE_CURSOR int cursorType = getFrame().getDrawSpace().getCursor().getType(); //㦋প哴ᷛ⦄೼ⱘ x Ϣ y തᷛ getFrame().getBufferedImage().setIsSaved( false ); public void dragBorder( MouseEvent e ) { //ᢪࡼ⬏Ꮧ䖍Ḛ ҷⷕ⏙ऩ˖code\image\src\org\crazyit\image\tool\AbstractTool.java ⱘᮍ⊩DŽ JScrollPaneˈ಴ℸᇍ⬏Ꮧ䖯㸠ᢪࡼˈህৃҹ䆒㕂⬏Ꮧⱘ໻ᇣDŽ೼ AbstractTool ЁᦤկϔϾ dragBorder ᷛᎺ䬂ᯊˈህৃҹ䗮䖛哴ᷛⱘ⿏ࡼᴹᬍব⬏Ꮧⱘ໻ᇣDŽ៥Ӏ೼߯ᓎЏ⬠䴶ⱘᯊ׭ˈህᇚ⬏Ꮧ䆒䅵ЎϔϾ ೼⬏೒Ꮉ݋Ёˈབᵰ⫼᠋ᇚ哴ᷛ⿏ࠄ⬏Ꮧ䖍㓬ⱘᯊ׭ˈ哴ᷛᣛ䩜䳔㽕ব៤ৃᢪࡼⱘ೒ᷛˈᔧᣝϟ哴 6.5.1 ᅲ⦄ᢪࡼ䖍Ḛᬍব⬏Ꮧ໻ᇣ њ㾷᠔᳝Ꮉ݋㉏ⱘ⠊㉏˖AbstractToolDŽܜϾᮍ⊩᥹ষˈ⬅݊ᅲ⦄㉏ᅲ⦄ˈ᥹ϟᴹ Tool ᦤկ mouseDraggedǃmouseMovedǃmouseReleasedǃmousePressedǃmouseClicked Ѩ /Download at http://www.pin5i.com Cursor cursor = getDefaultCursor(); //㦋প咬䅸哴ᷛᣛ䩜 int y = e.getY(); int x = e.getX(); //㦋প哴ᷛ⦄೼ⱘ x Ϣ y തᷛ public void mouseMoved( MouseEvent e) { ҷⷕ⏙ऩ˖code\image\src\org\crazyit\image\tool\AbstractTool.java AbstractTool ᇍ䆹ࡳ㛑ⱘᅲ⦄DŽ ϔϾএ߯ᓎϔϾ Cursorˈᑊ䇗⫼ drawSpace Ёⱘ setCursor 䆒㕂⦄೼Փ⫼ⱘ哴ᷛ㉏ൟˈҹϟᰃ ˄ᕔে䖍ᢪࡼ˅ǃCursor.S_RESIZE_CURSOR˄ᕔϟᢪࡼ˅ˈᬍব哴ᷛᔶ⢊ⱘᮍ⊩ᰃ⫼䖭ϝϾ㉏ൟЁⱘ 哴᳝ᷛϝ⾡ᔶ⢊㉏ൟ˖Cursor.NW_RESIZE_CURSOR˄ᕔেϟᢪࡼ˅ǃCursor.W_RESIZE_CURSOR ⬅Ѣᰃ哴ᷛ⿏ࡼᠻ㸠ⱘᮍ⊩ˈ៥Ӏ䳔㽕೼ᅲ⦄ Tool ᥹ষⱘ mouseMoved ᮍ⊩ⱘᯊ׭ˈᬍব哴ᷛⱘᣛ䩜ˈ ϟᬍব哴ᷛⱘᣛ䩜ᔶ⢊ˈЎ哴ᷛⱘ⿏ࡼࡴܹⲥ਀఼ˈމᔧ哴ᷛ⿏ࡼࠄ⬏Ꮧ䖍㓬ᯊˈ៥Ӏ㽕೼䖭⾡ᚙ 6.5.3 哴ᷛ⿏ࡼᯊᬍবᣛ䩜 Ёˈ៥Ӏ೼ createShape ЁᅮНњ݋ԧ⬏೒ⱘℹ偸ˈԚᰃˈҹϞⱘҷⷕᇚ draw ᮍ⊩⬭㒭њᄤ㉏এᅲ⦄DŽ 䖭Ͼ䆒䅵῵ᓣৃҹՓᄤ㉏ϡᖙএᬍবᭈϾㅫ⊩ⱘ㒧ᵘ㗠䞡ᮄᅮН䆹ㅫ⊩ⱘᶤϾ݋ԧℹ偸DŽ೼ҹϞⱘҷⷕ ῵ᓣˈ῵ᵓᮍ⊩ᰃᅮНᶤϾ᪡԰Ёⱘㅫ⊩㒧ᵘˈ㗠ᇚ䖭Ͼ㒧ᵘЁⱘᶤϔℹ݋ԧℹ偸ᓊ䖳ࠄᄤ㉏Ёࡴ䕑ˈ ೼ᅲ⦄䖭ϾᎹ݋ᮍ⊩ⱘ䖛⿟Ёˈ៥Ӏϡⶹϡ㾝Փ⫼њĀ῵ᵓᮍ⊩˄TemplateMethod˅ā䖭ϔ⾡䆒䅵 ׭ˈህ䳔㽕䇗⫼䖭Ͼ createShape ⱘᎹ݋ᮍ⊩DŽ ህ䳔㽕䇗⫼⬏೒ⱘᮍ⊩ˈᇚ೒⬏ࠄ⬠䴶Ёˈгህᰃ೼䇗⫼ mouseReleased Ϣ mouseDragged ᮍ⊩ⱘᯊ ᳔ৢ䇗⫼ draw ᮍ⊩ⳳℷ⬏೒DŽ៥Ӏ㽕ᯢⱑԩᯊ䇗⫼ createShape ⱘᮍ⊩ˈᔧ哴ᷛᢪࡼ៪㗙ᵒᓔⱘᯊ׭ˈ ℷᡞ೒ᔶ⬏ߎᴹˈাᰃᰒ⼎䖭Ͼ䔼䗍ˈ✊ৢݡᡞ೒⠛ⱘ isSaved ሲᗻ˄ᰃ৺Ꮖ㒣ֱᄬ˅ᬍবЎ falseˈ ⱘॳ಴ᰃ೼哴ᷛᵒᓔПࠡˈᑊ≵᳝ⳳخᇚ೒⠛䞡⬏ˈ䖭ḋܜབᵰ哴ᷛⱘԡ㕂ᰃ೼⬏Ꮧⱘ㣗ೈݙˈ佪 } } draw( g, getPressX(), getPressY(), e.getX(), e.getY() ); //⬏೒ᔶ getFrame().getBufferedImage().setIsSaved( false ); g.setColor( AbstractTool.color ); //䆒㕂买㡆 , AbstractTool.drawWidth, AbstractTool.drawHeight, null ); g.drawImage( getFrame().getBufferedImage(), 0, 0 //ᇚᭈᓴ೒⠛䞡⬏ && e.getY() > 0 && e.getY()< AbstractTool.drawHeight ) { && e.getX() > 0 && e.getX() < AbstractTool.drawWidth if( getPressX() > 0 && getPressY() > 0 //བᵰԡ㕂೼⬏Ꮧݙ private void createShape( MouseEvent e ,Graphics g ) { ҷⷕ⏙ऩ˖code\image\src\org\crazyit\image\tool\AbstractTool.java ᄤ㉏ⱘ draw ᮍ⊩ⱘҷⷕ䞡໡DŽܡᮍᓣ໘⧚ˈ䙓 ᮍ⊩⬅ϔϾ createShape ᮍ⊩এ䇗⫼ˈ೼ createShape ЁˈЏ㽕ᰃᡞ draw ᮍ⊩Ёϔѯ݅ৠ⚍ҹৠḋⱘ ᮍ⊩ˈ݋ԧⱘᅲ⦄⬅݊ᄤ㉏এ䞡ݭˈ಴Ў↣⾡Ꮉ݋⬏ߎⱘ೒ᔶ䛑ϡϔḋˈ᠔ҹ⬅݊ᄤ㉏এ䞡ݭDŽ㗠䖭Ͼ ⬏ߎⶽᔶǃἁ೚ᔶㄝ೒ᔶDŽ೼ AbstractTool ЁˈᅮНњϔϾ draw ᮍ⊩ᅠ៤䖭ѯᎹ԰ˈԚ䖭াᰃϔϾぎ ᔧ哴ᷛ䗝ᢽњᶤ⾡Ꮉ݋ˈ՟བⳈ㒓Ꮉ݋ˈৃҹ೼䗮䖛೼⬏ᵓϞᢪࡼ⬏ߎϔᴵⳈ㒓ˈᔧ✊гৃҹᢪࡼ ᅲ⦄⠊㉏ⱘ⬏೒ᮍ⊩ 6.5.2 /Download at http://www.pin5i.com repaint ᮍ⊩এࠋᮄˈ㾕ҹϟҷⷕDŽ ϸℹᎹ԰ˈϔϾᰃݡ⃵䇗⫼ createShape ᮍ⊩এ⬏೒ˈ঺໪ϔϾᰃ䇗⫼ drawSpace ⱘخᮍ⊩ᯊ䳔㽕 ↣⃵ᵒᓔ哴ᷛᯊˈৃ㛑ህᰃϔϾ⬏೒ࡼ԰ⱘᅠ៤ˈ᠔ҹ AbstractTool ೼ᅲ⦄ Tool ⱘ mouseReleased 6.5.5 䞡㒬೒⠛ } } setPressY( e.getY() ); setPressX( e.getX() ); && e.getY() > 0 && e.getY() < AbstractTool.drawHeight ) { if( e.getX() > 0 && e.getX() < AbstractTool.drawWidth //བᵰԡ㕂೼೒⠛㣗ೈݙˈ䆒㕂ᣝϟⱘതᷛ public void mousePressed(MouseEvent e) { ҷⷕ⏙ऩ˖code\image\src\org\crazyit\image\tool\AbstractTool.java 㕂ˈҹϟᰃ AbstractTool ⱘ mousePressed ᮍ⊩ⱘᅲ⦄DŽ ⹂ഄ⬏ߎᴹˈ᠔ҹ೼ mousePressed ᮍ⊩ЁˈЏ㽕ᰃ䆄ᔩ哴ᷛᣝϟᯊⱘԡޚതᷛԡ㕂ˈᠡ㛑ᡞϔϾ೒ᔶ ೼Փ⫼Ꮉ݋ᯊˈϡㅵᰃ⬏Ⳉ㒓៪㗙ⶽᔶˈᘏᰃ䳔㽕䆄ᔩ哴ᷛᣝϟᯊⱘതᷛԡ㕂ˈࡴϞ哴ᷛᵒᓔᯊⱘ 6.5.4 䆄ᔩ䆄ᔩ哴ᷛᣝϟⱘԡ㕂 } getFrame().getDrawSpace().setCursor( cursor ); //䆒㕂哴ᷛᣛ䩜㉏ൟ } cursor = new Cursor( Cursor.S_RESIZE_CURSOR ); //ᇚ哴ᷛᣛ䩜ᬍবЎϟᢪࡼᔶ⢊ && x < (int)AbstractTool.drawWidth/2 + 4 ) { && x > (int)AbstractTool.drawWidth/2 - 4 && y < AbstractTool.drawHeight + 4 if( y > AbstractTool.drawHeight - 4 //བᵰ哴ᷛᣛ䩜೼ϟЁ } cursor = new Cursor( Cursor.W_RESIZE_CURSOR ); //ᇚ哴ᷛᣛ䩜ᬍবЎেᢪࡼᔶ⢊ && y < (int)AbstractTool.drawHeight/2 + 4 ){ && y > (int)AbstractTool.drawHeight/2 - 4 && x < AbstractTool.drawWidth + 4 if( x > AbstractTool.drawWidth - 4 //བᵰ哴ᷛᣛ䩜೼েЁ } cursor = new Cursor( Cursor.NW_RESIZE_CURSOR ); //ᇚ哴ᷛᣛ䩜ᬍবЎেϟᢪࡼᔶ⢊ && y < AbstractTool.drawHeight + 4 ) { && y > AbstractTool.drawHeight - 4 && x < AbstractTool.drawWidth + 4 if( x > AbstractTool.drawWidth - 4 བᵰ哴ᷛᣛ䩜೼েϟ㾦// /Download at http://www.pin5i.com Փ⫼䪙ヨᎹ݋ᬜᵰབ೒ 6.8 ᠔⼎DŽ ⚍തᷛˈ᳔ৢ䇗⫼ drawSpace ⱘ repaint ᮍ⊩䞡㒬೒⠛DŽ೼ᴀゴЁˈ䪙ヨᎹ݋ᇍᑨⱘᰃ PencilTool ㉏ˈ ⱘॳ಴ᰃᛇ䖒ࠄ䪙ヨⱘᬜᵰˈгህᰃ䇈↣⃵⬏Ⳉ㒓ⱘ䍋⚍തᷛ䛑ᰃϞ⃵ⱘ㒜خ㕂ԡᔧࠡതᷛԡ㕂ˈ䖭ḋ തᷛϢ哴ᷛᔧࠡⱘതᷛԡ㕂Ўখ᭄এ䇗⫼ Graphics ⱘ drawLine ᮍ⊩⬏Ⳉ㒓ˈᑊᡞᣝϟᯊⱘതᷛԡ㕂䆒 ⬏ᏗПЁ˄getPressX() > 0 && getPressY() > 0˅ˈ֓ᡞ g ⱘ color 䆒㕂ЎᔧࠡᎹ݋ⱘ买㡆ˈҹᣝϟᯊⱘ ЁֱᄬࠄϔϾ BufferedImage ⱘᇍ䈵˄BufferImage ⱘᄤ㉏˅ˈ಴ℸৃҹⳈ᥹㦋ᕫDŽབᵰ哴ᷛⱘԡ㕂ᰃ೼ ᰃ⫼ BufferdImage Ёⱘ getGraphics 㦋প೒⠛ⱘ Graphics ᇍ䈵 gˈ⬅Ѣ៥Ӏ೼ ImageFrameܜ佪 } } getFrame().getDrawSpace().repaint(); setPressY( e.getY() ); setPressX( e.getX() ); g.drawLine( getPressX(), getPressY(), e.getX(), e.getY() ); g.setColor( AbstractTool.color ); if( getPressX() > 0 && getPressY() > 0 ) { Graphics g = getFrame().getBufferedImage().getGraphics(); //㦋প೒⠛ⱘ Graphics ᇍ䈵 super.mouseDragged(e); public void mouseDragged( MouseEvent e ) { ҷⷕ⏙ऩ˖code\image\src\org\crazyit\image\tool\PencilTool.java ҹϟҷⷕDŽ ⳟܜˈ䪙ヨᎹ݋ˈᅲ⦄ⱘᮍ⊩Џ㽕ᰃ೼ᢪࡼ哴ᷛⱘᯊ׭ˈ↣⃵䛑ҹ⬏Ⳉ㒓ⱘᔶᓣএ⬏ϟ䪙ヨⱘ䔼䗍 6.5.6 䪙ヨᎹ݋ ᅗⱘᄤ㉏এᅲ⦄ˈ᥹ϟ䴶ˈᇚᅲ⦄৘ϾᎹ݋ⱘࡳ㛑DŽ ࠄℸˈAbstractTool 䛑Ꮖ㒣ᅲ⦄ˈAbstractTool ᅲ⦄њ Tool ᥹ষⱘܼ䚼ᮍ⊩ˈᑊᦤկњϔѯᮍ⊩⬅ } getFrame().getDrawSpace().repaint(); //䞡㒬 setPressY( -1 ); setPressX( -1 ); //ᡞ pressX Ϣ pressY 䆒㕂Ў߱ྟؐ createShape( e, g ); Graphics g = getFrame().getBufferedImage().getGraphics(); //⬏೒ public void mouseReleased( MouseEvent e ) { ҷⷕ⏙ऩ˖code\image\src\org\crazyit\image\tool\AbstractTool.java /Download at http://www.pin5i.com g.drawOval(x, y, Math.abs(x1 - x2), Math.abs(y1 - y2)); // ⬏ἁ೚ int y = y2 > y1 ? y1 : y2; int x = x2 > x1 ? x1 : x2; // 䅵ㅫ䍋⚍ public void draw(Graphics g, int x1, int y1, int x2, int y2) { ἁ೚Ꮉ݋ⱘᅲ⦄བϟDŽҷⷕ⏙ऩ˖code\image\src\org\crazyit\image\tool\RoundTool.java } g.drawRect(x, y, Math.abs(x1 - x2), Math.abs(y1 - y2)); // ⬏ⶽᔶ int y = y2 > y1 ? y1 : y2; int x = x2 > x1 ? x1 : x2; // 䅵ㅫ䍋⚍ public void draw(Graphics g, int x1, int y1, int x2, int y2) { ⶽᔶᎹ݋ⱘᅲ⦄བϟDŽҷⷕ⏙ऩ˖code\image\src\org\crazyit\image\tool\RectTool.java } g.drawLine(x1, y1, x2, y2); public void draw(Graphics g, int x1, int y1, int x2, int y2) { Ⳉ㒓Ꮉ݋ⱘᅲ⦄བϟDŽҷⷕ⏙ऩ˖code\image\src\org\crazyit\image\tool\LineTool.java ˄⬏೒ᔶᮍ⊩˅ᮍ⊩ˈdraw ᮍ⊩᳔ৢ⬅ createShape ᮍ⊩䇗⫼DŽ Grapchis ⱘ drewXXXX ᮍ⊩˄drawLineǃdrawRectǃdrawOvalǃdrawRoundRect˅ˈ䛑䞡ݭ⠊㉏ⱘ draw 䖭াϾ㉏ߚ߿ҷ㸼Ⳉ㒓Ꮉ݋ǃⶽᔶᎹ݋ǃἁ೚Ꮉ݋ǃ೚ⶽᔶᎹ݋ˈᅲ⦄ⱘᮍ⊩ᕜㅔऩˈህᰃ䇗⫼ 6.5.7 Ⳉ㒓Ꮉ݋ǃⶽᔶᎹ݋ǃἁ೚Ꮉ݋੠೚ⶽᔶᎹ݋ ೒ 6.8 Փ⫼䪙ヨᎹ݋⬏೒ /Download at http://www.pin5i.com ೼ঠߏ哴ᷛⱘᯊ׭ˈҹ哴ᷛᔧࠡⱘതᷛϢ㄀ϔϾതᷛ⬏ϔᴵⳈ㒓ˈݡҹ哴ᷛᔧࠡⱘതᷛϢ᳔ৢϔϾതᷛ 哴ᷛᔧࠡⱘതᷛˈᵒᓔ哴ᷛⱘᯊ׭(mouseReleased)ˈህ⫼ϞϾ㒜⚍തᷛϢ哴ᷛᔧࠡⱘതᷛ⬏ϔᴵⳈ㒓ˈ ҷⷕЁᰃ䖭ḋᅲ⦄ⱘˈ↣⃵䛑䆄ᔩϞϔᴵⳈ㒓ⱘ㒜⚍തᷛˈབᵰ≵᳝Ϟϔᴵ㒓ˈ䙷М㒜⚍തᷛህᰃ ⬏ߎ㄀Ѡᴵ䖍ˈབℸ䞡໡ˈ᳔ৢঠߏ哴ᷛ䯁ড়䖭Ͼ໮䖍ᔶDŽڣˈᷛˈ⿏ࡼ哴ᷛࠄϟϾԡ㕂⚍ߏᵒᓔ ᰃᢪࡼ⬏㄀ϔᴵ䖍ˈ✊ৢᵒᓔ哴ܜ໮䖍ᔶⱘ໘⧚Ϣ݊ᅗ೒ᔶᑊϡϔḋˈᅗϡᰃϔ⃵ᗻ⬏ߎᴹⱘˈ佪 6.5.8 ໮䖍ᔶᎹ݋ ೒ 6.9 Ⳉ㒓Ꮉ݋ǃⶽᔶᎹ݋ǃἁ೚ᔶᎹ݋Ϣ೚ⶽᔶᎹ݋ⱘᬜᵰ Ꮉ݋ⱘ݋ԧᬜᵰབ೒ 6.9 ᠔⼎DŽ RoundTool ㉏ˈ೚ⶽᔶᎹ݋ᇍᑨⱘᰃ RoundRectTool ㉏DŽⳈ㒓Ꮉ݋ǃⶽᔶᎹ݋ǃἁ೚ᔶᎹ݋Ϣ೚ⶽᔶ ೼ᴀゴЁˈⳈ㒓Ꮉ݋ᇍᑨⱘᰃ LineTool ㉏ˈⶽᔶᎹ݋ᇍᑨⱘᰃ RectTool ㉏ˈἁ೚ᔶᎹ݋ᇍᑨⱘᰃˈ സ୯Ԥē௜ࠀ໌ԅᆵγᆴน෍໹ԅࢗಳᆵγdޙᄐຬෳēୣ൐ԅ෍໹՛༓ྑܢᅟġ҂ॴ } g.drawRoundRect(x, y, Math.abs(x1 - x2), Math.abs(y1 - y2), 20, 20); // ⬏೚ⶽᔶ int y = y2 > y1 ? y1 : y2; int x = x2 > x1 ? x1 : x2; // 䅵ㅫ䍋⚍ public void draw(Graphics g, int x1, int y1, int x2, int y2) { ೚ⶽᔶᎹ݋ⱘᅲ⦄བϟDŽҷⷕ⏙ऩ˖code\image\src\org\crazyit\image\tool\RoundRectTool.java { /Download at http://www.pin5i.com ೼ᴀゴЁˈ໮䖍ᔶᎹ݋ᇍᑨⱘᰃ PolygonTool ㉏ˈ໮䖍ᔶᎹ݋ⱘ݋ԧᬜᵰབ೒ 6.10 ᠔⼎DŽ } } getFrame().getDrawSpace().repaint(); lastY = -1; lastX = -1; firstY = -1; firstX = -1; setPressY( -1 ); setPressX( -1 ); draw( g, 0, 0, lastX, lastY ); draw( g, 0, 0, firstX, firstY ); , AbstractTool.drawWidth, AbstractTool.drawHeight, null ); g.drawImage( getFrame().getBufferedImage(), 0, 0 g.setColor( AbstractTool.color ); && e.getY() < AbstractTool.drawHeight ) { && e.getX() < AbstractTool.drawWidth && e.getY() > 0 if( e.getClickCount() == 2 && firstX > 0 && e.getX() > 0 Graphics g = getFrame().getBufferedImage().getGraphics(); public void mouseClicked(MouseEvent e) { ҷⷕ⏙ऩ˖code\image\src\org\crazyit\image\tool\PolygonTool.java തᷛϢ᳔ৢϔϾതᷛ⬏ϔᴵⳈ㒓˄lastX, lastY˅ˈ㾕ϟҹҷⷕDŽ ᳔ৢঠߏ哴ᷛⱘᯊ׭ˈህҹᔧࠡ哴ᷛതᷛϢ㄀ϔϾതᷛ˄firstX, firstY˅⬏ϔᴵⳈ㒓ˈݡҹᔧࠡ哴ᷛ ϔϾ哴ᷛԡ㕂ህ䆄ᔩЎᔧࠡ哴ᷛԡ㕂DŽ ؐˈ䙷М䇈ᯢ䖭ᰃ㄀ϔ⃵䇗⫼ℸᮍ⊩˄ᵒᓔ哴ᷛ˅ˈህᡞ䖭⃵⚍ߏᵒᓔⱘԡ㕂䆄ᔩЎ㄀ϔϾԡ㕂DŽ㗠Ϟ ᰃ䇗⫼⠊ᮍ⊩ⱘ mouseReleased এ⬏Ⳉ㒓ˈⳟ㉫ԧԡ㕂ⱘҷⷕˈབᵰ㄀ϔϾതᷛ䖬≵᳝㹿䌟ܜ佪 } lastY = e.getY(); lastX = e.getX(); } firstY = pressY; firstX = pressX; if( firstX == -1 ) { //䆒㕂㄀ϔϾϢ᳔ৢϔϾതᷛ super.mouseReleased( e ); //䇗⫼⠊ᮍ⊩⬏Ⳉ㒓 int pressY = getPressY(); int pressX = getPressX(); public void mouseReleased( MouseEvent e ) { ҷⷕ⏙ऩ˖code\image\src\org\crazyit\image\tool\PolygonTool.java ⳟⳟ mouseReleased ᮍ⊩DŽܜ⬏ϔᴵⳈ㒓ˈ䖒ࠄ䯁ড়ⱘⳂⱘˈ佪 /Download at http://www.pin5i.com } getFrame().getDrawSpace().repaint(); setPressY( e.getY() ); setPressX( e.getX() ); , Math.abs( e.getY() - getPressY() ) + size ); Math.abs( e.getX() - getPressX() ) + size g.fillRect( x - size , y - size , y = e.getY() - getPressY() > 0 ? getPressY() : e.getY(); x = e.getX() - getPressX() > 0 ? getPressX() : e.getX(); g.setColor( AbstractTool.color ); && e.getY() < AbstractTool.drawHeight ) { if( getPressX() > 0 && getPressY() > 0 && e.getX() < AbstractTool.drawWidth int size = 4; //⬏ヨ໻ᇣ int y = 0; int x = 0; Graphics g = getFrame().getBufferedImage().getGraphics(); super.mouseDragged(e); public void mouseDragged( MouseEvent e ) { ҷⷕ⏙ऩ˖code\image\src\org\crazyit\image\tool\BrushTool.java 㾕ҹϟҷⷕ˄ࠋᄤ˅DŽ ᰃࠋᄤⱘ໻ᇣDŽ㗠‵Ⲃ᪺੠ࠋᄤⱘᅲ⦄ᰃϔḋⱘˈাϡ䖛ˈࠋᄤ⫼ⱘᰃ݊ᅗ买㡆ˈ㗠‵Ⲃ᪺⫼ⱘᰃⱑ㡆DŽ ⱘⶽᔶˈⶽᔶⱘ໻ᇣህܙ฿ࠋᄤⱘᬜᵰᅲ⦄˖೼ᢪࡼ哴ᷛⱘᯊ׭ˈᘏᰃҹᔧࠡ哴ᷛⱘԡ㕂⬏᳝买㡆 6.5.9 ࠋᄤϢ‵Ⲃ᪺ ೒ 6.10 ໮䖍ᔶᎹ݋ /Download at http://www.pin5i.com ㉏ˈ஋๼Ꮉ݋ⱘᬜᵰབ೒ 6.11 ᠔⼎DŽ Ўᮄⱘതᷛএ⬏ᇣⶽᔶDŽ೼ᴀゴЁˈ஋๼Ꮉ݋ᇍᑨⱘᰃ AtomizerToolخˈᔧࠡതᷛࡴϞ䖭ϸϾ䱣ᴎ᭄ തᷛ˄x, y˅ⱘ䎱⾏˄x = new Random().nextInt(size)+ 1; y = new Random().nextInt(size) + 1;˅ˈ᳔ৢ བ஋๼Ꮉ݋㽕↣⃵ѻ⫳ 10 Ͼᇣ⚍ˈ䙷Мˈ↣⃵䛑ᰃѻ⫳ϸϾ䱣ᴎ᭄,ҷ㸼Ϣᔧࠡ؛ˈⳟࡴ㉫ⱘҷⷕ } } getFrame().getDrawSpace().repaint(); setPressY( e.getY() ); setPressX( e.getX() ); } g.fillRect( e.getX() + x , e.getY() + y , 1, 1 ); y = new Random().nextInt(size) + 1; x = new Random().nextInt(size)+ 1; for ( int i = 0 ; i < count ; i++ ) { g.setColor( AbstractTool.color ); && e.getY() < AbstractTool.drawHeight ) { && e.getX() < AbstractTool.drawWidth if( getPressX() > 0 && getPressY() > 0 int count = 10; //஋ᵾ⚍᭄ int size = 8; //஋ᵾ໻ᇣ int y = 0; int x = 0; public void draw(MouseEvent e,Graphics g) { ҷⷕ⏙ऩ˖code\image\src\org\crazyit\image\tool\AtomizerTool.java 㾕ҹϟҷⷕDŽ ஋๼ᬜᵰᅲ⦄˖⚍ߏᵒᓔ哴ᷛⱘᯊ׭ˈҹ哴ᷛᔧࠡⱘԡ㕂ˈ೼஋ᵾ໻ᇣ㣗ೈݙ䱣ᴎ⬏ߎ N Ͼᇣⶽᔶˈ 6.5.10 ஋๼Ꮉ݋ ㉏DŽ ˄咥ԧҷⷕᮁᰃ⬏ⶽᔶ˅DŽ೼ᴀゴЁˈࠋᄤᎹ݋ᇍᑨⱘᰃ BrushTool ㉏ˈ‵Ⲃ᪺Ꮉ݋ᇍᑨⱘᰃ EraserTool ೼ᢪࡼ哴ᷛ䖛⿟ЁˈᘏᰃӮ䖲㓁䇗⫼ࠄ䖭Ͼᮍ⊩ˈབᵰ哴ᷛⱘᔧࠡԡ㕂ᰃ೼⬏ᵓЁˈ䙷Мህ⬏ⶽᔶ { /Download at http://www.pin5i.com //䆒㕂ᔧࠡ᭛ӊ། File file = fileChooser.getSelectedFile(); //㦋প䗝ᢽⱘ᭛ӊ == ImageFileChooser.APPROVE_OPTION ) { if( fileChooser.showOpenDialog( frame ) //བᵰᠧᓔϔϾ᭛ӊ save( false, frame ); public void open( ImageFrame frame ) { ҷⷕ⏙ऩ˖code\image\src\org\crazyit\image\ImageService.java পࠄⱘ೒⠛Ϣ䇗⫼ drawSpace ⱘ repaint ᮍ⊩䞡⬏ǃ䆒㕂にষᷛ乬ˈ㾕ҹϟҷⷕDŽ 㦋প䗝ᢽࠄⱘ᭛ӊDŽ✊ৢՓ⫼ ImageIO ⱘ read ᮍ⊩ˈᡞ᭛ӊ䇏পߎᴹˈ᳔ৢᡞᔧࠡⱘ೒⠛᳓ᤶЎᮄ䇏 ໪㾖亢ḐDŽՓ⫼ JfileChooser ⱘ showOpenDialog ᮍ⊩ᔍߎ᭛ӊ䗝ᢽḚˈݡՓ⫼ getSelectedFile ᮍ⊩ JfileChooser ϡձ䌪ᴀഄⱘ GUI ᑇৄˈᅗᰃ㒃 Java ᅲ⦄ˈ೼ϡৠᑇৄ݋᳝ᅠܼⱘ㸠Ўˈᑊ݋᳝Ⳍৠⱘ JFileChooser ⫼Ѣ⫳៤ᠧᓔϔϾ᭛ӊᇍ䆱Ḛˈгᰃ⫼Ѣ⫳៤Āᠧᓔ᭛ӊāǃĀֱᄬ᭛ӊāⱘᇍ䆱Ḛˈ 6.6.1 ᠧᓔ೒⠛᭛ӊ ᭈϾ⬠䴶ⱘࠋᮄǃ㦰ऩㄝϮࡵ䘏䕥䛑ᬒ೼䖭Ͼᮍ⊩Ёᅲ⦄DŽ 䰸এ哴ᷛⱘ⬏೒ࡳ㛑ˈ߱ྟ࣪⬏ᵓǃ೒⠛ⱘᮄᓎᠧᓔϢֱᄬǃ৘⾡䴶ᵓⱘᰒ⼎Ϣ䱤㮣ǃ买㡆ⱘ㓪䕥ǃ 6.6 ImageService㉏ᅲ⦄ ೼᳔ৢϔ㡖ˈᇚӮ䆆㾷བԩᇍ೒ᔶ䖯㸠ֱᄬǃᠧᓔǃᮄᓎㄝⳌ݇᪡԰DŽ ᴀᇣ㡖Ёᅲ⦄⬏೒ⱘ෎ᴀᎹ݋ˈࣙᣀ䪙ヨǃࠋᄤǃ‵Ⲃ᪺ǃ໮䖍ᔶᎹ݋ㄝ⬏೒䕃ӊⱘ෎ᴀᎹ݋ˈ೼ ೒ 6.11 ஋๼Ꮉ݋ /Download at http://www.pin5i.com //㦋পৢ㓔ৡ String fileName = fileChooser.getSelectedFile().getName(); //㦋প᭛ӊৡ File currentDirectory = fileChooser.getCurrentDirectory(); //㦋পᔧࠡ䏃ᕘ == ImageFileChooser.APPROVE_OPTION) { if(fileChooser.showSaveDialog( frame ) //བᵰ䗝ᢽֱᄬ if( b ) { public void save( boolean b, ImageFrame frame ) { ҷⷕ⏙ऩ˖code\image\src\org\crazyit\image\ImageService.java 䆶䯂ᰃ৺ֱᄬˈབᵰ⫼᠋䗝ᢽᰃˈݡҹ true এ䇗⫼䖭Ͼᮍ⊩ֱᄬ᭛ӊˈ㾕ҹϟҷⷕDŽܜˈ؛ᄬˈབᵰЎ ೼䖭䞠䖬᳝঺໪ϔϾ䘏䕥ˈህᰃⳟ save ᮍ⊩Ӵ䖛ᴹⱘ boolean ㉏ൟⱘখ᭄ bˈབᵰЎⳳˈⳈ᥹ֱ ⊩ᡞ᭛ӊݭࠄ⺕ⲬDŽ Ў᭛ӊֱᄬⱘ䏃ᕘDŽ᳔ৢˈՓ⫼ ImageIO ⱘ write ᮍخপᔧࠡⱘ᭛ӊⳂᔩˈҹ䖭Ͼᔧࠡ䏃ᕘࡴϞ᭛ӊৡ ᰃ⫼ JFileChooser ⱘ getCurrentDirectory()ᮍ⊩㦋ܜֱᄬ೒⠛ⱘ᪡԰ϔḋᰃՓ⫼ JFileChooserˈ佪 6.6.2 ֱᄬ೒⠛ } } + " - ⬏೒" ); frame.setTitle( fileChooser.getSelectedFile().getName() //䆒㕂ֱᄬৢⱘにষᷛ乬 , frame.getDrawSpace(), width, height ); ImageService.setViewport( frame.getScroll() //䞡ᮄ䆒㕂 viewport frame.getDrawSpace().repaint(); //repaint frame.setBufferedImage( myImage ); , height, null ); myImage.getGraphics().drawImage( image, 0, 0, width //ᡞ䇏পࠄⱘ೒⠛⬏ࠄ myImage Ϟ䴶 , BufferedImage.TYPE_INT_RGB ); MyImage myImage = new MyImage( width, height //߯ᓎϔϾ MyImage AbstractTool.drawHeight = height; AbstractTool.drawWidth = width; int height = image.getHeight(); int width = image.getWidth(); //ᆑˈ催 } e.printStackTrace(); } catch ( java.io.IOException e ) { image = ImageIO.read( file ); //Ң᭛ӊ䇏প೒⠛ try { BufferedImage image = null; fileChooser.setCurrentDirectory( file ); /Download at http://www.pin5i.com g.setColor( Color.WHITE ); Graphics g = myImage.getGraphics(); , BufferedImage.TYPE_INT_RGB ); MyImage myImage = new MyImage( width, height //߯ᓎϔϾ MyImage AbstractTool.drawHeight = height; AbstractTool.drawWidth = width; int height = (int)getScreenSize().getHeight()/2; int width = (int)getScreenSize().getWidth()/2; //ᆑˈ催 save( false, frame ); public void createGraphics( ImageFrame frame ) { ҷⷕ⏙ऩ˖code\image\src\org\crazyit\image\ImageService.java 㡆ˈᡞᅗ᳓ᤶࠄᔧࠡ࠾䕥ⱘ೒⠛ˈ᳔ৢݡ䆒㕂にষᷛ乬ㄝ݊ᅗϰ㽓ˈ㾕ҹϟҷⷕ˖ 䗝ᢽᰃ৺ֱᄬⱘࡼ԰DŽ✊ৢজ䞡ᮄ߯ᓎϔϾ೒⠛ᇍ䈵ˈ䆒㕂䖭Ͼ೒⠛ⱘ䭓ǃᆑˈ䆒㕂೒⠛ⱘ买㡆Ўⱑ᠋ ⫼خܜˈ⠛䇗⫼ save ᮍ⊩ˈ䆶䯂ᰃ৺ֱᄬᔧࠡℷ೼㓪䕥ⱘ೒ܜӮܜ䇗⫼ᮄᓎ೒⠛ᮍ⊩ⱘᯊ׭ˈ佪 6.6.3 ᮄᓎ೒⠛ } } } save( true, frame ); //ֱᄬ೒⠛ if( checked == option.YES_OPTION ) { //བᵰ䗝ᢽᰃ , "⬏೒", option.YES_NO_OPTION, option.WARNING_MESSAGE); int checked = option.showConfirmDialog( frame, "ֱᄬᬍࡼ?" //ᰒ⼎⹂䅸ֱᄬᇍ䆱Ḛ YES_NO_OPTION JOptionPane option = new JOptionPane(); //ᮄᓎϔϾᇍ䆱Ḛ else if( !frame.getBufferedImage().isSaved() ) { } } frame.getBufferedImage().setIsSaved( true ); //Ꮖֱᄬ frame.setTitle( fileName + "." + suf + " - ⬏೒" ); //䆒㕂ֱᄬৢⱘにষᷛ乬 } ie.printStackTrace(); } catch ( java.io.IOException ie ) { , new File(savePath) ); ImageIO.write( frame.getBufferedImage(), suf //ᇚ೒⠛ݭࠄֱᄬ䏃ᕘ try { + fileName + "." + suf; String savePath = currentDirectory + "\\" //㒘ড়ֱᄬ䏃ᕘ String suf = fileChooser.getSuf(); /Download at http://www.pin5i.com new String[]{".TIF",".TIFF"}, "TIFF (*.TIF;*.TIFF)" ) ); this.addChoosableFileFilter( new MyFileFilter( new String[]{".GIF"}, "GIF (*.GIF)" ) ); this.addChoosableFileFilter( new MyFileFilter( "JPEG (*.JPG;*.JPEG;*.JPE;*.JFIF)") ); new String[]{".JPG",".JPEG",".JPE",".JFIF"}, this.addChoosableFileFilter( new MyFileFilter( new String[]{".BMP"}, "BMP (*.BMP)" ) ); this.addChoosableFileFilter( new MyFileFilter( private void addFilter() { ҷⷕ⏙ऩ˖code\image\src\org\crazyit\image\ImageFileChooser.java ϔѯ䖛Ⓒˈ㾕ҹϟҷⷕDŽخ㉏ ImageFileChooserˈ೼䖭Ͼ㉏䞠䴶ˈᇚᇍ᭛ӊ ϔѯ䖛Ⓒˈ៥Ӏ䖭䞠䗝ᢽњᠽሩ JFileChooser ㉏ˈгህᰃ㒻ᡓᅗDŽ䖭䞠ᅮНњᅗⱘϔϾᄫخ᭛ӊᖙ乏 ⬅Ѣ៥ӀাӮ䗝ᢽ೒⠛㉏ൟⱘ᭛ӊˈгাӮֱᄬЎ೒⠛㉏ൟⱘ᭛ӊˈ᠔ҹᇍ JFileChooser 䗝ᢽⱘ 6.6.5 ᭛ӊ䖛Ⓒ 买㡆DŽ ҷⷕϡӮ㒻㓁ᕔϟ䍄ˈㄝ⫼᠋䗝ᢽњˈህ䖨ಲϔϾ Color ᇍ䈵ˈ✊ৢ៥ӀՓ⫼䖭Ͼᇍ䈵ᬍবᔧࠡᎹ݋ⱘ ㉫ԧⱘഄᮍˈᰃᔍߎϔϾᷛ乬ЎĀ㓪䕥买㡆āˈ咬䅸䗝ᢽњ咥㡆ⱘ买㡆㓪䕥Ḛˈ೼⫼᠋᳾⹂ᅮПࠡˈ } frame.getCurrentColorPanel().setBackground( color ); //䆒㕂Ⳃࠡᰒ⼎ⱘ买㡆 AbstractTool.color = color; //䆒㕂Ꮉ݋ⱘ买㡆 color = color == null ? AbstractTool.color : color; , "㓪䕥买㡆", Color.BLACK ); .showDialog( frame.getColorChooser() Color color = JColorChooser //㦋প买㡆 public void editColor( ImageFrame frame ) { ҷⷕ⏙ऩ˖code\image\src\org\crazyit\image\ImageService.java ࠡՓ⫼ⱘ买㡆䆒㕂Ў䖭Ͼ买㡆ˈ㾕ҹϟҷⷕDŽ ህ㛑ᔍߎϔϾ买㡆㓪䕥ḚˈᑊϨৃҹᕫࠄ⫼᠋䗝ᢽ៪㗙㞾Ꮕ䇗ⱘ买㡆ˈ㦋পࠄ䖭Ͼ买㡆ৢˈ៥Ӏህᡞᔧ Փ⫼ swing ⱘ JColorChooser Փ买㡆㓪䕥఼ⱘᅲ⦄বⱘ䴲ᐌㅔऩˈা㽕Փ⫼ᅗⱘ showDialog ᮍ⊩ˈ 6.6.4 买㡆࠾䕥఼ } frame.setTitle( "᳾ੑৡ - ⬏೒" ); //䆒㕂ֱᄬৢⱘにষᷛ乬 , frame.getDrawSpace(), width, height ); ImageService.setViewport( frame.getScroll() //䞡ᮄ䆒㕂 viewport frame.getDrawSpace().repaint(); //repaint frame.setBufferedImage( myImage ); g.fillRect( 0, 0, width, height ); /Download at http://www.pin5i.com } open( frame ); if ( cmd.equals( "ᠧᓔ(O)" ) ) { } createGraphics( frame ); if( cmd.equals( "ᮄᓎ(N)" ) ) { } setVisible( frame.getColorPanel() ); if( cmd.equals("买᭭Ⲧ(C)") ) { } setVisible( frame.getToolPanel() ); if( cmd.equals("Ꮉ݋ㆅ(T)") ) { } editColor( frame ); if( cmd.equals( "㓪䕥买㡆" ) ) { public void menuDo( ImageFrame frame, String cmd ) { ҷⷕ⏙ऩ˖code\image\src\org\crazyit\image\ImageService.java ҹϟҷⷕDŽ ActionListener ⱘ getActionCommond ᮍ⊩㦋ᕫˈ䖭Ͼؐህᰃ㦰ऩⱘ᭛ᄫ˅ˈ䗝ᢽ䇗⫼ϡৠⱘᮍ⊩ˈ㾕 ᔧ⫼᠋⚍ߏᠧᓔǃֱᄬǃ买㡆ㄝㄝ㦰ऩⱘᯊ׭ˈ⿟ᑣᰃḍ᥂ ActionListener ᕫࠄⱘ cmd˄⫼ 6.6.6 ḍ᥂㦰ऩⱘ⚍ߏ䇗⫼Ⳍᑨⱘᮍ⊩ Нⱘৢ㓔ৡˈབᵰᰃህ䖨ಲ trueˈ৺߭䖨ಲ false; བᵰᰃⳂᔩˈᘏᰃ䖨ಲ trueˈҷ㸼ৃҹᠧᓔˈབᵰᰃ᭛ӊˈህ㽕߸ᮁ᭛ӊⱘৢ㓔ৡᰃ৺៥Ӏ㞾Ꮕᅮ } return f.isDirectory(); //བᵰᰃⳂᔩˈ䖨ಲ true,៪㗙䖨ಲ false } } return true; if ( f.getName().toUpperCase().endsWith( s ) ) { for ( String s : suffarr ) { //བᵰ᭛ӊⱘৢ㓔ৡড়⊩ˈ䖨ಲ true public boolean accept( File f ) { ҷⷕ⏙ऩ˖code\image\src\org\crazyit\image\ImageFileChooser.java ӊ㉏ൟᰃ৺Ⳍヺⱘˈ䇋ⳟҹϟҷⷕDŽ ᰃ ImageFileChooser ⱘϔϾݙ䚼㉏ˈᑊϨ䞡ݭњ FileFilter ⱘ accept ᮍ⊩ˈaccept ᮍ⊩ᰃ⫼ᴹ߸ᮁ᭛ 䖛Ⓒ఼ˈ㗠䖭ᮍ⊩⫼ࠄϔϾ FileFilter ㉏ൟⱘখ᭄ˈ೼䖭䞠ˈ៥Ӏгᠽሩњ FileFilterˈি MyFileFilterˈ ҢҷⷕЁৃҹⳟࠄˈህᰃ䇗⫼ JFileChooser ⱘ addChoosableFileFilter Ў᭛ӊ๲ࡴ৘⾡೒⠛㉏ൟⱘ } "᠔᳝೒ᔶ᭛ӊ") ); ".GIF",".TIF",".TIFF",".PNG",".ICO"}, new String[]{".BMP",".JPG",".JPEG",".JPE",".JFIF", this.addChoosableFileFilter( new MyFileFilter( new String[]{".ICO"}, "ICO (*.ICO)" ) ); this.addChoosableFileFilter( new MyFileFilter( new String[]{".PNG"}, "PNG (*.PNG)" ) ); this.addChoosableFileFilter( new MyFileFilter( /Download at http://www.pin5i.com ᕫ៥Ӏ㓪ݭⱘ⿟ᑣ㗺ড়ᗻ᳈Ԣˈ৥䇏㗙ሩ⦄њ䴶৥ᇍ䈵ⱘ໮ᗕǃ㒻ᡓϢᇕ㺙ⱘ⡍ᗻDŽ ゴЁ䰸њҟ㒡ϔѯ⬏೒ⱘᮍ⊩໪ˈ៥Ӏ䖬Փ⫼њ῵ᵓᮍ⊩䖭Ͼ䆒䅵῵ᓣˈᑊᇚᎹ݋ᢑ䈵៤ϔϾ᥹ষˈՓ ೒ⱘ෎ᴀࡳ㛑ˈࡴ⏅䇏㗙ᇍ䖭ѯ㉏Ϣᮍ⊩ⱘ⧚㾷ˈᄺӮ᳈ད᳈᳝ᡔᎻഄএՓ⫼䖭ѯ swing ࣙЁⱘ㉏DŽᴀ drawXXXX ᮍ⊩ˈՓ⫼ MouseMotionListenerǃMouseListener ㄝⲥ਀఼䖭ѯ᱂䗮ⱘ㉏ˈᅲ⦄њ䖭ѯ⬏ ᴀゴЏ㽕ᰃ䗮䖛⬏೒Ꮉ݋ⱘ෎ᴀᅲ⦄ˈ৥䇏㗙ሩ᳾њՓ⫼ Graphic ㉏ⱘ drawLineǃdrawRect ㄝ 6.7 ᴀゴᇣ㒧 ህᰃᡞ೒⠛䆒㕂ЎᏆֱᄬDŽ ՟བ೼ImageServiceⱘsave()ᮍ⊩䞠䴶ህ᳝䖭ḋⱘ䇁হ˖frame.getBufferedImage().setIsSaved( true )ˈ setIsSave ᮍ⊩ᡞ೒⠛ isSaved 䆒㕂Ў trueDŽབᵰ೼㓪䕥೒⠛ⱘᯊ׭ˈ䖭䞠Ӯݡᡞ isSaved 䆒㕂Ў falseDŽ ᄬ೒⠛ⱘࡼ԰ⱘᯊ׭ˈ䖭Ͼ㉏ᇚ㹿䇗⫼ֱخ᳝ϔϾ isSaved ⱘሲᗻˈᷛᖫ೒⠛ᰃ৺Ꮖ㒣ֱᄬDŽ೼ 䖭Ͼࡳ㛑ⱘᅲ⦄䴲ᐌㅔऩˈ៥Ӏⱘ೒⠛㉏ MyImage˄䖭Ͼ㉏㒻ᡓњ Java ⱘ BufferImage ㉏˅䞠䴶 6.6.7 ߸ᮁ೒⠛ᰃ৺Ꮖ㒣ֱᄬ } } exit( frame ); if( cmd.equals( "䗔ߎ(X)" ) ) { } save( true , frame ); if( cmd.equals( "ֱᄬ(S)" ) ) { ㄀ 6 ゴӓWindows ⬏೒ g29g /Download at http://www.pin5i.com ␌៣⬠䴶བ೒ 7.1 ᠔⼎DŽ␌៣ऎඳЁՓ⫼ⱘᰃ JPanelDŽ 7.3.1 ߯ᓎ␌៣⬠䴶 Ёˈ䖯㸠␌៣ⱘऎඳᰃ GamePanelˈ䆹㉏㒻ᡓѢ JPanelDŽ ໛␌៣⬠䴶ⱘⳌ݇㉏ˈ೼ᴀゴޚњ㾷њ䖲䖲ⳟ␌៣ⱘᅲ⦄ॳ⧚ৢˈ៥Ӏৃҹᓔྟ㓪ݭ␌៣⬠䴶ˈᑊ 7.3 ߯ᓎ␌៣⬠䴶Ϣ␌៣ऎඳ ៣DŽ ᳔ৢˈ៥ӀৃҹЎ␌៣ࡴܹߚ᭄䅵ㅫ੠ᯊ䯈䅵ㅫㄝ݊Ҫࡳ㛑DŽϟ䴶䅽៥Ӏᓔྟ㓪ݭ៥Ӏⱘ䖲䖲ⳟ␌ ᑨⱘؐ䆒㕂៤঺໪ⱘؐˈݡ䞡ᮄ㒬ࠊ␌៣ऎඳˈህৃҹ䖒ࠄ⍜䰸ⱘᬜᵰDŽ ⱘതᷛ⚍੠ᴀ⃵⚍ߏⱘതᷛ⚍ˈᅗӀᢹ᳝ৠϔᓴ೒⠛ᑊϨヺড়ϔᅮⱘ⍜䰸㾘߭ˈ䙷МˈᡞѠ㓈᭄㒘Ёᇍ ݊⃵ˈᔧ⚍ߏ␌៣ऎඳⱘᶤϾ⚍ᯊˈ៥Ӏৃҹᡒࠄ䆹⚍೼Ѡ㓈᭄㒘ЁᇍᑨⱘᶤϾؐDŽབᵰПࠡ⚍ߏ Object[][] array = new Object[ϔ㓈ⱘ᳔໻ؐ][Ѡ㓈ⱘ᳔໻ؐ]; ԰ᰃ␌៣ऎඳЁ y തᷛⱘؐˈᆍ఼ḍ᥂䖭ϾѠ㓈᭄㒘এᵘ䗴␌៣ऎඳDŽབϟҷⷕ߯ᓎϔϾѠ㓈᭄㒘˖ ᰃϔϾᆍ఼ᇍ䈵ˈѠ㓈᭄㒘Ёϔ㓈ⱘؐৃҹⳟ԰ᰃ␌៣ऎඳЁ x തᷛⱘؐˈѠ㓈᭄㒘ЁѠ㓈ⱘؐৃҹⳟ ៥Ӏৃҹᇚ⥽␌៣ऎඳ೼ Java ⿟ᑣЁⳟ԰ᰃϔϾѠ㓈᭄㒘ᇍ䈵ˈ␌៣ऎЁৃҹⳟ԰ˈܜⱘџᚙњDŽ佪 њ㾷њ⿟ᑣᅲ⦄䖭Ͼ␌៣ⱘॳ⧚ˈ䙷Мݭߎ⿟ᑣгህᰃϔӊㅔऩܜᅲ⦄䖲䖲ⳟ␌៣ᑊϡ໡ᴖˈབᵰ 7.2 䖲䖲ⳟ␌៣ॳ⧚ এᓔথϔℒሲѢ㞾Ꮕⱘऩᴎ䖲䖲ⳟDŽ 䆩এᓔথሲѢ㞾Ꮕⱘ䖲䖲ⳟˈ䖭ᰃϔӊकߚᛀᛣⱘџᚙDŽ೼ᴀゴЁˈ៥Ӏᇚ䆺㒚ⱘᬭ໻ᆊབԩՓ⫼ Java ⱘऩᴎ⠜䖲䖲ⳟ␌៣DŽ೼␌៣ⱘ䖛⿟Ёˈৃҹ㗗㰥ϟབԩՓ⫼㞾Ꮕᥠᦵⱘ⿟ᑣএᅲ⦄␌៣ⱘᬜᵰˈᑊᇱ 㾘߭⍜䰸ᅗӀⱘϔϾ␌៣DŽ៥Ӏ᳔ᐌ㾕ⱘᰃ೼ QQ ␌៣໻ख़䞠ⱘ QQ 䖲䖲ⳟˈ㔥㒰Ϟг᳝䆌໮৘ᓣ৘ḋ 䖲䖲ⳟ␌៣ˈᰃᣛ೼ϔϾऎඳݙˈߚᏗⴔ䆌໮ϡৠⱘ೒⠛ˈ䗮䖛⚍ߏϸᓴⳌৠⱘ೒⠛ᑊヺড়ϔᅮⱘ 7.1 䖲䖲ⳟ␌៣ㅔҟ ㄀  ゴऩᴎ䖲䖲ⳟ /Download at http://www.pin5i.com ҔМᰃ Piece ᇍ䈵ⱘᓔྟതᷛ੠㒧ᴳതᷛབ೒ 7.2 ᠔⼎DŽ ⠛ऴ᳝ϔᅮⱘԡ㕂ˈ䙷М៥Ӏ䳔㽕䆄ᔩᅗⱘᓔྟⱘ⚍ⱘ x തᷛ੠ y തᷛˈ䖬᳝㒧ᴳ⚍ⱘ x തᷛ੠ y തᷛˈ ᠔䳔㽕ⱘሲᗻDŽϔϾ Piece ᇍ䈵ेϔϾ೒⠛ᮍഫˈ䙷Мᅗᔧ✊ӮࣙᣀϔϾ೒⠛ᇍ䈵ˈ೒ܙ฿׭ݡᕔ䞠䴶 ḋⱘֵᙃਸ਼˛䖭ᰃ៥Ӏ᠔䳔㽕㗗㰥ⱘˈᔧ✊ˈϡϔᅮϔᓔྟህᡞ᠔᳝ⱘሲᗻ䛑ࡴܹˈ៥Ӏৃҹᛇࠄⱘᯊ ݋ԧⱘᇍ䈵ˈ಴ℸˈ៥ӀᮄᓎϔϾ Piece ㉏ˈ㸼⼎ЎϔϾ೒⠛ᮍഫˈ䖭Ͼ Piece ᇍ䈵䳔㽕ֱᄬϔѯҔМ ߯ᓎϔϾѠ㓈᭄㒘⫼Ѣ㸼⼎ϔϾ␌៣ऎඳⱘ೒⠛ˈ៥Ӏ᠔䳔㽕ⱘ೒⠛ᮍഫৃҹⳟ԰ᰃϔϾܜ៥Ӏ佪 7.3.3 ߯ᓎ೒⠛ᮍഫᇍ䈵 ᑊᠻ㸠᪡԰DŽ ᔧ␌៣Ё೼␌៣ऎඳЁ䗝ᢽњᶤϔ⚍ⱘᯊ׭ˈ៥Ӏህৃҹᅮԡࠄ䆹᭄㒘Ёⱘଃϔⱘؐˈݡএ߸ᮁ䖭Ͼؐ ៣ऎඳЁⱘ೒⠛ⳟ԰ᰃϔϾѠ㓈᭄㒘ˈ᭄㒘ⱘϔ㓈ؐᰃ x തᷛⱘؐˈ᭄㒘ⱘѠ㓈ؐᰃ y തᷛⱘؐˈ䙷Мˈ ᷛⱘ˄0ˈ0˅⚍ˈ␌៣ऎⱘ᳔Ϟ䴶ⱘ䖍ህᰃ x 䕈ˈ␌៣ऎ᳔Ꮊ䴶ⱘ䖍ህᰃ y 䕈DŽ䖭ḋˈ៥Ӏህৃҹᇚ␌ ೼ᴀゴⱘᓔ༈ᦤࠄˈ៥Ӏৃҹᇚ␌៣ऎⳟ԰ᰃϔϾതᷛDŽ␌៣ऎⱘ᳔ᎺϞ㾦ⱘ䙷ϔ⚍ৃҹⳟ԰ᰃത 7.3.2 ␌៣ऎඳᅲ⦄ॳ⧚ ೒ 7.1 ␌៣⬠䴶  /Download at http://www.pin5i.com private int indexY; //䆹ᇍ䈵೼᭄㒘ЁѠ㓈ⱘԡ㕂 private int indexX; //䆹ᇍ䈵೼᭄㒘Ёϔ㓈ⱘԡ㕂 ᇍ䈵ᯊˈᕜ䕏ᯧⱘᅮԡࠄᅗ೼᭄㒘ⱘԡ㕂˖ ঺໪ˈ៥Ӏ䖬㽕⏏ࡴϸϾሲᗻˈ⫼Ѣ䆄ᔩ䖭Ͼ Piece ᇍ䈵೼᭄㒘Ёⱘԡ㕂ᰃҔМˈ֓Ѣ៥Ӏᕫࠄ䆹 Piece[][] pieces = new Piece[length][length]; ៥ӀᅮНњϔϾᮍഫᇍ䈵ˈ䙷М೼ᅮНϔϾѠ㓈᭄ⱘᯊ׭ˈ៥Ӏৃҹ䖭ḋ˖ Ѣᓔྟ⚍ⱘതᷛϢ೒⠛ⱘ໻ᇣDŽއᴳ⚍ⱘ x തᷛ੠ y തᷛপ ⍜䰸DŽ䖭䞠䳔㽕⊼ᛣⱘᰃˈ៥Ӏ䖭䞠ⱘᵘ䗴఼খ᭄ᑊϡ䳔㽕ᦤկ㒧ᴳ⚍ⱘ x തᷛ੠ y തᷛˈ䖭ᰃ಴Ў㒧 ඳЁⱘԡ㕂Ꮖ㒣೎ᅮˈᅗⱘᓔྟതᷛϢ㒧ᴳതᷛ䛑ϡݡᬍবˈ᳈ϡӮএᬍবᅗⱘ೒⠛ˈⳈࠄ䖭Ͼᮍഫ㹿 䆌ᬍবˈ䖭ᰃ಴Ў೼߯ᓎ␌៣ऎඳᯊ䖭Ͼᮍഫ೼␌៣ऎܕ䖭Ͼᇍ䈵ϔᮺᵘ䗴ᅠ៤ৢˈᅗⱘ↣Ͼሲᗻ䛑ϡ ៥ӀЎ↣Ͼሲᗻাᦤկ getter ᮍ⊩ˈᑊϡᦤկ setter ᮍ⊩ˈ៥Ӏৃҹ䗮䖛ᵘ䗴఼ᴹᵘ䗴䖭Ͼᇍ䈵ˈ } //ҹϟⳕ⬹ getter ᮍ⊩ private int endY; //㒧ᴳ⚍ⱘ y തᷛ private int endX; //㒧ᴳ⚍ⱘ x തᷛ private int beginY; //ᓔྟ⚍ⱘ y തᷛ private int beginX; //ᓔྟ⚍ⱘ x തᷛ private BufferedImage image; //ֱᄬ䖭Ͼᮍഫᇍ䈵ⱘ᠔ᇍᑨⱘ೒⠛ public class Piece { ҷⷕ⏙ऩ˖code\linkgame\src\org\crazyit\linkgame\commons\Piece.java ᯢⱑњҔМᰃᓔྟ⚍തᷛϢ㒧ᴳ⚍തᷛПৢˈ៥ӀЎ Piece ᇍ䈵ࡴܹ䖭ѯሲᗻˈҷⷕབϟDŽ ೒ 7.2 Piece ᇍ䈵ⱘᓔྟ⚍Ϣ㒧ᴳ⚍  /Download at http://www.pin5i.com public static List getImages(File folder, String subfix) throws IOException { //⫼Ѣ㦋পᶤϾ᭛ӊ།ϟ䴶ⱘ᠔᳝೒⠛ ҷⷕ⏙ऩ˖code\linkgame\src\org\crazyit\linkgame\utils\ImageUtil.java ⣀ゟᬒࠄϔϾ᭛ӊ།ˈݡՓ⫼⿟ᑣ䇏পᅗӀˈᑊᇕ㺙៤ϔϾ䲚ড়䖨ಲˈᅲ⦄ҷⷕབϟDŽ Փ⫼DŽ⦄೼៥ӀᮄᓎϔϾ䇏প೒⠛ⱘᮍ⊩ˈ䇏পᶤϾ᭛ӊ།ϟ䴶ⱘヺড়ৢ㓔ⱘ೒⠛ˈ៥Ӏᡞᮍഫⱘ೒⠛ ៤䴭ᗕᮍ⊩㒭໪䚼خˈ៥Ӏᖙ乏㽕ᯢ⹂䖭Ͼ㉏ⱘ԰⫼ˈ䇏প៪㗙໘⧚೒⠛ⱘϔѯ݀݅ᮍ⊩ৃҹᬒࠄ䖭䞠 ᥹ϟᴹˈ៥Ӏএ߯ᓎ⫼Ѣ䇏প೒⠛੠໘⧚೒⠛ⱘ ImageUtil Ꮉ݋㉏ˈ೼Ў䖭ϾᎹ݋㉏⏏ࡴᮍ⊩ࠡˈ ႕dڟॕॕࢡԅۦᅟġუ 23 ჆෍ଫԅЩۤࣚΡ༖ྡྷྂēರ࠘ੋۚࢡēྙ׸ ໛њ 23 ᓴݙᆍϡৠⱘ೒⠛⫼Ѣ԰Ў೒⠛ᮍഫDŽޚ໛ད␌៣Ё᠔䳔㽕ⱘ೒⠛ˈᴀ՟Ёޚ៥Ӏᖙ乏ܜ佪 ᭄ⱘ䆱ˈ␌៣ᇚϡ㛑㒧ᴳњDŽ ⠛ˈᖙ᳝঺໪ϔᓴ೒⠛ㄝᕙⴔϢᅗⳌ䖲ˈгህᰃ೼␌៣ऎඳЁӏԩϔ㉏೒⠛ⱘϾ᭄䛑ᰃঠ᭄DŽབᵰЎऩ 㽕ᯢⱑ䖲䖲ⳟⱘ␌៣㾘߭ˈ⚍ߏϸᓴⳌৠⱘ೒⠛ᑊヺড়ϔᅮⱘ㾘߭ᠡ㛑⍜䰸ˈᤶ㿔Пˈ䱣֓⚍ߏϔᓴ೒ ܜѠ㓈᭄㒘ЁDŽ䖭ϔᇣ㡖ˈᇚҟ㒡ᗢḋএ䱣ᴎ䇏প೒⠛ˈᇚ೒⠛ⱘ乎ᑣᠧхㄝDŽ೼䇏প೒⠛Пࠡˈ៥Ӏ 䖲䖲ⳟⱘ␌៣ऎඳЁߚᏗⴔ೒⠛ˈབᵰ㽕߱ྟ࣪␌៣ऎඳˈ៥Ӏᖙ乏ᇚ䖭ѯ೒⠛䇏পˈᬒࠄ៥Ӏⱘ 7.3.5 ೒⠛ⱘ䇏প } Piece[][] pieces = gameService.getPieces(); //Փ⫼ GameService ᴹ㦋প Piece[][]Ѡ㓈᭄㒘 public void paint(Graphics g) { ҷⷕ⏙ऩ˖code\linkgame\src\org\crazyit\linkgame\view\GamePanel.java ⧚Ӯ䖭ѯ䖛⿟DŽབᵰ GamePanel ㉏ⱘ paint ᮍ⊩Ёৃҹ䖭ḋᕫࠄ Piece ᭄㒘DŽ 䗮䖛䖭ѯᮍ⊩এ㦋পѠ㓈᭄㒘ˈ㗠བԩএ߯ᓎˈ䆒㕂䖭ϾѠ㓈㒘᭄䞠䴶ⱘؐˈGamePanel ϡݡ䳔㽕এ ࣪ˈ៥Ӏৃҹᬒࠄ GameService ⱘᅲ⦄㉏Ёএˈ೼ GameService Ёᦤկϔѯ᥹ষᮍ⊩ˈGamePanel ⦄೼ˈ៥Ӏৃҹএׂᬍ GamePanel Ёⱘ paint ᮍ⊩ˈҷ㸼␌៣ऎඳⱘѠ㓈᭄㒘 Piece[][]ⱘ߯ᓎϢব } //ⳕ⬹݊Ҫ䆒ؐⱘҷⷕ public GamePanel(GameService gameService) { private GameService gameService; ҷⷕ⏙ऩ˖code\linkgame\src\org\crazyit\linkgame\view\GamePanel.java ᕫ೼ GamePanel Ёৃҹᣓࠄ GameService ᇍ䈵ˈҷⷕབϟDŽ GameService ⱘሲᗻˈᑊЎᅗⱘᵘ䗴఼Ёࡴܹ䆒㕂 GameService ⱘҷⷕˈ⫼Ѣ䆒㕂 GameServiceˈՓ њDŽ䖭ḋৃҹㅔ࣪㾚೒ⱘҷⷕˈг䙉ᕾњऩϔ㘠䋷ⱘॳ߭DŽ⦄೼៥ӀЎ GamePanel ㉏ࡴܹϔϾ ҹৢ៥Ӏ䳔㽕ᅲ⦄ᶤѯ␌៣䘏䕥ˈৃҹ೼䖭Ͼ᥹ষ䞠䴶ᅮНᮍ⊩ˈᑊ⬅ GameServiceImpl এᅲ⦄ } Piece[][] getPieces(); //ᅮНϔϾ᥹ষᮍ⊩, ⫼Ѣ䖨ಲϔϾѠ㓈᭄㒘 public interface GameService { ҷⷕ⏙ऩ˖code\linkgame\src\org\crazyit\linkgame\service\GameService.java 㾚೒ߚ⾏њDŽ GameServiceImplˈᇚ GameService ᥹ষ䆒㕂ࠄ GamePanel 䖭Ͼ㾚೒㒘ӊЁˈ䖭Ӏህৃҹ䖒ࠄ䘏䕥Ϣ ᮄᓎϔϾ GameService ⱘ᥹ষˈ⫼ѢᅮН␌៣䘏䕥ⱘ᥹ষᮍ⊩ˈݡЎ䖭Ͼ᥹ষᮄ๲ϔϾᅲ⦄㉏ 7.3.4 ߯ᓎ␌៣໘⧚㉏ ႏ୶ᄯdٲᅟġྻ౨०ّ೫໿ēලྂަӾ Piece ճເԅ /Download at http://www.pin5i.com //߯ᓎϔϾ䱣ᴎ᭄⫳៤఼ public static List randomImages(List sourceImages) { //䱣ᴎᠧх sourceImages ҹϟЎ⿟ᑣⱘᅲ⦄DŽҷⷕ⏙ऩ˖code\linkgame\src\org\crazyit\linkgame\utils\ImageUtil.java ‰ 㒧ᵰ˖ 2ˈ1ˈ5ˈ0ˈ4ˈ3 ‰ ⑤˖ 0ˈ1ˈ2ˈ3ˈ4ˈ5 䖭ḋ˖ڣ㋴ˈህདܗⱘ᭄䞣ᰃঠ᭄DŽ⦄೼ˈ៥Ӏ䳔㽕᳝ϔϾᮍ⊩এᠧхϔϾ䲚ড়䞠䴶ⱘ 㽕䱣ᴎᣓ 50 ᓴ೒⠛ेৃˈݡᇚ䖭 50 ᓴ೒⠛Ьҹ 2ˈेᰃ៥Ӏ᠔㽕ⱘ 100 ᓴ೒⠛ˈгৃҹֱ䆕↣⾡೒⠛ 䙷М៥Ӏᇚ㽕䇏প 100 ᓴ೒⠛԰Ўᮍഫˈ⬅Ѣ↣⾡೒⠛ᖙ乏Ўঠ᭄ˈ಴ℸ៥Ӏᇚ 100 䰸ҹ 2ˈгህᰃ䳔 Ⳍ݇໘⧚DŽ՟བˈ៥Ӏⱘ␌៣ऎඳ˄᳝೒⠛ⱘऎඳ˅Ў 10 Ь 10 ໻ᇣˈे῾ 10 Ͼᮍഫˈオ 10 Ͼᮍഫˈ ϔѯخ೒⠛೼␌៣ऎඳЁߎ⦄ⱘ⃵᭄ᖙ乏Ўঠ᭄ˈ␌៣ᠡৃҹ㒧ᴳˈ಴ℸˈ៥Ӏ䳔㽕೼䇏প೒⠛ⱘᯊ׭ 䖭ḋˈ៥ӀህৃҹҢ᭛ӊ།Ё䇏প೒⠛԰Ў៥Ӏ␌៣ⱘᮍഫњˈԚᰃˈࠡ䴶䇈ࠄˈ೼␌៣Ё↣ϔ⾡ } return result; } } return result; //ᔧ⑤೒⠛䲚ড়ⱘ size Ў 0 ᯊˈӮᡯߎ᭄㒘䍞⬠ⱘᓖᐌˈⳈ᥹䖨ಲ㒧ᵰ䲚 } catch (IndexOutOfBoundsException e) { result.add(image); //⏏ࡴࠄ㒧ᵰ䲚Ё BufferedImage image = sourceImages.get(index); //Ң⑤೒⠛䲚ড়Ё㦋প䆹೒⠛ᇍ䈵 int index = random.nextInt(sourceImages.size()); //䱣ᴎ㦋পϔϾ᭄ᄫˈࣙᣀ 0ˈϡࣙᣀ⑤೒⠛䲚ড়ⱘ size try { for (int i = 0; i < size; i++) { List result = new ArrayList(); //߯ᓎ㒧ᵰ䲚ড় Random random = new Random(); //߯ᓎϔϾ䱣ᴎ᭄⫳៤఼ public static List getRandomImages(List sourceImages, int size) { //䱣ᴎҢ sourceImages ⱘ䲚ড়Ё㦋প size ᓴ೒⠛ ҷⷕ⏙ऩ˖code\linkgame\src\org\crazyit\linkgame\utils\ImageUtil.java ഄ䖨ಲ䲚ড়DŽ ᥹ϟᴹˈ៥Ӏݡ㓪ݭϔϾ䱣ᴎ䇏প೒⠛ⱘᮍ⊩ˈ⫼Ѣ೼ϔϾ䲚ড়Ё䱣ᴎ䇏পϔᅮ᭄䞣ⱘ೒⠛ˈৠḋ ႕ē࿙Үൎပֺࣖԅ෍ଫԅЩࣚΡ༖ྡྷᄡdڟಾϰဈҮ ႕ē·ैྙڟᅟġྡྷ͎ᆴนॕॕࢡֺࣖԅ෍ଫēਤ჆෍ଫԅӖ໌ďЩࣚĐ՛Ρ༖ྡྷᄡēუಾҎහԅ } return result; } if (file.getName().endsWith(subfix)) result.add(ImageIO.read(file)); //བᵰ䆹᭛ӊヺড়ᣛᅮⱘ᭛ӊৢ㓔, ߭ࡴࠄ㒧ᵰ䲚Ё for (File file : items) { //ᇍ᭛ӊ߫㸼䖯㸠䘡ग़ List result = new ArrayList(); //߯ᓎ㒧ᵰ䲚ড়ᇍ䈵 File[] items = folder.listFiles(); ҢⳂᷛ᭛ӊ།Ё㦋প᭛ӊ߫㸼// /Download at http://www.pin5i.com for (int i = 0; i < this.pieces.length; i++) { int index = 0; //⏏ࡴϔϾᣓ೒⠛ⱘ㋶ᓩ int imageHeight = playImages.get(0).getHeight(); //ᣓ㄀ϔᓴ೒⠛ⱘ催 int imageWidth = playImages.get(0).getWidth(); //ᣓ㄀ϔᓴ೒⠛ⱘᆑˈ⬅ѢПࠡ㑺ᅮ↣ᓴ೒⠛ⱘ໻ᇣᖙ乏ϔ㟈ˈাᣓϔᓴेৃ List playImages = ImageUtil.getPlayImages(new File("images/pieces"), 10 * 10); //㦋প␌៣೒⠛ˈ᭄䞣Ў Piece ᭄㒘ϔ㓈ⱘ䭓ᑺЬҹѠ㓈ⱘ䭓ᑺ this.pieces = new Piece[10][10]; //߯ᓎϔϾ᳝ 100 Ͼᮍഫⱘ␌៣ऎඳ public void start() { ҷⷕ⏙ऩ˖code\linkgame\src\org\crazyit\linkgame\service\impl\GameServiceImpl.java 䆆߯ᓎ␌៣ऎඳˈGameServiceImpl ҷⷕབϟDŽܜӀৃҹЎᓔྟᣝ䪂߯ᓎ哴ᷛⲥ਀఼DŽᔧ✊ˈ៥Ӏ䖭䞠 ␌៣ऎඳˈ䞡ᮄ䅵ᯊˈ䞡ᮄ䅵ߚㄝˈᔧ⥽ᆊ⚍ߏњᓔྟᯊˈ៥Ӏህ㽕䇗⫼䖭Ͼ start ᮍ⊩ᓔྟ␌៣ˈ៥ ៥Ӏ⦄೼Ў GameService ᥹ষᦤկϔϾ start ᮍ⊩ˈ⫼Ѣᠻ㸠␌៣ᓔྟᯊⱘϔѯࡼ԰ˈ՟བ߱ྟ࣪ Ͼ᭄㒘ⱘ↣Ͼؐⱘব࣪ˈᇚӮᕅડࠄ␌៣ऎඳⱘሩ⦄ˈ಴ℸ䖭ϾѠ㓈᭄㒘ᇍ៥Ӏⱘ␌៣ᇸЎ䞡㽕DŽ ೼⬏␌៣ऎඳПࠡˈ៥Ӏᖙ乏㽕Ў␌៣ऎඳ᭄㒘䖯㸠䌟ؐˈᇚ␌៣ऎඳⳟ԰ϔϾѠ㓈᭄㒘ˈ䙷М䖭 7.3.6 ߯ᓎ␌៣ऎඳ೒⠛᭄㒘 ⾡೒⠛Ўঠ᭄ˈᑊ䱣ᴎᠧх೒⠛ⱘࡳ㛑DŽ GameService 䖭Ͼ䘏䕥㉏䞠䴶ᅲ⦄೒⠛䇏প੠䱣ᴎᠧхⱘࡳ㛑ˈা䳔㽕䇗⫼䖭ϔϾᎹ݋㉏ህৃҹ䖒ࠄৠ ᅠ៤њϔϾᎹ݋㉏⫼Ѣ໘⧚೒⠛ˈ䙷М೼߯ᓎ␌៣ऎඳⱘᯊ׭ˈ៥Ӏৃҹϡ⫼೼ܜ៥Ӏ೼䖭䞠 } return result; } result.add(sourceImages.get(numbers.get(i))); //Ң᭄ᄫ䲚ড়Ё㦋পᏆ㒣㹿ᠧхⱘ㋶ᓩˈ⑤೒⠛䲚ড়㦋প䖭Ͼ㋶ᓩⱘؐ for (int i = 0; i < sourceImages.size(); i++) { //ᇍ⑤೒⠛䲚ড়䖯㸠䘡ग़ List result = new ArrayList(); //߯ᓎϔϾ㒧ᵰ䲚ড় } } i--; continue; //䆹᭄ᄫᏆ㒣ᄬ೼Ѣ䲚ড়Ёˈi - 1 ᠻ㸠ᕾ⦃ } else { numbers.add(temp); //ᄬᬒ᭄ᄫⱘ䲚ড়Ё≵᳝䆹䱣ᴎ᭄ˈ⏏ࡴ䲚ড়Ё if (!numbers.contains(temp)) { //Ўњ⹂᭄ֱᄫ≵᳝䞡໡ˈབᵰ䆹᭄ᄫᏆ㒣೼ᄬᬒ᭄ᄫⱘ䲚ড়Ёˈ䞡ᮄݡ㦋পϔ⃵᭄ᄫ Integer temp = random.nextInt(sourceImages.size()); //䱣ᴎ߯ᓎϔϾ᭄ᄫˈ㣗ೈᰃ 0 ࠄখ᭄ sourceImage ⱘ size, ࣙᣀ 0 ϡࣙᣀ size for (int i = 0; i < sourceImages.size(); i++) { //㦋পϔϾ䲚ড়, 䞠䴶ᰃϔѯ㹿ᠧхⱘ᭄ᄫ List numbers = new ArrayList(); //߯ᓎϔϾᄬᬒ᭄ᄫⱘ䲚ড় Random random = new Random(); /Download at http://www.pin5i.com //䇗⫼ Graphics ㉏ⱘ drawImage ᮍ⊩⬏೒ˈҢ piece ⱘᓔྟതᷛᓔྟ⬏ ҷⷕ⏙ऩ˖code\linkgame\src\org\crazyit\linkgame\view\GamePanel.java ࠄ␌៣ऎඳЁˈ݋ԧⱘҷⷕབϟDŽ 㒘Ā⬏āࠄ GamePanel Ёˈৃҹ೼ᇍ Piece ᭄㒘䖯㸠ᕾ⦃ⱘ䖛⿟Ёˈ䘛ࠄᶤϾ䴲ぎ PieceˈेĀ⬏ā ೼ 7.3.6 ЁᏆ㒣߯ᓎདⱘ␌៣ऎඳ᭄㒘ˈ⦄೼এᅲ⦄ GamePanel ⱘ paint ᮍ⊩ˈ⫼Ѣᇚ␌៣ऎඳ᭄ 7.3.7 ḍ᥂᭄㒘⬏␌៣ऎඳ ࠽ϟⱘህᰃབᵰᇚ䆹᭄㒘Ā⬏āࠄ GamePanel ЁњDŽ བ೒ 7.3ˈህ⏙ἮϞ䴶ⱘҷⷕЎҔМ㽕䖭ḋᅲ⦄њˈ⦄೼ˈ៥Ӏⱘ␌៣ऎඳ᭄㒘Ꮖ㒣߱ྟ࣪དњˈ ೒ 7.3 ᓔྟതᷛϢ㒧ᴳതᷛⱘ䅵ㅫ  ೒ 7.3 ᇍϞҷⷕ԰ߎ㾷ᵤDŽ } } } index++; this.pieces[i][j] = piece; //ᇚ䆹ᇍ䈵ᬒ䖯᭄㒘Ё Piece piece = new Piece(i * imageWidth, j * imageHeight, i, j, playImages.get(index)); //ҹѠ㓈ⱘؐЬҹ೒⠛ⱘ催԰Ў䆹ᇍ䈵ⱘᓔྟ y തᷛ //ᵘ䗴ϔϾ Piece ᇍ䈵ˈҹϔ㓈ⱘؐЬҹ೒⠛ⱘᆑ԰Ў䆹ᇍ䈵ⱘᓔྟ x തᷛ for (int j = 0; j < this.pieces[i].length; j++) { /Download at http://www.pin5i.com protected List createPieces(GameConfiguration config, Piece[][] pieces) { public class SquareBoard extends AbstractBoard { ҷⷕ⏙ऩ˖code\linkgame\src\org\crazyit\linkgame\service\impl\SquareBoard.java ݭϔϾ SquareBoard ㉏এ㒻ᡓ AbstractBoardˈᑊᅲ⦄ᅗⱘ createPieces ᮍ⊩DŽ //ⳕ⬹ҹϟҷⷕ List notNullPieces = createPieces(config, pieces); //䖨ಲ䴲ぎⱘ Piece 䲚ড়, 䆹䲚ড়⬅ᄤ㉏এ߯ᓎ Piece[][] pieces = new Piece[config.getXSize()][config.getYSize()]; //߯ᓎ㒧ᵰ᭄㒘 ҷⷕ⏙ऩ˖code\linkgame\src\org\crazyit\linkgame\service\AbstractBoard.java ᬒ㕂ࠄ䆹ᇍ䈵Ёˈ㒳ϔ⬅䆹ᇍ䈵䋳䋷໘⧚␌៣೒⠛ⱘ߯ᓎDŽҷⷕབϟ˖ ៥ӀᅮНϔϾ AbstractBoard ᇍ䈵ˈЏ㽕⫼Ѣ߯ᓎ␌៣೒⠛ˈ៥ӀৃҹᇚҹϞ߯ᓎ␌៣ऎඳⱘҷⷕ 7.3.8 䱣ᴎ߱ྟ࣪␌៣ ೒ 7.4 ᇚѠ㓈᭄㒘Ā⬏āࠄ␌៣ऎ  30DŽ䞡ᮄ䖤㸠ˈᑊ⚍ߏᓔྟˈᬜᵰ೒ 7.4 ᠔⼎DŽ ⊼ᛣϞ䴶ⱘ㉫ԧᄫˈҷ㸼Ң㄀ϔϾᮍഫᓔྟˈx ੠ y ؐߚ߿ࡴ 30ˈे৥␌៣ऎඳⱘেϟ㾦৘⿏ࡼ Piece piece = new Piece(i * imageWidth + 30, j * imageHeight + 30, i, j, playImages.get(index)); ϔϾᮍഫߎ⦄ⱘ x തᷛ੠ y തᷛ˖ 䖭ḋˈህৃҹ䖯䖭Ͼ GamePanel 䖯㸠㒬ࠊњˈׂᬍ GameServiceImpl ㉏Ёⱘ start ᮍ⊩ˈ䆒㕂㄀ g.drawImage(piece.getImage(), piece.getBeginX(),piece.getBeginY(), null); /Download at http://www.pin5i.com ϡৠⱘ␌៣᭄㒘њˈᬜᵰབ೒ 7.5 ᠔⼎DŽ ᇍ䈵ˈབᵰ䳔㽕䱣ᴎ߯ᓎ䖭ϸϾᅲ՟ˈৃҹ䗮䖛 Random ᴹᅲ⦄DŽ␌៣↣⃵ start ⱘᯊ׭ˈህӮ߯ᓎߎ createPieces ᮍ⊩ЁⳈ᥹ಲ䖨㒧ᵰ䲚ড়ेৃDŽ⦄೼៥Ӏ㓪ݭњ SimpleBoard Ϣ SquareBoard ϸϾ } } } notNullPieces.add(piece); //⏏ࡴࠄ䴲ぎ Piece ᇍ䈵ⱘ䲚ড়Ё Piece piece = new Piece(i, j); ᵘ䗴ϔϾ Piece ᇍ䈵, া䆒㕂᭄㒘Ёⱘԡ㕂Ў i, jˈ݊Ҫؐϡ䆒㕂ܜ// if (i % 2 == 0) {//བᵰ x 㛑㹿 2 ᭈ䰸, ेऩ᭄߫ϡӮ߯ᓎᮍഫ //ࡴܹ߸ᮁ, ヺড়ϔᅮᴵӊᠡএᵘ䗴 Piece ᇍ䈵, ᑊࡴࠄ䲚ড়Ё for (int j = 0; j < pieces[i].length; j++) { for (int i = 0; i < pieces.length; i++) { List notNullPieces = new ArrayList(); ҷⷕ⏙ऩ˖code\linkgame\src\org\crazyit\linkgame\service\impl\SimpleBoard.java ᑊᅲ⦄ createPieces ᮍ⊩ˈҹϟᰃ SimpleBoard ⱘЏԧҷⷕDŽ ेৃDŽབᵰ⦄೼䖬䳔㽕ᵘᓎᮄⱘ␌៣᭄㒘ˈ䙷М៥ӀݡݭϔϾᄤ㉏ SimpleBoard এ㒻ᡓ AbstractBoard 䖭ḋˈབᵰ䖬䳔㽕ᵘ䗴ϡৠⱘ␌៣᭄㒘ˈ៥Ӏৃҹএ㒻ᡓ AbstractBoard ㉏ˈᑊᅲ⦄ createPieces } } return notNullPieces; } } notNullPieces.add(piece); //⏏ࡴࠄ䴲ぎ Piece ᇍ䈵ⱘ䲚ড়Ё Piece piece = new Piece(i, j); ᵘ䗴ϔϾ Piece ᇍ䈵, া䆒㕂᭄㒘Ёⱘԡ㕂Ў i, jˈ݊Ҫؐϡ䆒㕂ܜ// } (++ for (int j = 0; j < pieces[i].length; j for (int i = 0; i < pieces.length; i++) { List notNullPieces = new ArrayList(); /Download at http://www.pin5i.com public Piece findPiece(int mouseX, int mouseY) { ҷⷕ⏙ऩ˖code\linkgame\src\org\crazyit\linkgame\service\impl\GameServiceImpl.java ᮍ⊩ˈḍ᥂哴ᷛⱘ x തᷛ੠ y തᷛ˄े哴ᷛ⚍ߏⱘᶤϾ⚍˅㦋প⚍ߏⱘ䙷Ͼ Piece ᇍ䈵DŽ ⲥ਀఼DŽᔧ哴ᷛ೼␌៣ऎඳ䖯㸠⚍ߏৢˈḍ᥂⚍ߏⱘതᷛᕫࠄ݋ԧⱘᶤϾ Piece ᇍ䈵DŽᮄᓎϔϾ findPiece ೼೒⠛䖲᥹ࠡˈ៥Ӏ䳔㽕䗝ᢽϔᓴ೒⠛ˈᑊᷛ⼎ᅗᏆ㒣㹿䗝ᢽњDŽা䳔㽕೼Ў哴ᷛⱘ⚍ߏࡴܹџӊ 7.4.1 ೒⠛䗝ᢽ Piece ᇍ䈵ˈ៥Ӏ೼⚍ߏ䖲᥹ᯊˈা䳔㽕ᇚ䖭ѯ Piece ᇍ䈵⍜䰸ህৃҹᅲ⦄䖲᥹DŽ ᰃᇚ␌៣ऎඳݙⱘϸϾⳌৠ೒⠛ᣝ✻ϔᅮⱘ㾘߭䖲᥹䍋ᴹᑊ⍜䰸ˈ೼䖭䞠ˈ␌៣ऎඳЁᏆ㒣ֱᄬњ໮Ͼ ೼ 7.3 Ёˈ៥ӀᏆ㒣ৃҹ߯䗴ϔϾ䱣ᴎⱘ᭄㒘ˈ䖭ϔ㡖ˈ៥Ӏᓔྟᅲ⦄䖲᥹ⱘ⿟ᑣDŽ᠔䇧䖲᥹ˈህ 7.4 ᅲ⦄䖲᥹⿟ᑣ ϸϾᄤ㉏ህᰃᅲ⦄⠊㉏ᅮНⱘ㒧ᵘⱘᶤϔϾℹ偸DŽ SquareBoard Ϣ SimpleBoardˈ㒻 ᡓ њ AbstractBoardˈAbstractBoard ЁᅮНњ೒⠛⫳៤ⱘㅫ⊩ˈᅗⱘ ᄤ㉏ৃҹϡᬍবϔϾㅫ⊩ⱘ㒧ᵘेৃ䞡ᅮН䆹ㅫ⊩ⱘᶤѯ⡍ᅮℹ偸˄ᨬ㞾lj䆒䅵῵ᓣNJ˅DŽϞ䴶ⱘ ᵓᮍ⊩DŽ῵ᵓᮍ⊩ⱘὖᗉ˖ᅮНϔϾ᪡԰Ёⱘㅫ⊩ⱘ㒧ᵘˈ㗠ᇚϔѯℹ偸ᓊ䖳ࠄᄤ㉏ЁDŽ῵ᵓᮍ⊩Փᕫ ݊ᅲˈϞ䴶 SquareBoard Ϣ SimpleBoard 㒻ᡓѢ AbstractBoardˈՓ⫼њ݊Ёⱘϔ⾡䆒䅵῵ᓣ˖῵ ೒ 7.5 䱣ᴎ᭄㒘  /Download at http://www.pin5i.com ೒ 7.6 ᮍഫ㹿䗝Ёⱘᬜᵰ  㸠䗝ᢽᬜᵰབ೒ 7.6 ᠔⼎DŽ ᇚ䆒㕂 selectPiece Ў nullˈབᵰϡৃҹⳌ䖲ˈ䙷М selectPiece Ў䗝ᢽⱘ㄀ѠϾDŽ䖤㸠␌៣ᑊᇍ೒⠛䖯 䆌ߎ⦄ϔϾ㹿䗝Ёⱘ Piece ᇍ䈵ˈᔧ䗝ᢽ㄀ѠϾⱘᯊ׭ˈབᵰৃҹⳌ䖲ˈ䙷Мܕᛣⱘᰃ GamePanel া ાϔϾ Pieceˈݡਞ䆝 GamePanel ᔧࠡ䗝ᢽњાϔϾ Pieceˈ䅽 GamePanel এ䞡ᮄ㒬⬏DŽ䖭䞠䳔㽕⊼ ᢽњᶤϔϾ Piece ᇍ䈵ᯊˈ䳔㽕ᷛ䆚ᅗЎᏆ㒣㹿䗝Ёⱘ⢊ᗕˈ䖭ᯊ׭䳔㽕ਞ䆝哴ᷛⲥ਀఼ˈ哴ᷛ䗝ᢽњ 䗮䖛 findPiece ᮍ⊩ˈ៥Ӏৃҹ䗮䖛哴ᷛᅮԡࠄ᭄㒘ЁⱘᶤϾ Piece ᇍ䈵њˈ᥹ⴔˈ䖬㽕໘⧚ᔧ䗝 } return this.pieces[indexX][indexY]; //䖨ಲᴀᇍ䈵Ё␌៣ऎඳ᭄㒘ⱘᶤϾؐ int indexY = getIndex(relativeY, this.board.getCommonImageHeight()); //㦋প relativeY തᷛ೼␌៣ऎඳ᭄㒘ЁⱘѠ㓈ؐ, ㄀ѠϾখ᭄Ў↣ᓴ೒⠛ⱘ催 int indexX = getIndex(relativeX, this.board.getCommonImageWidth()); //㦋প relativeX തᷛ೼␌៣ऎඳ᭄㒘Ёⱘϔ㓈ؐ, ㄀ѠϾখ᭄Ў↣ᓴ೒⠛ⱘᆑ if (relativeX < 0 || relativeY < 0) { return null; } //བᵰ哴ᷛ⚍ߏⱘഄᮍ↨␌៣ऎඳЁ㄀ϔᓴ೒⠛ⱘᓔྟ x തᷛ੠ᓔྟ y തᷛ㽕ᇣ, े≵᳝ᡒࠄᮍഫ int relativeY = mouseY - this.config.getBeginImageY(); int relativeX = mouseX - this.config.getBeginImageX(); এ䖭Ͼؐޣ //GameConfiguration Ё䆒㕂ⱘ beginImageX/beginImageY ؐ, ಴ℸ䖭䞠㽕 //⬅Ѣ៥Ӏ೼߯ᓎ Piece ᇍ䈵ⱘᯊ׭, ᇚ↣Ͼ Piece ⱘᓔྟതᷛࡴњ if (this.board == null) return null; Ѣᰃ೼ᴀ㉏ⱘ board Ёᡒ Piece ᇍ䈵, བᵰ board Ўぎ, े␌៣ऎඳЁ≵᳝ᮍഫ⬅// /Download at http://www.pin5i.com Ͼᵘ䗴఼ⱘ԰⫼DŽ ⫼ϝϾখ᭄ⱘᵘ䗴఼ˈϸϾ⚍П䯈᳝ϸϾ䕀ᡬ⚍ⱘ䆱ˈህ䳔㽕䇗⫼ಯϾখ᭄ⱘᵘ䗴఼DŽ೒ 7.7 㾷ᵤ䖭ϝ ϸ⚍ৃҹⳈ᥹䖲᥹ˈ߭䇗⫼ϸϾখ᭄ⱘᵘ䗴఼ˈϸϾ⚍П䯈䳔㽕᳝ϔϾ䕀ᡬ⚍ᠡৃҹⳌ䖲ⱘ䆱ˈ䳔㽕䇗 䆹㉏াᦤկϝϾᵘ䗴఼੠ϔϾ䖨ಲᄬᬒ䖲᥹⚍䲚ড়ⱘᮍ⊩ˈᵘ䗴఼⫼Ѣ߯䗴䖲᥹⚍ֵᙃᇍ䈵ˈབᵰ } } return points; public List getLinkPoints() { //䖨ಲ䖲᥹䲚ড় private List points = new ArrayList();//߯ᓎϔϾ䲚ড়⫼Ѣֱᄬ䖲᥹⚍ public class LinkInfo { ҷⷕ⏙ऩ˖code\linkgame\src\org\crazyit\linkgame\commons\LinkInfo.java ЎϸϾ⚍ⱘ x തᷛϢ y തᷛᰃ৺ϔ㟈DŽޚPoint ᇍ䈵ᰃ৺ЎৠϔϾ⚍ˈ߸ᮁᷛ Ϟ䴶ᮄᓎϔϾ Point ㉏ˈ⫼Ѣֱᄬ⚍ⱘ x ੠ y തᷛˈ⊼ᛣˈ䆹㉏䞡ݭⱘ equals ᮍ⊩ˈ⫼Ѣ߸ᮁϸϾ } //ϟ䴶ⳕ⬹ setter ੠ getter ᮍ⊩ } return false; } return (p.getX() == this.x && p.getY() == this.y) ? true : false; //ᔧϸϾ Point ᇍ䈵ⱘ x തᷛ੠ y തᷛৠᯊⳌㄝⱘᯊ׭, 㸼⼎ᅗӀᰃৠϔϾ⚍ Point p = (Point)object; //ᇚখ᭄ᔎࠊ䕀៤ Point ᇍ䈵 if (object instanceof Point) { //བᵰ object ᰃ Point ㉏ൟ public boolean equals(Object object) { //䖭䞠䳔㽕䞡ݭ Object ⱘ equals ᮍ⊩, ⫼Ѣ߸ᮁϸϾ⚍ᰃ৺ЎৠϔϾ private int y; //䆄ᔩ䖭Ͼ⚍ᇍ䈵ⱘ y തᷛ private int x; //䆄ᔩ䖭Ͼ⚍ᇍ䈵ⱘ x തᷛ public class Point { ҷⷕ⏙ऩ˖code\linkgame\src\org\crazyit\linkgame\commons\Point.java LinkInfo 䞠᳝ϔϾ䲚ড়ሲᗻˈ⫼Ѣᄬᬒ↣ϔϾ PointDŽ݋ԧҷⷕབϟ˖ ᰃ⬅ᶤѯ䖲᥹⚍ᵘ៤ⱘˈ䖭䞠䳔㽕ϔϾ Point ᇍ䈵ˈ䖲᥹ৃҹⳟ԰ϔϾ LinkInfo ᇍ䈵ˈे㸼⼎䖲᥹ֵᙃDŽ ໛ϔѯᇍ䈵ˈ⫼Ѣᇕ㺙೼䖲᥹䖛⿟ЁӮ⫼ࠄⱘᇍ䈵ˈ՟བˈ䖲᥹ޚ೼ᅲ⦄䖲᥹ࡳ㛑ࠡˈ៥Ӏ䖬䳔㽕 7.4.2 ߯ᓎ䖲᥹ⱘⳌ݇ᇍ䈵 ⼎ПࠡᏆ㒣䗝ᢽњϔϾ Pieceˈህৃҹ߸ᮁᰃ৺ৃҹ⍜䰸DŽ ϔ⃵ˈ䛑䳔㽕ᇍ䲚ড়䖯㸠߸ᮁˈབᵰ䲚ড়ⱘ໻ᇣЎ 0ˈ߭Ⳉ᥹ࡴܹࠄ䲚ড়Ёˈབᵰ䲚ড়໻ᇣЎ 1ˈህ㸼 ࡴܹϔϾ䲚ড়ᴹֱᄬᏆ㒣䗝ᢽⱘ Piece ᇍ䈵ˈᔧ䗝ᢽњϔϾ Piece ᇍ䈵ৢˈᇚ݊ࡴܹࠄ䲚ড়ЁDŽ↣䗝ᢽ ೒ 7.6 Ёৃҹⳟࠄˈ␌៣ऎඳЁ᳔ᎺϞ㾦˄0ˈ0˅ⱘ䙷ϾᮍഫᏆ㒣㹿䗝ЁDŽ៥Ӏ䳔㽕Ў GamePanel /Download at http://www.pin5i.com DŽމѦᤶेৃᅲ⦄DŽ೒ 7.8 䆆㾷њ䖭޴⾡ԡ㕂ⱘᚙ ៥Ӏৃҹ䞡ᮄᠻ㸠 link ᮍ⊩ˈᇚ p1 ੠ p2 ϸϾখ᭄ⱘԡ㕂ˈމᎺϞ㾦੠ p2 ೼ p1 ⱘᎺϟ㾦ਸ਼ˈ䖭⾡ᚙ ⱘেϟ㾦˄p2 ⱘ indexX > p1 ⱘ indexXˈp2 ⱘ indexY > p1 ⱘ indexY˅ˈ䖭䞠ЎҔМϡ䆆 p2 ೼ p1 ⱘ ؐⳌৠ˅ˈp2 ೼ p1 ⱘেϞ㾦˄p2 ⱘ indexX > p1 ⱘ indexXˈp2 ⱘ indexY < p1 ⱘ indexY˅ˈp2 ೼ p1 ೼␌៣ऎඳЁⱘ݇㋏њˈ݋ԧৃߚЎ˖p1 Ϣ p2 ೼ৠϔ㸠˄indexY ؐⳌৠ˅ˈp1 Ϣ p2 ೼ৠϔ߫˄indexX ೼೒ 7.7 Ꮖ㒣ᦣ䗄њˈ䙷М᥹ϟᴹህᰃϸϾ Pieceމ៥Ӏ䳔㽕ᇍ␌៣ⱘ␌៣ऎඳ䖯㸠ߚᵤˈ䖲᥹ⱘᚙ LinkInfo link(Piece p1, Piece p2); //䖲᥹ϸϾ Piece ᇍ䈵, ৃҹ䖲᥹, 䖨ಲ LinkInfo ᇍ䈵 ҷⷕ⏙ऩ˖code\linkgame\src\org\crazyit\linkgame\service\GameService.java ᇍ䈵ϡৃ䖲᥹DŽ ⱘ LinkInfo ᇍ䈵њˈ㸼⼎ৃ৺ḍ᥂ϸϾ Piece ᇍ䈵䖨ಲϔϾ LinkInfo ᇍ䈵ˈབᵰ䖨ಲ nullˈ߭㸼⼎ϸϾ 䅽 GameServiceImpl এᅲ⦄ᅗेৃˈlink ᮍ⊩ⱘখ᭄ህᰃϸϾ Piece ᇍ䈵ˈ䖨ಲؐህᰃ៥ӀϞ䴶᠔ᅮН Ў GameService ᥹ষࡴᮄᮍ⊩ˈ䅽 GameListener 䇗⫼DŽ೼ GameService ᥹ষЁ⏏ࡴ link ᮍ⊩ˈ ⱘ LinkInfo ᇍ䈵䖯㸠໘⧚ˈ䅽 GamePanel ᇍ䈵䖯㸠䞡㒬DŽ DŽ៥Ӏ䖬䳔㽕 GameListener ㉏ḍ᥂ GameService ⱘ link ᮍ⊩䖨ಲމ᳔ৢЎϸϾ䕀ᡬ⚍ⱘᚙˈމⱘᚙ ߸ᮁ㛑ϡ㛑Ⳉ᥹Ⳍ䖲ˈݡ߸ᮁϔϾ䕀ᡬ⚍ܜˈ⦃བ೒ 7.7ˈ៥ϟ䴶ⱘ㓪ⷕህᰃᣝ✻䖭ϝ⾡ᮍᓣᴹᅲ ೒ 7.7 LinkInfo ㉏ⱘᦣ䗄 /Download at http://www.pin5i.com 䗮䘧ˈ৥Ꮊⱘ䗮䘧ˈ৥েⱘ䗮䘧ˈҔМি䗮䘧ਸ਼˛བ೒ 7.9 ᠔⼎DŽ ໛޴ϾᎹ݋ᮍ⊩ˈ೼ᅲ⦄ᯊ៥Ӏ䳔㽕⫼ࠄⱘDŽ៥Ӏ䳔㽕㦋পϔϾ Piece ৥Ϟⱘ䗮䘧ˈ৥ϟⱘޚ⦄೼ ໛㦋প䗮䘧ⱘᎹ݋ᮍ⊩ޚ 7.4.3 ⱘϔѯ݀⫼ᮍ⊩DŽ ᯊӮ⫼ࠄމϟ䴶ህৃҹⴔ᠟ݭϔѯᎹ݋ᮍ⊩ˈ⫼Ѣ໘⧚೼Ϟ䴶䖭޴Ͼᚙˈৢމ⦄೼њ㾷њ䖭޴⾡ᚙ p2 ၽ p1 ԅᆰ౨߸ۤᆰຏ߸ēྙ࡜ಾ p1 ၽ p2 ԅဗ౨߸ۤဗຏ߸ēྙၽ౨ੋԅெࣣᄯd ڴᅟġఢ ೒ 7.8 ϸϾ Piece ᇍ䈵ⱘԡ㕂݇㋏ /Download at http://www.pin5i.com 㓪ݭᅠᎺ䖍䗮䘧ⱘᮍ⊩ৢˈᣝ✻ৠḋⱘ䘧⧚㓪ݭ㦋প݊Ҫᮍ৥䗮䘧ⱘᮍ⊩DŽ } return result; } result.add(new Point(i, p.getY())); if (hasPiece(i, p.getY())) return result; //䘛ࠄ䱰⹡, 㸼⼎䗮䘧Ꮖ㒣ࠄሑ༈, Ⳉ᥹䖨ಲ for (int i = p.getX() - pieceWidth; i >= min; i = i - pieceWidth) { //㦋প৥Ꮊ䗮䘧, ⬅ϔϾ⚍৥Ꮊ䘡ग़, ℹ䭓Ў Piece ೒⠛ⱘᆑ List result = new ArrayList(); private List getLeftChanel(Point p, int min, int pieceWidth) { //㒭ϔϾ Point ᇍ䈵,䖨ಲᅗⱘᎺ䖍䗮䘧 } return (findPiece(x, y) == null) ? false : true private boolean hasPiece(int x, int y) { //߸ᮁ GamePanel Ёⱘ x, y ᑻᷛЁᰃ৺᳝ Piece ᇍ䈵 ҷⷕ⏙ऩ˖code\linkgame\src\org\crazyit\linkgame\service\impl\GameServiceImpl.java ᓘ⏙ἮҔМᰃ䗮䘧ⱘὖᗉҹৢˈ៥Ӏݭ㦋প䗮䘧ⱘ⿟ᑣ˖ ೒ 7.9 䗮䘧ⱘᦣ䗄 /Download at http://www.pin5i.com //㦋প↣ᓴ೒⠛ⱘᆑ੠催 Point p2Point = getPieceCenter(p2); //㦋প p2 ⱘЁᖗ⚍ Point p1Point = getPieceCenter(p1); //㦋প p1 ⱘЁᖗ⚍ if (p2.getIndexX() < p1.getIndexX()) return link(p2, p1); //བᵰ p2 ೼ p1 ⱘᎺ䖍, ߭䳔㽕䞡ᮄᠻ㸠ᴀᮍ⊩, ϸϾখ᭄Ѧᤶ if (!p1.isSameImage(p2)) return null; //བᵰ p1 ⱘ೒⠛Ϣ p2 ⱘ೒⠛ϡⳌৠ, ߭䖨ಲ null if (p1.equals(p2)) return null; //ϸϾ Piece ᰃৠϔϾ, े೼␌៣ऎඳЁ䗝ᢽњৠϔϾ Piece, 䖨ಲ null public LinkInfo link(Piece p1, Piece p2) { //ᅲ⦄᥹ষⱘ link ᮍ⊩ ҷⷕ⏙ऩ˖code\linkgame\src\org\crazyit\linkgame\service\impl\GameServiceImpl.java ᬍ link ᮍ⊩DŽׂˈމ䖭䞠ᰃᣛ≵᳝䕀ᡬ⚍ⱘᚙˈމ㸠˄ৠϔ߫˅ৃҹ䖲ⱘᚙ ᳝њ䖭ϸϾᮍ⊩Пৢˈ៥Ӏৃҹ߸ᮁϸϾ೼ৠϔ㸠˄ৠϔ߫˅ⱘ⚍П䯈ᰃ৺ৃ䖲њDŽϟ䴶ᅲ⦄ৠϔ } return false; } if (hasPiece(p1.getX(), i)) return true; for (int i = p1.getY() + pieceHeight; i < p2.getY(); i = i + pieceHeight) { if (p2.getY() < p1.getY()) return isYBlock(p2, p1, pieceHeight); //བᵰ p2 ೼ p1 ⱘϞ䴶, 䇗ᤶখ᭄ԡ㕂䞡ᮄ䇗⫼ᴀᮍ⊩ private boolean isYBlock(Point p1, Point p2, int pieceHeight) { //߸ᮁϸϾ x ᑻᷛⳌৠⱘ⚍ᇍ䈵П䯈ᰃ৺᳝䱰⹡, ҹ p1 ЎЁᖗ৥ϟ䘡ग़ } return false; } if (hasPiece(i, p1.getY())) return true; //བᵰ᳝䱰⹡ for (int i = p1.getX() + pieceWidth; i < p2.getX(); i = i + pieceWidth) { if (p2.getX() < p1.getX()) return isXBlock(p2, p1, pieceWidth); //བᵰ p2 ೼ p1 Ꮊ䖍, 䇗ᤶখ᭄ԡ㕂䇗⫼ᴀᮍ⊩ private boolean isXBlock(Point p1, Point p2, int pieceWidth) { //߸ᮁϸϾ y ᑻᷛⳌৠⱘ⚍ᇍ䈵П䯈ᰃ৺᳝䱰⹡, ҹ p1 ЎЁᖗ৥ে䘡ग़ ҷⷕ⏙ऩ˖code\linkgame\src\org\crazyit\linkgame\service\impl\GameServiceImpl.java ৠˈ㸼⼎ᅗӀϡৃ䖲ˈⳈ᥹䖨ಲDŽݡএᮄ๲ϸϾᎹ݋ᮍ⊩ˈ⫼Ѣ߸ᮁϔ㸠˄ϔ߫˅ⱘϸϾ⚍ᰃ৺᳝䱰⹡DŽ Ϟ䴶ⱘҷⷕˈབᵰ೼␌៣ऎඳЁ䗝ᢽњৠϔϾ Pieceˈ߭Ⳉ᥹䖨ಲ nullˈབᵰ p1 Ϣ p2 ⱘ೒⠛ϡⳌ } if (!p1.isSameImage(p2)) return null; //བᵰ p1 ⱘ೒⠛Ϣ p2 ⱘ೒⠛ϡⳌৠ, ߭䖨ಲ null if (p1.equals(p2)) return null; //ϸϾ Piece ᰃৠϔϾ, े೼␌៣ऎඳЁ䗝ᢽњৠϔϾ Piece, 䖨ಲ null public LinkInfo link(Piece p1, Piece p2) { //ᅲ⦄᥹ষⱘ link ᮍ⊩ ҷⷕ⏙ऩ˖code\linkgame\src\org\crazyit\linkgame\service\impl\GameServiceImpl.java ᅲ⦄᳔ㅔऩⱘ῾৥䖲᥹˄≵᳝䕀ᡬ⚍˅ˈৃҹᅲ⦄ link ᮍ⊩DŽܜ⦄೼䅽៥Ӏ ᳝䕀ᡬ⚍ⱘ῾৥䖲᥹≴ 7.4.4 int pieceWidth = this.board.getCommonImageWidth(); int pieceHeight = this.board.getCommonImageHeight(); //བᵰϸϾ Piece ೼ৠϔ㸠 if (p1.getIndexY() == p2.getIndexY()) { //ᅗӀ೼ৠϔ㸠ᑊП䯈ৃҹ䖲ˈे≵᳝Ⳉ᥹䱰⹡ if (!isXBlock(p1Point, p2Point, pieceWidth)) return new LinkInfo(p1Point, p2Point); } return null; } ⦄೼ৃҹ⌟䆩ϔϟˈᬜᵰབ೒ 7.10 ᠔⼎DŽ ೒ 7.10 ᅲ⦄῾৥Ⳍ䖲 7.4.5 ≵᳝䕀ᡬ⚍ⱘ㒉৥䖲᥹ ϟ䴶ᅲ⦄㒉৥䖲᥹ˈ੠῾৥䖲᥹ϔḋˈ䇗⫼Ꮉ݋ᮍ⊩ isYBlock ߸ᮁᰃ৺ৃҹ㒉৥䖲᥹ेৃˈЎ link ᮍ⊩ࡴܹབϟҷⷕDŽ ҷⷕ⏙ऩ˖code\linkgame\src\org\crazyit\linkgame\service\impl\GameServiceImpl.java //བᵰϸϾ Piece ೼ৠϔ߫ if (p1Point.getX() == p2Point.getX()) { if (!isYBlock(p1Point, p2Point, pieceHeight)) {//ᅗӀП䯈≵᳝ⳳ᥹䱰⹡, ≵᳝䕀ᡬ⚍ return new LinkInfo(p1Point, p2Point); } Download at http://www.pin5i.com/ } བᵰϸϾ Piece ೼ৠϔ߫ˈᑊϨᅗӀৃҹⳈ᥹㒉৥Ⳍ䖲˄≵᳝䕀ᡬ⚍˅ˈህ䖨ಲ LinkInfoˈᬜᵰབ೒ 7.11 ᠔⼎DŽ ೒ 7.11 ᅲ⦄㒉৥Ⳍ䖲 Ϟ䴶ᅲ⦄њ≵᳝䕀ᡬ⚍ⱘ῾৥䖲᥹੠≵᳝䕀ᡬ⚍ⱘ㒉৥䖲᥹ˈབᵰ೼ৠϔ㸠៪㗙ৠϔ߫ⱘϸϾ Piece ϡ㛑Ⳉ᥹䖲᥹ˈेᅗӀ㚃ᅮӮ᳝ϸϾ䕀ᡬ⚍ˈϟ䴶ᇚӮᅲ⦄DŽ 7.4.6 ϔϾ䕀ᡬ⚍ⱘ䖲᥹ ⦄೼៥Ӏܜⳟಲ೒ 7.8ˈ㄀Ѡ⾡ᚙމˈ㸼⼎᳝ϔϾ䕀ᡬ⚍ˈ៥Ӏ䳔㽕ᡒࠄ䖭Ͼ䕀ᡬ⚍ˈᮄ๲ϔϾ䘡 ग़ϸϾ䗮䘧ˈ㦋পᅗӀѸ⚍ⱘᎹ݋ᮍ⊩DŽ ҷⷕ⏙ऩ˖code\linkgame\src\org\crazyit\linkgame\service\impl\GameServiceImpl.java //䘡ग़ϸϾ䗮䘧, 㦋পᅗӀⱘѸ⚍ private Point getWrapPoint(List p1Chanel, List p2Chanel) { for (int i = 0; i < p1Chanel.size(); i++) { Point temp1 = p1Chanel.get(i); for (int j = 0; j < p2Chanel.size(); j++) { Point temp2 = p2Chanel.get(j); if (temp1.equals(temp2)) {//བᵰϸϾ List Ё᳝ܗ㋴᳝ৠϔϾ, 㸼ᯢ䖭ϸϾ䗮䘧᳝Ѹ⚍ return temp1; //䖭ϾѸ⚍ህᰃ៥Ӏ᠔䳔㽕ⱘ䕀ᡬ⚍ } Download at http://www.pin5i.com/ /Download at http://www.pin5i.com ೒ 7.12 ϔϾ䕀ᡬ⚍ˈp2 ೼ p1 ⱘেϟ㾦 ҹϞⱘҷⷕ㦋ᕫϸϾ Piece П䯈ⱘ䕀ᡬ⚍ˈབ೒ 7.12 ᠔⼎DŽ } return null; } return (linkPoint1 == null) ? linkPoint2 : linkPoint1; Point linkPoint2 = getWrapPoint(point1RightChanel, point2UpChanel); //㦋প p1 ৥ে੠ p2 ৥ϟⱘѸ⚍ Point linkPoint1 = getWrapPoint(point1DownChanel, point2LeftChanel); //㦋প p1 ৥ϟ੠ p2 ৥ᎺⱘѸ⚍ if (isRightDown(point1, point2)) {//point2 ೼ point1 ⱘেϟ㾦 } return (linkPoint1 == null) ? linkPoint2 : linkPoint1; //䖨ಲ݊ЁϔϾѸ⚍, བᵰ≵᳝Ѹ⚍, ߭䖨ಲ null Point linkPoint2 = getWrapPoint(point1UpChanel, point2LeftChanel); //㦋প p1 ৥Ϟ੠ p2 ৥ᎺⱘѸ⚍ Point linkPoint1 = getWrapPoint(point1RightChanel, point2DownChanel); //㦋প p1 ৥ে੠ p2 ৥ϟⱘѸ⚍ if (isRightUp(point1, point2)) {//point2 ೼ point1 ⱘেϞ㾦 List point2UpChanel = getUpChanel(point2, point1.getY(), pieceHeight); List point2LeftChanel = getLeftChanel(point2, point1.getX(), pieceWidth); List point2DownChanel = getDownChanel(point2, point1.getY(), pieceHeight); //㦋প p2 ৥ϟ, ৥Ꮊ, ৥ϟⱘϝϾ䗮䘧 List point1DownChanel = getDownChanel(point1, point2.getY(), pieceHeight); List point1UpChanel = getUpChanel(point1, point2.getY(), pieceHeight); List point1RightChanel = getRightChanel(point1, point2.getX(), pieceWidth); //㦋প p1 ৥ে, ৥Ϟ, ৥ϟⱘϝϾ䗮䘧 private Point getCornerPoint(Point point1, Point point2, int pieceWidth, int pieceHeight) { //㦋পϸϾϡ೼ৠϔ㸠៪㗙ৠϔ߫ⱘᑻᷛ⚍ⱘⳈ㾦䖲᥹⚍, ेা᳝ϔϾ䕀ᡬ⚍ Point˅DŽҷⷕ⏙ऩ˖code\linkgame\src\org\crazyit\linkgame\service\impl\GameServiceImpl.java 䖭ḋৃҹ㦋পϸϾ䗮䘧ⱘѸ⚍ˈݡᮄ๲ϔϾᎹ݋ᮍ⊩ˈ⫼Ѣ㦋পϸϾ Point П䯈ⱘ䕀ᡬ⚍˄ϔϾ } return null; { { /Download at http://www.pin5i.com DŽ䇋ⳟ೒ 7.14ˈ䇈ᯢᅗӀᰃབԩⳌ䖲ⱘDŽމ೼ৠϔ㸠᳝ϸϾ䕀ᡬ⚍ⱘᚙއ㾷ܜˈއ៥Ӏϔ⾡ϔ⾡ᴹ㾷 DŽމ‰ p2 ೼ p1 ⱘেϞ㾦ˈৠḋഄг᳝݁⾡䕀ᡬᚙ DŽމ‰ p2 ೼ p1 ⱘেϟ㾦ˈ䖭䞠ህ᳝݁⾡䕀ᡬᚙ DŽމ‰ ೼ৠϔ߫ˈϡ㛑Ⳉ᥹Ⳍ䖲ˈгᖙ乏᳝ϸϾ䕀ᡬ⚍ˈߚ৥ᎺϢ৥েϸ⾡䖲᥹ᚙ DŽމ‰ ೼ৠϔ㸠ˈϡ㛑Ⳉ᥹Ⳍ䖲ˈህᖙ乏᳝ϸϾ䕀ᡬ⚍ˈߚ৥ϞϢ৥ϟϸ⾡䖲᥹ᚙ 䕗໮ˈৃҹᘏ㒧Ўҹϟ޴⾡˖↨މⳌ䖲ˈϸϾ䕀ᡬ⚍ⱘᚙ 䖭ϔ㡖ˈᇚᅲ⦄ϸϾ Point 䗮䖛ϸϾ䕀ᡬ⚍䖯㸠ˈމϞϔᇣ㡖䆆њϸϾ Point П䯈ϔϾ䕀ᡬ⚍ⱘᚙ 7.4.7 ϸϾ䕀ᡬ⚍ⱘ䖲᥹ } return null; if (cornerPoint != null) return new LinkInfo(p1Point, cornerPoint, p2Point); Point cornerPoint = getCornerPoint(p1Point, p2Point, pieceWidth, pieceHeight); 㦋পϸϾ⚍ⱘⳈ㾦Ⳍ䖲ⱘ⚍, ेা᳝ϔϾ䕀ᡬ⚍// މ //᳝ϔϾ䕀ᡬ⚍ⱘᚙ //ⳕ⬹ࠡ䴶῾৥Ⳉ᥹Ⳍ䖲Ϣ㒉৥Ⳉ᥹Ⳍ䖲ⱘҷⷕ public LinkInfo link(Piece p1, Piece p2) { //ᅲ⦄᥹ষⱘ link ᮍ⊩ ҷⷕ⏙ऩ˖code\linkgame\src\org\crazyit\linkgame\service\impl\GameServiceImpl.java GameServiceImpl ⱘ link ᮍ⊩Ёࡴܹ߸ᮁҷⷕˈࠄ⦄೼ link ᮍ⊩ⱘҷⷕབϟDŽ p2 ೼ p1 ⱘেϞ㾦ˈे㦋প p1 ⱘϞে䗮䘧ˈp2 ⱘ৥ϟ䗮䘧ˈݡ㦋পᅗӀⱘѸ⚍ेৃDŽ೼ ೒ 7.13 ϔϾ䕀ᡬ⚍ˈp2 ೼ p1 ⱘেϞ㾦 བ೒ 7.13 ᠔⼎DŽމѸ⚍ˈᑊ䖨ಲ䆹⚍ेৃDŽᔧ p2 ೼ p1 ⱘেϞ㾦ⱘᚙ ៥Ӏ䳔㽕㦋প p1 ৥েⱘ䗮䘧ˈݡ㦋প p2 ৥Ϟⱘ䗮䘧ˈݡ߸ᮁϸϾ䗮䘧䯈ᰃ৺᳝ˈމ೒ 7.12 ⱘᚙ /Download at http://www.pin5i.com Ϟ䴶ⱘҷⷕ᳝⚍䲒ҹ⧚㾷ˈৃҹⳟ೒ 7.15DŽ } } return new LinkInfo(p1Point, point1, point2, p2Point); Point point2 = turns.get(point1); ㋴ⱘ value ؐ, े㄀ѠϾ䕀ᡬ⚍ܗ //㦋প map Ё㄀ϔϾ Point point1 = (Point)turn; ㋴ⱘ key ؐ, े㄀ϔϾ䕀ᡬ⚍ܗ //㦋প map Ё㄀ϔϾ ㋴ܗ //䘡ग़䆹 map, ᱖ᯊা䖨ಲ map Ё㄀ϔϾ for (Object turn : turns.keySet()) { if (turns.size() != 0) { Map turns = getLinkPoints(p1Point, p2Point, pieceWidth, pieceHeight); //䆹 map ⱘ key ᄬᬒ㄀ϔϾ䕀ᡬ⚍, value ᄬᬒ㄀ѠϾ䕀ᡬ⚍, map ⱘ size 䇈ᯢ᳝໮ᇥϾৃҹ䖲ⱘ䗨ᕘ ҷⷕ⏙ऩ˖code\linkgame\src\org\crazyit\linkgame\service\impl\GameServiceImpl.java ⊩⏏ࡴབϟҷⷕDŽ 䖨ಲ㄀ϔϾ䖲᥹䗨ᕘˈ᳔ৢݡ䅵ㅫ᳔ᇣⱘ䖲᥹ᮍᓣDŽЎ link ᮍܜᯢ䖭ϸϾ Point ᳝໮⾡䖲᥹䗨ᕘˈ៥Ӏ Ͼ MapˈMap ⱘ key Ў㄀ϔϾ䕀ᡬ⚍ˈmap ⱘ value Ў㄀ѠϾ䕀ᡬ⚍ˈབ map ⱘ size ϡℶ 1 ⱘ䆱ˈ䆕 ᵘᓎϔܜ䛑ࡴܹ㒧ᵰЁˈࠄ᳔ৢݡএ䅵ㅫ᳔䖥ⱘ䎱⾏DŽᅲ⦄ᯊ៥Ӏৃҹމᡞ䖭ϸ⾡ᚙܜৃҹⳌ䖲ˈ៥Ӏ 䛑ҷ㸼ᅗӀމ೒ 7.14 ৃҹⳟࠄˈᅗӀ p1 Ϣ p2 Ⳍ䖲ˈৃҹ೼Ϟ䴶䖲ˈгৃҹ೼ϟ䴶䖲ˈ䖭ϸ⾡ᚙ ೒ 7.14 ϸϾ䕀ᡬ⚍ˈp1 Ϣ p2 ೼ৠϔ㸠 /Download at http://www.pin5i.com ҷⷕ⏙ऩ˖code\linkgame\src\org\crazyit\linkgame\service\impl\GameServiceImpl.java ᮁˈ䇈ᯢᅗӀϸϾ Point ೼㒉৥᳝ϸϾ䕀ᡬ⚍ेৃDŽ䖭䞠Ⳉ᥹㒭ߎҷⷕDŽ Ⳉ᥹೼Ꮉ݋ᮍ⊩ getLinkPoints ৢ䴶ࡴϞ߸ˈމ㒉৥ⱘϸϾ䕀ᡬ⚍ⱘᚙˈމϟ䴶ݡএᅲ⦄㄀Ѡ⾡ᚙ ೒ 7.16 ῾৥ϸϾ䕀ᡬ⚍ⱘⳌ䖲 ೒ 7.15 Ёⱘ޴ᴵ咥㒓ˈབᵰ䖭޴ᴵ咥㒓䯈≵᳝䱰⹡ˈ᳔߭㒜Ӯࡴࠄ㒧ᵰⱘ map ЁDŽᬜᵰབ೒ 7.16DŽ ೒ 7.15 ῾৥ϸϾ䕀ᡬ⚍ⱘҷⷕ㾷䞞 if (isInColumn(point1, point2)) {//೼ৠϔ߫ //৥Ꮊ䘡ग़ //ҹ p1 ⱘЁᖗ⚍৥Ꮊ䘡ग़㦋প⚍䲚ড় List p1LeftChanel = getLeftChanel(point1, 0, pieceWidth); //ҹ p2 ⱘЁᖗ⚍৥Ꮊ䘡ग़㦋প⚍䲚ড় p2LeftChanel = getLeftChanel(point2, 0, pieceWidth); Map leftLinkPoints = getYLinkPoints(p1LeftChanel, p2LeftChanel, pieceWidth); //৥ে䘡ग़, ϡᕫ䍙䖛␌៣ऎඳⱘ䖍Ḛ //ҹ p1 ⱘЁᖗ⚍৥ে䘡ग़㦋প⚍䲚ড় p1RightChanel = getRightChanel(point1, widthMax, pieceWidth); //ҹ p2 ⱘЁᖗ⚍৥ে䘡ग़㦋প⚍䲚ড় List p2RightChanel = getRightChanel(point2, widthMax, pieceWidth); Map rightLinkPoints = getYLinkPoints(p1RightChanel, p2RightChanel, pieceWidth); result.putAll(leftLinkPoints); result.putAll(rightLinkPoints); } ⊼ᛣҹϞҷⷕЁⱘ 2 Ͼ Mapˈҷ㸼ⴔ৥ᎺϢ৥েϸ⾡䖲᥹ᚙމDŽᬜᵰབ೒ 7.17 ᠔⼎DŽ ೒ 7.17 ᅲ⦄㒉৥ϸϾ䕀ᡬ⚍䖲᥹ 㒉৥ϸϾ䕀ᡬ⚍੠῾৥ϸϾ䕀ᡬ⚍ϔḋˈ೒ 7.18 䇈ᯢЎҔМ䳔㽕䖭ḋᅲ⦄DŽ Download at http://www.pin5i.com/ /Download at http://www.pin5i.com 1 މ೒ 7.19 ϸϾ䕀ᡬ⚍ p2 ೼ p1 েϟ㾦ⱘᚙ DŽމϸϾ䕀ᡬ⚍ˈ೒ 7.19 㟇೒ 7.24 䆆㾷ϸϾ䕀ᡬ⚍ⱘᚙ Ͼ⚍П䯈≵᳝䱰⹡ⱘ䆱ˈህࡴࠄ㒧ᵰⱘ Map Ёএˈབᵰ᳝䱰⹡ˈᇚϡӮࡴࠄ㒧ᵰⱘ Map ЁDŽϟ䴶ᅲ⦄ ೒ 7.18 Ёⱘ咥㡆オ㒓ϸッˈህᰃ៥Ӏ㒧ᵰ Map Ёֱᄬⱘ key ੠ valueˈབᵰ䖭 key ੠ value 䖭ϸ ೒ 7.18 ҷⷕ䇈ᯢ ೒ 7.20 ϸϾ䕀ᡬ⚍ p2 ೼ p1 েϟ㾦ⱘᚙމ 2 ೒ 7.21 ϸϾ䕀ᡬ⚍ p2 ೼ p1 েϟ㾦ⱘᚙމ 3 ೒ 7.22 ϸϾ䕀ᡬ⚍ p2 ೼ p1 েϟ㾦ⱘᚙމ 4 Download at http://www.pin5i.com/ ೒ 7.23 ϸϾ䕀ᡬ⚍ p2 ೼ p1 েϟ㾦ⱘᚙމ 5 ೒ 7.24 ϸϾ䕀ᡬ⚍ p2 ೼ p1 েϟ㾦ⱘᚙމ 6 ᅲ⦄ⱘҷⷕབϟDŽ ҷⷕ⏙ऩ˖code\linkgame\src\org\crazyit\linkgame\service\impl\GameServiceImpl.java //㦋প point1 ৥ϟ䘡ग़, point2 ৥Ϟ䘡ग़ᯊ῾৥ৃ䖲᥹ⱘ⚍ Map downUpLinkPoints = getXLinkPoints(p1DownChanel, p2UpChanel, pieceWidth); //㦋প point1 ৥ে䘡ग़, point2 ৥Ꮊ䘡ग़ᯊ㒉৥ৃ䖲᥹ⱘ⚍ Map rightLeftLinkPoints = getYLinkPoints(p1RightChanel, p2LeftChanel, pieceHeight); //㦋পҹ p1 ЎЁᖗⱘ৥Ϟ䗮䘧 p1UpChanel = getUpChanel(point1, 0, pieceHeight); //㦋পҹ p2 ЎЁᖗⱘ৥Ϟ䗮䘧 p2UpChanel = getUpChanel(point2, 0, pieceHeight); //㦋প point1 ৥Ϟ䘡ग़, point2 ৥Ϟ䘡ग़ᯊ῾৥ৃ䖲᥹ⱘ⚍ Map upUpLinkPoints = getXLinkPoints(p1UpChanel, p2UpChanel, pieceWidth); //㦋পҹ p1 ЎЁᖗⱘ৥ϟ䗮䘧 p1DownChanel = getDownChanel(point1, heightMax, pieceHeight); //㦋পҹ p2 ЎЁᖗⱘ৥ϟ䗮䘧 p2DownChanel = getDownChanel(point2, heightMax, pieceHeight); //㦋প point1 ৥ϟ䘡ग़, point2 ৥ϟ䘡ग़ᯊ῾৥ৃ䖲᥹ⱘ⚍ Map downDownLinkPoints = getXLinkPoints(p1DownChanel, p2DownChanel, pieceWidth); //㦋পҹ p1 ЎЁᖗⱘ৥Ꮊ䗮䘧 List p1LeftChanel = getLeftChanel(point1, 0, pieceWidth); //㦋পҹ p2 ЎЁᖗⱘ৥Ꮊ䗮䘧 p2LeftChanel = getLeftChanel(point2, 0, pieceWidth); //㦋প point1 ৥Ꮊ䘡ग़, point2 ৥Ꮊ䘡ग़ᯊ㒉৥ৃ䖲᥹ⱘ⚍ Map leftLeftLinkPoints = getYLinkPoints(p1LeftChanel, p2LeftChanel, pieceHeight); //㦋পҹ p1 ЎЁᖗⱘ৥ে䗮䘧 p1RightChanel = getRightChanel(point1, widthMax, pieceWidth); //㦋পҹ p2 ЎЁᖗⱘ৥ে䗮䘧 List p2RightChanel = getRightChanel(point2, widthMax, pieceWidth); //㦋প point1 ৥ে䘡ग़, point2 ৥ে䘡ग़ᯊ㒉৥ৃҹ䖲᥹ⱘ⚍ Map rightRightLinkPoints = getYLinkPoints(p1RightChanel, p2RightChanel, pieceHeight); result.putAll(downUpLinkPoints); Download at http://www.pin5i.com/ /Download at http://www.pin5i.com for (Object turn : turns.keySet()) { if (turns.size() != 0) { Map turns = getLinkPoints(p1Point, p2Point, pieceWidth, pieceHeight); //䆹 map ⱘ key ᄬᬒ㄀ϔϾ䕀ᡬ⚍, value ᄬᬒ㄀ѠϾ䕀ᡬ⚍, map ⱘ size 䇈ᯢ᳝໮ᇥϾৃҹ䖲ⱘᮍᓣ ҷⷕ⏙ऩ˖code\linkgame\src\org\crazyit\linkgame\service\impl\GameServiceImpl.java ᳔ⷁⱘ䎱⾏䖨ಲˈॳᴹⱘҷⷕབϟDŽ ㋴Ёᡒߎܗ㋴ˈ⦄೼ˈ៥Ӏ䳔㽕Ң䖭ѯܗ៥Ӏ೼ࠡ䴶ᅲ⦄ⱘᯊ׭ˈাᰃ䖨ಲ㒧ᵰ map Ёⱘ㄀ϔϾ 7.4.8 ᡒߎ᳔ⷁ䎱⾏ ℹⱘ␌៣៤ᵰDŽ ᅲ⦄䖛⿟䖭䞠ϡݡ䆺㒚ᦣ䗄DŽ᠔᳝ⱘ䖲᥹ᮍᓣᏆ㒣ܼ䚼ᅲ⦄њˈ៥Ӏৃҹএⳟ␌៣ⱘᬜᵰˈԧ偠ϔϟ߱ ˈމ㉏Ԑˈ䛑ᰃ᳝ 6 ⾡ᚙމ੠ p2 ೼ p1 েϟ㾦ⱘᚙމ䖭⾡ᚙˈމϟ䴶ݡᅲ⦄ p2 ೼ p1 েϞ㾦ⱘᚙ ೒ 7.25 ᅲ⦄ p2 ೼ p1 ⱘেϟ㾦ϸϾ䕀ᡬ⚍ⱘ䖲᥹  ᬜᵰབ೒ 7.25 ᠔⼎DŽ Mapē࡜ಾӝζ෍ 7.19 Ӿ෍ 7.24 ԅ 6 ᄵெࣣd ّދᅟġ౨ੋ۱඘ԅ result.putAll(rightRightLinkPoints); result.putAll(leftLeftLinkPoints); result.putAll(downDownLinkPoints); result.putAll(upUpLinkPoints); result.putAll(rightLeftLinkPoints);//ᇚৃҹ䖲᥹ⱘ᠔᳝⚍䛑ᬒࠄ㒧ᵰЁ /Download at http://www.pin5i.com } break; if (i == points.size() - 1) {//ᕾ⦃ࠄ᳔ৢϔϾ for (int i = 0; i < points.size(); i++) { int result = 0; private int countAll(List points) { //䅵ㅫ points Ё᠔᳝⚍ⱘ䎱⾏ᘏ੠ } return result; } } result = info; temp1 = distance - shortDistance; if (distance - shortDistance < temp1) { //བᵰϟϔ⃵ᕾ⦃ⱘؐ↨ temp1 ⱘ䖬ᇣ, ߭⫼ᔧࠡⱘؐ԰Ў temp1 } result = info; temp1 = distance - shortDistance; if (i == 0) { //ᇚᕾ⦃㄀ϔϾⱘᏂ䎱⫼ temp1 ֱᄬ int distance = countAll(info.getLinkPoints()); //䅵ㅫߎ޴Ͼ⚍ⱘᘏ䎱⾏ LinkInfo info = infos.get(i); for (int i = 0; i < infos.size(); i++) { LinkInfo result = null; int temp1 = 0; private LinkInfo getShortcut(List infos, int shortDistance) { //೼ infos Ё㦋প݊ಯϾ⚍᳔ⷁⱘ䙷Ͼ LinkInfo ᇍ䈵 } return getShortcut(infos, shortDistance); } infos.add(new LinkInfo(p1, point1, point2, p2)); Point point2 = turns.get(point1); Point point1 = (Point)info; for (Object info : turns.keySet()) { //䘡ग़㒧ᵰ map, ᇚ䕀ᡬ⚍Ϣ䗝ᢽⱘ⚍ᇕ㺙៤ LinkInfo ᇍ䈵, ᬒࠄ䲚ড়Ё List infos = new ArrayList(); int shortDistance) { private LinkInfo getShortcut(Point p1, Point p2, Map turns, //㦋প p1 ੠ p2 П䯈᳔ⷁⱘ䖲᥹ֵᙃ ҷⷕ⏙ऩ˖code\linkgame\src\org\crazyit\linkgame\service\impl\GameServiceImpl.java ⦄೼ˈ៥Ӏᇍ䖭ѯҷⷕ԰ѯׂᬍˈ䅽ᅗ䖨ಲಯϾ⚍˄ϸϾ䗝ᢽⱘ⚍ˈϸϾ䕀ᡬ⚍˅П䯈᳔ⷁⱘ䎱⾏DŽ } } return new LinkInfo(p1Point, point1, point2, p2Point); Point point2 = turns.get(point1); ㋴ⱘ value ؐ, े㄀ѠϾ䕀ᡬ⚍ܗ //㦋প map Ё㄀ϔϾ Point point1 = (Point)turn; ㋴ⱘ key ؐ, े㄀ϔϾ䕀ᡬ⚍ܗ //㦋প map Ё㄀ϔϾ ㋴ܗ䘡ग़䆹 map, ᱖ᯊা䖨ಲ map Ё㄀ϔϾ// /Download at http://www.pin5i.com if (this.timer != null) { public void mousePressed(MouseEvent e) { ҷⷕ⏙ऩ˖code\linkgame\src\org\crazyit\linkgame\listener\BeginListener.java MouseInputAdapterˈা㽕䞡ݭ⠊㉏ⱘ mousePressed ᮍ⊩ेৃDŽ ᷛⲥ਀఼ˈ៥Ӏৃҹᇚ䖭Ͼⲥ਀఼ऩ⣀԰ЎϔϾ㉏ˈᮄᓎ BeginListener ˈ䆹㉏㒻ᡓѢ ⥽ᆊ⚍ߏњ␌៣ऎඳᯊˈ⿟ᑣЁ䳔㽕䇗⫼ GameService ⱘ start ᮍ⊩ˈ៥Ӏ䳔㽕Ўᓔྟᣝ䪂ࡴܹ哴 7.5.1 ࡴܹ䅵ߚࡳ㛑 7.5 ࡴܹ䅵ߚϢ䅵ᯊࡳ㛑 } this.linkInfo = null; //໘⧚ᅠৢ⏙ぎ linkInfo ᇍ䈵 drawLine(this.linkInfo, g); if (this.linkInfo != null) { //བᵰᔧࠡᇍ䈵Ё᳝ linkInfo ᇍ䈵, े䖲᥹ֵᙃ ೼ paint ᮍ⊩Ё䇗⫼Ϟ䴶ⱘᎹ݋ᮍ⊩˖ } } nextPoint.getX(), nextPoint.getY()); g.drawLine(currentPoint.getX(), currentPoint.getY(), Point nextPoint = points.get(i + 1); Point currentPoint = points.get(i); for (int i = 0; i < points.size() - 1; i++) { List points = linkInfo.getLinkPoints(); private void drawLine(LinkInfo linkInfo, Graphics g) { ҷⷕ⏙ऩ˖code\linkgame\src\org\crazyit\linkgame\view\GamePanel.java Ў LinkInfo Ёⱘ Point ᇍ䈵⬏ϞⳌᑨⱘ䖲᥹㒓ˈЎ LinkInfo ⬏Ϟ䖲᥹㒓DŽ ⦄೼៥Ӏᕫࠄњ LinkInfo ᇍ䈵ˈПࠡᑊ≵᳝ᇍ LinkInfo ᇍ䈵䖯㸠໘⧚ˈ⦄೼ৃҹ೼ GamePanel Ё 7.4.9 ⬏Ϟ䖲᥹㒓 䲚ড়Ёˈݡএ䘡ग़䖭Ͼ䲚ড়ˈ䗝প᳔᥹䖥᳔ⷁ䎱⾏ⱘ䙷ϔϾ LinkInfo 䖨ಲेৃDŽ ᅲ⦄ॳ⧚˖䘡ग़䕀ᡬ⚍ map Ёⱘ᠔᳝䕀ᡬ⚍ˈϢॳᴹ䗝ᢽⱘϸϾ⚍ᵘ៤ϔϾ LinkInfoˈݡࡴܹϔϾ } return xDistance + yDistance; int yDistance = Math.abs(p1.getY() - p2.getY()); int xDistance = Math.abs(p1.getX() - p2.getX()); private int getDistance(Point p1, Point p2) { //㦋পϸϾ LinkPoint П䯈ⱘ᳔ⷁ䎱⾏ } return result; } result += getDistance(point1, point2); Point point2 = points.get(i + 1); Point point1 = points.get(i); /Download at http://www.pin5i.com ᬜᵰ೒ 7.26 ᠔⼎DŽ } return this.grade; this.grade += this.config.getPerGrade(); public long countGrade() { private long grade = 0; //ࡴܹߚ᭄ሲᗻ,߱ྟؐЎ 0 ҷⷕ⏙ऩ˖code\linkgame\src\org\crazyit\linkgame\service\impl\GameServiceImpl.java ЁDŽGameService Ё⏏ࡴϔϾ᥹ষᮍ⊩ˈ䅽 GameServiceImpl এᅲ⦄䆹ᮍ⊩DŽ ࡳ䖲᥹њПৢˈህࡴϞϔᅮⱘߚ᭄ˈᑊ䆒㕂ࠄ GameService Ёˈ᳔ৢᡞ㒧ᵰᰒ⼎ࠄ GamePanel ⱘ JLabel ᅲ⦄᱂䗮ⱘ䅵ߚࡳ㛑↨䕗ㅔऩˈৃҹ೼ GameListener˄GamePanel ⱘ哴ᷛⲥ਀఼˅Ё߸ᮁˈᔧ៤ start ᮍ⊩ेৃDŽ BeginListener Ёⱘ哴ᷛ⚍ߏᮍ⊩䕗Ўㅔऩˈᓔྟᯊা㽕䆒㕂ߚ᭄ǃᯊ䯈ˈᑊ䇗⫼ GameService ⱘ } gamePanel.repaint(); // ᇍ gamePanel 䖯㸠䞡ᮄ㒬ࠊ timer.schedule(task, 0, 1000); this.timeLabel); task = new TimerTask(this.gamePanel, this.config.getGameTime(), // ᓔྟ䖯㸠ӏࡵ gameService.start(); // 䇗⫼ gameService ⱘ start ᮍ⊩ timeLabel.setText(String.valueOf(config.getGameTime())); // ᇚᯊ䯈ব៤ॳᴹ䆒㕂ⱘᯊ䯈(೼ GameConfiguration Ё䆒㕂) pointLabel.setText("0"); // ᇚߚ᭄⏙ 0 gamePanel.setOverImage(null); gamePanel.setSelectPiece(null); this.timer = new Timer(); } this.timer.cancel(); /Download at http://www.pin5i.com private JLabel timeLabel; private long gameTime; private GamePanel gamePanel; private long time; //ᔧࠡ⫼ᥝⱘᯊ׭ public class TimerTask extends java.util.TimerTask { ҷⷕ⏙ऩ˖code\linkgame\src\org\crazyit\linkgame\timer\TimerTask.java 㓪ݭϔϾ TimerTask ㉏ˈ⫼Ѣᅮᯊᠻ㸠ӏࡵDŽܜ ㉏ GameListener 䛑ᖙ乏ৃҹ᥻ࠊᯊ䯈DŽ ℶˈ಴ℸ 7.5.2BeginListener ㉏Ϣ␌៣ऎⱘⲥ਀ذ㒣≵᳝ᮍഫᯊˈे᳔ৢϔ⚍៤ࡳⱘ䖲᥹ৢˈᯊ䯈䅵ㅫ 㚃ᅮᰃ␌៣ᓔྟᯊᯊ䯈ᓔྟ䅵ㅫˈᔧ␌៣ऎЁᏆܜ䖭ᯊ׭ˈ៥Ӏ䳔㽕ⶹ䘧ા䞠䳔㽕ᯊ䯈䅵ㅫⱘˈ佪 if (this.overImage != null) g.drawImage(this.overImage, 0, 0, null); //བᵰ overImage ϡЎぎ, ߭㸼⼎␌៣Ꮖ㒣㚰߽៪㗙༅䋹 ҷⷕ⏙ऩ˖code\linkgame\src\org\crazyit\linkgame\view\GamePanel.java ᑊ೼ GamePanel Ёࡴܹ⬏㚰߽੠༅䋹೒⠛DŽˈޚ៣㚰߽੠༅䋹ⱘᷛ ೼ GamePanel ᇍ䈵Ё⏏ࡴϔϾ overImage ⱘሲᗻˈ⫼Ѣֱᄬ␌៣㚰߽៪㗙༅䋹ᯊ೒⠛ˈᑊҹℸ԰Ў␌ Ў៥Ӏⱘ␌៣ࡴܹ㚰߽੠༅䋹ⱘ߸ᮁˈ㚰߽੠༅䋹ⱘᦤ⼎ˈᰃ೼ GamePanel Ёᦤ⼎ⱘˈ಴ℸˈܜ ᳝೒⠛ᮍഫⱘ䆱ˈ␌៣ህ༅䋹DŽ ᭄῵ᓣˈᔧᯊ䯈ࠄњ 0 ৢˈ೼␌៣ऎݙབᵰ䖬צ⫼⦄೼ᅲ⦄䖲䖲ⳟⱘ䅵ᯊࡳ㛑ˈᴀ՟ⱘ䅵ᯊࡳ㛑䞛 7.5.2 ࡴܹ䅵ᯊࡳ㛑Ϣ␌៣ⱘ㚰߽ǃ༅䋹 ೒ 7.26 ࡴܹ䅵ߚࡳ㛑 /Download at http://www.pin5i.com དњˈ䅵ᯊࡳ㛑Ꮖ㒣ᅲ⦄њˈ⦄೼ৃҹএⳟϟ␌៣ᬜᵰབ೒ 7.27 ᠔⼎DŽ if (gamePanel.getOverImage() != null) return; ⱘ mousePressed ᮍ⊩᳔ࠡ䴶ࡴܹˈᔧ␌៣㚰߽៪㗙ˈএ⚍ߏ␌៣ऎˈህ偀Ϟ䖨ಲˈϡݡ԰ӏԩ߸ᮁ˖ ℶ䅵ᯊ఼ˈᑊ䆒㕂␌៣Ў㚰߽⢊ᗕˈᑊ೼ GameListenerذ䖭ḋˈᔧ៤ࡳ⚍ߏ䖲᥹᳔ৢϸϾᮍഫৢˈህ } return false; } } if (pieces[i][j] != null) return true; for (int j = 0; j < pieces[i].length; j++) { for (int i = 0; i < pieces.length; i++) { public boolean hasPieces(Piece[][] pieces) { //ᅲ⦄᥹ষⱘ hasPieces ᮍ⊩ ҷⷕ⏙ऩ˖code\linkgame\src\org\crazyit\linkgame\service\impl\GameServiceImpl.java ܹབϟ hasPieces ᮍ⊩ˈ⫼Ѣ߸ᮁ⬠䴶Ёᰃ৺䖬ᄬ೼೒⠛DŽ ߸ᮁ䆹᭄㒘Ё䈵ᰃⱘ↣ϔϾ Piece ᰃ৺Ўぎˈབᵰ䛑Ўぎˈ߭␌៣㚰߽ˈ೼ GameServiceImpl ㉏Ёࡴ ೼ GameListener Ёབᵰ៤ࡳ䖲᥹њϸϾ Piece ᇍ䈵ৢˈৃҹ߸ᮁ␌៣ऎᰃ৺䖬᳝ᮍഫⱘᄬ೼ˈे ℶDŽذ␌៣᠔࠽ⱘᯊ䯈ˈᔧ␌៣᠔࠽ⱘᯊ䯈Ў 0 ᯊˈ␌៣༅䋹ˈ䅵ᯊ఼ এ time ሲᗻⱘؐˈህᰃޣ ᮍ⊩Ёࡴϔˈ㸼⼎ run ↣ᠻ㸠ϔ⃵ˈtime ⱘؐࡴϔˈݡ⫼খ᭄ⱘ gameTime DŽ঺໪ݡЎ TimerTask ㉏⏏ࡴϔϾ⫼ᥝⱘᯊ䯈ሲᗻˈ⫼Ѣ䆄ᔩ␌៣Ꮖ㒣⫼ᥝⱘᯊ䯈ˈᑊ೼ runޚ༅䋹ⱘᷛ ಴ℸᖙ乏ࡴܹˈgameTime ៥Ӏֱᄬ೼ GameConfiguration ᇍ䈵ЁˈgamePanel Ёֱᄬњ␌៣㚰߽Ϣ timeLabel ᰃᯊ䯈ᰒ⼎ⱘⱘϔϾ JLabelˈ ܜ䳔㽕ᇚ gamePanleˈgameTimeˈtimeLabel ԰Ўখ᭄Ӵܹˈ佪 䆹㉏㒻ᡓ java.util.TimerTaskˈᅲ⦄ run ᮍ⊩ˈ㸼⼎䳔㽕ᠻ㸠ⱘࡼ԰DŽ೼ TimerTask ⱘᵘ䗴఼Ёˈ } } this.time += 1; //Փ⫼ⱘᯊ䯈+1 this.timeLabel.repaint(); this.timeLabel.setText(String.valueOf(this.gameTime - this.time)); //བᵰ␌៣ҡ✊㒻㓁, 䆒㕂ᯊ䯈 } this.gamePanel.repaint(); this.cancel(); //প⍜䖭Ͼӏࡵ this.gamePanel.setOverImage(ImageUtil.getImage("images/lose.gif")); //䆒㕂␌៣೒⠛Ў༅䋹 if (this.gameTime - this.time <= 0) { //␌៣ᯊ䯈Ꮖࠄ public void run() { /Download at http://www.pin5i.com ⱘ㉏ˈݡ䗮䖛Ꮉॖ῵ᓣ៪㗙 Java ⱘডᇘএ㦋প䖭ѯ㉏ˈ⫼Ѣএ߯ᓎ␌៣ⱘ⬠䴶DŽ ៤ऩ⣀خ៪㗙৘䚼ӊⱘ㘠㛑ᢑ䈵៤ᇍ䈵DŽ೼ᴀゴЁҷⷕ䖬ৃҹ䖯㸠䞡ᵘӬ࣪ˈ՟བᇚ↣Ͼ೒ᔶ⬠䴶㒘ӊ 㛑Ϣ䅵ᯊㄝࡳ㛑DŽ䞡⚍ҟ㒡њབԩ೼䞡ᵘҷⷕⱘ䖛⿟ЁӬ࣪ҷⷕᑊ᜶᜶ⱘᬍ୘᮶᳝䆒䅵ˈᇚ␌៣ⱘ䚼ӊ 䈵៤ϔϾѠ㓈᭄㒘ˈᑊᇍ䆹Ѡ㓈᭄㒘䖯㸠᪡԰ˈᅲ⦄њ䱣ᴎ␌៣᭄㒘ˈᅲ⦄␌៣ⱘⳌ䖲ˈ␌៣ⱘ䅵ߚࡳ ᴀゴЏ㽕䆆䗄བԩᅲ⦄ϔϾㅔऩⱘऩᴎ⠜䖲䖲ⳟˈ䆺㒚ҟ㒡њབԩᓎゟ⬠䴶ˈབԩᇚ␌៣ⱘऎඳᢑ 7.6 ᴀゴᇣ㒧 ೒ 7.27 ᳔㒜ⱘ␌៣ᬜᵰ ㄀ 7 ゴ ऩᴎ䖲䖲ⳟ g33g /Download at http://www.pin5i.com ᭛ӊǃⳂᔩⱘᮄᓎ⬠䴶DŽ ⱘ⬠䴶⹂ᅮЎ೒ 8.1 ⱘᏗሔDŽᓎゟད䖭ϾᏗሔˈ៥Ӏ䖬䳔㽕߯ᓎ㦰ऩˈᓎゟᎹ԰ぎ䯈䗝ᢽ⬠䴶ˈ乍Ⳃǃ ࠄϔᅮ⿟ᑺᯊӮ᳝ѯϡⶹ᠔᥾DŽ៥Ӏ䳔㽕ᓎゟخЁ䳔㽕ᇣℹᖿ䎥ˈབᵰϔᓔྟᅮⱘⳂᷛ䍞໻ˈ䙷Мᕔᕔ ೼ᓔথࠡˈ៥Ӏ䳔㽕ⶹ䘧ϔϾ IDE ᳝ѯҔМ⬠䴶ˈ䳔㽕ᰒ⼎ϔѯҔМˈ⊼ᛣ៥Ӏ೼㓪ݭ⿟ᑣⱘ䖛⿟ 8.3 ᓎゟ⬠䴶 Ӏњ㾷བԩএᅲ⦄䖭ѯकߚᐌ㾕ⱘࡳ㛑DŽ Eclipse 䖭ḋⱘӬ⾔Ꮉ݋ˈা䳔㽕ᇚএᅲ⦄㞾ᏅⱘϔϾᢹ᳝ㅔऩࡳ㛑ⱘ IDEˈ䅽៥ ڣ⦃៥Ӏᑊϡ䳔㽕ᅲ ᦤ⼎ˈdebugˈ᭛ӊ᧰㋶ㄝϔ㋏߫䰘ࡴⱘࡳ㛑ˈ䖭ѯࡳ㛑䛑໻໻ⱘᮍ֓њ⿟ᑣਬⱘᓔথᎹ԰DŽ೼ᴀゴЁ Java ᭛ӊⱘ㓪䆥੠䖤㸠ㄝ෎ᴀࡳ㛑ेৃDŽϔϾⳳℷⱘ Java IDE 䖬ࣙᣀϾᗻ࣪䆒㕂ˈҷⷕⱘ⫳៤ˈᠧ⚍ ᴀゴ៥ӀЏ㽕㓪ݭϔϾㅔऩⱘ Java IDE Ꮉ݋ˈࡳ㛑ᑊϡ໮ˈᅲ⦄᭛ӊ᪡԰ˈ᭛ᴀ᪡԰ˈ乍Ⳃㅵ⧚ˈ 8.2 Java IDEⱘЏ㽕ࡳ㛑 NetBeansǃJBuilder ㄝ䖭ѯࡳ㛑ᔎ໻ⱘ IDE Ꮉ݋DŽ Eclipseǃ ڣߚᵤࡳ㛑ǃ㓪䆥ࡳ㛑ǃdebug ࡳ㛑ㄝϔԧ࣪ⱘᓔথ༫ӊDŽ՟བ Java ⿟ᑣਬ㒣ᐌՓ⫼ⱘ IDE ᰃ Integrated Development Environment ⱘ㓽ݭˈे䲚៤ᓔথ⦃๗ˈህᰃ䲚៤њҷⷕ㓪ݭࡳ㛑ǃ 8.1 IDEᎹ݋ㅔҟ ㄀  ゴㅔऩ-DYD,'(Ꮉ݋ /Download at http://www.pin5i.com //߯ᓎ䖭Ͼߚ䱨㒘ӊⱘᆍ఼ˈᑊᇚ box ᇍ䈵੠ infoPane ᬒ㕂݊Ё infoArea.setEditable(false); //䆒㕂ֵᙃऎϡৃ㓪䕥 infoPane = new JScrollPane(infoArea); //ᇚ infoArea ᭛ᴀඳ԰Ў㒘ӊᬒࠄ infoPane Ё infoArea = new JTextArea("", 5, 50); //߯ᓎֵᙃᰒ⼎ऎⱘ᭛ᴀඳ box.add(desk); box.add(tabPane); box = new Box(BoxLayout.Y_AXIS);//䆒㕂 box ⱘᏗሔ desk.setBackground(Color.GRAY);//䆒㕂 desk ⱘ㚠᱃买㡆Ў♄㡆 desk = new JDesktopPane();//߯ᓎ JDesktopPane ᇍ䈵 tabPane = new JTabbedPane(JTabbedPane.TOP, JTabbedPane.SCROLL_TAB_LAYOUT ); //߯ᓎЏ㓪䕥ऎⱘ tabPane setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //䆒㕂にষ݇䯁ˈ䗔ߎ⿟ᑣ public void initFrame() { private JTextArea infoArea; //⫼Ѣᰒ⼎ֵᙃⱘ᭛ᴀඳ private JScrollPane infoPane;//ৃҹ⒮ࡼⱘ JScrollPane ᇍ䈵ˈ⫼Ѣᬒ infoArea private JSplitPane editorSplitPane;//⫼Ѣߚ䱨Џ㓪䕥ऎ੠ֵᙃᰒ⼎ऎⱘᆍ఼ private JDesktopPane desk;//߯ᓎϔϾ໮᭛ḷⱘḠ䴶ᆍ఼ private Box box;//ᄬᬒ tabPane Ϣ desk private JTabbedPane tabPane; //໮᭛ӊⱘ tab ᷛ乬 ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\EditorFrame.java ៥ӀৃҹᮄᓎϔϾ initFrame ⱘᮍ⊩ˈ䆹ᮍ⊩Џ㽕⫼ᴹ߯ᓎ䖭Ͼ JFrame 䞠䴶ⱘ৘Ͼ㒘ӊDŽ ᓎゟᅠ EditorFrame ৢˈ៥Ӏ䳔㽕ᕔ䖭Ͼ JFrame 䞠䴶⏏ࡴ㒘ӊˈ䅽݊ヺড়៥Ӏⱘ㽕∖DŽ⏏ࡴ㒘ӊ } } pack();//Փ JFrame 䇗ᭈ᳔Շ໻ᇣ super(title); //䆒㕂ᷛ乬 public EditorFrame(String title) { public class EditorFrame extends JFrame { ݡᮄᓎϔϾ⬠䴶ⱘ JFrame ㉏DŽҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\EditorFrame.java } } editorFrame.setVisible(true); EditorFrame editorFrame = new EditorFrame("ide"); public static void main(String[] args) { public class Main { ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\Main.java ϡৃ㓪䕥ⱘ JTextArea ᇍ䈵DŽᮄᓎ⿟ᑣܹষ Main ㉏ˈ԰Ў䖭Ͼ IDE Ꮉ݋ⱘܹষDŽ ᇍ䈵ЁᬒϔϾ JTabbedPane ԰Ў tab 义ㅒˈݡᄬᬒϔϾ JDesktopPane ᇍ䈵DŽֵᙃᰒ⼎ऎЏ㽕ᰃϔϾ ࠄ໮᭛ӊ㓪䕥ˈ಴ℸ䳔㽕 JDesktopPaneˈ಴ℸˈ៥ӀⱘЏ㓪䕥ЁЏ㽕ᄬᬒϔϾ Box ᇍ䈵ˈBoxخ䳔㽕 ᓎゟЏ⬠䴶ⱘ JFrameˈᑊᕔ䞠䴶⏏ࡴ៥Ӏ᠔䳔㽕ⱘ㒘ӊDŽ೼ᴀ՟Ёˈ៥Ӏܜᣝ✻೒ 8.1ˈ៥Ӏ䳔㽕 8.3.1 ᓎゟЏ㓪䕥ऎ੠ֵᙃᰒ⼎ऎ ⳂᷛDŽ ࠄҹϞⱘᏗሔˈᑊৃҹ㞾⬅ⱘᢪࡼ↣Ͼऎⱘ໻ᇣेৃˈ䖭ᰃ៥Ӏⱘ㄀ϔϾخࠄ䙷М໡ᴖˈা䳔㽕خ䳔㽕 њ Eclipse ⱘ⬠䴶ˈEclipse Ёⱘⱘ↣Ͼऎඳ䛑ৃҹ㞾⬅ᰒ⼎Ϣ݇䯁ˈ៥Ӏ೼ᴀ՟Ёᑊϡ⫼׳ ೒ 8.1 ೒ 8.1 ⹂ᅮ⬠䴶Ꮧሔ /Download at http://www.pin5i.com ҹϞҷⷕᮄᓎњϔϾ JScrollPane ⫼Ѣᄬᬒ៥Ӏⱘ乍Ⳃᰒ⼎ऎЁⱘ᭛ӊᷥˈ៥Ӏ᱖ᯊՓ⫼ JTree ⱘ } mainSplitPane.setDividerSize(3); //䆒㕂ߚ䱨ᴵⱘ㉫㒚 mainSplitPane.setDividerLocation(200); //䆒㕂ߚ䱨ᴵⱘԡ㕂 editorSplitPane); mainSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, treePane, //߯ᓎЏ⬠䴶ⱘ JSplitPaneˈ῾৥ˈᎺ䖍Ў treePaneˈে䖍Ў editorSplitPane treePane = new JScrollPane(tree); //߯ᓎৃ⒮ࡼⱘᆍ఼ᇍ䈵 tree = new JTree();//߯ᓎᷥᇍ䈵 … public void initFrame() { private JTree tree; //乍Ⳃᷥᇍ䈵 private JSplitPane mainSplitPane; //ᭈϾ⬠䴶ⱘߚ䱨㒘ӊⱘᆍ఼ private JScrollPane treePane; //ᄬᬒᷥⱘৃ⒮ࡼᆍ఼ ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\EditorFrame.java ˄Ⳃᔩ˅DŽ೼ҹϟҷⷕЁ߯ᓎ乍Ⳃᰒ⼎ऎˈ᱖ᯊ߯ᓎϔỉḋ՟ᷥҷ᳓៥Ӏⱘ᭛ӊᷥDŽ 乍Ⳃᰒ⼎ऎˈৃҹ⧚㾷៤ᰃϔϾ᭛ӊ㋏㒳ᷥˈ⫼Ѣᄬᬒϔỉ᭛ӊᷥˈᷥⱘ↣Ͼ㡖⚍䛑ҷ㸼ϔӑ᭛ӊ 8.3.2 ᓎゟ乍Ⳃᰒ⼎ऎ JTabbedPane ᇍ䈵ⱘ߯ᓎ˄ᠧᓔ˅ˈߛᤶㄝࡳ㛑DŽ ֵᙃㄝDŽ೼㓪䕥ऎЏⱘ Box ᇍ䈵Ёˈ䖬᳝ϔϾ tab ᷛㅒ义ᇍ䈵ˈ⦄೼ᮄᓎњ⬠䴶ˈ೼ᴀゴӮᅲ⦄䆹 ֵᙃᰒ⼎ऎⱘ԰⫼Џᰃ⫼ᴹᰒ⼎ϔѯ᪡԰ⱘֵᙃˈ՟བ㓪䆥 Java ⑤᭛ӊߎ䫭ˈ䖤㸠㉏ᠧॄⱘϔѯ ೒ 8.2 ߯ᓎЏ㓪䕥ऎ੠ֵᙃᰒ⼎ऎ ⼎DŽ JTabbedPane ੠ JDesktopPaneˈ䖤㸠 Main ৃⳟࠄᬜᵰˈᇍ݊䖯㸠ᢪࡼˈৃҹⳟࠄⱘᬜᵰབ೒ 8.2 ᠔ ҹϞҷⷕ߯ᓎњЏ㓪䕥ऎ੠ֵᙃᰒ⼎ऎˈЏ㓪䕥ऎЏ㽕ᰃϔϾ Box ᇍ䈵ˈ䆹ᇍ䈵䞠䴶᳝ } pack();//Փ JFrame 䇗ᭈ᳔Շ໻ᇣ add(editorSplitPane); editorSplitPane.setDividerLocation(500); editorSplitPane.setDividerSize(3); editorSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, box, infoPane); /Download at http://www.pin5i.com //ⳕ⬹݊Ҫ߯ᓎⲥ਀఼ⱘҷⷕ fileMenu.add(fileNew).setAccelerator(KeyStroke.getKeyStroke('N', InputEvent.CTRL_MASK)); //ᮄᓎ᭛ӊⱘⲥ਀఼ public void addListeners() { //Ў EditorFrame Ёⱘ㒘ӊ⏏ࡴⲥ਀఼ //ⳕ⬹݊Ҫ Action ⱘ߯ᓎ }; } //᱖ᯊᦤկぎᅲ⦄ public void actionPerformed(ActionEvent e) { private Action fileNew = new AbstractAction("ᮄᓎ᭛ӊ", new ImageIcon("images/newFile.gif")) { //ᮄᓎ᭛ӊⱘ Action ᇍ䈵 private JMenu fileMenu; //᭛ӊ㦰ऩ private JMenu editMenu; //㓪䕥㦰ऩᇍ䈵 private JMenuBar menuBar; //㦰ऩᷣᇍ䈵 ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\EditorFrame.java ㉏ЁᮄᓎϔϾ addListeners ᮍ⊩ˈ⫼Ѣ⏏ࡴ EditorFrame Ё৘Ͼ㒘ӊⱘⲥ਀఼DŽ ߯ᓎ᭛ӊⱘᮄᓎǃֱᄬǃ䖤㸠ǃᠧᓔˈ᭛ᴀⱘ໡ࠊǃ࠾ߛ੠㉬䌈ˈᑊ⏏ࡴⲥ਀఼DŽ೼ EditorFrameܜϟ䴶 ऩ᳝໡ࠊǃ㉬䌈੠࠾ߛDŽᎹ݋ᷣⱘࡳ㛑Џ㽕᳝ᮄᓎ᭛ӊǃᠧᓔǃֱᄬǃࠋᮄǃ䖤㸠ǃ໡ࠊǃ㉬䌈੠࠾ߛDŽ ఼DŽЏ㽕ⱘ᭛ӊ᪡԰㦰ऩ᳝ᮄᓎ˄᭛ӊǃⳂᔩ੠乍Ⳃ˅ǃᠧᓔǃֱᄬǃࠋᮄǃ䖤㸠੠䗔ߎDŽ᭛ᴀ᪡԰㦰 ೼ 8.3.2 ЁᓎゟњЏ⬠䴶ⱘᏗሔৢˈ᥹ϟᴹЎ䖭Ͼ IDE Ꮉ݋⏏ࡴ㦰ऩ੠Ꮉ݋ᷣˈᑊЎᅗӀ⏏ࡴⲥ਀ 8.3.3 ⏏ࡴ㦰ऩ੠Ꮉ݋ᷣ ೒ 8.3 ⏏ࡴњ乍Ⳃᰒ⼎ऎৢⱘ⬠䴶 ՟ᷥҷ᳓᭛ӊᷥDŽ䖤㸠ৃⳟࠄⱘᬜᵰབ೒ 8.3DŽ⼎ /Download at http://www.pin5i.com //Ꮉ݋ᴵ ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\EditorFrame.java Ёⱘ JToolBar ㉏ৃᅲ⦄Ꮉ݋ᷣˈᑊЎ JToolBar 䆒㕂ᇍᑨⱘ Action ेৃDŽ ᥹ϟᴹˈЎ IDE Ꮉ݋⏏ࡴᎹ݋ᷣˈᑊЎᎹ݋ᷣЁⱘ৘Ͼࡳ㛑䆒㕂ᇍᑨⱘ Action ᇍ䈵DŽՓ⫼ swing ೒ 8.4 ⏏ࡴ᭛ӊ㦰ऩϢ㓪䕥㦰ऩ བ೒ 8.4 ᠔⼎DŽ ೼ addListeners ᮍ⊩ˈЎ fileMenu ⏏ࡴ৘Ͼ Action ᇍ䈵ˈᑊ᳝䆒㕂њᇍᑨⱘᖿ᥋䬂DŽ䖤㸠ⱘᬜᵰ ϲӦ༓ྑಬຣොѻෳēୣ൐ Action ճເԅ actionPerformed ԅֺ֥඘՛ඔ٢ࣂಬຣd ᅟġྻ౨ӝ৲ᄯᄚပ໭ߙำߑԅ Action ճເēୣ൐ԅ Action ճເԅҗߙֺ֥ူୣྡྷᄡē҂ॴොѻ } addListeners(); setJMenuBar(menuBar); //䆒㕂 JFrame ⱘ㦰ऩᷣ menuBar.add(editMenu); //ᇚ㓪䕥㦰ऩ⏏ࡴࠄ㦰ऩᷣЁ menuBar.add(fileMenu); //ᇚ᭛ӊ㦰ऩ⏏ࡴࠄ㦰ऩᷣЁ fileMenu = new JMenu("᭛ӊ");//߯ᓎ᭛ӊ㦰ऩ editMenu = new JMenu("㓪䕥");//߯ᓎ㓪䕥㦰ऩᇍ䈵 menuBar = new JMenuBar();//߯ᓎ㦰ऩᷣᇍ䈵 … public void initFrame() { } //ⳕ⬹⏏ࡴ݊Ҫⲥ਀఼ⱘҷⷕ editMenu.add(copy).setAccelerator(KeyStroke.getKeyStroke('C', InputEvent.CTRL_MASK)); //⏏ࡴ໡ࠊⲥ਀఼ fileMenu.add(exit); /Download at http://www.pin5i.com ࠄ⦄೼ˈЏ㓪䕥ऎⱘᏗሔϢ㦰ऩ䛑ᓎདњˈПࠡ᠔ᅮϟⱘϸϾᇣⳂᷛ䛑Ꮖ㒣ᅲ⦄ˈ೼䖯ܹ䖭Ͼ⬠䴶 8.3.4 ᓎゟᎹ԰ぎ䯈䗝ᢽ⬠䴶 ೒ 8.5 ⏏ࡴᎹ݋ᷣ 䞡ᮄ䖤㸠 Main ㉏ˈৃⳟࠄᬜᵰབ೒ 8.5 ᠔⼎DŽ } … //ᣝ✻䆹ᮍ⊩ձ⃵⏏ࡴᠧᓔǃֱᄬǃࠋᮄǃ䖤㸠ǃ໡ࠊǃ࠾ߛ੠㉬䌈 toolBar.add(fileNew).setToolTipText("ᮄᓎ᭛ӊ"); //ЎᎹ݋ᴵ⏏ࡴ৘Ͼ᪡԰ ... public void addListeners() { //Ў EditorFrame Ёⱘ㒘ӊ⏏ࡴⲥ਀఼ } ... add(toolBar, BorderLayout.NORTH);//ᇚᎹ݋ᷣ⏏ࡴࠄ EditorFrame Ё toolBar.setMargin(new Insets(0, 10, 5, 5));//䆒㕂Ꮉ݋ᷣⱘ䖍䎱 toolBar.setFloatable(false);//䆒㕂Ꮉ݋ᷣϡৃ⿏ࡼ toolBar = new JToolBar(); … public void initFrame() { private JToolBar toolBar; /Download at http://www.pin5i.com ೒ 8.8 ᓎゟ⏏ࡴ⬠䴶 ⹂ᅮ੠প⍜ⱘᣝ䪂ˈ᱖ᯊϡ䳔㽕ࡴϞџӊˈ᠔ҹᅲ⦄↨䕗ㅔऩDŽᅲ⦄ⱘᬜᵰབ೒ 8.8 ᠔⼎DŽ ᭛ӊǃⳂᔩ੠乍Ⳃⱘ⬠䴶DŽ䆹⬠䴶া䳔㽕ϔϾ᭛ᴀḚˈ⫼Ѣ䕧ܹ᭛ӊৡ⿄ǃⳂᔩৡ⿄੠乍Ⳃৡ⿄ˈᑊ᳝ ៥Ӏ೼ࠡ䴶Ꮖ㒣ᓎゟњ㓪䕥Џ⬠䴶ǃ䗝ᢽᎹ԰ぎ䯈⬠䴶ˈ⦄೼ˈ䖬䳔㽕ϔϾ⏏ࡴⱘ⬠䴶ˈ⫼Ѣ⏏ࡴ 8.3.5 ᓎゟ᭛ӊǃⳂᔩǃ乍Ⳃ⏏ࡴ⬠䴶 䗮䖛䗝ᢽᣝ䪂䗝ᢽᎹ԰ぎ䯈DŽ⦄೼ˈᎹ԰ぎ䯈߯ᓎⱘ⬠䴶Ꮖ㒣ᅲ⦄њDŽ 䆌⫼᠋Ⳉ᥹䕧ܹᄫヺˈা㛑ܕ䪂ⱘџӊ᱖ᯊϡ⫼ᅲ⦄DŽ঺໪ˈᎹ԰ぎ䯈ᰒ⼎ⱘ᭛ᴀḚ䆒㕂Ўϡৃ⫼ˈϡ button.setEnabled(false)ेৃ䖒ࠄᬜᵰDŽ៥Ӏᴀᇣ㡖াᰃ߯ᓎ⬠䴶ˈ಴Ў᭛ӊ䗝ᢽᣝ䪂ǃ⹂ᅮ੠প⍜ᣝ ⊼ᛣˈ೒ 8.7 Ёⱘ⹂ᅮᣝ䪂Ўϡৃ⫼ˈ⬅Ѣ≵᳝䗝ᢽᎹ԰ぎ䯈ˈ䙷М䆹ᣝ䪂ህϡৃҹ⚍ߏˈՓ⫼ ೒ 8.7 ߯ᓎᎹ԰ぎ䯈䗝ᢽ⬠䴶˄SpaceFrame ㉏˅ 䗮䖛ᮄᓎϔϾ JFrame ᴹ߯ᓎ䆹⬠䴶ेৃDŽ೒ 8.7 ߯ᓎњϔϾᎹ԰ぎ䯈䗝ᢽ⬠䴶DŽ ಮٝᆴࣂ޷ē༪Շ੯ఊԅٝᆴࣂ޷ԉٟટdॅৃޚᅟġแਬϢΡᆳӾ Ͼ SpaceFrame ⱘ㉏⫼ѢᓎゟᎹ԰ぎ䯈䗝ᢽ⬠䴶DŽ ᠋এ䗝ᢽᎹ԰ぎ䯈ⱘⳂᔩˈᑊ᳝ϔϾ᭛ᴀḚᰒ⼎䆹Ⳃᔩⱘܼ䏃ᕘˈݡ᳝⹂ᅮ੠প⍜ⱘᣝ䪂DŽ៥Ӏ߯ᓎϔ њ Eclipse Ё䗝ᢽᎹ԰ぎ䯈ⱘ⬠䴶ˈ៥Ӏ᠔ᅮⱘ⬠䴶া䳔㽕᳝ϔϾ᭛ӊ䗝ᢽ఼ˈ䅽⫼⫼׳೒ 8.6 Ё ೒ 8.6 Eclipse Ё䗝ᢽᎹ԰ぎ䯈ⱘ⬠䴶 ࠄⱘᬜᵰ໻ὖབ೒ 8.6 ᠔⼎DŽخˈᓎゟϔϾ䗝ᢽᎹ԰ぎ䯈ⱘ⬠䴶ˈ䅽⫼᠋೼Փ⫼䖭Ͼ IDE ᯊ䗝ᢽᎹ԰ぎ䯈 Пࠡˈ៥Ӏ䳔㽕᳝ϔϾ⬠䴶䅽⫼᠋এ䆒㕂乍ⳂᄬᬒⱘⳂᔩˈेᎹ԰ぎ䯈DŽ⦄೼៥ӀݡᅮϟϔϾᇣⳂᷛˈ /Download at http://www.pin5i.com Ꮉ԰ぎ䯈䗝ᢽࡳ㛑DŽخ䖭Ͼ⬠䴶 ぎ䯈DŽ೼ࠡ 8.3.4 ᇣ㡖ˈ៥ӀᏆ㒣߯ᓎњ䗝ᢽᎹ԰ぎ䯈ⱘ⬠䴶ˈབ೒ 8.7 ᠔⼎DŽ䙷М⦄೼ˈ៥Ӏህೈ㒩 Eclipse 䙷ḋˈ䖯ܹᯊ䗝ᢽᎹ԰ ڣЏ⬠䴶ࠡˈ៥Ӏ䳔㽕⫼᠋䗝ᢽϔϾⳂᔩ԰ЎᎹ԰ぎ䯈ˈህདخ೼ 8.4 ᅲ⦄Ꮉ԰ぎ䯈䗝ᢽࡳ㛑 ϡৠⱘџDŽخ⫼᠋䕧ܹⱘৡ⿄ˈݡḍ᥂䖭Ͼৡ⿄԰໘⧚ˈ೼ϟ䴶ゴ㡖ˈᇚӮ⫼䖭Ͼ⬠䴶 䖭䞠াᓎゟњϔϾ⬠䴶DŽᅲ䰙Ϟˈ⏏ࡴ᭛ӊǃⳂᔩǃ乍Ⳃৃҹ݅⫼䖭Ͼ⬠䴶ˈ䖭Ͼ⬠䴶ⱘ԰⫼াᰃ㦋প ⦄೼߯ᓎњϔϾ⏏ࡴⱘ⬠䴶ˈৃ㛑Ӯ㾝ᕫ༛ᗾˈ䳔㽕⏏ࡴ᭛ӊǃⳂᔩǃ乍Ⳃˈ䳔㽕᳝ϝϾ⬠䴶ˈ㗠 } pack(); add(mainPanel); //ᇚ mainPanel ⏏ࡴࠄ JFrame Ё mainPanel.add(buttonPanel); mainPanel.add(namePanel); //ᇚ᭛ᴀḚⱘ JPanel ੠ᣝ䪂ⱘ JPanel ⏏ࡴࠄ mainPanel Ё buttonPanel.add(cancelButton); buttonPanel.add(new JLabel(" ")); buttonPanel.add(confirmButton); confirmButton.setEnabled(false); buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS)); //⹂ᅮ੠প⍜ⱘᣝ䪂 namePanel.add(nameText); namePanel.add(nameLabel); namePanel.setLayout(new BoxLayout(namePanel, BoxLayout.X_AXIS)); //䆒㕂ᄬᬒ᭛ᴀḚⱘ JPanel ⱘᏗሔ setResizable(false); setLocation(200, 200); mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS)); //䆒㕂⬠䴶Ꮧሔⱘҷⷕ cancelButton = new JButton("প⍜"); confirmButton = new JButton("⹂ᅮ"); buttonPanel = new JPanel(); nameText = new JTextField("", 20); nameLabel = new JLabel("᭛ӊৡ⿄˖"); namePanel = new JPanel(); mainPanel = new JPanel(); //߯ᓎϔ㋏߫⬠䴶㒘ӊⱘҷⷕ public AddFrame() { private JButton cancelButton; //প⍜ᣝ䪂 private JButton confirmButton; //⹂ᅮᣝ䪂 private JPanel buttonPanel; //ᬒᣝ䪂ⱘ Panel private JTextField nameText; //䕧ܹৡ⿄ⱘ JTextField private JLabel nameLabel; //ᰒ⼎᭛ӊⱘ JLabel private JPanel namePanel; //乍Ⳃৡ⿄ private JPanel mainPanel; //䆹 Frame ⱘ JPanel ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\AddFrame.java ᛣˈ೼䖭䞠ⱘ䖭Ͼ JFrameˈབᵰ݇䯁ⱘᯊ׭ˈাᰃ᱂䗮ⱘ䆒㕂Ўϡᰒ⼎ˈ㗠ϡᰃ䗔ߎ⿟ᑣDŽ⊼ /Download at http://www.pin5i.com super.approveSelection();//䇗⫼⠊㉏ⱘ䗝ᢽ᭛ӊᮍ⊩ spaceFrame.getConfirmButton().setEnabled(true); //䆒㕂⹂ᅮᣝ䪂ৃ⫼ spaceFrame.getPathText().setText(folder.getAbsolutePath());//䆒㕂 SpaceFrame ᭛ᴀḚ spaceFrame.setFolder(folder); //䆒㕂 SpaceFrame ⱘሲᗻ folder ⱘؐ File folder = getSelectedFile();//㦋প⫼᠋䗝ᢽⱘ᭛ӊ public void approveSelection() { //䞡ݭ⠊㉏ⱘ䗝ᢽ᭛ӊᮍ⊩ } this.spaceFrame = spaceFrame; super("/");//䆒㕂䗝ᢽ఼ᠧᓔᯊⱘⳂᔩ public SpaceChooser(SpaceFrame spaceFrame) { //䳔㽕ᇚ SpaceFrame ԰Ўᵘ䗴খ᭄ private SpaceFrame spaceFrame; class SpaceChooser extends JFileChooser { ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\SpaceFrame.java ᢽ఼ⱘᇍ䈵ˈᓎゟϔϾ SpaceChooser ㉏ˈৠḋৃҹᇚ䆹㉏ᬒࠄ SpaceFrame.java Ёˈ݋ԧҷⷕབϟDŽ ⬅Ѣ ChoseButtonListener ⱘᵘ䗴খ᭄Ё䳔㽕ϔϾ᭛ӊ䗝ᢽ఼ᇍ䈵ˈ಴ℸ៥Ӏ䳔㽕߯ᓎϔϾ᭛ӊ䗝 } } chooser.showOpenDialog(null); //ᰒ⼎䖭Ͼ᭛ӊ䗝ᢽ఼ chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); //䆒㕂䆹᭛ӊ䗝ᢽ఼া㛑䗝ᢽ᭛ӊⳂᔩ public void actionPerformed(ActionEvent arg0) { } this.chooser = chooser; public ChoseButtonListener(JFileChooser chooser) { //߯ᓎ ChoseButtonListener 䳔㽕᭛ӊ䗝ᢽ఼԰ᵘ䗴খ᭄ private JFileChooser chooser; //䳔㽕ᠧᓔⱘ᭛ӊ䗝ᢽ఼ᇍ䈵 class ChoseButtonListener implements ActionListener { ԧҷⷕབϟDŽҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\SpaceFrame.java ⦄೼ݭϔϾ䗝ᢽᣝ䪂ⱘⲥ਀఼㉏ ChoseButtonListenerˈৃҹᇚ䆹㉏ᬒࠄ SpaceFrame.java Ёˈ݋ 䯈ⱘⳂᔩDŽ ᴀḚে䖍ⱘĀ䗝ᢽāᣝ䪂ˈᔧ⚍ߏ䖭Ͼᣝ䪂ᯊˈህᔍߎ᭛ӊ䗝ᢽ఼ˈ⫼Ѣ䗝ᢽᶤϾ៥Ӏ䳔㽕԰ЎᎹ԰ぎ ೼䗝ᢽᎹ԰ぎ䯈ᯊˈ៥Ӏ䳔㽕ϔϾ᭛ӊ䗝ᢽ఼⫼Ѣ䗝ᢽ᭛ӊˈᑊϨা㛑䗝ᢽⳂᔩˈབ೒ 8.8 Ёⱘ᭛ 8.4.1 ᅲ⦄Ⳃᔩ䗝ᢽࡳ㛑 াⳟࠄᎹ԰䗝ᢽぎ䯈ⱘ⬠䴶DŽ 䆒㕂 EditorFrame Ўৃ㾕ˈᑊਞ䆝 EditorFrame 䗝ᢽњાϾᎹ԰ぎ䯈DŽ䖤㸠 Main ㉏ˈህৃҹⳟࠄᬜᵰˈ ᇚ EditorFrame Ӵ㒭 SpaceFrameˈ಴Ў೼ SapceFrame Ё䗝ᢽњᎹ԰ぎ䯈˄Ⳃᔩ˅ৢˈ៥Ӏ䳔㽕 } spaceFrame.setVisible(true); //䅽 SpaceFrame ৃ㾕 SpaceFrame spaceFrame = new SpaceFrame(editorFrame); //ᇚ editorFrame ᇍ䈵԰Ў SpaceFrame ⱘᵘ䗴খ᭄ EditorFrame editorFrame = new EditorFrame("ide");//߯ᓎ EditorFrameˈ᱖ᯊϡ⫼䆒㕂ৃ㾕 public static void main(String[] args) { ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\Main.java বЎϡৃ㾕ˈᑊᇚ䆹ᇍ䈵ᔧ԰খ᭄Ӵ㒭Ꮉ԰ぎ䯈ⱘ JFrame ㉏˄SpaceFrame ㉏˅ ᬍ⿟ᑣܹষ㉏ˈ⿟ᑣਃࡼˈᰒ⼎䗝ᢽᎹ԰ぎ䯈ⱘ⬠䴶ˈᑊᇚПࠡ߯ᓎⱘ EditorFrame ᇍ䈵ׂˈܜ佪 /Download at http://www.pin5i.com 䗝ᢽњᶤϾⳂᔩ䖯㸠ᠧᓔ᪡԰ˈৃҹⳟࠄ䗝ᢽᎹ԰Ⳃᔩⱘ⬠䴶བ೒ 8.10 ᠔⼎DŽ ೒ 8.9 䗝ᢽᎹ԰ぎ䯈 ⦄೼ˈ䖤㸠 Main ㉏ˈ⚍ߏ䗝ᢽᣝ䪂ˈৃⳟࠄᬜᵰˈབ೒ 8.9 ᠔⼎˖ } ... choseButton.addActionListener(new ChoseButtonListener(chooser)); //Ў choseButton ⏏ࡴⲥ਀఼, ᑊᇚ SpaceChooser ᇍ䈵԰Ўᵘ䗴খ᭄ ... //ⳕ⬹߯ᓎ⬠䴶ⱘ৘Ͼ㒘ӊ੠Ꮧሔⱘҷⷕ public SpaceFrame(EditorFrame editorFrame) { private SpaceChooser chooser; private JButton choseButton; ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\SpaceFrame.java ChoseButtonListener ᇍ䈵ˈ߯ᓎⱘҷⷕབϟDŽ ߯ᓎᅠ᭛ӊ䗝ᢽ఼ৢˈৃҹ SpaceFrame Ёⱘ䗝ᢽᣝ䪂⏏ࡴⲥ਀఼ˈ䆹ⲥ਀఼ህᰃϞ䴶ⱘ confirmButton˄⹂ᅮᣝ䪂˅䳔㽕ᦤկ getter ᮍ⊩DŽ 䖭ϝϾሲᗻЁˈfolder ሲᗻ䳔㽕ᦤկ setter ੠ getter ሲᗻˈpathText˄䏃ᕘᰒ⼎᭛ᴀḚ˅੠ private File folder; //⫼᠋䗝ᢽⱘ᭛ӊⳂᔩᇍ䈵 private JButton confirmButton; //Ꮉ԰ぎ䯈Ёⱘ⹂ᅮᣝ䪂 private JTextField pathText; //Ꮉ԰ぎ䯈Ёᰒ⼎⫼᠋䗝ᢽ᭛ӊⳂᔩⱘ JTextField ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\SpaceFrame.java ሲᗻˈpathText ሲᗻϢ confirmButton ሲᗻⱘҷⷕབϟDŽ 㕂Ў⫼᠋䗝ᢽⱘ᭛ӊⱘ㒱ᇍ䏃ᕘˈᇚᇚ SpaceFrame Ёⱘ⹂ᅮᣝ䪂䆒㕂Ўৃ⫼DŽSpaceFrame Ёⱘ folder ᭛ӊˈᔧ⫼᠋߽⫼᭛ӊ䗝ᢽ఼䗝ᢽњ᭛ӊⳂᔩᯊˈ䳔㽕ᇚ SpaceFrame Ёⱘ JTextFiedl pathText ⱘؐ䆒 ᛣҹϞҷⷕⱘ咥ԧ䚼ߚˈ೼ SpaceFrame Ёˈ䳔㽕⏏ࡴϔϾ File ⱘሲᗻˈ⫼Ѣᄬᬒ⫼᠋᠔䗝ᢽⱘ⊼ { { /Download at http://www.pin5i.com EditorFrame 䇗⫼ initFrame ᓎゟ⬠䴶ⱘᯊ׭ˈህᇚ䖭Ͼ WorkSpace ᇍ䈵԰Ўᮍ⊩খ᭄Ӵܹ˄Ϟ䴶ҷⷕ 䖭䞠៥Ӏ䳔㽕߯ᓎϔϾ WorkSpace ᇍ䈵ˈᇚ⫼᠋䗝ᢽᎹ԰ⳂᔩϢ editorFrame ԰Ўᵘ䗴খ᭄ˈ೼ } } spaceFrame.setVisible(false); //䅽Ꮉ԰䗝ᢽぎ䯈⬠䴶ϡৃ㾕 editorFrame.setVisible(true); //ᇚ EditorFrame 䆒Ўৃ㾕 editorFrame.initFrame(new WorkSpace(spaceFrame.getFolder())); //䇗 EditorFrame ⱘ initFrame ᮍ⊩߱ྟ࣪⬠䴶 public void actionPerformed(ActionEvent arg0) { } this.editorFrame = editorFrame; this.spaceFrame = spaceFrame; public ConfirmButtonListener(SpaceFrame spaceFrame, EditorFrame editorFrame) { private EditorFrame editorFrame; private SpaceFrame spaceFrame; class ConfirmButtonListener implements ActionListener { ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\SpaceFrame.java ߏњ⹂ᅮᣝ䪂ⱘ㸠ЎDŽ ϟ䴶ˈݡЎᎹ԰ぎ䯈䗝ᢽⱘ⹂ᅮᣝ䪂⏏ࡴϔϾⲥ਀఼ˈConfirmButtonListener ㉏ˈ⫼Ѣ໘⧚⫼᠋⚍ } ... } this.folder = folder; public WorkSpace(File folder) { private File folder; //Ꮉ԰ぎ䯈ᇍᑨⱘⳂᔩ public class WorkSpace { ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\commons\WorkSpace.java File ᇍ䈵DŽ ߯ᓎϔϾᎹ԰Ⳃᔩⱘᇍ䈵˖WorkSpace ㉏ˈ䆹㉏䳔㽕ֱᄬᎹ԰ぎ䯈ⱘⳂᔩܜᮍ⊩ӴܹϔϾᇍ䈵ेৃDŽ Ⳃᔩএ߯ᓎ乍ⳂᷥDŽ೼ EditorFrame ㉏Ёˈ߯ᓎ⬠䴶ⱘᰃ initFrame ᮍ⊩ˈ಴ℸˈা䳔㽕Ў initFrame ᔧ䗝ᢽᅠᎹ԰ぎ䯈ৢˈ䳔㽕ਞ䆝 EditorFrame 䗝ᢽњાϾⳂᔩˈݡḍ᥂䅽 EditorFrame ḍ᥂䗝ᢽⱘ 8.4.2 ᅲ⦄Ꮉ԰ぎ䯈䗝ᢽⱘ⹂ᅮᣝ䪂 Ꮉ԰ⱘⳂᔩDŽ ぎ䯈Ⳃᔩⱘ㒱ᇍ䏃ᕘӴ㒭 EditorFrameˈਞ䆝ᅗ䗝ᢽњⱘⳂᔩˈ䙷М EditorFrame ᇚӮҹ䖭ϾⳂᔩ԰Ў ᅲ⦄ϔѯҔМḋⱘࡼ԰˖䳔㽕ᇚ SpaceFrame বᕫϡৃ㾕ˈᰒ⼎ EditorFrameˈᑊᇚ⫼᠋᠔䗝ᢽⱘᎹ԰ ៥Ӏ䳔㽕ᯢⱑˈ⚍ߏњ⹂ᅮᣝ䪂ৢᑨ䆹ܜᅲ⦄њ䖭ϔℹПৢˈ៥Ӏ䖬䳔㽕ᅲ⦄⹂ᅮᣝ䪂ⱘࡼ԰ˈ佪 ヺড়៥Ӏⱘ乘ᳳᬜᵰDŽ 䗝ᢽᅠᎹ԰ぎ䯈ৢˈᰒ⼎ⱘ᭛ᴀḚህߎ⦄њ᠔䗝ⱘⳂᔩⱘ㒱ᇍ䏃ᕘˈ⹂ᅮᣝ䪂гবᕫৃ⫼њˈ䖭г ೒ 8.10 ᰒ⼎䗝ᢽⱘᎹ԰ぎ䯈 /Download at http://www.pin5i.com public ProjectTreeModel(ProjectTreeNode arg0) { public class ProjectTreeModel extends DefaultTreeModel { ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\tree\ProjectTreeModel.java ᵘ䗴఼ˈ⫼Ѣ䆒㕂䖭Ͼ῵ൟⱘḍ㡖⚍ˈҷⷕབϟDŽ ߯ᓎᅠ㡖⚍ᇍ䈵ৢˈ៥Ӏ䖬䳔㽕ϔϾᷥⱘ῵ൟᇍ䈵ˈᑊ㒻ᡓѢ DefaultTreeModel ㉏ˈ᱖ᯊাᦤկϔϾ ᓎ䖭ḋⱘϔϾᇍ䈵ˈ㸼⼎ህ߯ᓎϔϾᷥⱘ㡖⚍ˈ䆹ᇍ䈵㒻ᡓѢ swing ⱘ DefaultMutableTreeNode ㉏DŽ ProjectTreeNode ᇍ䈵䞠᳝䆹㡖⚍᠔ᇍᑨⱘ᭛ӊሲᗻ˄гৃҹᰃⳂᔩ˅ˈ䆹㡖⚍ⱘᄤ㡖⚍䲚ড়ˈ↣߯ } ... } children = new ArrayList();//߱ྟ࣪䆹㡖⚍ϟⱘᄤ㡖⚍䲚ড় this.file = file; super(file.getName(), allowsChildren); public ProjectTreeNode(File file, boolean allowsChildren) { 䆌᳝ᄤ㡖⚍ܕ //ProjectTreeNode ⱘᵘ䗴఼ˈখ᭄ߚ߿ᯊ䆹㡖⚍ᇍᑨⱘ᭛ӊˈᰃ৺ private List children; //䆹㡖⚍ϟⱘᄤ㡖⚍ private File file; //䆹㡖⚍ᇍᑨⱘ᭛ӊ public class ProjectTreeNode extends DefaultMutableTreeNode { ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\tree\ProjectTreeNode.java ߯ᓎ㡖⚍ᇍ䈵ˈ೼䖭䞠ˈ៥Ӏᇚ݊ੑৡЎ ProjectTreeNodeˈҷⷕབϟDŽܜˈܜ佪 ⚍ᯊⱘⲥ਀఼ᇍ䈵ㄝDŽ ೼߯ᓎ乍Ⳃᷥࠡˈ៥Ӏ䳔㽕ᇚᷥⱘ৘Ͼᇍ䈵߯ᓎˈ՟བᷥ᠔Փ⫼ⱘ modelˈᷥ㡖⚍ᇍ䈵ˈ䗝ᢽᷥ㡖 8.5.1 ߯ᓎᷥⱘ৘Ͼᇍ䈵 ỉ⼎՟ⱘᷥ᱖ᯊᬒ೼䆹ऎඳDŽ ऎⱘ乍ⳂᷥDŽ߯ᓎᷥˈ៥ӀৃҹՓ⫼ swing Ёⱘ JTree ᇍ䈵ˈ೼߯ᓎ乍Ⳃᰒ⼎ऎᯊˈ៥ӀᏆ㒣ᅮНњϔ 䞠䴶᳝Ꮉ԰ぎ䯈ⱘⳂᔩঞᇍᑨⱘ EditorFrame ᇍ䈵ˈ⦄೼ህḍ᥂䖭Ͼ WorkSpace ᇍ䈵ᴹ߯ᓎ乍Ⳃᰒ⼎ ᅲ⦄њᎹ԰ぎ䯈ⱘ䗝ᢽᣝ䪂ৢˈ⦄೼ⱘ EditorFrame ህৃҹᕫࠄϔϾ WorkSpace ⱘᇍ䈵ˈ䆹ᇍ䈵 8.5 ߯ᓎ乍Ⳃᷥ Ξғࣇನē༓ྑොѻё༝dڑԤݬॴ௜່ݧრܜΞಹߑēӲဈڑᅟġ༓ྑน SpaceFrame චަ ߎᴹњDŽ ⦄೼䖤㸠 Main ㉏ˈৃҹⳟࠄᔧ៥Ӏ䗝ᢽњᎹ԰ぎ䯈Ⳃᔩˈᑊ⚍ߏњ⹂ᅮৢˈ៥Ӏⱘ㓪䕥Џ⬠䴶ህ confirmButton.addActionListener(new ConfirmButtonListener(this, editorFrame)); //Ў⹂ᅮᣝ䪂⏏ࡴⲥ਀఼ Ў SpaceFrame ⱘ⹂ᅮᣝ䪂⏏ࡴⲥ਀఼˖ } ... this.workSpace = workSpace; public void initFrame(WorkSpace workSpace) { ... private WorkSpace workSpace; //䆹㓪䕥ऎ᠔ᇍᑨⱘᎹ԰ぎ䯈 ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\EditorFrame.java ⱘ咥ԧ䚼ߚ˅ˈᑊ䳔㽕೼ EditorFrame ㉏ЁᦤկϔϾ WorkSpace ⱘሲᗻ⫼Ѣֱᄬ䖭Ͼሲᗻˈҷⷕབϟ˖ /Download at http://www.pin5i.com ᅗ⫼ TreeCreator ᴹ߯ᓎ乍ⳂᷥDŽ ҹϞҷⷕⱘ咥ԧ䚼ߚ߯ᓎϔϾ TreeCreatorDŽ䙷Мˈ⦄೼ህৃҹএׂᬍ EditorFrame ЁⱘҷⷕˈՓ } spaceFrame.setVisible(true); //䅽 SpaceFrame ৃ㾕 SpaceFrame spaceFrame = new SpaceFrame(editorFrame); //ᇚ editorFrame ᇍ䈵԰Ў SpaceFrame ⱘᵘ䗴খ᭄ EditorFrame editorFrame = new EditorFrame("ide", treeCreator); //߯ᓎ EditorFrameˈ᱖ᯊϡ⫼䆒㕂ৃ㾕 TreeCreator treeCreator = new TreeCreatorImpl();//߯ᓎϔϾ TreeCreator public static void main(String[] args) { ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\Main.java … } this.treeCreator = treeCreator; super(title); //䆒㕂ᷛ乬 public EditorFrame(String title, TreeCreator treeCreator) { private TreeCreator treeCreator; //䋳䋷߯ᓎᷥⱘᇍ䈵 ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\EditorFrame.java Ё߯ᓎ EditorFrame ᇍ䈵ⱘҷⷕDŽ ೼߯ᓎᯊህᇚ TreeCreator ԰Ўᵘ䗴খ᭄ӴܹˈᑊЎ TreeCreator ᦤկ getter ᮍ⊩ˈݡׂᬍ೼ Main ㉏ TreeCeatorImpl ᱖ᯊাᦤկぎᅲ⦄ˈ᥹ϟᴹЎ EditorFrame ⱘᵘ䗴఼⏏ࡴᵘ䗴খ᭄ˈ䅽 EditorFrame } ProjectTreeNode createNode(File folder);ḍ᥂ϔϾⳂᔩ߯ᓎᅗⱘ㡖⚍ JTree createTree(EditorFrame editorFrame);ḍ᥂㓪䕥ⱘ EditorFrame ᇍ䈵߯ᓎ乍Ⳃᷥ public interface TreeCreator { ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\tree\TreeCreator.java ᔩ߯ᓎ㡖⚍˄ProjectTreeNode˅ⱘ᥹ষᮍ⊩DŽ ḋⱘ᥹ষᮍ⊩ˈ՟བ䳔㽕ϔϾḍ᥂ϔϾ WorkSpace ᇍ䈵এ߯ᓎϔϾ JTree ⱘᮍ⊩ˈ䖬䳔㽕ϔϾḍ᥂Ⳃ TreeCreator ᥹ষˈᑊᮄ๲ϔϾᅲ⦄㉏ TreeCreatorImplˈ៥Ӏ䳔㽕ᯢⱑ䖭Ͼ TreeCreator ᦤկϔѯҔМ Ԣҷⷕⱘ㗺ড়ˈEditorFrame ḍᴀϡ䳔㽕⧚Ӯ TreeCreator ᰃབԩএ߯ᓎᷥⱘDŽᮄᓎޣ䖭ḋৃҹ ҹݭϔϾ TreeCreator ⱘ᥹ষˈӴࠄ EditorFrame Ёˈݡ䗮䖛 TreeCreator ⱘ᥹ষᮍ⊩এ߯ᓎ乍Ⳃᷥˈ ᳝⚍ϡড়䗖ˈ䖭ᯊˈ៥Ӏৃڣⱘᮍ⊩Ёা䋳䋷߯ᓎЏ㓪䕥⬠䴶ˈབᵰᇚ߯ᓎ乍Ⳃᷥⱘҷⷕᬒࠄ݊Ёˈད ߯ᓎᅠϞ䴶ⱘϝϾᇍ䈵ৢˈ៥Ӏህৃҹ೼ EditorFrame ㉏Ёⱘ initFrame ߯ᓎ乍ⳂᷥњˈEditorFrame } } //᱖ᯊᦤկぎᅲ⦄ public void mousePressed(MouseEvent e) { } public ProjectTreeSelectionListener() { public class ProjectTreeSelectionListener extends MouseAdapter { ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\tree\ProjectTreeSelectionListener.java ㉏DŽ 㾺থⱘџӊ೼ᴀ՟Ёাᰃ᱂䗮ⱘᠧᓔ᭛ӊDŽ߯ᓎ ProjectTreeSelectionListener ㉏ᑊ㒻ᡓ MouseAdapter Ͼ῵ൟᇍ䈵䞠䴶⏏ࡴҷⷕDŽ⦄೼䖬䳔㽕߯ᓎᷥⱘⲥ਀఼ˈᔧ⫼᠋⚍ߏњᷥⱘᯊ׭ˈ䙷Мህ䳔㽕㾺থџӊˈ 䆹㉏ᑊ≵᳝ӏԩ㽚Ⲫ⠊㉏ⱘ៪㗙㞾Ꮕⱘ㸠Ўˈབᵰ೼ϟ䴶䳔㽕ࡴܹ㞾Ꮕⱘ㸠Ўⱘᯊ׭ˈህৃҹ೼䖭 } } super(arg0); /Download at http://www.pin5i.com ⱘᬜᵰབ೒ 8.11DŽ ҹϞҷⷕ߯ᓎᷥⱘḍ㡖⚍ᇍ䈵੠ᷥⱘ῵ൟᇍ䈵ˈᑊ⫼ treeModel ᇍ䈵߯ᓎ JTree ᑊ䖨ಲDŽᕫࠄⱘᷥ } return new JTree(treeModel); ProjectTreeModel treeModel = new ProjectTreeModel(root); //ҹḍ㡖⚍ᓎゟϔϾᷥ῵ൟᇍ䈵 ProjectTreeNode root = new ProjectTreeNode(spaceFolder, true); //߯ᓎᷥⱘḍ㡖⚍, ㉏ൟЎ ProjectTreeNode, ḍ㡖⚍ᇍᑨⱘ᭛ӊЎᎹ԰ぎ䯈Ⳃᔩ File spaceFolder = workSpace.getFolder();//㦋প workSpace ЁⱘᎹ԰ぎ䯈Ⳃᔩ public JTree createTree(WorkSpace workSpace) { ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\tree\TreeCreatorImpl.java 乍Ⳃᄬ೼Ѣ䖭ϾᎹ԰ぎ䯈ϟDŽ㾘߭Ꮖ㒣ࠊᅮˈ䙷М⦄೼ህৃҹএᅲ⦄ createTree ᮍ⊩њDŽ 乍Ⳃৡ⿄DŽ՟བˈᎹ԰ぎ䯈ϟ䴶᳝ϔӑĀproject1.projectāⱘ᭛ӊˈ䙷Мህᛣੇⴔ᳝ϔϾি project1 ⱘ ぎ䯈ϟҹ.project 㒧ሒⱘ᭛ӊˈབᵰᶹᡒࠄϔӑˈህ⹂ᅮ䆹Ꮉ԰ぎ䯈᳝ϔϾ乍Ⳃˈݡ䗮ᄫヺІ៾প㦋ᕫ ࠄᎹ԰ぎ䯈ⱘⳂᔩˈ䖭ḋˈݡ䗮䖛ϔᅮⱘ㾘߭ᶹᡒᎹ԰ぎ䯈ϟⱘ乍ⳂⳂᔩDŽᴀ՟Փ⫼ⱘ㾘߭ЎᶹᡒᎹ԰ ⦄೼៥Ӏህ䳔㽕এ㗗㰥བԩ೼ TreeCreatorImpl ⱘ createTree ᮍ⊩Ёএ߯ᓎϔỉ乍ⳂᷥњDŽ៥Ӏᕫ 8.5.2 ᅲ⦄߯ᓎ乍Ⳃᷥⱘࡳ㛑 } ... tree = treeCreator.createTree(this); //Փ⫼ treeCreator ߯ᓎᷥ ... public void initFrame(WorkSpace workSpace) { ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\EditorFrame.java /Download at http://www.pin5i.com List projectNames = getProjectNames(spaceFolder); //䇗⫼㦋প᠔᳝乍Ⳃৡ⿄ⱘ⾕᳝ᮍ⊩ private List getProjectFolders(File spaceFolder) { //㦋পᎹ԰ぎ䯈Ⳃᔩϟ᠔᳝ⱘ乍ⳂⳂᔩ ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\tree\TreeCreatorImpl.java Ϟ䴶ⱘᮍ⊩㦋পᎹ԰ぎ䯈Ⳃᔩϟ᠔᳝ҹ.project 㒧ሒⱘ᭛ӊˈݡ㦋প䖭ѯ᭛ӊⱘ᭛ӊৡ˄ϡ㽕ৢ㓔˅DŽ } return result; } } result.add(file.getName().substring(0, file.getName().indexOf(".project"))); //⏏ࡴࠄ㒧ᵰ䲚Ё if (file.getName().endsWith(".project")) { //㦋পҹ.project 㒧ሒⱘ᭛ӊ for (File file : spaceFolder.listFiles()) { //䘡ग़Ꮉ԰ぎ䯈ϟⱘ᠔᳝᭛ӊ List result = new ArrayList(); private List getProjectNames(File spaceFolder) { //㦋পᎹ԰ぎ䯈Ⳃᔩϟ᠔᳝ⱘ乍Ⳃৡ⿄ ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\tree\TreeCreatorImpl.java 㽕ᕫࠄ䖭ѯ᭛ӊⳂᔩˈ៥Ӏ䳔㽕ݭϸϾ⾕᳝ᮍ⊩DŽ ߯ᓎᅠ䖭ϸϾᇍ䈵ৢˈ៥Ӏ䳔㽕ݭϔѯ⾕᳝ᮍ⊩ˈ⫼Ѣ㦋পᎹ԰ぎ䯈Ⳃᔩϟⱘ乍ⳂⳂᔩⱘ䲚ড়ˈ㗠 ೒ 8.11 ߯ᓎা᳝ḍ㡖⚍ⱘᷥ /Download at http://www.pin5i.com if (!folder.isDirectory()) { 䆌ᢹ᳝ᄤ㡖⚍ܕ //བᵰখ᭄ foler ϡᰃϔϾⳂᔩⱘ䆱ˈ߯ᓎϔϾ ProjectTreeNode ᇍ䈵ᑊ䖨ಲˈ㸼ᯢᅗϡ ProjectTreeNode parent = null; //߯ᓎϔϾ⠊㡖⚍ˈेᴀᮍ⊩ेᇚ䖨ಲⱘ㡖⚍ᇍ䈵 public ProjectTreeNode createNode(File folder) { //ḍ᥂ϔϾⳂᔩএ߯ᓎ䆹Ⳃᔩ᠔ᇍᑨⱘ㡖⚍ᇍ䈵ˈ䆹ᇍ䈵ⱘ᠔᳝ⱘᄤ㡖⚍䛑Ꮖ㒣߯ᓎ ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\tree\TreeCreatorImpl.java ⫼䖭Ͼᮍ⊩এ߯ᓎᷥDŽ এᅲ⦄ createNode(File folder)ᮍ⊩ˈ䖭ᰃ಴Ў೼ TreeCreatorImpl ЁᇚӮՓܜᮍ⊩ᇮ᳾ᅲ⦄ˈ៥Ӏৃҹ ᥹ϟᴹˈݡএᅲ⦄ TreeCreator ⱘ঺໪ϔϾ᥹ষᮍ⊩ˈ㱑✊䖭ᯊ׭ TreeCreatorImpl Ёⱘ createTree ມವēუऺํ໹ᄯٓંৃᄯԅൎပำߑďͧࣳᆐંৃĐࠩ໻ॴ߅Ӧԅ૦༝dੋ܊֗ྡྷ͎ԅำߑݖၽંৃԅ ēંৃྡྷ͎ၽ஍ੋມವēژᅟġྻ౨ԅӝ৲ēᄍൎྻྑճำߑ೴ᆦβॅ०ұēಾ࿙น̟ზྡྷ͎ԅ๽ } return result; } if (!file.isDirectory()) result.add(new ProjectTreeNode(file, false)); //㄀Ѡ⃵䘡ग़ˈབᵰ䴲Ⳃᔩⱘ䆱ˈህࡴܹࠄ㒧ᵰ䲚ড়Ё for (File file : files) { } if (file.isDirectory()) result.add(new ProjectTreeNode(file, true)); //㄀ϔ⃵䘡ग़ˈབᵰᰃⳂᔩⱘ䆱ˈህࡴܹࠄ㒧ᵰ䲚ড়Ё for (File file : files) { //ᇍ䆹Ⳃᔩϟⱘ᠔᳝᭛ӊⱘ᭄㒘䖯㸠ϸ⃵䘡ग़ List result = new ArrayList(); File[] files = folder.listFiles();//㦋প䆹Ⳃᔩϟⱘ᠔᳝᭛ӊ private List createNodes(File folder) { //ḍ᥂ϔϾⳂᔩ߯ᓎᅗⱘ᠔᳝Ⳉ᥹㡖⚍ ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\tree\TreeCreatorImpl.java ᄤ㡖⚍˅DŽ 䆌ᢹ᳝ᄤ㡖⚍њDŽ㓪ݭ߯ᓎⳈ᥹㡖⚍ⱘҷⷕˈгህᰃḍ᥂ϔϾⳂᔩˈ߯ᓎᅗⱘᄤ㡖⚍˄Ⳉ᥹ܕᯊˈህϡ 䆌ᢹ᳝ᄤ㡖⚍ˈᔧ file ሲᗻᰃ᱂䗮ⱘ᭛ӊܕ⚍˄ProjectTreeNode˅ⱘ file ሲᗻᰃⳂᔩⱘᯊ׭ˈ䆹㡖⚍䛑 ϟˈ䆹㡖މProjectTreeNode ᇍ䈵ϟ䴶䛑ৃ㛑᳝ᅗⱘᄤ㡖⚍ˈП᠔ҹ䇈ᰃৃ㛑ˈᰃ಴Ў೼៥Ӏᔧࠡⱘᚙ ⦄೼ৃҹএ߯ᓎ㡖⚍ⱘᇍ䈵ˈᷥЁⱘ↣ϔϾ㡖⚍䛑ᇍᑨϔϾ ProjectTreeNode ᇍ䈵ˈ↣ϔϾ ϟ᳝ϔϾ test.project ⱘ᭛ӊˈԚᰃै≵᳝Ⳍᇍᑨⱘ乍ⳂⳂᔩˈ಴ℸϞ䴶ⱘ⿟ᑣᇚӮᗑ⬹䖭ϔϾ乍ⳂDŽ ՟བᎹ԰ぎ䯈˖މᔩˈབᵰᰃⳂᔩˈ߭ݡএ߸ᮁ䆹Ⳃᔩⱘৡ⿄ᰃ৺ЎᏆ᳝ⱘ乍Ⳃৡˈৃ㛑Ӯ᳝䖭ḋⱘᚙ ߸ᮁ䆹᭛ӊᰃ৺ЎⳂܜˈ⊼ᛣҹϞҷⷕⱘ咥ԧ䚼ߚˈϸϾ if ߸ᮁˈ೼䘡ग़Ꮉ԰ぎ䯈ϟⱘ᠔᳝᭛ӊᯊ } return result; } } } if (projectName.equals(file.getName()))result.add(file); if (file.isDirectory()) { //བᵰᎹ԰ぎ䯈ϟ䴶ⱘ᭛ӊᰃⳂᔩˈݡএ߸ᮁᰃ৺ᰃ乍ⳂⳂᔩ for (File file : files) { for (String projectName : projectNames) { File[] files = spaceFolder.listFiles(); //㦋পᎹ԰ぎ䯈ϟ䴶᠔᳝ⱘ᭛ӊ List result = new ArrayList(); /Download at http://www.pin5i.com ৃҹⳟࠄⱘᬜᵰབ೒ 8.12 ᠔⼎DŽ 䖭ḋᅲ⦄њ߯ᓎᷥⱘࡳ㛑њˈ៥Ӏৃҹᕔ᠔䗝ᢽⱘᎹ԰ぎ䯈Ё⏏ࡴ޴Ͼ᭛ӊⳂᔩ੠᭛ӊࡴҹ⌟䆩DŽ } return tree; } root.add(node); //৥ḍ㡖⚍⏏ࡴᄤ㡖⚍˄乍ⳂⳂᔩ˅ ProjectTreeNode node = createNode(projectFolder); //䇗⫼ createNode ߯ᓎᅗ᠔᳝ⱘᄤ㡖⚍ File projectFolder = projectFolders.get(i); //㦋পᕾ⦃ЁⱘⳂᔩ for (int i = 0; i < projectFolders.size(); i++) { //䘡ग़乍ⳂⳂᔩ䲚ড়ˈᑊЎ݊߯ᓎᄤ㡖⚍ List projectFolders = getProjectFolders(spaceFolder); //㦋পᎹ԰ぎ䯈ϟ䴶᠔᳝ⱘⳂᔩ˄ेϢ᳝ projectName.project ⳌᇍᑨⱘⳂᔩ˅ˈгህᰃ乍ⳂⳂᔩ JTree tree = new JTree(treeModel);//߯ᓎᷥᇍ䈵 ProjectTreeModel treeModel = new ProjectTreeModel(root);//ҹḍ㡖⚍ᓎゟϔϾᷥ῵ൟᇍ䈵 ProjectTreeNode root = new ProjectTreeNode(spaceFolder, true); //߯ᓎᷥⱘḍ㡖⚍, ㉏ൟЎ ProjectTreeNode, ḍ㡖⚍ᇍᑨⱘ᭛ӊЎᎹ԰ぎ䯈Ⳃᔩ File spaceFolder = editorFrame.getWorkSpace().getFolder(); //㦋প workSpace ЁⱘᎹ԰ぎ䯈Ⳃᔩ public JTree createTree(EditorFrame editorFrame) { ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\tree\TreeCreatorImpl.java ᅠᭈˈ䅽ᅗ៤ЎϔỉⳳℷⱘᷥDŽܙⱘḍˈ⦄೼ᇚ݊㸹 ݭᅠϞ䴶ⱘᮍ⊩ˈህϛџׅ໛ˈৃҹএᅲ⦄߯ᓎᷥⱘᮍ⊩њˈࠡ䴶䆹ᮍ⊩াᅲ⦄њϔञˈ߯ᓎњᷥ } return parent; } parent.add(createNode(node.getFile())); //䗦ᔦ߯ᓎᄤ㡖⚍ˈᑊᇚ䖨ಲⱘ㡖⚍⏏ࡴࠄ parent Ё for (ProjectTreeNode node : nodes) { //㦋পࠄ parent ϟ䴶ⱘ᠔᳝Ⳉ᥹ᄤ㡖⚍ৢˈݡএᕾ⦃䗦ᔦ䇗⫼ᴀᮍ⊩ List nodes = createNodes(parent.getFile()); //߽⫼Ϟ䴶ⱘ parent 㡖⚍এᶹᡒᅗϟ䴶᠔᳝ⱘⳈ᥹㡖⚍ } parent = new ProjectTreeNode(folder, true); //བᵰᰃϔϾⳂᔩⱘ䆱ˈ߭߯ᓎϞ䴶ⱘ parentˈ㸼ᯢᅗᰃϔϾⳂᔩˈৃҹᢹ᳝ᄤ㡖⚍ } else { return new ProjectTreeNode(folder, false); /Download at http://www.pin5i.com public static String FILE = "images/file.gif";//᮴ᄤ㡖⚍ⱘ᭛ӊ೒⠛ public static String FOLDER_OPEN = "images/folder-open.gif";//Ⳃᔩᠧᓔ೒⠛ public static String FOLDER_CLOSE = "images/folder-close.gif";//Ⳃᔩ݇䯁೒⠛ public class ImageUtil { ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\util\ImageUtil.java 㓪ݭϔϾ೒⠛᭛ӊⱘ䇏প㉏ ImageUtilˈ⫼Ѣ䇏পᷥ᠔⫼ࠄⱘ೒⠛˖ܜⱘ೒⠛DŽ 䰸њϡᰒ⼎ᷥⱘḍП໪ˈ៥Ӏ䖬ৃҹᅮࠊᷥⱘ৘⾡೒⠛ˈՓ⫼ DefaultTreeCellRenderer ᴹᅮࠊᷥ } return tree; tree.setRootVisible(false); //䆒㕂ᷥⱘḍ㡖⚍ϡৃ㾕 ... public JTree createTree(WorkSpace workSpace) { ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\tree\TreeCreatorImpl.java setRootVisible ᮍ⊩ेৃDŽ ೼ϔ㠀ⱘᎹ԰ぎ䯈ϟˈাӮᰒ⼎৘Ͼ乍Ⳃˈ಴ℸˈৃҹᇚ䖭ỉᷥⱘḍ㡖⚍䱤㮣ˈՓ⫼ JTree ⱘ 8.5.3 䆒㕂ᷥⱘ໪㾖 䛑ৃҹᰒ⼎೼ᷥϞњDŽ ៥Ӏৃҹⳟࠄ䖭ỉᷥˈҹᎹ԰ぎ䯈᠔೼ⱘⳂᔩ԰Ўḍ㡖⚍ˈᭈỉᷥ߯ᓎᅠњDŽᎹ԰ぎЁⱘ޴Ͼ乍Ⳃ ೒ 8.12 ߯ᓎϔỉᅠᭈⱘᷥ /Download at http://www.pin5i.com ݡ⃵䖤㸠 Main ㉏ˈᑊ䗝ᢽᎹ԰ぎ䯈ˈৃҹⳟࠄབ೒ 8.13 ⱘᬜᵰDŽ } return tree; tree.setRootVisible(false); //䆒㕂ᷥⱘḍ㡖⚍ϡৃ㾕 tree.expandPath(path); //䅽ᷥ咬䅸ሩᓔḍ㡖⚍ TreePath path = new TreePath(root); //߯ᓎḍ೼乍ⳂᷥЁⱘ䏃ᕘ ... public JTree createTree(WorkSpace workSpace) { ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\tree\TreeCreatorImpl.java ᳝㞾ࡼሩᓔˈ៥Ӏৃҹ㦋পᷥⱘḍˈݡᇍ݊䖯㸠ሩᓔ˄߯ᓎⱘᯊ׭˅DŽ 䖭ᯊ׭ݡএ䖤㸠 Main ㉏ˈৃҹথ⦄೼乍Ⳃᰒ⼎ऎᑊ≵᳝ᷥˈ䖭ᰃ⬅Ѣ៥Ӏ䱤㮣њḍˈԚᰃḍै≵ } return tree; tree.setRootVisible(false); //䆒㕂ᷥⱘḍ㡖⚍ϡৃ㾕 tree.setCellRenderer(renderer); //䆒㕂ᷥⱘ䚼ӊ໘⧚㉏ЎϞ䴶ⱘ renderer renderer.setClosedIcon(ImageUtil.getImageIcon(ImageUtil.FOLDER_CLOSE)); //Ⳃᔩ݇䯁ᯊⱘ೒⠛ renderer.setLeafIcon(ImageUtil.getImageIcon(ImageUtil.FILE)); //㡖⚍≵᳝ᄤ㡖⚍ⱘ೒⠛ renderer.setOpenIcon(ImageUtil.getImageIcon(ImageUtil.FOLDER_OPEN)); //Ⳃᔩᠧᓔᯊⱘ೒⠛ DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();//ᅮࠊ㡖⚍೒⠛ ... public JTree createTree(WorkSpace workSpace) { ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\tree\TreeCreatorImpl.java } } return new ImageIcon(getImage(path)); public static ImageIcon getImageIcon(String path) { } return ImageIO.read(new File(path)); public static Image getImage(String path) { Փ⫼ ImageIO ⌕䇏প೒⠛// /Download at http://www.pin5i.com content = new String(arr, 0, readLength); while ((readLength = fis.read(arr)) > 0) { int readLength ; byte[] arr = new byte[1024]; String content = null; FileInputStream fis= new FileInputStream(file); //Փ⫼ IO ⌕䇏প᭛ӊ try { StringBuffer result = new StringBuffer(); public static String readFile(File file) { //䇏পϔϾ᭛ӊⱘݙᆍ ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\util\FileUtil.java ᓎϔϾ FileUtil ⱘ㉏DŽ ೼⚍ᷥ㡖⚍ⱘᯊ׭䇏প㡖⚍˄᭛ӊ˅ⱘݙᆍˈ䖭ḋ೼ⲥ਀఼ⱘ㉏ˈህϡ䳔㽕݇ᖗབԩএ䇏প᭛ӊњDŽᮄ ᓔথϔϾ䇏প᭛ӊݙᆍⱘᎹ݋㉏ˈܜݙᆍᰒ⼎ࠄ EditorFrame ЁⱘЏ㓪䕥ऎⱘ᭛ᴀ㓪䕥ඳЁDŽ⦄೼៥Ӏ ᔩˈ䖭Ͼ JTree Ꮖ㒣ᐂ៥Ӏᅲ⦄њˈབᵰ⚍ߏⱘᰃϔϾ᱂䗮㡖⚍˄᱂䗮᭛ӊ˅ˈ䙷Мህᇚ䆹㡖⚍ⱘ᭛ӊ ϔѯҔМḋⱘџᚙˈᔧ⚍ߏ㡖⚍ⱘᯊ׭ˈབᵰ⚍ߏⱘᰃϔϾⳂᔩˈ䙷Мህሩᓔ䖭ϾⳂخ⚍ߏ㡖⚍ᯊ䳔㽕 ៥Ӏ䳔㽕ᯢⱑܜᯊ׭ᑊ≵᳝ᇍᅗ䖯㸠ᅲ⦄ˈ⦄೼៥ӀⱘᷥᏆ㒣៤ൟњˈৃҹᇍࡴܹ㡖⚍ⱘ䗝ᢽџӊDŽ佪 ೼ 8.5.1 ЁˈᏆ㒣߯ᓎњᷥⱘ৘ϾⳌ݇ⱘ㉏ˈ݊Ёህࣙᣀᷥⱘ㡖⚍ⱘ⚍ߏⲥ਀㉏ˈᔧᯊ߯ᓎ䆹㉏ⱘ 8.5.4 ᅲ⦄ᷥ㡖⚍ⱘ䗝ᢽџӊ ೒ 8.13 䆒㕂ᷥⱘ໪㾖 /Download at http://www.pin5i.com if (selectNode == null) return; //བᵰ≵᳝䗝ᢽ㡖⚍ˈህ䖨ಲ ProjectTreeNode selectNode = this.editorFrame.getSelectNode(); //ᕫࠄᔧࠡ᠔䗝ᢽⱘ㡖⚍ public void mousePressed(MouseEvent e) { ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\tree\ProjectTreeSelectionListener.java 䙷Мⲥ਀఼ህৃҹՓ⫼䖭Ͼᮍ⊩ᕫࠄᔧࠡᷥ᠔䗝ᢽⱘ㡖⚍DŽ } return null; //ᔧࠡ≵᳝䗝ᢽ㡖ህ䖨ಲ null } return selectNode; ProjectTreeNode selectNode = (ProjectTreeNode)path.getLastPathComponent(); //߯ᓎϔϾ ProjectTreeNode ᇍ䈵ᑊ⫼Ѣ䖨ಲ if (path != null) { //བᵰᔧࠡ䗝ᢽњ㡖⚍ TreePath path = tree.getSelectionPath(); //㦋ᕫᔧࠡᷥ䗝ᢽⱘ㡖⚍೼ᷥЁⱘ䏃ᕘ public ProjectTreeNode getSelectNode() { //㦋প㓪䕥఼Џ⬠䴶乍ⳂᷥЁ᠔䗝Ёⱘ㡖⚍ ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\EditorFrame.java 㡖⚍ⱘᮍ⊩ˈᷥⲥ਀఼ᕫࠄᔧࠡ᠔䗝ᢽⱘ㡖⚍ˈህৃҹᇍ݊䖯㸠໘⧚DŽ ᇍ䈵Ёⱘሲᗻˈ՟བৃҹ䇗⫼ EditorFrame Ёⱘᠧᓔ᭛ӊᮍ⊩DŽ೼ EditorFrame ЁᮄᓎϔϾ㦋পᔧࠡᷥ ᇚ EditorFrame ᇍ䈵Ӵ㒭ᷥⱘⲥ਀఼ৢˈⲥ਀఼ህৃҹ䇗⫼ EditorFrame ⱘᮍ⊩এ໘⧚ EditorFrame } return tree; ... tree.addMouseListener(new ProjectTreeSelectionListener(editorFrame)); //Ў乍Ⳃᷥ⏏ࡴϔϾᷥ䗝ᢽⲥ਀఼ ... public JTree createTree(EditorFrame editorFrame) { ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\tree\TreeCreatorImpl.java } this.editorFrame = editorFrame; public ProjectTreeSelectionListener(EditorFrame editorFrame) { //ᇚЏ⬠䴶ⱘ frame(EditorFrame)԰Ўᵘ䗴খ᭄Ӵܹⲥ਀఼ private EditorFrame editorFrame; //Џ⬠䴶ⱘ frame ৺⚍ߏњ᭛ӊ㡖⚍ˈབᵰ⚍ߏњ᭛ӊ㡖⚍ˈ߭䇗 FileUtil ⱘ䇏প᭛ӊⱘᮍ⊩㦋প᭛ӊݙᆍDŽ ਀఼㉏ ProjectTreeSelectionListener ⱘ mousePressed ᮍ⊩ˈᔧ哴ᷛ೼ᷥϞ⚍њⱘᯊ׭ˈህএ߸ᮁᰃ ҹϞҷⷕ߯ᓎϔϾ䇏প᭛ӊⱘᮍ⊩ˈ᳔㒜ⱘ䖨ಲؐЎ䆹᭛ӊⱘݙᆍᄫヺІDŽ⦄೼ৃҹএᅲ⦄ᷥⱘⲥ ѕ RunTimeExceptiondޟᅟġFileException ಾᆑՇ࿌࿓Шफē } return result.toString(); } throw new FileException("read '" + file.getAbsolutePath() + "' file error"); } catch(IOException e) { fis.close(); } result.append(content); /Download at http://www.pin5i.com ⊼ᛣϞ䴶ⱘ咥ԧ䚼ߚˈ㸼⼎䅽⏏ࡴ⬠䴶᠔ᰒ⼎ⱘ᭛ᄫˈ䳔㽕ᰒ⼎ⱘ᭛ᄫ䇋ⳟ೒ 8.14DŽ } … } this.handler = handler; this.editorFrame = editorFrame; this.info = info; public AddInfo(String info, EditorFrame editorFrame, AddHandler handler) { private AddHandler handler; //ᮄ๲ᅠৢⱘ໘⧚㉏ private EditorFrame editorFrame; //ফᮄ๲᪡԰ᕅડⱘ frame private String info; //ᄫヺІ, ೼ᮄ๲⬠䴶ⱘ text ࠡᰒ⼎, ՟བ: ᭛ӊৡ⿄ public class AddInfo { ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\commons\AddInfo.java ՟བ㽕Փ⫼ાϾ⏏ࡴ໘⧚㉏ˈ᠔ᰒ⼎ⱘϔѯ᭛ᄫㄝˈ៥ӀᮄᓎϔϾ AddInfo ⱘ㉏ˈ⫼Ѣֱᄬ䖭ѯֵᙃDŽ ೼ EditorFrame ᠧᓔ⏏ࡴⱘ⬠䴶˄AddFrame˅ⱘᯊ׭ˈ៥Ӏ䳔㽕ਞ䆝 AddFrame ϔѯⳌ݇ⱘֵᙃˈ } //䕧ܹᅠ᭛ӊৡ⿄ৢˈህᠻ㸠䖭䞠ⱘҷⷕ public void afterAdd(EditorFrame editorFrame, AddFrame addFrame, Object data) { ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\handler\add\AddFileHandler.java } void afterAdd(EditorFrame editorFrame, AddFrame addFrame, Object data); //খ᭄Ў EditorFrameˈAddFrameˈ䕧ܹⱘֵᙃ data ⱘџᚙ⬅ᅲ⦄㉏এᅲ⦄خⱘџᚙˈ䳔㽕خ //ᮄ๲ᅠৢ䳔㽕 public interface AddHandler { ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\handler\add\AddHandler.java ᓎ᭛ӊⱘ᪡԰DŽ AddHandler ⱘ᥹ষˈ⫼Ѣ໘⧚ᮄᓎⱘ᪡԰DŽЎ䖭Ͼ᥹ষᮄᓎϔϾᅲ⦄㉏ AddFileHandlerˈ⫼Ѣ໘⧚ᮄ ᮄᓎϔϾܜⱘⳂᔩDŽ೼ 8.3.5 ϔ㡖Ёᦤࠄˈᮄᓎ᭛ӊǃⳂᔩ੠乍Ⳃᰃ݅⫼ϔϾ⏏ࡴ⬠䴶ⱘˈϟ䴶៥Ӏ Ӏᴹᅲ⦄ᮄᓎ᭛ӊⱘࡳ㛑DŽ㽕ᮄᓎϔϾ᭛ӊˈࠡᦤᰃ䳔㽕⫼᠋೼ᷥ㡖⚍Ϟ䗝њϔϾ㡖⚍ˈᕫࠄ߯ᓎ᭛ӊ ៥Ӏ೼ 8.3.3 ϔ㡖Ё߯ᓎњ᭛ӊ᪡԰ⱘ㦰ऩ੠Ꮉ݋ᷣˈ೼ 8.3.5 ϔ㡖Ёᓎゟњ⏏ࡴⱘ⬠䴶ˈ⦄೼៥ 8.6.1 ᮄᓎ᭛ӊ ᮄㄝϔ㋏߫෎ᴀⱘ᭛ӊ᪡԰DŽ 䖭ϔ㡖ˈ៥Ӏᇚᅲ⦄᭛ӊⱘ᪡԰ࡳ㛑ˈࣙᣀᮄᓎ᭛ӊǃⳂᔩ੠乍Ⳃˈ᭛ӊⱘᠧᓔǃֱᄬǃ䖤㸠ǃࠋ 8.6 ᅲ⦄᭛ӊ᪡԰ࡳ㛑 ำߑԅЀᆴd უನ༓ྑၽ EditorFrame ᄯ໭ߙྡྷّ openFile ֺ֥ēϵ೴ಾ File ճເēႀನඔ٢ࣂಬຣē༓ྑ҉सӕࢗ ē࡜Ըဈ EditorFrame ԅ openFile ֺ֥ē܊ēၽ޳ධ୶ᄯēԄӾ༪ᄯԅࠋԤדᅟġྻ౨ӝ৲ԅ۱඘Ϧ } this.editorFrame.openFile(selectNode.getFile()); //Փ⫼ EditorFrame ⱘᮍ⊩ᴹᠧᓔ᭛ӊ if (selectNode.getFile().isDirectory()) return; བᵰ䗝ᢽⱘᰃϔϾⳂᔩˈ䖨ಲ// /Download at http://www.pin5i.com Ϟ䴶ⱘҷⷕЁˈ䗮䖛 AddInfo 㦋ᕫ໘⧚㉏ˈݡ䇗⫼ afterAdd ᮍ⊩ˈᇚⳌ݇ⱘֵᙃӴ䗦㒭݋ԧⱘ໘⧚ info.getHandler().afterAdd(info.getEditorFrame(), this, data); //ᮄ๲ৢӮ⍝ঞⱘϔѯϢϮࡵⳌ݇ⱘ᪡԰⬭㒭 Handler ㉏໘⧚ String data = nameText.getText();//㦋পᕫ⫼᠋䕧ܹ ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\AddFrame.java ᇍ䈵Ӵ㒭໘⧚㉏ˈ࠽ϟህᰃ໘⧚㉏ⱘџᚙњDŽ 䳔㽕ᰒ⼎DŽ೼᭛ᴀḚ䕧ܹњؐৢˈህৃҹ䇗⫼ AddHandler ⱘᮍ⊩໘⧚ˈᇚ AddFrame ੠ EditorFrame ᭛ᴀḚؐᬍবⱘⲥ਀఼ˈϢ 8.4 Ёᅲ⦄Ꮉ԰ぎ䯈䗝ᢽⱘࡳ㛑ϔḋˈ᭛ᴀḚ᳝ؐⱘᯊ׭ˈ⹂ᅮᣝ䪂ᠡ ‰ ⹂ᅮᣝ䪂ⱘⲥ਀఼ ‰ にষ݇䯁ⱘⲥ਀఼ ‰ প⍜ᣝ䪂ⱘⲥ਀఼ ‰ ᭛ᴀḚؐᬍবⱘⲥ਀఼DŽ ⱘџᚙ˖خⱘᮍ⊩ेৃDŽ៥Ӏ䳔㽕ᯢ⹂ AddFrame ᠔㽕 AddHandler ㉏এ໘⧚⏏ࡴࡼ԰ˈ㟇Ѣᗢḋ໘⧚ˈAddFrame ᅠܼϡ⫼݇ᖗDŽা䋳䋷䇗⫼ AddHandler AddFrame Ёˈ䖭ḋ AddFrame ህⶹ䘧བԩᰒ⼎ᇍᑨⱘ᭛ᄫˈ⏏ࡴᅠৢህৃҹ䇗⫼ AddInfo Ёⱘ Ϟ䴶ҷⷕЁᮄ๲њϸϾᮍ⊩ˈᅲ⦄њᮄᓎ᭛ӊⱘ㦰ऩˈᰒ⼎⏏ࡴⱘ⬠䴶ˈᑊᇚ AddInfo ᇍ䈵Ӵࠄ ႏϵ೴dٲႏ୶ē༓ྑྡྷّ AddInfo ճເᆴนٲAddFrame ԅ ؟ᅟġ༓ྑ༉ } addFrame.setVisible(true); addFrame.pack(); addFrame = new AddFrame(info); setEnabled(false); //Փ EditorFrame বЎϡৃ⫼ private void showAddFrame(AddInfo info) { //ᰒ⼎ᮄ๲ⱘ⬠䴶 } showAddFrame(info); AddInfo info = new AddInfo("᭛ӊৡ⿄˖", this, new AddFileHandler()); public void newFile() { //ᮄᓎ᭛ӊⱘᮍ⊩ }; } newFile(); public void actionPerformed(ActionEvent e) { private Action fileNew = new AbstractAction("ᮄᓎ᭛ӊ", new ImageIcon("images/newFile.gif")) { //ᮄᓎ᭛ӊⱘ Action ᇍ䈵 private AddFrame addFrame; //⏏ࡴⱘ⬠䴶 ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\EditorFrame.java ⦄೼ˈಲࠄ EditorFrame ЁˈЎᮄᓎ᭛ӊ⏏ࡴⲥ਀఼ˈᑊᇕ㺙ϔϾ AddInfo Ӵ㒭 AddFrameDŽ ᅟġ෍ 8.14 ᄯԅq༓ྑມವำᆓԅԙֺr࡜ಾ AddInfo ᄯԅ info ೫໿d ೒ 8.14 AddInfo 䇈ᯢ /Download at http://www.pin5i.com 䖤㸠 Main ㉏ˈ⏏ࡴ᭛ӊⱘࡳ㛑Ꮖ㒣ᅲ⦄њˈᬜᵰབ೒ 8.15 Ϣ 8.16 ᠔⼎DŽ editorFrame.reloadNode(selectNode); //䞡ᮄࡴ䕑᠔䗝ᢽⱘ㡖⚍ ᮍ⊩ˈׂᬍЎ˖ ಲ༈ݡএׂᬍ AddFileHandler ⱘ afterAdd ᮍ⊩ˈ೼ afterAdd Ё䇗⫼њ EditorFrame ⱘ reloadNode } model.reload(selectNode, treeCreator); //䞡ᮄࡴ䕑᠔䗝ᢽⱘ㡖⚍ ProjectTreeModel model = (ProjectTreeModel)getTree().getModel(); //ࠋᮄᷥⱘ㡖⚍ if (selectNode == null) return; public void reloadNode(ProjectTreeNode selectNode) { //ࠋᮄᷥ㡖⚍ ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\EditorFrame.java ЁˈⳈ᥹䅽 EditorFrame ࠋᮄᅗ㞾ᏅⱘᷥDŽ ೼ ProjectTreeModel Ё䞡ᮄᅲ⦄њ reload ᮍ⊩ৢˈৃҹᇚ೼ reloadNode ⱘࡳ㛑ᬒࠄ EditorFrame ೮ճເďgetTreeĐԅֺ֥dݍᅟġ༓ྑၽ EditorFrame ᄯඔ٢ֲ } super.reload(node); //䇗⫼ DefaultTreeModel ⱘ reload ᮍ⊩ parent.insert(node, index); //⏏ࡴࠄ⠊㡖⚍Ё node = creator.createNode(node.getFile());//ݡ䗮䖛 TreeCreator 㦋পᮄⱘ㡖⚍ 㺙 node 㡖⚍Ң parent Ёߴ䰸ܜ// ;( parent.remove(index int index = parent.getIndex(node); //㦋প node 㡖⚍೼⠊㡖⚍ⱘ㋶ᓩ if (parent == null) return; //⠊㡖⚍Ў nullˈ䖨ಲˈϡ䳔㽕 reload ProjectTreeNode parent = (ProjectTreeNode)node.getParent();//㦋প node 㡖⚍ⱘ⠊㡖⚍ public void reload(ProjectTreeNode node, TreeCreator creator) { ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\tree\ProjectTreeModel.java ֓䖒ࠄ៥Ӏ䳔㽕ࠋᮄᶤϾ㡖⚍ⱘᬜᵰDŽ ᮄˈ಴ℸˈ៥Ӏ䳔㽕Ў ProjectTreeModel ᅲ⦄ϔϾ reload ᮍ⊩ˈখ᭄ᰃ TreeNodeǃTreeCreatorˈҹ 䖤㸠 Main ㉏ˈᑊ䖯㸠ᮄᓎ᭛ӊⱘ᪡԰ˈৃҹথ⦄೼ᶤϾⳂᔩϟᮄᓎњϔϾ᭛ӊˈ䆹㡖⚍ᑊ≵᳝ࠋ } addFrame.setVisible(false); //䅽⏏ࡴⱘ frame ϡৃ㾕 editorFrame.setEnabled(true); //ՓЏ㓪䕥 frame ৃ⫼ model.reload(selectNode); //䞡ᮄࡴ䕑᠔䗝ᢽⱘ㡖⚍ ProjectTreeModel model = (ProjectTreeModel)editorFrame.getTree().getModel(); newFile.createNewFile(); File newFile = new File(folder.getAbsoluteFile() + File.separator + data); //߯ᓎ᭛ӊˈᬒࠄ folder ϟ } folder = parent.getFile(); selectNode = parent; ProjectTreeNode parent = (ProjectTreeNode)selectNode.getParent(); if (!folder.isDirectory()) { //བᵰ folder ϡᰃϔϾⳂᔩˈ߭⫼ selectNode ⱘ⠊㡖⚍˄ᰃϔϾⳂᔩ˅԰Ўᮄ᭛ӊⱘᄬᬒⳂᔩ File folder = selectNode.getFile();//㦋পᔧࠡ䗝ᢽ㡖⚍᠔ᇍᑨⱘ᭛ӊ ProjectTreeNode selectNode = editorFrame.getSelectNode();//㦋ᕫᔧࠡ᠔䗝ᢽⱘᷥ㡖⚍ public void afterAdd(EditorFrame editorFrame, AddFrame addFrame, Object data) { ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\handler\add\AddFileHandler.java ㉏ˈ䖭䞠ⱘ໘⧚㉏ᰃ AddFileHandlerDŽ ೒ 8.15 ᮄᓎ᭛ӊ Download at http://www.pin5i.com/ /Download at http://www.pin5i.com //བᵰ folder ϡᰃϔϾⳂᔩˈ߭⫼ selectNode ⱘ⠊㡖⚍˄ᰃϔϾⳂᔩ˅԰ЎᮄⳂᔩⱘ⠊Ⳃᔩ File folder = selectNode.getFile();//㦋প䆹㡖⚍᠔ᇍᑨⱘ᭛ӊᇍ䈵 ProjectTreeNode selectNode = editorFrame.getSelectNode();//㦋ᕫᷥЁ᠔䗝পⱘ㡖⚍ public void afterAdd(EditorFrame editorFrame, AddFrame addFrame, Object data) { ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\handler\add\AddFolderHandler.java ࡳ㛑DŽা䳔㽕⏏ࡴϔϾᅲ⦄㉏ AddFolderHandler ህৃҹᅲ⦄DŽ ৃҹḍ᥂䖭Ͼᇍ䈵Ёⱘ໘⧚㉏এᮄᓎ᭛ӊǃⳂᔩ៪㗙乍Ⳃˈ䖭ϔ㡖៥Ӏᇚ೼䖭Ͼ෎⸔Ϟᅲ⦄ᮄᓎⳂᔩⱘ ೼ 8.6.1 Ёᅲ⦄њᮄᓎ᭛ӊⱘࡳ㛑ˈ೼ AddFrame Ёˈ៥Ӏা䳔㽕Ӵ㒭䆹㉏ϔϾ AddInfo ᇍ䈵ˈህ 8.6.2 ᮄᓎⳂᔩ }; } reloadNode(getSelectNode()); public void actionPerformed(ActionEvent e) { private Action refresh = new AbstractAction("ࠋᮄ", new ImageIcon("images/refresh.gif")) { //ࠋᮄᷥⱘ Action ᇍ䈵 ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\EditorFrame.java ࠋᮄࡳ㛑ˈ䙷М⦄೼ৃҹᅲ⦄ EditorFrame Ёࠋᮄⱘ ActionDŽ ⱘ㡖⚍ˈݡ䖯㸠⫼ ProjectTreeModel ⱘ reload ᮍ⊩䖯㸠ࠋᮄᷥⱘᶤϾ㡖⚍ेৃDŽϞ䴶៥Ӏᅲ⦄њᷥⱘ ೼Ϟ䴶ⱘ⿟ᑣЁˈ៥ӀᏆ㒣ᅲ⦄њ᭛ӊ᪡԰Ёⱘࠋᮄࡳ㛑ˈা㽕Ўࠋᮄ㦰ऩ⏏ࡴⲥ਀఼ˈ㦋পᔧࠡ ೒ 8.16 ᮄᓎ᭛ӊৢࠋᮄᷥ /Download at http://www.pin5i.com Ⳃⱘ໘⧚㉏ AddProjectHandlerDŽ ᕫ᳈ᅠ୘ϔ⚍ˈৃҹᦤկ⬠䴶ᴹ䜡㕂䖭ѯֵᙃDŽϟ䴶ᇚᅲ⦄ᮄᓎ乍خᄬᬒ䖭ѯⳌ݇ⱘֵᙃDŽᔧ✊ˈབᵰ ೼ᮄᓎⳂᔩⱘᯊ׭ˈ䖬䳔㽕᳝⑤᭛ӊ᠔೼ⱘⳂᔩˈ㓪䆥ⳂᔩㄝֵᙃDŽᮄᓎϔϾ CompileConfig ㉏ᴹ } treePane.updateUI(); //᳈ᮄ⬠䴶 treePane.setViewportView(newTree); //䅽 treePane˄ᬒᷥⱘᆍ఼˅䆒㕂 newTree Ўᮄⱘ㾚೒ this.tree = newTree; //ᇚ tree ᇍ䈵বЎখ᭄Ёⱘ newTree public void refreshTree(JTree newTree) { //䞡ᮄ೼ treePane Ёᵘ䗴ϔ⃵ᷥ ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\EditorFrame.java ԰ぎ䯈˅ˈ಴ℸˈ៥Ӏ೼ EditorFrame ЁᦤկϔϾࠋᮄᭈỉᷥⱘᮍ⊩ˈҹ֓Ў䖭Ͼᅲ⦄㉏᠔䇗⫼DŽ ⑤᭛ӊⳂᔩ˄src˅ˈJava ᭛ӊⱘ㓪䆥Ⳃᔩ˄classes˅ㄝDŽᮄᓎᅠ乍Ⳃৢˈ䖬䳔㽕ࠋᮄᭈỉᷥ˄ࠋᮄᎹ Ͼ乍ⳂⳂᔩˈ䖬䳔㽕ᮄᓎϔϾ name.project ⱘ乍Ⳃᇍᑨ᭛ӊˈ䖬䳔㽕ᮄᓎϔѯϢ乍ⳂⳌ݇ⱘⳂᔩˈ՟བ ᮄᓎ AddProjectHandler ㉏ˈԚ AddProjectHandler Ϣᮄᓎ᭛ӊǃⳂᔩⱘᅲ⦄᳝᠔ϡৠˈϡҙᰃᮄᓎϔ Ϣ 8.6.2 ⱘᮄᓎⳂᔩࡳ㛑ϔḋˈা䳔㽕ࡴ໮ϔϾ AddHandler ⱘᅲ⦄㉏ˈᑊЎ㦰ऩࡴܹⲥ਀఼ेৃDŽ 8.6.3 ᮄᓎ乍Ⳃ 䖭ḋⱘᬜᵰህ੠ᮄᓎ᭛ӊⱘᬜᵰϔḋˈাᰃᮄᓎⱘᰃϔϾⳂᔩDŽ ēರဈॴ AddFolderHandler ಬຣफdדᅟġ౨ੋӝ৲ᄯԅ۱඘Ϧ } showAddFrame(info); AddInfo info = new AddInfo("Ⳃᔩৡ⿄˖", this, new AddFolderHandler()); public void newFolder() { //ᮄᓎⳂᔩⱘᮍ⊩ }; } newFolder(); public void actionPerformed(ActionEvent e) { private Action folerNew = new AbstractAction("ᮄᓎⳂᔩ", new ImageIcon("images/newFile.gif")) { //ᮄᓎⳂᔩⱘ Action ᇍ䈵 ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\EditorFrame.java 䴶ҷⷕᅲ⦄њᮄᓎⳂᔩ Action ⱘᮍ⊩DŽ Ϟ䴶ⱘҷⷕᰃ⏏ࡴⳂᔩৢⱘᅲ⦄ˈৠḋⱘ೼᠔䗝㡖⚍ϟ߯ᓎϔϾⳂᔩˈ߯ᓎᅠৢݡࠋᮄ⠊㡖⚍DŽϟ } addFrame.setVisible(false);//䅽⏏ࡴⱘ frame ϡৃ㾕 editorFrame.setEnabled(true);//䅽 EditorFrame ৃ⫼ editorFrame.reloadNode(selectNode);//ࠋᮄᷥⱘ㡖⚍ newFolder.mkdir();//߯ᓎᮄⱘⳂᔩ File newFolder = new File(folder.getAbsoluteFile() + File.separator + data); //߯ᓎϔϾ᭛ӊⳂᔩᇍ䈵 } folder = parent.getFile(); selectNode = parent; //䅽ᔧࠡ᠔䗝ᢽⱘ᭛ӊⱘ⠊Ⳃᔩ԰Ўᔧࠡ䗝ᢽⱘⳂᔩ ProjectTreeNode parent = (ProjectTreeNode)selectNode.getParent(); if (!folder.isDirectory()) { /Download at http://www.pin5i.com ᅲ⦄ᅠϞ䴶ⱘҷⷕৢˈ៥Ӏএ䖤㸠 Main ㉏ˈेৃⳟࠄᬜᵰˈབ೒ 8.17 ੠೒ 8.18 ᠔⼎DŽ } showAddFrame(info); AddInfo info = new AddInfo("乍Ⳃৡ⿄˖", this, new AddProjectHandler()); public void newProject() { //ᮄᓎ乍Ⳃⱘᮍ⊩ }; } newProject(); public void actionPerformed(ActionEvent e) { private Action projectNew = new AbstractAction("ᮄᓎ乍Ⳃ", new ImageIcon("images/newFile.gif")) { //ᮄᓎ乍Ⳃⱘ Action ᇍ䈵 ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\EditorFrame.java ᷥDŽ᥹ϟᴹݡЎ EditorFrame ㉏Ёⱘᮄᓎ乍Ⳃ㦰ऩ⏏ࡴⲥ਀఼DŽ name.projectǃ⑤᭛ӊᄬᬒⳂᔩ੠㓪䆥Ⳃᔩৢˈݡ䇗⫼ EditorFrame Ёⱘࠋᮄᭈỉᷥⱘᮍ⊩এࠋᮄ乍Ⳃ Ϟ䴶ҷⷕЁⱘ咥ԧ䚼ߚߚ߿߯ᓎ name.project ᭛ӊǃ⑤᭛ӊᄬᬒⳂᔩ੠㓪䆥䕧ߎⳂᔩDŽᮄᓎᅠ } addFrame.setVisible(false); //䅽ᔧࠡ᠔䗝ᢽⱘ᭛ӊⱘ⠊Ⳃᔩ԰Ўᔧࠡ䗝ᢽⱘⳂᔩ editorFrame.setEnabled(true); //䅽 EditorFrame বᕫৃ⫼ editorFrame.refreshTree(newTree); JTree newTree = editorFrame.getTreeCreator().createTree(editorFrame); //ࠋᮄᭈỉᷥ output.mkdir(); src.mkdir(); //߯ᓎ src ੠ output ϸϾⳂᔩ File.separator + CompileConfig.OUTPUT_DIR); File output = new File(projectFolder.getAbsoluteFile() + //Java ᭛ӊ㓪䆥ⱘ䕧ܹⳂᔩ File.separator + CompileConfig.SRC_DIR); File src = new File(projectFolder.getAbsoluteFile() + //߯ᓎ乍Ⳃⱘ src Ⳃᔩ੠㓪䆥Ⳃᔩ if (!projectFolder.exists()) projectFolder.mkdir(); //乍ⳂⳂᔩϡᄬ೼ˈ ߯ᓎ乍Ⳃ᭛ӊⳂᔩ if (!projectFile.exists()) projectFile.createNewFile(); //乍Ⳃ᭛ӊϡᄬ೼ˈ ߯ᓎ乍Ⳃ᭛ӊ File projectFolder = new File(spaceFolder.getAbsoluteFile() + File.separator + data); //߯ᓎ乍ⳂⳂᔩ File.separator + data + ".project"); File projectFile = new File(spaceFolder.getAbsoluteFile() + //߯ᓎ.project ᭛ӊ File spaceFolder = editorFrame.getWorkSpace().getFolder();//㦋পᎹ԰ぎ䯈᠔೼ⱘⳂᔩ public void afterAdd(EditorFrame editorFrame, AddFrame addFrame, Object data) { ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\handler\add\AddProjectHandler.java ೒ 8.17 ᅲ⦄ᓎᮄ乍Ⳃ Download at http://www.pin5i.com/ /Download at http://www.pin5i.com AddHandler ⱘ݋ԧᅲ⦄ˈা䳔㽕೼ EditorFrame ᵘ䗴ϡৠⱘ AddHandler 㒭 AddFrame ेৃDŽ ৢ䇗⫼䆹᥹ষⱘ afterAdd ᮍ⊩DŽ೼䖭䞠ㄪ⬹῵ᓣ᠔ԧ⦄ⱘӬ࢓ህᰃ AddFrame ϡ䳔㽕এ⧚Ӯ খϢ㗙ˈህᰃ៥Ӏࠡ䴶᠔ݭⱘ AddFrame ㉏ˈ䆹㉏㓈ᡸϔϾ AddHandler ᥹ষˈᑊ೼ᮄ๲⬠䴶⚍ߏ⹂ᅮ ⱘᅲ⦄㉏ࣙᣀ˖AddFileHandlerˈAddFolderHandlerˈAddProjectHandlerDŽ೼ㄪ⬹῵ᓣЁⱘ Context ៥Ӏ೼ 8.6.1 Ёᮄᓎ᭛ӊᯊˈ៥ӀህᮄᓎњϔϾ AddHandler ⱘ᥹ষˈᑊᇍ݊䖯㸠݋ԧϡৠⱘᅲ⦄ˈᅗ ‰ Context˖Փ⫼ϔϾ Strategy 䖯㸠䜡㕂ˈ㓈ᡸϔϾᇍ Strategy ᇍ䈵ⱘᓩ⫼DŽ ‰ ConcreteStrategy˖ҹ Strategy Ў᥹ষᅲ⦄ᶤϾ݋ԧⱘㅫ⊩DŽ ‰ Strategy˖ᅮНㅫ⊩ⱘ݀݅᥹ষˈ䅽 Context ᴹ䇗⫼ᶤϾ ConcreteStrategy ᅮНⱘㅫ⊩DŽ ㄪ⬹῵ᓣⱘখϢ㗙Џ㽕᳝˖ 䆒䅵῵ᓣЁⱘㄪ⬹῵ᓣDŽ ࠄℸˈ៥ӀᏆ㒣ᅲ⦄њᮄᓎ᭛ӊǃⳂᔩ੠乍Ⳃⱘࡳ㛑ˈ೼䖭Ͼ䖛⿟Ёˈ៥Ӏϡⶹϡ㾝ⱘՓ⫼ 23 ⾡ 8.6.4 ㄪ⬹῵ᓣ Ꮉ԰ぎ䯈ৢˈ䆹乍Ⳃህৃҹ೼乍ⳂᷥЁᰒ⼎DŽ ߯ᓎᅠ乍ⳂৢˈIDE Ӯᐂ៥Ӏ߯ᓎ src ੠ classes Ⳃᔩˈᑊ߯ᓎ name.project ⱘ᭛ӊˈϟ⃵䖯ܹ䆹 ೒ 8.18 ᮄᓎ乍Ⳃৢࠋᮄᭈỉᷥ /Download at http://www.pin5i.com 䖤㸠 Main ㉏ˈݡ⚍ߏᶤϾ᭛ӊ㡖⚍ˈेৃⳟࠄབ೒ 8.19 ⱘᬜᵰDŽ } tabPane.setSelectedIndex(tabPane.getTabCount() - 1); tabPane.addTab(file.getName(), null, null, file.getAbsolutePath()); //೼ tabPane Ё⏏ࡴϔϾ tab 义ˈtab 义䴶ⱘᷛ乬Ў᭛ӊৡ⿄ˈtab ⱘ tips Ў᭛ӊⱘ㒱ᇍ䏃ᕘ iframe.reshape(0, 0, 400, 300); //䆒㕂 iframe ⱘԡ㕂Ϣ໻ᇣ iframe.show(); desk.add(iframe); iframe.add(new JScrollPane(editPane)); EditPane editPane = new EditPane(file); //ᮄᓎϔϾ EditPane ᇍ䈵 JInternalFrame iframe = new JInternalFrame(file.getAbsolutePath(), true, true, true, true); //߯ᓎϔϾ JInternalFrame ᇍ䈵ˈtitle Ў᭛ӊⱘ㒱ᇍ䏃ᕘ setTitle(file.getAbsolutePath()); //䆒㕂 EditorFrame ⱘᷛ乬Ў䆹᭛ӊⱘܼ䏃ᕘ public void openFile(File file) { //ᠧᓔ᭛ӊⱘᮍ⊩ ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\EditorFrame.java 䕥ᇍ䈵ˈᮄᓎ JInternalFrame ᇍ䈵ˈ᳔ৢ䆒㕂 JTabbedPane ᇍ䈵DŽ EditPane ᠔䳔㽕ᰒ⼎ⱘݙᆍⱘᴹ⑤᭛ӊDŽᅲ⦄ EditorFrame ⱘ openFile ᮍ⊩ˈᮄᓎにԧˈᮄᓎ᭛ᴀ㓪 EditPane ᦤկϔϾᵘ䗴఼ˈখ᭄ЎϔϾ File ᇍ䈵ˈ㸼⼎䖭Ͼ EditPane ᠔ᇍᑨⱘ᭛ӊˈгህᰃ䖭Ͼ } } public EditPane(File file){ public class EditPane extends JTextPane { ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\EditPane.java JTextPane ⱘᄤ㉏ˈ㸼⼎ϔϾ᭛ᴀⱘ㓪䕥ඳᇍ䈵DŽ䆹ᇍ䈵䳔㽕ᅲ⦄ ೼ᮄᓎ᭛ᴀ㓪䕥ᇍ䈵ⱘᯊ׭ˈ䳔㽕ᕫࠄ㹿ᠧᓔⱘ᭛ӊˈݡՓ⫼ IO 䇏প᭛ӊⱘݙᆍDŽϟ䴶ᮄᓎϔϾ ‰ ݡ䆒㕂ᇍᑨⱘ JTabbedPane ᇍ䈵 ‰ ᇚᮄᓎⱘ JInternalFrame ᇍ䈵ᬒࠄ EditorFrame Ёⱘ JDesktopPane ᆍ఼Ё ‰ ᮄᓎϔϾ᭛ᴀ㓪䕥ᇍ䈵ˈ៥Ӏ䖭䞠Փ⫼ JTextPane ㉏ˈᑊᇚ݊ᬒࠄ JInternalFrame Ё ‰ ᮄᓎϔϾにԧᇍ䈵ˈՓ⫼ JInternalFrame ㉏ ⱘџᚙ˖خ៥Ӏ䳔㽕ᯢⱑˈᠧᓔ᭛ӊᯊˈIDE 䳔㽕ܜ佪 Ͼᮍ⊩ˈ໘⧚᭛ӊⱘᠧᓔˈᑊᰒ⼎೼㓪䕥ऎЁDŽ Ёⱘ openFile ᮍ⊩ᠧᓔϔӑ᭛ӊˈ೼ 8.5.4 Ёᑊ≵᳝এᅲ⦄ openFile ᮍ⊩ˈ೼ᴀᇣ㡖ЁᇚӮএᅲ⦄䖭 ೼ᴀゴ 8.5.4 ЁˈᏆ㒣ᅲ⦄њᷥ㡖⚍ⱘⲥ਀఼ˈ⚍ߏњᷥⱘᶤϾ㡖⚍ⱘᯊ׭ˈህ䇗⫼ EditorFrame ᭛ӊᠧᓔ 8.6.5 /Download at http://www.pin5i.com public EditPane(File file) { ✊ৢׂᬍ EditPane ⱘᵘ䗴఼˖ } } return result.toString(); fis.close(); } result.append(content); content = new String(arr, 0, readLength); while ((readLength = fis.read(arr)) > 0) { int readLength ; byte[] arr = new byte[1024]; String content = null; FileInputStream fis= new FileInputStream(file); //Փ⫼ IO ⌕䇏প᭛ӊ StringBuffer result = new StringBuffer(); public static String readFile(File file) { //䇏পϔϾ᭛ӊⱘݙᆍ public class FileUtil { ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\util\FileUtil.java প᭛ӊˈᑊ䖨ಲ᭛ӊݙᆍDŽ ᮄᓎњ tab 义Ϣ EditPane ৢˈ䳔㽕ḍ᥂᭛ӊᴹᰒ⼎ᇍᑨⱘݙᆍˈᮄᓎϔϾ FileUtil ㉏ˈϧ䮼⫼ᴹ䇏 ೒ 8.19 ᠧᓔ᭛ӊᬜᵰ /Download at http://www.pin5i.com //ⳕ⬹ᵘ䗴఼ҷⷕ EditPane editPane) { public EditFile(File file, boolean saved, JInternalFrame iframe, private EditPane editPane; //䆹᭛ӊ᠔ᇍᑨⱘ㓪䕥఼ private JInternalFrame iframe; //䆹᭛ӊᇍᑨⱘにষ private boolean saved; //䆹᭛ӊᰃ৺Ꮖ㒣㹿ֱᄬ private File file; //ᔧࠡ㓪䕥ⱘ᭛ӊ public class EditFile { ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\commons\EditFile.java 㛑DŽᮄᓎϔϾ EditFile ⱘ㉏ˈ㸼⼎ϔϾ㓪䕥ⱘ᭛ӊᇍ䈵DŽ ᭛ӊ䯈ⱘߛᤶˈׂᬍњ᭛ӊৢˈ݇䯁ᇍᑨⱘ JInternalFrame ᆍ఼ᯊ䳔㽕ᔍߎᦤ⼎ˈ䆶䯂ᰃ৺㽕ֱᄬㄝࡳ ᅲ⦄њᠧᓔ㦰ऩⱘࡳ㛑ⱘৢˈ៥Ӏ䳔㽕এӬ࣪ᠧᓔ᭛ӊⱘࡳ㛑ˈ՟བᠧᓔৢ໮ӑ᭛ӊৢˈ䳔㽕䖯㸠 ࠋԤdۥē༓ྑಁᄢ೮ԅӲ஍༪႔ࠋԤนࣂē௾೮Ϣ༪ᄯఉדᅟġྻ౨ӝ৲ԅ۱඘Ϧ } } super.approveSelection(); this.editorFrame.openFile(file); this.editorFrame.getTree().setSelectionPath(null); //䆒㕂ᷥᔧࠡ䗝ᢽⱘ㡖⚍Ў null, 䅽ᷥ≵᳝㹿䗝Ё File file = getSelectedFile(); public void approveSelection() { } showOpenDialog(editorFrame); this.editorFrame = editorFrame; super(editorFrame.getWorkSpace().getFolder()); //߽⫼ editorFrame ⱘᎹ԰ぎ䯈԰Ў᭛ӊ䗝ᢽ఼ᠧᓔᯊⱘ咬䅸Ⳃᔩ //䇗⫼⠊㉏ⱘᵘ䗴఼ public FileChooser(EditorFrame editorFrame){ private EditorFrame editorFrame; class FileChooser extends JFileChooser { } fileChooser = new FileChooser(this); public void selectFile() { }; } selectFile(); public void actionPerformed(ActionEvent e) { private Action open = new AbstractAction("ᠧᓔ", new ImageIcon("images/open.gif")) { //ᠧᓔ᭛ӊⱘ Action ᇍ䈵 private FileChooser fileChooser; //᭛ӊ䗝ᢽ఼ ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\EditorFrame.java ӊњDŽ೼ EditorFrame.java ЁᮄᓎϔϾ FileChooser ㉏DŽ 䗮䖛᭛ӊ䗝ᢽ఼ᕫࠄ䳔㽕ᠧᓔⱘ᭛ӊˈݡ䇗⫼ EditorFrame ⱘ openFile ህৃҹᅲ⦄ᠧᓔ䴲乍ⳂЁⱘ᭛ ⱘᠧᓔ᭛ӊࡳ㛑ˈ䆹ࡳ㛑াᰃ䗮䖛ϔϾ᭛ӊ䗝ᢽ఼ᴹᠧᓔ᭛ӊˈᠧᓔ᭛ӊⱘ᪡԰೼Ϟ䴶Ꮖ㒣ᰒ⼎ˈা㽕 䞡ᮄ䖤㸠ህৃҹⳟࠄ᭛ӊⱘݙᆍᏆ㒣㹿䇏পˈᑊᬒ㕂ࠄᇍᑨⱘ EditPane ЁDŽ᥹ϟᴹˈᅲ⦄㦰ऩЁ } this.setText(FileUtil.readFile(file)); 䆒㕂ᔧࠡ᭛ᴀЎ᭛ӊⱘݙᆍ// /Download at http://www.pin5i.com private EditFile getOpenFile(File file) { //Ңᴀ㉏Ё䆄ᔩᏆ㒣ᠧᓔⱘ᭛ӊ䲚ড়ЁᕫࠄϢখ᭄ file ϔḋⱘ EditFile ᇍ䈵 } openNewFile(file); //ᠧᓔᮄⱘ᭛ӊ } return; openExistFile(openedFile, file); if (openedFile != null) { //བᵰ᭛ӊᏆ㒣ᠧᓔњˈ䇗⫼ openExistFile ᮍ⊩ EditFile openedFile = getOpenFile(file); //೼ᠧᓔ᭛ӊⱘ䲚ড়Ёᶹᡒ䆹᭛ӊ, ݡ߸ᮁ䆹᭛ӊᰃ৺Ꮖ㒣ᠧᓔ } if (file.equals(currentFile.getFile())) return; //བᵰখ᭄ file ᰃᔧࠡℷ೼㓪䕥ⱘ᭛ӊˈ䖨ಲ if (currentFile != null) { public void openFile(File file) { //ᠧᓔ᭛ӊⱘᮍ⊩ } iframe.toFront(); iframe.setSelected(true); public void showIFrame(JInternalFrame iframe) { //ᰒ⼎ iframe ᇍ䈵 ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\EditorFrame.java ⼎ᇚ㹿ᠧᓔⱘ᭛ӊ᠔ᇍᑨⱘにষ˄JInternalFrame˅DŽ ᭛ӊᏆ㒣㹿ᠧᓔњˈ䙷Мህߛᤶ tab 义ˈᑊ䱤㮣Пࠡ㓪䕥ⱘ᭛ӊ᠔ᇍᑨⱘにষ˄JInternalFrame˅ˈݡᰒ ˄openFiles˅DŽᔧᠧᓔϔӑ᭛ӊᯊˈ䳔㽕߸ᮁ䆹ӑ᭛ӊᰃ৺Ꮖ㒣ᄬ೼Ѣ᭛ӊ߫㸼ЁDŽབᵰᇚ㽕㹿ᠧᓔⱘ ҹϞҷⷕЎ EditorFrame 䆒㕂њᔧࠡᠧᓔⱘ᭛ӊ˄currentFile˅ˈᑊ䆄ᔩњ᠔᳝ᠧᓔⱘ᭛ӊ … } this.openFiles.add(currentFile); //⏏ࡴࠄᠧᓔ᭛ӊⱘ䲚ড়Ё this.currentFile = currentFile; //䆒㕂ᔧࠡᠧᓔⱘ᭛ӊ EditFile currentFile = new EditFile(file, true, iframe, editPane); //ᵘ䗴ϔϾᔧׂࠡᬍⱘ᭛ӊ ... public void openFile(File file) { //ᠧᓔ᭛ӊⱘᮍ⊩ private List openFiles = new ArrayList();//ᠧᓔ᭛ӊⱘ䲚ড় private EditFile currentFile; //ᔧࠡℷ೼㓪䕥ⱘ᭛ӊᇍ䈵 ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\EditorFrame.java ЁᮄᓎϔϾ䲚ড়ˈ⫼Ѣֱᄬ᠔᳝ᠧᓔⱘ᭛ӊᇍ䈵DŽ ᥹ϟᴹЎ EditorFrame ⏏ࡴ EditFile ሲᗻˈ䆹ሲᗻ㸼⼎ᔧࠡℷ೼㓪䕥ⱘ᭛ӊᇍ䈵DŽᑊЎ EditorFrame EditPaneDŽ JInternalFrame ᇍ䈵˄䆹᭛ӊᠧᓔᯊ߯ᓎⱘ JInternalFrame˅ˈ䖬᳝䆹᭛ӊ᠔ᇍᑨⱘ᭛ᴀ㓪䕥఼ᇍ䈵 䆹㉏Ёࣙ৿њϔϾ᭛ӊᇍ䈵ˈϔϾ㸼⼎ᰃ৺㹿ֱᄬⱘᏗᇨؐˈϔϾ䆹᭛ӊᇍ䈵᠔ᇍᑨⱘ { { for (EditFile openFile : openFiles) { if (openFile.getFile().equals(file)) return openFile; } return null; } //ḍ᥂খ᭄ file 㦋প䆹 file ᇍᑨⱘ tab 义Ёⱘ㋶ᓩ private int getFileIndex(File file) { //བᵰҢᠧᓔ᭛ӊⱘ䲚ড়ЁᡒϡࠄϢখ᭄ file ᇍᑨⱘ EditFile ᇍ䈵ˈ䖨ಲ-1 EditFile openFile = getEditFile(file); if (openFile == null) return -1; return getTabIndex(openFile.getIframe().getToolTipText()); } //೼ᠧᓔⱘ᭛ӊЁ㦋ᕫ᭛ӊᰃ file ⱘ EditFile ᇍ䈵 private EditFile getEditFile(File file) { for (EditFile openFile : openFiles) { if (openFile.getFile().equals(file)) return openFile; } return null; } //ḍ᥂ᄫヺІ tips এ tabPane Ёᇏᡒᇍᑨⱘ㋶ᓩ public int getTabIndex(String tips) { for (int i = 0; i < this.tabPane.getTabCount(); i++) { if (this.tabPane.getToolTipTextAt(i).equals(tips)) return i; } return -1;//খ᭄ tips ≵᳝೼ tabPane Ёˈ䖨ಲ-1 } //ᠧᓔᏆ㒣ᄬ೼ⱘ᭛ӊ˄Ꮖ㒣೼㓪䕥᭛ӊ䲚ড়Ёⱘ᭛ӊ˅ˈopenFiles 䲚ড়Ёⱘ᭛ӊ public void openExistFile(EditFile openedFile, File willOpenFile) { //ᇚ tab 义ব៤ᔧࠡ᠔䗝ᢽ᭛ӊⱘ㋶ᓩ tabPane.setSelectedIndex(getFileIndex(willOpenFile)); //ᰒ⼎ iframe showIFrame(openedFile.getIframe()); //䆒㕂ᔧࠡᠧᓔ᭛ӊ this.currentFile = openedFile; //⏏ࡴࠄᔧࠡᠧᓔⱘ᭛ӊ䲚ড়Ё this.openFiles.add(openedFile); } //ᠧᓔϔϾϡ೼㓪䕥䲚ড়Ёⱘ᭛ӊ public void openNewFile(File file) { setTitle(file.getAbsolutePath());//䆒㕂 EditorFrame ⱘᷛ乬Ў䆹᭛ӊⱘܼ䏃ᕘ //߯ᓎϔϾ JInternalFrame ᇍ䈵ˈtitle Ў᭛ӊⱘ㒱ᇍ䏃ᕘ JInternalFrame iframe = new JInternalFrame(file.getAbsolutePath(), true, true, true, true); EditPane editPane = new EditPane(file); //ᮄᓎϔϾ EditPane ᇍ䈵 iframe.add(new JScrollPane(editPane)); desk.add(iframe); iframe.show(); iframe.reshape(0, 0, 400, 300); tabPane.addTab(file.getName(), null, null, file.getAbsolutePath()); tabPane.setSelectedIndex(tabPane.getTabCount() - 1); //䆒㕂ᔧࠡⱘ᭛ӊᇍ䈵 Download at http://www.pin5i.com/ /Download at http://www.pin5i.com } if (openFile.getIframe().equals(iframe)) return openFile; for (EditFile openFile : openFiles) { public EditFile getEditFile(JInternalFrame iframe) { //ḍ᥂ JInternalFrame ೼ᠧᓔⱘ᭛ӊ䲚ড়Ё㦋পⳌᑨⱘ᭛ӊᇍ䈵 } return null; } if (iframe.getTitle().equals(title)) return iframe; for (JInternalFrame iframe : iframes) { JInternalFrame[] iframes = desk.getAllFrames(); public JInternalFrame getIFrame(String title) { //ḍ᥂ JInternalFrame ᷛ乬ᡒࠄ JInternalFrame ᇍ䈵 ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\EditorFrame.java ҹϞҷⷕⱘ咥ԧ䚼ߚˈ೼ EditorFrame Ёᅲ⦄䖭ϸϾᮍ⊩DŽ } } editorFrame.setCurrentFile(currentFile); //䆒㕂 EditorFrame ᔧࠡ㓪䕥ⱘ᭛ӊЎ tab ᇍᑨⱘ᭛ӊ EditFile currentFile = editorFrame.getEditFile(currentFrame); //ḍ᥂ᔧࠡⱘ JInternalFrame ᇍ䈵㦋ᕫᇍᑨⱘ᭛ӊ editorFrame.showIFrame(currentFrame); //䅽ᔧࠡ⚍ߏⱘ JInternalFrame ᇍ䈵ৃ㾕 JInternalFrame currentFrame = editorFrame.getIFrame(tab.getToolTipTextAt(index)); //ḍ᥂ tab 义ⱘ tips(᭛ӊⱘ㒱ᇍ䏃ᕘ)㦋ᕫᔧࠡⱘ JInternalFrame ᇍ䈵 if (index == -1) return; int index = tab.getSelectedIndex(); //㦋ᕫ tab 义ⱘ㋶ᓩ JTabbedPane tab = (JTabbedPane)e.getSource(); //㦋ᕫᔧࠡ⚍ߏ tab 义ᇍ䈵 public void stateChanged(ChangeEvent e) { } this.editorFrame = editorFrame; public TabListener(EditorFrame editorFrame) { private EditorFrame editorFrame; //Џ⬠䴶ⱘ IFrame public class TabListener implements ChangeListener { ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\TabListener.java ᬍবˈ៥ӀৃҹЎ tab 义˄tabPane˅⏏ࡴⲥ਀఼ TabListenerˈᔧ⚍ߏњ tab ᯊˈՓ݊থ⫳ⳌᑨⱘᬍবDŽ ᅲ⦄њ᭛ӊⱘᠧᓔৢˈ䖤㸠 Main ㉏ৃⳟࠄᬜᵰˈৃҹথ⦄⚍ߏ tab 义ᯊˈᇍᑨⱘにষᑊ≵᳝থ⫳ 䆒㕂ᔧࠡᠧᓔ᭛ӊⱘᇍ䈵ˈݡᇚ䆹ᇍ䈵ᬒࠄ openFiles 䲚ড়ЁDŽ ᑊ᥻ࠊにষᰒ⼎ेৃˈ᳔ৢ䆒㕂ᔧࠡᠧᓔⱘ᭛ӊ˄currentFile˅DŽopenNewFile ᮍ⊩Џ㽕䋳䋷߯ᓎにষˈ ᮍ⊩Џ㽕ᰃҢ openFiles 䲚ড়Ё㦋পᇍᑨⱘ EditFile ᇍ䈵ˈᕫࠄ䆹ᇍ䈵ৢৃҹᕫࠄᇍᑨⱘ JInternalFrame བᵰᏆ㒣ᠧᓔњˈ߭䇗⫼ openExistFile ᮍ⊩ˈབᵰ≵᳝㹿ᠧᓔˈ߭䇗⫼ openNewFile ᮍ⊩DŽopenExistFile ⊼ᛣˈҹϞҷⷕⱘ咥ԧ䚼ߚˈ߸ᮁᇚ㹿ᠧᓔⱘ᭛ӊᰃ৺Ꮖ㒣㹿ᠧᓔ˄ᄬ೼Ѣ openFiles 䲚ড়Ё˅ˈ } this.openFiles.add(this.currentFile); //ᇚᔧࠡⱘ᭛ӊࡴࠄᠧᓔⱘ᭛ӊ䲚ড়Ё this.currentFile = new EditFile(file, true, iframe, editPane); /Download at http://www.pin5i.com } } } saveFile(file); //䇗⫼ EditorFrame ⱘֱᄬᮍ⊩ᇍ᭛ӊ䖯㸠ֱᄬ if (JOptionPane.YES_OPTION == val) { //⚍ߏњ䳔㽕ֱᄬ JOptionPane.YES_NO_OPTION); int val = JOptionPane.showConfirmDialog(this, "ᰃ৺㽕ֱᄬ˛", "䆶䯂", //ᔍߎ䆶䯂 if (!file.isSaved()) { //䆹ӑ᭛ӊׂᬍ䖛≵ֱ᳝ᄬ public void askSave(EditFile file) { //䆶䯂ᰃ৺㽕ֱᄬ, খ᭄Ўᇚ㹿ᠧᓔⱘ᭛ӊ ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\EditorFrame.java ៥Ӏ೼ EditorFrame Ёᅲ⦄䖭ϸϾᮍ⊩DŽ ೼にষ݇䯁ⱘᯊ׭䆶䯂ᰃ৺㽕ֱᄬ䆹᭛ӊˈݡᠻ㸠݇䯁にষⱘᮍ⊩ˈܜˈ⊼ᛣϞ䴶ҷⷕЁⱘ咥ԧ䚼ߚ } } editorFrame.closeIFrame(iframe); //݇䯁ᔧࠡⱘ iframe editorFrame.askSave(editFile); //䆶䯂ᰃ৺㽕ֱᄬ EditFile editFile = editorFrame.getCurrentFile(); //㦋পᔧࠡ᪡԰ⱘ᭛ӊ JInternalFrame iframe = (JInternalFrame)e.getSource(); //㦋পᔧࠡ݇䯁᪡԰᠔ᇍᑨⱘ JInternalFrame public void internalFrameClosing(InternalFrameEvent e) { //にষ݇䯁ᠻ㸠ⱘᮍ⊩ } editorFrame.getTabPane().setSelectedIndex(tapIndex); int tapIndex = editorFrame.getTabIndex(iframe.getTitle()); JInternalFrame iframe = editorFrame.getDesk().getSelectedFrame(); public void internalFrameActivated(InternalFrameEvent e) { //にষ▔⌏ᠻ㸠ⱘᮍ⊩ } this.editorFrame = editorFrame; public IFrameListener(EditorFrame editorFrame) { private EditorFrame editorFrame; //Џ⬠䴶ᇍ䈵 public class IFrameListener extends InternalFrameAdapter { ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\IFrameListener.java ᮄᓎϔϾ IFrameListener ㉏ˈ԰Ўにষⱘⲥ਀఼DŽ にষ㹿▔⌏៪㗙㹿݇䯁ᯊˈ䳔㽕⏏ࡴϔѯ㸠Ўˈ՟བߛᤶ tab 义䴶ˈ݇䯁ᯊᔍߎ䆶䯂ᰃ৺㽕ֱᄬㄝ㸠ЎDŽ 䖤㸠 Main ㉏ৃⳟࠄᬜᵰˈ⚍ߏ tab 义ˈৃⳟࠄにষ䯈ⱘߛᤶDŽ᥹ϟᴹᅲ⦄にষᇍ䈵ⱘⲥ਀఼ˈᔧ tabPane.addChangeListener(new TabListener(this)); //Ў tabPane ⏏ࡴⲥ਀఼ ܹབϟҷⷕ˖ ᅲ⦄њ tab 义ⱘⲥ਀఼ৢˈЎ tabPane ᇍ䈵䆒㕂ⲥ਀఼DŽ೼ EditorFrame ⱘ addListeners ᮍ⊩Ёࡴ } return null; /Download at http://www.pin5i.com JInternalFrame ᇍ䈵⏏ࡴⲥ਀఼DŽ ೼ EditorFrame ⱘᵘ䗴఼Ё߯ᓎ IFrameListener ᇍ䈵ˈᑊ೼ openNewFile ᮍ⊩ЁЎᮄᓎⱘ Ⴣࠋಬຣ൑d ԅੋ܊Đē؞ֺ֥ႀನඔ٢ࣂಬຣdแਬߜၽד༓ྑͬӉē႕ᄓ໻ saveFile ֺ֥ďྻ౨ӝ৲ԅ۱඘Ϧڴ ำߑďEditFileĐԅ saved ೫໿ࢅՇēఢހᅟġӝ৲ᄯԅ askSave ֺ֥ēಾ׮Ӱѻ༸ูಾٔ࡬Ӳ஍Ω } return -1; } if (openFiles.get(i).equals(editFile)) return i; for (int i = 0; i < this.openFiles.size(); i++) { private int getEditFileIndex(EditFile editFile) { //㦋প editFile ೼ᠧᓔⱘ᭛ӊ䲚ড়Ёⱘ㋶ᓩ } } } this.currentFile = openFiles.get(openFilesIndex - 1); //ϡᰃ㄀ϔӑˈгϡᰃ᳔ৢϔӑ } else { this.currentFile = openFiles.get(openFiles.size() - 2); ᭄㄀Ѡӑצ //བᵰ݇䯁ⱘᰃ᳔ৢϔӑˈপ } else if (openFilesIndex == (openFiles.size() - 1)) { this.currentFile = openFiles.get(openFilesIndex + 1); //བᵰ݇䯁ⱘ᭛ӊᰃ㄀ϔӑˈᣓ䲚ড়Ёⱘ㄀Ѡӑ if (openFilesIndex == 0) { } else {//བᵰ䖬᳝݊Ҫ᭛ӊˈ߸ᮁ݇䯁ⱘ᭛ӊԡ㕂 this.currentFile = null; if (this.openFiles.size() == 1) { //བᵰ䆹᭛ӊᏆ㒣ᰃ᠔᳝ᠧᓔⱘ᭛ӊⱘ᳔ৢϔӑ int openFilesIndex = getEditFileIndex(closeFile); //㦋প݇䯁᭛ӊ೼ᠧᓔ᭛ӊ䲚ড়Ёⱘ㋶ᓩ private void afterClose(EditFile closeFile) { //ᔧ݇䯁ϔӑ᭛ӊৢˈ䆒㕂ᴀᇍ䈵ⱘ currentFile ሲᗻ } openFiles.remove(closeFile); //Ңᠧᓔⱘ᭛ӊ䲚ড়Ёߴ䰸䖭Ͼ݇䯁ⱘ᭛ӊ getTabPane().remove(index); //Ң tab 义Ёߴ䰸 int index = getTabIndex(iframe.getTitle()); //㦋ᕫ䆹 iframe ೼ tab 义Ёᇍᑨⱘ㋶ᓩ afterClose(closeFile); //䆒㕂ᴀ㉏Ёⱘ currentFile ሲᗻ EditFile closeFile = getEditFile(iframe); //㦋ᕫᔧࠡⱘ᭛ӊˈे㽕݇䯁ⱘ᭛ӊᇍ䈵 public void closeIFrame(JInternalFrame iframe) { //݇䯁ϔϾにষ } //᱖ᯊᦤկぎᅲ⦄ public void saveFile(EditFile file) { ᄬ᭛ӊⱘᮍ⊩ֱ// /Download at http://www.pin5i.com super.paint(g); public void paint(Graphics g){ //䞡⬏䆹㒘ӊˈ䆒㕂㸠ো } this.setMargin(new Insets(3, 40, 0, 0)); //䆒㕂䆹᭛ḷⱘ义䖍䎱 this.doc = getStyledDocument();//㦋প䖭Ͼ㓪䕥఼ⱘ῵ൟ᭛ḷ this.setText(FileUtil.readFile(file)); public EditPane(File file) { private SimpleAttributeSet lineAttr = new SimpleAttributeSet();//ᅮН᭛ḷЁ㸠᭄᭛ᴀⱘ໪㾖ሲᗻ protected StyledDocument doc; //ḋᓣ᭛ḷᇍ䈵 ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\EditPane.java এᅲ⦄↨䕗ㅔऩⱘ㸠োࡳ㛑DŽ ܜIDEˈབ EclipseǃEditPlus ㄝˈ䛑᳝᳝ᰒ⼎㸠᭄ˈ催҂ᰒ⼎ⱘࡳ㛑ˈᴀᇣ㡖ᅲ⦄䖭ϸϾᐌ㾕ⱘࡳ㛑DŽ ೼ 8.6.5 Ё៥Ӏᅲ⦄њ᭛ӊⱘᠧᓔࡳ㛑ˈᑊৃҹ೼ EditPane Ёᰒ⼎ᇍᑨⱘݙᆍˈᇍѢϔѯᐌ㾕ⱘ 8.6.6 ᰒ⼎㸠᭄Ϣ催҂ ᱖ᯊᰃぎᅲ⦄ˈ಴ℸ⹂ᅮֱᄬ≵᳝ӏԩᬜᵰDŽ 䞡ᮄ䖤㸠 Main ㉏ˈᇍᠧᓔⱘϔӑ᭛ӊ԰ߎׂᬍˈৃҹⳟࠄᔍߎⱘᦤ⼎DŽ⬅Ѣ៥Ӏֱᄬ᭛ӊⱘᮍ⊩ } ... editPane.getDocument().addDocumentListener(new EditDocumentListener(this)); //Ў EditPane ⏏ࡴ䬂Ⲭⲥ਀఼ EditPane editPane = new EditPane(file); //ᮄᓎϔϾ EditPane ᇍ䈵 ... public void openNewFile(File file) { //ᠧᓔϔϾϡ೼㓪䕥䲚ড়Ёⱘ᭛ӊ ᮍ⊩DŽҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\EditorFrame.java ҷⷕЁⱘ咥ԧ䚼ߚˈ䆒㕂ᔧࠡ㓪䕥᭛ӊⱘ saved ሲᗻЎ falseDŽৠᯊׂᬍ EditFrame ⱘ openNewFile } public void removeUpdate(DocumentEvent e) {} } this.editorFrame.getCurrentFile().setSaved(false); //䆒㕂ᔧࠡ㓪䕥ⱘ᭛ӊⱘ saved ሲᗻЎ false public void insertUpdate(DocumentEvent e) { public void changedUpdate(DocumentEvent e) {} } this.editorFrame = editorFrame; public EditDocumentListener(EditorFrame editorFrame) { private EditorFrame editorFrame; //Џ⬠䴶ᇍ䈵 public class EditDocumentListener implements DocumentListener { ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\EditDocumentListener.java ⱘ saved ሲᗻЎ falseDŽᮄᓎϔϾ EditDocumentListener ㉏DŽ ሲᗻህϔⳈᰃ trueˈ៥Ӏ䳔㽕Ў EditPane ⏏ࡴϔϾⲥ਀఼ˈ೼᭛ᴀⱘݙᆍথ⫳ᬍবᯊˈ䆒㕂 EditFile ⱘᯊ׭ˈҡ✊᮴⊩ᔍߎ䆶䯂ˈ䖭ᰃ⬅Ѣ EditFile ⱘ saved Ң≵᳝㹿ᬍব䖛ˈ೼߯ᓎ䖭Ͼᇍ䈵ⱘᯊ׭ˈ䆹 Ⳍᑨⱘߛᤶˈ䖭ህᰃ៥Ӏ᠔䳔㽕ⱘᬜᵰˈԚᰃˈेՓᇍ᭛ӊ䖯㸠њׂᬍˈ೼݇䯁にষخݙᆍ䱣ⴔ⚍ߏ㗠 ᅲ⦄њにষⱘⲥ਀఼ৢˈৃҹ䖤㸠 Main ㉏ˈ⚍ߏ乍ⳂᷥϞⱘ৘Ͼ㡖⚍ˈৃҹⳟࠄ tab 义Ϣᰒ⼎ⱘ iframe.addInternalFrameListener(this.iframeListener); Ў JInternalFrame ⏏ࡴにষⲥ਀఼// /Download at http://www.pin5i.com //䆒㕂ℷᐌ᭛ᴀⱘ买㡆ǃ໻ᇣ public SyntaxFormatter(String syntaxFile) { SimpleAttributeSet normalAttr = new SimpleAttributeSet(); //ᅮН᭛ḷⱘℷᐌ᭛ᴀⱘ໪㾖ሲᗻ = new HashMap(); private Map attMap //ҹϔϾ Map ֱᄬ݇䬂ᄫ੠买㡆ⱘᇍᑨ݇㋏ class SyntaxFormatter { ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\EditPane.java ѯ݇䬂ᄫˈݡՓ⫼ SyntaxFormatter 䖯㸠催҂ᰒ⼎DŽ ߚᵤˈᇚ stx ᭛ӊЁᅮНⱘ买㡆Ϣ݇䬂ᄫ݇㘨䍋ᴹˈ೼ EditPane Ёᇍᅗ䞠䴶ⱘ᭛ᴀ䖯㸠ߚᵤˈ䘛ࠄᶤ ೼ EditPane.java ЁᮄᓎϔϾ SyntaxFormatter ㉏ˈ⫼Ѣ䇏প stx ᭛ӊˈՓ⫼ Scanner ㉏䖯䆹᭛ӊ䖯㸠 ϟ䴶এᅲ⦄催҂ࡳ㛑ˈ催҂ࡳ㛑ˈгህᰃᇚᶤѯᐌ⫼ⱘ݇䬂ᄫՓ⫼ϡৠⱘ买㡆ᰒ⼎೼᭛ᴀ㓪䕥఼ЁDŽ ೒ 8.20 ᅲ⦄ᰒ⼎㸠᭄ࡳ㛑 䖤㸠 Main ᑊᠧᓔϔӑ᭛ӊˈৃҹⳟࠄᬜᵰབ೒ 8.20 ᠔⼎DŽ } } (int)((count + 1) * 1.5020 * StyleConstants.getFontSize(lineAttr))); g.drawString(String.valueOf(j), 3, for (int count = 0, j = 1; count <= line; count++, j++) { //↣㸠㒬ࠊϔϾ㸠ো g.setColor(new Color(40, 40, 40)); //䆒㕂㸠োⱘ买㡆 g.fillRect(0, 0, this.getMargin().left - 10, getSize().height); //㒬ࠊ㸠᭄ⶽᔶḚ g.setColor(new Color(230, 230, 230)); //䆒㕂买㡆 int line = root.getElementIndex(doc.getLength());//㦋ᕫ㸠ো Element root = doc.getDefaultRootElement(); /Download at http://www.pin5i.com for (Object keyword : keywords) { //䘡ग़᠔᳝݇䬂ᄫ ArrayList keywords = attMap.get(att); //পߎᔧࠡ买㡆ᇍᑨⱘ᠔᳝݇䬂ᄫ for (SimpleAttributeSet att : attMap.keySet()) { outer : SimpleAttributeSet currentAttributeSet = null; //ֱᄬ䳔㽕ᇍᔧࠡऩ䆡ᇍᑨⱘ໪㾖ሲᗻ int start , int length) { public void setHighLight(StyledDocument doc , String token , //䆒㕂䇁⊩催҂ } return normalAttr; public SimpleAttributeSet getNormalAttributeSet() { //䖨ಲ䆹Ḑᓣ఼䞠ℷᐌ᭛ᴀⱘ໪㾖ሲᗻ } } attMap.put(att , keywords); StyleConstants.setForeground(att, new Color(color)); SimpleAttributeSet att = new SimpleAttributeSet(); if (keywords.size() > 0 && color > -1) { //ᡞ᳔ৢⱘ݇䬂ᄫ੠买㡆ᇍᑨ䍋ᴹ } } if (line.trim().length() > 0) keywords.add(line.trim()); //ᇍѢ᱂䗮㸠ˈ↣㸠ݙᆍ⏏ࡴࠄ݇䬂ᄫ List 䞠 } else { color = Integer.parseInt(line.substring(1) , 16); keywords = new ArrayList(); ໛ޚ //䞡ᮄ߯ᓎᮄⱘ݇䬂ᄫ ListˈЎϟϔϾ䇁⊩Ḑᓣ } attMap.put(att , keywords); //ᇚᔧࠡ买㡆੠݇䬂ᄫ List ᇍᑨ䍋ᴹ //StyleConstants.setFontSize(att, 14); StyleConstants.setForeground(att, new Color(color)); SimpleAttributeSet att = new SimpleAttributeSet(); //পߎᔧࠡ㸠ⱘ买㡆ؐˈᑊᇕ㺙៤ SimpleAttributeSet ᇍ䈵 if (keywords.size() > 0 && color > -1) { if (line.startsWith("#")) { //བᵰᔧࠡ㸠ҹ#ᓔ༈ String line = scaner.nextLine(); while(scaner.hasNextLine()) { //ϡᮁ䇏প䇁⊩᭛ӊⱘݙᆍ㸠 ArrayList keywords = new ArrayList(); int color = -1; scaner = new Scanner(new File(syntaxFile)); Scanner scaner = null; //߯ᓎϔϾ Scanner ᇍ䈵ˈ䋳䋷ḍ᥂䇁⊩᭛ӊࡴ䕑买㡆ֵᙃ //StyleConstants.setFontSize(normalAttr, 14); StyleConstants.setForeground(normalAttr, Color.BLACK); /Download at http://www.pin5i.com if (isQuot && (token.endsWith("\"") || token.endsWith("\'"))) { int tokenPos = s.indexOf(token , curStart); //ᡒߎᔧࠡߚᵤऩ䆡೼ s ᄫヺІⱘЁԡ㕂 for (String token : tokens) { boolean isQuot = false; int curStart = 0; //ᅮНᔧࠡߚᵤऩ䆡ⱘ೼ s ᄫヺІЁⱘᓔྟԡ㕂 String[] tokens = s.split("\\s+|\\.|\\(|\\)|\\{|\\}|\\[|\\]"); //ҹぎḐǃ⚍োㄝ԰Ўߚ䱨ヺ String s = doc.getText(start, length); //পߎ᠔᳝㹿ׂᬍⱘᄫヺІ if (length < docChangeLength) length = docChangeLength + 1; int length = para.getEndOffset() - start; //ᅮН㹿ׂᬍ䚼ߚⱘ䭓ᑺ if (start > docChangeStart) start = docChangeStart; //བᵰ᭛ḷׂᬍԡ㕂↨ᔧࠡ㸠䖬ࠡ ᷛ᠔೼㸠ⱘ㸠༈೼᭛ḷЁԡ㕂ܝ int start = para.getStartOffset();//ᅮН ᷛ᠔೼ԡ㕂ⱘ㸠ܝ Element para = root.getElement(line); //㦋প int line = root.getElementIndex(cursorPos); ᷛᦦܹヺⱘԡ㕂ܝ int cursorPos = this.getCaretPosition();//㦋প᭛ḷЁ ㋴ˈे᭛ḷݙⱘܼ䚼ݙᆍܗ Element root = doc.getDefaultRootElement();//㦋প᭛ḷⱘḍ public void syntaxParse() { //ߚᵤ݇䬂ᄫᮍ⊩ private SimpleAttributeSet lineAttr = new SimpleAttributeSet();//ᅮН᭛ḷЁ㸠᭄᭛ᴀⱘ໪㾖ሲᗻ private int docChangeLength = 0; //ֱᄬ᭛ḷᬍবⱘ䭓ᑺ private int docChangeStart = 0; //ֱᄬ᭛ḷᬍবⱘᓔྟԡ㕂 private SimpleAttributeSet quotAttr = new SimpleAttributeSet(); protected SyntaxFormatter formatter = new SyntaxFormatter("java.stx");//ᵘ䗴ϔϾ᭛ᴀḐᓣ఼ protected StyledDocument doc; //ḋᓣ᭛ḷᇍ䈵 ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\EditPane.java ϔϾߚᵤ᭛ᴀⱘᮍ⊩DŽ ᵰ䳔㽕䖯㸠催҂ᰒ⼎ˈ߭䇗⫼ setHighLight˄ҹϞҷⷕⱘ咥ԧᄫ˅ᮍ⊩ेৃDŽϟ䴶Ў EditPane ㉏⏏ࡴ ҹϞҷⷕЁˈSyntaxFormatter ⱘᵘ䗴఼⫼Ѣߚᵤ stx ᭛ӊˈᇚ买㡆Ϣ݇䬂ᄫ⫼ Map ֱᄬ䍋ᴹˈབ } } } doc.setCharacterAttributes(start, length, normalAttr, false); } else {//৺߭Փ⫼᱂䗮໪㾖ᴹ䆒㕂䆹ऩ䆡 doc.setCharacterAttributes(start, length, currentAttributeSet, false); //䆒㕂ᔧࠡऩ䆡ⱘ买㡆 if (currentAttributeSet != null){ //བᵰᔧࠡऩ䆡ᇍᑨⱘ໪㾖ሲᗻϡЎぎ } } } break outer; currentAttributeSet = att; //䏇ߎᕾ⦃ˈᑊ䆒㕂ᔧࠡऩ䆡ᇍᑨⱘ໪㾖ሲᗻ if (keyword.toString().equals(token)) { བᵰ䆹݇䬂ᄫϢᔧࠡऩ䆡Ⳍৠ// /Download at http://www.pin5i.com 䖯㸠䬂Ⲭ⚍ߏህ䖯㸠䇁⊩ߚᵤDŽ䖤㸠 Main ㉏ˈৃҹⳟࠄᬜᵰབ೒ 8.21 ᠔⼎DŽ ೼᭛ӊᠧᓔⱘᯊ׭˄ᵘ䗴 EditPane ⱘᯊ׭˅ˈህᇍ᭛ᴀ䖯㸠ߚᵤᑊ䖯㸠催҂ᰒ⼎ˈ೼㓪䕥ⱘᯊ׭ˈ } }); } syntaxParse(); public void keyReleased(KeyEvent ke) { this.addKeyListener(new KeyAdapter() { //⏏ࡴᣝ䬂ⲥ਀఼ˈᔧᣝ䬂ᵒᓔᯊ䖯㸠䇁⊩ߚᵤ syntaxParse(); this.setMargin(new Insets(3, 40, 0, 0)); //䆒㕂䆹᭛ḷⱘ义䖍䎱 this.doc = getStyledDocument(); this.setText(FileUtil.readFile(file)); public EditPane(File file) { ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\EditPane.java ᇍ᭛ᴀ䖯㸠催҂ᰒ⼎DŽׂᬍ EditPane ⱘᵘ䗴఼ˈᔧᠧᓔ᭛ӊⱘᯊ׭ˈህᇍ᭛ӊݙᆍ䖯㸠催҂ᰒ⼎DŽ ҹϞҷⷕЁˈsyntaxParse ᮍ⊩ᇍ᭛ᴀ䖯㸠ߚᵤˈབᵰᰃ݇䬂ᄫˈ߭Փ⫼ SyntaxFormatter Ḑᓣ఼ } } curStart = tokenPos + token.length(); //ᓔྟߚᵤϟϔϾऩ䆡 } formatter.setHighLight(doc , token , start + tokenPos, token.length()); //Փ⫼Ḑᓣ఼ᇍᔧࠡऩ䆡䆒㕂买㡆 } else { isQuot = true; doc.setCharacterAttributes(start + tokenPos, token.length(), quotAttr, false); && !(token.endsWith("\"") || token.endsWith("\'"))) { } else if ((token.startsWith("\"") || token.startsWith("\'")) doc.setCharacterAttributes(start + tokenPos, token.length(), quotAttr, false); && (token.endsWith("\"") || token.endsWith("\'"))) { } else if ((token.startsWith("\"") || token.startsWith("\'")) doc.setCharacterAttributes(start + tokenPos, token.length(), quotAttr, false); } else if (isQuot && !(token.endsWith("\"") || token.endsWith("\'"))) { isQuot = false; doc.setCharacterAttributes(start + tokenPos, token.length(), quotAttr, false); /Download at http://www.pin5i.com 䖬䳔㽕ᇚ䆹ӑ᭛ӊ㓪䆥ࠄⳌᑨⱘⳂᔩЁDŽ៥ӀᮄᓎϔϾ SaveHandler ᥹ষˈ䞠䴶᳝ϔϾ save ⱘᮍ⊩ˈ ϟˈབᵰ㽕ֱᄬϔӑ Java ᭛ӊⱘ䆱ˈֱᄬⱘᯊ׭މ䖭ḋህৃҹᅲ⦄᭛ӊⱘֱᄬˈԚᰃˈ೼ᅲ䰙ᚙ } fos.close(); ps.close(); ps.print(content); PrintStream ps = new PrintStream(fos); FileOutputStream fos = new FileOutputStream(file); public static void writeFile(File file, String content) { ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\util\FileUtil.java Ў FileUtil ㉏ᮄ๲ϔϾ writeFile ᮍ⊩ˈՓ⫼ IO ⌕ᇚݙᆍݭࠄ᭛ӊЁDŽ } file.setSaved(true); //䆒㕂᪡԰ⱘ EditFile ᇍ䈵Ꮖ㒣㹿ֱᄬ FileUtil.writeFile(file.getFile(), file.getEditPane().getText()); //Փ⫼ FileUtil ⱘᮍ⊩ᇚ EditFile ᇍᑨⱘ㓪䕥఼ⱘݙᆍݭ䖯᭛ӊ public void saveFile(EditFile file) { //ֱᄬ᭛ӊⱘᮍ⊩ ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\EditorFrame.java 㽕ֱᄬ᭛ӊˈᔧᯊᇮ᳾ᅲ⦄ saveFile ᮍ⊩ˈ⦄೼ᇍ saveFile ԰ϔϾㅔऩⱘᅲ⦄DŽ ೼ EditorFrame Ё᳝ϔϾ saveFile ⱘᮍ⊩ˈ೼ 8.6.5 Ёˈᔧᇍ᭛ӊ䖯㸠њׂᬍˈህᔍߎ䆶䯂ˈᰃ৺ 8.6.7 ᭛ӊⱘֱᄬࡳ㛑 ࠄ䖭䞠ˈᰒ⼎㸠ো੠݇䬂ᄫ催҂ᰒ⼎ⱘࡳ㛑Ꮖ㒣ᅠ៤ˈ᥹ϟᴹᅲ⦄᭛ӊⱘ᪡԰DŽ ೒ 8.21 ᅲ⦄催҂ᰒ⼎ࡳ㛑 /Download at http://www.pin5i.com StringBuffer result = new StringBuffer(); public static String getProcessString(Process process) { //㦋পϔϾ䖯⿟ⱘ䫭䇃ֵᙃ } return buffer.toString(); is.close(); for (int n; (n = is.read(b)) != -1;) buffer.append(new String(b, 0, n)); byte[] b = new byte[4096]; StringBuffer buffer = new StringBuffer(); private static String getProcessString(InputStream is) { } return getProcessString(process.getErrorStream()); public static String getErrorMessage(Process process) { //䖨ಲ䖯⿟ process ⱘ䫭䇃ֵᙃ ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\util\CommandUtil.java 䖨ಲϔϾ䖯⿟˄Process˅ⱘ䫭䇃ֵᙃDŽ ҹ䗮䖛䖭Ͼ䖯⿟ⱘ䫭䇃ֵᙃˈⳟϟ៥Ӏⱘ Java ᭛ӊ㓪䆥ᰃ৺ℷ⹂DŽ㓪ݭϔϾ CommandUtil ⱘ㉏ˈ⫼Ѣ ᮍ⊩ᠻ㸠ϔϾ Java ⱘ㓪䆥ੑҸˈ䆹ᮍ⊩䖨ಲϔϾ Process ᇍ䈵ˈህᰃᠻ㸠䖭ϾੑҸᯊⱘ䖯⿟ˈ៥Ӏৃ Java ᭛ӊⱘᯊ׭ˈህৃҹᇚ⑤᭛ӊ㓪䆥ࠄᣛᅮⱘⳂᔩЁDŽ㓪䆥 Java ᭛ӊˈ៥Ӏ䳔㽕 Runtime ㉏ⱘ exec Ϟ䴶ⱘҷⷕᕫࠄᔧࠡ㡖⚍᠔ሲⱘ乍ⳂⳂᔩˈৃҹ䗮䖛䖭Ͼ乍ⳂⱘⳂᔩ㦋প䆹乍Ⳃⱘ㓪䆥䏃ᕘˈֱᄬ } return null; } } if (selectNode.isNodeAncestor(project)) return project.getFile(); Ё㡖⚍ᰃ䆹 project ϟⱘᄤ㡖⚍ܜ //ᔧࠡ for (ProjectTreeNode project : projects) { if (selectNode != null) { ProjectTreeNode selectNode = getSelectNode(); List projects = root.getChildren(); //㦋পḍ㡖⚍ϟⱘ᠔᳝ᄤ㡖⚍˄े乍Ⳃ㡖⚍䲚ড়˅ ProjectTreeNode root = (ProjectTreeNode)getSelectNode().getRoot(); //㦋পḍ㡖⚍(Ꮉ԰ぎ䯈) public File getCurrentProject() { //䖨ಲ乍Ⳃᷥᔧࠡ᠔䗝Ёⱘ㡖⚍᠔ሲⱘ乍Ⳃ㡖⚍ᇍᑨⱘⳂᔩ ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\EditorFrame.java ݡЎ EditorFrame ᦤկϔϾᮍ⊩ˈ⫼Ѣ䖨ಲᔧࠡ䗝ᢽ㡖⚍᠔ሲⱘ乍ⳂⱘⳂᔩᇍ䈵DŽ } return null; FileUtil.writeFile(editFile.getFile(), editFile.getEditPane().getText()); EditFile editFile = editorFrame.getCurrentFile(); public String save(EditorFrame editorFrame) { //ᦤկϔϾֱᄬᮍ⊩ˈ԰Ў᱂䗮ⱘֱᄬ ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\handler\save\CommonSaveHandler.java CommonSaveHandler ᅲ⦄ SaveHandler ᥹ষDŽ String save(EditorFrame editorFrame); //䆒㕂䆹᭛ḷⱘ义䖍䎱 SaveHandler ᥹ষ˖ ᄬˈݡᮄᓎϔϾ JavaSaveHandler 㒻ᡓ CommonSaveHandler ㉏ˈ䆹㉏໘⧚ֱᄬ Java ᭛ӊDŽ 䖨ಲؐᰃֱᄬ᭛ӊᯊⱘֵᙃˈᮄᓎϔϾ CommonSaveHandler ⱘ㉏㒻ᡓ SaveHandler ԰Ў໘⧚᱂䗮ֱ /Download at http://www.pin5i.com return process; batFile.delete(); //ᇚ bat ᭛ӊߴ䰸 process.waitFor(); batFile.getAbsolutePath()); Process process =Runtime.getRuntime().exec(WINDOWS_COMMAND + //ᠻ㸠䆹 bat ᭛ӊ writer.close(); writer.write(command); FileWriter writer = new FileWriter(batFile); //ᇚੑҸݭܹ᭛ӊЁ if (!batFile.exists()) batFile.createNewFile(); File batFile = new File("dump.bat"); //೼ windows ϟᇚੑҸ⫳៤ϔӑ bat ᭛ӊ, ݡᠻ㸠䆹 bat ᭛ӊ public static Process executeCommand(String command) { ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\util\CommandUtil.java Ў CommandUtil ࡴܹϔϾᎹ݋ᮍ⊩DŽ 䖭ѯ䯂乬DŽܡӀৃҹᇚ䖭ѯᣐ㺙ⱘੑҸᬒࠄϔϾ bat ᭛ӊЁˈݡᇍ᳝ぎḐⱘⳂᔩࡴܹݦোˈ䖭ḋህৃҹ䙓 䖭䞠䳔㽕⊼ᛣⱘᰃˈ೼ Windows ㋏㒳Ёˈབᵰ᭛ӊⳂᔩЁ᳝ぎḐˈ䙷МህϡӮℷᐌ㓪䆥ˈ಴ℸ៥ } return CommandUtil.getProcessString(process); //䖨ಲ䖯⿟ⱘֵᙃ process.waitFor();//ㄝᕙ䖯⿟䖤㸠㒧ᴳ Process process = Runtime.getRuntime().exec(command); //ᠻ㸠ੑҸ String command = "cmd /c javac -d " + classPath + " " + filePath; //ᣐ㺙ᄫヺІੑҸˈ䆹ੑҸাৃ೼ windows ϟ䖤㸠 String filePath = editorFrame.getCurrentFile().getFile().getAbsolutePath(); //㦋ᕫ⑤᭛ӊⱘ᭛ӊ䏃ᕘ + File.separator + CompileConfig.OUTPUT_DIR; String classPath = editorFrame.getCurrentProject().getAbsolutePath() //㦋ᕫ乍Ⳃⱘ㓪䆥䏃ᕘˈ乍ⳂⳂᔩࡴ CompileConfig Ё䜡㕂ⱘ䕧ߎⳂᔩ private String javac(EditorFrame editorFrame) { //ᠻ㸠 javac ੑҸ } return javac(editorFrame); super.save(editorFrame); //䇗⫼⠊㉏ⱘֱᄬᮍ⊩ public String save(EditorFrame editorFrame) { ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\handler\save\JavaSaveHandler.java ϟ䴶ৃҹএᅲ⦄ JavaSaveHandler ㉏DŽ } return error + message; String message = getProcessString(process.getInputStream()); String error = getErrorMessage(process); public static String getRunString(Process process) { //䖨ಲϔϾ䖯⿟ⱘֵᙃ } return result.toString(); else result.append("ᠻ㸠ᅠ៤"); if (errorString.length() != 0) result.append("䫭䇃: " + errorString); String errorString = getErrorMessage(process); //䇗⫼ CommandUtil ⱘᮍ⊩ /Download at http://www.pin5i.com ㉏ˈᑊᮄᓎϔӑ Java ᭛ӊ䖯㸠ֱᄬˈᬜᵰབ೒ 8.22DŽ Ϟ䴶ⱘЁҟ㗙ᇍ䈵ˈ䳔㽕೼ᵘ䗴఼Ё߱ྟ࣪ˈᑊ೼Ⳍᑨⱘⲥ਀఼Ё䇗⫼ saveFile ᮍ⊩DŽ䖤㸠 Main } file.setSaved(true); //ݭᅠ᭛ӊৢˈ䆒㕂ᔧࠡ᭛ӊⱘֱᄬ⢊ᗕЎ trueˈ㸼⼎Ꮖ㒣ֱᄬ infoArea.setText(result); //ᇚ㒧ᵰᬒࠄֵᙃᰒ⼎ऎⱘ᭛ᴀඳЁ String result = saveMediator.doSave(this); //䇗⫼Ёҟ㗙ᇍ䈵ⱘᮍ⊩এֱᄬ᭛ӊ if (file == null) return; public void saveFile(EditFile file) { //⫼Ѣֱᄬᔧࠡ᠔ᠧᓔⱘ᭛ӊ private SaveMediator saveMediator; //Ёҟ㗙ᇍ䈵 ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\EditorFrame.java 䖭ḋˈ៥ӀህᅮНњϔϾЁҟ㗙ⱘ㾦㡆ˈݡএׂᬍ EditorFrame Ёֱᄬ᭛ӊⱘᮍ⊩ेৃDŽ } return result; } result = commonHandler.save(editorFrame); } else {//ᠻ㸠᱂䗮ⱘֱᄬ result = javaHandler.save(editorFrame); if (fileName.endsWith(".java")) {//ֱᄬ java ᭛ӊ ᅮ໘⧚㉏އ //߸ᮁ᭛ӊᰃ৺Ў Java ᭛ӊˈ ݡ String result = null; String fileName = editorFrame.getCurrentFile().getFile().getName(); //㦋ᕫᔧࠡ㓪䕥ⱘ᭛ӊৡ public String doSave(EditorFrame editorFrame) { } this.javaHandler = new JavaSaveHandler(); this.commonHandler = new CommonSaveHandler(); public SaveMediatorConcrete() { //ᵘ䗴ϸϾ໘⧚ֱᄬⱘᇍ䈵 private SaveHandler javaHandler; private SaveHandler commonHandler; ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\handler\save\SaveMediatorConcrete.java public abstract String doSave(EditorFrame editorFramet); //䳔㽕ᄤ㉏এᅲ⦄ⱘֱᄬᮍ⊩ ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\handler\save\SaveMediator.java ⱘ doSave ᮍ⊩DŽ Ԣ䖭ḋⱘҷⷕ㗺ড়DŽᮄᓎϔϾЁҟ㗙ᢑ䈵㉏ SaveMediatorDŽᑊЎ݊ᮄ๲ϔϾᄤ㉏ˈᅲ⦄ SaveMediatorޣ Java ᭛ӊˈݡ⫼ϡৠⱘ SaveHandler ㉏এ໘⧚ֱᄬࡼ࡯ˈ៥ӀৃҹՓ⫼䆒䅵῵ᓣЁⱘЁҟ㗙῵ᓣˈᴹ ᭛ӊDŽབᵰⳈ᥹೼ EditorFrame ЁՓ⫼ SaveHandler ᇍ䈵ˈ䙷МᇚӮ໮ࡴ䆌໮ⱘ߸ᮁˈ߸ᮁ᭛ӊᰃ৺Ў JavaSaveHandler 㒻ᡓѢ CommonSaveHandler ㉏ˈᑊ೼䆹㉏ⱘ෎⸔Ϟ䖤㸠 javac ੑҸˈ㓪䆥 java return CommandUtil.getProcessString(p); Process p = CommandUtil.executeCommand(command); String command = "javac -d \"" + classPath + "\" \"" + filePath + "\""; //ᣐ㺙ᄫヺІੑҸˈ䆹ੑҸাৃ೼ windows ϟ䖤㸠 䙷Мৃҹׂᬍ㓪䆥 Java ᭛ӊᮍ⊩ˈⳈ᥹䇗⫼Ꮉ݋㉏Ёⱘ executeCommand ᮍ⊩˖ { /Download at http://www.pin5i.com //ᣐ㺙ੑҸ String className = getClassName(projectPath, sourcePath); //㦋প㉏ৡ + File.separator + CompileConfig.OUTPUT_DIR; String classPath = editorFrame.getCurrentProject().getAbsolutePath() //㦋ᕫ乍Ⳃⱘ㓪䆥䏃ᕘˈ乍ⳂⳂᔩࡴ CompileConfig Ё䜡㕂ⱘ䕧ߎⳂᔩ String sourcePath = editorFrame.getCurrentFile().getFile().getAbsolutePath(); //㦋ᕫ⑤᭛ӊⱘܼ䏃ᕘ String projectPath = editorFrame.getCurrentProject().getAbsolutePath(); //㦋ᕫ乍ⳂⳂᔩⱘ䏃ᕘ public String run(EditorFrame editorFrame) { ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\handler\run\JavaRunHandler.java ⱘ㉏ˈ⫼Ѣ䖤㸠 Java 㓪䆥ৢⱘ class ᭛ӊDŽ 㓪䆥ⱘॳ⧚ϔḋˈ䛑ᰃՓ⫼ Runtime Ёⱘ exec ᮍ⊩ᴹᠻ㸠 java ੑҸेৃDŽᮄ๲ϔϾ JavaRunHandler ೼ 8.6.6 Ё៥Ӏᅲ⦄њֱᄬᑊ㓪䆥 Java ᭛ӊⱘࡳ㛑ˈ䖭ϔ㡖ᇚᅲ⦄䖤㸠 Java ᭛ӊⱘࡳ㛑ˈ䖤㸠Ϣ 8.6.7 䖤㸠Java᭛ӊ EditorFrame Ϣֱᄬ໘⧚㉏П䯈ⱘҷⷕ㗺ড়DŽ ᭛ӊⱘֱᄬ᪡԰Ꮖ㒣ᅠ៤ˈ೼ᅲ⦄ⱘ䖛⿟Ёˈ៥ӀՓ⫼њ䆒䅵῵ᓣЁⱘЁҟ㗙῵ᓣˈ䰡Ԣњ ೒ 8.22 䖯㸠 Java ᭛ӊֱᄬ /Download at http://www.pin5i.com ೼Ⳍᑨⱘⲥ਀఼Ё䇗⫼ run ᮍ⊩ेৃDŽ䖤㸠 Main ㉏ˈ䖤㸠 Java ᭛ӊˈᕫࠄⱘᬜᵰབ೒ 8.23DŽ dܤႏ୶ᄯѺಳٲᅟġrunHandler ༓ྑၽ EditorFrame ԅ } infoArea.setText(result); String result = runHandler.run(this); //ᇚ㒧ᵰᰒ⼎ ᄬֱܜ saveFile(getCurrentFile());//䖤㸠ࠡ public void run() { //䖤㸠᭛ӊⱘᮍ⊩ private JavaRunHandler runHandler; //䖤㸠 class ᭛ӊⱘ໘⧚㉏ ҷⷕ⏙ऩ˖code\editor\src\org\crazyit\editor\EditorFrame.java Ͼ run ᮍ⊩ˈ⫼Ѣ䖤㸠᭛ӊ˄ᔧࠡা㛑䖤㸠 class ᭛ӊ˅ˈݡএᅲ⦄䖤㸠ⱘ ActionDŽ Java 䖤㸠㉏᭛ӊⱘੑҸᰃ˖java –cp classpath package + classNameDŽЎ EditorFrame ㉏ᮄ๲ϔ } return result; result = result.replace(String.valueOf(File.separatorChar), "."); result = result.replace(".java", ""); String result = sourcePath.replace(temp, ""); File.separator; String temp = projectPath + File.separator + CompileConfig.SRC_DIR + private String getClassName(String projectPath, String sourcePath) { //ḍ᥂乍ⳂⳂᔩⱘ䏃ᕘ੠ Java ⑤᭛ӊⱘ䏃ᕘ㦋পϔϾ㉏ⱘܼ䰤ᅮ㉏ৡ } return CommandUtil.getRunString(p); Process p = CommandUtil.executeCommand(command); String command = "java -cp \"" + classPath + "\" " + className; /Download at http://www.pin5i.com public void actionPerformed(ActionEvent e) { private Action copy = new AbstractAction("໡ࠊ", new ImageIcon("images/copy.gif")) { //໡ࠊ᭛ᴀⱘ Action ᇍ䈵 ᅲ⦄໡ࠊⱘ Action˖ ࠄDŽخ೼㓪䕥ⱘ EditPane˄㒻ᡓѢ JTextPane˅ᇍ䈵ेৃ ᅲ⦄໡ࠊǃ࠾ߛ੠㉬䌈ࡳ㛑䛑䴲ᐌㅔऩˈJTextPane Ꮖ㒣ᐂ៥Ӏᅲ⦄དњˈ៥Ӏা䳔㽕ᣓಲᔧࠡℷ 8.7.1 ᭛ᴀ᪡԰ ⱘ Action ᇍ䈵ˈাᰃᇮ᳾ᅲ⦄ˈᴀᇣ㡖ᇍᅗӀ䖯㸠ᅲ⦄DŽ ᴀ՟Ё᭛ᴀ᪡԰Џ㽕ࣙᣀ໡ࠊǃ㉬䌈੠࠾ߛ䖭ϝϾࡳ㛑ˈ೼ᓎゟ⬠䴶ⱘᯊ׭ˈህᏆ㒣ᓎゟњⳌᇍᑨ 8.7 ᅲ⦄᭛ᴀ᪡԰ 㸠ˈ乍ⳂᷥⱘࠋᮄㄝDŽ 8.6 Ꮖ㒣ܼ䚼ᅲ⦄њ៥Ӏ೼ᴀゴᓔ༈᠔ᅮНⱘ᭛ӊ᪡԰ⱘࡳ㛑ˈࣙᣀ᭛ӊⱘᮄᓎˈᠧᓔˈֱᄬˈ䖤 ೒ 8.23 ᅲ⦄䖤㸠 Java ᭛ӊࡳ㛑 ㄀ 8 ゴㅔऩJava IDE Ꮉ݋ g51g if (getCurrentFile() != null) { getCurrentFile().getEditPane().copy(); } } }; ᅲ⦄࠾ߛⱘ Action˖ //࠾ߛ᭛ᴀⱘ Action ᇍ䈵 private Action cut = new AbstractAction("࠾ ߛ", new ImageIcon("images/cut.gif")) { public void actionPerformed(ActionEvent e) { if (getCurrentFile() != null) getCurrentFile().getEditPane().cut(); } }; ᅲ⦄㉬䌈ⱘ Action˖ //㉬䌈᭛ᴀⱘ Action ᇍ䈵 private Action paste = new AbstractAction("㉬䌈", new ImageIcon("images/paste.gif")) { public void actionPerformed(ActionEvent e) { if (getCurrentFile() != null) getCurrentFile().getEditPane().paste(); } }; ࠄℸˈ᭛ᴀ᪡԰ⱘⳌᇍᑨⱘ Action 䛑Ꮖ㒣ᅲ⦄DŽ 8.8 ᴀゴᇣ㡖 ᴀゴᅲ⦄њϔϾㅔऩⱘ Java IDE Ꮉ݋ˈ෎ᴀⱘࡳ㛑᳝᭛ӊⱘ᪡԰ˈ᭛ᴀⱘ᪡԰ˈᰒ⼎㸠᭄ⱘࡳ㛑ˈ 催҂ᰒ⼎݇䬂ᄫⱘࡳ㛑ˈߚᵤ䇁⊩ⱘࡳ㛑ˈ೼ᅲ⦄䖭ѯࡳ㛑ⱘ䖛⿟Ёˈ䆺㒚ҟ㒡њϞ䗄ࡳ㛑ⱘᅲ⦄ᮍ⊩ Ϣᅲ⦄ॳ⧚ˈᑊՓ⫼њㄪ⬹῵ᓣǃЁҟ㗙῵ᓣㄝ䆒䅵῵ᓣˈЏ㽕䆆䗄њ JTree ⱘՓ⫼ˈབԩ߽⫼ JTree ᅲ⦄߯ᓎ乍Ⳃᷥˈࠋᮄ乍Ⳃᷥㄝࡳ㛑DŽ Download at http://www.pin5i.com/ /Download at http://www.pin5i.com 䴶བ೒ 9.2 ᠔⼎DŽ Ѹᯧⱘ݋ԧֵᙃˈࣙᣀ䆹ヨѸᯧ᠔⍝ঞⱘ䞥乱ǃ䫔ଂ䞣ǃѸᯧ᮹ᳳǃѸᯧ᠔⍝ঞⱘкㄝֵᙃDŽ߱ℹⱘ⬠ ᑊᦤկϔϾ䕧ܹ᮹ᳳⱘ䕧ܹḚˈৃҹḍ᥂᮹ᳳ䖯㸠ᶹ䆶䆹໽ⱘѸᯧ䆄ᔩDŽᴀ⬠䴶ⱘϟञ䚼ߚЎᰒ⼎ᶤヨ Ѹᯧㄝ᪡԰ˈ೼ᴀ⬠䴶ⱘϞञ䚼ߚЎѸᯧ䆄ᔩ߫㸼ˈ៥Ӏ㑺ᅮѸᯧ䆄ᔩ߫㸼᠔ᰒ⼎ⱘЎҞ໽䖯㸠ⱘѸᯧˈ 䫔ଂㅵ⧚⬠䴶Џ㽕⫼Ѣ䖯㸠кⱘ䫔ଂ᪡԰ˈ⏏ࡴϔᴀ䳔㽕ѸᯧⱘкǃҢѸᯧ߫㸼Ё⿏䰸䆹кǃ䖯㸠 9.2.2 䫔ଂㅵ⧚⬠䴶 এᅲ⦄㋏㒳ⱘ䫔ଂ੠ܹᑧࡳ㛑DŽܜˈЁˈ៥Ӏᇚ೼᳔ৢݡএᅲ⦄⫼᠋ⱘⱏᔩ ⷕ䕧ܹḚՓ⫼ⱘᰃ JPasswordField ㉏ˈՓ⫼ JPasswordField ᇍ䈵ᑊϡӮᰒ⼎ॳྟ䕧ܹⱘᄫヺDŽ೼ᴀゴ ⬠䴶䴲ᐌⱘㅔऩˈাᰃϸϾ䕧ܹḚˈϔϾᣝ䪂ेৃᅲ⦄DŽᏤো䕧ܹḚՓ⫼ⱘᰃ JTextField ㉏ˈ㗠ᆚ ೒ 9.1 ⱏᔩ⬠䴶 ᠋ৡ੠ᆚⷕ䛑ℷ⹂ᯊˈህৃҹ䖯ܹ㋏㒳DŽ䳔㽕ᓎゟⱘⱏᔩ⬠䴶བ೒ 9.1 ᠔⼎DŽ ೼䖯ܹ㋏㒳ࠡˈ៥Ӏ䳔㽕㒣䖛ㅔऩⱘ䅸䆕ᠡ㛑䖯ܹˈ಴ℸ䳔㽕ᦤկϔϾㅔऩⱘⱏᔩ⬠䴶ˈᔧ䕧ܹ⫼ 9.2.1 ⱏᔩ⬠䴶 ⱘ৘Ͼ⬠䴶DŽ ೼ᅲ⦄ࡳ㛑ࠡˈ៥Ӏ䳔㽕Ў䖭Ͼ㋏㒳ᓎゟ⬠䴶੠䆒䅵᭄᥂ᑧˈᴀᇣ㡖ᇚᓎゟ೼䖭Ͼᇣ㋏㒳Ё᠔䳔㽕 9.2 ᓎゟ⬠䴶 བԩՓ⫼ Java ⱘডᇘᴎࠊҹঞབԩᇍ㋏㒳䖯㸠ߚሖㄝⶹ䆚⚍DŽ ܹᑧㅵ⧚ǃ䫔ଂㅵ⧚ㄝࡳ㛑ˈ䗮䖛䖭ѯㅔऩⱘࡳ㛑ˈৃҹ䅽៥Ӏњ㾷བԩ߽⫼ JDBC 䖯㸠᭄᥂ᑧ᪡԰ǃ ᴀゴҟ㒡བԩᓔথϔϾ CS 㒧ᵘⱘ೒к䖯ᄬ䫔㋏㒳ˈ䆹㋏㒳ⱘЏ㽕᳝ߎ⠜⼒ㅵ⧚ǃкᴀㅵ⧚ǃкⱘ 9.1 乍Ⳃㅔҟ  ゴ೒к䖯ᄬ䫔㋏㒳 ㄀ /Download at http://www.pin5i.com བ೒ 9.3 ᠔⼎DŽ ܹᑧㅵ⧚⬠䴶੠䫔ଂㅵ⧚⬠䴶ⱘᏗሔ໻㟈Ⳍৠˈাᰃ݊Ёⱘ᭄᥂߫੠᭛ᄫ᳝᠔Ꮒ䎱DŽܹࡴㅵ⧚⬠䴶 9.2.3 ܹᑧㅵ⧚⬠䴶 ⳟ೒к䖯ᄬ䫔㋏㒳ҷⷕ⏙ऩЁⱘ SalePanel ㉏DŽ ⱘ JScrollPane ᇍ䈵Џ㽕ᄬᬒϔϾ JTableˈϟ䴶ⱘ JScrollPane Џ㽕ᬒϔѯ Box ᇍ䈵䖯㸠Ꮧሔˈ䆺㒚䇋 ᰃϔϾ JSplitPane ᇍ䈵ˈϞ䴶ᬒϔϾϾ JScrollPane ᇍ䈵ˈϟ䴶䚼ߚгᰃϔϾ JScrollPane ᇍ䈵DŽϞ䴶 ೼ᴀ՟Ёˈ↣ϔϾ⬠䴶ᇍᑨϔϾ JPanelˈ↣Ͼ⬠䴶䛑ᰃϔϾ JPanel ㉏ⱘᄤ㉏ˈ䫔ଂㅵ⧚⬠䴶䞠䴶 ᳝᠔њ㾷DŽމ⢊ᯊᇍ䆹ᴀкⱘ ঺໪ˈབᵰ೼кᴀⱘϟᢝḚЁ䗝ᢽњᶤᴀкᯊˈᇚӮᏺߎ䆹ᴀкⱘऩҟ੠ᑧᄬˈད䅽Փ⫼㗙೼᪡԰ ·ೠԅ߬྾೴२ᆙۤē࿙ҮωϢ༓ྑఆ್ࠩ໻೛దd ᆙޮಾဎْ·ೠԅӦޮѐྻ߬྾೴२Ԅѻࣿԅē߬྾ఐ୙ಾӲ஍߬྾ನຂහԅನ޷นᅹԅēᆙ೴२ಾْ Đēᆙޮc߬྾ఐ୙ۤᆙ೴२ಾϢࢶ೛దԅēუಾဎဟדᅟġ࡮඘੶Γ߬྾ԅ࠘ੋď࠘ੋԅຏੋϦ ✊гৃҹ䖯㸠ߴ䰸᪡԰DŽ೼⬠䴶ⱘ᳔Ϟ䴶ˈ᳝ϔϾᶹ䆶ᣝ䬂ˈৃҹ䕧ܹ᮹ᳳ䖯㸠Ѹᯧ䆄ᔩᶹ䆶DŽ ᔧ䗝ᢽњϔᴀкᑊ䕧ܹⳌᑨⱘѸᯧ᭄䞣ˈݡ⚍ߏ⏏ࡴᯊˈेৃ৥Ѹᯧ⍝ঞⱘк߫㸼Ё⏏ࡴкⱘ䆄ᔩˈᔧ ᯧЁ䆹ᴀкߎଂⱘ᭄䞣DŽѸᯧЁ⍝ঞⱘк߫㸼ϟˈৃҹ䗝ᢽк੠䕧ܹкⱘ᭄䞣ˈᑊᦤկ⏏ࡴ੠ߴ䰸ᣝ䪂ˈ Ѹᯧ᮹ᳳǃᘏ᭄䞣ǃѸᯧ⍝ঞкⱘ߫㸼ˈ݊ЁѸᯧЁ⍝ঞкⱘ߫㸼ЁˈࣙᣀкⱘкৡǃऩӋǃ੠ᴀ⃵Ѹ ᯧ᮹ᳳǃᘏ᭄䞣DŽ䫔ଂㅵ⧚⬠䴶ⱘϟ䴶䚼ߚˈᰃᰒ⼎݋ԧᶤヨѸᯧⱘ⬠䴶ˈᰒ⼎ᇍᑨᶤヨѸᯧⱘᘏӋǃ བ೒ 9.2ˈ䫔ଂㅵ⧚⬠䴶ⱘѸᯧ䆄ᔩ߫㸼Ёˈ↣߫ⱘᄫ↉ߚ߿Ў䆹Ѹᯧ᠔⍝ঞⱘкᴀৡǃᘏӋǃѸ ೒ 9.2 䫔ଂ⬠䴶 /Download at http://www.pin5i.com ⧚⬠䴶བ೒ 9.4 ᠔⼎DŽ ⱘ೒⠛ˈЎㅔऩ䍋㾕ˈᴀ՟Ёⱘ↣ᴀкা᳝ϔᓴ೒⠛ˈ಴ℸϡ䳔㽕঺໪ᓎゟ㸼ᴹᄬᬒкⱘ೒⠛DŽкᴀㅵ Ӯᇍкⱘᑧᄬ䞣ѻ⫳ᕅડDŽкⱘ෎ᴀֵᙃ䰸њкৡǃӋḐҟ㒡ǃ᠔ሲߎ⠜⼒੠кⱘ⾡㉏໪ˈ䖬䳔㽕᳝к ⫼Ѣ䫔ଂ੠ܹᑧDŽ䖭䞠䳔㽕⊼ᛣⱘᰃˈ೼ᮄ๲ϔᴀкⱘᯊ׭ˈ䆹кⱘᑧᄬЎ 0ˈা᳝㒣䖛䫔ଂ੠ܹᑧᠡ кᴀㅵ⧚⬠䴶Џ㽕ࣙᣀкᴀⱘᶹ䆶ǃ߫㸼ǃᶹⳟㄝࡳ㛑ˈкᴀ೼ᴀ՟Ё԰Ў෎⸔᭄᥂ⱘ㾦㡆ˈЏ㽕 9.2.4 кᴀㅵ⧚⬠䴶 ೼ᴀ՟Ёˈܹᑧㅵ⧚⬠䴶ᇍᑨⱘ㉏Ў RepertoryPanel ㉏ˈᰃ JPanel ⱘᄤ㉏DŽ ᅟġၽ༪႔੶·ೠನē༓ྑᆑՎӛѻ؞ೠԅ࣏Ӊd ࡴ੠ߴ䰸кᴀˈ㸼⼎䆹ܹᑧ䆄ᔩЁ᠔⍝ঞⱘкᴀঞᇍᑨⱘ᭄䞣DŽ ᴀⱘ߫㸼ˈৠḋഄˈܹᑧ᮹ᳳϢᘏ᭄䞣䛑ϡৃҹ᠟ࡼ䕧ܹDŽ೼ᮄ๲ϔᴵܹᑧ䆄ᔩᯊˈৃҹ৥кᴀ߫㸼⏏ ੠ܹᑧкᴀⱘᘏ᭄䞣DŽ⬠䴶ϟ䴶䚼ߚᰃ݋ԧᶤᴵܹᑧ䆄ᔩⱘ݋ԧֵᙃˈࣙᣀܹᑧ᮹ᳳǃᘏ᭄䞣੠ܹᑧк Ϣ䫔ଂㅵ⧚⬠䴶㉏ԐˈϞ䴶ⱘ߫㸼ᰃܹᑧ䆄ᔩˈϔᴵܹᑧ䆄ᔩࣙᣀܹᑧᯊ᠔⍝ঞⱘкᴀǃܹᑧ᮹ᳳ ೒ 9.3 ᑧᄬㅵ⧚⬠䴶 /Download at http://www.pin5i.com ᢽ䆹к᠔ᇍᑨⱘ⾡㉏DŽ⾡㉏ㅵ⧚⬠䴶བ೒ 9.5 ᠔⼎DŽ ㉏ᑊϡ䳔㽕ӏԩ㑺ᴳˈা䳔㽕䕧ܹ⾡㉏ⱘৡ⿄ेৃDŽ೼кⱘㅵ⧚⬠䴶Ёˈབᵰ䳔㽕⏏ࡴϔᴀкˈᖙ乏䗝 ӀᦤկϔϾ⾡㉏ㅵ⧚⬠䴶ˈৃҹ⫼Ѣㅵ⧚৘Ͼ⾡㉏DŽ⾡㉏೼ᴀ՟ЁϢкᴀϔḋˈሲѢ෎⸔᭄᥂DŽᮄ๲⾡ кⱘ⾡㉏ˈ㸼⼎ϔᴀкᰃሲѢાϾ⾡㉏ⱘˈ՟བ䅵ㅫᴎǃ᭛ᄺǃ⊩ᕟㄝㄝˈ䖭ѯ䛑ᰃкⱘ⾡㉏ˈ៥ 9.2.5 ⾡㉏ㅵ⧚⬠䴶 Ϣࠡ䴶ⱘ⬠䴶ϔḋˈ೼⿟ᑣЁˈ៥Ӏᮄ๲ϔϾ BookPanel ԰Ў䆹⬠䴶DŽ ᰒ⼎кᴀ᠔ᇍᑨⱘ㓽⬹೒ˈᔧ⫼᠋⚍ߏ㓽⬹೒ⱘᯊ׭ˈৃҹᔍߎᮄⱘにষ⫼Ѣሩ⼎໻೒DŽ ೼⬠䴶ⱘϟᮍ᳝ϔϾ㸼ऩˈ⫼Ѣᶹⳟǃׂᬍ੠⏏ࡴкᴀ᪡԰ˈ㸼ऩⱘে䖍ᰃкⱘ೒⠛ᰒ⼎ऎˈ⫼Ѣ ᰃ⬅Ѣ䫔ଂ˄ܹᑧ˅Ё᠔⍝ঞⱘᰃ໮ᴀкˈ㗠кᴀㅵ⧚⬠䴶Ё↣ϔᴵкⱘ䆄ᔩাᰃҷ㸼ϔᴀкDŽ ℸ໘ⱘкᴀৡ⿄Ϣ䫔ଂㅵ⧚˄ܹᑧㅵ⧚˅⬠䴶Ёⱘкᴀৡ⿄᳝᠔ऎ߿ˈ䖭䞠ⱘкᴀৡ⿄ⱘ߫ᆑ䕗じˈ䖭 кᴀㅵ⧚⬠䴶Ϟ䴶ⱘ߫㸼Џ㽕ᰒ⼎кᴀৡ⿄ǃㅔҟǃ᠔ሲ⾡㉏ǃߎ⠜⼒ǃᑧᄬ᭄䞣੠ӋḐ䖭ѯֵᙃˈ ೒ 9.4 кᴀㅵ⧚⬠䴶 /Download at http://www.pin5i.com ೒ 9.6 ᠔⼎DŽ ⏏ࡴϔᴀкˈ䰸њ䳔㽕᳝⾡㉏П໪ˈ䖬䳔㽕䗝ᢽ䆹кⱘߎ⠜⼒ˈ៥ӀᦤկϔϾߎ⠜⼒ㅵ⧚⬠䴶DŽབ 9.2.6 ߎ⠜⼒ㅵ⧚⬠䴶 ⱘ㸼ऩDŽ೼⿟ᑣЁˈ៥Ӏᮄ๲ϔϾ TypePanel ԰Ў䆹⬠䴶DŽ བ೒ 9.5 ᠔⼎ˈ⾡㉏ㅵ⧚⬠䴶ᑊϡ໡ᴖˈϞ䴶ᰃϔϾㅔऩⱘ߫㸼ˈϟ䴶ᰃ᱂䗮ⱘᶹⳟǃᮄ๲Ϣׂᬍ ೒ 9.5 ⾡㉏ㅵ⧚⬠䴶 /Download at http://www.pin5i.com public JTable getJTable() { } this.table = table; public void setJTable(JTable table) { protected Vector datas; //߫㸼᭄᥂ private JTable table; //ᄬᬒ᭄᥂ⱘ table ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\ui\CommonPanel.java ҹϟҷⷕЎ৘Ͼ⬠䴶ᇍ䈵ⱘ⠊㉏˄CommonPanel˅ⱘҷⷕDŽ ϔѯৃҹ䞡⫼ⱘҷⷕᦤछ㟇⠊㉏ˈᑊৃҹՓ⫼Ā῵ᵓᮍ⊩āˈᦤկϔѯᢑ䈵ⱘᮍ⊩䅽৘Ͼᄤ㉏এᅲ⦄DŽ Ͼ㸼ऩˈ಴ℸˈ៥Ӏৃҹᇚ䖭޴Ͼ⬠䴶ⱘ݅ৠ䚼ߚᦤপߎᴹˈ԰Ў↣Ͼ⬠䴶ᇍ䈵˄JPanel˅ⱘ⠊㉏ˈᇚ ᴀ՟Ё⍝ঞⱘ৘Ͼ⬠䴶ˈ៥Ӏৃҹথ⦄⬠䴶޴Т䛑໻ৠᇣᓖˈ⬠䴶Ϟ䴶䚼ߚᰃϔϾ߫㸼ˈϟ䴶ᰃϔ 9.2.7 ׂᬍ⬠䴶ҷⷕ Џ⬠䴶ࡴϞⳌᑨⱘ㦰ऩˈ៥Ӏⱘ⬠䴶ህܼ䚼ᅠ៤DŽ ࠄℸˈ៥Ӏ᠔䳔㽕ⱘ⬠䴶Ꮖ㒣ܼ䚼ᅠ៤њˈ೼ 9.2.7 Ё៥Ӏᇚᇍ৘Ͼ⬠䴶ⱘҷⷕ䖯㸠Ӭ࣪ˈݡএЎ Ͼ㦰ऩⱘᯊ׭ˈJFrame Ёⱘ৘Ͼ JPanel ᇍ䈵䱤㮣ˈাᰒ⼎ᇍᑨⱘ䙷Ͼ JPanelDŽ Ң 9.2.2 ࠄ 9.2.6 ⱘ৘Ͼ㋏㒳⬠䴶ˈ䛑ᰃᄬ೼ѢϔϾ JFrame Ёˈ៥Ӏৃҹᦤկ㦰ऩˈᔧ⚍ߏњᶤ 䴶DŽ ⬠䴶ⱘ㸼ऩ㉏Ԑˈ䛑ᰃ⫼Ѣᶹⳟǃׂᬍ੠ᮄ๲DŽ೼⿟ᑣЁˈ៥Ӏᮄ๲ϔϾ ConcernPanel ᴹ㸼⼎䖭Ͼ⬠ ߎ⠜⼒߫㸼ЁˈЏ㽕ᰒ⼎ߎ⠜⼒ৡ⿄ǃ㘨㋏Ҏǃ㘨㋏⬉䆱੠ㅔҟ䖭ѯֵᙃˈϟ䴶ⱘ㸼ऩϢࠡ䴶޴Ͼ ೒ 9.6 ߎ⠜⼒ㅵ⧚⬠䴶 /Download at http://www.pin5i.com ⊩ˈ݋ԧҷⷕབϟDŽ ᄬᬒ৘Ͼ⬠䴶ᇍ䈵ⱘ JFrame ㉏ЁˈৃҹᦤկϔϾᮍ⊩ˈᔧ⬠䴶থ⫳䕀ᤶᯊˈህ䇗⫼ setViewDatas ᮍ setViewDatas ⱘᮍ⊩ˈЏ㽕Ң᭄᥂ᑧЁ䇏পⳌ݇ⱘ᭄᥂ˈݡ䇗⫼⠊㉏ⱘ initDatas ᮍ⊩ᵘᓎ߫㸼DŽ䙷М ᦤկ߫ⱘ䲚ড়໪ˈ䖬䳔㽕ᦤկ᭄᥂ˈгህᰃ CommonPanel Ёⱘ datas ሲᗻDŽ៥Ӏ䖭䞠ᦤկњϔϾ 㒭⠊㉏ϡৠⱘ߫ˈ䅽⠊㉏䖯㸠ᰒ⼎ˈ䰸њ䳔㽕ˈމ⠊㉏ᅮНད㾘㣗Пৢˈᄤ㉏ህৃҹḍ᥂ϡৠⱘᚙ id ⱘ߫DŽ خ㸼Ёˈᖙ乏㽕᳝ϔϾ߫ৡি ᦤկњϔϾ getSelectId ᮍ⊩ˈ⫼ᴹ㦋ᕫ JTable ሲᗻЁ᠔䗝Ёⱘᶤϔ㸠Ё id ߫ⱘؐˈгህᰃᛣੇⴔ೼߫ ᥂ˈ⬅ᄤ㉏এᅲ⦄˗clear()ᮍ⊩ᰃࠋᮄ↣Ͼ⬠䴶ϟ䖍ⱘ㸼ऩˈৠḋ⬅ᄤ㉏এᅲ⦄DŽ೼ CommonPanel Ё ՟བ䳔㽕䆒㕂ᶤϔ߫ⱘᆑᑺ៪㗙䆒㕂߫㸼ⱘ㸠催ㄝㄝ˗㗠 setViewDatas()ᮍ⊩ᰃ䞡ᮄএ᭄᥂ᑧ䇏প᭄ ⦄ⱘᮍ⊩ˈgetColumns()⬅ᄤ㉏এᦤկ߫㸼ⱘ߫䲚ড়˗setTableFace()ᰃ⬅ᄤ㉏এ䆒㕂 JTable ⱘᰒ⼎ˈ ㅵ⧚⬠䴶Ёⱘ䫔ଂ䆄ᔩ߫㸼ǃкᴀㅵ⧚⬠䴶Ёⱘкᴀ߫㸼ㄝDŽ⊼ᛣҷⷕЁⱘ咥ԧ䚼ߚˈ䛑ᰃ⬅ᄤ㉏এᅲ ҹϞⱘҷⷕˈᦤկϔϾ JTable ⱘሲᗻˈ䖭ᰃ಴Ў↣Ͼ⬠䴶Ё䛑᳝ϔϾЏ㽕ⱘ߫㸼ᇍ䈵ˈ՟བ䫔ଂ } return id; String id = (String)table.getValueAt(row, column); int column = table.getColumn("id").getModelIndex(); int row = table.getSelectedRow(); public String getSelectId(JTable table) { //㒭ᄤ㉏Փ⫼ⱘᮍ⊩, ⫼Ѣ㦋পϔϾ߫㸼ⱘ id ߫ؐ public abstract void clear(); //⏙ぎ⬠䴶ϟ䖍ⱘ߫㸼 public abstract void setViewDatas(); //䆒㕂᭄᥂߫㸼ⱘᮍ⊩ˈ⬅ᄤ㉏এᅲ⦄ public abstract void setTableFace(); //䆒㕂߫㸼ⱘḋᓣ, ⬅ᄤ㉏এᅲ⦄ public abstract Vector getColumns(); //㦋প㸼߫䲚ড়, ⬅ᄤ㉏এᅲ⦄ } getJTable().repaint(); initData(); public void refreshTable() { //ࠋᮄ߫㸼ⱘᮍ⊩ } setTableFace(); //䆒㕂㸼Ḑḋᓣ tableModel.setDataVector(getDatas(), getColumns()); //ᇚ᭄᥂䆒ܹ㸼Ḑ Model Ё DefaultTableModel tableModel = (DefaultTableModel)this.table.getModel(); if (this.table == null) return; public void initData() { //ᇚ᭄᥂䆒㕂䖯 JTable Ё } this.datas = datas; public void setDatas(Vector datas) { } return datas; public Vector getDatas() { } return this.table; /Download at http://www.pin5i.com ⱘᰃ MySQL5.0 ԰Ў᭄᥂ᑧDŽ೼䆒䅵᭄᥂ᑧࠡˈ៥Ӏৃҹ⹂ᅮˈ㋏㒳Ⳍ݇ⱘ㸼ˈҢ᳔෎⸔ᓔྟˈ᳝ߎ⠜ ೼ 9.2 Ё៥ӀᏆ㒣ᓎゟњ㋏㒳ⱘⳌ݇⬠䴶ˈ᥹ϟᴹˈ䖭ϔᇣ㡖ᇚ䆒䅵䖭Ͼ㋏㒳ⱘ᭄᥂ᑧDŽᴀ՟Փ⫼ 9.3 䆒䅵㋏㒳᭄᥂ᑧ ᓔྟ䆒䅵᭄᥂ᑧDŽ getColumns()ǃsetTableFace()ǃsetViewDatas()੠ clear()ᮍ⊩ˈ㗠᭄᥂᱖ᯊϡᖙᦤկˈϟϔ㡖៥Ӏᇚ ໛Ꮉ԰ৢˈህৃҹׂᬍ৘Ͼ⬠䴶ᇍᑨⱘ㉏ˈএ㒻ᡓ CommonPanel ेৃˈᅲ⦄ޚњҹϞⱘخ { { ; return false public boolean isCellEditable(int row, int column) { Ḑϡৃ㓪䕥ܗ //䞡ݭ⠊㉏ⱘᮍ⊩, Փ᠔᳝ⱘऩ } getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION); //䆒㕂㸼Ḑা㛑䗝ᢽϔ㸠 super(dm); public CommonJTable(TableModel dm) { public class CommonJTable extends JTable { ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\ui\CommonJTable.java Ḑৃҹ㓪䕥ˈ಴ℸ䞡ݭ JTable ⱘ isCellEditable ᮍ⊩ेৃˈҷⷕབϟDŽܗ㽕䅽߫㸼ⱘ↣Ͼऩ ঺໪ˈ↣Ͼ⬠䴶ⱘЏ߫㸼៥ӀՓ⫼ϔϾ CommonJTable ᇍ䈵ˈ䆹ᇍ䈵㒻ᡓѢ JTableˈ៥Ӏᑊϡ䳔 ҹϞҷⷕ㸼⼎⚍ߏњ䫔ଂㅵ⧚ⱘ㦰ऩৢˈህ䇗⫼ changePanel ᮍ⊩ˈ䕀ᤶ⬠䴶ᑊ߱ྟ᭄࣪᥂DŽ }; } changePanel(salePanel); //䇗⫼䕀ᤶⱘᮍ⊩ public void actionPerformed(ActionEvent e) { private Action sale = new AbstractAction("䫔ଂㅵ⧚", new ImageIcon("images/sale.gif")) { ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\ui\MainFrame.java ⬠䴶Ё䕀ᤶᯊˈᔧ⚍ߏњᇍᑨⱘ㦰ऩৢˈݡ䖯㸠䕀ᤶ˄䇗⫼ changePanel ᮍ⊩˅DŽ ҹϞҷⷕⱘ咥ԧ䚼ߚˈ䇗৘Ͼ⬠䴶ᇍ䈵⠊㉏ⱘ setVieweDatas ᮍ⊩䞡ᮄ䇏প᭄᥂ᑊࠋᮄ߫㸼ˈ৘Ͼ } commonPanel.clear(); //⏙ぎ⬠䴶ϟ䖍ⱘ㸼ऩ commonPanel.setViewDatas (); //䇗⫼ CommonPanel ⱘᮍ⊩䞡ᮄ䇏প᭄᥂ᑊࠋᮄ߫㸼 this.setVisible(true); this.repaint(); this.currentPanel = commonPanel; //䆒㕂ᔧࠡⱘ JPanel this.add(commonPanel); //⏏ࡴ䳔㽕ᰒ⼎ⱘ JPanel this.remove(currentPanel); //⿏䰸ᔧࠡᰒ⼎ⱘ JPanel private void changePanel(CommonPanel commonPanel) { //ߛᤶ৘Ͼ⬠䴶 ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\ui\MainFrame.java /Download at http://www.pin5i.com CREATE TABLE IF NOT EXISTS `T_BOOK_TYPE` ( -- к⾡㉏ া᳝ㅔऩⱘϝϾᄫ↉ˈ߯ᓎⱘ SQL བϟDŽ ‰ TYPE_INTRO˖⾡㉏ⱘㅔҟDŽ ‰ TYPE_NAME˖⾡㉏ⱘৡ⿄DŽ ‰ ID˖Џ䬂 ID ᄫ↉њDŽ߯ᓎ T_BOOK_TYPE 㸼ˈࣙᣀҹϟⱘᄫ↉˖ ⾡㉏㸼Ϣߎ⠜⼒㸼ϔḋˈᑊϡ໡ᴖˈг≵᳝ӏԩⱘ໪䬂ˈḍ᥂ 9.2.5 ⱘ⬠䴶ˈህৃҹ⹂ᅮ䳔㽕ાѯ 9.3.2 䆒䅵⾡㉏㸼 ⭥ᰃᇍ㞾Ꮕ㛑࡯ⱘϔ⾡ᦤ催DŽ Ԛヨ㗙䖬ᰃᏠᳯ㛑໳㞾Ꮕᇱ䆩এ㓪ݭ䖭ѯ SQLˈ಴ЎᇍѢ߱ᄺ㗙㗠㿔ˈৃҹ㞾Ꮕএ㓪ݭ䖭ѯ SQLˈ᮴ བᵰԴ᳝ MySQL ⱘϔѯㅵ⧚Ꮉ݋ˈৃҹՓ⫼䖭ѯᎹ݋䖯㸠߯ᓎ㸼ˈᑊϡ䳔㽕㞾Ꮕкݭ SQL 䇁হˈ ); PRIMARY KEY (`ID`) -- ໄᯢЏ䬂 `PUB_INTRO` varchar(200), -- ㅔҟ `PUB_LINK_MAN` varchar(50), -- 㘨㋏Ҏ `PUB_TEL` varchar(50), -- 㘨㋏⬉䆱 `PUB_NAME` varchar(50), -- ߎ⠜⼒ৡ⿄ `ID` int AUTO_INCREMENT NOT NULL, -- Џ䬂⫳៤ㄪ⬹Ў㞾ࡼ๲䭓 CREATE TABLE IF NOT EXISTS `T_PUBLISHER` ( -- ߎ⠜⼒ SQLDŽ ⬅Ѣߎ⠜⼒ᰃ᳔෎⸔ⱘ᭄᥂ˈ಴ℸϡ䳔㽕݊Ҫⱘ໪䬂݇㋏DŽҹϟЎ߯ᓎ T_PUBLISHER 䖭Ͼ㸼ⱘ ‰ PUB_INTRO˖ߎ⠜⼒ㅔҟDŽ ‰ PUB_LINK_MAN˖㘨㋏ҎDŽ ‰ PUB_TEL˖㘨㋏⬉䆱DŽ ‰ PUB_NAME˖ߎ⠜⼒ৡ⿄DŽ ‰ ID˖Џ䬂 IDDŽ MySQL Ёᓎゟ㸼 T_PUBLISHERˈߎ⠜⼒㸼ࣙᣀⱘᄫ↉᳝˖ ೼ 9.2.6 Ёˈ៥ӀᏆ㒣ᓎゟњߎ⠜⼒ⱘㅵ⧚⬠䴶ˈ⦄೼া䳔㽕ḍ᥂⬠䴶ᴹ䆒䅵᭄᥂ᑧेৃDŽ៥Ӏ೼ 9.3.1 䆒䅵ߎ⠜⼒㸼 MySQL Ёⱘ CREATE ੠ USE 䇁⊩ৃᶹⳟ MySQL ⱘᐂࡽ᭛ḷDŽ USE BOOK_SYSTEM; -- Փ⫼ BOOK_SYSTEM CREATE DATABASE IF NOT EXISTS BOOK_SYSTEM; -- ߯ᓎ DATABASE ߯ᓎ᭄᥂ᑧˈ೼ MySQL Ёˈ߯ᓎ BOOK_SYSTEM ᭄᥂ᑧˈ݋ԧⱘ SQL 䇁হབϟ˖ བᵰϞ䴶ⱘ᭛ᄫ䲒ҹ⧚㾷ˈৃҹⳟϟ䴶ⱘ᭄᥂ᑧ㒧ᵘˈ᳈᳝ࡽѢ⧚㾷DŽ೼䆒䅵৘Ͼ㸼ࠡˈ៥Ӏ䳔㽕 кⱘܹᑧ䆄ᔩϔḋDŽ кⱘܹᑧ䆄ᔩሲѢᶤϔᴵܹᑧ䆄ᔩˈৃҹ⧚㾷៤䖭ᰃкϢܹᑧ䆄ᔩⱘ݇㋏㸼ˈৠḋഄˈкⱘ䫔ଂ䆄ᔩϢ ᔩг⍝ঞ໮ᴀкˈ಴ℸ䖬䳔㽕кⱘܹᑧ䆄ᔩ㸼੠кⱘ䫔ଂ䆄ᔩ㸼ˈϔᴵкⱘܹᑧ䆄ᔩᇍᑨϔᴀкˈ䆹ᴵ 㸼ǃкⱘ㉏ൟ㸼ǃк㸼ǃܹᑧ䆄ᔩ㸼੠䫔ଂ䆄ᔩ㸼ˈ݊Ёˈϔᴵܹᑧ䆄ᔩЁ⍝ঞ໮ᴀкˈϔᴵ䫔ଂ䆄⼒ /Download at http://www.pin5i.com ‰ RECORD_DATE˖ܹᑧ᮹ᳳDŽ ‰ ID˖Џ䬂 IDDŽ ϟ˖ ᳝ৃ㛑ܹᑧ໮ᴀкDŽܹᑧ䆄ᔩˈা䳔㽕䆄ᔩܹᑧⱘᯊ䯈ेৃˈᮄᓎ T_IN_RECORD 㸼ˈ䆹㸼ⱘᄫ↉བ ೼䆒䅵ܹᑧ䆄ᔩࠡˈ៥Ӏ䳔㽕ⶹ䘧ⱘᰃˈϔϾܹᑧ䆄ᔩˈࣙ৿໮ᴀкⱘܹᑧˈгህᰃ䇈ˈϔ⃵ܹᑧˈ 9.3.4 䆒䅵ܹᑧ䆄ᔩ㸼 ); PRIMARY KEY (`ID`) FOREIGN KEY (`PUB_ID_FK`) REFERENCES `T_PUBLISHER` (`ID`), -- ໄᯢߎ⠜⼒໪䬂 FOREIGN KEY (`TYPE_ID_FK`) REFERENCES `T_BOOK_TYPE` (`ID`), -- ໄᯢ⾡㉏ⱘ໪䬂 `REPERTORY_SIZE` bigint(10), -- ᑧᄬ᭄䞣 `AUTHOR` varchar(200), -- ԰㗙 `IMAGE_URL` varchar(200), -- 㓽⬹೒ URL `PUB_ID_FK` int NOT NULL, -- ߎ⠜⼒໪䬂 `TYPE_ID_FK` int NOT NULL, -- ⾡㉏໪䬂 `BOOK_PRICE` double, -- кⱘऩӋ `BOOK_INTRO` varchar(200), -- кㅔҟ `BOOK_NAME` varchar(50), -- кৡ⿄ `ID` int AUTO_INCREMENT NOT NULL, -- ID ᄫ↉ˈ㞾๲ CREATE TABLE IF NOT EXISTS `T_BOOK` ( -- к ҹ䆒㕂кⱘᑧᄬ䞣DŽ߯ᓎ䆹㸼ⱘ SQL བϟDŽ ᅮⱘˈ಴ℸ೼ׂᬍкⱘᯊ׭ˈϡৃއЁ䗮䖛䕧ܹ៪㗙䗝ᢽݭܹࠄ᭄᥂ᑧЁˈԚᰃᑧᄬ䞣ᰃ⬅䫔ଂϢܹᑧ 䖭䞠䳔㽕⊼ᛣϔϟˈкⱘৡᄫǃㅔӋǃऩӋǃ᠔ሲⱘ⾡㉏ǃߎ⠜⼒ǃ㓽⬹೒੠԰㗙ˈ䛑ৃҹ೼⬠䴶 ‰ REPERTORY_SIZE˖ᑧᄬ䞣DŽ ‰ AUTHOR˖԰㗙ৡ⿄DŽ ‰ IMAGE_URL˖㓽⬹೒ⱘഄഔDŽ ‰ PUB_ID_FK˖ߎ⠜⼒໪䬂DŽ ‰ TYPE_ID_FK˖к᠔ሲⱘ⾡㉏ ID ໪䬂DŽ ‰ BOOK_PRICE˖кⱘऩӋDŽ ‰ BOOK_INTRO˖кⱘㅔҟDŽ ‰ BOOK_NAME˖кⱘৡᄫDŽ ‰ ID˖Џ䬂 ID ࡴ໪䬂݇㋏ेৃˈᮄ๲㸼 T_BOOKˈҹϟЎк㸼৘Ͼᄫ↉ⱘᦣ䗄˖ ⠜⼒ǃ㓽⬹೒ഄഔǃᑧᄬ੠ӋḐˈ⾡㉏㸼੠ߎ⠜⼒㸼Ꮖ㒣೼ 9.3.2 Ϣ 9.3.1 Ё䆒䅵њˈা䳔㽕Ўк㸼⏏ ೼ 9.2.4 ⱘ⬠䴶Ёˈ៥Ӏৃҹⳟࠄˈϔᴀк᠔䳔㽕ⱘᄫ↉ࣙᣀ˖кৡǃㅔҟǃ԰㗙ǃ᠔ሲ⾡㉏ǃߎ 9.3.3 䆒䅵к㸼 ); PRIMARY KEY (`ID`) `TYPE_INTRO` varchar(200), -- ⾡㉏ㅔҟ `TYPE_NAME` varchar(50), -- ⾡㉏ৡ⿄ ID` int AUTO_INCREMENT NOT NULL, -- Џ䬂⫳៤ㄪ⬹Ў㞾ࡼ๲䭓` /Download at http://www.pin5i.com ‰ BOOK_ID_FK˖䆹ᴵкⱘѸᯧ䆄ᔩ᠔ᇍᑨⱘкDŽ ‰ ID˖Џ䬂 ID ߯ᓎњѸᯧ㸼ৢˈݡএ䆒䅵кⱘѸᯧ㸼ˈ݋ԧᄫ↉Ϣкⱘܹᑧ䆄ᔩ㸼ⳌԐ˖ ); PRIMARY KEY (`ID`) `RECORD_DATE` datetime,-- Ѹᯧ᮹ᳳ `ID` int AUTO_INCREMENT NOT NULL, CREATE TABLE IF NOT EXISTS `T_SALE_RECORD` ( -- Ѹᯧ䆄ᔩ㸼, ϔϾѸᯧ䆄ᔩࣙᣀ໮Ͼкⱘ䫔ଂ䆄ᔩ, ϔ⃵Ѹᯧৃ㛑᳝໮ᴀк ߯ᓎ䆹㸼ⱘ SQL བϟDŽ ‰ RECORD_DATE˖Ѹᯧ᮹ᳳDŽ ‰ ID˖ID Џ䬂DŽ ㋏DŽ䫔ଂ䆄ᔩ㸼ⱘᄫ↉བϟ˖ ᇍᑨ໮ᴵкⱘ䫔ଂ䆄ᔩˈϔ⃵䫔ଂ᠔⍝ঞ໮ᴀкˈϢܹᑧ䆄ᔩϔḋˈ䛑䗮䖛ϔϾЁ݇㋏㸼ᴹԧ⦄䖭⾡݇ 䫔ଂ䆄ᔩ㸼Ϣܹᑧ䆄ᔩ㸼໻ৠᇣᓖˈ䛑ᰃ䳔㽕߯ᓎϔϾ䫔ଂ㸼ˈݡ߯ᓎкⱘ䫔ଂ㸼DŽϔᴵ䫔ଂ䆄ᔩ 9.3.5 䆒䅵䫔ଂ䆄ᔩ㸼 ߯ᓎᅠњ䖭ϸϾ㸼Пৢˈህᅠ៤њкⱘܹᑧⳌ݇㸼ⱘ䆒䅵DŽ ࢶdއസޙۤd࿙ҮωϢ༓ྑၽ೴࡬࣏ᄯඔ٢ᆓխēᄚ༓ྑၽё༝ᄯࠩ໻ ᅟġၽద࣏ζᄯēωਠပඔ٢ద࣏ᆙ೴२უّᆓխēუಾဎဟద࣏ᆙ೴२ಾْ·ೠԅద࣏೴२ԅᆙ ); PRIMARY KEY (`ID`) FOREIGN KEY (`T_IN_RECORD_ID_FK`) REFERENCES `T_IN_RECORD` (`ID`), -- ໄᯢܹᑧ䆄ᔩ໪䬂 FOREIGN KEY (`BOOK_ID_FK`) REFERENCES `T_BOOK` (`ID`), -- ໄᯢкⱘ໪䬂 `IN_SUM` int(10), -- ܹᑧ᭄䞣 `T_IN_RECORD_ID_FK` int, -- ᇍᑨⱘܹᑧ䆄ᔩ `BOOK_ID_FK` int, -- ܹᑧⱘк `ID` int AUTO_INCREMENT NOT NULL, -- ID 㞾๲ CREATE TABLE IF NOT EXISTS `T_BOOK_IN_RECORD` ( -- кⱘܹᑧ䆄ᔩ ߯ᓎⱘ SQL 䇁হབϟDŽ ‰ IN_SUM˖䖭ϔᴵкⱘܹᑧ䆄ᔩЁᇍᑨⱘкⱘܹᑧ᭄䞣DŽ 䖭Ͼ݇㋏г㸼⼎њϔ⃵ܹᑧৃҹ᳝໮ᴵкⱘܹᑧ䆄ᔩDŽ ‰ T_IN_RECORD_ID_FK˖ܹᑧ䆄ᔩⱘ໪䬂ˈ㸼⼎䖭ϔᴵкⱘܹᑧ䆄ᔩ᠔ᇍᑨⱘᰃાϔ⃵ܹᑧˈ ‰ BOOK_ID_FK˖кⱘ໪䬂ˈ㸼⼎䖭ϔᴵкⱘܹᑧ䆄ᔩ᠔ᇍᑨⱘкDŽ ‰ ID˖䆹㸼ⱘЏ䬂 ID ⼎ϔ⃵ܹᑧЁ᠔⍝ঞⱘкDŽ߯ᓎ T_BOOK_IN_RECORD 㸼ˈ䆹㸼ⱘᄫ↉བϟ˖ 䆒䅵ᅠܹᑧ䆄ᔩৢˈ៥Ӏ䖬䳔㽕㗗㰥ˈϔ⃵ܹᑧ⍝ঞњ໮ᴀкˈ಴ℸ䳔㽕⏏ࡴϔϾ݇㋏㸼ˈ⫼ᴹ㸼 ); PRIMARY KEY (`ID`) `RECORD_DATE` datetime, -- ܹᑧ᮹ᳳ `ID` int AUTO_INCREMENT NOT NULL, CREATE TABLE IF NOT EXISTS `T_IN_RECORD` ( -- ܹᑧ䆄ᔩ㸼, ϔ⃵ܹᑧӮܹ໮ᴀк ҹϟЎ߯ᓎ䆹㸼ⱘ SQLDŽ /Download at http://www.pin5i.com public class Concern extends ValueObject { ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\vo\Concern.java Concern ㉏ҷⷕབϟDŽ ҷ㸼ϔϾߎ⠜⼒ˈ䆹㉏㒻ᡓѢ ValueObjectˈℸ໪ˈߎ⠜⼒㸼Ё᳝ⱘᄫ↉ˈ䛑䳔㽕೼䖭Ͼ㉏ЁডᑨߎᴹDŽ ᓎゟњ⠊㉏ ValueObject ৢˈℸᯊݡএᮄᓎߎ⠜⼒᠔ᇍᑨⱘᅲԧ Concern ㉏ˈϔϾ Concern ᇍ䈵 ḋˈ೼ϟ䴶ゴ㡖ᇚӮ䆺㒚䆆䗄DŽ ߫ৡᄫᇍᑨˈ⬅Ѣ䖭Ͼᰃ৘Ͼ㸼ᇍ䈵ⱘ⠊㉏ˈ಴ℸ䳔㽕㑺ᅮ↣Ͼ㸼ⱘЏ䬂ੑৡᖙ乏Ў IDˈЎҔМ䳔㽕䖭 䆹㉏াᦤկњϔϾ ID ᄫ↉ˈᑊᦤկ setter ੠ getter ᮍ⊩ˈ⊼ᛣˈID ሲᗻᖙ乏Ϣ᭄᥂ᑧЁ㸼ⱘ ID } //ⳕ⬹ setter ੠ getter ᮍ⊩ private String ID; //ID ᄫ↉,ᇍᑨ᭄᥂ᑧЁⱘ ID ߫ public class ValueObject { ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\vo\ValueObject.java ᮄᓎ৘Ͼ㸼ᇍᑨᇍ䈵ⱘ⠊㉏ ValueObjectˈValueObject ⱘҷⷕབϟDŽ Ѣ↣Ͼ㸼Ё䛑᳝ ID ϔ߫ˈ಴ℸ៥ӀৃҹᓎゟϔϾ⠊㉏ˈᦤկ ID ᄫ↉DŽ ᓎゟϔϾ⠊㉏ˈ⬅ܜ䳔㽕Ў䖭ѯ㸼ᓎゟⳌᑨⱘᇍ䈵ˈ↣ϔϾ㸼ৃҹᇍᑨϔϾᇍ䈵DŽৃҹЎ↣Ͼ㸼ⱘᇍ䈵 ೼ 9.3 ЁˈᏆ㒣䆒䅵њ㋏㒳᠔䳔㽕ⱘ৘Ͼ㸼ˈࣙᣀߎ⠜⼒㸼ǃк⾡㉏㸼ǃк㸼ㄝˈ䙷М⦄೼ˈ៥Ӏ 9.4.1 䆒䅵㸼ⱘᇍᑨ㉏ DŽסञࡳ 㓪ݭདˈݡএ䖯㸠ϮࡵᓔথˈࠄϮࡵᓔথⱘᯊ׭ˈৃҹџܜࣙᣀᶹ䆶ǃׂᬍㄝDŽᇚϔѯৃҹ䞡⫼ⱘҷⷕ ໛Ꮉ԰ˈᰃᣛ㓪ݭϔѯ݀⫼ⱘᮍ⊩ˈ՟བ JDBC ⱘ෎ᴀ᪡԰ˈޚህৃҹ䖯㸠ᓔথˈᴀᇣ㡖Ё᠔䆆ⱘᓔথ ໛ᓔথⱘᎹ԰њDŽ݊ᅲᔧ䆒䅵ད᭄᥂ᑧˈ៥Ӏޚ೼ 9.3 ᇣ㡖Ёˈ᭄᥂ᑧᏆ㒣䆒䅵དњˈ᥹ϟᴹৃҹ ໛Ꮉ԰ޚ9.4 ᓔথࠡⱘ 䖯㸠᭄᥂ᑧ᪡԰ㄝⶹ䆚DŽ ⱘ㸼Ⳍᇍ໡ᴖњϔ⚍DŽ೼ϟϔ㡖Ёˈ៥Ӏᇚ䆆㾷བԩՓ⫼ Java ⱘডᇘ䖯㸠ᇍ䈵Ϣ㸼ⱘ᯴ᇘϢՓ⫼ JDBC ࠄℸˈ᭄᥂ᑧ৘Ͼ㸼ⱘ㒧ᵘᏆ㒣䆒䅵ᅠ៤њˈ৘Ͼ㸼ⱘ㒧ᵘ䛑कߚㅔऩˈা᳝ᑧᄬϢ䫔ଂϸϾ῵ഫ २ൎࢅՇԅd ᅟġ߬྾ζᄯωਠပඔ٢߬྾ᆙޮۤ߬྾ᆙ೴२უ०ّᆓխēუಾဎဟუ०ّᄔಾဎْ·ೠԅ߬྾ ); PRIMARY KEY (`ID`) FOREIGN KEY (`T_SALE_RECORD_ID_FK`) REFERENCES `T_SALE_RECORD` (`ID`), FOREIGN KEY (`BOOK_ID_FK`) REFERENCES `T_BOOK` (`ID`), `TRADE_SUM` int(10), -- 䫔ଂ᭄䞣 `T_SALE_RECORD_ID_FK` int, -- 䆹кⱘ䫔ଂ䆄ᔩ᠔ᇍᑨⱘѸᯧ䆄ᔩ `BOOK_ID_FK` int, -- 䫔ଂⱘк `ID` int AUTO_INCREMENT NOT NULL, CREATE TABLE IF NOT EXISTS `T_BOOK_SALE_RECORD` ( -- кⱘ䫔ଂ䆄ᔩ, ϔᴵ䆄ᔩᇍᑨϔᴀк ߯ᓎкⱘܹᑧ䆄ᔩ㸼ⱘ SQL བϟDŽ ‰ TRADE_SUM˖䆹䆄ᔩЁᇍᑨⱘкⱘѸᯧ᭄䞣DŽ T_SALE_RECORD_ID_FK˖䆹ᴵкⱘѸᯧ䆄ᔩ᠔ᇍᑨⱘѸᯧ䆄ᔩDŽ ‰ /Download at http://www.pin5i.com public class SaleRecord extends ValueObject { 䫔ଂ䆄ᔩ㉏ SaleRecordˈҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\vo\SaleRecord.java } //ⳕ⬹ setter ੠ getter ᮍ⊩ private String IN_SUM; //ܹᑧ᭄䞣 private String T_IN_RECORD_ID_FK; //ᇍᑨ䫔ଂ䆄ᔩ໪䬂 private String BOOK_ID_FK; //ᇍᑨкⱘ໪䬂, Ң᭄᥂ᑧᶹߎᴹᯊ᳝ؐ public class BookInRecord extends ValueObject { кⱘܹᑧ䆄ᔩ㉏ BookInRecordˈҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\vo\BookInRecord.java } //ⳕ⬹ getter ੠ setter ᮍ⊩ private String RECORD_DATE; //ܹᑧ᮹ᳳ public class InRecord extends ValueObject { ܹᑧ䆄ᔩ㉏ InRecordˈҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\vo\InRecord.java ⱘ㉏ǃ䫔ଂ䆄ᔩ㸼ᇍᑨⱘ㉏੠кⱘ䫔ଂ䆄ᔩ㸼ᇍᑨⱘ㉏DŽ ߯ᓎᅠϝϾ෎⸔᭄᥂㸼᠔ᇍᑨⱘ㉏ৢˈ᥹ϟᴹݡএ߯ᓎܹᑧ䆄ᔩ㸼ᇍᑨⱘ㉏ǃкⱘܹᑧ䆄ᔩ㸼ᇍᑨ } private String IMAGE_URL; //೒⠛ url 䞣ټ private String REPERTORY_SIZE; //ᄬ private String PUB_ID_FK; //ߎ⠜⼒໪䬂 private String TYPE_ID_FK; //⾡㉏໪䬂 private String BOOK_PRICE; //кⱘऩӋ private String BOOK_INTRO; //ㅔҟ private String BOOK_NAME; //кᴀৡ⿄ public class Book extends ValueObject { Book ⱘҷⷕབϟˈҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\vo\Book.java } //ⳕ⬹ setter ੠ getter ᮍ⊩ private String TYPE_INTRO; //ㅔҟ private String TYPE_NAME; //ৡ⿄ public class Type extends ValueObject { Type ⱘҷⷕབϟˈҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\vo\Type.java ⱘᇍ䈵˄Concern˅ˈ঺໪᠔᳝ሲᗻ㉏ൟ䛑䳔㽕Ў StringDŽ ੠ߎ⠜⼒ⱘ໪䬂 PUB_ID_FKˈৠḋഄгᦤկ䖭ϸϾ㉏ሲᗻˈᑊϡᰃᦤկ⾡㉏ⱘᇍ䈵˄Type˅੠ߎ⠜⼒ Book ҷ㸼к㉏ൟDŽ೼ Book ㉏Ё䳔㽕⊼ᛣⱘᰃˈ⬅Ѣк㸼Ё᳝ϸϾ䬂ˈߚ߿ᰃ⾡㉏ⱘ໪䬂 TYPE_ID_FK ᦤկ ID ᄫ↉DŽᣝ✻ᓎゟ Concern ᇍ䈵ⱘᮍ⊩ˈݡএᓎゟ Type ᇍ䈵ǃBook ᇍ䈵ˈType ҷ㸼кᴀ㉏ˈ ⊼ᛣ৘Ͼሲᗻⱘੑৡˈᖙ乏㽕Ϣ᭄᥂ᑧЁ㸼ⱘᄫ↉ϔ㟈ˈ⬅Ѣ㒻ᡓњ ValueObject ㉏ˈ಴ℸϡ䳔㽕 } //ⳕ⬹৘Ͼሲᗻⱘ getter ੠ setter ᮍ⊩ private String PUB_INTRO; //ㅔҟ private String PUB_LINK_MAN; //㘨㋏Ҏ private String PUB_TEL; //ߎ⠜⼒⬉䆱 private String PUB_NAME; ߎ⠜⼒ৡ⿄// /Download at http://www.pin5i.com } } JDBC_PASS = properties.getProperty("jdbc.pass"); JDBC_USER = properties.getProperty("jdbc.user"); JDBC_URL = properties.getProperty("jdbc.url"); JDBC_DRIVER = properties.getProperty("jdbc.driver"); //㦋ᕫ䜡㕂ⱘ৘Ͼሲᗻ properties.load(is); //ࡴ䕑䕧ܹ⌕ static { public static String JDBC_PASS; //᭄᥂ᑧᆚⷕ public static String JDBC_USER; //᭄᥂ᑧ⫼᠋ৡ public static String JDBC_URL; //jdbc 䖲᥹ url public static String JDBC_DRIVER; //᭄᥂ᑧ偅ࡼ private static InputStream is = PropertiesUtil.class.getResourceAsStream(CONFIG); //䇏প䌘⑤᭛ӊ, 䆒㕂䕧ܹ⌕ private static String CONFIG = "/cfg/jdbc.properties";//䜡㕂᭛ӊⱘ䏃ᕘ private static Properties properties = new Properties();//䆹䆄ᔩ᠔ᇍᑨⱘкⱘ䫔ଂ᭄䞣 public class PropertiesUtil { ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\jdbc\PropertiesUtil.java ㉏ˈҷⷕབϟDŽ ᓎゟད䆹᭛ӊৢˈݡ㓪ݭ㉏এ䇏প䆹᭛ӊˈ㦋ᕫ᠔䳔㽕ⱘؐेৃDŽ⫼Ѣ䇏প䜡㕂ⱘ PropertiesUtil jdbc.pass=book //ᆚⷕ jdbc.user=book //᭄᥂ᑧ⫼᠋ৡ jdbc.url=jdbc:mysql://localhost:3306/book_system //䖲᥹ഄഔ jdbc.driver=com.mysql.jdbc.Driver //JDBC 偅ࡼ ᓎゟ䜡㕂᭛ӊ jdbc.propertiesˈݙᆍབϟ˖ ᭛ӊेৃDŽ ഄഔǃ⫼᠋ৡ੠ᆚⷕˈ៥Ӏৃҹᬒࠄ䜡㕂᭛ӊЁˈབᵰ䳔㽕᳈ᤶ᭄᥂ᑧ៪㗙ഄഔˈা䳔㽕ׂᬍ䖭ӑ䜡㕂 ⬅Ѣᴀゴ⍝ঞࠄ᭄᥂ᑧ᪡԰ˈ಴ℸϢ᭄᥂ᑧⳌ݇ⱘϔѯ䜡㕂ˈ՟བᇍᑨ᭄᥂ᑧⱘⳌ݇偅ࡼǃ᭄᥂ᑧ 9.4.2 㓪ݭ䜡㕂䇏প㉏ ೼ᓔথ䖛⿟Ё᠔ԧ⦄ⱘ԰⫼DŽ ࠄ⦄೼ˈϢ㋏㒳Ⳍ݇ⱘ৘Ͼ㸼᠔ᇍᑨⱘ㉏䛑Ꮖ㒣㓪ݭདњˈϟ䴶ᇣ㡖ˈ៥Ӏᇚ䆆㾷䖭ѯᓎゟདⱘ㉏ } //ⳕ⬹ setter ੠ getter ᮍ⊩ private String TRADE_SUM; //䆹䆄ᔩ᠔ᇍᑨⱘкⱘ䫔ଂ᭄䞣 private String T_SALE_RECORD_ID_FK; //䆹䆄ᔩᇍᑨⱘ䫔ଂ䆄ᔩⱘ໪䬂 private String BOOK_ID_FK; //䆹䆄ᔩᇍᑨⱘкⱘ໪䬂 public class BookSaleRecord extends ValueObject { ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\vo\BookSaleRecord.java кⱘ䫔ଂ䆄ᔩ㉏ BookSaleRecordDŽ } //ⳕ⬹ setter ੠ getter ᮍ⊩ private String RECORD_DATE; //Ѹᯧ᮹ᳳ /Download at http://www.pin5i.com } return jdbcExecutor; if (jdbcExecutor == null) jdbcExecutor = new JDBCExecutor(); //བᵰᴀ㉏᠔㓈ᡸ jdbcExecutor ሲᗻЎぎ,߭䇗⫼⾕᳝ⱘᵘ䗴఼㦋ᕫᅲ՟ public static JDBCExecutor getJDBCExecutor() { //ᦤկϔϾ䴭ᗕᮍ⊩䖨ಲᴀ㉏ⱘᅲ՟ } stmt = connection.createStatement(); //߯ᓎ Statement ᇍ䈵 connection = DriverManager.getConnection(URL, USER, PASS); //߯ᓎ᭄᥂ᑧ䖲᥹ Class.forName(DRIVER); //߱ྟ࣪ JDBC 偅ࡼᑊ䅽偅ࡼࡴ䕑ࠄ jvm Ё private JDBCExecutor() { //⾕᳝ᵘ䗴఼ ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\jdbc\JDBCExecutor.java ϟ䴶೼ JDBCExecutor ⱘᵘ䗴఼Ё߯ᓎ৘Ͼᇍ䈵ˈݡᦤկϔϾᮍ⊩䖨ಲ JDBCExecutor ⱘᅲ՟DŽ ߣ೭d JDBCExecutor ಾӦൟԅē࿙Үࢶྻͬᄃၽ࿫ဈᄯᄚҗߙྡྷّ ConnectionēӦൟ੦಴ߜၽຏྡྷ໌ࠋᄯື຅ ႏ୶ᄯҗߙ ConnectionēဎဟٲJDBCExecutor ճເēࢶྻၽ ّྡྷܙ੦಴ē௾ JDBCExecutor फป ēဎဟҗߙྡྷّ Connection ճເ༓ྑۛ׎۳Ӗԅᆇၗē࿙ҮแਬರဈӦൟדᅟġྻ౨ӝ৲ԅ۱඘Ϧ private Statement stmt; //㓈ᡸϔϾᴀ㉏ൟⱘᇍ䈵 private static JDBCExecutor jdbcExecutor; //㓈ᡸϔϾᴀ㉏ൟⱘᇍ䈵 private Connection connection; //䖲᥹ᇍ䈵 private static String PASS = PropertiesUtil.JDBC_PASS; //㦋ᕫ䖲᥹᭄᥂ᑧⱘᆚⷕ private static String USER = PropertiesUtil.JDBC_USER; //㦋ᕫ䖲᥹᭄᥂ᑧⱘ⫼᠋ৡ private static String URL = PropertiesUtil.JDBC_URL; //㦋ᕫ url private static String DRIVER = PropertiesUtil.JDBC_DRIVER; //㦋ᕫ偅ࡼ ᮄᓎ JDBCExecutor ㉏ˈ䆹㉏݋᳝ሲᗻབϟ˖ 䳔㽕ᗢḋএ䖲᥹ˈ᥹ⴔ៥Ӏ䳔㽕䖭Ͼ㉏ᐂ៥Ӏᦤկᶹ䆶ǃᠻ㸠 SQL ㄝࡳ㛑DŽ⹂ᅮདⳂᷛৢˈᓔྟ㓪ݭDŽ 㚃ᅮᰃᐂ៥Ӏ䖯㸠᭄᥂ᑧ䖲᥹ˈ៥ӀПࠡ䜡㕂њ䖲᥹ⱘⳌ݇ሲᗻˈԚᰃ⿟ᑣᑊϡⶹ䘧៥ӀܜѯҔМDŽ佪 خ䜡㕂དњПৢˈ៥Ӏৃҹᓔྟⴔ᠟㓪ݭ᭄᥂ᑧⱘ᪡԰㉏ˈԚ೼䙷Пࠡˈ៥Ӏ䳔㽕ᯢ⹂䖭Ͼ㉏ᐂ៥ ip:3306/book_systemˈ⫼᠋ৡᆚⷕЎԴⱘMySQL⫼᠋ᆚⷕDŽ 偅ࡼ໪ˈ䖬䳔㽕䜡㕂᭄᥂ᑧⱘ䖲᥹ഄഔǃ⫼᠋ৡ੠ᆚⷕˈjdbc.url=jdbc:mysql:// ᳡ࡵ఼ http://dev.mysql.com/downloads/connector/j/5.1.htmlϟ䕑ˈϟ䕑ৢᇚ偅ࡼࣙࡴࠄ⦃๗ব䞣ЁDŽ䰸њ䜡㕂 ⱘ᭄᥂ᑧ偅ࡼࣙDŽᴀ՟ЁՓ⫼ⱘ偅ࡼࣙ⠜ᴀЎ 5.1.6 ˈབᵰ䳔㽕᳔ᮄⱘ偅ࡼ⿟ᑣˈ䇋ࠄ ⱘjdbc.driverሲᗻˈህᰃ᭄᥂ᑧⱘ䖲᥹偅ࡼˈ೼ᴀ՟Ё៥ӀՓ⫼њMySQL᭄᥂ᑧˈ಴ℸ䳔㽕ᦤկMySQL ೼ 9.4.2 Ёˈ៥ӀᏆ㒣ᦤկњ䜡㕂ˈৃҹׂᬍᇍᑨⱘ䜡㕂᭛ӊᴹ䖲᥹᭄᥂ᑧˈjdbc.properties᭛ӊЁ ৠⱘ᭄᥂ᑧᦤկϡৠⱘ偅ࡼˈህৃҹ䖒ࠄ䖲᥹ϡৠ᭄᥂ᑧⱘ㽕∖DŽ 㒳ϔⱘ SQL ᭄᥂ᄬপ᥹ষˈৃҹ䅽⿟ᑣਬϡ䳔㽕݇ᖗབԩএ䖲᥹ϡৠⱘ᭄᥂ᑧˈা䳔Ўϡޚᦤկњᷛ JDBC ᰃ Java Data Base Connectivity ⱘㅔ⿄ˈᰃ Java Ё䖯㸠᭄᥂ᑧ䖲᥹ⱘᡔᴃDŽJDBC ⱘ API 9.4.3 㓪ݭJDBC᪡԰㉏ 䇏পⱘ৘Ͼ䜡㕂ⱘ԰⫼ˈᇚ೼ 9.4.3 Ё԰䆺㒚䆆㾷DŽ /Download at http://www.pin5i.com //䘡ग़᠔᳝ⱘሲᗻ Field[] allFields = addFields(superFields, fields); //⠊㉏ⱘሲᗻ੠㞾ᏅⱘሲᗻⳌࡴ Field[] superFields = clazz.getSuperclass().getDeclaredFields(); //㦋প⠊㉏ⱘሲᗻ Field[] fields = clazz.getDeclaredFields(); //㦋পᴀᇍ䈵ⱘሲᗻ Object vo = clazz.newInstance(); //߯ᓎ㉏ⱘᅲ՟ while (rs.next()) { public static Collection getDatas(Collection result, ResultSet rs, Class clazz) { //ᇚ rs Ёⱘؐᇕ㺙៤ϔϾ䲚ড় ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\commons\DataUtil.java ⷕབϟDŽ Ͼؐ԰Ў䖭ѯ㉏˄㸼ᇍ䈵˅ⱘሲᗻˈᑊ䋳䋷߯ᓎ䖭ѯᇍ䈵ˈᬒࠄ䲚ড়ЁএDŽᮄᓎ DataUtil ㉏ˈ݋ԧⱘҷ ೼ 9.4.1 Ёˈ៥Ӏ䆒䅵њ৘Ͼ㸼᠔ᇍᑨⱘ㉏ˈ䙷М೼ᇕ㺙ⱘ䖛⿟Ёˈᇚ㒧ᵰ䲚˄ResultSet˅Ёⱘᶤ ㉏ˈ䖯䆹㒧ᵰ䲚䖯㸠ᇕ㺙ˈᑊ䖨ಲᇍᑨⱘ䲚ড়DŽ ៥Ӏᑊ≵᳝݇䯁 ResultSetˈ䖭ᰃ⬅Ѣ៥Ӏ䳔㽕ᇍ䆹㒧ᵰ䲚䖯㸠ϔѯᇕ㺙DŽ೼ᴀᇣ㡖ЁᇚᓎゟϔϾᎹ݋ ೼ 9.4.3 ЁˈJDBCExecutor ЁᦤկњϔϾ executeQuery ᮍ⊩ˈ䆹ᮍ⊩䖨ಲ ResultSet ᇍ䈵ˈᔧᯊ 9.4.4 ߯ᓎ᭄᥂䕀ᤶᎹ݋㉏ ݭᅠњDŽ ೼ҹϞҷⷕЁˈᇚӮ䖨ಲᠻ㸠䆹হ SQL ᠔ѻ⫳ⱘЏ䬂DŽࠄ䖭䞠ˈ៥Ӏⱘ JDBCExecutor ㉏Ꮖ㒣㓪 } return result; rs.close(); while(rs.next()) result = rs.getInt(1); //䖨ಲ᳔ৢϔϾЏ䬂 ResultSet rs = stmt.getGeneratedKeys(); //㦋ᕫЏ䬂 stmt.executeUpdate(sql); //ᠻ㸠 SQL 䇁হ int result = -1; public int executeUpdate(String sql) { //ᠻ㸠ऩহ INSERTǃUPDATE ៪ DELETE 䇁হ, བᵰᠻ㸠 INSERT ᯊ, 䖨ಲЏ䬂 㓪ݭᠻ㸠 SQL ⱘᮍ⊩ˈҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\jdbc\JDBCExecutor.java ᇍ䈵䖯㸠ϔѯ໘⧚ˈ಴ℸ೼䖭䞠ϡ䖯㸠݇䯁᪡԰ˈ䆹ᮍ⊩াᰃㅔऩⱘ䖯㸠ᶹ䆶DŽ ೼Ϟ䴶ⱘҷⷕЁˈᑊ≵᳝݇䯁 ResultSet ⱘҷⷕˈⳈ᥹䖨ಲ ResultSet ᇍ䈵ˈ៥Ӏ䖬䳔㽕ᇍ ResultSet } return result; ResultSet result = stmt.executeQuery(sql); //߽⫼ Statement ᇍ䈵ᠻ㸠খ᭄ⱘ sql public ResultSet executeQuery(String sql) { //ᠻ㸠ϔহᶹ䆶ⱘ sql, ᑊ䖨ಲ ResultSet ᇍ䈵 㓪ݭᠻ㸠ᶹ䆶ⱘᮍ⊩ˈҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\jdbc\JDBCExecutor.java ࡁൟֺ֥җߙ JDBCExecutor ಬैd ઝϦԅྡྷّލ൑ᆑڶႏ୶௾ୣ൐फ௠җߙ JDBCExecutor ԅಬैēෳ࠘ᄚટඹٲpublic ԅ ࿙ҮϢࢶඔ٢ ႏ୶ē࿙น༓ྑͬћუّफᄚҗߙྡྷұēٲᅟġၽྻ౨ӝ৲ᄯēඔ٢ॴྡྷّ JDBCExecutor ԅഒပ /Download at http://www.pin5i.com ResultSetǃConnection ੠ Statement ㄝᇍ䈵DŽ Ёˈ៥ӀৃҹⳈ᥹㓪ݭ SQLˈᕫࠄ㒧ᵰ䲚ˈӴ䗦㒭 DataUtil ㉏ˈ䅽ᅗ䕀ᤶ៤䲚ড়ˈ៥Ӏϡݡ䳔㽕এ໘⧚ Փ⫼ Java ⱘডᇘ䖯㸠ᄫ↉Ϣ㉏ሲᗻⱘ᯴ᇘˈ䖭ѯ䛑Ў៥Ӏৢ䴶ⱘࡳ㛑ᓔথᠧདњ෎⸔ˈ೼ᓔথⱘ䖛⿟ ໛Ꮉ԰Ꮖ㒣ܼ䚼ᅠ៤њˈ೼ᴀᇣ㡖Ёˈᦣ䗄њབԩ䖲᥹ JDBC 䖯㸠᭄᥂ᑧ䖲᥹ˈޚࠄ䖭䞠ˈᓔথⱘ ݇䯁 ResultSet ᇍ䈵DŽ䖭гᰃЎҔМ೼ JDBCExecutor ϡ䳔㽕݇䯁 ResultSet ⱘॳ಴DŽ ㅔऩⱘ䇈ˈDataUtil ህᰃḍ᥂ ResultSet ᴹ߯ᓎ ValueObject ⱘ䲚ড়ˈᔧ߯ᓎᅠৢˈህ䳔㽕೼䖭䞠 getDatas ⱘ clazz খ᭄ӴܹˈҢ㗠߯ᓎӴܹ㉏ⱘᇍ䈵DŽ Ё᳝ϔϾ ID ሲᗻˈህᇍᑨњ৘Ͼ㸼Ёⱘ ID ᄫ↉DŽValueObject ϟ䴶ⱘ৘Ͼᄤ㉏ⱘ class 䛑ৃҹ԰Ў ҹḍ᥂㉏ⱘሲᗻৡ⿄ᕫࠄ ResultSet Ёᇍᑨⱘᄫ↉ؐDŽ೼ᴀ՟Ёˈ⠊㉏ህᰃ ValueObjectˈValueObject ᗻੑৡ䛑䳔㽕Ϣ᭄᥂ᑧЁⱘ㸼ᄫ↉ϔ㟈ˈ೼䖭䞠ˈ៥ӀᕜᆍᯧህৃҹᕫࠄᶤϾᄫ↉ⱘ setter ᮍ⊩ˈᑊৃ ሲᗻˈݡ䗮䖛 setter ᮍ⊩ᇚ ResultSet Ёᇍᑨⱘؐ䆒㕂ࠄᇍ䈵ЁDŽ៥Ӏ೼ 9.4.1 Ёˈ㑺ᅮњ৘Ͼ㉏ⱘሲ Փ⫼ class ⱘ newInstance ᮍ⊩߯ᓎ㉏ⱘᅲ՟ˈݡ㦋ᕫ⠊㉏੠ᴀ㉏ⱘܜˈ⊼ᛣҹϞҷⷕⱘ咥ԧ䚼ߚ } return l.toArray(new Field[f1.length + f2.length]); for (Field f : f2) l.add(f); for (Field f : f1) l.add(f); List l = new ArrayList(); private static Field[] addFields(Field[] f1, Field[] f2) { //ⳌࡴϸϾ᭄㒘 } return methodName; String methodName = "set" + begin + end; String end = fieldName.substring(1, fieldName.length()); String begin = fieldName.substring(0, 1).toUpperCase(); private static String getSetterMethodName(String fieldName) { //ḍ᥂ሲᗻৡ㦋ᕫ setter ᮍ⊩ⱘᮍ⊩ৡ } setterMethod.invoke(vo, value); //Ң ResultSet Ё㦋পϢ䆹ᇍ䈵ሲᗻৡϔ㟈ⱘᄫ↉, ᑊᠻ㸠 setter ᮍ⊩ String value = rs.getString(field.getName()); //ᔧՓ⫼ ResultSet 㦋পᶤϾᄫ↉ⱘᯊ׭, བᵰ≵᳝䆹ᄫ↉, Ӯߎ⦄ SQLException, ೼䖭䞠ᗑ⬹䆹ᓖᐌ Method setterMethod) { private static void invokeMethod(ResultSet rs, Field field, Object vo, //ᠻ㸠ϔϾᮍ⊩, Ң ResultSet Ё㦋পϔϾᄫ↉ⱘ᭄᥂, 䇗⫼ vo ⱘ setter ᮍ⊩ } return result; rs.close(); } result.add(vo); } invokeMethod(rs, field, vo, setterMethod); Method setterMethod = clazz.getMethod(setterMethodName, field.getType()); //㦋ᕫ setter ᮍ⊩ String setterMethodName = getSetterMethodName(field.getName()); //㦋ᕫ setter ᮍ⊩ⱘᮍ⊩ৡ for (Field field : allFields) { /Download at http://www.pin5i.com 䳔㽕ᅲ⦄ⱘᮍ⊩ findAllˈҹϟЎ findAll ⱘ᥹ষᮍ⊩ঞᳳᅲ⦄DŽ ᮄᓎ ConcernDao ⱘᅲ⦄㉏˄᭄᥂䆓䯂ሖⱘᅲ⦄㉏˅ConcernDaoImplˈЎ ConcernDao ⏏ࡴϔϾ 9.5.2 㦋পܼ䚼ߎ⠜⼒ ConcernDao ӴܹDŽ ܹࠄ ConcernPanel˄ߎ⠜⼒ㅵ⧚⬠䴶˅ЁDŽৠḋⱘˈ೼ ConcernService ᅲ⦄㉏ⱘᵘ䗴఼Ёˈᇚ 䙷М೼㸼⦄ሖⱘ JPanel Ёˈ៥Ӏৃҹ೼ JPanel ⱘᵘ䗴఼Ёˈᇚ ConcernService ԰Ўᵘ䗴খ᭄Ӵ } //ᦤկ䳔㽕ᅲ⦄ⱘ᥹ষᮍ⊩ public interface ConcernService { ߎ⠜⼒Ϯࡵ᥹ষ˖ } //ᦤկ䳔㽕ᅲ⦄ⱘ᥹ষᮍ⊩ public interface ConcernDao { ߎ⠜⼒᭄᥂䆓䯂ሖ᥹ষ˖ དⱘ᭄᥂䲚ড়DŽ 䙷М೼ DAO Ё↣Ͼᅲ⦄㉏এ㒻ᡓ CommonDaoImpl ⱘ䆱ˈेৃՓ⫼ getDatas ᮍ⊩䖨ಲᏆ㒣ᇕ㺙 } } return DataUtil.getDatas(result, rs, clazz); //ᇍ ResultSet 䖯㸠ᇕ㺙ᑊ䖨ಲ䲚ড় ResultSet rs = getJDBCExecutor().executeQuery(sql); //ᠻ㸠 SQL 䖨ಲ ResultSet ᇍ䈵 Class clazz) { public Collection getDatas(String sql, Collection result, //ḍ᥂খ᭄ⱘ SQL, ᄬᬒ㒧ᵰⱘ䲚ড়ᇍ䈵, ੠݋ԧⱘ᭄᥂ᑧ᯴ᇘᇍ䈵䖨ಲϔϾ䲚ড় } return JDBCExecutor.getJDBCExecutor(); public JDBCExecutor getJDBCExecutor() { //䖨ಲ JDBCExecutor ᇍ䈵 public class CommonDaoImpl { ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\dao\impl\CommonDaoImpl.java 㓁ˈᦤկϔѯৃ㛑↣Ͼ DAO 䛑ӮՓ⫼ࠄⱘᮍ⊩DŽᮄᓎ CommonDaoImpl ᇍ䈵ˈ݋ԧҷⷕབϟDŽ ᭄᥂䆓䯂ሖᓎゟϔϾ⠊㉏ˈ㒭䆹ሖ৘Ͼ DAO ᅲ⦄㉏এ㒻ܜˈ៥Ӏ⦄೼ᓔྟᓔথ᭄᥂䆓䯂ሖⱘҷⷕ 䚼ߚᅲ⦄ⱘᯊ׭ˈⳈ᥹᳈ᤶᅲ⦄㉏ेৃDŽ ሖЁՓ⫼᭄᥂䆓䯂ሖⱘ᥹ষˈህㅫᅲ⦄থ⫳њᬍবˈгৃҹϡ⫼এ᳈ᬍ䇗⫼㗙ⱘҷⷕˈᔧ䳔㽕᳈ᬍᶤϔ বˈ䖒ࠄ䞡⫼ⱘⳂⱘDŽϮࡵሖϢ᭄᥂䆓䯂ሖߚ߿ᦤկ৘㞾ⱘ᥹ষˈ೼㸼⦄ሖЁՓ⫼Ϯࡵሖⱘ᥹ষˈϮࡵ ব࣪ˈ՟བϡݡՓ⫼ swing ԰Ў㸼⦄ሖˈՓ⫼ jsp ⱘ䆱ˈ䙷МˈϮࡵሖǃ᭄᥂䆓䯂ሖⱘҷⷕᇚϡ䳔㽕ᬍ ៥Ӏৃҹᇚ㋏㒳ߚЎϝሖ˖㸼⦄ሖǃϮࡵሖǃ᭄᥂䆓䯂ሖˈ䖭ḋߚሖⱘད໘೼Ѣˈབᵰ㾚೒ሖথ⫳ 9.5.1 ߚሖ㒧ᵘ ӏԩⱘ໪䬂ˈ⬠䴶Ёা᳝ϔϾㅔऩⱘ߫㸼੠㸼ऩˈᅲ⦄䍋ᴹ↨䕗ㅔऩDŽ Ңᴀᇣ㡖ᓔྟˈ៥Ӏᓔྟᅲ⦄㋏㒳ࡳ㛑ˈҢ᳔ㅔऩⱘߎ⠜⼒ㅵ⧚ࡳ㛑ᓔྟˈ⬅Ѣߎ⠜⼒㸼Ёϡ⍝ঞ ߎ⠜⼒ㅵ⧚ࡳ㛑 9.5 /Download at http://www.pin5i.com Vector v = new Vector(); for (Concern c : datas) { Vector view = new Vector(); private Vector changeDatas(Vector datas) { //ᇚ᭄᥂䕀ᤶ៤㾚೒㸼ḐⱘḐᓣ ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\ui\ConcernPanel.java ᥹ϟᴹˈህᰃ㸼⦄ሖབԩᕫࠄ᭄᥂ᑊ䖯㸠ᰒ⼎њDŽ ೼ϮࡵሖЁDŽ ∖ˈߎ⠜⼒ৡᄫЁ৿᳝㣅᭛ᄫ↡ⱘߎ⠜⼒ϡᰒ⼎ࠄ㸼⦄ሖЁˈ䙷М䋳䋷໘⧚䖭Ͼ䳔∖ⱘҷⷕˈህᑨ䆹ݭ བᵰ䳔㽕䖯㸠ϔѯϮࡵⱘ໘⧚ˈ䙷Мৃҹ೼Ϯࡵሖⱘᅲ⦄㉏Ё䖯㸠໘⧚DŽ՟བˈབᵰᶤ໽᳝ϔϾ䳔 ࢶdއၶԅྜ๠ēᄚ༓ྑԄӾ೴࡬؏ۥဎဟแਬუऺωਠပఉ ēუಾݍ৴౨ֲ܊ճເԄӾ೴࡬ู׀सēರဈ೴࡬҉ۥࠩ໻ఉۦނڴᅟġၽྜ๠Њᄯēωਠပճࠒ } } return dao.findAll(); public Collection getAll() { //Ⳉ᥹䖨ಲ᭄᥂ } this.dao = dao; public ConcernServiceImpl(ConcernDao dao) { //೼ᵘ䗴఼Ё䆒㕂 ConcernDao private ConcernDao dao; //᭄᥂䆓䯂ᇍ䈵 public class ConcernServiceImpl implements ConcernService { ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\service\impl\ConcernServiceImpl.java ConcernServiceImplDŽ } Collection getAll(); //㦋পܼ䚼ⱘߎ⠜⼒ public interface ConcernService { ConcernServiceˈҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\service\ConcernService.java ᥹ϟᴹˈݡএᅲ⦄Ϯࡵሖ˖ Ԣњҷⷕⱘ㗺ড়DŽޣˈेৃᇚ㒧ᵰᇕ㺙៤䲚ড় ໛Ꮉ԰ⱘ䞡㽕ᗻˈ៥Ӏϡ䳔㽕ݡএ໘⧚ ResultSet ᇍ䈵ˈޚⱘخᅮDŽ೼䖭䞠ˈህৃҹԧӮࠄ೼ 9.4 Ё᠔އ ⊼ᛣҹϞҷⷕЁⱘ咥ԧ䚼ߚˈ䇗⫼⠊㉏ⱘᮍ⊩䖨ಲ᭄᥂䲚ড়ˈ䖨ಲᗢḋⱘ᭄᥂䲚ড়ˈḍ᥂খ᭄㉏ൟ } } return getDatas(sql, new Vector(), Concern.class); //䇗⫼⠊㉏ⱘ getDatas ᮍ⊩䖨ಲ䲚ড়, 䖨ಲⱘ㉏ൟᰃ䲚ড়, 䲚ড়䞠䴶ᄬᬒⱘ㉏ൟᰃ Concern String sql = "SELECT * FROM T_PUBLISHER pub ORDER BY pub.ID DESC"; //ᶹᡒⱘ SQL public Collection findAll() { public class ConcernDaoImpl extends CommonDaoImpl implements ConcernDao { ConcernDaoImplˈҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\dao\impl\ConcernDaoImpl.java } Collection findAll(); //ᶹᡒܼ䚼ⱘߎ⠜⼒ᑊ䖨ಲ䲚ড় public interface ConcernDao { ConcernDaoˈҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\dao\ConcernDao.java /Download at http://www.pin5i.com ᰃ⠊㉏ⱘ initData ᮍ⊩ˈ݋ԧ䇋ⳟ 9.2.7 ゴ㡖DŽᬜᵰབ೒ 9.7 ᠔⼎DŽ ⠊㉏ᕫࠄ᭄᥂ǃJTable ᇍ䈵੠߫䲚ড়ৢˈህৃҹḍ᥂䖭ѯᇍ䈵ᴹ߯ᓎ߫㸼њDŽ߯ᓎ߫㸼ⱘᮍ⊩Џ㽕 } //ⳕ⬹݊Ҫ⬠䴶ҷⷕ setJTable(table); //䇗⫼⠊㉏ⱘ setJTable ᮍ⊩ JTable table = new CommonJTable(model); DefaultTableModel model = new DefaultTableModel(null, this.columns); //䆒㕂߫㸼 setViewDatas();//䆒㕂߫㸼᭄᥂ initColumns();//߱ྟ࣪߫䲚ড় this.service = service; public ConcernPanel(ConcernService service) { CommonPanel ᵘ䗴఼˖ ៥Ӏ೼Ϟ䴶᠔㓪ݭⱘ setViewDatas ᮍ⊩ˈৠḋⱘг䳔㽕೼ᵘ䗴఼Ё䇗⫼DŽ Ըဈ setJTable ֺ֥ēߜᆐफҗߙԅ JTable ಁᄢӾؓफᄯd ႏ୶ᄯē༓ྑٲᅟġၽ 9.2.7 ᄯēแਬߜྡྷ໔ӝ৲ඔӾؓफ CommonPanel ᄯē࿙Үēၽّْ࠘ੋԅ } setDatas(datas); //䇗⫼⠊㉏ⱘ setDatas ᮍ⊩ Vector datas = changeDatas(concerns); //Փ⫼Ϯࡵ᥹ষᕫࠄ᭄᥂ Vector concerns = (Vector)service.getAll();//Փ⫼Ϯࡵ᥹ষᕫࠄ᭄᥂ public void setViewDatas() { //ᅲ⦄⠊㉏ᮍ⊩ˈᶹ䆶᭄᥂ᑧᑊ䖨ಲᇍᑨⱘ᭄᥂Ḑᓣ, 䇗⫼⠊㉏ⱘ setDatas ᮍ⊩䆒㕂᭄᥂䲚ড় } return view; } view.add(v); v.add(c.getPUB_INTRO()); v.add(c.getPUB_TEL()); v.add(c.getPUB_LINK_MAN()); v.add(c.getPUB_NAME()); v.add(c.getID()); /Download at http://www.pin5i.com ৠḋഄˈ೼ϮࡵሖЁˈ៥Ӏгϡ䳔㽕ᇍ㒧ᵰ䲚䖯㸠໘⧚ˈা䳔㽕䖨ಲ᭄᥂ेৃDŽ } return dao.findByName(name); public Collection query(String name) { //ᅲ⦄ ConcernService ᥹ষⱘᮍ⊩ ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\service\impl\ConcernServiceImpl.java ⊼ᛣҹϞҷⷕⱘ like ݇䬂ᄫˈ䖭ᰃ MySQL ᦤկⱘ݇䬂ᄫDŽ } return getDatas(sql, new Vector(), Concern.class); //䇗⫼⠊㉏ᮍ⊩䖨ಲ㒧ᵰ䲚 name + "%' ORDER BY pub.ID DESC"; String sql = "SELECT * FROM T_PUBLISHER pub WHERE pub.PUB_NAME like '%" + //Փ⫼ like ݇䬂ᄫ public Collection findByName(String name) { //ᅲ⦄᥹ষ ConcernDao ⱘᮍ⊩ᮍ⊩ ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\dao\impl\ConcernDaoImpl.java MySQL ⱘᦤկϔϾ like ݇䬂ᄫˈৃҹᅲ⦄ SQL ⱘ῵㊞ᶹ䆶DŽ᭄᥂䆓䯂ሖϢϮࡵሖⱘᅲ⦄བϟDŽ ᴀ՟Ёᅲ⦄῵㊞ᶹ䆶ࡳ㛑कߚㅔऩˈ䗮䖛ߎ⠜⼒ⱘৡ⿄䖯㸠῵㊞ᶹ䆶ˈݡᇚ㒧ᵰ䲚䖨ಲࠄ㸼⦄ሖDŽ 9.5.4 ῵㊞ᶹ䆶 ೒ 9.7 㦋ᕫܼ䚼ߎ⠜⼒ /Download at http://www.pin5i.com public void setTableFace() { //䆒㕂㸼Ḑḋᓣ ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\ui\ConcernPanel.java ⱘᮍ⊩ˈ䳔㽕৘Ͼᄤ㉏এᅲ⦄DŽConcernPanel ⱘ setTableFace ᅲ⦄བϟDŽ ᄬᬒ᭄᥂ⱘ ID ؐˈᑊ䆒㕂 ID ߫Ў䱤㮣ˈৃҹ೼ setTableFace ᮍ⊩Ё䆒㕂ˈ䆹ᮍ⊩ᰃ CommonPanel ߎ⠜⼒߫㸼ⱘᯊ׭ˈ䳔㽕Ў߫㸼ⱘ JTable ᇍ䈵⏏ࡴϔ߫ˈϧ䮼⫼Ѣخ᥂ˈᑊሩ⦄೼㸼ऩЁDŽᔧ៥Ӏ೼ ᶹⳟߎ⠜⼒ˈ䳔㽕೼⚍ߏ߫㸼ⱘᯊ׭ˈᕫࠄ䆹ᴵ᭄᥂᠔ᇍᑨⱘ IDˈ✊ৢݡ䇗⫼Ϯࡵሖⱘᮍ⊩ᕫࠄ᭄ 9.5.4 ᶹⳟߎ⠜⼒ ೒ 9.8 ῵㊞ᶹ䆶 setDatas ᮍ⊩੠ refreshTable ᮍ⊩䆒㕂᭄᥂੠ࠋᮄ㸼ḐDŽᕫࠄⱘᬜᵰབ೒ 9.8 ᠔⼎DŽ ೼㸼⦄ሖⱘ ConcernPanel Ёˈ䗮䖛Ϯࡵ᥹ষᕫࠄ᭄᥂ৢˈݡ䖯㸠᭄᥂Ḑᓣ䕀ᤶˈ䇗⫼⠊㉏ⱘ } refreshTable();//ࠋᮄ߫㸼 setDatas(datas); //䆒㕂᭄᥂ Vector datas = changeDatas(concerns); //䕀ᤶ᭄᥂Ḑᓣ Vector concerns = (Vector)service.query(name); String name = this.queryName.getText(); private void query() { //ᣝৡᄫ῵㊞ᶹ䆶 ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\ui\ConcernPanel.java /Download at http://www.pin5i.com ᮍ⊩ˈᕫࠄߎ⠜⼒ᇍ䈵ৢˈݡᇚߎ⠜⼒ⱘ৘Ͼؐ䆒㕂ࠄᇍᑨⱘഄᮍDŽ ϮࡵሖϢПࠡⱘϔḋˈⳈ᥹䖨ಲ᭄᥂ˈϡᖙ㒣䖛ӏԩ໘⧚DŽ䙷М⦄೼ህৃҹএᅲ⦄㸼⦄ሖⱘ view } return dao.find(id); public Concern find(String id) { //ᅲ⦄᥹કᮍ⊩ ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\service\impl\ConcernServiceImpl.java བᵰ᭄᥂䲚ⱘ size ϡЎ 1 ⱘ䆱ˈ߭䖨ಲ nullDŽ ᭄᥂䆓䯂ሖЁˈϢᶹ䆶᠔᳝ߎ⠜⼒ᮍ⊩ϔḋˈাᰃᬍবњϔϟ SQLˈᕫࠄњⳌᑨⱘ᭄᥂䲚ˈݡ߸ᮁ } return (datas.size() == 1) ? datas.get(0) : null; List datas = (List)getDatas(sql, new ArrayList(), Concern.class); //㦋ᕫ᭄᥂ˈᑊ䖨ಲ㄀ϔᴵ String sql = "SELECT * FROM T_PUBLISHER pub WHERE pub.ID = '" + id + "'"; //ᶹ䆶ⱘ SQL public Concern find(String id) { //ᅲ⦄ ConcernDao ᥹કᮍ⊩ ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\dao\impl\ConcernDaoImpl.java ⱘഄᮍDŽ ಲ Concern ᅲԧˈᔧ㸼⦄ሖᕫࠄ݋ԧⱘᶤϾ Concern ৢˈህৃҹḍ᥂ᅗⱘ৘Ͼሲᗻˈ䆒㕂ࠄ䳔㽕ᰒ⼎ ᔧ䗝ᢽњϔ㸠᭄᥂ᯊˈህ䇗⫼ view ᮍ⊩䖯㸠ᶹⳟˈ᥹ϟᴹˈ㓪ݭϮࡵሖ੠᭄᥂䆓䯂ሖⱘҷⷕˈ䖨 ߫ⱘؐDŽ ⊼ᛣҹϞҷⷕⱘ咥ԧ䚼ߚˈ䇗⫼њ⠊㉏˄CommonPanel˅ⱘ getSelectId ᮍ⊩ˈ䖨ಲ䗝ᢽ㸠ⱘ id }); } } view(id); String id = getSelectId(getJTable()); //䇗⫼⠊㉏ⱘᮍ⊩㦋ᕫ䗝ᢽ㸠ⱘ id if (getJTable().getSelectedRowCount() != 1) return; //བᵰ≵᳝䗝Ёӏԩϔ㸠, ߭䖨ಲ if (!event.getValueIsAdjusting()) { //ᔧ䗝ᢽ㸠ᯊ哴ᷛ䞞ᬒᯊᠡᠻ㸠 public void valueChanged(ListSelectionEvent event) { getJTable().getSelectionModel().addListSelectionListener(new ListSelectionListener(){ //㸼Ḑ䗝ᢽⲥ਀఼ } //᱖ᯊぎᅲ⦄ private void view(String id) { //ᶹⳟϔϾߎ⠜⼒ ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\ui\ConcernPanel.java ᢽ㸠ⱘֵᙃᰒ⼎ࠄ߫㸼ЁDŽConcernPanel Ё⏏ࡴⲥ਀఼DŽ 䆒㕂њ߫㸼ⱘḋᓣৢˈ⦄೼ৃҹЎ߫㸼ⱘ Jtable ⏏ࡴⲥ਀఼ˈᔧ䗝ᢽϔ㸠᭄᥂ⱘᯊ׭ˈৃҹᇚ᠔䗝 } getJTable().setRowHeight(30); getJTable().getColumn("ㅔҟ").setMinWidth(400); getJTable().getColumn("id").setMaxWidth(-1); getJTable().getColumn("id").setMinWidth(-1); 䱤㮣 id ߫// /Download at http://www.pin5i.com ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\ui\ConcernPanel.java Ϟᮄ๲ϔϾᮄⱘߎ⠜⼒ⱘᯊ׭ˈৃҹ⚍ߏ⏙ぎᣝ䪂DŽ ⚍ߏֱᄬˈህᰃׂᬍ᪡԰ˈ≵᳝ؐ⚍ߏֱᄬᣝ䪂ˈህᰃᮄ๲᪡԰DŽᔧ⫼᠋ℷ೼ᶹⳟϔϾߎ⠜⼒ˈজᛇ偀 ৃҹⳟࠄ᳝ϔϾ䱤㮣ⱘ JTextField ᴹֱᄬߎ⠜⼒ⱘ IDˈгህᰃ䇈ˈᔧ䖭Ͼ䱤㮣ⱘ JTextField ᳝ؐⱘᯊ׭ˈ ᰃᔧࠡ㓪䕥ⱘ᭄᥂ᰃ৺᳝ IDˈ៥Ӏ೼ᶹⳟߎ⠜⼒ⱘᯊ׭ˈޚҔМᯊ׭ᰃᮄ๲ˈҔМᯊ׭ᰃׂᬍˈ߸ᮁⱘᷛ ЁDŽԚ೼ᭈϾ⬠䴶Ёˈা᳝ϔϾֱᄬᣝ䪂ˈгህᰃ䇈ˈ᮴䆎ᮄ๲䖬ᰃׂᬍ᪡԰ˈ䛑ᰃՓ⫼ৠϔϾᣝ䪂DŽ ᥹ϟᴹᅲ⦄ᮄ๲ߎ⠜⼒ⱘࡳ㛑ˈϢᶹⳟߎ⠜⼒ⱘࡳ㛑ⳌডˈᰃҢ䕧ܹḚЁᕫࠄ᭄᥂ˈݡݭ䖯᭄᥂ᑧ 9.5.5 ᮄ๲ߎ⠜⼒ ೒ 9.9 ᶹⳟߎ⠜⼒ ᶹⳟߎ⠜⼒ⱘࡳ㛑Ꮖ㒣ᅲ⦄ˈ݋ԧᬜᵰ䇋ⳟ೒ 9.9DŽ } this.pubIntro.setText(c.getPUB_INTRO());//䆒㕂ㅔҟⱘ᭛ᴀඳ this.pubTel.setText(c.getPUB_TEL());//䆒㕂㘨㋏⬉䆱ⱘ᭛ᴀḚ this.pubLinkMan.setText(c.getPUB_LINK_MAN());//䆒㕂㘨㋏Ҏⱘ᭛ᴀḚ this.concernName.setText(c.getPUB_NAME());//䆒㕂ߎ⠜⼒ৡ⿄ⱘ᭛ᴀḚ this.concernId.setText(c.getID());//䆒㕂 ID ⱘ JTextFiled˄䱤㮣˅ Concern c = service.find(id); //䗮䖛Ϯࡵ᥹ষᕫࠄߎ⠜⼒ᇍ䈵 private void view(String id) { //ᶹⳟϔϾߎ⠜⼒ ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\ui\ConcernPanel.java /Download at http://www.pin5i.com ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\ui\ConcernPanel.java ᥹ϟᴹህ㽕㸼⦄ሖᇚ᭄᥂ᇕ㺙៤ϔϾ Concern ᇍ䈵ˈݡ䇗⫼Ϯࡵሖⱘ add ᮍ⊩ᠻ㸠ᮄ๲᪡԰DŽ } return find(id); //ḍ᥂ ID ݡᶹᡒᇍ䈵 String id = dao.add(c); //ᕫࠄ䖨ಲⱘ ID public Concern add(Concern c) { ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\service\impl\ConcernServiceImpl.java ᮄ๲ϔϾ Concern ᇍ䈵DŽҹϟᰃ concernServiceImpl ⱘᅲ⦄DŽ 䈵ᠻ㸠 SQL ⱘ executeUpdate ᮍ⊩ˈ䆹ᮍ⊩䖨ಲᮄ᭄᥂ⱘЏ䬂DŽЎ ConcernService ⏏ࡴ᥹ষᮍ⊩ˈ ҹϞҷⷕⱘ咥ԧ䚼ߚˈ䇗⫼⠊㉏ⱘ getJDBCExecutor()ᮍ⊩ˈ㦋ᕫ JDBCExecutor ᇍ䈵ˈ䇗⫼䆹ᇍ } return id; String id = String.valueOf(getJDBCExecutor().executeUpdate(sql)); //䇗⫼ JDBCExecutor ⱘ executeUpdate ᮍ⊩ˈᑊ䖨ಲᮄ᭄᥂ⱘЏ䬂 "', '" + c.getPUB_INTRO() + "')"; c.getPUB_NAME() + "', '" + c.getPUB_TEL() + "', '" + c.getPUB_LINK_MAN() + String sql = "INSERT INTO T_PUBLISHER VALUES (ID, '" + //߽⫼ߎ⠜⼒ᇍ䈵ᣐ㺙 SQL public String add(Concern c) { ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\dao\impl\ConcernDaoImpl.java String add(Concern concern); //⏏ࡴϔϾߎ⠜⼒ᑊ䖨ಲᮄ๲ⱘ id ℸᯊˈৃҹএᅲ⦄Ϯࡵሖ੠᭄᥂䆓䯂ሖDŽConcernDao ⏏ࡴᮄⱘ᥹ষᮍ⊩˖ }); } save(); public void actionPerformed(ActionEvent arg0) { saveButton.addActionListener(new ActionListener(){ //ֱᄬᣝ䪂ⲥ਀఼ Ўֱᄬᣝ䪂⏏ࡴⲥ਀఼˖ } //᱖ᯊϡᅲ⦄ private void update() { //ׂᬍᮍ⊩ } //᱖ᯊϡᅲ⦄ private void add() { //ᮄ๲ᮍ⊩ } else update(); if (this.concernId.getText().equals("")) add(); //བᵰ id Ўぎ, ߭Ўᮄ๲, ϡЎぎ߭Ўׂᬍ᪡԰ } return; showWarn("䇋䕧ܹߎ⠜⼒ৡ⿄"); //䇗⫼⠊㉏ⱘᮍ⊩ˈᔍߎ䫭䇃ᦤ⼎ if (this.concernName.getText().trim().equals("")) { private void save() { ᄬᮍ⊩ֱ// /Download at http://www.pin5i.com "', pub.PUB_TEL='" + c.getPUB_TEL() + "', pub.PUB_LINK_MAN='" + c.getPUB_LINK_MAN() + String sql = "UPDATE T_PUBLISHER pub SET pub.PUB_NAME='" + c.getPUB_NAME() + //߽⫼খ᭄ᣐ㺙ׂᬍⱘ SQL public String update(Concern c) { ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\dao\impl\ConcernDaoImpl.java 䈵DŽ ৠˈ䛑ᰃ䇏ࠄ᭄᥂ˈݡᠻ㸠 SQL 䖯㸠ׂᬍDŽЎ ConcernDao ࡴܹ᥹ষᮍ⊩ˈ⫼ѢׂᬍϔϾ Concern ᇍ ᅮⱘˈᑊϨ೼ 9.5.5 ЁᏆ㒣᳝ϔϾ update ᮍ⊩ˈাᰃ≵᳝ᦤկᅲ⦄DŽׂᬍϢᮄ๲໻㟈ⳌއJTextFiled ᴹ ೼ 9.5.5 Ёˈ៥ӀᏆ㒣ⶹ䘧ˈֱᄬᣝ䪂ᰃ᳝ϸϾ᪡԰ⱘˈ㟇Ѣᠻ㸠ાϔϾ᪡԰ˈ⬅ᄬᬒ ID ⱘ䱤㮣ⱘ 9.5.6 ׂᬍߎ⠜⼒ }); } clear(); public void actionPerformed(ActionEvent arg0) { clearButton.addActionListener(new ActionListener(){ //⏙ぎᣝ䪂ⲥ਀఼ ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\ui\ConcernPanel.java ᮍ⊩ˈ䆹ᮍ⊩гৃҹ䅽⬠䴶Ёⱘ⏙ぎᣝ䪂Փ⫼ˈЎ⏙ぎᣝ䪂⏏ࡴⲥ਀఼˖ ᔧ䇗⫼Ϯࡵᮍ⊩ᮄ๲њϔϾߎ⠜⼒ৢˈ䳔㽕ᇚ߫㸼ࠋᮄˈᇚ㸼ऩ⏙ぎˈ಴ℸ៥Ӏᅲ⦄⠊㉏ⱘ clear ⱘᵘ䗴఼DŽ ៥Ӏ೼ DataUtil Փ⫼ Class 䞠䴶ⱘ newInstance ᮍ⊩ˈ಴ℸ៥Ӏ䳔㽕Ў Concern ᰒ⼎ᦤկϔϾ᮴খ᭄ ⊼˖ҹϞҷⷕЁⱘ getConcern ᮍ⊩ˈ䖨ಲᯊ䳔㽕⫼ࠄ Concern ⱘᵘ䗴఼ˈ䆹ᵘ䗴఼᳝ 5 Ͼখ᭄ˈ } this.pubIntro.setText(""); this.pubTel.setText(""); this.pubLinkMan.setText(""); this.concernName.setText(""); this.concernId.setText(""); //⏙ぎ㸼ऩЁⱘ৘Ͼ᭛ᴀḚ˄ඳ˅ⱘؐ refreshTable();//䇗⫼⠊㉏ⱘࠋᮄ߫㸼ᮍ⊩ public void clear() { //ᅲ⦄⠊㉏ⱘᮍ⊩ˈ⏙ぎ㸼ऩᑊࠋᮄ߫㸼 } clear();//ࠋᮄ㸼ऩ setViewDatas();//䞡ᮄ䇏প᭄᥂ service.add(c); Concern c = getConcern();//㦋ᕫ⬠䴶Ёⱘؐᑊᇕ㺙៤ᇍ䈵 private void add() { //ᮄ๲ᮍ⊩ } return new Concern(null, concernName, pubTel, pubLinkMan, pubIntro); String pubIntro = this.pubIntro.getText(); String pubLinkMan = this.pubLinkMan.getText(); String pubTel = this.pubTel.getText(); String concernName = this.concernName.getText(); private Concern getConcern() { Ң㸼ऩЁ㦋প᭄᥂ᑊᇕ㺙៤ Concern ᇍ䈵, ≵᳝ id ؐ// /Download at http://www.pin5i.com 㦋ᕫ᭄᥂ᑧЁܼ䚼ⱘкˈᑊᇚ݊ᰒ⼎೼߫㸼ˈϢߎ⠜⼒㉏Ԑˈݭϔহ SQL ᶹᡒᕫࠄ㒧ᵰⱘ䲚ড়ˈ 9.6.1 㦋পܼ䚼к ԧϔ㟈ⱘDŽ ⷕDŽ䰸њкᴀ䆄ᔩ᳝໪䬂੠䳔㽕᳝೒⠛໘⧚ⱘࡳ㛑໪ˈ݊Ҫࡳ㛑䛑Ϣߎ⠜⼒ㅵ⧚㉏ԐˈѸѦᮍᓣгᰃ໻ ৠᯊˈг䳔㽕㗗㰥བԩᕫࠄ䖭ϸ⾡᭄᥂DŽ঺໪ˈк䖬ࣙᣀ೒⠛ˈ಴ℸᇍ೒⠛ⱘ໘⧚Ϟ䖬䳔㽕㓪ݭϔѯҷ Ϣߎ⠜⼒ㅵ⧚ࡳ㛑Ⳍ↨ˈкᴀㅵ⧚໡ᴖϔ⚍ˈ಴ЎкϢߎ⠜⼒ǃкᴀ⾡㉏Ⳍ݇㘨ˈ಴ℸ೼᪡԰кⱘ 9.6 кᴀㅵ⧚ࡳ㛑 ᄫ↉ᇥˈ಴ℸ೼ᴀ՟Ёⳕ⬹བԩᅲ⦄кᴀ⾡㉏ㅵ⧚DŽ ⠜⼒ㅵ⧚᳈ࡴㅔऩˈৠḋഄˈкᴀ㉏ൟㅵ⧚г≵᳝ӏԩⱘ໪䬂݇㘨ˈкᴀ㉏ൟ㸼ⱘᄫ↉г↨ߎ⠜⼒㸼ⱘ ᴹ䛑↨䕗ㅔऩˈϮࡵሖ޴Тϡ䳔㽕໘⧚ӏԩϮࡵDŽ੠ߎ⠜⼒ㅵ⧚Ⳍ㉏Ԑⱘкᴀ⾡㉏ㅵ⧚ˈᅲ⦄䍋ᴹ↨ߎ ߎ⠜⼒ⱘᶹⳟǃᮄ๲੠ׂᬍࡳ㛑䛑Ꮖ㒣ᅲ⦄њˈ⬅Ѣߎ⠜⼒Ϣ݊Ҫⱘ㸼≵᳝ӏԩ݇㘨ˈ಴ℸᅲ⦄䍋 ༓ྑ setIDd ܮēဎဟ؞ճເਠပ IDē࿙Ү܊ԅᄔݦԄྡྷّਠပ ID ೫໿ԅ Concern ճເēupdate ֺ֥ᄯݦԄ؞ճເ ڑᅟġྻ౨۱඘ӝ৲น getConcern ֺ֥ēူ໭႙ನԅ getConcern ֺ֥ྡྷྂē՛ಾٔ࡬࠘ੋᄯّْອ } refreshTable(); //ࠋᮄ߫㸼 setViewDatas(); //䞡ᮄ䇏প᭄᥂ service.update(c); c.setID(id); Concern c = getConcern(); //ḍ᥂⬠䴶᭄᥂㦋ᕫ Concern ᇍ䈵 String id = this.concernId.getText(); //পᕫᔧׂࠡᬍ䆄ᔩⱘ ID private void update() { //ׂᬍᮍ⊩ ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\ui\ConcernPanel.java } return find(id); //䞡ᮄᶹᡒ䆹ᇍ䈵 String id = dao.update(c); //䇗⫼ DAO ᮍ⊩ׂᬍᇍ䈵 public Concern update(Concern c) { ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\service\impl\ConcernServiceImpl.java Ў ConcernService ᥹ষࡴܹ update ᮍ⊩ˈᑊ೼ ConcernServiceImpl Ёᦤկᅲ⦄DŽ } return c.getID(); //Ⳉ᥹䖨ಲᇍ䈵ⱘ ID getJDBCExecutor().executeUpdate(sql); //ᠻ㸠 SQL pub.PUB_INTRO='" + c.getPUB_INTRO() + "' WHERE pub.ID='" + c.getID() + "'"; ,'" /Download at http://www.pin5i.com book.setType(typeDao.find(book.getTYPE_ID_FK())); //ᶹᡒߎᇍᑨⱘ⾡㉏ˈݡЎк䆒㕂⾡㉏ᇍ䈵 for (Book book : result) { //䘡ग़㒧ᵰ䲚ড়ˈ䆒㕂↣ϔϾкⱘᇍ䈵 private Collection setAssociate(Collection result) { //䆒㕂݇㋏ᇍ䈵 } return setAssociate(result); //䇗⫼ setAssociate ᮍ⊩䆒㕂݇㘨ⱘϸϾᇍ䈵 Collection result = bookDao.findAll(); public Collection getAll() { //ᅲ⦄᥹ষᮍ⊩ } this.concernDao = concernDao; this.typeDao = typeDao; this.bookDao = bookDao; public BookServiceImpl(BookDao bookDao, TypeDao typeDao, ConcernDao concernDao) { //ᔧᵘ䗴 BookServiceImpl ⱘᯊ׭ˈ䳔㽕ϝϾ᥹ষ private ConcernDao concernDao; private TypeDao typeDao; private BookDao bookDao; ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\service\impl\BookServiceImpl.java TypeDao ੠ ConcernDao ᴹḍ᥂ ID ᶹᡒϸϾᇍ䈵њDŽ ಴ℸ೼ BookService ⱘᅲ⦄㉏Ёˈ៥Ӏ䳔㽕ᇚϸϾ໪䬂䕀ᤶ៤ϸϾᇍ䈵ˈ䖭ᯊⱘ䕀ᤶˈ߭䳔㽕 //ⳕ⬹ setter ੠ getter ᮍ⊩ private Concern concern; //кᇍᑨⱘߎ⠜⼒ˈϢ type Ⳍৠ private Type type; //кᴀ⾡㉏ˈҢ᭄᥂ᑧᶹ䆶ߎᴹⱘᯊ׭ˈ䖭ϾሲᗻЎ nullˈݡ䗮䖛ᴀ㉏ⱘ TYPE_ID_FK এ䆒㕂䖭Ͼሲᗻ ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\vo\Book.java ৡ⿄੠⾡㉏ৡ⿄ˈ಴ℸ៥Ӏ䳔㽕Ў Book ㉏⏏ࡴϸϾሲᗻˈߚ߿ᰃ⾡㉏˄Type˅੠ߎ⠜⼒˄Concern˅DŽ ˄PUB_ID_FK˅੠ߎ⠜⼒˄TYPE_ID_FK˅໪䬂ˈাᰃֱᄬϔϾ IDˈԚᰃ⬠䴶Ϟ䳔㽕ᰒ⼎ⱘᰃߎ⠜⼒ ೼ᅲ⦄ BookService ⱘᯊ׭ˈ៥Ӏ䳔㽕ⶹ䘧ˈBook ᇍ䈵䞠䴶ࣙ৿᳝к㸼ⱘ৘Ͼᄫ↉ˈࣙᣀ⾡㉏ BookServiceImpl ⱘᅲ⦄ˈࡴܹ getAll ᮍ⊩ˈ⫼Ѣᶹᡒܼ䚼ⱘкᴀDŽ䆹᥹ষ㒭⬠䴶ᇍ䈵䇗⫼DŽ getDatas ᮍ⊩ᇍ ResultSet 䖯㸠њᇕ㺙DŽ⏏ࡴϔϾ BookService ⱘϮࡵ᥹ষˈᑊЎ݊ᦤկϔϾ Ϣߎ⠜⼒ㅵ⧚㉏Ԑˈাᦤկϔহ SQLˈݡ䗮䖛䆹হ SQLˈ䇗⫼⠊㉏ⱘ getDatas ᮍ⊩䖨ಲ᭄᥂ˈ } } return getDatas(sql, new Vector(), Book.class); //䇗⫼⠊㉏ⱘᮍ⊩䖨ಲ᭄᥂ String sql = "SELECT * FROM T_BOOK book ORDER BY book.ID desc"; //ᶹᡒⱘ SQL 䇁হ public Collection findAll() { public class BookDaoImpl extends CommonDaoImpl implements BookDao { ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\dao\impl\BookDaoImpl.java ㉏ˈЎ BookDao ࡴܹ findAll ⱘ᥹ষᮍ⊩ˈ⫼Ѣᶹᡒܼ䚼ⱘкᴀDŽ ᅲ⦄᭄᥂䆓䯂ሖˈݡᅲ⦄ϮࡵሖDŽᮄᓎ᥹ষ BookDaoˈᑊЎ݊ᦤկ BookDaoImpl ⱘᅲ⦄ܜ៥Ӏˈܜ佪 CommonPanel ⱘᄤ㉏ˈা䳔㽕䞡ݭϔѯᮍ⊩ˈᑊ䆒㕂Ⳍ݇ⱘ᭄᥂䅽⠊㉏এḍ᥂䖭ѯ᭄᥂⫳៤߫㸼ेৃDŽ ݡՓ㒧ᵰᰒ⼎೼⬠䴶ЁDŽ೼ 9.2.4 Ёˈкᴀㅵ⧚⬠䴶ᇍᑨⱘᰃ BookPanel ㉏ˈৠḋഄˈ䆹㉏гᰃ /Download at http://www.pin5i.com 㽕໾໡ᴖⱘ໘⧚DŽ೼㸼⦄ሖЁˈ䳔㽕ᦤկϔϾሲᗻˈ⫼ᴹֱᄬᔧࠡ᠔ᰒ⼎ⱘ೒⠛ˈ⬅Ѣ⬠䴶Ёা᳝ϔϾ Ͼሲᗻ˄Type ੠ Concern˅ˈ䞡⫼ 9.6.1 Ё BookServiceImpl ⱘ setAssociate ᮍ⊩ህৃҹ䆒㕂ˈᑊϡ䳔 ೼Ϯࡵሖ໘⧚ᯊˈᔧᕫࠄϔϾ Book ᇍ䈵ⱘᯊ׭ˈϢ㦋ᕫܼ䚼кⱘࡳ㛑ϔḋˈ䳔㽕䆒㕂 Book ᇍ䈵ⱘϸ 䆹হ SQL Ⳍᔧⱘㅔऩˈid ⬅⬠䴶Ӵܹˈৃҹ೼ BookPanel Ё䇗⫼⠊㉏ⱘ getSelectId ेৃ㦋ᕫˈ SELECT * FROM T_BOOK book WHERE book.ID='" + id + "'"; id ᶹᡒкᴀⱘ SQL˖ ϟ䴶ⱘ㸼ऩˈࡳ㛑↨䕗ㅔऩˈাᰃᇚ৘Ͼؐᬒ㕂ࠄᇍᑨⱘऎඳेৃDŽ೼䖭䞠ϡ䆺㒚ᦣ䗄ˈϟ䴶ᦤկḍ᥂ ᶹⳟкᴀⱘࡳ㛑Ϣᶹⳟߎ⠜⼒ⱘࡳ㛑ϔḋˈ䛑ᰃᔧ䗝ᢽњ߫㸼Ёⱘᶤϔ㸠᭄᥂ᯊˈህᇚ᭄᥂ᰒ⼎೼ 9.6.2 ᶹⳟкᴀ ᕫࠄ᭄᥂ৢˈݡ䇗⫼⠊㉏ⱘ setDatas ੠ refreshTable ᮍ⊩ህৃҹᅲ⦄ˈ೼ᴀ㡖ϡݡ䌬䗄DŽ ᮍ⊩ेৃDŽ݋ԧⱘᅲ⦄Ϣߎ⠜⼒ㅵ⧚ϔḋDŽкᴀⱘ῵㊞ᶹ䆶гᰃՓ⫼ MySQL ⱘ like ݇䬂ᄫेৃᅲ⦄ˈ ⱘ setViewDatas 䳔㽕䅽ᵘ䗴఼এ䇗⫼DŽ⊼ᛣˈ䖬䳔㽕䆒㕂ϔϾ䱤㮣ⱘ id ߫ˈᅲ⦄⠊㉏ⱘ setTableFace Ϣߎ⠜⼒ϔḋˈ⏏ࡴҹϞϸϾᮍ⊩ˈ䆒㕂䳔㽕ᰒ⼎ⱘ߫㸼᭄᥂ˈݡ䆒ᅮⳌ݇ⱘ߫ेৃˈҹϞҷⷕⱘ } return view; } view.add(v); v.add(book.getBOOK_PRICE()); v.add(book.getREPERTORY_SIZE()); v.add(book.getConcern().getPUB_NAME()); v.add(book.getType().getTYPE_NAME()); v.add(book.getAUTHOR()); v.add(book.getBOOK_INTRO()); v.add(book.getBOOK_NAME()); v.add(book.getID()); Vector v = new Vector(); for (Book book : datas) { Vector view = new Vector(); private Vector changeDatas(Vector datas) { //ᇚ᭄᥂䕀ᤶ៤㾚೒㸼ḐⱘḐᓣ } setDatas(datas); //䇗⫼⠊㉏ᮍ⊩䆒㕂㒧ᵰ䲚ড় Vector datas = changeDatas(books); //䕀ᤶᰒ⼎Ḑᓣ Vector books = (Vector)bookService.getAll(); //ᶹᡒᇍᑨⱘ᭄᥂ private void setViewDatas() { //ᅲ⦄⠊㉏ᮍ⊩ˈᶹ䆶᭄᥂ᑧᑊ䖨ಲᇍᑨⱘ᭄᥂Ḑᓣ, 䇗⫼⠊㉏ⱘ setDatas ᮍ⊩䆒㕂᭄᥂䲚ড় ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\ui\BookPanel.java ㉏ৡ⿄ⱘ䆱ˈⳈ᥹ book.getType().getTYPE_NAME()ेৃˈϟ䴶ᅲ⦄㸼⦄ሖDŽ ҹϞҷⷕⱘ咥ԧ䚼ߚˈ䆒㕂 Book ⱘϸϾ໪䬂ᇍ䈵DŽ䖭ᯊ׭ˈ೼⬠䴶ᰒ⼎䳔㽕পᕫߎ⠜⼒ৡ⿄੠⾡ } return result; } book.setConcern(concernDao.find(book.getPUB_ID_FK())); ᶹᡒߎᇍᑨⱘߎ⠜⼒ˈݡЎк䆒㕂ߎ⠜⼒ᇍ䈵// /Download at http://www.pin5i.com Collection types = this.typeService.getAll(); //䇗⫼⾡㉏Ϯࡵ᥹ষপᕫܼ䚼ⱘ⾡㉏ private void addTypes() { //Ң᭄᥂ᑧЁ㦋পܼ䚼ⱘ⾡㉏ᑊ⏏ࡴࠄϟᢝḚЁ ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\ui\BookPanel.java 㟈DŽҹϟҷⷕ߱ྟ࣪ϟᢝḚ˄ᰒ⼎᭄᥂ᑧЁ᠔᳝ⱘ Type˅DŽ ᰃ䖭ϸϾ Type ⱘ ID ᰃ৺ϔޚ㽕䞡ݭ䖭ϸϾᮍ⊩DŽ೼ equals ᮍ⊩Ёˈ៥Ӏ߸ᮁϸϾ Type ᰃ৺Ⳍㄝⱘᷛ Ёˈ䇗⫼䆹ᇍ䈵ⱘ toString ᮍ⊩ᰒ⼎ˈ߸ᮁᰃ৺䗝ЁњᶤϔϾϟᢝḚˈ䇗⫼ equals ᮍ⊩ˈ಴ℸ៥Ӏ䳔 䆹ᮍ⊩⫼ᴹ߯ᓎϔϾType ᇍ䈵ˈ䆹ᇍ䈵䞡ݭњtoString੠equalsᮍ⊩ˈ⬅Ѣ೼ϟᢝḚ˄JComboBox˅ } return type; type.setID(source.getID()); }; } return false; } if (getID().equals(t.getID())) return true; Type t = (Type)obj; if (obj instanceof Type) { public boolean equals(Object obj) { } return source.getTYPE_NAME(); public String toString(){ Type type = new Type(){ private Type makeType(final Type source) { //߯ᓎϔϾ Type ᇍ䈵, ⫼Ѣ⏏ࡴࠄϟᢝḚЁ, 䆹ᮍ⊩Ё߯ᓎⱘ Type ᇍ䈵䞡ݭњ toString ੠ equals ᮍ⊩ ⊼ᛣҹϞҷⷕⱘ咥ԧ䚼ߚˈ䳔㽕䆒㕂⾡㉏੠ߎ⠜⼒ϟᢝḚⱘؐDŽҹϟᰃ makeType ᮍ⊩ⱘҷⷕ˖ } refreshImage(); //䞡ᮄ䆒㕂ᔧࠡᰒ⼎ⱘ೒⠛ this.currentImagePath = book.getIMAGE_URL(); this.currentImage = new ImageIcon(book.getIMAGE_URL()); this.concernComboBox.setSelectedItem(makeConcern(book.getConcern())); this.typeComboBox.setSelectedItem(makeType(book.getType())); this.author.setText(book.getAUTHOR()); this.intro.setText(book.getBOOK_INTRO()); this.price.setText(book.getBOOK_PRICE()); this.bookName.setText(book.getBOOK_NAME()); this.bookId.setText(book.getID()); //䆒㕂৘Ͼᇍᑨऎඳⱘؐ Book book = bookService.get(id); String id = getSelectId(getJTable()); private void view() { //ᶹⳟкᴀ } this.imageLabel.setIcon(this.currentImage);//imageLabel Ў೒⠛ᰒ⼎ⱘ JLabel private void refreshImage() { //ࠋᮄ೒⠛ᰒ⼎ⱘ JLabel ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\ui\BookPanel.java ᰒ⼎೒⠛ⱘഄᮍ˄JLabel˅ˈ಴ℸˈг䳔㽕᳝ϔϾ䖭ḋⱘሲᗻᴹֱᄬ೒⠛DŽ /Download at http://www.pin5i.com bookService.add(book); Book book = getBook(); //Ң⬠䴶Ёপᕫкᴀⱘؐᑊᇕ㺙៤ Book ᇍ䈵ˈᮄ๲кᴀᯊᑧᄬЎ 0 private void add() { //ᮄ๲кᴀ } concern.getID(), String.valueOf(0), this.currentImagePath, author); return new Book(null, bookName, intro, price, type.getID(), //this.currentImagePath ᰃᔧࠡᰒ⼎ⱘ೒⠛䏃ᕘ Concern concern = (Concern)this.concernComboBox.getSelectedItem(); //Ңߎ⠜⼒ϟᢝḚЁপᕫⳌᑨⱘߎ⠜⼒ Type type = (Type)this.typeComboBox.getSelectedItem(); //Ң⾡㉏ϟᢝḚЁপᕫᇍᑨⱘ⾡㉏ String author = this.author.getText(); String intro = this.intro.getText(); String price = this.price.getText(); String bookName = this.bookName.getText(); private Book getBook() { //Ң⬠䴶Ё㦋প᭄᥂ᑊᇕ㺙៤ Book ᇍ䈵 ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\ui\BookPanel.java } return get(id); String id = bookDao.add(book); public Book add(Book book) { //ᅲ⦄ BookService ⱘ add ᮍ⊩ ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\service\impl\BookServiceImpl.java } return String.valueOf(getJDBCExecutor().executeUpdate(sql)); //ᠻ㸠 SQL ᑊ䖨ಲ ID "', '" + book.getAUTHOR() + "', '" + book.getREPERTORY_SIZE() + "')"; "', '" + book.getPUB_ID_FK() + "', '" + book.getIMAGE_URL() + book.getBOOK_INTRO() + "', '" + book.getBOOK_PRICE() + "', '" + book.getTYPE_ID_FK() + String sql = "INSERT INTO T_BOOK VALUES (ID, '" + book.getBOOK_NAME() + "', '" + //ḍ᥂ book ᇍ䈵ᣐ㺙 SQL public String add(Book book) { //ᅲ⦄᥹ষ BookDao ⱘ add ᮍ⊩ ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\dao\impl\BookDaoImpl.java ᇚ᭄᥂䆓䯂ሖ੠Ϯࡵሖⱘҷⷕᅲ⦄DŽܜ᭄᥂ᑧDŽ кⱘ㸼ऩϢߎ⠜ᓣ⼒㸼ऩϡৠˈ䳔㽕ᅲ⦄᭛ӊϞӴⱘࡳ㛑ˈ݊Ҫᄫ↉䛑ৃҹⳈ᥹Փ⫼ SQL ݭܹࠄ 9.6.3 ᮄ๲кᴀ ᮍ⊩DŽ ߎ⠜⼒ⱘϟᢝḚϢ⾡㉏ϟᢝḚϔḋˈ⏏ࡴࠄϟᢝḚ䞠䴶ⱘ↣Ͼᇍ䈵ˈ䛑䳔㽕䞡ݭ toString ੠ equals } } this.typeComboBox.addItem(makeType(type)); //typeComboBox ᰃ⾡㉏ϟᢝḚᇍ䈵 for (Type type : types) { /Download at http://www.pin5i.com //䕧ߎࠄ᭛ӊ g.drawImage(newImage, 0, 0, null); //೼ᮄⱘ㓧ᄬ೒⠛Ё⬏೒ Graphics g = bi2.getGraphics(); BufferedImage bi2 = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //ҹᮄⱘ催੠ᆑᵘ䗴ϔϾᮄⱘ㓧ᄬ೒⠛ int width = newImage.getWidth(null); int height = newImage.getHeight(null); //㦋পय़㓽ৢ೒⠛ⱘ催੠ᆑ Image newImage = getImage(bi); //㦋ᕫᮄⱘ Image ᇍ䈵 } return output; ImageIO.write(bi, formatName, new FileOutputStream(output)); if (!compress) { //བᵰϡ䖯㸠य़㓽, Ⳉ᥹ݭܹ᭛ӊᑊ䖨ಲ BufferedImage bi = ImageIO.read(imageFile); //䇏প೒⠛ File output = new File(url); boolean compress) { public static File makeImage(File imageFile, String url, String formatName, */ * @return ⫳៤ⱘ೒⠛᭛ӊ * @param compress ᰃ৺䖯㸠य़㓽 * @param formatName ⫳៤೒⠛ⱘḐᓣ * @param url ॳ೒Ⳍᇍ䏃ᕘ * @param imageFile ೒⠛᭛ӊ * 㒣䖛ॳ೒⠛ⱘय़㓽ৢ, ⫳៤ϔᓴᮄⱘ೒⠛ /** public final static int MAX_HEIGHT = 240; //य़㓽೒⠛ⱘ᳔໻催 public final static int MAX_WIDTH = 220; //य़㓽೒⠛ⱘ᳔໻ᆑ ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\commons\ImageUtil.java 㓪ݭϔϾ ImageUtil ㉏ˈ⫼ᴹϧ䮼໘⧚೒⠛DŽܜ㾜DŽ ᑊݡ⫳៤ϔᓴᇍᑨⱘ㓽⬹೒ˈ㓽⬹೒೼кᴀㅵ⧚⬠䴶ᰒ⼎ˈᔧ⚍ߏњ㓽⬹೒ᯊˈৃҹᰒ⼎ॳ೒㒭⫼᠋⌣ ϔᴀкা᳝ϔᓴ೒⠛DŽ೼⫼᠋䗝ᢽњ೒⠛ৢˈ៥Ӏৃҹᇚ⫼᠋᠔䗝ᢽⱘ೒⠛໡ࠊࠄ៥Ӏⱘ乍ⳂⳂᔩЁˈ ᕫ໾໡ᴖˈ಴ℸخ⬠䴶ᦤկϔϾ䗝ᢽ೒⠛ⱘᣝ䪂ˈৃҹᦤկ㒭⫼᠋䗝ᢽкⱘ೒⠛ˈ೼ᴀ՟Ёˈϡᖙ 9.6.4 ೒⠛ϞӴࡳ㛑 ׂᬍкᴀⱘᑧᄬˈা᳝䫔ଂ៪㗙ܹᑧⱘᯊ׭ˈᠡӮᇍᑧᄬѻ⫳ᕅડDŽϟ䴶䆺㒚䆆㾷བԩ䖯㸠೒⠛ϞӴDŽ ᴀˈབᵰ᳝ⱘ䆱ˈे㸼⼎ׂᬍDŽ೼ᮄ๲кᴀⱘᯊ׭ˈкᴀⱘᑧᄬᑨ䆹Ў 0ˈׂᬍкᴀⱘᯊ׭ˈᑊϡᑨ䆹 Ѣᔧࠡ㸼ऩЁᰃ৺᳝кއ⬠䴶ϞাᦤկњϔϾֱᄬⱘᣝ䪂ˈ⚍ߏֱᄬᯊ䖯㸠ᮄ๲䖬ᰃׂᬍ᪡԰ˈপ } clear(); //ࠋᮄ߫㸼, ⏙ぎ㸼ऩ setViewDatas(); 䞡ᮄ䇏প᭄᥂// /Download at http://www.pin5i.com ⱘ催੠ᆑ䛑ϡ໻Ѣ೒⠛ᰒ⼎ऎⱘ催੠ᆑDŽᰒ⼎ৃҹ䆩ϟ೒⠛ϞӴⱘࡳ㛑ˈབ೒ 9.10 ᠔⼎DŽ upload ᮍ⊩Ёˈ䳔㽕⫳៤ϸᓴ೒⠛ˈϔᓴᰃॳ೒ⱘࡃᴀˈϢॳ೒ϔ㟈ˈ঺໪ϔᓴᰃ㓽⬹೒ˈ㓽⬹೒ } refreshImage();//ࠋᮄ೒⠛ᰒ⼎ऎ this.currentImagePath = smallFilePath; //䆒㕂⬠䴶ᰒ⼎ⱘ೒⠛ url this.currentImage = new ImageIcon(file.getAbsolutePath());//䆒㕂⬠䴶ᰒ⼎ⱘ೒⠛ᇍ䈵 File source = ImageUtil.makeImage(selectFile, bigFilePath, "jpg", false); //⫳៤ॳ೒ File file = ImageUtil.makeImage(selectFile, smallFilePath, "jpg", true); //⫳៤㓽⬹೒ String bigFilePath = "upload/" + uuid + "-big.jpg";//ॳ೒ⱘ url String smallFilePath = "upload/" + uuid + ".jpg";//㓽⬹೒ⱘ url String uuid = ImageUtil.getUUID();//Փ⫼ uuid ⫳៤᭛ӊৡˈֱ䆕᭛ӊৡଃϔ public void upload(File selectFile) { //ϞӴ೒⠛ ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\ui\BookPanel.java ҹϞҷⷕⱘ咥ԧ䚼ߚˈՓ⫼кᴀㅵ⧚⬠䴶ᇍ䈵ⱘ upload ᮍ⊩ˈҹϟᰃ upload ᮍ⊩ⱘᅲ⦄DŽ } } super.approveSelection(); this.bookPanel.upload(file); //䇗⫼кᴀㅵ⧚⬠䴶ᇍ䈵ⱘ upload ᮍ⊩ File file = getSelectedFile();//㦋ᕫ䗝ᢽⱘ᭛ӊ public void approveSelection() { //䗝ᢽњ᭛ӊৢ㾺থ } this.bookPanel = bookPanel; public FileChooser(BookPanel bookPanel){ BookPanel bookPanel; //кᴀㅵ⧚⬠䴶ᇍ䈵 class FileChooser extends JFileChooser { ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\ui\BookPanel.java ᭛ӊDŽ ҹϞҷⷕЁⱘ getImage ᮍ⊩䖨ಲय़㓽ৢⱘ Image ᇍ䈵DŽϟ䴶㓪ݭϔϾ᭛ӊ䗝ᢽ఼ⱘ㉏ˈ໘⧚⫼᠋䗝ᢽ url Ёˈℸ໘ⱘ url ৃҹᰃⳌᇍ䏃ᕘDŽབᵰ䳔㽕䖯㸠य़㓽ˈ䙷Мᖙ乏㽕⹂ᅮय़㓽ৢⱘ೒⠛ⱘ催੠ᆑˈ಴ℸ ࠄᮄⱘ⌕ڣ⊼ᛣҹϞҷⷕⱘ咥ԧ䚼ߚˈᔧϡ䳔㽕䖯㸠य़㓽໘⧚ⱘᯊ׭ˈⳈ᥹Փ⫼ ImageIO 䕧ߎ೒ } return uuid.toString(); UUID uuid = UUID.randomUUID(); public static String getUUID() { //⫳៤ uuidˈ԰ЎϞӴⱘ᭛ӊৡ } } return bi.getScaledInstance(-10, MAX_HEIGHT, Image.SCALE_AREA_AVERAGING); } else { return bi.getScaledInstance(MAX_WIDTH, -10, Image.SCALE_AREA_AVERAGING); if (bi.getWidth() > bi.getHeight()) { private static Image getImage(BufferedImage bi) { //ᔧখ᭄ bi ⱘ催↨ᆑ໻ⱘᯊ׭ˈՓ⫼᳔໻ⱘ催ᴹय़㓽೒⠛ //䖨ಲϔϾ Image ᇍ䈵ˈᔧখ᭄ bi ⱘᆑ↨催໻ⱘᯊ׭ˈՓ⫼᳔໻ⱘᆑᴹय़㓽೒⠛DŽ } return output; ImageIO.write(bi2, formatName, new FileOutputStream(output)); /Download at http://www.pin5i.com Ў SaleRecord ⏏ࡴሲᗻ˖ ৡ⿄੠Ѹᯧᘏ䞣DŽ ᇍ䈵˄Type ੠ Concern˅DŽ㗠೼䫔ଂㅵ⧚Ёˈ៥Ӏ䖬䳔㽕Ў䫔ଂ䆄ᔩ䅵ㅫℸ⃵ѸᯧⱘᘏӋǃѸᯧⱘкᴀ ೼ 9.6 кᴀㅵ⧚ࡳ㛑Ёˈᶹߎ᠔᳝ⱘкᴀৢˈ䖬䳔㽕ḍ᥂кᴀⱘ໪䬂䆒㕂кᴀᇍ䈵˄Book˅ⱘϸϾ݇㘨 ᶹ䆶᠔᳝ⱘ䫔ଂ䆄ᔩˈϢࠡ䴶ⱘ㉏Ԑˈϡৠⱘഄᮍህᰃᶹߎ㒧ᵰ䲚ড়ৢˈ䳔㽕ᇍ䆹䲚ড়䖯㸠໘⧚ˈ 9.7.1 䫔ଂ䆄ᔩ߫㸼 ᄬ䫔ଂ䆄ᔩˈݡ䗤ᴵֱᄬкⱘ䫔ଂ䆄ᔩDŽֱܜ䫔ଂ䆄ᔩⱘᯊ׭ˈህ䳔㽕 ๲ࡴ៪㗙ߴ䰸ϔᴵ䆄ᔩⱘᯊ׭ˈ㋏㒳䖬㽕ЎᘏӋ੠䌁ф᭄䞣䖯㸠䅵᭄䞣ˈᑊᰒ⼎ࠄⳌᑨⱘഄᮍDŽᔧֱᄬ 䫔ଂ䆄ᔩৃ㛑⍝ঞ໮ᴀкˈ಴ℸˈ೼㸼ऩЁ䳔㽕᳝кᴀѸᯧ䆄ᔩⱘ߫㸼DŽ঺໪ˈ೼ЎкᴀѸᯧ䆄ᔩ߫㸼 䫔ଂㅵ⧚ࡳ㛑ˈѸѦᮍᓣϢࠡ䴶޴ᇣ㡖ⱘࡳ㛑㉏ԐˈԚᰃ೼㸼ऩ໘⧚ⱘᯊ׭ህ᳝᠔ϡৠˈ⬅Ѣϔᴵ 9.7 䫔ଂㅵ⧚ࡳ㛑 㛑Ϣкᴀⱘᮄ๲ࡳ㛑ϔḋˈ೼ℸϡݡ䌬䗄DŽ ࠄℸˈкᴀⱘᮄ๲ࡳ㛑Ꮖ㒣ᅲ⦄ˈкᴀⱘׂᬍࡳ㛑Ϣߎ⠜⼒ⱘׂᬍࡳ㛑ϔḋˈ೒⠛ⱘϞӴϢᰒ⼎ࡳ JFrameˈᰒ⼎ᇍᑨ೒⠛ⱘॳ೒DŽ བᵰ䳔㽕⌣㾜ॳ೒ˈৃҹЎ೒⠛ᰒ⼎ऎⱘ JLabel ⏏ࡴⲥ਀఼ˈᔧ⚍ߏњ䆹 JLabel ᯊˈᔍߎϔϾ ೒ 9.10 ೒⠛ϞӴࡳ㛑 /Download at http://www.pin5i.com r.setBookNames(getBookNames(brs)); //䆒㕂 SaleRecord ⱘкৡ䲚ড় r.setBookSaleRecords((Vector)brs); //䆒㕂 SaleRecord ᇍ䈵Ёⱘкⱘ䫔ଂ䆄ᔩ䲚ড় setBook(brs); //䆒㕂㒧ᵰ䲚Ёⱘ↣ϔϾ book ሲᗻ Collection brs = bookSaleRecordDao.findBySaleRecord(r.getID()); //ᶹᡒ䆹䆄ᔩ᠔ᇍᑨⱘкⱘ䫔ଂ䆄ᔩ private SaleRecord processDatas(SaleRecord r) { //໘⧚ϔϾ SaleRecord, 䆒㕂ᅗⱘкᴀ䫔ଂ䆄ᔩሲᗻ੠кᴀৡᄫሲᗻ } return records; } processDatas(r); for (SaleRecord r : records) { Collection records = saleRecordDao.findByDate(today, tomorrow); String tomorrow = DateUtil.getDateString(nextDate); //ᕫࠄᯢ໽ⱘ᮹ᳳ, ḐᓣЎ yyyy-MM-dd String today = DateUtil.getDateString(date); //ᕫࠄҞ໽ⱘ᮹ᳳ, ḐᓣЎ yyyy-MM-dd Date nextDate = DateUtil.getNextDate(date); //ᕫࠄϟϔ໽ public Collection getAll(Date date) { //ᅲ⦄᥹ষᮍ⊩ ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\service\impl\SaleRecordServiceImpl.java ⱘкˈ᥹ϟᴹᅲ⦄䫔ଂ䆄ᔩⱘϮࡵሖDŽ ҹϞᮍ⊩ˈህৃҹ㦋ᕫϔ⃵Ѹᯧ䆄ᔩЁ᠔᳝ⱘкⱘѸᯧ䆄ᔩˈৠḋⱘгৃҹপᕫѸᯧ䆄ᔩЁ᠔⍝ঞ } } return getDatas(sql, new Vector(), BookSaleRecord.class); saleRecordId + "'"; String sql = "SELECT * FROM T_BOOK_SALE_RECORD r WHERE r.T_SALE_RECORD_ID_FK='" + public Collection findBySaleRecord(String saleRecordId) { //ḍ᥂䫔ଂ䆄ᔩ id 㦋ᕫкⱘ䫔ଂ䆄ᔩ䲚ড় BookSaleRecordDao { public class BookSaleRecordDaoImpl extends CommonDaoImpl implements ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\dao\impl\BookSaleRecordDaoImpl.java ЁᮄࡴϔϾᮍ⊩ˈḍ᥂Ѹᯧ䆄ᔩⱘ id ᶹᡒࠄᇍᑨⱘкᴀѸᯧ䆄ᔩ䲚ড়DŽ ໘⧚ˈ䆒㕂៥Ӏᮄ๲ⱘ޴ϾሲᗻDŽ೼㓪ݭ SaleRecord ⱘϮࡵሖᯊˈ䳔㽕೼кᴀѸᯧ䆄ᔩⱘ᭄᥂䆓䯂ሖ ᴀкⱘৡ⿄ᄫヺІˈ໮ᴀкП䯈Փ⫼䗫ো䱨ᓔDŽ࠽ϟⱘџˈህᰃ೼Ϯࡵሖᇍᶹߎᴹⱘ SaleRecord 䖯㸠 Ў SaleRecord ⏏ࡴ޴Ͼሲᗻˈߚ߿ᰃ䫔ଂᘏ᭄䞣ǃѸᯧ䞥乱ǃкⱘѸᯧ䆄ᔩ੠৘ᴀкⱘৡ⿄DŽ৘ //ⳕ⬹ setter ੠ getter ᮍ⊩ private String bookNames; //䆹䆄ᔩЁᇍᑨ᠔᳝кⱘৡ⿄, ᰒ⼎⫼ private Vector bookSaleRecords; //кⱘ䫔ଂ䆄ᔩ private double totalPrice; //ᘏӋ䪅 private int amount; 䫔ଂⱘᘏ᭄䞣// //䆒㕂᭄䞣ϢᘏӋ r.setAmount(getAmount(brs)); r.setTotalPrice(getTotalPrice(brs)); return r; } //㦋পϔ⃵ѸᯧЁ⍝ঞⱘᘏӋ private double getTotalPrice(Collection brs) { double result = 0; for (BookSaleRecord br : brs) { //кᴀⱘѸᯧ䞣 int tradeSum = Integer.valueOf(br.getTRADE_SUM()); //кⱘऩӋ double bookPrice = Double.valueOf(br.getBook().getBOOK_PRICE()); result += (bookPrice * tradeSum); } return result; } //㦋পϔ⃵ѸᯧЁ᠔᳝кᴀⱘѸᯧ䞣 private int getAmount(Collection brs) { int result = 0; //䘡ग़кⱘѸᯧ䆄ᔩˈ䅵ㅫᘏӋ for (BookSaleRecord br : brs) { result += Integer.valueOf(br.getTRADE_SUM()); } return result; } //䆒㕂খ᭄Ёⱘ↣ϔϾ BookSaleRecord ⱘ book ሲᗻ private void setBook(Collection brs) { for (BookSaleRecord br : brs) { //䇗кᴀⱘ᭄᥂䆓䯂ሖ᥹ষ Book book = bookDao.find(br.getBOOK_ID_FK()); br.setBook(book); } } //㦋পϔ⃵ѸᯧЁ᠔᳝кᴀⱘৡᄫ, ҹ䗫ো䱨ᓔ private String getBookNames(Collection brs) { if (brs.size() == 0) return ""; StringBuffer result = new StringBuffer(); for (BookSaleRecord br : brs) { Book book = br.getBook(); result.append(book.getBOOK_NAME() + ", "); } //এᥝ᳔ৢⱘ䗫োᑊ䖨ಲ return result.substring(0, result.lastIndexOf(",")); } ҹϞҷⷕЁⱘ getTotalPrice ᮍ⊩⫼Ѣᕫࠄϔ⃵ѸᯧⱘӋḐ˗getAmount ᮍ⊩⫼Ѣᕫࠄϔ⃵Ѹᯧⱘ кᴀᘏ᭄˗setBook ᮍ⊩⫼Ѣ䆒㕂кᴀ䫔ଂ䆄ᔩᇍ䈵˄BookSaleRecord˅Ёⱘ book ሲᗻˈᔧ✊ˈ೼䙷 Пࠡ㽕Ў BookSaleRecord ㉏⏏ࡴ book ሲᗻ˗getBookNames ᮍ⊩ᰃᇚ᠔᳝ѸᯧЁ⍝ঞⱘкᴀⱘৡ⿄ 䲚ড়䍋ᴹˈ⫼䗫ো䱨ᓔDŽ䖭ḋˈ㦋পܼ䚼ⱘѸᯧ䆄ᔩᯊˈᕫࠄⱘ↣ϔϾѸᯧ䆄ᔩᇍ䈵˄SaleRecord˅ˈ 䛑ৃҹⳈ᥹ getBookNames ៪㗙 getAmount ህৃҹᕫࠄѸᯧЁ⍝ঞкⱘৡ⿄੠ᘏ᭄䞣њDŽ Download at http://www.pin5i.com/ /Download at http://www.pin5i.com this.recordDate.setText(record.getRECORD_DATE()); this.totalPrice.setText(String.valueOf(record.getTotalPrice())); this.saleRecordId.setText(record.getID()); refreshBookSaleRecordTableData(); //ࠋᮄкᴀ䫔ଂ߫㸼 this.bookSaleRecordDatas = record.getBookSaleRecords(); //䆒㕂ᔧࠡкᴀ䫔ଂ᭄᥂ SaleRecord record = saleRecordService.get(saleRecordId); //ᕫࠄкⱘѸᯧ䆄ᔩ String saleRecordId = getSelectId(getJTable()); private void view() { //ᶹⳟϔᴵ䫔ଂ䆄ᔩ } setBookSaleRecordTableFace(); //䆒㕂㸼Ḑḋᓣ tableModel.setDataVector(view, this.bookSaleRecordColumns); //ᇚ᭄᥂䆒ܹ㸼Ḑ Model Ё DefaultTableModel tableModel = (DefaultTableModel)this.bookSaleRecordTable.getModel(); Vector view = changeBookSaleRecordDate(this.bookSaleRecordDatas); private void refreshBookSaleRecordTableData() { //ࠋᮄкᴀ䫔ଂ䆄ᔩⱘ߫㸼 ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\ui\SalePanel.java ሖᮍ⊩੠Ϯࡵሖᮍ⊩ˈⳈ᥹ᇍ⬠䴶䖯㸠໘⧚DŽ кⱘৡ⿄ǃѸᯧᘏӋ੠Ѹᯧ᭄䞣ˈ᳔ৢ䖨ಲ SaleRecord ᇍ䈵DŽҹϟⳕ⬹ᶹᡒ SaleRecord ⱘ᭄᥂䆓䯂 ࠄ SaleRecord ᇍ䈵ˈ䇗⫼ 9.7.1 Ёⱘ processDatas ᮍ⊩ᇍऩϾ SaleRecord 䖯㸠ؐⱘ໘⧚ˈߚ߿䆒㕂 ᅲ⦄᭄᥂䆓䯂ሖ੠Ϯࡵሖˈᕫܜ↣ϔ⃵Ѹᯧ䆄ᔩЁ᠔⍝ঞⱘ໮ᴀкⱘ䆄ᔩˈ䛑䳔㽕೼ϟ䴶ⱘ߫㸼ᰒ⼎DŽ ᶹⳟѸᯧ䆄ᔩˈϢᶹⳟߎ⠜⼒ǃᶹⳟкᴀㄝࡳ㛑㉏Ԑˈাᰃ䳔㽕໘⧚кⱘ䫔ଂ䆄ᔩⱘ߫㸼ᰒ⼎ˈे 9.7.2 ᶹⳟѸᯧ䆄ᔩ ᕫࠄᰒ⼎ⱘ᭄᥂ৢˈህৃҹⳈ᥹ᰒ⼎೼߫㸼Ёˈ݋ԧᰒ⼎䇋ᶹⳟ 9.5.2 Ёᅲ⦄㦋পܼ䚼ߎ⠜⼒ࡳ㛑DŽ } return view; } view.add(v); v.add(record.getAmount()); //ᕫࠄѸᯧᘏ᭄ v.add(record.getRECORD_DATE()); v.add(record.getTotalPrice()); //ᕫࠄѸᯧⱘᘏӋ v.add(record.getBookNames()); //ᕫࠄϔ⃵ѸᯧЁ᠔᳝кⱘৡᄫ v.add(record.getID()); Vector v = new Vector(); for (SaleRecord record : records) { Vector view = new Vector(); private Vector changeDatas(Vector records) { //ᇚ᭄᥂䕀ᤶ៤Џ߫㸼ⱘ᭄᥂Ḑᓣ ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\ui\SalePanel.java /Download at http://www.pin5i.com } Double.valueOf(r.getBook().getBOOK_PRICE())); totalPrice += (Integer.valueOf(r.getTRADE_SUM()) * for (BookSaleRecord r : this.bookSaleRecordDatas) { double totalPrice = 0; private void countTotalPrice() { //৥кⱘ䫔ଂ䆄ᔩ߫㸼Ёߴ䰸៪㗙⏏ࡴϔᴀкⱘᯊ׭䅵ㅫᘏӋ } setTotalAmount(); //䅵ㅫᘏ᭄䞣 countTotalPrice(); //䅵ㅫᘏӋ refreshBookSaleRecordTableData(); //ࠋᮄ߫㸼 appendOrUpdate(book, amount); String amount = this.bookAmount.getText(); Book book = (Book)bookComboBox.getSelectedItem(); //㦋ᕫ䗝Ёⱘᇍ䈵 private void appendBook() { //৥߫㸼⏏ࡴϔᴵкⱘ䫔ଂ䆄ᔩ ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\ui\SalePanel.java Ͼ JTable ⏏ࡴϸϾџӊˈߚ߿ᰃҢ䆹 JTable Ё⏏ࡴϔᴀк੠ߴ䰸ϔᴀкDŽҹϟЎϸϾᮍ⊩ⱘᅲ⦄DŽ ߫㸼˄⬠䴶Ϟ䴶ⱘ߫㸼˅ϔḋˈ঺໪ˈ䰸њ䳔㽕ᇚкⱘ䫔ଂ䆄ᔩᬒ㕂ࠄϟ䴶ⱘ JTableˈ䖬㽕Ўϟ䴶ⱘ䖭 ݊ᅲ䫔ଂㅵ⧚⬠䴶↨кᴀㅵ⧚⬠䴶໮њϔϾ JTable ᇍ䈵ˈ⫼Ѣᰒ⼎кⱘ䫔ଂ䆄ᔩˈ໘⧚ᮍᓣϢЏ } return view; } view.add(v); v.add(r.getBook().getID()); v.add(r.getTRADE_SUM()); v.add(r.getBook().getBOOK_PRICE()); v.add(r.getBook().getBOOK_NAME()); v.add(r.getID()); Vector v = new Vector(); for (BookSaleRecord r : records) { Vector view = new Vector(); private Vector changeBookSaleRecordDate(Vector records) { //ᇚкⱘ䫔ଂ䆄ᔩ䕀ᤶ៤߫㸼Ḑᓣ } this.bookSaleRecordTable.getColumn("bookId").setMaxWidth(-1); this.bookSaleRecordTable.getColumn("bookId").setMinWidth(-1); //䱤㮣ᇍᑨⱘк id ߫ this.bookSaleRecordTable.getColumn("id").setMaxWidth(-1); this.bookSaleRecordTable.getColumn("id").setMinWidth(-1); //䱤㮣䫔ଂ䆄ᔩ id ߫ this.bookSaleRecordTable.setRowHeight(30); private void setBookSaleRecordTableFace() { //䆒㕂кᴀ䫔ଂ䆄ᔩⱘḋᓣ } this.amount.setText(String.valueOf(record.getAmount())); /Download at http://www.pin5i.com ᕫࠄⱘᬜᵰབ೒ 9.11 ᠔⼎DŽ 䆹᭄᥂ˈ䅽кⱘѸᯧ䆄ᔩⱘ᭄ؐࡴϞᮄ䕧ܹⱘ᭄ؐˈབᵰ䆹кϡ೼߫㸼Ёˈ߭ݡ৥䞠䴶ᮄ๲ϔᴵ䆄ᔩDŽ ⱘ appendOrUpdate ᮍ⊩ˈᔧ৥߫㸼⏏ࡴϔᴀкⱘ䫔ଂ䆄ᔩᯊˈབᵰ䆹ᴀкᏆ㒣೼߫㸼Ёњˈህএׂᬍ ҹϞҷⷕЁⱘ appendBook ੠ removeBook 䋳䋷໘⧚⏏ࡴ੠ߴ䰸кᴀⱘ䫔ଂ䆄ᔩ߫㸼ⱘ᭄᥂DŽҹϞ } countTotalPrice(); setTotalAmount(); //䞡ᮄ䅵ㅫᘏӋ੠ᘏ᭄䞣 refreshBookSaleRecordTableData(); //ࠋᮄ߫㸼 this.bookSaleRecordDatas.remove(bookSaleRecordTable.getSelectedRow()); //೼䲚ড়Ёߴ䰸ᇍᑨⱘ㋶ᓩⱘ᭄᥂ private void removeBook() { //Ң߫㸼Ё⿏䰸ϔᴵкⱘ䫔ଂ䆄ᔩ } } r.setTRADE_SUM(String.valueOf(newAmount)); int newAmount = Integer.valueOf(amount) + Integer.valueOf(r.getTRADE_SUM()); } else { this.bookSaleRecordDatas.add(record); record.setTRADE_SUM(amount); record.setBook(book); BookSaleRecord record = new BookSaleRecord(); //߯ᓎ BookSaleRecord ᇍ䈵ᑊ⏏ࡴࠄ᭄᥂䲚ড়Ё if (r == null) { //བᵰЎぎ, ߭Ўᮄ⏏ࡴⱘк, 䴲ぎ, ߭䆹кᏆ㒣೼߫㸼Ё BookSaleRecord r = getBookSaleRecordFromView(book); private void appendOrUpdate(Book book, String amount) { //⏏ࡴ៪㗙ׂᬍкᴀѸᯧ䆄ᔩЁⱘᇍ䈵 } this.amount.setText(String.valueOf(amount)); } amount += Integer.valueOf(r.getTRADE_SUM()); for (BookSaleRecord r : this.bookSaleRecordDatas) { int amount = 0; private void setTotalAmount() { //৥кⱘ䫔ଂ䆄ᔩ߫㸼Ёߴ䰸៪㗙⏏ࡴϔᴀкⱘᯊ׭䆒㕂ᘏ᭄䞣 } this.totalPrice.setText(String.valueOf(totalPrice)); /Download at http://www.pin5i.com } return String.valueOf(getJDBCExecutor().executeUpdate(sql)); "', '" + record.getT_SALE_RECORD_ID_FK() + "', '" + record.getTRADE_SUM() + "')"; String sql = "INSERT INTO T_BOOK_SALE_RECORD VALUES (ID, '" + record.getBook().getID() + //ֱᄬϔᴵкⱘ䫔ଂ䆄ᔩ public String saveBookSaleRecord(BookSaleRecord record) { ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\dao\impl\BookSaleRecordDaoImpl.java кⱘ䫔ଂ䆄ᔩ᭄᥂䆓䯂ሖ BookSaleRecordDaoImplDŽ ᯧ⍝ঞ໮ᴀкˈ಴ℸ៥Ӏ䳔㽕ֱᄬкⱘѸᯧ䆄ᔩˈᑊϨᰃ໮ᴵDŽ ⬅Ѣ៥Ӏⱘ䫔ଂ䆄ᔩ㸼ˈাᰃֱᄬњϔϾѸᯧᯊ䯈ˈ಴ℸϡ䳔㽕໾໡ᴖⱘ໘⧚ˈԚᰃˈ⬅Ѣϔ⃵Ѹ } return String.valueOf(getJDBCExecutor().executeUpdate(sql)); String sql = "INSERT INTO T_SALE_RECORD VALUES(ID, '" + r.getRECORD_DATE() + "')"; //ᠻ㸠 SQL ݭ᭄ܹ᥂ public String save(SaleRecord r) { ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\dao\impl\SaleRecordDaoImpl.java ᮹ᳳǃ৘ᴀкⱘѸᯧֵᙃㄝDŽҹϟЎ᭄᥂䆓䯂ሖ˄SaleRecordDaoImpl˅ⱘᅲ⦄DŽ ᔧ೼⬠䴶Ёᕫࠄ৘Ͼ᭄᥂ৢˈेৃҹ䇗⫼Ϯࡵሖ᥹ষⱘᮍ⊩ֱᄬѸᯧ䆄ᔩˈ㸼⦄ሖⱘ᭄᥂ࣙᣀѸᯧ 9.7.3 ᅲ⦄ᮄ๲Ѹᯧ䆄ᔩࡳ㛑 ೒ 9.11 ᅲ⦄Ѹᯧ䆄ᔩⱘᶹⳟࡳ㛑DŽ /Download at http://www.pin5i.com ৃҹখ㗗䫔ଂㅵ⧚ࡳ㛑ⱘᅲ⦄DŽܹᑧㅵ⧚ࡳ㛑བ೒ 9.12 ᠔⼎DŽ 䴶ⱘϟ䴶䚼ߚгᰃ᳝ϔϾܹᑧⱘкⱘ䆄ᔩ߫㸼ˈॳ⧚ϢкⱘѸᯧ䆄ᔩ߫㸼ϔ㟈DŽܹᑧㅵ⧚ࡳ㛑ⱘᅲ⦄ˈ ᇥᑧᄬˈܹᑧᰃ๲ࡴᑧᄬDŽϢ䫔ଂㅵ⧚ࡳ㛑ϔḋⱘᰃˈܹᑧㅵ⧚⬠ޣḋг䳔㽕ׂᬍкᴀⱘᑧᄬˈ䫔ଂᰃ 䖛ⳌৠⱘѸѦᮍᓣᕫࠄ䫔ଂ៪㗙ܹᑧⱘⳂⱘˈԚᰃϢПϡৠⱘഄᮍᰃˈ೼໘⧚ܹᑧϮࡵⱘᯊ׭ˈ៥Ӏৠ ⧚ᮍ⊩ϢЏ߫㸼ⱘ໘⧚ᮍᓣ໻㟈ⳌৠDŽ៥Ӏৃҹএⳟϔϟܹᑧㅵ⧚⬠䴶ˈϢ䫔ଂㅵ⧚⬠䴶ϔḋˈ䛑ᰃ䗮 ࠄℸˈ䫔ଂ⬠䴶ⱘࡳ㛑Ꮖ㒣ᅠ៤ˈϢ݊Ҫࡳ㛑Ⳍ↨ˈ䫔ଂㅵ⧚⬠䴶াᰃ໮њϔϾ߫㸼ˈ䆹߫㸼ⱘ໘ 䯈ᶹ䆶ᔧ໽᠔᳝ⱘ䫔ଂ䆄ᔩˈݡ䖯㸠᭄᥂ᰒ⼎ˈ೼ℸϡݡ䆺㒚䌬䗄DŽ кᴀⱘᄬᑧ䞣ᰃ৺䎇໳ˈབᵰϡ䎇໳߭ᡯߎᓖᐌDŽϟ䴶ৃҹᅲ⦄䫔ଂ䆄ᔩⱘᶹ䆶ˈৃҹḍ᥂ᶤϔ໽ⱘᯊ ҹϞҷⷕֱᄬњ䫔ଂ䆄ᔩᑊׂᬍкᴀⱘᑧᄬ䞣ˈ೼䖯㸠ֱᄬ䫔ଂ䆄ᔩࠡˈ䳔㽕䖯㸠Ϯࡵ߸ᮁˈ߸ᮁ } } bookDao.updateRepertory(b); b.setREPERTORY_SIZE(String.valueOf(leave)); //䆒㕂ᑧᄬᑊᇚᑧᄬ᭄ֱᄬࠄ᭄᥂ᑧ Integer.valueOf(r.getTRADE_SUM()); int leave = Integer.valueOf(b.getREPERTORY_SIZE()) - //䅵ㅫ࠽ԭⱘᑧᄬ Book b = bookDao.find(bookId); String bookId = r.getBook().getID(); //ׂᬍкⱘᑧᄬ bookSaleRecordDao.saveBookSaleRecord(r); r.setT_SALE_RECORD_ID_FK(id); //䆒㕂䫔ଂ䆄ᔩ id for (BookSaleRecord r : record.getBookSaleRecords()) { //ݡֱᄬкⱘѸᯧ䆄ᔩ String id = saleRecordDao.save(record); ᄬѸᯧ䆄ᔩֱܜ// { { ;(" throw new BusinessException(b.getBOOK_NAME() + " ⱘᑧᄬϡ໳ Integer.valueOf(b.getREPERTORY_SIZE())) { if (Integer.valueOf(r.getTRADE_SUM()) > //ᔧᄬᑧϡ໳ᯊ,ᡯߎᓖᐌ Book b = bookDao.find(bookId); String bookId = r.getBook().getID(); for (BookSaleRecord r : record.getBookSaleRecords()) { //䘡ग़߸ᮁкⱘᑧᄬᰃ৺ϡ໳ public void saveRecord(SaleRecord record) { ҷⷕ⏙ऩ˖code\book\src\org\crazyit\book\service\impl\SaleRecordServiceImpl.java BookSaleRecord˄໮Ͼ˅੠ SaleRecord˄ϔϾ˅㒭ࠄϸϾ᭄᥂䆓䯂ሖेৃDŽ ᯧ䆄ᔩᇍᑨ໮кᴀⱘ䫔ଂ䆄ᔩˈ಴ℸϔᴵкⱘ䫔ଂ䆄ᔩˈা䳔㽕᳝ϔᴀкⱘ໪䬂DŽϮࡵሖা䳔㽕ᇚ ᄬкᴀⱘ䫔ଂ䆄ᔩˈা䳔㽕ֱᄬᇍᑨⱘ䫔ଂ䆄ᔩⱘ໪䬂ǃкᴀ䆄ᔩ໪䬂੠Ѹᯧ᭄䞣ˈ⬅Ѣϔ⃵Ѹֱ /Download at http://www.pin5i.com \startup.batāेৃˈ咬䅸ⱘ⫼᠋ৡ੠ᆚⷕ䛑ᰃ crazyitˈᴀゴ᠔Փ⫼ⱘ MySQL ⠜ᴀᰃ 5.0DŽ ㋏㒳\book.jarāׂᬍࣙϟⱘĀbook.jar\cfg\jdbc.propertisā䜡㕂᭛ӊˈ᳔ৢ䖤㸠Ābin\೒к䖯ᄬ䫔㋏㒳 к䖯ᄬ䫔㋏㒳\sqlāⳂᔩϟⱘ BOOK_SYSTEM.sql ᇐܹԴⱘ MySQL ᭄᥂ᑧЁˈᠧᓔĀbin\೒к䖯ᄬ䫔 དⱘҷⷕˈৃҹᇍᴀゴЁⱘҷⷕ䖯㸠䞡ᵘˈᑊৃҹᑨ⫼ᇍᑨⱘ䆒䅵῵ᓣDŽ䖤㸠ᴀゴᅝܼ䳔㽕ᇚĀbin\೒ ЁㅔऩⱘՓ⫼њ῵ᵓᮍ⊩䖭⾡䆒䅵῵ᓣˈᇚ৘Ͼ⬠䴶Ёⱘ݅ৠ⚍ᦤপࠄ⠊㉏䖯㸠໘⧚DŽབᵰ䳔㽕ݭ᳈㡃 Ԣњ৘ሖП䯈ⱘҷⷕ㗺ড়DŽ೼㸼⦄ሖޣJTable ⱘ໘⧚ˈ㸼⦄ሖǃϮࡵሖϢ᭄᥂䆓䯂ሖⱘߚሖ㒧ᵘˈ໻໻ ⱘডᇘᡔᴃᇕ㺙᭄᥂ˈՓ⫼ऩᗕ῵ᓣএ㦋ᕫ᭄᥂ᑧ䖲᥹ˈ೒⠛ⱘय़㓽໘⧚ㄝⶹ䆚⚍DŽ೼⬠䴶ЁЏ㽕Փ⫼ ೼ᴀゴЁˈ៥Ӏᅲ⦄њϔϾㅔऩⱘ೒к䖯ᄬ䫔㋏㒳ˈᄺдњՓ⫼ JDBC 䖯㸠᭄᥂ᑧ᪡԰ˈՓ⫼ Java 9.8 ᴀゴᇣ㒧 ೒ 9.12 ܹᑧㅵ⧚ࡳ㛑 ㄀ 9 ゴ ೒к䖯ᄬ䫔㋏㒳g42g /Download at http://www.pin5i.com ⱘᴗ䰤ेৃDŽ⫼᠋ⱏᔩ⬠䴶བ೒ 10.1 ᠔⼎DŽ ㅔऩخ᭄᥂ᴗ䰤ᰃᣛ⫼᠋ⱘ䑿ӑᑊϡ㛑এ᪡԰˄ᶹⳟ˅ᶤѯ᭄᥂DŽᴀゴⱘ䞡⚍ᑊϡᰃᴗ䰤ˈ಴ℸা䳔㽕 䰤ϔ㠀ࣙᣀࡳ㛑ᴗ䰤੠᭄᥂ᴗ䰤ˈࡳ㛑ᴗ䰤ᰃᣛᶤϾ⫼᠋ᑊϡ㛑ⳟࠄ䖭Ͼࡳ㛑ˈҢ㗠ϡ㛑Փ⫼䖭Ͼࡳ㛑ˈ ᠋ⱘ䑿ӑˈݡḍ᥂䖭Ͼ䑿ӑএ߸ᮁાѯࡳ㛑ৃҹՓ⫼៪㗙ϡৃҹՓ⫼ˈгህᰃ៥Ӏᐌ䇈ⱘᴗ䰤䯂乬DŽᴗ བᵰ䳔㽕䖯㸠џࡵ䎳䏾㋏㒳䖯㸠Ϯࡵ᪡԰ˈ䳔㽕䗮䖛ⱏᔩ䖯ܹDŽ⫼᠋䖯㸠ⱏᔩˈৃҹ䅽㋏㒳ⶹ䘧⫼ 10.2.1 ⱏᔩ⬠䴶 䳔∖⹂ᅮ⬠䴶Ϣ㋏㒳ѸѦDŽ ḍ᥂ܜ㋏㒳ϔḋˈ䛑ᰃՓ⫼ Swing ߯ᓎ㋏㒳⬠䴶ˈৠḋՓ⫼ MySQL ԰Ў㋏㒳᭄᥂ᑧDŽ೼ᴀᇣ㡖ˈ៥Ӏ ៥Ӏ໻ὖњ㾷њ㋏㒳ⱘ䳔∖ৢˈৃҹḍ᥂䖭ѯ䳔∖߯ᓎ⬠䴶DŽᴀゴϢࠡ䴶ゴ㡖Ёⱘ೒к䖯ᄬ䫔ㅵ⧚ 10.2 ᓎゟ⬠䴶 ህ᳝䖭ѯˈ䆹㋏㒳ⱘࡳ㛑ᑊϡ໡ᴖˈা㽕⹂ᅮད䳔∖ˈᓔথህ᳈ࡴㅔऩDŽ ҹ೼䖭Ͼ䖛⿟Ёњ㾷ࠄ᠟ϟⱘᎹ԰佅੠ᑺঞ໘⧚џᚙⱘ㛑࡯ㄝֵᙃDŽᭈϾ㋏㒳ⱘ໻㟈䳔㽕ৃˈމ䖭Ͼᚙ Ҫৃҹᇚџࡵ䕀থ㒭঺໪ⱘৠџˈ䅽݊ᐂᖭ໘⧚ˈ㋏㒳䳔㽕䆄ᔩϔᴵ䕀থ䆄ᔩˈ䅽џࡵㅵ⧚㗙ৃҹњ㾷 ᔧϔϾਬᎹ᥹ᬊࠄϔϾџࡵⱘᯊ׭ˈབᵰҪথ⦄㞾Ꮕ᳔䖥ḍᴀ≵᳝ᯊ䯈៪㗙≵᳝㛑࡯এ໘⧚ˈ䙷М ϔϾㅔऩⱘџࡵ䎳䏾㋏㒳DŽخ㋏㒳ˈাᰃ 䅶ऩخҹ೼㋏㒳໪ਞ䆝Ҫⱘᅶ᠋ˈ៥ӀᏆ㒣থ䋻њˈ䇋ᅶ᠋ᶹᬊㄝDŽ䖭䞠䳔㽕⊼ᛣⱘᰃˈ៥Ӏᑊϡᰃ㽕 ៤䖭Ͼџࡵ䖯㸠Ꮉ԰ˈᠻ㸠ᅠ䆹џࡵৢˈህᇚ໘⧚㒧ᵰਞ䆝㋏㒳ˈᔧ㒣⧚ⳟࠄџࡵⱘ໘⧚㒧ᵰৢˈህৃ 䳔㽕Ҫⱘ᠟ϟএ䖯㸠໘⧚ˈህৃҹᮄᓎϔϾџࡵˈߚথࠄҪⱘ᠟ϟˈ᥹ᬊࠄ䖭ϾџࡵⱘਬᎹˈህ㽕Ўᅠ ㄝⳌֵ݇ᙃˈབᵰџࡵ໘⧚ᅠ៤ˈ䙷Мㅵ⧚Ҏህ䳔㽕೼㋏㒳໪䖯㸠ϔѯᎹ԰ˈ՟བᶤ㒣⧚᥹ࠄϔϾ䅶ऩˈ ǃџࡵⱘᔧࠡ໘⧚Ҏމ⢊㸠㗙໘⧚џࡵˈᇚџࡵⱘ໘⧚㒧ᵰড作㒭ㅵ⧚Ҏˈㅵ⧚Ҏৃҹᶹⳟџࡵⱘ䖯㸠 ᭈϾџࡵ䎳䏾㋏㒳ⱘЁᖗᰃџࡵˈ᠔᳝ⱘ᪡԰䛑ᰃೈ㒩ⴔџࡵ䖯㸠ⱘˈㅵ⧚Ҏ߯ᓎǃߚথџࡵˈᠻ 䖬≵᳝䗮DŽ ࠄϔᅮ⿟ᑺⱘᯊ׭ˈህӮ᳝⚍ϡⶹ᠔᥾ˈ಴ЎϮࡵخМ೼ᓔথⱘ䖛⿟ЁˈህӮѻ⫳ϔ㋏߫ⱘ䯂乬ˈ՟བ 㽕ᣝ✻䖭ѯ䳔∖䖯㸠Ⳍ݇ⱘ䆒䅵ˈݡᣝ✻䖭ѯ䳔∖এᅲ⦄Ⳍᑨⱘࡳ㛑DŽབᵰϔᓔྟ≵᳝⹂ᅮད䳔∖ˈ䙷 ᓔথџࡵ䎳䏾㋏㒳ࠡˈ៥Ӏ䳔㽕⹂ᅮ䆹㋏㒳ⱘ䳔∖ˈϔᮺ⹂ᅮњ䳔∖ˈ៥Ӏ೼ᓔথⱘ䖛⿟Ёˈህ䳔 10.1 ⹂ᅮ㋏㒳䳔∖ ᅮⱘⲥ᥻԰⫼DŽ೼ᴀゴЁˈ៥ӀህՓ⫼ Java ᓔথϔϾㅔऩⱘ CS џࡵ䎳䏾㋏㒳DŽ ↣ϔ䰊↉ⱘ໘⧚㒧ᵰֱᄬࠄ㋏㒳Ёˈߚথџࡵⱘㅵ⧚㗙ህৃҹᯊᯊࠏࠏњ㾷џࡵⱘ䖯ሩˈᇍџࡵ䍋ࠄϔ ࡵˈᑊᇚ䆹џࡵߚথࠄⳌᑨⱘਬᎹএ໘⧚ˈਬᎹ᥹ᬊࠄ䖭Ͼџࡵⱘᯊ׭ˈህৃҹᇍџࡵ䖯㸠໘⧚ˈᑊᇚ ⱘᯊ׭ˈৃҹՓ⫼џࡵ䎳䏾㋏㒳߯ᓎϔϾџއ㗙䳔㽕䌁фᶤѯᴤ᭭˅ˈ㗠䖭Ͼџࡵϡᖙ⬅㞾Ꮕ҆㞾এ㾷 ࡵ䖯㸠䎳䏾ǃㅵ⧚DŽ՟བ᳝䖭ḋϔϾഎ᱃ˈᔧᶤ݀ৌⱘ㒣⧚᥹ᬊࠄϔϾџࡵ˄՟བ䳔㽕໘⧚ᶤᓴ䅶ऩ៪ ೼ӕϮᑨ⫼Ёˈ៥Ӏ㒣ᐌӮՓ⫼ࠄϔѯџࡵ䎳䏾㋏㒳ˈ䖭ѯџࡵ䎳䏾㋏㒳ৃҹᐂࡽ៥Ӏ᳝ᬜⱘᇍџ ㄀  ゴџࡵ䎳䏾㋏㒳 /Download at http://www.pin5i.com ᗕˈ⢊خབᵰ⫼᠋᳔䖥≵᳝ᯊ䯈໘⧚䆹џࡵ˄Ꮉ԰佅੠˅ˈህৃҹᇚџࡵ㕂Ў᱖ᯊϡ˖خ‰ ᱖ᯊϡ ‰ 䕀থџࡵ˖ᔧ⫼᠋≵᳝ᯊ䯈៪㗙≵᳝㛑࡯ᠻ㸠䆹џࡵⱘᯊ׭ˈৃҹᇚ䆹џࡵ䕀থ㒭݊Ҫ໘⧚ҎDŽ ‰ ᅠ៤џࡵ˖ᔧ⫼᠋ᠻ㸠ᅠᶤϾџࡵৢˈৃҹ⚍ߏᅠ៤ˈਞ䆝㋏㒳Ꮖ㒣໘⧚ᅠ䆹џࡵњDŽ ‰ ᶹ䆶џࡵ˖ḍ᥂џࡵⱘϡৠ⢊ᗕᶹ䆶Ⳍᑨ⢊ᗕⱘџࡵˈᑊᰒ⼎ࠄ߫㸼ЁDŽ ៥ⱘџࡵ⬠䴶Ёˈ咬䅸ᰒ⼎ⱘᰃ䖯㸠Ёⱘџࡵˈ䆹⬠䴶ࣙᣀњབϟ᪡԰˖ ೒ 10.2 ៥ⱘџࡵ⬠䴶  ሩ⦄ⱘህᰃ䆹⬠䴶DŽ៥ⱘџࡵ⬠䴶བ೒ 10.2 ᠔⼎DŽܜџࡵ䎳䏾㋏㒳ˈ佪 ⱘџࡵ䖯㸠໘⧚ˈ៥ⱘџࡵࡳ㛑ᰃ↣Ͼ⫼᠋䛑݋᳝ⱘࡳ㛑ˈϡ䳔㽕ࡴӏࡵⱘᴗ䰤߸ᮁˈ಴ℸ⫼᠋ϔⱏᔩ ៥ⱘџࡵ⬠䴶Џ㽕ᰒ⼎⫼᠋䳔㽕໘⧚ⱘџࡵˈᑊᦤկϔѯ᪡԰䖭ѯџࡵⱘᣝ䪂ˈ䅽⫼᠋ৃҹᇍ㞾Ꮕ 10.2.2 ៥ⱘџࡵ⬠䴶 JPasswordField ㉏ˈᑊϡᰃ JTextFieldDŽ ⱏᔩ⬠䴶कߚㅔऩˈϸϾ䕧ܹḚ੠ϸϾᣝ䪂ेৃˈ݊Ё䳔㽕⊼ᛣⱘᰃˈᆚⷕḚՓ⫼ⱘᰃ ೒ 10.1 ㋏㒳ⱏᔩ⬠䴶  /Download at http://www.pin5i.com এ䗝ᢽ䳔㽕䕀থⱘҎDŽџࡵ䕀থ⬠䴶བ೒ 10.4 ᠔⼎DŽ Ꮕⱘџࡵ䕀থ㒭݊Ҫৠџ䖯㸠໘⧚DŽџࡵ䕀থ⬠䴶Ϣџࡵ໘⧚⬠䴶⿡ᖂ᳝ϔ⚍ϡৠˈ䆹⬠䴶䳔㽕䅽⫼᠋ ⫼᠋থ⦄㞾Ꮕ≵᳝ᯊ䯈៪㗙≵᳝㛑࡯এᅠ៤䆹џࡵⱘᯊ׭ˈህৃҹՓ⫼џࡵⱘ䕀থࡳ㛑ˈᇚሲѢ㞾 10.2.4 џࡵ䕀থ⬠䴶 ೼៥Ӏⱘџࡵ⬠䴶Ёˈ䖬᳝ϔϾ䕀থࡳ㛑ˈ೼ϟ䴶ゴ㡖Ёᦣ䗄DŽ ⱘ᪡԰ᯊˈᠡӮ໘⧚џࡵⱘ⬠䴶ˈخϢϡخ䗄Ⳍ݇ⱘֵᙃDŽ೼䖭䞠䳔㽕⊼ᛣⱘᰃˈ⫼᠋䖯㸠ᅠ៤ǃ᱖ᯊϡ џࡵ໘⧚⬠䴶ᦤկϔϾ JTextField ᴹᰒ⼎џࡵⱘᷛ乬ˈᑊᦤկϔϾ JTextArea ⱘ᭛ᴀḚˈ䅽⫼᠋ᦣ ೒ 10.3 џࡵ໘⧚⬠䴶  ಴ℸ㋏㒳䳔㽕ᦤկϔϾ໘⧚⬠䴶DŽџࡵ໘⧚⬠䴶བ೒ 10.3 ᠔⼎DŽˈخᯊϡ 㗠䆹⫼᠋জᛇ䇈ᯢЎҔМ᱖ˈخህ䳔㽕ᦤկϔϾ⬠䴶ˈ䅽⫼᠋䖯㸠Ⳍ݇ⱘ䇈ᯢˈ՟བ⫼᠋⚍ߏњ᱖ᯊϡ ⱘџࡵ໘⧚ࡳ㛑ˈᔧ⫼᠋⚍ߏњ䖭ѯࡳ㛑ⱘᯊ׭ˈخϢϡخ೼៥ⱘџࡵ⬠䴶ˈᦤկњᅠ៤ǃ᱖ᯊϡ ⱘ໘⧚⬠䴶خǃϡخ10.2.3 џࡵⱘᅠ៤ǃ᱖ᯊϡ া䳔㽕Ў䆹 JTable ᇍ䈵ᦤկ TableCellRenderer ⱘ㉏ᴹ␆ᶧ߫㸼ेৃDŽ ೼ᴀ㋏㒳Ёˈџࡵࣙᣀⱘ⢊ᗕ೼ϟ䴶ゴ㡖ᇚӮ䆺㒚ᦣ䗄DŽ៥ⱘџࡵ⬠䴶Џ㽕ᰃϔϾ JTable ᇍ䈵ˈ ӏԩⱘᎹ԰DŽخāⱘ⢊ᗕˈ䅽ㅵ⧚㗙ⶹ䘧ˈ⫼᠋ϡӮЎ䆹џࡵخࡵⱘ⢊ᗕ䆒㕂ЎĀϡ བᵰ⫼᠋㾝ᕫ䖭Ͼџࡵ≵᳝ᄬ೼ⱘᛣН៪㗙ḍᴀህϡᠧㅫᅠ៤䖭Ͼџࡵˈህৃҹᇚ䆹џ˖خ‰ ϡ 䙷Мㅵ⧚㗙ህৃҹⶹ䘧ˈ䆹⫼᠋≵᳝ᯊ䯈໘⧚DŽ  ೒ 10.4 џࡵ䕀থ⬠䴶 བ೒ 10.4 ᠔⼎ˈ೼џࡵ䕀থ⬠䴶Ёˈ᳝ϔϾ䗝ᢽ⫼᠋ⱘᣝ䪂ˈᔧ⫼᠋⚍ߏњ䗝ᢽ⫼᠋ᯊˈህ䳔㽕 ᔍߎ⫼᠋߫㸼ⱘ⬠䴶ˈ䅽⫼᠋এ䗝ᢽ䳔㽕䕀থⱘৠџˈ⫼᠋䗝ᢽ⬠䴶೼ϟ䴶ⱘ⫼᠋ㅵ⧚ゴ㡖ЁᇚӮ䆺㒚 ᦣ䗄DŽ 10.2.5 џࡵㅵ⧚⬠䴶 џࡵㅵ⧚⬠䴶Џ㽕䅽ㅵ⧚Ҏਬ䖯㸠џࡵⱘㅵ⧚ˈ՟བᮄ๲џࡵǃᶹⳟџࡵ⌕⿟ㄝˈϢ៥ⱘџࡵ⬠䴶 ϔḋˈᦤկϔϾ JTable ᇍ䈵ˈ䅽ㅵ⧚Ҏਬৃҹ⏙᱄ⱘњ㾷৘⾡⢊ᗕⱘџࡵDŽ䖭䞠䳔㽕⊼ᛣⱘᰃˈᶤϔ Ͼџࡵㅵ⧚㗙া㛑ⳟࠄ㞾Ꮕথ䍋ⱘџࡵˈᶹⳟࠄџࡵⱘⳌ݇⢊ᗕϢ⌕⿟ৢˈህৃҹ೼㋏㒳໪䖯㸠䎳䖯Ϣ ໘⧚DŽџࡵㅵ⧚⬠䴶བ೒ 10.5 ᠔⼎DŽ Download at http://www.pin5i.com/ /Download at http://www.pin5i.com ⱘⳌֵ݇ᙃৢˈ㋏㒳ህᇚ䖭ѯֵᙃֱᄬࠄ᭄᥂ᑧЁDŽᮄᓎџࡵ⬠䴶བ೒ 10.6 ᠔⼎DŽ ᣛᅮⳌ݇ⱘ໘⧚ҎDŽᮄᓎⱘџࡵࣙᣀџࡵⱘᷛ乬ǃݙᆍǃᅠ៤ᯊ䯈ǃ໘⧚Ҏㄝֵᙃˈᔧ⫼᠋䕧ܹњџࡵ џࡵㅵ⧚㗙᥹ᬊࠄϔϾᮄⱘџࡵˈ䳔㽕ϟথࠄ㞾Ꮕⱘ᠟ϟ䖯㸠໘⧚ⱘᯊ׭ˈৃҹᮄᓎϔϾџࡵˈᑊ 10.2.6 ᮄᓎџࡵ⬠䴶 ‰ 㕂Ў᮴ᬜ˖ᇚϔϾџࡵ䆒㕂Ў᮴ᬜˈ䙷Мџࡵᠻ㸠Ҏህৃҹⶹ䘧ϡᖙЎ䆹џࡵ䖯㸠ӏԩⱘᎹ԰DŽ џࡵ䖯㸠໘⧚DŽ ᇍ䆹ܜࡲ䆹џࡵˈ䅽џࡵ໘⧚Ҏⶹ䘧ˈ䳔㽕ڀ㑻䕗催ˈ߭ৃҹܜࡲџࡵ˖བᵰϔϾџࡵⱘӬڀ ‰ ‰ ᮄᓎџࡵ˖߯ᓎϔϾᮄⱘџࡵˈᑊЎ݊ᣛᅮ໘⧚ҎDŽ ‰ џࡵᶹ䆶˖ৃҹᶹ䆶৘⾡⢊ᗕⱘџࡵDŽ џࡵㅵ⧚⬠䴶ᦤկњབϟࡳ㛑˖ ೒ 10.5 џࡵㅵ⧚⬠䴶  /Download at http://www.pin5i.com ⫼᥹ষⱘ confirm ᮍ⊩ेৃˈϡ䳔㽕⧚Ӯ݋ԧⱘᅲ⦄DŽ⫼᠋䗝ᢽ⬠䴶བ೒ 10.7 ᠔⼎DŽ 䙷М೼⫼᠋䗝ᢽ⬠䴶Ёˈা䳔㽕ᦤկϔϾ䖭ḋⱘ᥹ষेৃˈ᪡԰Ҏ䗝ᢽњᶤϔϾ⫼᠋ৢˈা䳔㽕䇗 } void confirm(String userId, String realName); */ * @param realName * @param userId * ೼⫼᠋䗝ᢽ⬠䴶⚍ߏ⹂ᅮৢᠻ㸠ⱘᮍ⊩ /** public interface UserSelectHandler { ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\ui\handler\UserSelectHandler.java˖ 䗝ᢽњᶤϾ⫼᠋ৢˈা䳔㽕䇗⫼ϡৠⱘᮍ⊩ेৃDŽᮄᓎϔϾ⫼᠋䗝ᢽ໘⧚᥹ষDŽ ህ䳔㽕ᠧᓔ⫼᠋䗝ᢽ⬠䴶䖯㸠䗝ᢽˈ೼ᴀゴЁˈ៥ӀՓ⫼ৠϔϾ⬠䴶ᴹᅲ⦄⫼᠋䗝ᢽⱘࡳ㛑DŽᔧ᪡԰Ҏ ೼џࡵ䕀থ⬠䴶Ϣᮄᓎџࡵ⬠䴶Ёˈ䛑᳝ϔϾ⫼᠋䗝ᢽࡳ㛑ˈᔧ᪡԰Ҏ䳔㽕䖯㸠⫼᠋䗝ᢽⱘᯊ׭ˈ 10.2.7 ⫼᠋䗝ᢽ⬠䴶 ԰Ў⫼᠋䗝ᢽ⬠䴶DŽ བ೒ 10.6 ᠔⼎ˈᮄᓎџࡵЁৠḋ᳝ϔϾ䗝ᢽ⫼᠋ⱘᣝ䪂ˈϢџࡵ䕀থ⬠䴶ϔḋˈՓ⫼ৠϔϾ⬠䴶 ೒ 10.6 ᮄᓎџࡵ⬠䴶   ೒ 10.7 ⫼᠋䗝ᢽ⬠䴶 䙷М᪡԰Ҏ䗝ᢽњ⫼᠋ৢˈህৃҹᠻ㸠⫼᠋䗝ᢽ᥹ষⱘ confirm ᮍ⊩DŽ ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\ui\dialog\SelectUserDialog.java˖ //⫼᠋䗝ᢽ໘⧚㉏ private UserSelectHandler selectHandler; //ᵘ䗴఼ public SelectUserDialog(UserSelectHandler selectHandler) { this.selectHandler = selectHandler; ... } //⚍ߏ⹂ᅮᠻ㸠ⱘᮍ⊩ private void confirm() { //ᕫࠄ䗝ᢽⱘ⫼᠋ id Ϣⳳᅲৡ⿄ //䇗⫼⫼᠋䗝ᢽ໘⧚㉏ⱘᮍ⊩ this.selectHandler.confirm(id, realName); this.setVisible(false); } ҹϞҷⷕᰃ⫼᠋䗝ᢽ⬠䴶ⱘ䚼ߚᅲ⦄ˈ⊼ᛣҹϞҷⷕⱘ咥ԧ䚼ߚˈ᪡԰Ҏ⚍ߏњ⹂ᅮৢˈህᠻ㸠 confirm ᮍ⊩DŽ䖭ḋˈ⫼᠋䗝ᢽ⬠䴶ህৃҹᅲ⦄䞡⫼ⱘࡳ㛑ˈ಴ℸ೼ᴀゴЁˈা᳝ϔϾ⫼᠋䗝ᢽ⬠䴶㉏DŽ Download at http://www.pin5i.com/ /Download at http://www.pin5i.com 10.2.9 ᠔⼎DŽ Ͼ⫼᠋ৢˈ䆹⫼᠋ህᢹ᳝њ᠔ߚ䜡ⱘ㾦㡆ᴗ䰤ˈህৃҹՓ⫼ᴀ㋏㒳ЁⱘⳌ݇ࡳ㛑DŽᮄᓎ⫼᠋⬠䴶བ೒ ᮄᓎϔϾ⫼᠋ˈ䳔㽕ֱᄬ⫼᠋ৡ˄㋏㒳⫼᠋ৡ˅ǃ⫼᠋ᆚⷕǃ⫼᠋ⳳᅲྦྷৡϢߚ䜡㾦㡆ˈᮄᓎњϔ 10.2.9 ᮄᓎ⫼᠋⬠䴶 䘏䕥ߴ䰸ˈेϡᇚ䆹⫼᠋ֵᙃሩ⼎ࠄ⫼᠋ㅵ⧚⬠䴶ЁDŽ 㸠ᶤϾџࡵˈབᵰᇚ݊Ң᭄᥂ᑧЁߴ䰸ˈ䙷МᇚӮᕅડ݊Ҫ᭄᥂ˈ಴ℸ៥Ӏৃҹᇚ䆹⫼᠋䖯㸠 ‰ ߴ䰸⫼᠋˖ᇚ䆹⫼᠋Ң㋏㒳Ёߴ䰸ˈ䖭䞠䳔㽕⊼ᛣⱘᰃˈ⬅ѢॳᴹᏆ㒣ᄬ೼ⱘ⫼᠋ৃ㛑ℷ೼ᠻ ‰ ᮄᓎ⫼᠋˖ᮄᓎϔϾ⫼᠋ᑊߚ䜡Ⳍᑨⱘ㾦㡆DŽ ‰ ᶹ䆶⫼᠋˖ḍ᥂⫼᠋ྦྷৡᶹ䆶Ⳍ݇ⱘ⫼᠋ˈ೼ᅲ⦄Ё៥ӀՓ⫼῵㊞ᶹ䆶DŽ ⫼᠋ㅵ⧚⬠䴶ࣙᣀབϟࡳ㛑˖ ೒ 10.2.8 ⫼᠋ㅵ⧚⬠䴶  ᮄ๲⫼᠋ǃߴ䰸⫼᠋ㄝDŽ⫼᠋ㅵ⧚⬠䴶བ೒ 10.2.8 ᠔⼎DŽ Ӏ䳔㽕Ўџࡵ䎳䏾㋏㒳ᦤկϔϾ⫼᠋ㅵ⧚⬠䴶ˈ䅽㋏㒳ㅵ⧚ਬৃҹ䕏ᯧⱘㅵ⧚৘Ͼ⫼᠋DŽ⫼᠋ㅵ⧚ࣙᣀ ՟བ᳝ᮄⱘਬᎹܹ㘠ˈ᳝ᮻⱘਬᎹ⾏㘠ˈ಴ℸˈ៥ˈމ೼ᅲ䰙ᑨ⫼Ёˈ䖬ߎ⦄ϔ㋏߫ⱘҎਬবࡼᚙ ㅵ⧚⬠䴶᠋⫼ 10.2.8 /Download at http://www.pin5i.com //⠊㉏ⱘሲᗻ੠㞾ᏅⱘሲᗻⳌࡴ Field[] superFields = clazz.getSuperclass().getDeclaredFields(); //㦋প⠊㉏ⱘሲᗻ Field[] fields = clazz.getDeclaredFields(); //㦋পᴀᇍ䈵ⱘሲᗻ Object vo = clazz.newInstance(); //߯ᓎ㉏ⱘᅲ՟ while (rs.next()) { try { public static Collection getDatas(Collection result, ResultSet rs, Class clazz) { //ᇚ rs Ёⱘؐᇕ㺙៤ϔϾ䲚ড় public class DataUtil { ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\util\DataUtil.java˖ к䖯ᄬ䫔㋏㒳ⱘ᭄᥂䕀ᤶᎹ݋㉏ⱘ݋ԧᅲ⦄DŽ ৡ⿄ᖙ乏㽕Ϣ᭄᥂ᑧЁⱘ㸼ᄫ↉ৡ⿄ϔ㟈DŽ៥Ӏৃҹᇚ೒к䖯ᄬ䫔ㅵ⧚㋏㒳ⱘᎹ݋ᣓᴹՓ⫼ˈҹϟᰃ೒ ᥂ᑧЁ䇏পࠄⱘ ResultSet ᇍ䈵䕀ᤶ៤݋ԧⱘᶤϾ Java ᇍ䈵DŽԚ᳝ϔϾࠡᦤህᰃˈJava ᇍ䈵Ёⱘሲᗻ 䫔㋏㒳˅ˈ೼ᴀゴЁˈ៥ӀৠḋՓ⫼䖭ϔᮍᓣᴹᇍ៥Ӏⱘ᭄᥂䖯㸠䕀ᤶˈᔧᠻ㸠ᶹ䆶ⱘᯊ׭ˈ៥Ӏᇚ᭄ ೼ᓔথ೒к䖯ᄬ䫔ㅵ⧚㋏㒳ⱘᯊ׭ˈ៥ӀՓ⫼ Java ⱘডᇘᴹᇍ᭄᥂䖯㸠ᇕ㺙˄䆺㒚䇋ⳟ೒к䖯ᄬ 10.3.1 ᭄᥂䕀ᤶᎹ݋㉏ ᴹᇍ䖭ѯ㒘ӊ˄DAOǃϮࡵ䘏䕥˅䖯㸠ㅵ⧚DŽ Ӏৃҹᕜ䕏ᵒⱘᇍ৘ሖП䯈ⱘ㒘ӊ䖯㸠ㅵ⧚ˈ⬅ѢᴀゴЁᑊϡ⍝ঞ IoC ᆍ఼ˈ಴ℸ៥ӀৃҹՓ⫼ϔϾ㉏ Ѧ໘⧚ˈϮࡵ䘏䕥ሖЏ㽕⫼Ѣ໘⧚㋏㒳ⱘⳌ݇Ϯࡵˈ㾚೒ሖЏ㽕ᰃ⬠䴶㒘ӊDŽབᵰ᳝Փ⫼ IoC ᆍ఼ˈ៥ ೼ᴀゴЁˈ៥Ӏᇚ㋏㒳ߚ៤ϝሖ㒧ᵘˈDAO ሖǃϮࡵ䘏䕥ሖϢ㾚೒ሖˈDAO ሖ⫼Ѣ䖯㸠᭄᥂ᑧѸ ໛ޚ10.3 ᓔথ ϟ䴶ⱘゴ㡖Ёˈ៥Ӏᇚᅲ⦄䖭ѯࡳ㛑DŽ ⱘ৘Ͼ⬠䴶ˈᑊ⹂ᅮњ㋏㒳ⱘѸѦˈ⬠䴶Ёⱘ৘Ͼ㒘ӊഛՓ⫼ Swing ⱘ㒘ӊˈ೼䖭䞠ϡݡ䆺㒚ᦣ䗄DŽ೼ ӏԩࡳ㛑ˈ㒣⧚ϡৃҹՓ⫼⫼᠋ㅵ⧚ࡳ㛑ˈਬᎹা㛑Փ⫼៥ⱘџࡵࡳ㛑DŽ೼ᴀᇣ㡖Ёˈ៥Ӏࠊᅮњ㋏㒳 ೼ᴀゴЁˈЎњㅔऩ䍋㾕ˈ៥Ӏݙ㕂ϝ⾡⫼᠋㾦㡆˖ㅵ⧚ਬǃ㒣⧚ϢਬᎹˈㅵ⧚ਬৃҹՓ⫼㋏㒳ⱘ ೒ 10.9 ᮄᓎ⫼᠋  /Download at http://www.pin5i.com ⊩ˈህৃҹᇚᄫ↉ؐ䆒㕂ࠄ݋ԧⱘᇍ䈵ЁDŽ 䈵Ёˈ᳔ৢࡴܹࠄ㒧ᵰ䲚Ё䖨ಲDŽ៥ӀՓ⫼њ Java ⱘডᇘᴹᡒࠄᄫ↉ᇍᑨⱘ㉏ሲᗻˈݡ䇗⫼ setter ᮍ ҹϞҷⷕЁⱘ getDatas ᮍ⊩ˈ䘡ग़খ᭄ⱘ ResultSetˈᇚ ResultSet ЁⱘᶤϾᄫ↉ؐ䆒㕂ࠄⳂᷛᇍ } } return l.toArray(new Field[f1.length + f2.length]); for (Field f : f2) l.add(f); for (Field f : f1) l.add(f); List l = new ArrayList(); private static Field[] addFields(Field[] f1, Field[] f2) { //ⳌࡴϸϾ᭄㒘 } return methodName; String methodName = "set" + begin + end; String end = fieldName.substring(1, fieldName.length()); String begin = fieldName.substring(0, 1).toUpperCase(); private static String getSetterMethodName(String fieldName) { //ḍ᥂ሲᗻৡ㦋ᕫ setter ᮍ⊩ⱘᮍ⊩ৡ } } //ᗑ⬹ᓖᐌ } catch (Exception e) { } setterMethod.invoke(vo, value); //Ң ResultSet Ё㦋পϢ䆹ᇍ䈵ሲᗻৡϔ㟈ⱘᄫ↉, ᑊᠻ㸠 setter ᮍ⊩ if (!"null".equals(value)) { String value = rs.getString(field.getName()); //ᔧՓ⫼ ResultSet 㦋পᶤϾᄫ↉ⱘᯊ׭, བᵰ≵᳝䆹ᄫ↉, Ӯߎ⦄ SQLExceptionˈᗑ⬹䆹ᓖᐌ try { Method setterMethod) { private static void invokeMethod(ResultSet rs, Field field, Object vo, //ᠻ㸠ϔϾᮍ⊩, Ң ResultSet Ё㦋পϔϾᄫ↉ⱘ᭄᥂, 䇗⫼ vo ⱘ setter ᮍ⊩ } return result; } throw new DataException(e.getMessage()); e.printStackTrace(); } catch (Exception e) { rs.close(); } result.add(vo); } invokeMethod(rs, field, vo, setterMethod); Method setterMethod = clazz.getMethod(setterMethodName, field.getType()); //㦋ᕫ setter ᮍ⊩ String setterMethodName = getSetterMethodName(field.getName()); //㦋ᕫ setter ᮍ⊩ⱘᮍ⊩ৡ for (Field field : allFields) { //䘡ग़᠔᳝ⱘሲᗻ Field[] allFields = addFields(superFields, fields); /Download at http://www.pin5i.com //ḍ᥂খ᭄ⱘ SQL, ᄬᬒ㒧ᵰⱘ䲚ড়ᇍ䈵, ੠݋ԧⱘ᭄᥂ᑧ᯴ᇘᇍ䈵䖨ಲϔϾ䲚ড় } return JDBCExecutor.getJDBCExecutor(); public JDBCExecutor getJDBCExecutor() { //䖨ಲ JDBCExecutor ᇍ䈵 public class BaseDaoImpl { ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\dao\impl\BaseDaoImpl.java˖ ᠻ㸠㉏DŽ getJDBCExecutor ᮍ⊩ˈgeDatas ᮍ⊩ᇍ ResultSet ᇍ䈵䖯㸠ᇕ㺙ˈgetJDBCExecutor ᮍ⊩䖨ಲ JDBC ᮄᓎϔϾ DAO ሖˈ䅽݊Ҫⱘ DAO ᇍ䈵䛑এ㒻㓁䆹෎㉏ˈ䆹෎㉏াᦤկ getDatas ੠ 10.3.3 DAOሖⱘ෎㉏ ҹϞⱘҷⷕЁՓ⫼ JDBC Ёⱘ Statement ᠻ㸠 SQL 䇁হᕫࠄⳌᑨⱘ ResultSet ᇍ䈵DŽ } return result; rs.close(); } result = rs.getInt(1); //䖨ಲ᳔ৢϔϾЏ䬂 while(rs.next()) { ResultSet rs = stmt.getGeneratedKeys(); //㦋ᕫЏ䬂 stmt.executeUpdate(sql); //ᠻ㸠 SQL 䇁হ public int executeUpdate(String sql) { //ᠻ㸠ऩহ INSERTǃUPDATE ៪ DELETE 䇁হ, བᵰᠻ㸠 INSERT ᯊ, 䖨ಲЏ䬂 } return result; ResultSet result = stmt.executeQuery(sql); //߽⫼ Statement ᇍ䈵ᠻ㸠খ᭄ⱘ sql public ResultSet executeQuery(String sql) { //ᠻ㸠ϔহᶹ䆶ⱘ sql } return count; result.close(); Integer count = result.getInt(1); result.next(); ResultSet result = stmt.executeQuery(sql); //߽⫼ Statement ᇍ䈵ᠻ㸠খ᭄ⱘ sql public Integer count(String sql) { //䅵ㅫ᭄᥂ᘏ᭄ ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\jdbc\JDBCExecutor.java˖ ҹϟᰃ䆹㉏ⱘᦤկⱘ޴Ͼ෎ᴀᮍ⊩DŽ ϔѯ෎ᴀⱘ SQL ᠻ㸠ᮍ⊩ˈࣙᣀ SQL ⱘᠻ㸠ǃ᭄᥂ᑧⱘᶹ䆶ㄝˈᑊϨ䆹㉏೼ᴀ㋏㒳Ёা᳝ϔϾᅲ՟DŽ ᭄᥂ᑧᠻ㸠㉏ֱᄬњ᭄᥂ᑧ䖲᥹ⱘⳌֵ݇ᙃˈࣙᣀ䖲᥹ IPǃ⫼᠋ৡǃᆚⷕㄝ JDBC 䖲᥹ֵᙃˈᦤկ ᭄᥂ᑧᠻ㸠㉏ 10.3.2 /Download at http://www.pin5i.com public class User extends ValueObject { ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\model\User.java˖ ᷛ䆚ሲᗻˈᷛ䆚䆹⫼᠋᭄᥂ᰃ৺㹿ߴ䰸DŽ ⱘˈ᠔ҹ䖬㽕ᦤկϔϾخ᠋ⱘ᭄᥂ˈ㗠ᇍ䆹⫼᠋ⱘ᭄᥂䖯㸠䘏䕥ߴ䰸ˈ䖭ᰃЎњϡᕅડ݊ҪϮࡵ᭄᥂㗠 ᠔ሲⱘ㾦㡆໪䬂ˈ䖬䳔㽕⊼ᛣⱘᰃˈ಴Ў៥Ӏ㽕ᅲ⦄ߴ䰸⫼᠋ⱘࡳ㛑ˈߴ䰸⫼᠋ᯊᑊϡᰃⳳᅲⱘߴ䰸⫼ ᥹ϟᴹ䆒䅵ϔϾ⫼᠋ᇍ䈵ˈ㸼⼎㋏㒳ЁⱘϔϾ⫼᠋ᇍ䈵ˈ⫼᠋ᇍ䈵ࣙᣀ⫼᠋ৡǃᆚⷕǃⳳᅲྦྷৡϢ } private String ROLE_NAME; //㾦㡆ৡ⿄, བ㒣⧚, ᘏⲥ public class Role extends ValueObject { ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\model\Role.java ᑊϡᰃᴗ䰤ㅵ⧚ˈ಴ℸাᦤկᴗ䰤ৡ⿄԰Ўᴗ䰤ᷛ䆚DŽ ߯ᓎϔϾ㾦㡆ᇍ䈵ˈ೼㋏㒳Ё㸼⼎⫼᠋ⱘ㾦㡆ˈ㾦㡆ᇍ䈵াࣙᣀ㾦㡆ৡ⿄ϔϾሲᗻˈ⬅Ѣᴀゴ䞡⚍ 10.4.1 䆒䅵⫼᠋ㅵ⧚ⱘⳌ݇ᇍ䈵 ㋏㒳Ёⱘ㾦㡆DŽ ᯊ׭ˈ៥Ӏৃҹᇚ䖭ѯֵᙃ߯ᓎϔϾ⫼᠋㸼ˈ⫼Ѣֱᄬ⫼᠋䆄ᔩˈ঺໪䖬䳔㽕᳝ϔϾ㾦㡆㸼ˈ⫼Ѣֱᄬ Ң⫼᠋ㅵ⧚⬠䴶ЁৃҹⳟࠄˈϔϾ⫼᠋ࣙᣀ⫼᠋ৡǃᆚⷕǃⳳᅲᬍৡϢ㾦㡆ˈ䙷М೼䆒䅵᭄᥂ᑧⱘ 10.4 ⫼᠋ㅵ⧚ ໛ᅠ៤ˈ᥹ϟᴹৃҹ䖯㸠ࡳ㛑ᓔথDŽޚࠄℸˈ೼ᓔথЁ᠔Փ⫼ࠄⱘ৘Ͼ㉏䛑Ꮖ㒣 ݊Ҫⱘؐᇍ䈵㒻ᡓѢ ValueObjectˈህᛣੇⴔ䆹ؐᇍ䈵ᇍᑨⱘ᭄᥂ᑧ㸼Ёᖙ乏㽕᳝ϔϾ ID ᄫ↉DŽ } private String ID; //ID ᄫ↉,ᇍᑨ᭄᥂ᑧЁⱘ ID ߫ public class ValueObject { ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\model\ValueObject.java˖ 㸠᯴ᇘDŽ៥ӀᦤկϔϾ᠔᳝ؐᇍ䈵ⱘ෎㉏ˈ᠔᳝ⱘؐᇍ䈵䛑䳔㽕㒻㓁ѢᅗDŽ ៥Ӏ೼ᓔথӕϮᑨ⫼ᯊ᠔Փ⫼ⱘ ORM ḚᶊϔḋˈᦤկϔϾ Java ᇍ䈵Ϣ᭄᥂ᑧⱘ㸼䖯ڣ⿄ϔ㟈ˈህད ೼ᴀ㋏㒳Ёˈ៥ӀՓ⫼ϔϾؐᇍ䈵ᴹ㸼⼎ϔϾ᭄᥂㸼ˈᑊϨ䆹ؐᇍ䈵ЁⱘሲᗻϢ᭄᥂㸼Ёⱘᄫ↉ৡ 10.3.4 ؐᇍ䈵෎㉏ ߎৃҹՓ⫼ getJDBCExecutor ᮍ⊩Ⳉ᥹ᕫࠄ JDBC ᠻ㸠ᇍ䈵ˈᑊᠻ㸠Ⳍᑨⱘ SQL 䇁হDŽ ಲˈᦤկњ䆹ᮍ⊩ৢˈ݊Ҫⱘ DAO ᅲ⦄㉏ህৃҹ㒻㓁䖭Ͼ෎㉏ˈᑊՓ⫼ getDatas ᮍ⊩ᇍ᭄᥂䖯㸠ᇕ㺙DŽ ⊼ᛣҹϞⱘ咥ԧҷⷕˈՓ⫼њ᭄᥂䕀ᤶᎹ݋㉏䖯㸠᭄᥂ᇕ㺙ˈᇚ ResultSet ᇍ䈵ᇕ㺙៤ϔϾ䲚ড়䖨 } } return DataUtil.getDatas(result, rs, clazz); //ᇍ ResultSet 䖯㸠ᇕ㺙ᑊ䖨ಲ䲚ড় ResultSet rs = getJDBCExecutor().executeQuery(sql); //ᠻ㸠 SQL 䖨ಲ ResultSet ᇍ䈵 Class clazz) { public Collection getDatas(String sql, Collection result, /Download at http://www.pin5i.com } List findRoles(); */ * @return * ᶹᡒܼ䚼ⱘ㾦㡆 /** Role find(String id); */ * @return * @param id * ḍ᥂ ID ᶹᡒ㾦㡆 /** public interface RoleDao { ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\dao\RoleDao.java˖ 㓪ݭ SQL 䇁হ䅽 JDBC ᠻ㸠㉏ᠻ㸠ˈᕫࠄⳌᑨⱘ᭄᥂ৢݡ䖨ಲDŽҹϟᰃ㾦㡆 DAO ᥹ষᅮНⱘᮍ⊩DŽ ᮄᓎ㾦㡆 DAO Ϣ⫼᠋ DAO ᥹ষˈᑊЎ݊ᦤկᅲ⦄㉏ˈDAO ሖ㒘ӊЏ㽕䋳䋷Ϣ᭄᥂ᑧ䖯㸠ѸѦˈ 10.4.2 ⫼᠋ⱏᔩ ) CASCADE CONSTRAINT `t_user_ibfk_1` FOREIGN KEY (`ROLE_ID`) REFERENCES `t_role` (`ID`) ON DELETE KEY `ROLE_ID` (`ROLE_ID`), PRIMARY KEY (`ID`), `PASS_WD` varchar(255) NOT NULL, `IS_DELETE` varchar(255) NOT NULL default '0', `REAL_NAME` varchar(255) default NULL, `ROLE_ID` int(10) NOT NULL, `USER_NAME` varchar(255) NOT NULL, `ID` int(10) NOT NULL auto_increment, CREATE TABLE `t_user` ( ) PRIMARY KEY (`ID`) `ID` int(10) NOT NULL auto_increment, `ROLE_NAME` varchar(255) NOT NULL, CREATE TABLE `t_role` ( ᯢˈ᠔᳝ⱘ᭄᥂㸼 ID ഛЎ㞾ࡼ๲䭓˄auto_increment˅DŽ߯ᓎ㾦㡆㸼Ϣ⫼᠋㸼ⱘ SQL 䇁হབϟ˖ 䆒䅵њ⫼᠋ⱘϸϾᇍ䈵ৢˈህৃҹḍ᥂䖭ϸϾᇍ䈵ˈ߯ᓎⳌᑨⱘ᭄᥂ᑧ㸼ˈ೼ᴀゴЁˈབ᮴⡍߿䇈 } private String IS_DELETE; //ᰃ৺㹿ߴ䰸, 0 ≵᳝㹿ߴ䰸, 1 ЎᏆ㒣ߴ䰸 private String REAL_NAME; //⫼᠋ⳳᅲৡ⿄ private String ROLE_ID; //⫼᠋㾦㡆 id, ᭄᥂ᑧᄫ↉ private String PASS_WD; //ᆚⷕ private String USER_NAME; ৡ⿄᠋⫼// /Download at http://www.pin5i.com UserService ЁᦤկϔϾ login ⱘᮍ⊩ˈ⫼ᴹ䖯㸠⫼᠋ⱏᔩˈབᵰ⫼᠋ৃҹ៤ࡳⱏᔩˈ߭䆒㕂ࠄ ೼ UserDao ЁᮄᓎϔϾ findUser ᮍ⊩ˈ⫼Ѣḍ᥂⫼᠋ৡ੠ᆚⷕᶹᡒ݋ԧⱘᶤϾ⫼᠋ˈᑊ೼ ApplicationContext.userService.method; ህৃҹᕫࠄᶤϾ㒘ӊⱘᅲ՟DŽ ApplicationContext 䋳䋷߯ᓎϢ㓈ᡸ䖭ѯ DAO 㒘ӊϢϮࡵ䘏䕥㒘ӊˈՓ⫼㗙া䳔㽕Փ⫼ҹϟҷⷕˈ } } userService = new UserServiceImpl(userDao, roleDao); userDao = new UserDaoImpl(); roleService = new RoleServiceImpl(roleDao); roleDao = new RoleDaoImpl(); logDao = new LogDaoImpl(); static { public static UserService userService; //⫼᠋Ϯࡵ䘏䕥᥹ষ public static RoleService roleService; //㾦㡆Ϯࡵ䘏䕥᥹ষ public static RoleDao roleDao; //㾦㡆 DAO ᥹ষ public static UserDao userDao; //⫼᠋ DAO ᥹ষ public static User loginUser; //ⱏᔩⱘ⫼᠋ public class ApplicationContext { ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\util\ApplicationContext.java˖ ᮍˈ䛑ৃҹ䕏ᯧⱘᕫࠄ䖭ѯ㒘ӊⱘᅲ՟DŽ կϔϾ ApplicationContext ⱘᇍ䈵ˈᴹֱᄬ䖭ѯ DAO ៪㗙Ϯࡵ䘏䕥㒘ӊⱘᅲ՟ˈ䙷М೼㋏㒳ⱘӏԩഄ Ϯࡵ䘏䕥ሖᰃ䅽㾚೒ሖ䇗⫼ⱘˈ಴ℸ៥Ӏ䖬䳔㽕݇ᖗ䖭ѯϮࡵ䘏䕥ᇍ䈵ⱘ߯ᓎˈ៥ӀৃҹЎ㋏㒳ᦤ ᰃབԩᅲ⦄ⱘˈϔᮺϮࡵ៪㗙 DAO ሖথ⫳ব࣪ˈгϡᖙএׂᬍ䇗⫼㗙ⱘҷⷕDŽ ⱘ䆱ˈ䖭ѯ㒘ӊⱘ䇗⫼㗙ˈা䳔㽕Ⳉ᥹Փ⫼᥹ষᴹ䇗⫼ᮍ⊩ˈᑊϡ䳔㽕এ݇ᖗ䖭ѯ㒘ӊخկ᥹ষˈ䖭ḋ ḋˈ䳔㽕㓪ݭϔϾ⫼᠋ⱘϮࡵ䘏䕥᥹ষˈݡЎ݊ᦤկᅲ⦄DŽ೼ DAO ሖϢϮࡵ䘏䕥ሖЁˈ៥Ӏ䛑䳔㽕ᦤ UserServiceImplˈ㋏㒳ЁⱘⳌ݇Ϯࡵ䘏䕥ҷⷕ䛑ᇚ㹿ᬒ㕂ࠄϮࡵ䘏䕥ሖ䖯㸠໘⧚ˈϢ DAO ሖⱘᅲ⦄ϔ ᮄᓎ⫼᠋ DAO ᥹ষϢ݊ᅲ⦄㉏ˈᑊᮄᓎ⫼᠋Ϯࡵ䘏䕥᥹ষ UserService Ϣ݊ᅲ⦄㉏ ˄getDatas Ϣ getJDBCExecutor˅ˈҹϞҷⷕা䋳䋷⫼ѢᕫࠄⳌ݇ⱘ㾦㡆ᇍ䈵DŽ ೼ҹϞҷⷕЁˈRoleDaoImpl 㒻ᡓѢ DAO ෎㉏˖BaseDaoImplˈ䙷Мህৃҹ䇗⫼෎㉏ⱘϸϾᮍ⊩ } } return (List)getDatas(sql, new ArrayList(), Role.class); String sql = "select * from T_ROLE"; public List findRoles() { } return result.size() == 1 ? result.get(0) : null; List result = (List)getDatas(sql, new ArrayList(), Role.class); String sql = "select * from T_ROLE ro where ro.id = '" + id + "'"; public Role find(String id) { public class RoleDaoImpl extends BaseDaoImpl implements RoleDao { ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\dao\impl\RoleDaoImpl.java˖ 㾦㡆 DAO ᥹ষᦤկњḍ᥂ ID ᶹᡒ㾦㡆Ϣᶹᡒܼ䚼㾦㡆ⱘࡳ㛑ˈҹϟᰃ䆹᥹ষᅲ⦄㉏ⱘ݋ԧᅲ⦄DŽ /Download at http://www.pin5i.com ৃҹ㹿䘏䕥ߴ䰸ⱘˈ಴ℸ㓪ݭ SQL 䇁হⱘᯊ׭ˈ䳔㽕ࡴܹ䘏䕥ߴ䰸ⱘ߸ᅮᴵӊDŽ ೼ᅲ⦄ findUsers ᮍ⊩ⱘᯊ׭ˈ䳔㽕⊼ᛣⱘᰃˈᶹ䆶ⱘᯊ׭䳔㽕ࡴܹᶹ䆶ᴵӊˈ೼ᴀゴЁˈ⫼᠋ᰃ List findUsers(); */ * @return * ᶹᡒܼ䚼ⱘ⫼᠋ /** ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\dao\UserDao.java˖ ೼᥹ষ UserDao ЁᅮНϔϾ findUsers ᮍ⊩ˈ⫼Ѣ䖨ಲ㋏㒳Ёܼ䚼ⱘ⫼᠋DŽ 10.4.3 ᶹᡒܼ䚼⫼᠋ ҹϞⱘ咥ԧҷⷕЁˈ䇗⫼њ UserService ⱘ login ᮍ⊩䖯㸠ⱏᔩDŽ } } ViewUtil.showWarn(e.getMessage(), this); e.printStackTrace(); } catch (Exception e) { MainFrame mf = new MainFrame(); this.setVisible(false); ApplicationContext.userService.login(userName, passwd); try { //䖯㸠ⱏᔩ String passwd = getPassword(); //ᕫࠄᆚⷕ String userName = this.userName.getText(); //ᕫࠄ⫼᠋ৡ private void login() { //⚍ߏ⹂ᅮᣝ䪂㾺থⱘᮍ⊩ ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\ui\LoginFrame.java˖ ᰃⱏᔩ⬠䴶 LoginFrame ⱘ䚼ߚᅲ⦄DŽ ೼ⱏᔩ⬠䴶ЁˈህৃҹⳈ᥹䇗⫼ UserService ⱘ login ᮍ⊩䖯㸠⫼᠋ⱏᔩˈᑊ䖯㸠ᓖᐌᤩᤝDŽҹϟ } ApplicationContext.loginUser = user; user.setRole(role); Role role = this.roleDao.find(user.getROLE_ID()); if (user == null) throw new BusinessException("⫼᠋ৡᆚⷕ䫭䇃"); //≵᳝ᡒࠄ⫼᠋, ᡯߎᓖᐌ User user = this.userDao.findUser(userName, passwd); public void login(String userName, String passwd) { ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\service\impl\UserServiceImpl.java˖ } return users.size() == 1 ? users.get(0) : null; List users = (List)getDatas(sql, new ArrayList(), User.class); userName + "' and u.PASS_WD = '" + passwd + "' and u.IS_DELETE = '0'"; String sql = "select * from T_USER u where u.USER_NAME = '" + public User findUser(String userName, String passwd) { ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\dao\impl\UserDaoImpl.java˖ ApplicationContext Ёⱘ loginUserˈ㸼⼎ℷ೼Փ⫼㋏㒳ⱘ⫼᠋ˈབᵰ≵᳝ⱏᔩ៤ࡳˈ߭ᡯߎϮࡵᓖᐌDŽ /Download at http://www.pin5i.com } else if (USER_NAME.equals(columnName)) { return user.getID(); if (USER_ID.equals(columnName)) { User user = this.datas.get(row); if (this.datas != null) { String columnName = this.getColumnName(column); public Object getValueAt(int row, int column) { //␆ᶧ↣ϔ߫ⱘ᭄᥂ } this.datas = datas; public void setDatas(List datas) { } return columnNames.length; public int getColumnCount() { } return columnNames[col]; public String getColumnName(int col) { } return 0; } return this.datas.size(); if (this.datas != null) { public int getRowCount() { //䖨ಲ᭄᥂㸠᭄ private List datas; //㸼⼎ᴀ߫㸼ⱘ᭄᥂䲚ড় ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\ui\table\UserTableModel.java˖ ߫㸼䖯㸠␆ᶧˈা䳔㽕ᦤկϔϾ TableModel ेৃˈҹϟᰃ⫼᠋߫㸼ⱘ TableModel ⱘ䚼ߚᅲ⦄DŽ ৃҹ䇗⫼ getUsers ᮍ⊩ᕫࠄܼ䚼ⱘ㋏㒳⫼᠋DŽᴀゴⱘ⫼᠋߫㸼ᰃϔϾ JTable ⱘᄤ㉏ˈབᵰ䳔㽕ᇍ⫼᠋ Ⳍᑨⱘ Role ᇍ䈵˄ࠄ᭄᥂ᑧЁᶹᡒ˅DŽ೼㾚೒ሖЁˈা䳔㽕䗮䖛 ApplicationContext ⱘ userService ህ Role ሲᗻˈᔧᶹߎ User ᇍ䈵ⱘᯊ׭ˈ䆹ሲᗻЎ nullˈ಴ℸ៥Ӏ䳔㽕ḍ᥂ User ⱘ ROLE_ID ሲᗻএ㦋প ҹϞҷⷕⱘ咥ԧ䚼ߚˈՓ⫼ UserDao Ϣ RoleDao ᴹ㦋প᭄᥂ˈ䳔㽕⊼ᛣⱘᰃˈUser ᇍ䈵Ё᳝ϔϾ } return users; } u.setRole(role); Role role = this.roleDao.find(u.getROLE_ID()); for (User u : users) { List users = this.userDao.findUsers(); public List getUsers() { ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\service\impl\UserServiceImpl.java˖ ⦄DŽ ೼ UserService ЁᅮН getUsers ᮍ⊩ˈ⫼Ѣ䖨ಲܼ䚼ⱘ㋏㒳⫼᠋DŽҹϟᰃ getUsers ᮍ⊩ⱘ݋ԧᅲ } return users; List users = (List)getDatas(sql, new ArrayList(), User.class); String sql = "select * from T_USER u where u.IS_DELETE = '0'"; public List findUsers() { ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\dao\impl\UserDaoImpl.java˖ /Download at http://www.pin5i.com ೒ 10.10 ᶹᡒܼ䚼㾦㡆  ᠔⼎DŽ ҹϞҷⷕⱘ咥ԧ䚼ߚˈ䇗⫼ RoleService ⱘ getRoles ᮍ⊩ᕫࠄܼ䚼ⱘ㾦㡆ˈ݋ԧⱘᬜᵰབ೒ 10.10 } } this.roleSelect.addItem(r); for (Role r : roles) { List roles = ApplicationContext.roleService.getRoles(); this.roleSelect.removeAllItems(); private void createRoleSelect() { //߯ᓎ㾦㡆ϟᢝ ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\ui\dialog\AddUserDialog.java˖ ϟᢝḚⱘҷⷕDŽ կϔϾ getRoles ᮍ⊩ˈ⫼Ѣ䖨ಲܼ䚼ⱘ㾦㡆ˈ೼㾚೒ሖЁˈা䳔㽕䇗⫼䆹ᮍ⊩ेৃDŽҹϟᰃ߯ᓎ㾦㡆 ᮄ๲⫼᠋ˈ䰸䳔㽕⫼᠋ৡǃᆚⷕ੠⫼᠋ⳳᅲྦྷৡ໪ˈ䖬䳔㽕䗝ᢽ⫼᠋ⱘ㾦㡆DŽ೼ RoleService Ёᦤ 10.4.4 ᮄ๲⫼᠋ࡳ㛑 䖭䞠䳔㽕⊼ᛣⱘᰃˈ᮴䆎⫼᠋ⱏᔩ䖬ᰃᶹᡒܼ䚼⫼᠋ˈ䛑䳔㽕ࡴܹ䴲䘏䕥ߴ䰸ⱘᴵӊ߸ᮁDŽ this.dataTable.updateUI(); this.tableModel.setDatas(users); List users = ApplicationContext.userService.getUsers(); ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\ui\UserPanel.java˖ TableModel ᇍ䈵ˈᇚ⫼᠋䲚ড়䆒㕂ࠄ TableModel ЁेৃDŽ ೼⫼᠋ㅵ⧚⬠䴶Ёা䳔㽕䇗⫼ UserService ⱘ getUsers ᮍ⊩ህৃҹᕫࠄܼ䚼ⱘ㋏㒳⫼᠋ˈݡᕫࠄ } return super.getValueAt(row, column); } } return user.getRole().getROLE_NAME(); } else if (ROLE.equals(columnName)) { return user.getREAL_NAME(); } else if (REAL_NAME.equals(columnName)) { return user.getUSER_NAME(); /Download at http://www.pin5i.com + realName + "%' and u.IS_DELETE = '0'"; String sql = "select * from T_USER u where u.REAL_NAME like '%" public List query(String realName) { ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\dao\impl\UserDaoImpl.java˖ 䬂ᄫ䖯㸠῵㊞ᶹ䆶DŽҹϟᰃ UserDaoImpl ⱘᶹ䆶ᮍ⊩ⱘᅲ⦄DŽ ⱘֵᙃࠄ᭄᥂ᑧЁ䖯㸠῵㊞ᶹ䆶DŽ⬅Ѣ៥ӀՓ⫼ⱘᰃ MySQL ᭄᥂ᑧˈ಴ℸৃҹՓ⫼ MySQL ⱘ like ݇ ⫼᠋ㅵ⧚⬠䴶ЁˈᦤկњϔϾᶹ䆶⫼᠋ⱘࡳ㛑ˈ᪡԰Ҏ䕧ܹ⫼᠋ྦྷৡৢˈ㋏㒳ህӮḍ᥂⫼᠋᠔䕧ܹ 10.4.6 ᶹ䆶⫼᠋ ㅔऩⱘׂᬍ⫼᠋㸼ⱘ IS_DELETE ᄫ↉ؐˈݡՓ⫼ JDBC ᠻ㸠㉏ᴹᠻ㸠䆹হ SQL ेৃDŽ } getJDBCExecutor().executeUpdate(sql.toString()); .append(" where u.ID = '" + id + "'"); sql.append(" set u.IS_DELETE = '1'") StringBuffer sql = new StringBuffer("update T_USER u"); public void delete(String id) { ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\dao\impl\UserDaoImpl.java˖ 䴶ेৃDŽׂᬍ᭄᥂Փ⫼ SQL ⱘ update 䇁হDŽҹϟᰃ UserDaoImpl ⱘᅲ⦄DŽ ᥂ˈ៥ӀাЎ݊ᦤկ䘏䕥ߴ䰸ˈेᇚ⫼᠋㸼ⱘ IS_DELETE 䆒㕂Ў 1ˈ䅽䆹᭄᥂ϡӮߎ⦄೼⫼᠋ㅵ⧚⬠ ೼ᴀゴЁˈ⬅Ѣ⫼᠋᳝ৃ㛑ϢᶤᴵϮࡵ᭄᥂㒥ᅮ˄џࡵ˅ˈ಴ℸϡৃҹ䱣֓ߴ䰸ˈབᵰ䳔㽕ߴ䰸᭄ 10.4.5 ߴ䰸⫼᠋ ๲DŽ ࡴܹϔϾḍ᥂⫼᠋ᶹᡒ⫼᠋ᇍ䈵ⱘᮍ⊩ˈབᵰ㛑ᡒࠄⳌᑨⱘ⫼᠋ˈ߭㸼⼎䆹⫼᠋ৡᏆ㒣ᄬ೼ˈϡৃҹᮄ ೼ᅲ⦄ᮄ๲⫼᠋ⱘϮࡵ䘏䕥ᯊˈབᵰᄬ೼Ⳍৠ⫼᠋ৡⱘ⫼᠋᭄᥂ˈ߭䳔㽕ᡯߎᓖᐌˈৃҹЎ UserDao } this.userDao.save(user); if (u != null) throw new BusinessException("䆹⫼᠋ৡᏆ㒣ᄬ೼"); User u = this.userDao.findUser(user.getUSER_NAME()); //ḍ᥂ᮄⱘ⫼᠋ৡএᶹᡒ, ߸ᮁᰃ৺ᄬ೼Ⳍৠ⫼᠋ৡⱘ⫼᠋ public void addUser(User user) { ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\service\impl\UserServiceImpl.java˖ } getJDBCExecutor().executeUpdate(sql.toString()); .append(user.getPASS_WD() + "')"); .append("0', '") .append(user.getREAL_NAME() + "', '") .append(user.getROLE_ID() + "', '") sql.append(user.getUSER_NAME() + "', '") StringBuffer sql = new StringBuffer("insert into T_USER VALUES (ID, '"); public void save(User user) { ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\dao\impl\UserDaoImpl.java˖ হDŽ ⱘ⫼᠋䖯㸠ֱᄬˈᑊ೼ UserDao ЁᦤկϔϾֱᄬ⫼᠋ⱘᮍ⊩ˈ䇗⫼ JDBC ᠻ㸠㉏ᴹᠻ㸠ᦦܹⱘ SQL 䇁 㾦㡆ৡ⿄DŽҢ⬠䴶Ёᕫࠄ⫼᠋ⱘ৘Ͼֵᙃৢˈህৃҹ೼ UserService ЁᅮНϔϾ addUser ᮍ⊩ˈᇍᮄ๲ ㋴ˈ䳔㽕䞡ݭ Role ⱘ toString ᮍ⊩ˈ䖨ಲܗ೼䖭䞠䳔㽕⊼ᛣⱘᰃˈབᵰᇚ Role ᇍ䈵԰ЎϟᢝḚⱘ /Download at http://www.pin5i.com 䛑䳔㽕ֱᄬࠄ᭄᥂ᑧЁˈḍ᥂៥Ӏ᠔䆒䅵ⱘ䖭ѯሲᗻˈ߯ᓎџࡵⳌᑨⱘ㸼DŽ Ҏⱘ䯂乬ˈЎњҹৢⱘৃᠽሩᗻˈ៥Ӏৃҹᓎゟ໮ϔϾ݇㘨ᴹ㸼⼎䖭⾡݇㋏DŽҹϞⱘ Transaction ሲᗻ ᣛ⦄೼ℷ೼ᠻ㸠џࡵⱘ⫼᠋DŽ⬅Ѣџࡵৃҹ೼⫼᠋П䯈䖯㸠䕀থˈ಴ℸህᄬ೼ᔧࠡ໘⧚ҎϢࠡϔϾ໘⧚ 䰸њџࡵⱘ᳔෎ᴀⱘ޴Ͼሲᗻ໪ˈ䳔㽕⊼ᛣⱘᰃᔧࠡ໘⧚Ҏ ID ϢϞϔϾ໘⧚Ҏ IDˈᔧࠡ໘⧚Ҏᰃ } private String IS_HURRY; //ᰃ৺䳔㽕㋻ᗹ໘⧚, 0 Ўϡ⫼, 1 䳔㽕㋻ᗹ໘⧚ private String TS_STATE; //џࡵ⢊ᗕ private String PRE_HANDLER_ID; //ϞϔϾ໘⧚Ҏ ID private String HANDLER_ID; //ᔧࠡ໘⧚Ҏ ID private String INITIATOR_ID; //থ䍋Ҏ ID private String TS_CREATEDATE; //ᓔྟ᮹ᳳ(߯ᓎ᮹ᳳ) private String TS_FACTDATE; //ᅲ䰙ᅠ៤᮹ᳳ private String TS_TARGETDATE; //Ⳃᷛᅠ៤᮹ᳳ private String TS_CONTENT; //џࡵݙᆍ private String TS_TITLE; //џࡵᷛ乬 public class Transaction extends ValueObject { ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\model\Transaction.java˖ Transaction ᇍ䈵ⱘሲᗻབϟDŽ ߯ᓎᯊ䯈ǃџࡵ⢊ᗕǃᰃ৺䳔㽕㋻ᗹ໘⧚ㄝֵᙃDŽ߯ᓎϔϾ Transaction ⱘ㉏ˈ㒻ᡓ ValueObjectˈ ᳝ᷛ乬ǃݙᆍǃᅠ៤ᯊ䯈ǃ໘⧚Ҏ੠џࡵথ䍋Ҏˈ䰸њ䖭ѯֵᙃ໪ˈџࡵ䖬䳔㽕ֱᄬџࡵⱘᅠ៤ᯊ䯈ǃ ⱘᯊ׭ˈ៥Ӏгৃҹ䞛⫼䖭ḋⱘᮍᓣ䖯㸠䆒䅵DŽҢ 10.2.6 ⱘᮄᓎџࡵ⬠䴶ЁৃҹⳟߎˈϔϾџࡵࣙ৿ⱘ ೼ 10.4.1 Ёˈ៥ӀᏆ㒣䆒䅵њ㾦㡆Ϣ⫼᠋ᇍ䈵ˈᑊḍ᥂䖭ѯᇍ䈵߯ᓎ㸼㒧ᵘˈ䙷М೼䆒䅵џࡵᇍ䈵 10.5.1 䆒䅵џࡵᇍ䈵 ᯊˈৃҹᇚ䆹џࡵ㕂Ў᮴ᬜˈ䖭ḋџࡵ໘⧚ҎህϡᖙЎ䆹џࡵ԰ߎ≵᳝ᖙ㽕ⱘ໘⧚DŽ ࡲࡳ㛑ˈ䅽䋳䋷䆹џࡵⱘਬᎹⳟࠄˈ䆹џࡵ䳔㽕㋻ᗹ໘⧚DŽᔧㅵ⧚㗙থ⦄ᮄᓎњϔѯ≵᳝ᛣНⱘџࡵڀ ሲਬᎹЁˈ䖬ৃҹᶹⳟ㞾Ꮕথ䍋ⱘџࡵ⢊ᗕϢ⌕⿟ˈབᵰথ⦄㋏㒳Ё᳝ѯџࡵᓊৢˈৃҹՓ⫼῵ഫЁⱘ џࡵㅵ⧚῵ഫᰃ⬅ӕϮⱘㅵ⧚Ҏਬ䖯㸠᪡԰ⱘˈ೼䆹῵ഫЁˈৃҹᮄᓎџࡵᑊᇚџࡵߚথࠄᶤϾϟ 10.5 џࡵㅵ⧚ ᥂ᯊˈህৃҹՓ⫼Ālike %java%ā䖯㸠ᶹ䆶DŽ ҹϞҷⷕⱘ咥ԧ䚼ߚˈՓ⫼њ like ݇䬂ᄫ䖯㸠ᶹ䆶ˈ՟བ㽕ᶹ䆶⫼᠋ྦྷৡ৿᳝ĀjavaāᄫヺІⱘ᭄ } return users; List users = (List)getDatas(sql, new ArrayList(), User.class); /Download at http://www.pin5i.com String columnName = this.getColumnName(column); public Object getValueAt(int row, int column) { } return columnNames.length; public int getColumnCount() { } return columnNames[col]; public String getColumnName(int col) { } return 0; } return this.datas.size(); if (this.datas != null) { public int getRowCount() { } return this.datas; public List getDatas() { } this.datas = datas; public void setDatas(List datas) { ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\ui\table\TransactionTableModel.java˖ կϔϾ TableModelDŽҹϟᰃџࡵ߫㸼ⱘ䚼ߚᅲ⦄DŽ Ң᭄᥂ᑧЁᶹ䆶ࠄ᭄᥂ৢˈህ䳔㽕ᇚ᭄᥂ሩ⼎ࠄ⬠䴶ˈџࡵ߫㸼Ϣ⫼᠋߫㸼ⱘᅲ⦄ϔ㟈ˈ䛑䳔㽕ᦤ ᗹ໘⧚ⱘ᭄᥂ᇚӮ㹿ᬒࠄࠡ䴶DŽ џࡵᶹ䆶ⱘᯊ׭ˈ䆹ᮍ⊩гৃҹ䞡⫼DŽҹϞҷⷕⱘ咥ԧ䚼ߚˈ䖬䳔㽕ᇍᶹߎᴹⱘ᭄᥂䖯㸠ᥦᑣˈ䳔㽕㋻ TransactionDao ᦤկⱘᶹ䆶ᮍ⊩᳝ϸϾখ᭄ˈϔϾᰃџࡵⱘ⢊ᗕˈ঺໪ϔϾᰃ⫼᠋ⱘ IDˈᔧ⫼᠋䖯㸠 ⬅Ѣџࡵ㸼Ёᄬ೼᳝থ䍋Ҏⱘ໪䬂ˈ಴ℸৃҹⳈ᥹㓪ݭ SQL 䖯㸠ᶹ䆶ˈ䳔㽕⊼ᛣⱘᰃˈ } return result; new ArrayList(), Transaction.class); List result = (List)getDatas(sql.toString(), .append(state + "' order by ts.IS_HURRY desc"); .append(" and ts.TS_STATE = '") .append(userId + "'") sql.append(" where ts.INITIATOR_ID = '") StringBuffer sql = new StringBuffer("select * from T_TRANSACTION ts"); public List findInitiatorTransactions(String state, String userId) { ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\dao\impl\TransactionDaoImpl.java˖ Ў TransactionDao ࡴܹᶹ䆶ᮍ⊩ˈ䆹ᮍ⊩ⱘ݋ԧᅲ⦄བϟDŽ ᶹᡒDŽ њϔϾ INITIATOR_ID ⱘሲᗻˈ㸼⼎থ䍋Ҏⱘ IDˈ䙷М៥Ӏህৃҹḍ᥂ⱏᔩ⫼᠋ⱘ ID এ᭄᥂ᑧЁ䖯㸠 ᑊ䖯ܹџࡵㅵ⧚ᯊˈህ䳔㽕ࠄ᭄᥂ᑧЁᶹᡒ䆹⫼᠋᠔থ䍋ⱘџࡵDŽ೼䆒䅵џࡵᇍ䈵ⱘᯊ׭ˈህᏆ㒣ᦤկ ೼џࡵㅵ⧚Ёˈџࡵⱘㅵ⧚㗙ৃҹⳟࠄ㞾Ꮕথ䍋ⱘџࡵᑊᇍ݊䖯㸠䎳䏾ˈᔧㅵ⧚㗙ⱏᔩࠄ㋏㒳Ёˈ 10.5.2 ḍ᥂থ䍋Ҏᶹᡒџࡵ ˄՟བ䕀থ˅ˈ಴ℸϡ䳔㽕䆒䅵݊ҪⳌ݇ⱘᇍ䈵DŽ г䳔㽕Ў䖭ϸϾ᥹ষ⏏ࡴᅲ⦄㉏DŽ೼ᴀᇣ㡖Ёˈ⬅Ѣাᰃㅵ⧚㗙䖯㸠џࡵㅵ⧚ˈᑊϡ䳔㽕ᅲ⦄݊Ҫࡳ㛑 ৠḋഄˈгЎџࡵᇍ䈵߯ᓎᅗⱘ DAO ᇍ䈵ϢϮࡵ䘏䕥ᇍ䈵˖TransactionDaoǃTransactionServiceDŽ /Download at http://www.pin5i.com џࡵ߫㸼ⱘ݋ԧᬜᵰབ೒ 10.11 ᠔⼎DŽ } this.dataTable.updateUI(); this.tableModel.setDatas(datas); List datas = getDatas(); public void readData() { //џࡵㅵ⧚⬠䴶䇏প᭄᥂, ᅲ⦄⠊㉏ⱘᢑ䈵ᮍ⊩ } return datas; .getInitiatorTransaction(loginUser, currentState); List datas = ApplicationContext.transactionService User loginUser = ApplicationContext.loginUser; private List getDatas() { //Ң᭄᥂ᑧЁ䇏প᭄᥂ ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\ui\TransactionManagePanel.java˖ 䇗⫼ JTable ⱘ updateUI ᮍ⊩ህৃҹ᳈ᮄ⬠䴶ⱘ᭄᥂DŽ ೼џࡵ߫㸼ⱘ TableModel Ё㓈ᡸϔϾ Listⱘ䲚ড়ˈা䳔㽕೼໪⬠䆒㕂䖭Ͼ䲚ড়ˈݡ } return super.getValueAt(row, column); } } return (t.getTS_FACTDATE() == null) ? "" : t.getTS_FACTDATE(); } else if (TS_FACTDATE.equals(columnName)) { return t.getInitiator().getREAL_NAME(); } else if (INITIATOR.equals(columnName)) { return t.getHandler().getREAL_NAME(); } else if (CURRENT_HANDLER.equals(columnName)) { return t.getTS_CREATEDATE(); } else if (TS_CREATEDATE.equals(columnName)) { return t.getTS_TARGETDATE(); } else if (TS_TARGETDATE.equals(columnName)) { return t.getTS_CONTENT(); } else if (TS_CONTENT.equals(columnName)) { return t.getTS_TITLE(); } else if (TS_TITLE.equals(columnName)) { return getStateImage(t.getTS_STATE()); } else if (TS_STATE.equals(columnName)) { return t.getID(); if (TS_ID.equals(columnName)) { Transaction t = this.datas.get(row); if (this.datas != null) { /Download at http://www.pin5i.com ҷⷕ⏙ऩ˖ ᓎњϔϾ UserSelectHandler ⱘ᥹ষˈᔧᮄᓎџࡵ䗝ᢽ໘⧚Ҏᯊˈ䳔㽕ᅲ⦄䖭Ͼ᥹ষDŽ ೼⬠䴶Ё䕧ܹњџࡵⱘⳌֵ݇ᙃৢˈ䖬䳔㽕䗝ᢽџࡵ໘⧚Ҏˈ೼ 10.2.7 Ё߯ᓎњ⫼᠋䗝ᢽ⬠䴶ˈ߯ ᯊ׭ˈTransaction ⱘ IS_HURRY˄ᰃ৺㋻ᗹ˅ⱘᷛ䆚ЎĀ0āDŽ ҹϞҷⷕᣐ㺙њϔহ insert ⱘ SQL 䇁হˈݡ䗮䖛 JDBC ᠻ㸠㉏ᴹᠻ㸠ˈ䳔㽕⊼ᛣⱘᰃᮄᓎџࡵⱘ } getJDBCExecutor().executeUpdate(sql.toString()); .append(t.getTS_STATE() + "', '0')"); .append(t.getPRE_HANDLER_ID() + ", '") .append(t.getHANDLER_ID() + ", ") .append(t.getINITIATOR_ID() + ", ") .append(t.getTS_CREATEDATE() + "', ") .append(t.getTS_FACTDATE() + "', '") .append(t.getTS_TARGETDATE() + "', '") .append(t.getTS_CONTENT() + "', '") sql.append(t.getTS_TITLE() + "', '") StringBuffer sql = new StringBuffer("insert into T_TRANSACTION values(ID, '"); public void save(Transaction t) { ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\dao\impl\TransactionDaoImpl.java˖ Ў TransactionDao ⏏ࡴᮍ⊩ˈҹϟᰃֱᄬџࡵᮍ⊩ⱘᅲ⦄DŽ 㸠ᮄᓎ᪡԰ⱘҎ˄ϡৃᬍব˅DŽ insert 䇁হݭࠄ᭄᥂ᑧЁDŽ೼ᮄᓎџࡵⱘᯊ׭ˈ䳔㽕⊼ᛣⱘᰃˈџࡵⱘ߱ྟ⢊ᗕᰃ䖯㸠Ёˈথ䍋Ҏᰃ䖯 ᑊϨ೼ 10.5.1 Ё䆒䅵њџࡵᇍ䈵ˈ಴ℸ៥Ӏা䳔㽕Ң⬠䴶Ёᕫࠄ⫼᠋䕧ܹⱘџࡵֵᙃˈݡ䗮䖛 SQL ⱘ ϔ⃵᪡԰ˈ䛑䳔㽕䆄ᔩ䍋ᴹˈ䅽џࡵㅵ⧚㗙䖯㸠ᶹⳟϢ䎳䏾DŽ೼ 10.2.6 ЁᏆ㒣߯ᓎњᮄᓎџࡵⱘ⬠䴶ˈ ᮄᓎџࡵᛣੇⴔϔ⃵џࡵⱘᓔྟˈџࡵথ䍋ҎҢ䖭ᯊ䍋ˈህᓔྟᇍџࡵ䖯㸠ⲥ᥻ˈਬᎹᇍџࡵⱘ↣ 10.5.3 ᮄᓎџࡵ ೒ 10.11 џࡵ߫㸼  /Download at http://www.pin5i.com } else { c.setToolTipText(icon.getDescription()); c.setIcon(icon); ImageIcon icon = (ImageIcon)value; if (value instanceof ImageIcon) { //䆒㕂೒⠛ c.setText(""); isSelected, hasFocus, row, column); JLabel c = (JLabel)super.getTableCellRendererComponent(table, value, boolean isSelected, boolean hasFocus, int row, int column) { public Component getTableCellRendererComponent(JTable table, Object value, code\transaction\src\org\crazyit\transaction\ui\table\TransactionTableCellRenderer.java˖ ҷⷕ⏙ऩ˖ Ḑⱘḋᓣ䖯㸠␆ᶧDŽܗ೼⬠䴶ሖЎ JTable ⏏ࡴϔϾ TableCellRenderer ᴹᇍ߫㸼ऩ 䖭ḋⱘ䆱ˈ䳔㽕㋻ᗹ໘⧚ⱘџࡵህӮ㹿ᬒࠄџࡵ߫㸼ⱘ᳔ࠡ䴶ˈԚᰃҡ✊ϡ໳䝦Ⳃˈ಴ℸ៥Ӏৃҹ } getJDBCExecutor().executeUpdate(sql.toString()); "where ts.ID = '" + id + "'"; String sql = "update T_TRANSACTION ts set ts.IS_HURRY = '1' " + public void hurry(String id) { ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\dao\impl\TransactionDaoImpl.java˖ ࡵা䳔㽕ᬍবϔϟ䖭ϾؐेৃDŽ ࡲџڀIS_HURRY ؐˈ೼ḍ᥂থ䍋Ҏᶹ䆶џࡵⱘᯊ׭ˈџࡵᰃḍ᥂ IS_HURRY ሲᗻ䖯㸠ᥦᑣⱘˈ಴ℸ ࡲџࡵⱘࡳ㛑ˈᬍবџࡵᇍ䈵ⱘڀ⫼џࡵㅵ⧚㗙থ⦄џࡵ䖯ᑺ㨑ৢ៪㗙䳔㽕㋻ᗹ໘⧚ˈህৃҹՓ ࡲџࡵڀ 10.5.4 NewTransactionUserSelectHandler ⱘ confirm ᮍ⊩া䳔㽕ᬍব߯ᓎџࡵ⬠䴶ⱘϸϾ᭛ᴀḚⱘؐेৃDŽ 䙷М⫼᠋䗝ᢽᇍ䆱ḚЁՓ⫼ⱘ݊ᅲህᰃ NewTransactionUserSelectHandler ⱘ confirm ᮍ⊩DŽ this.userDialog = new SelectUserDialog(this.selectHandler); //߯ᓎ⫼᠋䗝ᢽᇍ䆱Ḛ this.selectHandler = new NewTransactionUserSelectHandler(this); ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\ui\dialog\NewTransactionDialog.java˖ 䴶ⱘᯊ׭ˈህՓ⫼ NewTransactionUserSelectHandler ԰Ўᵘ䗴খ᭄DŽ ҹϞⱘ NewTransactionUserSelectHandler ㉏ᅲ⦄њ UserSelectHandler ᥹ষˈ೼ᠧᓔ⫼᠋䗝ᢽ⬠ } } this.newDialog.getHandlerIdField().setText(userId); this.newDialog.getHandlerField().setText(realName); //ᬍবᮄ๲にষⱘ໘⧚Ҏ᭛ᴀḚⱘؐ public void confirm(String userId, String realName) { } this.newDialog = newDialog; public NewTransactionUserSelectHandler(NewTransactionDialog newDialog) { private NewTransactionDialog newDialog; //ᮄᓎџࡵⱘにষ public class NewTransactionUserSelectHandler implements UserSelectHandler { va˖ code\transaction\src\org\crazyit\transaction\ui\handler\impl\NewTransactionUserSelectHandler.ja /Download at http://www.pin5i.com TransactionDaoImpl˖ ৃҹ䖯㸠߸ᮁᑊᢦ㒱䖭ḋⱘ᪡԰DŽ 㽕ᇚџࡵ㕂Ў᮴ᬜˈ䖭ᰒ✊ᰃ≵᳝⧚⬅ⱘˈ಴ℸᔧ⫼᠋ᇍᏆ㒣ᅠ៤ⱘџࡵ䖯㸠㕂Ў᮴ᬜⱘ᪡԰ᯊˈ㋏㒳 ऩˈা䳔㽕ᇚџࡵⱘ⢊ᗕ䆒㕂ࠄ᮴ᬜⱘᷛ䆚ेৃˈԚ᳝ϔ⚍䳔㽕⊼ᛣⱘᰃˈᔧџࡵᏆ㒣ᅠ៤ᯊˈབᵰ䖬 ᮴ᬜˈ໘⧚Ҏⳟࠄ䆹џࡵᏆ㒣ᰃ᮴ᬜᯊˈህϡᖙݡ⌾䌍Ꮉ԰䞣೼䆹џࡵϞDŽᇚџࡵ㕂Ў᮴ᬜᅲ⦄䕗Ўㅔ ᔧџࡵㅵ⧚㗙থ⦄џࡵᏆ㒣༅এ݊ᛣНˈेՓᏆ㒣ᅠ៤г≵᳝ӏԩ԰⫼ˈ䙷Мℸᯊৃҹᇚџࡵ㕂Ў 10.5.5 ᇚџࡵ㕂Ў᮴ᬜ ㋻ᗹ໘⧚DŽ བ೒ 10.12 ᠔⼎ˈৃҹⳟࠄˈᥦ೼᳔ࠡ䴶ⱘџࡵᄫԧᏆ㒣Ў咥ԧˈৃҹ᳈ࡴ䝦Ⳃⱘ㸼⦄䆹џࡵ䳔㽕 ࡲџࡵڀ ೒ 10.12  ᄫԧবЎ㉫ԧˈ䖭ḋৃҹ᳈ࡴ䝦Ⳃⱘਞ䆝໘⧚Ҏˈ䖭Ͼџࡵ䳔㽕㋻ᗹ໘⧚DŽ݋ԧⱘᬜᵰབ೒ 10.12 ᠔⼎DŽ ҹϞҷⷕⱘ咥ԧ䚼ߚˈᕫࠄⳌᑨⱘ᭄᥂ˈ߸ᮁᰃ৺䳔㽕䖯㸠㋻ᗹ໘⧚ˈབᵰ䳔㽕ⱘ䆱ˈህᇚ䆹㸠ⱘ } return c; } } c.setFont(hurryFont); if (datas.get(row).getIS_HURRY().equals("1")) { //߸ᮁᰃ৺䳔㽕㋻ᗹ໘⧚ if (datas != null) { List datas = model.getDatas(); TransactionTableModel model = (TransactionTableModel)table.getModel(); c.setHorizontalAlignment(JLabel.CENTER); //䆒㕂ሙЁ } c.setText(value.toString()); /Download at http://www.pin5i.com public class Comment extends ValueObject { ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\model\Comment.java˖ џࡵ䆘䆎ᇍ䈵ࣙᣀҹϟሲᗻDŽ } private String OPERATE_DATE; //䖯㸠䕀থ᪡԰⫼᠋ᇍ䆹џࡵⱘ䕀থᯊ䯈 private String TARGET_USER_ID; //䖯㸠䕀থ᪡԰ⱘⳂᷛ⫼᠋ private String USER_ID; //䖯㸠䕀থ᪡԰ⱘ⫼᠋ private String TS_ID; //џࡵ ID public class UserTransfer extends ValueObject { ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\model\UserTransfer.java˖ џࡵ䕀থᇍ䈵ࣙᣀҹϟሲᗻDŽ ⱘ䆄ᔩˈ䖭ѯ᮹ᖫᇚӮᰒ⼎ࠄџࡵᶹⳟ⬠䴶ЁDŽ 䖭ϔ⃵䕀থⱘֵᙃֱᄬࠄ᭄᥂ᑧˈ䖬䳔㽕ֱᄬ⫼᠋ᇍџࡵⱘϔѯ䆘䆎ˈ᮹ᖫᰃֱᄬਬᎹ↣ϔ⃵᪡԰џࡵ ࠄⱘ݊Ҫᇍ䈵ˈ䖭ѯᇍ䈵ࣙᣀ˖џࡵ䕀থǃџࡵ䆘䆎Ϣ᮹ᖫᇍ䈵DŽ⫼᠋䖯㸠↣ϔ⃵џࡵ䕀থˈ䛑䳔㽕ᇚ ೼ࠡ䴶ⱘゴ㡖ЁህᏆ㒣䆒䅵њ⫼᠋ǃ㾦㡆ǃџࡵᇍ䈵ˈ䖭䞠᠔䇈ⱘ݊Ҫᇍ䈵ˈᰃᣛ㋏㒳᠔䳔㽕Փ⫼ 10.6.1 䆒䅵݊Ҫᇍ䈵 ㅵ⧚ⱘⳌ݇ᇍ䈵DŽ ⱘџࡵ⬠䴶Ёˈৠḋഄ᳝ϔϾџࡵ߫㸼ˈ಴ℸˈ᮴䆎Ң߫㸼␆ᶧ఼៪㗙 TableModelˈ䛑ৃҹՓ⫼џࡵ ೼ 10.5 ゴ㡖ЁᏆ㒣ᅲ⦄њџࡵㅵ⧚ⱘⳌ݇᪡԰ˈ䙷М᥹ϟᴹህ䳔㽕ᅲ⦄ਬᎹᇍџࡵⱘ໘⧚DŽ೼៥ 10.6 ៥ⱘџࡵ Ё䆺㒚ᦣ䗄DŽ џࡵㅵ⧚ⱘࡳ㛑෎ᴀᏆ㒣ᅲ⦄ˈԚᰃ䖬᳝ϔϾᶹⳟџࡵⱘࡳ㛑ᑊ≵᳝ᅲ⦄ˈ䆹ࡳ㛑ᇚ೼ϟ䴶ⱘゴ㡖 ҹ䖯㸠㕂Ў᮴ᬜ໘⧚DŽ ᅮᰃ৺ৃއᕫࠄϔϾџࡵᇍ䈵ˈݡџࡵᇍ䈵ⱘ⢊ᗕ䖯㸠߸ᮁˈݡܜˈ೼ TransactionServiceImpl Ё } } this.transactionDao.invalid(id); } else { throw new BusinessException("џࡵᏆ㒣ᅠ៤ˈϡৃҹ䆒㕂Ў᮴ᬜ"); if (t.getTS_STATE().equals(TransactionState.FINISHED)) { Transaction t = this.transactionDao.find(id); //བᵰџࡵᏆ㒣ᅠ៤ˈ߭ϡৃҹ㕂Ў᮴ᬜ public void invalid(String id) { ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\dao\impl\TransactionDaoImpl.java˖ } getJDBCExecutor().executeUpdate(sql.toString()); TransactionState.INVALID + "' where ts.ID = '" + id + "'"; String sql = "update T_TRANSACTION ts set ts.TS_STATE = '" + public void invalid(String id) { /Download at http://www.pin5i.com 䳔㽕䇗⫼䆹᥹ষⱘ handler ᮍ⊩ेৃDŽ 䗝ᢽ⬠䴶ϔḋˈᦤկϔϾ TransactionHandler ᥹ষˈџࡵ໘⧚⬠䴶া᠋⫼ڣ಴ℸ៥Ӏ䳔㽕ˈ˅خϢϡخ 䕧ܹⳌᑨⱘ䆘䆎DŽ೼ᴀゴЁ៥Ӏা᳝ϔϾџࡵ໘⧚⬠䴶ˈԚՓ⫼䆹⬠䴶ⱘै᳝ϝϾࡳ㛑˄ᅠ៤ǃ᱖ᯊϡ ⱘᣝ䪂ᯊˈ㋏㒳ህ䳔㽕ᔍߎ 10.2.3 Ёⱘџࡵ໘⧚⬠䴶ˈ䅽⫼᠋خᔧ⫼᠋䗝ᢽњџࡵᑊ⚍ߏњ᱖ᯊϡ ā⢊ᗕخ10.6.2 ᇚџࡵ䆒ЎĀ᱖ᯊϡ џࡵㅵ⧚Ёᰃḍ᥂থ䍋Ҏ ID এᶹᡒџࡵDŽ Ϣџࡵㅵ⧚Ёⱘ findInitiatorTransactions ᮍ⊩ϡৠⱘᰃˈ䆹ᮍ⊩ᰃḍ᥂໘⧚Ҏ ID এᶹᡒџࡵˈ㗠 } return result; new ArrayList(), Transaction.class); List result = (List)getDatas(sql.toString(), .append(state + "' order by ts.IS_HURRY desc"); .append(" and ts.TS_STATE = '") .append(userId + "'") sql.append(" where ts.HANDLER_ID = '") StringBuffer sql = new StringBuffer("select * from T_TRANSACTION ts"); public List findHandlerTransactions(String state, String userId) { ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\dao\impl\TransactionDaoImpl.java˖ ᶹ䆶ܼ䚼៥ⱘџࡵা䳔㽕೼ TransactionDaoImpl Ёᦤկᶹ䆶ᮍ⊩ेৃDŽ ѯ᪡԰ℷᰃ៥Ӏ䳔㽕䆄ᔩ᮹ᖫⱘഄᮍˈ಴ℸ೼䆒䅵᮹ᖫᇍ䈵ⱘᯊ׭ˈህЎ᮹ᖫᇍ䈵Ϣ䆘䆎ᇍ䈵䆒㕂݇㘨DŽ ߯ᓎ⬠䴶ⱘᯊ׭ˈ៥ӀህᏆ㒣⹂ᅮњˈᇍџࡵ䖯㸠໘⧚៪㗙䕀থⱘᯊ׭ˈ䛑䳔㽕㒭ќϔᅮⱘ䆘䆎ˈ㗠䖭 ೼䖭䞠䳔㽕⊼ᛣⱘᰃˈ᮹ᖫᇍ䈵Ё᳝ϔϾ COMMENT_IDˈ㸼⼎䆄ᔩϔ⃵᮹ᖫ䳔㽕᳝ϔᴵ䆘䆎ˈ೼ } private String TS_DESC; //᮹ᖫᦣ䗄 private String TS_ID; //ᇍᑨⱘџࡵ private String COMMENT_ID; //ᇍᑨⱘ䆘䆎 private String HANDLER_ID; //໘⧚Ҏ private String LOG_DATE; //䆄ᔩⱘ᮹ᳳ public class Log extends ValueObject { ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\model\Log.java˖ ᮹ᖫᇍ䈵ࣙᣀҹϟሲᗻDŽ } private String TRANSACTION_ID; //䆘䆎ⱘџࡵ private String USER_ID; //䆘䆎Ҏ ID private String CM_DATE; //䆘䆎᮹ᳳ private String CM_CONTENT; //䆘䆎ݙᆍ private String CM_TITLE; 䆘䆎ᷛ乬// /Download at http://www.pin5i.com TransactionHandler ⱘᅲ⦄㉏DŽForAWhileHandler˖ āⱘ໘⧚㉏خāDŽҹϟᰃĀ᱖ᯊϡخџࡵⱘ⢊ᗕᑊϡᰃ೼䖯㸠Ёˈ䙷Мህϡৃҹᇚџࡵ㕂ЎĀ᱖ᯊϡ ೼㓪ݭϮࡵ䘏䕥ⱘᯊ׭ˈ៥Ӏ䳔㽕⊼ᛣⱘᰃˈབᵰ⫼᠋໘⧚ⱘϡᰃ㞾Ꮕⱘџࡵˈ߭䳔㽕ᦤ⼎ˈབᵰ } getJDBCExecutor().executeUpdate(sql.toString()); TransactionState.FOR_A_WHILE + "' where ts.ID = '" + id + "'"; String sql = "update T_TRANSACTION ts set ts.TS_STATE = '" + public void forAWhile(String id) { ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\dao\impl\TransactionDaoImpl.java˖ āⱘ⢊ᗕˈা䳔㽕Փ⫼ SQL ⱘ update 䇁হˈᬍবџࡵⱘ⢊ᗕेৃDŽخᇚџࡵ䆒㕂ЎĀ᱖ᯊϡ } this.htDialog.setVisible(true); this.htDialog.setHandler(this.forAWhileHandler); this.htDialog.setTransaction(t); //ᰒ⼎໘⧚ᇍ䆱Ḛ Transaction t = ApplicationContext.transactionService.get(id); //ᕫࠄџࡵᇍ䈵 } return; ViewUtil.showWarn("䇋䗝ᢽ䳔㽕᪡԰ⱘџࡵ", this); if (id == null) { String id = ViewUtil.getSelectValue(this.dataTable, "id"); private void forAWhile() { خ //᱖ᯊϡ private TransactionHandler forAWhileHandler = new ForAWhileHandler(); ໘⧚㉏خ //᱖ᯊϡ ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\ui\MyTransactionPanel.java˖ ˄HandleTransactionDialog˅DŽ ⢊ᗕᯊˈህৃҹՓ⫼ϔϾ TransactionHandler ⱘᅲ⦄㉏ᴹᵘ䗴џࡵ໘⧚⬠䴶 āخҹϞҷⷕⱘ咥ԧ䚼ߚˈা䇗⫼ TransactionHandler ⱘ handler ᮍ⊩DŽ䙷Мᇚџࡵ䆒㕂ЎĀ᱖ᯊϡ } } ViewUtil.showWarn(e.getMessage(), this); } catch (Exception e) { this.setVisible(false); this.myPanel.readData(); //ࠋᮄ⬠䴶߫㸼 this.handler.handler(comment); //໘⧚䆘䆎Ϣ䆒㕂џࡵ⢊ᗕ try { comment.setUSER_ID(ApplicationContext.loginUser.getID()); comment.setTRANSACTION_ID(this.transationId.getText()); comment.setCM_DATE(ViewUtil.formatDate(new Date())); comment.setCM_CONTENT(this.content.getText()); Comment comment = new Comment(); private void confirm() { //⹂ᅮ䖯㸠џࡵ໘⧚ ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\ui\dialog\HandleTransactionDialog.java˖ ҹϟᰃџࡵ໘⧚⬠䴶ⱘ⹂ᅮᮍ⊩DŽ /Download at http://www.pin5i.com TransactionState.NOT_TO_DO + "' where ts.ID = '" + id + "'"; String sql = "update T_TRANSACTION ts set ts.TS_STATE = '" + public void notToDo(String id) { ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\dao\impl\TransactionDaoImpl.java˖ Ў TransactionDao ࡴܹᬍব⢊ᗕⱘᮍ⊩DŽ ⢊ᗕˈᇚ᮴⊩ᘶ໡ࠄ䖯㸠Ё⢊ᗕˈৠḋഄˈгϡ㛑ᅠ៤DŽ āDŽџࡵϔᮺ䆒㕂Ў䆹خāⱘ⢊ᗕϟᠡৃҹᇚ݊⢊ᗕᬍবЎĀϡخԚᰃџࡵা㛑೼䖯㸠Ё៪㗙Ā᱖ᯊϡ ā⢊ᗕ㉏Ԑˈা䳔㽕ᬍবџࡵⱘ⢊ᗕˈݡֱᄬ䆘䆎Ϣ᮹ᖫेৃDŽخϢ 10.6.2 Ёᇚџࡵ㕂ЎĀ᱖ᯊϡ ā⢊ᗕخ10.6.3 ᇚџࡵ㕂ЎĀϡ ህӮֱᄬϔᴵֵᙃ᭄᥂Ϣ᮹ᖫ᭄᥂ࠄ᭄᥂ᑧDŽ䖭ѯ᭄᥂ⱘ԰⫼ᇚ೼ϟ䴶ⱘゴ㡖Ёᦣ䗄DŽ āⱘ᪡԰ᯊˈخҹϞҷⷕⱘ createLog ᮍ⊩ᰃᎹ݋ᮍ⊩ˈ⫼Ѣֱᄬϔᴵ᮹ᖫֵᙃˈ⫼᠋䖯㸠Ā᱖ᯊϡ } this.logDao.save(log); log.setTS_DESC(desc); log.setTS_ID(tsId); log.setLOG_DATE(ViewUtil.timeFormatDate(new Date())); log.setHANDLER_ID(handlerId); log.setCOMMENT_ID(commentId); Log log = new Log(); private void createLog(String tsId, String handlerId, String commentId, String desc) { //߯ᓎ᮹ᖫ } } ᗕ");⢊خ throw new BusinessException("џࡵ䴲䖯㸠Ё, ϡৃҹ㕂Ў᱖ᯊϡ else { { ;("خ createLog(id, userId, String.valueOf(commentId), " ᱖ᯊϡ Integer commentId = this.commentDao.save(comment); //ֱᄬ䆘䆎 this.transactionDao.forAWhile(id); if (t.getTS_STATE().equals(TransactionState.PROCESSING)) { //া᳝೼䖯㸠Ёⱘџࡵᠡৃҹᬍবℸ⢊ᗕ } throw new BusinessException("া㛑໘⧚㞾Ꮕⱘџࡵ"); if (!t.getHANDLER_ID().equals(userId)) { ᗕ⢊خ //া᳝㞾Ꮕⱘџࡵᠡৃҹ㕂Ў᱖ᯊϡ Transaction t = this.transactionDao.find(id); public void forAWhile(String id, String userId, Comment comment) { ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\service\impl\TransactionServiceImpl.java˖ 㸠䖭Ͼ᪡԰ⱘᯊ׭ˈህ䳔㽕ᇚⳌᑨⱘ䆘䆎ֱᄬࠄ᭄᥂ᑧˈᑊֱᄬⳌᑨⱘ᮹ᖫֵᙃDŽ āˈᔧ⫼᠋䖯خҹϞҷⷕЁˈ䇗⫼њџࡵϮࡵ䘏䕥᥹ষⱘ forAWhile ᮍ⊩ᇚџࡵⱘ⢊ᗕ㕂ЎĀ᱖ᯊϡ } } comment.getUSER_ID(), comment); ApplicationContext.transactionService.forAWhile(comment.getTRANSACTION_ID(), 䆒㕂џࡵⱘ⢊ᗕ, ݡ⏏ࡴ䆘䆎ܜᗕ, 䳔㽕⢊خ //ᇚџࡵ㕂Ў᱖ᯊϡ public void handler(Comment comment) { public class ForAWhileHandler implements TransactionHandler { /Download at http://www.pin5i.com 䙷М೼џࡵ໘⧚⬠䴶⚍ߏ⹂ᅮⱘᯊ׭ˈህӮᠻ㸠 NotToDoHandler ⱘ handler ᮍ⊩DŽ } this.htDialog.setVisible(true); this.htDialog.setHandler(this.notToDoHandler); //䆒㕂໘⧚㉏ this.htDialog.setTransaction(t); //ᰒ⼎໘⧚ᇍ䆱Ḛ Transaction t = ApplicationContext.transactionService.get(id); //ᕫࠄџࡵᇍ䈵 } return; ViewUtil.showWarn("䇋䗝ᢽ䳔㽕᪡԰ⱘџࡵ", this); if (id == null) { String id = ViewUtil.getSelectValue(this.dataTable, "id"); private void notToDo() { private TransactionHandler notToDoHandler = new NotToDoHandler(); ໘⧚㉏خᅮϡއ// ˖ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\ui\MyTransactionPanel.java ೼៥ⱘџࡵ⬠䴶Ёˈህ䳔㽕߯ᓎ໘⧚㉏DŽ } } comment.getUSER_ID(), comment); ApplicationContext.transactionService.notToDo(comment.getTRANSACTION_ID(), ݡ⏏ࡴ䆘䆎 ,خ //䗝ᢽᇚџࡵ⢊ᗕᬍবЎϡ public void handler(Comment comment) { public class NotToDoHandler implements TransactionHandler { ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\ui\handler\impl\NotToDoHandler.java˖ ৠḋഄˈ⬅ѢՓ⫼њৠϔϾџࡵ໘⧚⬠䴶ˈ಴ℸህ䳔㽕ᦤկϔϾ TransactionHandler ⱘᅲ⦄㉏DŽ } } ᗕ");⢊خ throw new BusinessException("ϡৃҹ㕂Ў᱖ᯊϡ else { { ;("خᅮϡއ " ,( createLog(id, userId, String.valueOf(commentId Integer commentId = this.commentDao.save(comment); //ֱᄬ䆘䆎 this.transactionDao.notToDo(id); || t.getTS_STATE().equals(TransactionState.FOR_A_WHILE)) { if (t.getTS_STATE().equals(TransactionState.PROCESSING) ⱘџࡵᠡৃҹᬍবℸ⢊ᗕخ //া᳝೼䖯㸠ЁⱘџࡵϢ᱖ᯊϡ } throw new BusinessException("া㛑໘⧚㞾Ꮕⱘџࡵ"); if (!t.getHANDLER_ID().equals(userId)) { ᗕ⢊خ //া᳝㞾Ꮕⱘџࡵᠡৃҹ㕂Ў᱖ᯊϡ Transaction t = this.transactionDao.find(id); public void notToDo(String id, String userId, Comment comment) { ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\service\impl\TransactionServiceImpl.java˖ ЎџࡵⱘϮࡵ䘏䕥ሖࡴܹⳌ݇Ϯࡵᮍ⊩DŽ } getJDBCExecutor().executeUpdate(sql.toString()); /Download at http://www.pin5i.com this.userDialog = new SelectUserDialog(this.selectHandler); //⫼᠋䗝ᢽᇍ䆱Ḛ this.selectHandler = new TransferUserSelectHandler(this); //⫼᠋䗝ᢽ໘⧚㉏ ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\ui\dialog\TransferTransactionDialog.java˖ 䙷М೼䕀থџࡵⱘ⬠䴶ˈ䳔㽕⫼᠋䗝ᢽ໘⧚㉏DŽ } } this.dialog.getRealNameText().setText(realName); this.dialog.getUserIdText().setText(userId); public void confirm(String userId, String realName) { } this.dialog = dialog; public TransferUserSelectHandler(TransferTransactionDialog dialog) { private TransferTransactionDialog dialog; //䕀থџࡵⱘ໘⧚にষ public class TransferUserSelectHandler implements UserSelectHandler { code\transaction\src\org\crazyit\transaction\ui\handler\impl\TransferUserSelectHandler.java˖ ҷⷕ⏙ऩ˖ ᓎџࡵᯊⱘ䗝ᢽ⫼᠋ᅲ⦄䇋খⳟ 10.5.3 ゴ㡖DŽ ✻ᮄᓎџࡵᯊ䗝ᢽ⫼᠋ⱘᮍᓣˈᦤկϔϾ UserSelectHandler ⱘᅲ⦄㉏ TransferUserSelectHandlerˈᮄ 㽕䕀থⱘ⫼᠋ˈџࡵ䕀থ⬠䴶೼ 10.2.4 ЁᏆ㒣߯ᓎDŽ೼ᴀ㋏㒳Ёˈা᳝ϔϾ⫼᠋䗝ᢽ⬠䴶ˈ಴ℸ៥Ӏᣝ ໘⧚ˈ䕀থџࡵⱘ⬠䴶Ϣࠡ䴶ⱘ᪡԰⿡ᖂ᳝ϔ⚍ϡϔḋˈ䆹⬠䴶ᦤկњ⫼᠋䗝ᢽࡳ㛑ˈ䅽᪡԰Ҏ䗝ᢽ䳔 ᔧ⫼᠋≵᳝ᯊ䯈៪㗙≵᳝㛑࡯এ໘⧚ᶤϾџࡵⱘᯊ׭ˈৃҹᇚ䆹џࡵ䕀থ㒭݊Ҫⱘৠџˈ䅽݊ҷЎ 10.6.5 䕀থџࡵ ㉏ԐDŽ āخāϢĀϡخᅠ៤џࡵᦤկ TransactionHandler ⱘᅲ⦄㉏ FinishHandlerˈ݊Ҫᅲ⦄ഛϢĀ᱖ᯊϡ } this.htDialog.setVisible(true); this.htDialog.setHandler(this.finishHandler); this.htDialog.setTransaction(t); //ᰒ⼎໘⧚ᇍ䆱Ḛ Transaction t = ApplicationContext.transactionService.get(id); //ᕫࠄџࡵᇍ䈵 } return; ViewUtil.showWarn("䇋䗝ᢽ䳔㽕᪡԰ⱘџࡵ", this); if (id == null) { String id = ViewUtil.getSelectValue(this.dataTable, "id"); private void finish() { //ᇚџࡵ㕂Ўᅠ៤⢊ᗕ ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\service\impl\TransactionServiceImpl.java˖ ⱘ⢊ᗕϔ㟈ˈ䛑ᰃ䳔㽕ᬍবџࡵ⢊ᗕˈݡᮄᓎ䆘䆎䆄ᔩϢ᮹ᖫ䆄ᔩDŽҹϟᰃϮࡵ䘏䕥ⱘᅲ⦄DŽ āخāϢĀϡخᅠ៤џࡵˈাᰃਬᎹᇚ㞾Ꮕ໘⧚ⱘџࡵ㕂Ўᅠ៤⢊ᗕˈϢࠡ䴶ゴ㡖Ёⱘ㕂ЎĀ᱖ᯊϡ ᅠ៤џࡵ 10.6.4 /Download at http://www.pin5i.com setUser(t); Transaction t = this.transactionDao.find(id); public Transaction view(String id) { ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\service\impl\TransactionServiceImpl.java˖ ᶹⳟ᮹ᖫⱘ݋ԧϮࡵᅲ⦄DŽ ⿟ˈ䛑Ӯֱᄬϔᴵ᮹ᖫ䆄ᔩˈ಴ℸ䖯㸠ᶹⳟџࡵⱘ᪡԰ᯊˈ៥Ӏৃҹᇚ䖭ѯ᮹ᖫᰒ⼎ࠄ⬠䴶ЁDŽҹϟᰃ џࡵⱘ⌕䗮䖛⿟ˈ՟བ⬆䕀থњџࡵ㒭Эˈ䖭ϔ䖛⿟䛑䳔㽕೼ᶹⳟџࡵЁԧ⦄ˈ㗠ਬᎹ೼᪡԰џࡵⱘ䖛 ᔧ䗝ᢽњᶤᴵџࡵ䆄ᔩ䖯㸠ᶹⳟᯊˈ៥Ӏ䳔㽕ᕫࠄџࡵⱘ݋ԧֵᙃˈ䰸њ䖭ѯֵᙃ໪ˈ䖬䳔㽕ᕫࠄ ᅲ⦄ᶹⳟџࡵⱘࡳ㛑DŽ ៥Ӏᑊ≵᳝೼ࠡ䴶ⱘゴ㡖Ё߯ᓎᶹⳟџࡵⱘ⬠䴶ˈг≵᳝ᦤկӏԩᶹⳟџࡵⱘᅲ⦄ˈ೼ᴀᇣ㡖ˈ៥Ӏᇚ ೼㋏㒳Ёⱘ↣ϔϾ⫼᠋ˈ䛑ৃҹᶹⳟࠄϢ㞾ᏅⳌ݇ⱘџࡵˈা䳔㽕೼џࡵ߫㸼䖯㸠ঠߏ᪡԰ेৃˈ 10.6.6 ᶹⳟџࡵ Ϟҷⷕⱘ咥ԧ䚼ߚߚ߿ֱᄬ䕀থ䆄ᔩ੠ᬍবџࡵⱘ໘⧚ҎDŽ ⧚Ҏˈᇚџࡵᇍ䈵˄Transaction˅ⱘࠡϔ໘⧚Ҏ˄PRE_HANDLER_ID˅䆒㕂Ўॳᴹⱘ໘⧚Ҏ IDDŽҹ џࡵ䕀থᑊϡ䳔㽕ᬍবџࡵⱘ⢊ᗕˈাᰃֱᄬϔᴵ䕀থ䆄ᔩˈᑊᇚџࡵⱘᔧࠡ໘⧚Ҏ䆒㕂Ўᮄⱘ໘ } } ⱘџࡵᠡৃҹ䕀থ");خ throw new BusinessException("া᳝䖯㸠Ё៪㗙᱖ᯊϡ } else { targetUser.getREAL_NAME() + " "); createLog(t.getID(), sourceUserId, String.valueOf(commentId), " 䕀থ㒭 " + User targetUser = this.userDao.find(targetUserId); sourceUserId, comment.getTRANSACTION_ID()); this.transactionDao.changeHandler(targetUserId, //ᬍবџࡵ䆄ᔩⱘᔧࠡ໘⧚Ҏ id Ϣࠡϔ໘⧚Ҏ id Integer commentId = this.commentDao.save(comment); //ֱᄬ䆘䆎 this.userTransferDao.save(ut); //ᮄ๲䕀থ䆄ᔩ ut.setOPERATE_DATE(ViewUtil.formatDate(new Date())); ut.setTARGET_USER_ID(targetUserId); ut.setUSER_ID(sourceUserId); ut.setTS_ID(comment.getTRANSACTION_ID()); UserTransfer ut = new UserTransfer(); || t.getTS_STATE().equals(TransactionState.FOR_A_WHILE)) { if (t.getTS_STATE().equals(TransactionState.PROCESSING) ⱘџࡵᠡৃҹ䕀থخ //া᳝೼䖯㸠ЁⱘџࡵϢ᱖ᯊϡ } throw new BusinessException("া㛑໘⧚㞾Ꮕⱘџࡵ"); if (!t.getHANDLER_ID().equals(sourceUserId)) { //া᳝㞾Ꮕⱘџࡵᠡৃҹ䕀থ Transaction t = this.transactionDao.find(comment.getTRANSACTION_ID()); Comment comment) { public void transfer(String targetUserId, String sourceUserId, ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\service\impl\TransactionServiceImpl.java˖ 䕥ⱘ݋ԧᅲ⦄DŽ ҹϞᅲ⦄њ⫼᠋ⱘ䗝ᢽˈ䙷Мᔧ䕧ܹᅠⳌᑨ䕀থֵᙃৢˈ⚍ߏ⹂ᅮህৃҹ䖯㸠ֱᄬˈҹϟᰃϮࡵ䘏 /Download at http://www.pin5i.com ҹϞⱘ咥ԧҷⷕˈᇚ᮹ᖫ㒘㺙៤ⳌᑨⱘᄫヺІˈݡᰒ⼎ࠄ᭛ᴀඳЁˈ᳔ৢⱘᬜᵰབ೒ 10.13 ᠔⼎DŽ } } } + log.getComment().getCM_CONTENT() + "\n"); + log.getLOG_DATE() + " ᇚџࡵ " + log.getTS_DESC() + ": " this.processArea.append(log.getHandler().getREAL_NAME() + " Ѣ " for (Log log : this.transaction.getLogs()) { this.processArea.setText(""); this.initiator.setText(this.transaction.getInitiator().getREAL_NAME()); this.handler.setText(this.transaction.getHandler().getREAL_NAME()); this.targetDate.setText(this.transaction.getTS_TARGETDATE()); this.content.setText(this.transaction.getTS_CONTENT()); this.title.setText(this.transaction.getTS_TITLE()); if (this.transaction != null) { //བᵰᔧࠡ⬠䴶ⱘ Transaction ᇍ䈵ϡЎぎ,߭䆒㕂Ⳍᑨⱘؐ if (!b) return; super.setVisible(b); public void setVisible(boolean b) { ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\ui\dialog\ViewTransactionDialog.java˖ ࠄ䖭Ͼ Transaction ৢˈህৃҹሩ⦄䖭Ͼᇍ䈵᠔ֱᄬⱘⳌֵ݇ᙃDŽҹϟᶹⳟџࡵ⬠䴶ⱘ䚼ߚᅲ⦄DŽ ҹϞҷⷕЁˈḍ᥂џࡵᕫࠄⳌᑨⱘ᮹ᖫˈݡ䆒㕂ࠄ Transaction ᇍ䈵Ёˈ䙷М೼⬠䴶ᇍ䈵Ёˈᔧᕫ } return t; t.setLogs(logs); } log.setHandler(user); log.setComment(comment); User user = this.userDao.find(log.getHANDLER_ID()); Comment comment = this.commentDao.find(log.getCOMMENT_ID()); for (Log log : logs) { List logs = this.logDao.find(id); ᶹᡒⳌᑨⱘ᮹ᖫ//  ೒ 10.13 ᶹⳟџࡵ ৃҹҢ೒ 10.13 Ёⳟࠄˈ᭛ᴀඳЁᰒ⼎њџࡵⱘ໘⧚䖛⿟ˈ䙷Мџࡵㅵ⧚ਬህৃҹ䗮䖛ᶹⳟџࡵᴹ њ㾷ᭈϾџࡵⱘ䖯㸠⢊ᗕDŽ 10.6.7 ᶹ䆶џࡵ џࡵㅵ⧚Ё᳝ϔϾџࡵᶹ䆶ࡳ㛑ˈ䆹ࡳ㛑ḍ᥂џࡵⱘ⢊ᗕএᶹ䆶ⳌᑨⱘџࡵDŽৠḋഄˈ೼៥ⱘџࡵ Ёг᳝ϔϾџࡵᶹ䆶ࡳ㛑ˈԚᰃ䆹ࡳ㛑Ϣџࡵㅵ⧚Ёⱘᶹ䆶ࡳ㛑᳝ϡৠП໘ˈ៥ⱘџࡵⱘᶹ䆶ࡳ㛑ˈ⢊ ᗕ䗝ᢽϟᢝḚЁˈ↨џࡵㅵ⧚Ёⱘᶹ䆶໮њϔϾ⢊ᗕˈབ೒ 10.14 ᠔⼎DŽ Download at http://www.pin5i.com/ /Download at http://www.pin5i.com ࠄℸˈᴀゴⱘџࡵ䎳䏾㋏㒳ⱘࡳ㛑Ꮖ㒣ܼ䚼ᅲ⦄DŽ 䕀থ䆄ᔩˈ಴ℸ䖬䳔㽕䗮䖛ҹϞⱘ咥ԧҷⷕএᥝ䞡໡ⱘџࡵ᭄᥂DŽ ᰃϔϾџࡵ㹿䕀থњ໮⃵ˈህѻ⫳њ໮⃵މথ䆄ᔩᕫࠄџࡵⱘ IDˈህৃҹᡒߎⳌᑨⱘџࡵˈԚᰃ᳝ѯᚙ এ UserTransfer ᶹ䆶᠔᳝ⱘ䕀থ䆄ᔩˈݡ䗮䖛䕀ܜҹϞҷⷕˈབᵰᶹ䆶ⱘᰃĀ៥䕀থⱘџࡵāˈ߭ } return setUnion(datas); user.getID()); List datas = this.transactionDao.findHandlerTransactions(state, //݊Ҫ⢊ᗕ߭Ⳉ᥹ḍ᥂⢊ᗕএ᭄᥂ᑧᶹ䆶 } return setUnion(datas); datas = removeRepeat(datas); //এᥝ䞡໡᭄᥂ } datas.add(t); Transaction t = this.transactionDao.find(ut.getTS_ID()); for (UserTransfer ut : transfers) { List transfers = this.userTransferDao.find(user.getID()); //ᶹᡒ䕀থ䆄ᔩ List datas = new ArrayList(); if (state.equals(State.TRANSFER)) { //བᵰ⢊ᗕখ᭄ᰃ transfer(䕀থⱘџࡵ), ߭ᶹᡒ៥䕀থⱘџࡵ public List getHandlerTransaction(User user, String state) { ҷⷕ⏙ऩ˖code\transaction\src\org\crazyit\transaction\service\impl\TransactionServiceImpl.java˖ ࠄⱘ䕀থ䆄ᔩ䕀ᤶ៤Ⳍᑨⱘџࡵᇍ䈵DŽ ᶹ䆶ⱘᯊ׭ˈ㋏㒳ህϡᰃࠄџࡵ㸼Ёᶹ䆶ˈ㗠ᰃࠄ䕀থ䆄ᔩ㸼˄UserTransfer˅Ё䖯㸠ᶹ䆶ˈݡᇚᶹ䆶 ៥ⱘџࡵⱘᶹ䆶ࡳ㛑ϟᢝḚЁ໮ߎњϔϾĀ៥䕀থⱘџࡵā䖭ϔϾ⢊ᗕˈᔧ⫼᠋䗝ᢽњ䆹⢊ᗕ䖯㸠 ೒ 10.14 ᶹ䆶џࡵ  /Download at http://www.pin5i.com ゴՓ⫼ⱘ MySQL ⠜ᴀᰃ 5.0DŽ MySQL ᭄᥂ᑧˈ᳔ৢ䖤㸠Ābin\џࡵ䎳䏾㋏㒳\startup.batāेৃˈ咬䅸⫼᠋ৡ੠ᆚⷕ䛑ᰃ crazyitDŽᴀ transaction.jar\cfg\jdbc.propertiesˈᑊᇚĀbin\џࡵ䎳䏾㋏㒳\sqlāⳂᔩϟⱘ transaction.sql ᇐܹԴⱘ ゴⱘ⿟ᑣˈ䳔㽕ׂᬍĀbin\ џࡵ䎳䏾㋏㒳āЁⱘ transaction.jar ࣙЁⱘ䜡㕂᭛ӊ˖ ゴЁৠḋՓ⫼њϝሖ㒧ᵘˈᔧ݊Ёϔሖⱘ䘏䕥៪㗙ᅲ⦄থ⫳ব࣪ᯊˈ䛑ϡӮᕅડ䇗⫼㗙ⱘՓ⫼DŽ䖤㸠ᴀ ⧚㋏㒳㉏Ԑˈ಴ℸᓔথⱘ䞡⚍ᬒ೼Ϯࡵ䘏䕥Ёˈᴀゴᦣ䗄᳈໮ⱘᰃџࡵ䎳䏾㋏㒳Ϯࡵࡳ㛑ⱘᅲ⦄DŽ೼ᴀ ᴀゴᓔথњϔϾ䕗Ўㅔऩⱘџࡵ䎳䏾㋏㒳ˈᴀゴ᠔⍝ঞⱘᡔᴃᑊϡ䲒ˈᑩሖⱘᅲ⦄Ϣ೒к䖯ᄬ䫔ㅵ 10.7 ᴀゴᇣ㒧 ㄀ 10 ゴ џࡵ䎳䏾㋏㒳 g35g /Download at http://www.pin5i.com MainFrame.javaˈЏ⬠䴶ⱘ᳔㒜ᬜᵰབ೒ 11.2 ᠔⼎DŽ 䌘⑤ֵᙃϔഫՓ⫼ JList ᅲ⦄ˈЏ㽕ᇚ䌘⑤ⱘ䚼ߚֵᙃᰒ⼎ࠄ䆹ऎඳDŽᴀゴЁЏ⬠䴶ᇍᑨⱘᰃ 䳔㽕ᰒ⼎Ⳍᑨⱘ೒⠛ǃϟ䕑䖯ᑺ䳔㽕Փ⫼䖯ᑺᴵ䖯㸠ᰒ⼎ㄝDŽ ᯊ䯈ㄝDŽϟ䕑߫㸼Փ⫼ JTable ᅲ⦄ˈ㗠ϟ䕑߫㸼Ёⱘ↣ϔ߫䛑䳔㽕䖯㸠ϡৠⱘ␆ᶧˈ՟བ䌘⑤ⱘ⢊ᗕ ϟ䕑߫㸼Џ㽕ᰃᇚ䌘⑤ⱘֵᙃሩ⼎㒭⫼᠋ˈࣙᣀ䌘⑤ⱘ⢊ᗕǃ䌘⑤ৡ⿄ǃϟ䕑䗳ᑺǃ䖯ᑺҹঞՓ⫼ ⑤ˈབᵰ៥Ӏ䳔㽕⏏ࡴ݊Ҫⱘࡳ㛑ˈ՟བᇍ䌘⑤䖯㸠ߚ㉏ㄝˈ䛑ৃҹ৥ᇐ㟾ᷥЁࡴܹⳌ݇ⱘ㡖⚍DŽ ᇐ㟾ᷥৃҹ䅽⫼᠋䗝ᢽ⌣㾜ϡৠⱘ䌘⑤ˈ՟བℷ೼ϟ䕑ⱘ䌘⑤ǃϟ䕑༅䋹ⱘ䌘⑤ҹঞ៤ࡳϟ䕑ⱘ䌘 JTree ᴹᅲ⦄ˈϟ䕑߫㸼ৃҹՓ⫼ JTable ᅲ⦄ˈ䌘⑤ֵᙃৃҹՓ⫼ JList ᴹᅲ⦄DŽ ϟ䕑Ꮉ݋ⱘЏ⬠䴶Џ㽕ࣙᣀᇐ㟾ᷥǃϟ䕑߫㸼ǃ䌘⑤ֵᙃ䖭ϝ໻ഫˈᇐ㟾ᷥ៥ӀৃҹՓ⫼ swing ⱘ 11.2.1 Џ⬠䴶 ೒ 11.1 䖙䳋ⱘ ⍂にষ  Ӏᇚҟ㒡བԩᅲ⦄䖭ѯⳌ݇ⱘ⬠䴶DŽ 㒭⫼᠋ˈབ೒ 11.1 ᠔⼎DŽ㋏㒳ӏࡵ೒ᷛЏ㽕ᰃᇚϟ䕑Ꮉ݋ⱘ೒ᷛᰒ⼎ࠄ㋏㒳ⱘӏࡵᷣЁDŽ᥹ϟᴹˈ៥ Flashget Ϣ䖙䳋䖭ѯϟ䕑Ꮉ݋ϔḋˈ೼ሣᐩЁᓎゟϔϾᇣにষˈ䆹にষৃҹሩ⦄ϔѯϟ䕑ֵᙃ ڣህད ⬠䴶Џ㽕䅽⫼᠋䕧ܹ䳔㽕ϟ䕑ⱘ䌘⑤ഄഔㄝֵᙃˈ䅽ϟ䕑Ꮉ݋ৃҹᇍ䆹䌘⑤䖯㸠䖲᥹Ϣϟ䕑DŽ ⍂にষ ৥⫼᠋ሩ⼎ϟ䕑䌘⑤ⱘֵᙃˈ՟བᰒ⼎ℷ೼ϟ䕑ⱘ䌘⑤ֵᙃǃᏆϟ䕑ᅠ៤ⱘ䌘⑤ֵᙃㄝDŽ⏏ࡴϟ䕑䌘⑤ ϟ䕑Ꮉ݋ࣙᣀЏ⬠䴶ǃ⏏ࡴϟ䕑䌘⑤ⱘ⬠䴶ǃ ⍂にষϢ㋏㒳ӏࡵᷣ೒ᷛDŽϟ䕑Ꮉ݋ⱘЏ⬠䴶Џ㽕 11.2 ᓎゟϟ䕑Ꮉ݋ⱘ⬠䴶 ϔϾ HTTP ⱘ໮㒓⿟ϟ䕑Ꮉ݋DŽ ҹঞ BT ⱘᬃᣕㄝˈ঺໪ˈϟ䕑Ꮉ݋䖬䳔㽕ᦤկ໮㒓⿟ϟ䕑ǃᮁ⚍㓁Ӵㄝࡳ㛑ˈᴀゴЏ㽕ҟ㒡བԩᅲ⦄ ᅲ䖭ѯϟ䕑Ꮉ݋᠔ᦤկⱘࡳ㛑ˈ៥ӀৠḋৃҹՓ⫼ Java ᴹᅲ⦄ˈ䖭ѯᎹ݋ࣙᣀ HTTP ϟ䕑ǃFTP ϟ䕑 䕑ֵᙃࣙᣀ˖ϟ䕑䌘⑤ⱘ໻ᇣǃϟ䕑䖯ᑺǃϟ䕑䗳ᑺҹ⫼݊Ҫϔѯৃҹ䅽⫼᠋њ㾷ϟ䕑䖯ᑺⱘֵᙃDŽ݊ ݋ৃҹकߚᮍ֓ⱘᇍѦ㘨㔥䌘⑤䖯㸠ϟ䕑DŽ䖭ѯᎹ݋䗮䖛೒ᔶ⬠䴶ˈᇚϟ䕑ⱘֵᙃሩ⼎㒭⫼᠋ˈ䖭ѯϟ ៥Ӏᑇᯊ೼Փ⫼Ѧ㘨㔥ⱘᯊ׭ˈ䛑ӮՓ⫼ࠄ䆌䆌໮໮ⱘϟ䕑Ꮉ݋ˈ՟བ Flashgetǃ䖙䳋ㄝˈ䖭ѯᎹ 11.1 ໮㒓⿟ϟ䕑Ꮉ݋ㅔҟ ㄀  ゴ໮㒓⿟ϟ䕑Ꮉ݋ /Download at http://www.pin5i.com ҷⷕ⏙ऩ˖code\flashget\src\org\crazyit\flashget\navigation\DownloadingNode.java˖ ⦄ DownloadNodeDŽ 䆹᥹ষᅮНњϸϾᮍ⊩ˈߚ߿䖨ಲ㡖⚍ⱘ᭛ᄫϢ೒ᷛˈ䙷МৃҹᮄᓎϔϾℷ೼ϟ䕑ⱘ㡖⚍ᇍ䈵ˈᅲ } ImageIcon getImageIcon(); */ * @return * 䖨ಲᇍᑨ೒ᷛ /** String getText(); */ * @return * 㦋ᕫ㡖⚍ৡ⿄ /** public interface DownloadNode { ҷⷕ⏙ऩ˖code\flashget\src\org\crazyit\flashget\navigation\DownloadNode.java˖ ⫼Ѣ㸼⼎ᇐ㟾ᷥⱘ㡖⚍ˈ䆹ᷥϟ䴶᠔᳝ⱘ㡖⚍ᇍ䈵䛑ᖙ乏ᅲ⦄䖭Ͼ᥹ষDŽ ໛㸼⼎䖭ѯ㡖⚍ⱘᇍ䈵DŽᮄᓎϔϾ DownloadNode ᥹ষˈޚ䕑ᅠ៤㡖⚍DŽ೼ᓎゟᇐ㟾ᷥࠡˈ៥Ӏ䳔㽕 བ೒ 11.2 ᠔⼎ˈᇐ㟾ᷥЏ㽕᳝ϔϾӏࡵ㡖⚍ˈӏࡵ㡖⚍ϟ䴶ࣙᣀℷ೼䕑㡖⚍ǃϟ䕑༅䋹㡖⚍੠ϟ 11.2.2 ᇐ㟾ᷥ བԩᅲ⦄䖭ϝϾऎඳDŽ ᵰˈЏ⬠䴶ϝϾऎඳ˄ᇐ㟾ᷥǃ䌘⑤߫㸼੠䌘⑤ֵᙃ˅䛑䳔㽕䖯㸠⡍߿ⱘ໘⧚ˈҹϟⱘゴ㡖ᇚ䆺㒚ᦣ䗄 ϟ䕑ǃߴ䰸䗝Ё䌘⑤੠ߴ䰸Ꮖ㒣ᅠ៤ⱘӏࡵDŽЎњ䖒ࠄ៥Ӏ᠔䳔㽕ⱘᬜذ԰˖ᮄᓎ䌘⑤ǃϟྟϟ䕑ǃ᱖ བ೒ 11.2 ᠔⼎ˈЏ⬠䴶䰸њϞ䗄ⱘϝ໻ऎඳ໪ˈ䖬ᦤկњϔϾᎹ݋ᷣˈ⫼Ѣᄬᬒᇍ䌘⑤ⱘⳌ݇᪡ ೒ 11.2 Џ⬠䴶ᬜᵰ  /Download at http://www.pin5i.com ߫䳔㽕Փ⫼䖯ᑺᴵ԰Ўᰒ⼎ݙᆍDŽ೼ᴀゴЁˈ៥ӀՓ⫼ϔϾĀ䖯ᑺᴵⱘᄤ㉏ā԰Ўϟ䕑䖯ᑺ߫ⱘ␆ᶧ఼ˈ ೼䌘⑤߫㸼Ёˈ↣ϔ߫䛑䳔㽕᳝⡍ᅮⱘ␆ᶧ఼ˈ՟བ⢊ᗕ߫䳔㽕Փ⫼೒ᷛ԰Ўᰒ⼎ݙᆍˈϟ䕑䖯ᑺ 11.2.3 䌘⑤߫㸼 ౨ēఉ๠ࠋԤᄚಾٔࠋԤԅྡྷّᆐࠋԤdޝᅟ࿉ġζੋ౨ఉ๠ࠋԤಾٔࠋԤďఢ෍ 11.2 ൎವĐēӬಬ JTreeˈЏ㽕⫼Ѣ䆒㕂ᇐ㟾ᷥⱘⳌ݇ሲᗻˈ՟བ䱤㮣ḍ㡖⚍ǃ䆒㕂␆ᶧ఼ㄝDŽ ҹϞҷⷕⱘ咥ԧ䚼ߚˈ៥ӀՓ⫼њϔϾ NavigationTree ⱘᇍ䈵ᴹ㸼⼎ϔỉᇐ㟾ᷥˈ䆹㉏㒻ᡓѢ } this.navTree = new NavigationTree(root); tn.add(new DefaultMutableTreeNode(finishNode)); tn.add(new DefaultMutableTreeNode(failNode)); tn.add(new DefaultMutableTreeNode(downloadingNode)); //߯ᓎ৘Ͼ㡖⚍ root.add(tn); DefaultMutableTreeNode tn = new DefaultMutableTreeNode(taskNode); DefaultMutableTreeNode root = new DefaultMutableTreeNode(); private void createTree() { //Џ⬠䴶Ё߯ᓎᷥ ҷⷕ⏙ऩ˖code\flashget\src\org\crazyit\flashget\ui\MainFrame.java˖ ೼߯ᓎᇐ㟾ᷥⱘᯊ׭ˈ䳔㽕ᷥᣛᅮ␆ᶧ఼ˈᑊ߯ᓎ৘Ͼ㡖⚍DŽ } return this; } this.setText(obj.getText()); this.setIcon(obj.getImageIcon()); if (obj != null) { //䆒㕂᭛ᴀϢ೒⠛ DownloadNode obj = (DownloadNode)node.getUserObject(); //ᕫࠄ㡖⚍ᇍ䈵 DefaultMutableTreeNode node = (DefaultMutableTreeNode)value; //ᕫࠄᷥ㡖⚍ expanded, leaf, row, hasFocus); super.getTreeCellRendererComponent(tree, value, sel, boolean hasFocus) { boolean sel, boolean expanded, boolean leaf, int row, public Component getTreeCellRendererComponent(JTree tree, Object value, ҷⷕ⏙ऩ˖code\flashget\src\org\crazyit\flashget\ui\NavigationTreeCellRender.java˖ ˄DownloadNode ⱘᅲ⦄㉏˅ˈݡḍ᥂䖭ѯᇍ䈵ᕫࠄ᭛ᴀϢ೒ᷛDŽ ೼߯ᓎᇐ㟾ᷥⱘᯊ׭ˈህৃҹЎᇐ㟾ᷥ䆒㕂␆ᶧ఼DŽ೼ᷥⱘ␆ᶧ఼Ёˈ៥ӀৃҹᕫࠄⳌᑨⱘ㡖⚍ᇍ䈵 DownloadingNode 㸼⼎ϔϾℷ೼ϟ䕑ⱘ㡖⚍ˈҹৠḋⱘᮍ⊩ˈᮄ๲݊Ҫ㡖⚍ᇍᑨⱘᅲ⦄㉏ˈ䙷М } } return "ℷ೼ϟ䕑"; public String getText() { } return ImageUtil.DOWNLOADING_NODE_IMAGE; public ImageIcon getImageIcon() { public class DownloadingNode implements DownloadNode { /Download at http://www.pin5i.com ֵᙃˈֱᄬ߫㸼᭄᥂ㄝDŽҹϟᰃ䆹 TableModel ㉏ⱘ䚼ߚᅲ⦄DŽ ᦤկϔϾ TableModel ᴹ䋳䋷໘⧚߫㸼Ёⱘ᭄᥂DŽᴀゴ᠔ᦤկⱘ TableModelˈࣙᣀњໄᯢϟ䕑߫㸼ⱘ߫ ⱘব࣪ⱘˈ಴ℸ៥ӀৃҹذDownloadTable ᑊϡ䋳䋷໘⧚᭄᥂DŽ೼ϟ䕑ⱘ䖛⿟Ёˈ䆌໮ⱘ᭄᥂䛑ᰃ೼ϡ 䰸њ䖯ᑺᴵ߫П໪ˈ݊Ҫⱘ߫䛑ᰃՓ⫼ DownloadTableCellRenderer ԰Ў߫ⱘ␆ᶧ఼DŽ೼ᴀゴЁˈ …ⳕ⬹݊Ҫ߫ new DownloadProgressBar()); this.getColumn(DownloadTableModel.PLAN_COLUMN).setCellRenderer( new DownloadTableCellRenderer()); this.getColumn(DownloadTableModel.STATE_COLUMN).setCellRenderer( Ḑ␆ᶧܗ //䆒㕂ऩ ҷⷕ⏙ऩ˖code\flashget\src\org\crazyit\flashget\ui\DownloadTable.java Ḑ␆ᶧ఼DŽܗ೼ DownloadTable Ё䆒㕂ऩ ߫㸼ᇍᑨⱘᰃ DownloadTable ㉏ˈ䆹㉏㒻ᡓѢ JTableˈDownloadTable ㉏Џ㽕䋳䋷䆒㕂߫㸼ⱘḋᓣDŽ ߯ᓎњϸϾ␆ᶧ఼ৢˈ៥Ӏৃҹ೼߯ᓎ߫㸼ⱘᯊ׭ˈᇚᇍᑨⱘ߫䆒㕂Ⳍᑨⱘ␆ᶧ఼DŽ೼ᴀゴˈϟ䕑 } return this; this.setToolTipText(value.toString()); this.setHorizontalAlignment(JLabel.CENTER); //䆒㕂ሙЁ else setBackground(table.getBackground()); if (isSelected) super.setBackground(table.getSelectionBackground()); //߸ᮁᰃ৺䗝Ё else this.setText(value.toString()); if (value instanceof Icon) this.setIcon((Icon)value); //߸ᮁᰃ৺䳔㽕ᰒ⼎೒⠛ boolean isSelected, boolean hasFocus, int row, int column) { public Component getTableCellRendererComponent(JTable table, Object value, ҷⷕ⏙ऩ˖code\flashget\src\org\crazyit\flashget\ui\DownloadTableCellRenderer.java˖ ␆ᶧDŽ ᰃϔϾ䖯ᑺᴵˈৠᰃгᰃϔϾ߫㸼ⱘ␆ᶧ఼DŽϟ䴶ᮄᓎ݊Ҫ߫ⱘ␆ᶧ఼ˈ䆹␆ᶧ఼䋳䋷߫㸼Ё݊Ҫ߫ⱘ ҹϞⱘ DownloadProgressBar ㉏ˈ㒻ᡓѢ JProgressBar ᑊᅲ⦄ TableCellRendererˈ㸼⼎䖭Ͼ㉏ } } return this; this.setString(value.toString() + " %"); this.setValue(intValue); int intValue = (int)floatValue.floatValue(); Float floatValue = Float.parseFloat(value.toString()); boolean isSelected, boolean hasFocus, int row, int column) { public Component getTableCellRendererComponent(JTable table, Object value, } this.setForeground(Color.green); this.setStringPainted(true); super(0, 100); public DownloadProgressBar() { TableCellRenderer { public class DownloadProgressBar extends JProgressBar implements ҷⷕ⏙ऩ˖code\flashget\src\org\crazyit\flashget\ui\DownloadProgressBar.java˖ Ҫⱘ߫߭Փ⫼ৠϔϾ␆ᶧ఼DŽҹϟᰃ䖯ᑺᴵ␆ᶧ఼ⱘᅲ⦄DŽ݊ /Download at http://www.pin5i.com ೒ 11.3 ϟ䕑߫㸼ᬜᵰ  ⼎DŽ 㒣߯ᓎˈ៥Ӏৃҹ೼ DownloadTableModel ᰃᮄᓎϔѯ⌟䆩᭄᥂ᴹᶹⳟ݋ԧⱘᬜᵰDŽᬜᵰབ೒ 11.3 ᠔ ᗕ߫ˈ៥Ӏ䳔㽕䖨ಲϔϾ೒ᷛᇍ䈵ˈ䖯ᑺᴵ߫䳔㽕䖨ಲϔϾ float ؐDŽࠄℸˈ᭄᥂߫㸼ⱘ৘Ͼᇍ䈵䛑Ꮖ ᮍ⊩ˈ݊Ё getRowCount ᮍ⊩䖨ಲ᭄᥂䆄ᔩ᭄ˈgetValueAt ᮍ⊩䖨ಲ↣ϔ߫Ⳍᑨⱘ᭄᥂DŽ՟བ䌘⑤⢊ Ӏ䳔㽕䞡ݭ DefaultTableModel ⱘ getRowCountǃgetColumnNameǃgetColumnCount ੠ getValueAt DownloadTableModel 㒻ᡓѢ DefaultTableModel ㉏ˈҹϞҷⷕⱘ columnNames ᰃ߫ৡ᭄㒘ˈ៥ } return super.getValueAt(row, column); //ᇍ߫ৡ䖯㸠߸ᮁˈݡ䖨ಲⳌᑨⱘ᭄᥂ String columnName = this.getColumnName(column); public Object getValueAt(int row, int column) { } return columnNames.length; public int getColumnCount() { } return columnNames[col]; public String getColumnName(int col) { } return 0; public int getRowCount() { ҷⷕ⏙ऩ˖code\flashget\src\org\crazyit\flashget\ui\DownloadTableModel.java˖ /Download at http://www.pin5i.com 㒓⿟᭄DŽᮄᓎ⬠䴶ⱘᬜᵰབ೒ 11.4 ᠔⼎DŽ ֱᄬⱘ᭛ӊৡˈ঺໪ˈ⬅Ѣᴀゴ㓪ݭⱘᰃϔϾ໮㒓⿟ϟ䕑Ꮉ݋ˈ಴ℸ䖬㽕ᦤկϟᢝḚ䅽⫼᠋䗝ᢽϟ䕑ⱘ ᮄᓎϟ䕑⬠䴶Џ㽕ᰃ䅽⫼᠋ᮄᓎϔϾϟ䕑䌘⑤ˈϔϾᮄⱘϟ䕑䌘⑤ࣙᣀ䌘⑤ϟ䕑ഄഔǃֱᄬⳂᔩ੠ 11.2.5 ᮄᓎϟ䕑ӏࡵ⬠䴶 ᥂ˈ៥Ӏ೼ϟ䴶ⱘゴ㡖ЁᇚӮᅲ⦄DŽ 䌘⑤ֵᙃᰒ⼎䛑䕗Ўㅔऩˈ៥Ӏ೼ࠡ䴶ⱘḜ՟Ё䛑ᦤկњϡৠⱘᅲ⦄DŽԚᰃϟ䕑߫㸼Ёᑊ≵᳝ᅲ䰙ⱘ᭄ ࠄℸˈЏ⬠䴶ⱘ৘Ͼᇍ䈵Ꮖ㒣ᓎゟˈЏ⬠䴶ⱘϝϾऎඳЁˈা᳝䌘⑤߫㸼ᅲ⦄䕗Ў໡ᴖˈᇐ㟾ᷥǃ } this.infoJList.setListData(infoList.toArray()); this.infoList.add(this.info); this.infoList.add(this.downloadDate); this.infoList.add(this.filePath); this.infoList.add(this.fileSize); //ࡴܹ৘Ͼ Info ᇍ䈵 this.infoJList = new JList(); private void createList() { ҷⷕ⏙ऩ˖code\flashget\src\org\crazyit\flashget\ui\MainFrame.java˖ JList ⱘҷⷕDŽ 䙷М೼߯ᓎ JList ⱘᯊ׭ˈ៥Ӏৃҹ৥ JList Ёࡴܹ໮Ͼⱘ Info ᇍ䈵DŽҹϟᰃ೼ MainFrame Ё߯ᓎ } private String value; //ᇍᑨⱘؐ public class Info { ҷⷕ⏙ऩ˖code\flashget\src\org\crazyit\flashget\info\Info.java˖ ㋴DŽܗᓎゟϔϾ Info ⱘ㉏ᴹ㸼⼎ JList ЁⱘϔϾ ᇍ䈵DŽ ᄬⱘ䏃ᕘǃϟ䕑ᯊ䯈Ϣ䌘⑤⢊ᗕDŽ៥ӀՓ⫼ϔϾ JList ᇍ䈵ᴹᅲ⦄ֵᙃᰒ⼎ऎˈ೼ MainFrame Ё߯ᓎ䆹 ՟བᰒ⼎䌘⑤ϟ䕑ⱘ㒱ᇍ䏃ᕘˈϟ䕑ᯊ䯈ㄝDŽ೼ᴀゴЁˈ䆹ऎඳা䋳䋷ᰒ⼎᭛ӊ໻ᇣǃ᭛ӊֱˈܙϾ㸹 ݊ᅲ೼䌘⑤߫Ёˈ៥ӀᏆ㒣ৃҹⳟࠄ䌘⑤ⱘ໮᭄ֵᙃˈ䌘⑤ֵᙃᰒ⼎ऎඳৃҹ԰Ўᇍ䌘⑤߫㸼ⱘϔ 11.2.4 䌘⑤ֵᙃᰒ⼎ DownloadTableModel ЁࡴܹⳌᑨⱘ᭄᥂䲚ড়ेৃDŽ ϟ䕑߫㸼ⱘᬜᵰЁˈ៥Ӏⱘ᭄᥂䛑ᰃ㰮ᢳⱘˈབᵰ៥Ӏҹৢ䳔㽕ࡴܹⳳᅲⱘ᭄᥂ˈা䳔㽕೼ /Download at http://www.pin5i.com  ⍂にষⱘ݋ԧᬜᵰབ೒ 11.5 ᠔⼎DŽ } g.drawImage(img, 0, 0, this); public void paint(Graphics g) { } this.setVisible(true); this.setAlwaysOnTop(true); //䆒㕂䆹にষᘏᰃ೼ࠡ this.setLocation(locationX, locationY); int locationY = screen.height - (int)(screen.height / 1.06); int locationX = screen.width - screen.width / 5; //䆒㕂にষԡ㕂 this.setSize(36, 36); //䆒㕂にষ໻ᇣ Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); //ᕫࠄሣᐩ໻ᇣ this.mainFrame = mainFrame; public SuspendWindow(MainFrame mainFrame) { ҷⷕ⏙ऩ˖code\flashget\src\org\crazyit\flashget\ui\SuspendWindow.java˖ ⱘ໻ᇣˈᑊ䞡ݭ⠊㉏ⱘ paint ᮍ⊩ˈᇚⳌᑨⱘ೒⠛Ā⬏āࠄ JWindow ЁDŽ ᅲ⦄ϔϾ ⍂にষˈৃҹ㒻ᡓ Swing Ёⱘ JWindow ㉏ˈՓ⫼ setSize ᮍ⊩ᴹ䆒㕂䆹 JWindow ᇍ䈵 11.2.6  ⍂にষ Ⳍ݇ⱘᦤ⼎DŽ ׭ˈ៥Ӏህ䳔㽕ҢĀϟ䕑ഄഔāϢĀ঺ᄬ᭛ӊৡāЁᕫࠄ᳔㒜ⱘ᭛ӊৡDŽབᵰϡ㛑ᕫࠄ᭛ӊৡˈ߭䖯㸠 ഔⱘᯊ׭ˈ៥Ӏ䳔㽕㓪ݭ⿟ᑣ៾পⳌᑨⱘ᭛ӊৡˈ԰ЎĀ঺ᄬ᭛ӊৡāⱘؐˈᔧ⫼᠋᳔㒜⚍ߏ⹂ᅮⱘᯊ ᮄᓎϟ䕑⬠䴶ᅲ⦄䕗Ўㅔऩˈাᰃϔѯ᱂䗮ⱘ⬠䴶᥻ӊˈ݊Ҫ䳔㽕⊼ᛣⱘᰃˈᔧ⫼᠋䕧ܹњϟ䕑ഄ ೒ 11.4 ᮄᓎϟ䕑⬠䴶  /Download at http://www.pin5i.com ᷛሲᗻˈᔧ⫼᠋ᇍにষ䖯㸠ᢪࡼⱘᯊ׭ݡᇍにষⱘԡ㕂䖯㸠ⳌᑨⱘᬍবDŽ Ў ⍂にষࡴܹњে䬂㦰ऩৢˈ䖬䳔㽕ᅲ⦄ ⍂にষⱘᢪࡼDŽ៥Ӏা㽕೼にষᇍ䈵Ёֱᄬにষⱘത 䙷Мᓎ䆂Փ⫼᳈催⠜ᴀⱘ JDKDŽ ⠜ᴀᰃ 1.6.0 ᯊˈ䙷Мে䬂㦰ऩᇚӮϡ㛑ߎ⦄ˈ䖭ᰃ䆹⠜ᴀⱘϔϾ Bugˈབᵰ䇏㗙Փ⫼ⱘᰃ 1.6.0 ⠜ᴀˈ ҹϞҷⷕⱘ咥ԧ䚼ߚᰒ⼎ে䬂㦰ऩDŽ೼ JWindow ЁՓ⫼ JPopupMenu ㉏ⱘᯊ׭ˈབᵰᔧࠡⱘ Java }); } } popupMenu.show(SuspendWindow.this, e.getX(), e.getY()); if (e.isPopupTrigger()) { public void mouseReleased(MouseEvent e) { this.addMouseListener(new MouseAdapter() { ҷⷕ⏙ऩ˖code\flashget\src\org\crazyit\flashget\ui\SuspendWindow.java˖ ߯ᓎњ㦰ऩৢˈ៥Ӏ䳔㽕Ўにষࡴܹ哴ᷛⲥ਀఼ˈᔧ⫼᠋䖯㸠ে䬂⚍ߏⱘᯊ׭ˈህ㽕ᇚ㦰ऩᰒ⼎DŽ } ... this.popupMenu.add(this.openItem); //ࡴܹ㦰ऩ private void createPopupMenu() { ҷⷕ⏙ऩ˖code\flashget\src\org\crazyit\flashget\ui\SuspendWindow.java˖ ⦄哴ᷛⱘে䬂㦰ऩˈJPopupMenu Ёᄬᬒ৘Ͼ JMenuItemDŽ ᓎゟњ ⍂にষৢˈ៥Ӏ䖬㽕Ў䆹にষࡴܹⳌ݇ⱘে䬂㦰ऩˈ៥ӀৃҹՓ⫼ JPopupMenu ㉏ᴹᅲ ೒ 11.5  ⍂にষ  /Download at http://www.pin5i.com Ⳍ݇ᇍ䈵DŽ ᓎˈ䖭ѯ⬠䴶㒘ӊᅲ⦄ 䕑ⱘ ࠄℸˈ໮㒓⿟ϟ䕑Ꮉ݋ⱘ⬠䴶ܼ䚼߯ Ⳍᇍ䕗Ўㅔऩˈ᥹ϟᴹˈ៥Ӏᇚ䆒䅵ϟ ೒ 11.7 ࡴܹ㋏㒳ӏࡵᷣ  Swing ⱘ JPopupMenu ԰Ўᔍߎ㦰ऩDŽࡴܹӏࡵᷣৢⱘᬜᵰབ೒ 11.7 ᠔⼎DŽ ⬅Ѣ TrayIcon াᬃᣕՓ⫼ awt ⱘ PopupMenu ԰Ў哴ᷛে䬂ᔍߎ㦰ऩˈ಴ℸ៥Ӏ೼䖭䞠≵᳝Փ⫼ tray.add(this.trayIcon); this.trayIcon.setToolTip("໮㒓⿟ϟ䕑Ꮉ݋"); this.trayIcon = new TrayIcon(trayIconImage, "໮㒓⿟ϟ䕑Ꮉ݋", this.popupMenu); SystemTray tray = SystemTray.getSystemTray(); ҷⷕ⏙ऩ˖code\flashget\src\org\crazyit\flashget\ui\MainFrame.java˖ Փ⫼䆹㉏ৃҹЎӏࡵᷣⱘ೒ᷛࡴܹ㦰ऩǃᦤ⼎᭛ᴀㄝֵᙃˈҹϟҷⷕ߯ᓎӏࡵ೒ᷛDŽ JDK1.6 ᦤկњ㋏㒳ᠬⲬ˄ӏࡵᷣ˅ⱘᬃᣕˈ៥ӀৃҹՓ⫼ TrayIcon ㉏ᴹ㸼⼎ϔϾӏࡵᷣⱘ೒ᷛˈ 11.2.7 ӏࡵᷣ೒ᷛ ៥ӀᏆ㒣߯ᓎњ ⍂にষⱘ㦰ऩˈ䖭ѯ㦰ऩⱘ݋ԧᅲ⦄೼ϟ䕑ⱘゴ㡖ЁᇚӮᦣ䗄DŽ ೒ 11.6  ⍂にষ᳔㒜ᬜᵰ  ⱘ setLocation ᮍ⊩䖯㸠ԡ㕂ⱘᬍবDŽᅲ⦄њ䖭ѯৢˈ ⍂にষⱘ᳔㒜ᬜᵰབ೒ 11.6 ᠔⼎DŽ  ⍂にষ䖯㸠⚍ߏⱘᯊ׭ˈህ䗮䖛ҹϞⱘ咥ԧҷⷕᴹ䆒䅵തᷛؐˈᔧ䖯㸠ᢪࡼⱘᯊ׭ˈህӮ䇗⫼ JWindow ҹϞҷⷕЁЎ哴ᷛࡴܹњ MouseMotionListener ⲥ਀఼ˈᅲ⦄њ mouseDragged ᮍ⊩ˈᔧ⫼᠋೼ } } y = e.getY(); x = e.getX(); public void mousePressed(MouseEvent e) { this.addMouseListener(new MouseAdapter() { }); } setLocation(xScreen - x, yScreen - y); int yScreen = e.getYOnScreen(); int xScreen = e.getXOnScreen(); //㦋ᕫᔧࠡ哴ᷛ೼ሣᐩЁⱘതᷛ public void mouseDragged(MouseEvent e) { this.addMouseMotionListener(new MouseMotionAdapter() { ҷⷕ⏙ऩ˖code\flashget\src\org\crazyit\flashget\ui\SuspendWindow.java˖ /Download at http://www.pin5i.com public String getState() { } return ImageUtil.DOWNLOADING_IMAGE; public ImageIcon getIcon() { ˖ҷⷕ⏙ऩ code\flashget\src\org\crazyit\flashget\state\Downloading.java˖ ೒ᷛϢ⢊ᗕ᭛ᴀDŽ ᮄᓎϔϾ Downloading ㉏ˈ㒻ᡓ AbstractStateˈ䆹㉏㸼⼎ℷ೼ϟ䕑ⱘ⢊ᗕDŽ䆹㉏䖨ಲⳌᑨⱘ⢊ᗕ ᵰ᳝䳔㽕ˈ߭ݡএᅲ⦄ init Ϣ destory ᮍ⊩DŽ 䈵Ўএᅲ⦄ ᮍinit Ϣ destory ᮍ⊩˄ぎᅲ⦄˅ˈ䙷Мᅗⱘᄤ㉏া䳔㽕ᅲ⦄ getIcon Ϣ getState ⊩ेৃˈབ ⢊ᗕ䛑䳔㽕ᅲ⦄ init Ϣ destory ᮍ⊩ˈ᠔ҹ೼᥹ষϢᅲ⦄㉏ЁࡴܹϔϾᢑ䈵㉏˄AbstractState˅ˈ䅽䆹ᢑ ೼ᴀゴЁˈϔϾ䌘⑤ৃҹ᳝໮ ℸ ϔϾ⢊ᗕࡴܹϡৠⱘᅲ⦄DŽ⬅Ѣᑊϡᰃ↣ϔ⾡ ⢊ᗕ೒ᷛ Task ⾡⢊ᗕˈ಴ 䳔㽕Ў↣ 䌘⑤㹿䆒㕂Ў঺໪ⱘ⢊ᗕˈ䙷М೼䆒㕂ࠡˈህ䳔㽕ᠻ㸠 destory ᮍ⊩ˈ㸼⼎䆹⢊ᗕ䫔↕DŽ TaskState Ё᳝ init Ϣ destory ᮍ⊩ˈབᵰϔϾ䌘⑤㹿䆒㕂Ўᶤ⾡⢊ᗕˈ䳔㽕ᠻ㸠 init ᮍ⊩ˈབᵰ䆹 㽕ᦤկϔϾ䖨ಲ ⱘᮍ⊩DŽ State ⱘ getState ᮍ⊩⫼Ѣ䖨ಲ䌘⑤⢊ᗕⱘᄫヺІDŽ ⑤߫㸼Ё᳝ϔ߫䌘⑤⢊ᗕˈ䆹߫ЁᇚӮᰒ⼎Ⳍᑨⱘ೒⠛ᴹ㸼⼎ᔧࠡ䌘⑤ⱘ⢊ᗕˈ಴ℸ೼ TaskState Ё䳔 TaskState ᥹ষЁ᳝ getIcon ᮍ⊩ˈ䆹ᮍ⊩䖨ಲ⢊ᗕⱘ೒⠛ˈ៥Ӏ೼ 11.2.3 Ё߯ᓎњ䌘⑤߫㸼ˈ䌘 void destory(Resource resouse); */ * 䆹⢊ᗕ㒧ᴳᯊᠻ㸠ⱘᮍ⊩ /** void init(Resource resource); */ * 䆹⢊ᗕ߱ྟ࣪ᠻ㸠ⱘᮍ⊩ /** String g ; etState() */ * @return * 䖨ಲ⢊ᗕⱘᄫヺІ /** ImageIcon getIcon(); */ * @return * 䖨ಲ䆹⢊ᗕϟⱘ೒⠛ /** ҷⷕ⏙ऩ˖code\flashget\src\org\crazyit\flashget\state\TaskState.java˖ ϔϾ Tas ϔϾ䌘⑤೼៥Ӏⱘϟ䕑 11.3.1 䌘⑤⢊ᗕᇍ䈵 䖬䳔㽕߯ᓎϔϾĀഫāᇍ䈵DŽ ⬅Ѣᴀゴᅲ⦄ⱘᰃϔϾ໮㒓⿟ϟ䕑Ꮉ݋ˈ಴ℸϔϾ䌘⑤ᇚӮ㹿ߚЎ໮Ͼഫ䖯㸠ϟ䕑ˈ᠔ҹ 㽕䆒䅵Ⳍ݇ⱘᇍ䈵ˈ՟བ㽕ϟ䕑ᶤϾ䌘⑤ kState ᥹ষᴹ㸼⼎䌘⑤ⱘ⢊ᗕˈTaskState ⱘᮍ⊩བϟDŽ ੠ϟ䕑༅䋹ㄝˈ៥ӀᮄᓎذᎹ݋Ё᳝໮⾡⢊ᗕˈࣙᣀϟ䕑Ёǃϟ䕑ᅠ៤ǃ᱖ Ⳍᑨⱘᇍ䈵DŽ ǃϟ䕑ᅠ៤ㄝ˅ˈ䙷М៥Ӏ䳔㽕Ў䖭ѯ⢊ᗕ߯ᓎذϔϾ݋ԧⱘᇍ䈵ˈ䆹ᇍ䈵᳝䆌໮ⱘ⢊ᗕ˄ϟ䕑Ёǃ᱖ ೼ᅲ⦄ϟ䕑ࡳ㛑ࠡˈ៥Ӏ䳔 ˈ៥Ӏৃҹᇚ䖭Ͼ䌘⑤ᢑ䈵៤ 䆒䅵ϟ䕑ⱘⳌ݇ᇍ䈵 11.3 /Download at http://www.pin5i.com //䌘⑤ϟ䕑ৢⱘৡ⿄ private String filePath; //䌘⑤ֱᄬ䏃ᕘ private String url; //䌘⑤ഄഔ private String id; //ᷛ䆚䆹䌘⑤ⱘ id ҷⷕ⏙ऩ˖code\flashget\src\org\crazyit\flashget\object\Resource.java˖ ೼ᴀゴЁՓ⫼ϔϾ Resource ᇍ䈵㸼⼎ϔϾ䌘⑤DŽ ᔧ⫼᠋䕧ܹњϟ䕑 11.3.3 䌘⑤ᇍ䈵 ⱘ᭛ӊৡˈ㗠Ϩ᭛ӊৢ㓔Ў.partDŽ ᦤկˈԚᰃ ᗻDŽ 䌘⑤ⱘ᠔ֵ᳝ᙃˈࣙᣀᷛ䆚䌘⑤ⱘ idǃ䖲᥹ഄഔǃֱᄬⳂᔩǃ᭛ӊৡ⿄ǃ䌘⑤໻ᇣㄝㄝϔ㋏߫Ⳍ݇ⱘሲ ഄഔㄝϟ䕑ֵᙃৢˈϔϾ䌘⑤ᇍ䈵ህ㽕ᄬ೼Ѣ៥Ӏⱘ㋏㒳Ёˈ䆹䌘⑤ᇍ䈵ࣙᣀњ ଃϔ ໪⬠ ϔϾഫᇍ䈵ⱘৡ⿄ˈህ䳔㽕⬅㞾Ꮕ䖯㸠߯ᓎˈҹϞҷⷕⱘ咥ԧ䚼ߚˈՓ⫼ UUID ᴹ⫳៤ ҹϞҷⷕ㸼⼎ϔϾഫˈ݊Ё䳔㽕⊼ᛣⱘᰃˈᓔྟϟ䕑ⱘԡ㕂ǃϟ䕑䭓ᑺϢᔧࠡⱘϟ䕑ԡ㕂䛑ৃҹ⬅ } this.partName = UUID.randomUUID() + ".part"; this.currentLength = currentLength; this.length = length; this.begin = begin; public Part(int begin, int length, int currentLength) { private String partName; //↣Ͼ Part ᇍᑨⱘ᭛ӊ private int currentLength; //Ꮖ㒣ϟ䕑ⱘ᭛ӊ䭓ᑺ private int length; //䖭Ͼ part ᭛ӊⱘ䭓ᑺ private int begin; //ϟ䕑ⱘᓔྟԡ㕂 ҷⷕ⏙ऩ˖code\flashget\src\org\crazyit\flashget\object\Part.java˖ Ͼ Part ᇍ䈵ᴹ㸼⼎ϔϾߚᢚⱘഫDŽ ᑺᰃ 0ˈ݊Ҫഫձℸ㉏᥼ˈབᵰ㒓⿟೼ᶤϾᯊ↉ϟ䕑њ 10ˈ䙷Мᔧࠡϟ䕑䭓ᑺህЎ 10DŽᴀゴЁՓ⫼ϔ ߚᢚ៤ 5 ӑˈ↣ϔӑ໻ᇣЎ 20ˈ䙷М㄀ϔഫⱘᓔྟϟ䕑ԡ㕂ህᰃ 0ˈϟ䕑䭓ᑺᰃ 20ˈᔧࠡϟ䕑䭓 100 ⱘ䌘⑤ˈ⫼᠋ ᔧϟ䕑Ꮉ݋ᕫࠄᶤϾ䌘⑤ⱘᯊ׭ˈ៥ህ䳔㽕ᇚ䖭Ͼ䌘⑤ᢚߚ៤㢹ᑆϾഫDŽ՟བˈ⦄೼᳝ϔϾ໻ᇣЎ 䆹ഫⱘ᭛ӊৡDŽ ᙃDŽϔϾഫ㸼⼎ⱘϟ䕑ֵᙃ᳝ˈ䆹ഫⱘϟ䕑ᓔྟԡ㕂ǃ䆹ഫ䳔㽕ϟ䕑ⱘ䭓ᑺǃᔧࠡϟ䕑ⱘ䭓ᑺҹঞ ᴀゴЁᅲ⦄ⱘᰃ 11.3.2 ഫᇍ䈵 䕑˄Down 䌘⑤ 䗝ᢽњՓ⫼ 5 ᴵ㒓⿟䖯㸠ϟ䕑˄㾕೒ 11.4ˈ䗝ᢽϟ䕑㒓⿟˅ˈ䙷Мህᛣੇⴔ㽕ᇚ䖭Ͼ 䕑ֵ ݡᠻ㸠㢹ᑆᴵ㒓⿟ˈ↣ϔᴵ㒓⿟䋳䋷ϟ䕑㞾Ꮕⱘഫˈ಴ℸ៥Ӏ䳔㽕ᓎゟϔϾഫⱘᇍ䈵ˈᴹֱᄬⳌ݇ⱘϟ ϔϾ໮㒓⿟ϟ䕑Ꮉ݋ˈ೼ᓔྟϟ䕑ⱘᯊ׭ˈ៥Ӏ䳔㽕ᇚϔϾ䌘⑤ᢚߚ៤㢹ᑆⱘഫˈ ϟ䕑˄Pause˅DŽذ೼ϟ loading˅ǃϟ䕑༅䋹˄Failed˅ǃϟ䕑ᅠ៤˄Finished˅੠᱖ Փ⫼ৠḋⱘᮍ⊩߯ᓎ݊Ҫ⢊ᗕ㉏ˈ೼ᴀゴЁˈ䌘⑤ⱘ⢊ᗕࣙᣀ˖ℷ೼䖯㸠䖲᥹˄Connecting˅ǃℷ } return "downloading"; /Download at http://www.pin5i.com //߸ᮁПࠡᰃ৺Ꮖ㒣প䖛᭛ӊ໻ᇣ URL resourceURL = new URL(this.url); //䖯㸠ϔ⃵᭛ӊ䖲᥹ public int getSize() { ҷⷕ⏙ऩ˖code\flashget\src\org\crazyit\flashget\object\Resource.java˖ ໻ᇣⱘᮍ⊩ᅲ⦄DŽ URLConnection ৃҹ⫳៤ HTTP 䇋∖ˈ៥Ӏ೼ᅲ⦄ϟ䕑ⱘᯊ׭ˈৠḋг䳔㽕䆹㉏DŽҹϟᰃ㦋ᕫ䌘⑤ 㸠䖲᥹Փ⫼ HttpURLConnection ㉏DŽᴀゴᅲ⦄ⱘᰃ HTTP ϟ䕑 ೼䖭䞠䳔㽕⊼ᛣⱘᰃˈབᵰ㽕ᕫࠄ᭛ӊⱘ໻ᇣ˄size ሲᗻ˅ˈ䳔㽕ᇍ䌘⑤䖯㸠ϔ⃵䖲᥹ˈᇍ䌘⑤䖯 এ䖯㸠䖲᥹ˈབᵰৃҹℷᐌ䖲᥹ˈ߭䳔㽕偀Ϟ䖯㸠ϟ䕑DŽ ܹњϟ䕑ֵᙃ⚍ߏ⹂ᅮⱘᯊ׭ˈህ䳔㽕ᇚ䖭Ͼ䌘⑤ⱘ⢊ᗕ䆒㕂Ў䖲᥹Ё˄Connecting˅ˈ㋏㒳ᑨ䆹偀Ϟ s ሲᗻᰃϔϾ䲚ড়ᇍ䈵ˈ㸼⼎䖭Ͼ䌘⑤䳔㽕ᢚߚⱘഫDŽᔧϔϾ䌘⑤䖯㸠ᵘ䗴ⱘᯊ׭ˈгህᰃ⫼᠋䕧 ⿟᭄ˈे⏏ Http ˈ಴ℸ䳔㽕⫼ࠄ HttpURLConnection ㉏ˈ part Ϣ㒓 ࡴϟ䕑ӏࡵ⬠䴶˄೒ 11.4˅ⱘ৘Ͼ⬠䴶㒘ӊؐˈ݊Ҫⱘሲᗻ⬅ᵘ䗴఼䖯㸠䆒㕂DŽ݊Ё ҹϞᰃ Resource ᇍ䈵ⱘሲᗻϢᵘ䗴఼ˈ݊Ё䆹ᇍ䈵ⱘᵘ䗴఼া᳝ᄫヺІ urlǃֱᄬⳂᔩǃ᭛ӊৡ⿄ } this.threadSize = threadSize; this.state = DownloadContext.CONNECTION; this.saveFile = new File(filePath + File.separator + fileName); this.parts = new ArrayList(); this.fileName = fileName; this.filePath = filePath; this.url = url; this.id = UUID.randomUUID().toString(); public Resource(String url, String filePath, String fileName, int threadSize) { private int preLength; //Ϟϔ⃵ϟ䕑ⱘ໻ᇣ private int threadSize; //ϟ䕑ⱘ㒓⿟᭄ private List parts; //᭛ӊ᠔ᢚߚⱘഫ private int spareTime; //࠽ϟⱘᯊ䯈 private int costTime; //Փ⫼ⱘᯊ䯈 private float speed; //ϟ䕑䗳ᑺ private float progress; //䖯ᑺ private Date downloadDate; //ϟ䕑᮹ᳳ private int size = -1; //᭛ӊ໻ᇣ private TaskState state; //⢊ᗕ private File saveFile; //䌘⑤ϟ䕑ৢⱘ᭛ӊᇍ䈵 private String sourceName; //䌘⑤ৡ⿄ private String fileName; /Download at http://www.pin5i.com } return (spareSize / (int)this.speed) / 1000; if (this.speed == 0) return this.spareTime; int spareSize = getSize() - getCurrentLength(); //ᕫࠄ࠽ԭ䭓ᑺ public int getSpareTime() { //ᕫࠄ࠽ԭᯊ䯈 } return result; } result += p.getCurrentLength(); for (Part p : this.parts) { int result = 0; public int getCurrentLength() { //ᕫࠄ䆹䌘⑤Ꮖ㒣ϟ䕑ⱘ䭓ᑺ(䅵ㅫ᠔᳝ഫⱘ䭓ᑺ) } return speed; speed = Math.round(speed * 100) / 100.0f; preLength = currentLength; //ᇚᔧࠡϟ䕑ⱘ䭓ᑺ䆒㕂Ўࠡϔ⃵ϟ䕑ⱘ䭓ᑺ(ᴀ⃵䗳ᑺ䅵ㅫᏆ㒣ᅠ៤) speed = (currentLength - preLength) / 1024.0f; এࠡϔ⃵ϟ䕑ⱘ䭓ᑺ, ᕫࠄᘏϟ䕑䞣ᑊ䅵ㅫߎ䗳ᑺޣ //ᇚᔧࠡϟ䕑ⱘ䭓ᑺ int currentLength = getCurrentLength(); //ᕫࠄᔧࠡ᠔᳝ഫϟ䕑ⱘ໻ᇣ public float getSpeed() { //䖨ಲϟ䕑䗳ᑺ, 䳔㽕ᕫࠄܼ䚼Ꮖϟ䕑ⱘ䭓ᑺ } return progress; this.progress = Math.round(100.0f * getCurrentLength() / getSize()); public float getProgress() { //䖨ಲϟ䕑ⱘ䖯ᑺ ҷⷕ⏙ऩ˖code\flashget\src\org\crazyit\flashget\object\Resource.java˖ ߭ᰃܼ䚼ഫᏆϟ䕑ⱘ䭓ᑺᘏ੠DŽҹϟᰃ Resource 䖨ಲ䖭޴Ͼሲᗻⱘᅲ⦄DŽ ᰃ໮㒓⿟ϟ䕑Ꮉ݋ˈ↣ϔᴵ㒓⿟䛑Ӯ৥ϔϾĀഫāݭܹϟ䕑ⱘֵᙃˈ䙷МϔϾ䌘⑤Ꮖϟ䕑ⱘ䭓ᑺˈ ᯊ䯈˅ৠḋгᰃ䳔㽕⬅ Resource ㉏䖯㸠䅵ㅫˈ঺໪ˈ៥Ӏ䖬䳔㽕ᕫࠄ䆹䌘⑤Ꮖ㒣ϟ 䰸њ size ሲᗻ໪ˈprogress ሲᗻ˄ϟ䕑䖯ᑺ˅ǃspeed ሲᗻ˄ϟ䕑䗳ᑺ˅੠ spareTime ሲᗻ˄࠽ԭ 䌘⑤ⱘ໻ᇣৢˈ䳔㽕䇗⫼ HttpURLConnection ⱘ disconnect ᮍ⊩݇䯁䖲᥹DŽ 䖲᥹ˈ䖭䙓 ϔϾ 䕑ⱘ䭓ᑺDŽ⬅Ѣ䖭 ᕫࠄ њ↣⃵䇗⫼ getSize ᮍ⊩ⱘᯊ׭ˈݡএᇍ䌘⑤䖯㸠ϔ⃵䖲᥹DŽ䖭䞠䳔㽕⊼ᛣⱘᰃˈᔧܡ 䖯㸠 ҹϞⱘҷⷕЁˈЎњ㛑ᦤछᗻ㛑ˈ䳔㽕߸ᮁ size ሲᗻⱘؐˈབᵰ䆹ሲᗻᏆ㒣㹿䌟ؐˈ䙷МᇚϡӮݡ } return this.size; } urlConnection.disconnect(); //পᕫ᭛ӊ໻ᇣৢ䖨ಲ this.size = urlConnection.getContentLength(); //ᕫࠄ䌘⑤໻ᇣ urlConnection.connect(); HttpURLConnection urlConnection = (HttpURLConnection)resourceURL.openConnection(); //߯ᓎ HTTP 䖲᥹ᇍ䈵 if (this.size == -1) { /Download at http://www.pin5i.com return getResources(FINISHED); public List getFinisheds() { } return getResources(DOWNLOADING); public List getDownloadings() { } return getResources(FAILED); public List getFaileds() { ҷⷕ⏙ऩ˖code\flashget\src\org\crazyit\flashget\DownloadContext.java˖ ড় 㸠䖛Ⓒˈҹϟ৘ᮍ⊩䖨ಲ৘⾡⢊ᗕⱘ䌘⑤DŽ DownloadContext Ёֱᄬњ䌘 ⑤䲚 䖯 ⑤䲚ড়ˈ಴ℸབᵰ៥Ӏ䳔㽕ᕫࠄᶤ⾡⢊ᗕⱘ䌘⑤ᯊˈህ䳔㽕ᇍ䖭Ͼ䌘 public List resources = new ArrayList(); //ᔧࠡϟ䕑⿟ᑣЁ᠔᳝ⱘӏࡵ public static Pause PAUSE = new Pause(); public static Finished FINISHED = new Finished(); public static Failed FAILED = new Failed(); public static Downloading DOWNLOADING = new Downloading(); public static Connecting CONNECTION = new Connecting(); //ϟ䕑䌘⑤⢊ᗕⱘ৘Ͼᅲ⦄㉏ public static final int MAX_THREAD_COUNT = 5; //᳔໻㒓⿟᭄ ҷⷕ⏙ऩ˖code\flashget\src\org\crazyit\flashget\DownloadContext.java˖ ៥Ӏ䳔㽕Ўϟ䕑Ꮉ݋ 11.3.4 Ϟϟ᭛ᇍ䈵 ᮄ⢊ᗕⱘ init ᗕǃ᳔໻㒓⿟㒘ㄝֵᙃDŽ ᓎゟϔϾϞϟ᭛ᇍ䈵ˈ䆹ᇍ䈵ֱᄬϔѯ݀݅ⱘֵᙃˈ՟བ䌘⑤䲚ড়ǃ䌘⑤ⱘ⢊ ᠻ㸠 ᮍ⊩DŽ ߸ᮁ䌘⑤ᰃ৺㹿䆒㕂њϡৠ⢊ᗕˈݡᠻ㸠ॳᴹ⢊ᗕⱘ destory ᮍ⊩ˈ᳔ৢܜˈҹϞҷⷕⱘ咥ԧ䚼ߚ } this.state = state; } } state.init(this); //ᠻ㸠ᮄ⢊ᗕⱘ init ᮍ⊩ this.state.destory(this); //ᠻ㸠ॳᴹ⢊ᗕⱘ䫔↕ᮍ⊩ //ϸϾ⢊ᗕϔ㟈, ϡ䳔㽕䖯㸠ӏԩࡼ԰, ϡϔ㟈, ᠻ㸠䆹⢊ᗕⱘᮍ⊩ if (!this.state.equals(state)) { //߸ᮁখ᭄ⱘ⢊ᗕϢᴀᇍ䈵ⱘ⢊ᗕᰃ৺ϔ㟈 if (this.state != null) { public void setState(TaskState state) { ҷⷕ⏙ऩ˖code\flashget\src\org\crazyit\flashget\object\Resource.java˖ ྟ࣪Ϣ䫔↕DŽ䙷М៥Ӏ䳔㽕೼ Resource ᇍ䈵ⱘ setState Ё䇗⫼ init Ϣ destory ᮍ⊩DŽ 㸼⼎ˈᑊϨ䆹᥹ষ䛑᳝Ⳍᑨ Resource ᇍ䈵Ё᳝ϔϾ state ሲᗻᴹ㸼⼎䌘⑤ⱘ⢊ᗕˈᴀゴЁ䌘⑤ⱘ⢊ᗕՓ⫼њ TaskState ᥹ষᴹ ㅫᔧࠡⱘϟ䕑䗳ᑺDŽ Resource ⱘ߱ ⢊ᗕⱘᅲ⦄㉏ˈ೼ TaskState Ё៥ӀᅮНњ init Ϣ destory ᮍ⊩ˈ㸼⼎⢊ᗕ ᴹ䅵 এࠡϔ⃵ⱘϟ䕑䞣ޣ䗮䖛 ᇍ䈵ⱘ preLength ሲᗻᴹֱᄬࠡϔ⃵ϟ䕑䞣ˈݡՓ⫼ᴀ⃵ⱘϟ䕑䞣 ҹϞҷⷕⱘ getCurrentLength ᮍ⊩ˈ䅵ㅫ᠔᳝ഫⱘᏆϟ䕑ᘏ੠DŽgetSpeed ᮍ⊩䅵ㅫϟ䕑ⱘ䗳ᑺˈ /Download at http://www.pin5i.com //⫼᠋⚍ߏ⹂ᅮᯊ㾺থⱘᮍ⊩ } return saveFileName; } saveFileName = FileUtil.getFileName(url); if (saveFileName == null || saveFileName.equals("")) { //བᵰֱᄬⱘ᭛ӊৡ⿄Ўぎ, ߭Ң url Ё៾প᭛ӊৡ⿄ String saveFileName = this.saveFileName.getText(); String url = this.address.getText(); private String getSaveFileName() { //䖨ಲ䳔㽕ֱᄬⱘ᭛ӊৡ ҷⷕ⏙ऩ˖code\flashget\src\org\crazyit\flashget\ui\NewTaskFrame.java˖ ៥Ӏ೼ 11.2.5 Ёᓎゟњᮄᓎϟ 11.4.1 ⬠䴶 ᵰ᭛ӊЁDŽ Part ᇍ䈵ˈݡЎ↣ϔϾ Part ᇍ䈵ਃࡼϔᴵϟ䕑ⱘ㒓⿟ˈϟ䕑ᅠৢˈݡᇚ䖭 5 Ͼ Part ᇍ䈵ᬒࠄϟ䕑ⱘ㒧 ⱘ㒓⿟᭄ᴹᇍϟ䕑ⱘ䌘⑤䖯㸠ߚഫˈ⫼᠋䗝ᢽњ 5 ᴵ㒓⿟ˈ䙷Мህ䳔㽕Ў Resource ᇍ䈵⏏ࡴ 5 Ͼ ࠄ ᇍ䈵ेৃDŽҹϟᰃᮄ๲ϟ䕑䌘⑤⬠䴶ⱘ݋ԧᅲ⦄DŽ 䕑䌘⑤ⱘ⬠䴶ˈা䳔㽕ᕫࠄ⫼᠋ⱘ⬠䴶䕧ܹˈݡᇕ㺙៤ϔϾ Resource ᮄ๲ϟ䕑䌘⑤ 䗝ᢽ Ͼ Resource ᇍ䈵ˈ⏏ࡴࠄϞϟ᭛ᇍ䈵ⱘ䲚ড়ЁेৃDŽᮄᓎϔϾ Resource ⱘᯊ׭ˈ៥Ӏ䳔㽕ḍ᥂⫼᠋ ೼ 11.3.3 Ё߯ᓎњ䌘⑤ᇍ䈵ˈᅲ⦄䌘⑤ⱘϟ䕑ˈা䳔㽕䗮䖛 11.2.5 Ёⱘ⏏ࡴ䌘⑤ϟ䕑⬠䴶ᮄᓎϔ 11.4 ϟ䕑䌘⑤ ℸˈϟ䕑Ꮉ݋ⱘ৘Ͼᇍ䈵Ꮖ㒣䆒䅵ᅠ៤ˈϟ䴶ⱘゴ㡖Ёˈᇚҟ㒡བԩᅲ⦄䌘⑤ⱘϟ䕑ϢഫⱘߚᢚDŽ 䙷Мབᵰ䳔㽕Փ⫼ DownloadContext ⱘ৘Ͼᮍ⊩ᯊˈৃҹՓ⫼ ContextHolder.ctx.XXX 䇁হDŽ } public static DownloadContext ctx = new DownloadContext(); //ϟ䕑Ꮉ݋Ϟϟ᭛ public class ContextHolder { ҷⷕ⏙ऩ˖code\flashget\src\org\crazyit\flashget\ContextHolder.java˖ ContextHolder ⱘ㉏ᴹֱᄬ DownloadContext ⱘଃϔᅲ՟DŽ nloadContext ⱘᮍ⊩ˈህᖙ乏ᅲ՟࣪䖭Ͼ㉏DŽЎњֱ䆕䖭Ͼ㉏া᳝ϔϾᅲ՟ˈৃҹᓎゟϔϾ ˈ៥Ӏৃ Dow ҷⷕ ҹⳟࠄˈDownloadContext ⱘ৘Ͼᮍ⊩䛑ϡᰃ䴭ᗕⱘˈгህᰃᛣੇⴔ㽕Փ⫼ བᵰ㽕ᕫࠄℷ೼ϟ䕑ⱘ䌘⑤ˈ䇗⫼ DownloadContext Ёⱘ getDownloadings ᮍ⊩ेৃDŽ䗮䖛ҹϞ } return result; } } result.add(r); if (state.getState().equals(r.getState().getState())) { for (Resource r : resources) { //䘡ग़䌘⑤䲚ড়, ߸ᮁⳌᑨⱘ⢊ᗕ List result = new ArrayList(); private List getResources(TaskState state) { { private void confirm() { if (getSaveFileName() == null) { this.warnLabel.setText("䇋䕧ܹℷ⹂ⱘ᭛ӊৡ"); return; } this.warnLabel.setText(" "); Resource r = createResource(); ContextHolder.ctx.resources.add(r); this.setVisible(false); } //ḍ᥂⬠䴶䕧ܹ߯ᓎ Resource ᇍ䈵 private Resource createResource() { String url = this.address.getText(); String filePath = this.target.getText(); String fileName = getSaveFileName(); int threadCount = (Integer)this.threadCount.getSelectedItem(); return new Resource(url, filePath, fileName, threadCount); } ҹϞҷⷕⱘ咥ԧ䚼ߚˈ߯ᓎњ Resource ᇍ䈵ৢˈህ䆹ᇍ䈵ࡴܹࠄϟ䕑Ꮉ݋ⱘϞϟ᭛ЁˈҹϞҷⷕ Ёⱘ ⊩⬅⫼᠋⚍ߏ⹂ᅮᯊ㾺থDŽᇚᮄⱘ䌘⑤ᬒࠄϞϟ᭛˄DownloadContext˅Ёৢˈ䙷Мϟ 䕑߫ confirm ᮍ 㸼ህৃҹḍ᥂Ϟϟ᭛Ёⱘ䌘⑤䲚ড়ˈᇍ䌘⑤߫㸼䖯㸠␆ᶧDŽ ҷⷕ⏙ऩ˖code\flashget\src\org\crazyit\flashget\ui\DownloadTableModel.java˖ public DownloadTableModel() { super(); this.resources = ContextHolder.ctx.resources; } //ᕫࠄ䆄ᔩᘏ᭄ public int getRowCount() { if (this.resources != null) { return this.resources.size(); } else { return 0; } } public Object getValueAt(int row, int column) { Resource r = this.resources.get(row); if (r == null) return super.getValueAt(row, column); String columnName = this.getColumnName(column); if (columnName.equals(STATE_COLUMN)) { return r.getState().getIcon(); } else if (columnName.equals(FILE_NAME_COLUMN)) { return r.getFileName(); } else if (columnName.equals(FILE_SIZE_COLUMN)) { return r.getSize() / 1024 + " k"; } else if (columnName.equals(PLAN_COLUMN)) { return r.getProgress(); } else if (columnName.equals(SPEED_COLUMN)) { return r.getSpeed() + " k/s"; } else if (columnName.equals(HAS_DOWN_COLUMN)) { return r.getCurrentLength() / 1024; Download at http://www.pin5i.com/ /Download at http://www.pin5i.com ᑊ䖨ಲ䌘⑤ⱘ໻ᇣˈ಴ℸ៥Ӏৃҹ೼೒ 11.8 ЁⳟࠄĀ᭛ӊ໻ᇣāϔ߫Ў䌘⑤ⱘ໻ᇣDŽ getSize 䖲᥹ ᗕЎ䖲᥹Ё⢊ᗕDŽ೼ Resource ᇍ䈵ЁˈᦤկњϔϾ ⱘᮍ⊩˄11.3.3˅ˈ䆹ᮍ⊩⫼Ѣ䖯㸠 HTTP Resource ᇍ䈵೼ᵘ䗴ⱘᯊ׭ˈህ䆒ᅮ䌘⑤⢊ᗕЎ䖲᥹Ё˄Connecting˅ˈ಴ℸ೒ 11.8 Ё䌘⑤ⱘ⢊ ೒ 11.8 ᮄ๲ϟ䕑  ᮄᓎϔϾϟ䕑䌘⑤ˈᠻ㸠ֱᄬৢᬜᵰབ೒ 11.8 ᠔⼎DŽ ᮍ⊩ᴹ䆒㕂⬠䴶ᰒ⼎ⱘ᭄᥂DŽ 䆌໪⬠䇗⫼ TableModel ⱘ䌘⑤䲚ড়ⱘ setterܕℸᔧ⫼᠋೼ᇐ㟾ᷥЁ⚍ߏᶹⳟ݊Ҫ⢊ᗕ䌘⑤ⱘᯊ׭ˈህ setter ᮍ⊩ˈ⬅Ѣ TableModel াᰃ䋳䋷᥻ࠊ⬠䴶ⱘ䌘⑤ᰒ⼎ˈᅗᑊϡⶹ䘧䆹ᰒ⼎ાѯ⢊ᗕⱘ䌘⑤ˈ಴ ׭ˈህҢϞϟ᭛ЁᕫࠄⳌᑨⱘ䌘⑤䲚ড়ˈ䳔㽕⊼ᛣⱘᰃˈ೼ TableModel Ёˈ៥Ӏ䳔㽕Ў䌘⑤䲚ড়ᦤկ ҹϞҷⷕЁˈЎ TableModel ᮄᓎϔϾ Listⱘሲᗻˈ䆹 TableModel ᇍ䈵䖯㸠ᵘ䗴ⱘᯊ } return super.getValueAt(row, column); } return r.getId(); } else if (columnName.equals(ID_COLUMN)) { return r.getSpareTime() + " ⾦"; } else if (columnName.equals(SPARE_TIME_COLUMN)) { return r.getCostTime() + " ⾦"; else if (columnName.equals(COST_TIME_COLUMN)) { { /Download at http://www.pin5i.com closeStream(is, urlConnection, this.raf); } this.part.setCurrentLength(this.part.getCurrentLength() + perRead); raf.write(buffer, 0, perRead); //߸ᮁ䌘⑤ᇍ䈵⢊ᗕ while ((perRead = is.read(buffer)) != -1) { this.raf.seek(this.part.getCurrentLength()); //೼.part ᭛ӊЁ䆒㕂ᔧࠡ᠔䇏পⱘᣛ䩜 this.resource.setState(DownloadContext.DOWNLOADING); //䆒㕂⢊ᗕЎϟ䕑 int perRead = 0; byte[] buffer = new byte[MAX_BUFFER_SIZE]; InputStream is = urlConnection.getInputStream(); //བᵰᡒϡࠄⳌᑨⱘ䌘⑤, ᇚᡯߎ java.io.FileNotFoundException urlConnection.connect(); //བᵰ䖲᥹ϡϞⳌᑨⱘഄഔ, ᡯߎ java.net.UnknownHostException urlConnection.setRequestProperty("Range", "bytes=" + begin + "-" + end); HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection(); } return; this.raf.close(); if (begin >= end) { //བᵰᰃᓔྟ⚍໻Ѣ㒧ᴳ⚍, 䆕ᯢ䆹ഫᏆ㒣ϟ䕑ᅠ៤ int end = part.getBegin() + part.getLength() - 1; int begin = part.getBegin() + part.getCurrentLength(); //䅵ㅫᓔྟ⚍Ϣ㒧ᴳ⚍ public void run() { ҷⷕ⏙ऩ˖code\flashget\src\org\crazyit\flashget\thread\DownloadThread.java˖ ᇍ䈵ᇚ䇏পࠄᄤ㡖䕧ߎࠄᴀഄ᭛ӊЁDŽҹϟᰃ DownloadThread ⱘ run ᅲ⦄DŽ ᅲ⦄᭛ӊⱘϟ䕑ˈЏ㽕ᰃՓ⫼ HttpURLConnection 䇏প䖰⿟ⱘ䌘⑤ˈݡ䗮Փ⫼ RandomAccessFile } return new URL(urlPath); private URL createURL(String urlPath) { } this.resource = resource; this.part = part; this.raf = raf; this.url = createURL(resource.getUrl()); public DownloadThread(Resource resource, RandomAccessFile raf, Part part) { */ * ϟ䕑㒓⿟ᵘ䗴఼ /** DownloadThread˖ ϾഫⳌ݇㘨ⱘˈ಴ℸᵘ䗴఼Ёᖙ乏㽕᳝ Part ᇍ䈵DŽ ᅲ⦄ run ᮍ⊩ेৃDŽབᵰ䌘⑤㹿ߚЎ 5 ഫˈ䙷Мህ㽕߯ᓎ 5 ᴵ㒓⿟ˈҢ䖭䞠ৃҹⳟߎˈThread ᰃϢᶤ ⱘഫᴹ߯ᓎⳌᑨⱘϟ䕑㒓⿟DŽᓎゟϟ䕑㒓⿟㉏ DownloadThreadˈ䆹㉏㒻ᡓѢ Thread ㉏ˈ៥Ӏা䳔㽕 ⫼᠋䕧ܹњ䌘⑤ϟ䕑ⱘⳌֵ݇ᙃৢˈህ䳔㽕ḍ᥂⫼᠋䗝ᢽⱘ㒓⿟᭄䖯㸠ഫⱘߚࡆˈݡḍ᥂䖭ѯߚࡆ ᓎゟϟ䕑㒓⿟ 11.4.2 /Download at http://www.pin5i.com RandomAccessFile rav = new RandomAccessFile(r.getFilePath() + r.getParts().add(p); Part p = new Part((i * partLength), length, 0); //߯ᓎ৘Ͼ Part ᇍ䈵 } length = r.getSize() - i * partLength; if (i == (r.getThreadSize() - 1)) { এࠡ䴶ഫⱘᘏ੠ޣ //བᵰᰃ᳔ৢϔഫ, ߭Փ⫼ᘏ᭄ᴹ int length = partLength; for (int i = 0; i < r.getThreadSize(); i++) { timer.schedule(timeTask, 0, 1000); Timer timer = new Timer(); CountTimeTask timeTask = new CountTimeTask(r); //ᯊ䯈䅵ㅫӏࡵ int partLength = r.getSize() / r.getThreadSize() + 1; //䅵ㅫߎ↣ϔഫⱘ໻ᇣ r.setState(DownloadContext.CONNECTION); if (r.getDownloadDate() == null) r.setDownloadDate(new Date()); //䆒㕂ϟ䕑᮹ᳳ public void doDownload(Resource r) { ҷⷕ⏙ऩ˖code\flashget\src\org\crazyit\flashget\thread\DownloadHandler.java˖ ⱘ݋ԧᅲ⦄DŽ 㒓⿟᭄ᇍϟ䕑ⱘ䌘⑤䖯㸠ߚࡆˈ߯ᓎϔϾ Part ᇍ䈵ˈݡਃࡼϔᴵϟ䕑㒓⿟DŽҹϟᰃ doDownload ᮍ⊩ ೼ 11.2.2 Ёˈ៥Ӏ߯ᓎњϟ䕑㒓⿟ˈ䙷М೼ᅲ⦄ doDownload ᮍ⊩ⱘᯊ׭ˈህৃҹḍ᥂⫼᠋䗝ᢽⱘ resumeDownload ᮍ⊩ˈ䞡ᮄ䖯㸠ϟ䕑DŽ ℶϟ䕑ৢˈབᵰᛇ䞡ᮄ䖯㸠ϟ䕑ˈৃҹ䇗⫼ذ䳔㽕ᦤկ resumeDownload ᮍ⊩ˈᔧ䌘⑤㹿 ㉏ᦤկ doDownload ᮍ⊩ˈ⫼᠋ᮄᓎϟ䕑䌘⑤ᯊˈৃҹ䇗⫼䆹ᮍ⊩䖯㸠ϟ䕑DŽDownloadHandler ৠᯊг ೼䖯㸠᭛ӊߚࡆࠡˈ៥ӀᓎゟϔϾ DownloadHandler ᇍ䈵ˈ⫼Ѣ໘⧚᭛ӊⱘϟ䕑Ϣ㒻㓁ϟ䕑ˈ䆹 11.2.3 䖯㸠᭛ӊߚࡆ ӊЁ˄.part ᭛ӊ˅DŽ 䌘⑤䖨ಲⱘ⌕ˈݡᇚ InputStream 䇏পࠄⱘᄫ↉䗮䖛 RandomAccessFile ⱘ write ᮍ⊩ݭܹࠄᴀഄⱘ᭛ 䖯㸠䆒㕂DŽᔧ HttpURLConnection ᇍ䈵䖯㸠䖲᥹ৢˈৃҹḍ᥂䆹ᇍ䈵ᕫࠄϔϾ InputStream ᇍ䈵ˈ䇏প Ϣ㒧ᴳ⚍ˈৃҹՓ⫼ HttpURLConnection.setRequestProperty("Range", "bytes=" + begin + "-" + end) currentLength ሲᗻࡴϞᓔྟ⚍ˈ䅽⿟ᑣⶹ䘧ᴀ⃵ϟ䕑䳔㽕Ң 10 ᓔྟˈࠄ 19 㒧ᴳDŽᣛᅮϟ䕑ⱘᓔྟ⚍ ԚᰃℸᯊᏆϟ䕑ሲᗻ˄Part ⱘ currentLength ሲᗻ˅ህᰃ 10DŽ䅵ㅫᓔྟ⚍ⱘᯊ׭ˈ䳔㽕ᇚ Part ⱘ ⱘᣛ䩜DŽᔧ㄀Ѡ⃵䖯㸠ϟ䕑ⱘᯊ׭ˈ៥Ӏৃҹᕫⶹ Part ᇍ䈵ⱘᓔྟ⚍ᰃ 0ˈ㒧ᴳ⚍ᰃ 19ˈ䭓ᑺᰃ 20ˈ ⿟ᑣ㒜ℶˈ⿟ᑣህ䳔㽕䆄ᔩᔧࠡⱘϟ䕑⚍ˈ䗮䖛ҹϞҷⷕⱘ咥ԧ䚼ߚ䆄ᔩᔧࠡ RandomAccessFile ᭛ӊ М㄀ϔϾ Part ᇍ䈵ⱘᓔྟ⚍ህᰃ 0ˈ㒧ᴳ⚍ህᰃ 19ˈ䭓ᑺᰃ 20DŽᔧ䖭Ͼ Part ᇍ䈵㹿ϟ䕑ࠄ 10 ⱘᯊ׭ 䅵ㅫߎᓔྟϟ䕑⚍Ϣᓔϟⱘ㒧ᴳ⚍ˈབᵰϔϾ䌘⑤໻ᇣᰃ 100ˈ㹿ߚ៤ 5 ӑˈ䙷ܜˈ೼ҹϞҷⷕЁ } raf.close(); urlConnection.disconnect(); is.close(); RandomAccessFile raf) throws IOException { private void closeStream(InputStream is, HttpURLConnection urlConnection, 䯁খ᭄Ёⱘ⌕݇// { /Download at http://www.pin5i.com ... public MainFrame { }; } downloadTable.updateUI(); //ࠋᮄ߫㸼 public void actionPerformed(ActionEvent e) { ActionListener refreshTable = new ActionListener() { ҷⷕ⏙ऩ˖code\flashget\src\org\crazyit\flashget\ui\MainFrame.java˖ 䳔㽕Ў⬠䴶㒘ӊࡴܹⳌ݇ⱘ᥻ࠊˈ䅽݊೼ᶤ↉ᯊ䯈ݙ䖯㸠ࠋᮄDŽ 㒓⿟⿟ᑣᏆ㒣㓪ݭᅠњˈԚᰃЏ⬠䴶ⱘ䌘⑤߫㸼ᑊϡӮ䱣ⴔ᭛ӊⱘϟ䕑㗠থ⫳ব࣪ˈ䖭ᰃ⬅Ѣ៥Ӏ ⱘϟ䕑㒓⿟Ꮖ㒣⫳ᬜDŽ ೼๲䭓ˈ䇈ᯢ៥Ӏذ㸠⌟䆩ˈৃҹⳟࠄ㋏㒳Ꮖ㒣ᐂ៥Ӏ⫳៤њ㢹ᑆӑ.part ᭛ӊˈᑊϨ䖭ѯ᭛ӊⱘ໻ᇣϡ ҹϞⱘ咥ԧҷⷕ䇗⫼ DownloadHandler ⱘ doDownload ᮍ⊩䖯㸠ϟ䕑DŽ៥Ӏৃҹᮄᓎϟ䕑䌘⑤䖯 } this.setVisible(false); ContextHolder.dh.doDownload(r); ContextHolder.ctx.resources.add(r); Resource r = createResource(); this.warnLabel.setText(" "); } return; this.warnLabel.setText("䇋䕧ܹℷ⹂ⱘ᭛ӊৡ"); if (getSaveFileName() == null) { private void confirm() { ҷⷕ⏙ऩ˖code\flashget\src\org\crazyit\flashget\ui\NewTaskFrame.java˖ ⫼ doDownload ᮍ⊩䖯㸠ϟ䕑ेৃDŽ ⫼᠋ᮄᓎϟ䕑䌘⑤⚍ߏ NewTaskFrame ⱘ⹂ᅮᣝ䪂ˈ㾺থথ䆹㉏ⱘ confirm ᮍ⊩ˈা㽕৥䆹ᮍ⊩䇗 } public static DownloadHandler dh = new DownloadHandler(); //ϟ䕑໘⧚㉏ public static DownloadContext ctx = new DownloadContext(); //ϟ䕑Ꮉ݋Ϟϟ᭛ public class ContextHolder { ҷⷕ⏙ऩ˖code\flashget\src\org\crazyit\flashget\ContextHolder.java˖ DownloadHandler ϡᖙ↣⃵Փ⫼ⱘᯊ׭䛑߯ᓎϔϾᅲ՟ˈৃҹᇚ݊ᬒࠄ ContextHolder ㉏Ё߱ྟ࣪DŽ ᔧ⫼᠋ᮄᓎϟ䕑䌘⑤ⱘᯊ׭ˈህৃҹ䇗⫼ DownloadHandler ⱘ doDownload ᮍ⊩䖯㸠ϟ䕑ˈ⬅Ѣ Փ⫼ⱘᯊ׭ˈ䆹ᅮᯊ఼↣䱨ϔ⾦ˈᇚ Resource ⱘ costTime ሲᗻࡴ 1 ेৃDŽ 䳔㽕⊼ᛣⱘᰃˈϔᴵ㒓⿟াӮϟ䕑ϔϾ.part ᭛ӊDŽҹϞҷⷕⱘ咥ԧ䚼ߚᰃϔϾᅮᯊ఼ˈ⫼Ѣ䅵ㅫ䌘⑤᠔ Part ᇍ䈵ⱘ partName ሲᗻ߯ᓎ.part ᭛ӊˈ䆹ሲᗻ೼ᵘ䗴 Part ⱘᯊ׭ˈህ䗮䖛 UUID ⫳៤ଃϔⱘৡ⿄DŽ RandomAccessFile ᇍ䈵ˈህৃҹਃࡼϟ䕑㒓⿟DŽ೼߯ᓎ RandomAccessFile ᇍ䈵ⱘᯊ׭ˈ៥ӀՓ⫼ DownloadHandler ⱘ doDownload ᮍ⊩Ёˈা䳔㽕䅵ㅫߎ↣ϔഫⱘ໻ᇣˈݡ߯ᓎϔϾ } } t.start(); t.setPriority(6); 㑻ܜ //䆒㕂㒓⿟Ӭ DownloadThread t = new DownloadThread(r, rav, p); File.separator + p.getPartName(), "rw"); /Download at http://www.pin5i.com //߸ᮁᰃ৺ϟ䕑ᅠ៤, བᵰϟ䕑ᅠ៤, ߭䖯㸠ড়ᑊ᭛ӊ ... public void run() { ҷⷕ⏙ऩ˖code\flashget\src\org\crazyit\flashget\thread\DownloadThread.java˖ 䙷Мህᛣੇⴔ↣ϔᴵ㒓⿟೼ϟ䕑ᅠ㞾Ꮕ䙷ϔ䚼ߚⱘᄫ㡖ৢˈ䛑䳔㽕䖯㸠ϔ⃵߸ᮁDŽ currentLength ሲᗻⳌࡴˈ߸ᮁᰃ৺໻Ѣ䌘⑤ⱘ sizeDŽ೼ᴀゴЁˈϔϾ Part ᇍ䈵⬅ϔᴵ㒓⿟䖯㸠ϟ䕑ˈ 䴶᠔᳝ⱘ.part ᭛ӊᰃ৺Ꮖ㒣ϟ䕑ᅠ៤ˈ៥Ӏ㽕ᕫࠄ䆹䌘⑤ᇍᑨⱘ᠔᳝ Part ᇍ䈵ˈݡ⫼䖭ѯ Part ᇍ䈵ⱘ ⊼ᛣⱘᰃˈড়ᑊࠡ៥Ӏ䳔㽕߸ᮁᰃ৺Ꮖ㒣ϟ䕑ᅠ៤ˈ㗠߸ᮁϔϾ᭛ӊᰃ৺ϟ䕑ᅠ៤ˈ䳔㽕߸ᮁ䆹䌘⑤䞠 ℸ៥Ӏ䳔㽕೼ϟ䕑ⱘ㒓⿟Ё㓪ݭ᭛ӊড়ᑊⱘᅲ⦄ˈᑊ᠔᳝ⱘ.part ᭛ӊড়ᑊ៤ϔӑⳂᷛ᭛ӊDŽ೼䖭䞠䳔㽕 ै≵᳝ѻ⫳ˈ䖭ᰃ⬅Ѣϟ䕑㒓⿟াᰃᐂ៥Ӏᇚᄫ㡖ֱᄬࠄ䖭ѯ.part ᭛ӊЁˈᑊ≵᳝ᐂ៥Ӏ䖯㸠ড়ᑊˈ಴ ᔧ᭛ӊϟ䕑ᅠ៤ৢˈࠄֱᄬⳂᔩˈৃҹⳟࠄˈাᰃ᳝޴ӑ.part ᭛ӊ೼䙷ˈ㗠៥Ӏ᠔䳔㽕ⱘⳂᷛ᭛ӊ 11.2.4 ᭛ӊড়ᑊ ೒ 11.9 ᅲ⦄᭛ӊϟ䕑㒓⿟  ࣙЁⱘ Timer ㉏DŽℸᯊৃҹ䖤㸠⿟ᑣˈᮄᓎϟ䕑ˈৃҹⳟࠄᬜᵰབ೒ 11.9 ᠔⼎DŽ Փ⫼ JTable ⱘ updateUI ᮍ⊩ᇍ⬠䴶䖯㸠ࠋᮄˈ䳔㽕⊼ᛣⱘᰃˈҹϞҷⷕⱘ Timer ᇍ䈵ᰃ java. swing } ... timer.start(); Timer timer = new Timer(1000, refreshTable); ߯ᓎᅮᯊ఼// /Download at http://www.pin5i.com ... public void run() { ҷⷕ⏙ऩ˖code\flashget\src\org\crazyit\flashget\thread\DownloadThread.java˖ ߸ᮁˈབᵰ䌘⑤ⱘ⢊ᗕ㹿䆒㕂៤ Pause ⱘ䆱ˈ䙷М run ᮍ⊩ህ䖨ಲDŽ ᪡԰ˈ಴ℸৃҹ೼ DownloadThread ⱘ run ᮍ⊩Ё˄while ᕾ⦃ԧ˅ࡴܹذњ᱖خ⿟ᑊϡⶹ䘧៥ӀᏆ㒣 Pause˅ेৃˈԚᰃ㒓˄ذϔϾ䌘⑤ⱘϟ䕑ᯊˈህৃҹᇚ᠔䗝ᢽⱘ䌘⑤⢊ᗕᬍЎ᱖ذᔧ⫼᠋䳔㽕᱖ ϟ䕑ذ11.2.5 ᱖ ℶ䌘⑤⫼ᯊⱘ䅵ᯊ఼DŽذ᠔᳝ⱘ.part ᭛ӊߴ䰸ˈᑊ ݭܹˈ᳔ৢˈᇚ䌘⑤ⱘ⢊ᗕ䆒㕂Ўᅠ៤DŽ䖭䞠䳔㽕⊼ᛣⱘᰃˈᇚ䌘⑤ⱘ⢊ᗕ䆒㕂Ўᅠ៤⢊ᗕৢˈ䳔㽕ᇚ ⊩ˈᇚ䆹䌘⑤᠔᳝ⱘ.part ᭛ӊড়ᑊࠄⳂᷛ᭛ӊЁDŽ᭛ӊⱘܼᑊՓ⫼ IO ⌕ेৃᅠ៤ˈ䇏প.part ᭛ӊˈݡ ҹϞҷⷕЁⱘ isFinished ᮍ⊩߸ᮁ䌘⑤ᰃ৺ϟ䕑ᅠ៤ˈབᵰ䌘⑤ϟ䕑ᅠ៤ৢˈݡ䇗⫼ uniteParts ᮍ } this.resource.setState(DownloadContext.FINISHED); bos.close(); } is.close(); } bos.write(buffer, 0, bytesRead); //ݭࠄ᭛ӊЁ temp += bytesRead; while ((bytesRead = is.read(buffer)) != -1) { int temp = 0; int bytesRead; byte[] buffer = new byte[1024]; InputStream is = new FileInputStream(partFile); //㦋ᕫ᭛ӊ䕧ܹ⌕ part)); File partFile = new File(FileUtil.getPartFilePath(this.resource, //ᕫࠄ.part ᭛ӊ for (Part part : parts) { false); OutputStream bos = new FileOutputStream(this.resource.getSaveFile(), //߯ᓎ᭛ӊ䕧ߎ⌕, 䕧ߎࠄϟ䕑᭛ӊ List parts = this.resource.getParts(); private void uniteParts() throws IOException { //ড়ᑊ part ᭛ӊ } return (downCount >= fileLength) ? true : false; for (Part part : parts) downCount += part.getCurrentLength(); int downCount = 0; //䅵ㅫᏆϟ䕑ⱘᘏ᭄ List parts = this.resource.getParts(); private boolean isFinished(int fileLength) { //߸ᮁᰃ৺ϟ䕑ᅠ៤, 䘡ग़ϟ䕑᭛ӊⱘ৘Ͼ.part ᭛ӊ } if (isFinished(this.resource.getSize())) uniteParts(); ᛣ䖭䞠䳔㽕ᕫࠄᭈϾ᭛ӊⱘ໻ᇣ, 㗠ϡᰃᶤϾ.part ᭛ӊⱘ໻ᇣ⊼// /Download at http://www.pin5i.com ࠄℸˈ᭛ӊⱘϟ䕑Ꮖ㒣ܼ䚼ᅲ⦄ˈ៥Ӏৃҹ䖤㸠⿟ᑣᶹⳟᬜᵰˈϟ䕑ᅠৢᬜᵰབ೒ 11.10 ᠔⼎DŽ ࠄⳌᑨⱘ Part ᇍ䈵ˈ㗠ᓔྟϟ䕑ⱘᮍ⊩߭ᰃ߯ᓎ Part ᇍ䈵DŽ 㒻㓁ϟ䕑ⱘᮍ⊩Ϣᓔྟϟ䕑ⱘᮍ⊩ᅲ⦄ᑊ᮴໾໻ऎ߿ˈ㒻㓁ϟ䕑ᮍ⊩Ёৃҹ䗮䖛 Resource ᇍ䈵ᕫ } } t.start(); DownloadThread t = new DownloadThread(r, rav, p); //ਃࡼ㒓⿟ File.separator + p.getPartName(), "rw"); RandomAccessFile rav = new RandomAccessFile(r.getFilePath() + //߯ᓎ RandomAccessFile ᇍ䈵 Part p = r.getParts().get(i); //ᕫࠄ Part ᇍ䈵 for (int i = 0; i < r.getParts().size(); i++) { timers.put(r.getId(), timer); //ᇚ Timer ᇍ䈵ᬒࠄ Map Ё, key Ў䆹䌘⑤ⱘ id timer.schedule(timeTask, 0, 1000); Timer timer = new Timer(); CountTimeTask timeTask = new CountTimeTask(r); //䅵ㅫՓ⫼ᯊ䯈 if (r.getState() instanceof Finished) return; public void resumeDownload(Resource r) { ҷⷕ⏙ऩ˖code\flashget\src\org\crazyit\flashget\thread\DownloadHandler.java˖ ᮍ⊩䆄ᔩњ᭛ӊᣛ䩜ˈ䙷Мህৃҹ㒻㓁䖯㸠ϟ䕑DŽ 䙷М㒓⿟ህӮḍ᥂䖭ѯ䇏পֵᙃᴹ䅵ㅫᓔྟ⚍੠㒧ᴳ⚍ˈ⬅Ѣ៥ӀՓ⫼ RandomAccessFile Ёⱘ seek ੠ Resource 䞡ᮄਃࡼϔᴵ㒓⿟DŽ⬅Ѣ Part ЁֱᄬњⳌᑨⱘ䇏পֵᙃ˄䇏পᓔྟ⚍ǃ䭓ᑺǃᏆ䇏প䭓ᑺ˅ˈ ࠄⳌᑨⱘ.part ᭛ӊᑊḍ᥂䖭ѯ᭛ӊ߯ᓎ RandomAccessFile ᇍ䈵ˈ᳔ৢҹ PartǃRandomAccessFile DownloadHandler Ёⱘ resumeDownload ᮍ⊩DŽ䅽㋏㒳ᕫࠄᔧࠡ䌘⑤ⱘ Part ᇍ䈵ˈݡḍ᥂䖭ѯᇍ䈵ᡒ 䳔㽕䅽݊㒻㓁ϟ䕑ˈ䙷М៥Ӏৃҹᅲ⦄᠋⫼ˈৢذᔧϔϾ⢊ᗕЎϟ䕑Ёⱘ䌘⑤㹿᱖ 11.2.6 㒻㓁ϟ䕑 } } ... } return; closeStream(is, urlConnection, this.raf); if (this.resource.getState() instanceof Pause) { ذ //߸ᮁ䌘⑤ᇍ䈵ⱘ⢊ᗕᰃ৺㹿ׂᬍ៤᱖ while ((perRead = is.read(buffer)) != -1) { /Download at http://www.pin5i.com Serializable ᥹ষˈ䙷Мᇚᡯߎ java.io. InvalidClassExceptionDŽ ԰Ў䆹 Resource ⱘ⢊ᗕˈ಴ℸ䖭Ͼ⢊ᗕ᥹ষᖙ乏㒻ᡓ Serializable ᥹ষDŽབᵰ䖭ѯᇍ䈵≵᳝ᅲ⦄ ৃҹᑣ߫࣪ⱘˈ಴ℸ ResourceǃPart Ёϡ㛑ߎ⦄ϡৃҹᑣ߫࣪ⱘሲᗻˈ೼ Resource ЁՓ⫼њ TaskState ೼ Java Ёˈᑣ߫࣪ϔϾᇍ䈵ˈ䆹ᇍ䈵ᖙ乏ᅲ⦄ Serializable ᥹ষˈ㗠Ϩ৘Ͼᇍ䈵Ёⱘሲᗻᖙ乏гᰃ ៥Ӏৃҹᇚ DownloadContext 䖯㸠ᑣ߫࣪DŽ 䆹䌘⑤᠔᳝ⱘഫ˄Part ᇍ䈵˅ˈህৃҹᕫࠄ䆹ഫᏆϟ䕑ⱘ䭓ᑺǃ䆹ഫⱘ䭓ᑺǃᓔྟ⚍੠㒧ᴳ⚍DŽ಴ℸˈ ϔϾ䌘⑤ᇍ䈵Ёֱᄬњ᠔᳝ⱘ䌘⑤ֵᙃ˄䗳ᑺǃ䖯ᑺǃᏆϟ䕑ㄝ˅ˈᕫࠄᶤϾ䌘⑤ᇍ䈵ৢˈህৃҹᕫࠄ DownloadContext ⱘᇍ䈵ˈ䆹ᇍ䈵ֱᄬњϔϾ䌘⑤䲚ড়ˈ㸼⼎ᔧࠡϟ䕑Ꮉ݋ЁⳌ݇ⱘ᠔᳝䌘⑤ᇍ䈵ˈ↣ ೼ᓔྟ㓪ⷕࠡˈ៥Ӏ䳔㽕ᯢⱑ㽕ᑣ߫࣪ાѯᇍ䈵DŽ೼ࠡ䴶ⱘ⿟ᑣЁˈ៥Ӏ߯ᓎњϔϾ 11.5.1 䖯㸠ᑣֱ߫࣪ᄬᇍ䈵 䗮䖛 Java ⱘᑣ߫࣪ᴹֱᄬ᠔᳝ⱘ䖭ѯᇍ䈵DŽ ᴀゴⱘ⿟ᑣЁˈ៥Ӏᑊϡ⍝ঞӏԩⱘ᭄᥂ᑧ⿟ᑣˈ಴ℸ≵᭄᳝᥂ᑧ䅽៥Ӏএ䖯㸠ֱᄬˈ಴ℸˈ៥Ӏৃҹ ⑤᠔᳝ⱘֵᙃˈഫᇍ䈵ֱᄬњᔧࠡ᠔ϟ䕑ⱘ䭓ᑺㄝ䞡㽕ֵᙃˈ಴ℸ៥Ӏ䳔㽕ᇚ䖭ϸϾᇍ䈵䖯㸠ֱᄬDŽ೼ 䖯㸠ֱᄬDŽᅲ䰙Ϟˈ៥Ӏ䳔㽕ֱᄬⱘᰃ䌘⑤ᇍ䈵˄Resource˅ˈഫᇍ䈵˄Part˅ˈ⬅Ѣ䌘⑤ᇍ䈵ֱᄬ䆹䌘 ᔧ⫼᠋݇䯁њϟ䕑Ꮉ݋ˈ㗠Ϩϟ䕑Ꮉ݋ℷ೼ϟ䕑ᶤѯ䌘⑤ⱘᯊ׭ˈ៥Ӏ䳔㽕ᇍ䖭ѯℷ೼ϟ䕑ⱘ䌘⑤ 11.5 ֱᄬϟ䕑ֵᙃ ೒ 11.10 ϟ䕑ᅠ៤  /Download at http://www.pin5i.com File serFile = FileUtil.SERIALIZABLE_FILE; public void reverseSer() { //ডᑣ߫࣪ } this.downloadTable.refresh(); //ࠋᮄ߫㸼 model.setResources(ContextHolder.ctx.resources); //ᇚֱᄬⱘ䌘⑤䆒㕂ࠄ߫㸼Ё DownloadTableModel model = (DownloadTableModel)this.downloadTable.getModel(); private void addTableData() { //ᇚ DownloadContext Ёⱘ᠔᳝䌘⑤ᇍ䈵䆒㕂ࠄ߫㸼ⱘ TableModel Ё ҷ 䈵DŽ 䕑Ꮉ݋ਃࡼⱘᯊ׭ˈҢᑣ߫࣪᭛ӊЁᕫࠄПࠡⱘ DownloadContext ᇍ䈵ˈЏ㽕ᰃᕫࠄ৘Ͼ䌘⑤ᇍ ⷕ⏙ऩ˖code\flashget\src\org\crazyit\flashget\ui\MainFrame.java˖ ೼ϟ ᅲ⦄њᑣ߫࣪ᇚ DownloadContext ֱᄬࠄᑣ߫࣪᭛ӊЁৢˈ៥Ӏ䖬䳔㽕㓪ݭ⿟ᑣ䖯㸠ডᑣ߫࣪ˈ 11.5.2 ডᑣ߫࣪ }); } System.exit(0); mainFrame.serializable(); public void actionPerformed(ActionEvent arg0) { this.quitItem.addActionListener(new ActionListener() { ҷⷕ⏙ऩ˖code\flashget\src\org\ ⫼ MainFrame ⱘ serializable ᮍ⊩DŽ ⍂にষҹঞӏࡵᷣ೒ᷛˈ಴ℸ䆹ᮍ⊩⬅ ⍂にষ੠ӏࡵᷣ೒ᷛⱘে䬂㦰ऩ䇗⫼ˈᔧ⿟ᑣ䗔ߎⱘᯊ׭ˈ䇗  nloadContext˄Ϟϟ᭛˅ᇍ䈵DŽserializable ᮍ⊩ᅮН೼ MainFrame ЁˈԚᰃˈ⬅Ѣ៥Ӏᅲ⦄њ crazyit\flashget\ui\SuspendWindow.java˖ Dow ҹϞҷⷕЁˈՓ⫼ ObjectOutStream ᇚᇍ䈵ݭࠄ txt ᭛ӊЁˈ㗠䖭Ͼᇍ䈵ህᰃ៥Ӏⱘ } fos.close(); oos.close(); oos.writeObject(ContextHolder.ctx); //ᇚϞϟ᭛ᇍ䈵ݭࠄᑣ߫࣪᭛ӊЁ ObjectOutputStream oos = new ObjectOutputStream(fos); FileOutputStream fos = new FileOutputStream(serFile); if (!serFile.exists()) serFile.createNewFile(); //߸ᮁᑣ߫࣪᭛ӊᰃ৺ᄬ೼, ϡᄬ೼߭߯ᓎ File serFile = FileUtil.SERIALIZABLE_FILE; } } r.setState(ContextHolder.ctx.PAUSE); if (r.getState() instanceof Downloading) { for (Resource r : ContextHolder.ctx.resources) { ℶذᇚ᠔᳝ℷ೼ϟ䕑ⱘӏࡵܜ //ᑣ߫࣪ࠡ public void serializable() { //ᑣ߫࣪(DownloadContext ᇍ䈵) ҷⷕ⏙ऩ˖code\flashget\src\org\crazyit\flashget\ui\MainFrame.java˖ Ў MainFrame ᦤկϔϾ serializable ⱘᮍ⊩ˈ⫼Ѣ⿟ᑣ䗔ߎᯊ䖯㸠ᑣ߫࣪DŽ /Download at http://www.pin5i.com infoJList.setListData(infoList.toArray()); //䞡ᮄ䆒㕂 JList ᭄᥂ info.setValue(RESOURCE_INFO_TEXT + r.getState().getState()); DateUtil.formatDate(r.getDownloadDate())); downloadDate.setValue(DOWNLOAD_DATE_TEXT + r.getSaveFile().getAbsolutePath()); filePath.setValue(FILE_PATH_TEXT + fileSize.setValue(FILE_SIZE_TEXT + r.getSize()); //䆒㕂ֵᙃᰒ⼎ऎඳⱘؐ if (r == null) return; Resource r = getResource(); //ᕫࠄ⚍ߏⱘ䌘⑤ public void mouseClicked(MouseEvent e) { this.downloadTable.addMouseListener(new MouseAdapter() { //⚍ߏ߫㸼哴ᷛⲥ਀఼ ҷⷕ⏙ऩ˖code\flashget\src\org\crazyit\flashget\ui\Ma 䕗Ўㅔऩˈা䳔㽕Ўϟ䕑߫㸼ࡴܹ哴ᷛⲥ਀఼ेৃDŽ ࡵ੠ᶹⳟӏࡵㄝࡳ㛑≵᳝ᅲ⦄ˈ䙷М೼ᴀᇣ㡖ˈᇚӮᅲ⦄䖭ѯ᪡԰DŽ 䚼ӏܼذডᑣ߫࣪DŽ೼ᴀゴⱘϟ䕑Ꮉ݋Ёˈ䖬᳝ߴ䰸ӏࡵǃߴ䰸Ꮖ㒣ᅠ៤ӏࡵǃᓔྟܼ䚼ӏࡵǃ᱖ 11.6 ϟ䕑ⱘ䌘⑤⍜༅ˈ䞡ᮄਃࡼϟ䕑Ꮉ݋ৢˈህৃҹ㒻㓁ϟ䕑䌘⑤DŽ ᮍ⊩DŽࠄℸˈ៥Ӏᅲ⦄њ⫼᠋䗔ߎ⿟ᑣৢֱᄬᔧࠡϟ䕑䌘⑤ⱘֵᙃˈ⫼᠋䗔ߎ⿟ᑣৢˈϡᖙᢙᖗПࠡ᠔ ৢЎϟ䕑߫㸼䆒㕂᭄᥂ˈ䅽߫㸼ৃҹᰒ⼎Ⳍᑨⱘ᭄᥂DŽ៥Ӏৃҹ೼ MainFrame ᵘ䗴఼Ё䇗⫼ reverseSer ಴ℸᔧ䗮䖛ডᑣ߫࣪ᕫࠄ DownloadContext ᇍ䈵ৢˈ䖬䳔㽕ᇚ䆹ᇍ䈵䆒㕂ࠄ ContextHolder Ёˈ᳔ inFrame.java˖ ᅲ⦄ ᔧ⫼᠋೼ϟ䕑߫㸼䗝ᢽњᶤϔϾ䌘⑤ᯊˈህৃҹᇚ䆹䌘⑤ⱘⳌֵ݇ᙃ⏏ࡴࠄֵᙃᰒ⼎ऎⱘ JList ЁDŽ 11.6.1 ᶹⳟӏࡵ 䖯㸠 ǃ㒻㓁ˈ⿟ᑣ䗔ߎᇍ䌘⑤䖯㸠ᑣ߫ৢˈਃࡼ⿟ᑣᯊذ೼ࠡ䴶ⱘゴ㡖Ёˈ៥Ӏᅲ⦄њ䌘⑤ⱘϟ䕑ǃ᱖ ݊Ҫ᪡԰ Ёˈ ҹϞҷⷕЁⱘ reverseSer ᮍ⊩䖯㸠ডᑣ߫࣪ˈ⬅Ѣ DownloadContext ᇍ䈵ֱᄬ೼ ContextHolder } addTableData(); //䆒㕂߫㸼 } e.printStackTrace(); } catch (Exception e) { fis.close(); ois.close(); ContextHolder.ctx = (DownloadContext)ois.readObject(); //䆒㕂 ContextHolder ⱘ DownloadContext ObjectInputStream ois = new ObjectInputStream(fis); FileInputStream fis = new FileInputStream(serFile); //ᕫࠄ᭛ӊ䕧ܹ⌕ try { if (!serFile.exists()) return; /Download at http://www.pin5i.com //ߴ䰸᠔᳝ⱘ.part ᭛ӊ r.setState(ContextHolder.ctx.PAUSE); ℶذᇚӏࡵܜ// ; if (r == null) return Resource r = getResource(); private void delete() { */ * ߴ䰸䌘⑤ /** ҷⷕ⏙ऩ˖code\flashget\src\org\crazyit\flashget\ui\MainFrame.java˖ ᗕˈݡҢϞϟ᭛˄DownloadContext˅ⱘ䌘⑤䲚ড়Ёߴ䰸䆹䌘⑤DŽ⢊ذӏࡵⱘ⢊ᗕ䆒㕂Ў᱖ ᇚ䆹ܜ䙷МህӮ݇䯁䖭Ͼ䌘⑤᠔᳝⌕ˈ಴ℸ䳔㽕ߴ䰸ϔϾӏࡵᑊߴ䰸ᅗ᠔ᇍᑨⱘ.part ᭛ӊˈህ㽕ˈذ᱖ ѯ.part ᭛ӊℷ೼㹿Փ⫼DŽ៥Ӏ೼ᅲ⦄ϟ䕑㒓⿟ⱘᯊ׭ˈ೼ while ᕾ⦃ԧЁࡴܹњ߸ᮁˈབᵰϔϾ䌘⑤㹿 䙷Мᇚϡৃҹߴ䰸䖭ѯ.part ᭛ӊˈ⬅Ѣ䖭ˈذ᭛ӊ䛑᳝ϔᴵ㒓⿟ᇍ݊䖯㸠᭄᥂ݭܹˈབᵰϡᇚӏࡵ᱖ ݡߴ䰸䆹䌘⑤Ⳍᑨⱘ.part ᭛ӊDŽ⬅Ѣ䌘⑤೼ϟ䕑ᯊˈ↣ϔϾ.partˈذᇚӏࡵ᱖ܜߴ䰸ϔϾӏࡵˈ䳔㽕 11.6.2 ߴ䰸ӏࡵ ೒ 11.11 ᶹⳟ䌘⑤  ᶹⳟ䌘⑤ᬜᵰབ೒ 11.11 ᠔⼎DŽ ;({ { /Download at http://www.pin5i.com MainFrame˖ ⱘ⢊ᗕЎ Pause˅DŽ 䆒㕂ℷ೼ϟ䕑ӏࡵ˄ذ䚼ӏࡵϢᅲ⦄ᓔྟܼ䚼ӏࡵ㉏Ԑˈাᰃᇚℷ೼ϟ䕑ⱘӏࡵܼ䚼᱖ܼذᅲ⦄᱖ 䚼ӏࡵܼذ11.6.5 ᱖ } } } ContextHolder.dh.resumeDownload(r); if (r.getState() instanceof Pause || r.getState() instanceof Failed) { for (Resource r : ContextHolder.ctx.resources) { public void startAllTask() { */ * ᓔྟܼ䚼ӏࡵ /** ҷⷕ⏙ऩ˖code\flashget\src\org\crazyit\flashget\ui\MainFrame.java˖ Downloading˅ˈབᵰЎ݊Ҫ⢊ᗕ˄ℷ೼ϟ䕑៪㗙ϟ䕑ᅠ៤˅ˈ߭ϡৃҹᓔྟDŽ ៪㗙 Failed˄ϟ䕑༅䋹˅ˈ䙷Мህৃҹᓔྟ˄ᇚ䌘⑤ⱘ⢊ᗕ䆒㕂Ў˅ذབᵰ䌘⑤⢊ᗕЎ Pause˄᱖ ᦤկϔϾ public ⱘᮍ⊩ˈ⫼Ѣᓔྟܼ䚼ⱘӏࡵˈḍ᥂Ϟϟ᭛ᕫࠄܼ䚼ⱘ䌘⑤ˈᑊᇍ䖭ѯ䌘⑤䖯㸠߸ᮁˈ ᓔྟܼ䚼ӏࡵࡳ㛑া೼ ⍂にষ੠ӏࡵᷣ೒ᷛⱘ哴ᷛে䬂㦰ऩЁߎ⦄ˈ៥Ӏৃҹ೼ MainFrame Ё 11.6.4 ᓔྟܼ䚼ӏࡵ ટd ᅟ࿉ġྻ౨ԅ deleteFinished ֺ֥ಾ public ԅēუಾဎဟ༧׼ғࣇူఉ๠ः՛ပ౗҂෻юఉ๠ԅٟ } } } it.remove(); if (r.getState() instanceof Finished) { Resource r = (Resource)it.next(); for (Iterator it = ContextHolder.ctx.resources.iterator(); it.hasNext();) { public void deleteFinished() { */ * ߴ䰸Ꮖϟ䕑ᅠ៤ⱘ䌘⑤ /** ҷⷕ⏙ऩ˖code\flashget\s ⑤ҢϞϟ᭛Ёߴ䰸ेৃDŽ rc\org\crazyit\flashget\ui\MainFrame.java˖ Ͼ䌘 ߴ䰸Ꮖ㒣ᅠ៤ⱘӏࡵ↨䕗ㅔऩˈ⬅Ѣ䖭ѯ䌘⑤Ꮖ㒣ϟ䕑ᅠ៤ˈ಴ℸϡӮᄬ೼.part ᭛ӊˈা䳔㽕ᇚ䖭 11.6.3 ߴ䰸Ꮖ㒣ᅠ៤ⱘӏࡵ } ContextHolder.ctx.resources.remove(r); //ҢϞϟ᭛䲚ড়Ёߴ䰸䌘⑤ FileUtil.deletePartFiles(r); /Download at http://www.pin5i.com 㸼ˈህৃҹᕫࠄⳌᑨⱘ䌘⑤᭄᥂DŽ ⫼᠋ᇍ ⍂にষ䖯㸠哴ᷛঠߏⱘᯊ׭ˈབᵰЏにষ˄MainFrame˅ᰃ䱤㮣ⱘˈ䙷Мህ䳔㽕ᇚЏにষ 11.6.7 ᠧᓔ/݇䯁Џにষ 䕑߫ ҹϞҷⷕˈḍ᥂⫼᠋᠔䗝ᢽⱘ㡖⚍䆒㕂 MainFrame ⱘ currentNode ሲᗻˈݡḍ᥂䆹ሲᗻএࠋᮄϟ } model.setResources(ContextHolder.ctx.getResources(currentNode)); DownloadTableModel model = (DownloadTableModel)this.downloadTable.getModel(); private void refreshTable() { */ * ࠋᮄ߫㸼 /** } refreshTable(); this.currentNode = selectNode; DownloadNode selectNode = getSelectNode(); private void selectTree() { */ * ⚍ߏᇐ㟾ᷥ㾺থⱘᮍ⊩ /** } ... }); } selectTree(); public void mouseClicked(MouseEvent e) { this.navTree.addMouseListener(new MouseAdapter(){ //⚍ߏᇐ㟾ᷥ哴ᷛⲥ਀఼ ... private void initlisteners() { ҷⷕ⏙ऩ˖code\flashget\src\org\crazyit\flashget\ui\MainFrame.java 㽕Ўᇐ㟾ᷥࡴܹ哴ᷛⲥ਀఼ˈݡ䇗⫼Ϟϟ᭛ᇍ䈵ⱘⳌᑨᮍ⊩ेৃᅲ⦄DŽ ᭛ᇍ䈵˄DownloadContext˅ⱘᯊ׭ˈህᏆ㒣Ў䆹ᇍ䈵߯ᓎњ䖨ಲ৘Ͼ⢊ᗕ䌘⑤ⱘᮍ⊩ˈ៥Ӏা䳔 ˖ Ϟϟ ⫼᠋೼ᇐ㟾ᷥЁ⚍ߏњᶤϾ㡖⚍ⱘᯊ׭ˈህ䳔㽕ḍ᥂᠔⚍ߏⱘ㡖⚍ˈᕫࠄⳌᑨⱘ䌘⑤䲚ড়DŽ೼߯ᓎ 11.6.6 㡖⚍ⱘ⚍ߏ } } } r.setState(ContextHolder.ctx.PAUSE); if (r.getState() instanceof Downloading) { for (Resource r : ContextHolder.ctx.resources) { public void pauseAllTask() { */ 䚼ӏࡵܼذ᱖ * **/ /Download at http://www.pin5i.com ᑣৃҹ೼䆹⦃๗ϟ៤ࡳ䖤㸠DŽᏠᳯᴀゴⱘ໮㒓⿟ϟ䕑Ꮉ݋ˈᇍ䇏㗙㛑᳝ᕜ໻ਃথDŽ ড়ᑊࠄ᳔㒜ⱘ᭛ӊЁDŽᴀゴⱘᓔথՓ⫼ⱘ Java ⠜ᴀᰃ 1.6.0_18ˈ᪡԰㋏㒳ᰃ Windows XPˈᴀゴⱘ⿟ ϾĀഫāᓎゟϔᴵ㒓⿟䖯㸠ϟ䕑ˈᅲ⦄њ໮㒓⿟ϟ䕑ˈϟ䕑ᅠ᠔᳝ⱘĀഫāৢˈݡՓ⫼ IO ⌕ᇚ䖭ѯĀഫā ㋏㒳ӏࡵᷣ೒ᷛЁDŽᴀゴⱘ䞡⚍ᰃ໮㒓⿟ϟ䕑ˈ೼⿟ᑣЁᇚϔϾ䌘⑤ߚࡆЎ㢹ᑆϾĀഫāˈЎ↣ϔ Ӵⱘࡳ㛑ˈՓ⫼њᑣ߫ Ꮉ݋ˈᄺдњՓ⫼ Http ܹࠄ ⫼њ㞾ᅮНⱘ TableModel ᴹࡼᗕ᳈ᮄ߫㸼᭄᥂ˈᅲ⦄㉏ԐѢ Flashgetǃ䖙䳋ⱘ ⍂にষˈᑊᇚ⿟ᑣࡴ ᑊՓ⫼ RandomAccessFile ᴹᅲ⦄ᮁ⚍㓁 ࣪Ϣডᑣֱ߫࣪ᄬᇍ䈵ֵᙃDŽ⬠䴶ЁՓ ᴀゴЁᅲ⦄њϔϾ HTTP ໮㒓⿟ϟ䕑 URLConnection 䖯㸠䌘⑤䖲᥹ǃϟ䕑ˈ 11.7 ᴀゴᇣ㒧 ࠄℸˈ໮㒓⿟ϟ䕑Ꮉ݋Ꮖ㒣ܼ䚼ᅲ⦄ˈ៥ӀৃҹҢ㔥Ϟᡒϔѯ䌘⑤䖯㸠Ⳍ݇ⱘ⌟䆩DŽ }); } } mainFrame.setVisible(true); } else { mainFrame.setVisible(false); if (mainFrame.isVisible()) { public void actionPerformed(ActionEvent arg0) { this.openItem.addActionListener(new ActionListener() { ҷⷕ⏙ऩ˖code\flashget\src\org\crazyit\flashget\ui\SuspendWindow.java˖ ˄MainFrame˅ᰒ⼎ߎᴹ˄setV 䯁āᯊˈህ䳔㽕ᰒ⼎៪㗙䱤㮣ЏにষDŽ isible(true)˅DŽ⫼᠋⚍ߏњ ⍂にষ㦰ऩ˄བ೒ 11.6 ᠔⼎˅ⱘĀᠧᓔ/݇ ㄀ 11 ゴ ໮㒓⿟ϟ䕑Ꮉ݋g30g /Download at http://www.pin5i.com ӊˈ಴ℸ៥Ӏ೼ᴀゴⱘ䚂ӊᅶ᠋ッЁˈᇚӮՓ⫼䖭Ͼण䆂ᴹ᥹ᬊ䚂ӊDŽ TCP ⱘ 110 ッষˈ⬅Ѣ⦄೼᳔ᐌ⫼ⱘᰃ㄀ϝ⠜ˈ಴ℸ⿄Ў POP3DŽㅔऩⱘ䇈ˈ䖭Ͼण䆂⫼Ѣ᥹ᬊ⬉ᄤ䚂 ϟˈᅗՓ⫼މPOP3 ᰃ Post Office Protocol ⱘ㓽ݭˈे䚂ሔण䆂ˈ⫼Ѣ⬉ᄤ䚂ӊⱘ᥹ᬊˈϔ㠀ᚙ 12.1.3 POP3 ण䆂ㅔҟ 䗕䚂ӊˈህ䳔㽕Փ⫼ࠄ䖭ϔϾ SMTP ᳡ࡵ఼ഄഔDŽ བ Gmail ⱘ䚂ㆅᦤկⱘ SMTP ᳡ࡵ఼ഄഔᰃ˖smtp.gmail.comˈབᵰ៥Ӏ䳔㽕Փ⫼៥Ӏⱘ Gmail ᴹথ ϟˈSMTP Փ⫼ 25 ԰ⲥ਀ッষDŽ໻䚼ߚⱘ䚂ㆅ䛑ᬃᣕ䖭 SMTP ण䆂ˈ՟މण䆂থ䗕䚂ӊDŽ೼ϔ㠀ⱘᚙ ϞӴ䗦৘⾡⬉ᄤ䚂ӊⱘण䆂ˈৃҹᇚ䚂ӊҢᴀᅶ᠋ッӴ䗕ࠄ䚂ӊ᳡ࡵ఼ˈㅔऩⱘ䇈ˈ៥ӀৃҹՓ⫼䖭Ͼ SMTP ᰃ Simple Mail Transfer Protocol ⱘ㓽ݭˈ䆥Ўㅔऩ䚂ӊӴ䕧ण䆂ˈSMTP 㹿⫼ᴹ೼Ѧ㘨㔥 12.1.2 SMTPण䆂ㅔҟ ᴀゴЁՓ⫼њ SMTP ण䆂੠ POP3 ण䆂ᴹ䖯㸠থ䗕䚂ӊ੠᥹ᬊ䚂ӊˈϟ䴶ҟ㒡䖭ϸ⾡ण䆂DŽ ЁˈህৃҹՓ⫼ JavaMail APIDŽ ᴀゴЁ᠔Փ⫼ⱘ JavaMail ⠜ᴀЎ 1.4.2ˈϟ䕑њ JavaMail ৢˈᇚ mail.jar ࡴܹ乍Ⳃⱘ CLASS_PATH http://java.sun.com/products/javamail/downloads/index.html ҹҢҹϟ㔥ഔᕫࠄ JavaMail ⱘࣙ˖ ᅗᦤկњ⣀ゟѢ৘Ͼᑇৄ੠⣀ゟѢ৘⾡ण䆂ⱘḚᶊˈৃҹ䅽៥Ӏএᵘᓎϔѯ䚂ӊ੠⍜ᙃⱘᑨ⫼DŽ៥Ӏৃ JavaMail API ᰃ Sun ᦤկⱘ໘⧚⬉ᄤ䚂ӊⱘ APIˈৃҹᮍ֓ⱘՓ⫼ᅗᴹ䖯㸠ϔѯᐌ⫼ⱘ䚂ӊ᪡԰ˈ 12.1.1 JavaMailㅔҟ 㸠ㅔऩⱘᦣ䗄DŽ ᇍ JavaMail 䖯ܜҹ䖒ࠄ᪡԰䚂ӊⱘⳂⱘˈЎњ⒵䎇䖭Ͼ㽕∖ˈ៥Ӏ䖬ӮՓ⫼ XStream 䖭Ͼ乍Ⳃˈϟ䴶 催䚂ӊᅶ᠋ッⱘᗻ㛑ˈϡᖙ↣⃵䛑ϞѦ㘨㔥Ϟএϟ䕑䖭ѯ䚂ӊˈা䳔㽕䗮䖛᪡԰ᴀഄ㋏㒳Ёⱘ᭛ӊህৃ ҹᦤৃخFoxmail ϔḋᇚ䚂ӊϟ䕑ࠄᴀഄⱘ㋏㒳ЁˈՓ⫼ϔѯ᭛ӊᴹᇍ䖭ѯ䚂ӊ䖯㸠ֱᄬˈ䖭ѯ ڣ՟བ ᛇࠄⱘህᰃ JavaMail APIˈ঺໪ˈ៥Ӏ䖬䳔㽕ᇍ䚂ӊ䖯㸠ֱᄬˈܜՓ⫼ Java ᓔথ䚂ӊᅶ᠋ッˈ佪 12.1 ᴀゴ⍝ঞⱘᡔᴃ ㅵ⧚䚂ӊेৃˈҢ䖭ѯࡳ㛑ЁԧӮ䖭ѯᅶ᠋ッⱘॳ⧚DŽ ᴀゴ៥Ӏᇚҟ㒡བԩՓ⫼ Java এᓔথϔℒ㞾Ꮕⱘ䚂ӊᅶ᠋ッˈࡳ㛑ᑊϡ䳔㽕໾໡ᴖˈৃҹᬊথ䚂ӊˈ ៥Ӏᬊথ䚂ӊᏺᴹњᮍ֓ˈϡᖙݡএᠧᓔ㔥义এᶹⳟ䚂ӊˈাᠧᓔ䖭ѯᅶ᠋ッˈህৃҹ䕏ᵒⱘᬊথ䚂ӊDŽ ೼᮹ᐌⱘ⫳⌏Ёˈ៥Ӏ䛑Փ⫼ࠄ䆌໮ⱘ䚂ӊᅶ᠋ッˈ՟བ FoxmailǃOutlook ㄝˈ䖭ѯ䚂ӊᅶ᠋㒭 ㄀  ゴ䚂ӊᅶ᠋ッ /Download at http://www.pin5i.com ᠔⼎DŽ ᅶ᠋ッⱘЏ⬠䴶䳔㽕䖯㸠৘⾡ⱘ䚂ӊ໘⧚ˈ಴ℸ⬠䴶Ⳍᇍ䕗Ў໡ᴖˈЏ⬠䴶ⱘ᳔㒜ᬜᵰབ೒ 12.2 12.2.2 ᅶ᠋ッЏ⬠䴶 ᑨⱘ Java ㉏Ў乍ⳂЁⱘ LoginFrameDŽ ⱏᔩ⬠䴶कߚㅔऩˈϔϾ䕧ܹḚ੠ϸϾᣝ䪂ህᵘ៤њⱏᔩ⬠䴶ˈ⚍ߏপ⍜ህ䗔ߎᅶ᠋ッDŽ䆹⬠䴶ᇍ ೒ 12.1 ⱏᔩ⬠䴶 Ѣᇍ䚂ㆅⱘ䜡㕂DŽⱏᔩ⬠䴶བ೒ 12.1 ᠔⼎DŽއ䖲ܹҪⱘ䚂ӊˈপ ៥Ӏ䳔㽕ᦤկϔϾⱏᔩ⬠䴶ˈ䅽⫼᠋⹂ᅮ⫼᠋ৡˈ៥Ӏ䖭䞠ᑊϡ䳔㽕䖯㸠ᆚⷕ偠䆕ˈ಴Ў⫼᠋㛑৺ 12.2.1 ⱏᔩ⬠䴶 ᳔ৢˈ䳔㽕ᦤկϔϾݭ䚂ӊⱘ⬠䴶ˈ䅽⫼᠋এ䕧ܹ䚂ӊֵᙃˈᑊথ䗕ࠄ݋ԧⱘഄഔDŽ ৢˈ䖬䳔㽕᳝ϔϾ䜡㕂⬠䴶ˈ䅽⫼᠋এ䜡㕂䚂ㆅ˄⫼᠋ৡ੠ᆚⷕ˅ǃSMTP ण䆂੠ POP3 ण䆂ㄝֵᙃˈ ֵᙃDŽ᥹ϟᴹˈ䳔㽕ᓎゟϔϾЏ⬠䴶ˈ⫼ᴹ䖯㸠ᶹⳟ䚂ӊǃথ䗕䚂ӊǃ᥹ᬊ䚂ӊ੠໘⧚䚂ӊㄝ᪡԰ˈ✊ 䳔㽕ϔϾ䖯ܹ㋏㒳ⱘ⬠䴶˄ⱏᔩ˅ˈ䅽⫼᠋এ䕧ܹ⫼᠋ৡˈḍ᥂⫼᠋ৡএᓎゟϢ䖭Ͼ⫼᠋ৡⳌ݇ⱘ⫼᠋ ᔧ✊ᰃˈܜ⹂ᅮ㽕ѯҔМ⬠䴶ˈ佪ܜᓎゟⳌ݇ⱘ⬠䴶DŽ៥Ӏ䳔㽕ܜ೼ᓔথ䚂ӊⱘЏ㽕ࡳ㛑ࠡˈ៥Ӏ 12.2 ᓎゟ⬠䴶 བԩՓ⫼DŽ ࠄ乍Ⳃⱘ CLASS_PATH ЁेৃՓ⫼ XStream ⱘ APIDŽJavaMail ੠ XStream ⱘՓ⫼ᇚ೼ҹϟゴ㡖ҟ㒡 ᴀゴЁ᠔Փ⫼ⱘ⠜ᴀЎ 1.3.1ˈϟ䕑ৢᇚ lib ϟⱘ xstream-1.3.1.jar ࣙϢ xpp3_min-1.1.4c.jar ⱘࣙࡴ http://xstream.codehaus.org/download.html ៥ӀৃҹҢϟ䴶ⱘ㔥ഔᕫࠄ XStream˖ 㒳Ёˈ↣⃵᪡԰䚂ӊⱘᯊ׭ˈ䛑ৃҹᇍ䖭ѯ XML ᭛ӊ䖯㸠᪡԰ˈᦤ催њ䚂ӊᅶ᠋ッⱘᗻ㛑DŽ ⱘᗻ㛑DŽ⬅ѢᴀゴЁϡ⍝ঞ᭄᥂ᑧᑨ⫼ˈ಴ℸৃҹᇚ䚂ӊ䕀ᤶ៤ϔѯ XML Ḑᓣⱘ᭛ӊˈֱᄬ೼ᴀഄ㋏ Ёⱘ䚂ӊߴ䰸ˈ䖭ḋᔧ៥Ӏ⌣㾜䚂ӊⱘᯊ׭ˈৃҹϡᖙݡҢ䖰⿟ⱘ䚂ӊ᳡ࡵ఼ϟ䕑ˈᦤ催њ䚂ӊᅶ᠋ッ 䈵DŽ೼ᴀゴЁˈ៥ӀҢ䖰⿟ⱘ䚂ӊ᳡ࡵ఼ᕫࠄ䚂ӊৢˈ䳔㽕ϟ䕑ࠄᴀഄ䖯㸠ֱᄬˈݡᇚ䖰⿟䚂ӊ᳡ࡵ఼ XStream ᰃϔϾㅔऩⱘ Java ᑧˈᅗৃҹᇚ Java ᇍ䈵䕀ᤶ៤ XMLˈгৃҹᇚ XML 䕀ᤶ៤ Java ᇍ XStreamㅔҟ 12.1.4 /Download at http://www.pin5i.com ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\ui\MainFrame.java ᮍ⊩DŽ ໪㾖Ϟ䖭ᑊϡᰃϔỉᷥˈԚ೼ᴀゴЁՓ⫼њᷥˈҹϟᰃᇐ㟾ᷥⱘᅲ⦄ҷⷕˈMainFrame ⱘ createTree ᅶ᠋ッЏ⬠䴶ᇍᑨⱘ㉏Ў MainFrameDŽᅲ⦄೒ 12.2 ⱘ⬠䴶ˈ䳔㽕⊼ᛣⱘᰃᇐ㟾ᷥⱘᅲ⦄ˈ⬅Ѣ೼ ‰ 䆒㕂˖䜡㕂⫼᠋䚂ㆅⳌ݇ⱘֵᙃˈࣙᣀ䋺োǃᆚⷕǃSMTP ण䆂Ϣ POP3 ण䆂ㄝDŽ ‰ 䖬ॳ䚂ӊ˖ᇚ೼ൗഒㆅЁⱘ䚂ӊ䖬ॳࠄॳᴹⱘⳂᔩDŽ ‰ ᕏᑩߴ䰸䚂ӊ˖೼ᴀ㋏㒳Ёᕏᑩߴ䰸䆹䚂ӊˈϔᮺߴ䰸ˈᇚϡৃ㹿䖬ॳDŽ ‰ ߴ䰸䚂ӊ˖ᇚ᠔䗝ᢽⱘ䚂ӊᬒࠄൗഒㆅЁDŽ ‰ 䕀থ䚂ӊ˖Ϣಲ໡䚂ӊϔḋˈԚᰃ䕀থᯊᬊֵҎЎぎDŽ ‰ ಲ໡䚂ӊ˖ᔧ⫼᠋䗝ᢽњᶤϔӑ䚂ӊ⚍ߏಲ໡ᯊˈᔍߎݭ䚂ӊⱘ⬠䴶DŽ ‰ ݭ䚂ӊ˖ᔍߎݭ䚂ӊⱘ⬠䴶ˈ䅽⫼᠋㓪ݭ䚂ӊDŽ ⱘ⬠䴶ˈᇚ䗝ᢽⱘ䚂ӊֵᙃᰒ⼎ࠄݭ䚂ӊⱘ⬠䴶DŽ ‰ থ䗕䚂ӊ˖ᔧ⫼᠋䗝ᢽњᶤϔӑ೼থӊㆅǃᏆথ䗕ǃ㤝〓ㆅ੠ൗഒㆅⱘ䚂ӊᯊˈህᔍߎݭ䚂ӊ ‰ ᬊপ䚂ӊ˖ࠄ䖰⿟ⱘ䚂ӊ᳡ࡵ఼Ёᬊপᮄᬊⱘ䚂ӊDŽ ᠔ᅮН᪡԰ߚ߿᳝˄೒ 12.2 ⱘऎඳĻ⬅Ꮊᕔে˅˖ ೼ĺऎඳˈ䆹䚂ӊⱘ䰘ӊህӮᰒ⼎೼ĹऎඳˈĻऎඳᰃϔϾᎹ݋ᴵˈᄬᬒᇍ䚂ӊⱘ৘Ͼ᪡԰ˈ䆹Ꮉ݋ᴵ ⱘ߫㸼ህᰒ⼎ϡৠ㉏ൟⱘ䚂ӊˈᔧ៥Ӏ⚍ߏњĸऎඳ߫㸼Ёⱘᶤϔᇕ䚂ӊᯊˈ䚂ӊⱘЏ㽕ֵᙃህӮᰒ⼎ ৃҹ䖯ܹࠄᬊӊㆅǃথӊㆅǃᏆথ䗕ǃ㤝〓ㆅ੠ൗഒㆅЁˈᔧ៥Ӏ⚍ߏᇐ㟾ᷥϞⱘ৘Ͼ㡖⚍ᯊˈĸऎඳ ೒ 12.2 Ёᅶ᠋ッⱘЏ⬠䴶Ё㒧ᵘ䕗Ў໡ᴖˈ೒ 12.2 Ёⱘķऎඳˈᰃϔỉᇐ㟾ᷥˈ⚍ߏϡৠⱘ䫒᥹ˈ ೒ 12.2 ᅶ᠋ッЏ⬠䴶 /Download at http://www.pin5i.com ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\box\InBox.java ѨϾ Box ㉏া䳔㽕এ㒻ᡓ AbstractBox ᑊᅲ⦄ getText ᮍ⊩ेৃˈҹϟЎ InBox ㉏ⱘᅲ⦄DŽ } } return getText(); public String toString() { //䞡ݭ toString ᮍ⊩, 䇗⫼᥹ষⱘ getText ᮍ⊩, getText ᮍ⊩⬅ᄤ㉏এᅲ⦄ } return this.icon; } this.icon = new ImageIcon(imagePath); if (this.icon == null) { public ImageIcon getImageIcon(String imagePath) { //ᅲ⦄᥹ষⱘᮍ⊩ private ImageIcon icon; //䆹 box ᠔ᇍᑨⱘ೒⠛(ᰒ⼎೼ᷥϞⱘ೒⠛) public abstract class AbstractBox implements MailBox { ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\box\AbstractBox.java ݡᮄᓎϔϾ᥹䈵㉏ˈ㒭ѨϾ Box ㉏এ㒻ᡓDŽ } ImageIcon getImageIcon(); //䖨ಲᇍᑨ೒ᷛ String getText(); //㦋ᕫ box ৡᄫ public interface MailBox { ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\box\MailBox.java Box ㉏ⱘᅲ⦄DŽ ѨϾ㉏ߚ߿ҷⷕⴔᇐ㟾ᷥЁⱘѨϾϡৠ䫒᥹DŽ঺໪䖬᳝ϔϾ哴ᷛⲥ਀఼㉏੠㡖⚍໘⧚㉏ˈҹϟЎѨϾ ҹϞⱘҷⷕЁˈՓ⫼њѨϾ Box ⱘ㉏˖InBoxǃOutBoxǃSentBoxǃDraftBox ੠ DeletedBoxˈ䖭 } return tree; tree.setCellRenderer(cellRenderer); SailTreeCellRenderer cellRenderer = new SailTreeCellRenderer(); //䆒㕂㡖⚍໘⧚㉏ tree.setRootVisible(false); //䱤㮣ḍ㡖⚍ tree.addMouseListener(new SailTreeListener(this)); //ࡴܹ哴ᷛⲥ਀఼ JTree tree = new JTree(root); //߯ᓎᷥ root.add(new DefaultMutableTreeNode(new DeletedBox())); root.add(new DefaultMutableTreeNode(new DraftBox())); root.add(new DefaultMutableTreeNode(new SentBox())); root.add(new DefaultMutableTreeNode(new OutBox())); root.add(new DefaultMutableTreeNode(new InBox())); //ࡴܹ৘Ͼᄤ㡖⚍ DefaultMutableTreeNode root = new DefaultMutableTreeNode(); //߯ᓎḍ㡖⚍ private JTree createTree() { ߯ᓎᇐ㟾ⱘᷥ// /Download at http://www.pin5i.com DefaultMutableTreeNode selectNode = (DefaultMutableTreeNode)treePath.getLastPathComponent(); //ᕫࠄ㹿䗝Ёⱘ㡖⚍ if (treePath == null) return false; TreePath treePath = tree.getSelectionPath(); //ᕫࠄ䗝Ёⱘ TreePath private boolean isSelected(DefaultMutableTreeNode node, JTree tree) { //߸ᮁϔϾ node ᰃ৺㹿䗝Ё } return this; this.setIcon(box.getImageIcon()); //䆒㕂ᷥ㡖⚍᠔ᰒ⼎ⱘ೒ᷛ else this.setFont(null); if (isSelected(node, tree)) this.setFont(this.selectFont); ᅮՓ⫼ᄫԧއ //߸ᮁᰃ৺䗝Ё, ݡ this.setText(box.getText()); MailBox box = (MailBox)node.getUserObject(); DefaultMutableTreeNode node = (DefaultMutableTreeNode)value; boolean hasFocus) { boolean sel, boolean expanded, boolean leaf, int row, public Component getTreeCellRendererComponent(JTree tree, Object value, } this.selectFont = new Font(null, Font.BOLD, 12); public SailTreeCellRenderer() { private Font selectFont; //ᷥ㡖⚍㹿䗝Ёᯊⱘᄫԧ public class SailTreeCellRenderer extends DefaultTreeCellRenderer { ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\ui\SailTreeCellRenderer.java ⚍໘⧚㉏ SailTreeCellRendererDŽ ϔϾㅔऩⱘⲥ਀఼㉏ˈ哴ᷛⱘ⚍ߏᮍ⊩≵᳝ᅲ⦄ˈ៥Ӏ೼ϟ䴶ゴ㡖Ӯᦤկᅲ⦄DŽҹϟҷⷕᰃᷥⱘ㡖 } } //᱖ᯊᦤկぎᅲ⦄ public void mousePressed(MouseEvent e) { } this.mainFrame = mainFrame; public SailTreeListener(MainFrame mainFrame) { private MainFrame mainFrame; public class SailTreeListener extends MouseAdapter { ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\ui\SailTreeListener.java ᅲ⦄њѨϾ Box ৢˈ៥Ӏৃҹ೼ᷥЁՓ⫼䖭ѨϾ Boxˈҹϟᰃᇐ㟾ᷥⱘ哴ᷛџӊⲥ਀఼㉏DŽ ḋˈ䖭䞠ϡ䌈ߎҷⷕˈ݋ԧৃⳟᴀк᠔䰘ⱘҷⷕDŽ InBox ㉏ҷ㸼ᷥЁⱘᬊӊㆅ㡖⚍ˈা䳔㽕䖨ಲ᭛ᄫϢ೒⠛ेৃDŽ݊ҪⱘಯϾ Box ㉏Ϣ InBox ᅲ⦄ϔ } } return super.getImageIcon("images/in-tree.gif"); public ImageIcon getImageIcon() { } return "ᬊӊㆅ"; public String getText() { public class InBox extends AbstractBox { /Download at http://www.pin5i.com if (this.setupFrame == null) { private void setup() { //䆒㕂ᮍ⊩ˈ ᠧᓔ䆒㕂⬠䴶 }; } setup(); public void actionPerformed(ActionEvent e) { private Action setup = new AbstractAction("䆒㕂", new ImageIcon("images/setup.gif")) { //䆒㕂 ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\ui\MainFrame.java ᠧᓔ䜡㕂⬠䴶ⱘҷⷕDŽ ៪㗙থ䗕䚂ӊDŽ೼ᴀゴЁˈ䜡㕂⬠䴶ᇍᑨⱘ㉏Ў SetupFrameDŽҹϟЎ೼ᅶ᠋ッЏ⬠䴶˄MainFrame˅ ֵᙃᑊ⚍ߏ⹂ᅮৢˈ䜡㕂⬠䴶ᇚ݇䯁ˈϔᮺ䜡㕂ᅠৢˈᭈϾᅶ᠋ッᇚӮҹ䖭ѯ䜡㕂Ўձ᥂ˈএᬊপ䚂ӊ 䆹⬠䴶ᑊ≵᳝໮໡ᴖⱘ㒧ᵘˈাᰃ⬅޴Ͼ᱂䗮ⱘ JLabel Ϣ JTextField 㒘៤ˈᔧ⫼᠋฿ݭᅠⳌ݇ⱘ ೒ 12.3 䜡㕂⬠䴶 ᳡ࡵ఼੠ POP3 ッষˈ䜡㕂⬠䴶བ೒ 12.3 ᠔⼎DŽ 䜡㕂⬠䴶Џ㽕⫼ᴹ䜡㕂⫼᠋ⱘ䚂ㆅֵᙃˈࣙᣀ䚂ㆅ䋺োǃᆚⷕǃSMTP ᳡ࡵ఼ǃSMTP ッষǃPOP3 12.2.3 䜡㕂⬠䴶 code\foxmail\src\org\crazyit\foxmail\ui\MainFrame.javaDŽ ᇍ䈵ˈĸĹĺĻऎඳⱘᅲ⦄೼䖭䞠ϡݡ䆺㒚ᦣ䗄ˈ䇋ⳟҷⷕ⏙ऩⱘ Ͼ JTableˈĹऎඳᰃϔϾ JList ᇍ䈵ˈĺऎඳᰃϔϾ JTextArea ᇍ䈵ᑊϡৃ㓪䕥ˈĻऎඳᰃϔϾ JToolBar ҹϞЎ೒ 12.2 Ёķऎඳᇐ㟾ᷥⱘᅲ⦄ˈᭈϾ⬠䴶ⱘᏗሔৃҹՓ⫼ JSplitPane ᴹᅲ⦄ˈĸऎඳᰃϔ ᑨⱘ೒⠛Ϣ᭛ᄫˈݡ䆒㕂ࠄᷥⱘ㡖⚍Ёˈ᳔ৢ߸ᮁ㡖⚍ᰃ৺㹿䗝Ёˈᴹ䆒㕂ϡৠⱘᄫԧDŽ ಴ℸা䳔㽕ᔎࠊ㉏ൟ䕀ᤶЎ MailBox ेৃˈḍ᥂ᕫࠄⱘ㡖⚍ᇍᑨⱘ MailBox ᇍ䈵ˈৃҹ㦋ᕫ䆹 Box ᇍ ℸˈҹϞҷⷕⱘ咥ԧ䚼ߚˈ៥Ӏৃҹᕫࠄ䖭ѯ Box ᇍ䈵ˈ㗠ѨϾ Box ᇍ䈵䛑ᰃ MailBox ᥹ষⱘᅲ⦄ˈ ⬅Ѣ៥Ӏ೼߯ᓎᷥ㡖⚍ⱘᯊ׭ˈՓ⫼ⱘᰃѨϾ Box ԰Ў DefaultMutableTreeNode ⱘᵘ䗴খ᭄ˈ಴ } } return false; if (node.equals(selectNode)) return true; བᵰ䗝Ёⱘ㡖⚍Ϣখ᭄ⱘ㡖⚍ϔ㟈, 䙷Мᛣੇⴔখ᭄ⱘ㡖⚍㹿䗝Ё// /Download at http://www.pin5i.com ԧᅲ⦄ᮍᓣϢЏ⬠䴶˄MainFrame˅ⱘᅲ⦄㉏Ԑˈ㗠Ϩ݋ԧⱘ᪡԰гϢЏ⬠䴶ⳌԐˈᔧ⫼᠋⚍ߏњ㓪ݭ 䚂ӊ㓪ݭ⬠䴶೼ᴀゴЁᇍᑨⱘ㉏Ў MailFrameˈᭈϾ⬠೼ⱘᏗሔ䛑ৃҹՓ⫼ JSplitPane ᴹᅲ⦄ˈ݋ Ёߴ䰸DŽ ‰ ߴ䰸䰘ӊ˖ᔧ⫼᠋೼䰘ӊⱘ߫㸼Ё˄JList˅䗝ᢽњϔӑ䰘ӊ⚍ߏߴ䰸ᯊˈ䆹䰘ӊᑊҢ䰘ӊ߫㸼 ‰ ๲ࡴ䰘ӊ˖Ўℷ೼㓪ݭⱘ䚂ӊ๲ࡴϔϾ䰘ӊˈ๲ࡴᅠৢᇚ೼೒ 12.4 ⱘऎඳķ˄JList˅Ёᰒ⼎DŽ ‰ ֱᄬ㟇㤝〓ㆅ˖ᇚ䆹ᇕ䚂ӊֱᄬࠄ㤝〓ㆅЁˈৃҹ԰݊Ҫ䚂ӊⱘ῵ᵓDŽ ‰ ֱᄬ㟇থӊㆅ˖ᇚ䆹ᇕℷ೼㓪ݭⱘ䚂ӊֱᄬࠄথӊㆅЁˈㄝᕙϟ⃵᪡԰ݡ䖯㸠থ䗕DŽ ‰ থ䗕˖ᇚ䚂ӊ偀Ϟথ䗕ࠄᬊӊҎЁDŽ ĹᰃϔϾ JToolBarˈৃҹᇍ䆹ᇕℷ೼㓪ݭⱘ䚂ӊ䖯㸠ϔ㋏߫ⱘ᪡԰ˈҢᎺࠄেⱘ᪡԰ߚ߿Ў˖ ೒ 12.4 Ёⱘऎඳķᰃ䚂ӊⱘ䰘ӊᄬᬒऎඳˈᇍᑨϔϾ JList ᇍ䈵ˈऎඳĸᰃ䚂ӊⱘℷ᭛ऎඳˈऎඳ ೒ 12.4 䚂ӊ㓪ݭ⬠䴶 Ϣ䰘ӊˈᄬᬒ䰘ӊৃҹᦤկϔϾ JList ᴹᅲ⦄DŽ䚂ӊ㓪ݭ⬠䴶བ೒ 12.4 ᠔⼎DŽ JTextField ੠ϔϾ JTextArea ᴹ䅽⫼᠋䕧ܹⳌ݇ⱘ䚂ӊֵᙃˈ՟བᬊӊҎǃᡘ䗕ǃ䚂ӊЏ乬ǃ䚂ӊݙᆍ ᳔ৢᴹᅠ៤ᭈϾᅶ᠋ッⱘ᳔ৢϔϾ⬠䴶˖㓪ݭ䚂ӊ⬠䴶DŽ䚂ӊ㓪ݭ⬠䴶ᑊϡ໡ᴖˈাᰃᦤկϔѯ 12.2.4 䚂ӊ㓪ݭ⬠䴶 } this.setupFrame.setVisible(true); } this.setupFrame = new SetupFrame(this); /Download at http://www.pin5i.com } //ⳕ⬹৘Ͼሲᗻⱘ setter ੠ getter ᮍ⊩ private boolean reset = false; //ᰃ৺䖯㸠䞡㕂ֵᙃ private int pop3Port; //pop3 ⱘッষ private String pop3Host; //pop3 䚂ӊ᳡ࡵ఼ private int smtpPort; //smtp ッষ private String smtpHost; //smtp 䚂ӊ᳡ࡵ఼ private String password; //ᆚⷕ private String account; //⫼᠋Ꮴো private String user; //㋏㒳⫼᠋ public class MailContext { ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\ui\MailContext.java SMTP ッষǃPOP3 ण䆂੠ POP3 ッষˈᮄᓎ MailContext ㉏ˈ䆹㉏೼ϟ䴶ᇚӮकߚᐌ⫼DŽ 䳔㽕߯ᓎϔϾ䚂ӊϞϟ᭛ᇍ䈵ˈ⫼Ѣֱᄬ䚂ᴵㆅⱘ䖲᥹ֵᙃˈࣙᣀ䋺োǃᆚⷕǃSMTP ण䆂ǃܜ佪 12.3.1 ߯ᓎ䚂ㆅϞϟ᭛ᇍ䈵 ⱘ䚂ӊᇍ䈵ᰃ Messageˈ಴ℸ䆹᥹ষ䳔㽕ᇚ Message 䕀ᤶ៤ Mail ᇍ䈵DŽ ϞDŽ䰸ℸП໪䖬䳔㽕ϔϾ䚂ӊⱘ䇏প᥹ষˈ⫼Ѣ䇏প䚂ӊˈ⬅Ѣ៥ӀՓ⫼ JavaMail Ң䚂ӊ᳡ࡵ఼ᕫࠄ ໛དˈ՟བϢ䚂ㆅⳌ݇ⱘ䚂ӊޚܜ䖭ѯџࠡˈ៥Ӏ䳔㽕ᇚ䚂ӊⱘ৘Ͼᇍ䈵خᰒ⼎೼ᬊӊㆅⱘ߫㸼Ёˈ೼ ᥹ᬊ䚂ӊ៥ӀՓ⫼ POP3 ण䆂ˈ䗮䖛⫼᠋䜡㕂ⱘ䋺ো੠ᆚⷕˈࠄ䖰⿟ⱘ䚂ӊ᳡ࡵ఼Ёϟ䕑䚂ӊˈᑊ 12.3 ߯ᓎᅶ᠋ッⱘ෎⸔ᇍ䈵 ࡳ㛑ˈࣙᣀֱᄬ⫼᠋ⱘ䜡㕂ǃ᥹ᬊ䚂ӊǃথ䗕䚂ӊ੠᪡԰䚂ӊㄝࡳ㛑DŽ ࠄℸˈ៥Ӏ䖭Ͼ䚂ӊᅶ᠋ッⱘ᠔᳝⬠䴶䛑Ꮖ㒣ᅠ៤њˈ᥹ϟᴹ៥Ӏᓔྟ㓪ݭ䖭Ͼ䚂ӊᅶ᠋ッⱘЏԧ } this.mailFrame.setVisible(true); //㓪ݭ䚂ӊⱘ⬠䴶ᇍ䈵 mailFrame private void write() { }; } write(); public void actionPerformed(ActionEvent e) { private Action write = new AbstractAction("ݭ䚂ӊ", new ImageIcon("images/new.gif")) { //ݭ䚂ӊ ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\ui\MainFrame.java ⱘҷⷕDŽ 䚂ӊⱘᯊ׭ˈህ䳔㽕ᰒ⼎䆹⬠䴶DŽҹϟЎ೼Џ⬠䴶˄MainFrame˅Ёᠧᓔ䚂ӊ㓪ݭ⬠䴶˄MailFrame˅ /Download at http://www.pin5i.com MailContextˈ䞡ᮄ㦋ᕫ Store ᇍ䈵DŽҹϞҷⷕЁⱘ咥ԧ䚼ߚᰃՓ⫼ JavaMail এ䖲᥹䚂ㆅDŽ 㸼⼎䆹 MailContext ᇍ䈵ᰃ৺㹿᳈ᬍ䖛ˈབᵰ⫼᠋䞡ᮄ䆒㕂њ䚂ㆅ䋺োǃᆚⷕㄝֵᙃˈ䙷Мህ䳔㽕ਞ䆝 㒳ⱘᓔ䫔䛑ᕜ໻ˈা䳔㽕ϔ⃵߯ᓎˈेৃϔⳈՓ⫼䆹ᅲ՟DŽ঺໪ˈ៥Ӏ䖬ᦤկњϔϾ reset ⱘᏗᇨؐˈ JDBC Ёⱘ Connection ᇍ䈵ⱘ߯ᓎϔḋˈ߯ᓎϔ⃵ Connection ㋏ ڣ݅⫼ϔϾ Stroe ᇍ䈵ेৃˈ䖭ህད ӊ᳡ࡵ఼ˈ಴ℸ߯ᓎ៪㗙䖲᥹䛑䳔㽕ㄝᕙ䕗䭓ᯊ䯈ˈ㗠៥Ӏϔᮺ䜡㕂དњПৢˈህ᮴䳔↣⃵䛑এ߯ᓎˈ Ўぎ៪㗙≵᳝䖲᥹Ϟⱘᯊ׭ˈህ䖯㸠 Store ᇍ䈵ⱘ߯ᓎˈ䖭ᰃ⬅Ѣ Store ᇍ䈵߯ᓎ䳔㽕䖲᥹ࠄ䖰⿟ⱘ䚂 ೼䖭䞠໪⬠ৃҹ䗮䖛ϔϾ getStore ⱘᮍ⊩ᕫࠄ Store ᇍ䈵ˈ㗠Ϩ䳔㽕㒣䖛߸ᮁˈབᵰᔧ Store ᇍ䈵 } }; } return new PasswordAuthentication(getAccount(), getPassword()); protected PasswordAuthentication getPasswordAuthentication() { return new Authenticator() { private Authenticator getAuthenticator() { //ᇚ䋺োϢᆚⷕᇕ㺙៤ϔϾ JavaMail ⱘ Authenticator ᇍ䈵ˈ㸼⼎䳔㽕㒣䖛䑿ӑ偠䆕 } return this.store; } } throw new MailConnectionException("䖲᥹䚂ㆅᓖᐌˈ䇋Ẕᶹ䜡㕂"); e.printStackTrace(); } catch (Exception e) { this.store = store; store.connect(); Store store = session.getStore(url); ᇍ䈵ټ //ᕫࠄ䚂ㆅⱘᄬ getAccount(), getPassword()); URLName url = new URLName("pop3", getPop3Host(), getPop3Port(), null, //Փ⫼ pop3 ण䆂᥹ᬊ䚂ӊ Session session = Session.getDefaultInstance(props, getAuthenticator()); //߯ᓎ mail ⱘ Session Properties props = System.getProperties(); try { if (this.store == null || !this.store.isConnected()) { } this.reset = false; this.session = null; this.store = null; if (this.reset) { //䞡㕂њֵᙃ, 䆒㕂 Store Ў null public Store getStore() { //䖨ಲ Store ᇍ䈵 private Store store; ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\ui\MailContext.java DŽټ׭ˈৃҹ偀Ϟ䖨ಲ䖭Ͼ Store ᇍ䈵ˈ䆹ᇍ䈵ҷ㸼䚂ㆅⱘҧ 䰸њ䳔㽕ᦤկҹϞҷⷕⱘሲᗻ໪ˈ䖬䳔㽕ᦤկϔϾ JavaMail ⱘ Store ᇍ䈵ˈᔧ໪⬠䇋∖䖭Ͼ㉏ⱘᯊ /Download at http://www.pin5i.com ҹϞЎϸϾ೼ᴀゴЁ᳔෎⸔ⱘϸϾᇍ䈵ˈϟ䴶ᓔྟᅲ⦄ⱏᔩࡳ㛑੠⫼᠋䜡㕂ࡳ㛑DŽ ⱘ䆱ॳᴹ᭛ӊⱘ᭛ӊৡᇚӮ϶༅ˈ಴ℸ䳔㽕Փ⫼ sourceName ᴹֱᄬ⑤᭛ӊⱘৡᄫDŽخ ӊⱘৡ⿄ˈᔧ៥Ӏᇚ᭛ӊֱᄬ೼ᴀഄⱘᯊ׭ˈ䳔㽕ᇚ䰘ӊ䞡ੑৡᑊҹ UUID ԰Ў䖭ѯ᭛ӊⱘৡ⿄ˈ䖭ḋ FileObject াֱᄬϔϾ sourceName ੠ϔϾ File ᇍ䈵ˈsourceName 㸼⼎⫼᠋೼ϞӴ䰘ӊᯊⱘ⑤᭛ } //ⳕ⬹ setter ੠ getter ᮍ⊩ private File file; //ᇍᑨⱘ᭛ӊ private String sourceName; //⑤᭛ӊৡᄫ public class FileObject { ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\object\FileObject.java ᠋ッЁҷ㸼ϔϾ FileObject ᇍ䈵ˈҹϟᰃ FileObject ⱘሲᗻDŽ ೼ҹϞⱘҷⷕЁˈMail ᇍ䈵䖬᳝ϔϾ files ሲᗻˈҷ㸼䆹䚂ӊ᠔ᇍᑨⱘ᠔᳝䰘ӊˈ↣ϔϾ䰘ӊ೼ᅶ 䏃ᕘDŽ ᖙ乏ᰃଃϔⱘˈ಴ℸ䳔㽕Փ⫼ UUID ᴹ⫳៤DŽ䖭Ͼ xmlName াᰃϔϾ᭛ӊৡˈᑊϡᰃ XML ᭛ӊⱘ㒱ᇍ ᗻˈ䆹ሲᗻҷ㸼䆹 Mail ᇍ䈵᠔ᇍᑨⱘ XML ᭛ӊⱘ᭛ӊৡˈ䆒㕂 Mail ⱘ xmlName ⱘሲᗻᯊˈ䆹᭛ӊৡ ಴ℸ䖭䞠ⱘ Mail ᇍ䈵ህᰃ៥Ӏ䳔㽕䕀ᤶⱘᇍ䈵ˈ䙷М៥Ӏህ䳔㽕Ў Mail ᇍ䈵ᦤկϔϾ xmlName ⱘሲ ᣝ✻៥Ӏ᳔߱ⱘ㽕∖ˈབᵰҢ䖰⿟ⱘ䚂ӊ᳡ࡵ఼ϟ䕑њ䚂ӊৢˈ䳔㽕ᇚ䚂ӊ䕀ᤶ៤ϔӑ XML ᭛ӊˈ } //ⳕ⬹ setter ੠ getter ᮍ⊩ private String from; ᇍᑨⱘᬒމ //䚂ӊᴹ⑤, བᵰᰃҢ䚂ㆅᬊࠄⱘ, ߭ᬒ೼ INBOX, ݊Ҫᚙ private List files; //䰘ӊ private List ccs; //ᡘ䗕 private String content; //䚂ӊℷ᭛ private boolean hasRead; //ᰃ৺Ꮖ㒣㹿䯙䇏 private String size; //䚂ӊ໻ᇣ private Date receiveDate; //᮹ᳳ private String subject; //䚂ӊᷛ乬 private List receivers; //ᬊӊҎ, ৃҹ໮Ͼ private String sender; //থ䗕Ҏ private String xmlName; //೼ᴀഄ㋏㒳Ёҷ㸼䆹䚂ӊⱘ xml ᭛ӊⱘৡᄫ, Փ⫼ UUID ԰Ў᭛ӊৡ˖uuid.xml public class Mail { ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\object\Mail.java ⱘ৘ϾሲᗻབϟDŽ ೼៥Ӏⱘᅶ᠋ッЁˈ䳔㽕ᦤկϔϾ Mail ᇍ䈵ˈ೼ᴀ㋏㒳ЁϔϾ Mail ᇍ䈵ҷ㸼ϔᇕ䚂ӊˈMail ᇍ䈵 ߯ᓎ䚂ӊᇍ䈵 12.3.2 /Download at http://www.pin5i.com private static Properties getProperties(File propertyFile) throws IOException { //ḍ᥂᭛ӊᕫࠄ Properties ᇍ䈵 public class PropertiesUtil { ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\util\PropertiesUtil.java ⱘᅲ⦄DŽ PropertiesUtil ㉏䖯㸠߯ᓎ MailContext ᇍ䈵ˈৠᯊгՓ⫼њ FileUtil ⱘ޴Ͼᐌ䞣ˈҹϟᰃ PropertiesUtil ӊ䞠䴶ⱘݙᆍ˄MailContext Ёⱘሲᗻ˅䕀ᤶ៤ MailContext ᇍ䈵ˈݡᰒ⼎Џ⬠䴶ᇍ䈵DŽҷⷕЁՓ⫼њ Ў䆹⫼᠋ᇍᑨⱘⳂᔩᰃ৺ᄬ೼ˈݡ㦋ᕫ䆹⫼᠋ⱘ䜡㕂᭛ӊˈݡᇚ䜡㕂᭛ޚ⫼᠋ᰃ৺㄀ϔ⃵ⱏᔩˈ߸ᮁᷛ ߸ᮁ䆹ܜˈ⫣೼ҹϞⱘҷⷕЁˈ⫼᠋⚍䕧ܹ⫼᠋ৡᑊ⚍ߏ⹂ᅮ䖯㸠ⱏᔩˈህᠻ㸠ҹϞⱘ confirm ᮍ } } throw new LoginException("䜡㕂᭛ӊ䫭䇃"); } catch (IOException e) { this.setVisible(false); //䱤㮣ⱏᔩ⬠䴶 this.mainFrame.setVisible(true); //ᰒ⼎Џ⬠䴶 this.mainFrame = new MainFrame(ctx); //߯ᓎ㋏㒳⬠䴶Џᇍ䈵 ctx.setUser(user); //䆒㕂 MailContext ⱘ user ሲᗻ MailContext ctx = PropertiesUtil.createContext(config); //䇏প䜡㕂ᑊ䕀ᤶЎ MailContext ᇍ䈵 if (!config.exists()) config.createNewFile(); //≵᳝ᇍᑨⱘ䜡㕂᭛ӊˈ߭߯ᓎ try { File config = new File(folder.getAbsolutePath() + FileUtil.CONFIG_FILE); //ᕫࠄ䜡㕂᭛ӊ if (!folder.exists()) folder.mkdir(); //䆹⫼᠋ⱘⳂᔩϡᄬ೼ˈҷ㸼䆹⫼᠋㄀ϔ⃵䖯ܹ㋏㒳ˈ߯ᓎ⫼᠋Ⳃᔩ File folder = new File(FileUtil.DATE_FOLDER + user); //ᕫࠄ⫼᠋ৡᇍᑨⱘⳂᔩ String user = this.userField.getText(); //ᕫࠄ⫼᠋ৡˈৃҹ೼ℸࡴܹ偠䆕ˈབᵰЎぎˈ߭ᦤ⼎ᑊ䖨ಲ private void confirm() { ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\ui\LoginFrame.java এ߯ᓎ៪㗙ᅮԡࠄሲѢ䆹⫼᠋ⱘⳂᔩDŽҹϟЎ LoginFrame ⱘ⹂ᅮᮍ⊩DŽ ೼ᴀゴЁˈ⫼᠋ⱏᔩᑊϡ䳔㽕䖯㸠ᆚⷕ偠䆕ˈা䳔㽕⫼᠋এ⹂䅸㞾Ꮕⱘ⫼᠋ৡˈ㋏㒳ḍ᥂䖭Ͼ⫼᠋ 12.4.1 ᅲ⦄⫼᠋ⱏᔩ ᴹᅲ⦄⫼᠋ⱏᔩⱘࡳ㛑DŽܜⳂᔩˈϟ䴶 ⱘ䚂ㆅㄝˈབᵰϔϾ⫼᠋Փ⫼໮Ͼϡৠⱘ䚂ㆅˈ䙷Мህ䳔㽕೼⫼᠋ᇍᑨⱘⳂᔩϟݡ߯ᓎ໮Ͼϡৠⱘ䋺ো 㽕Ў䖭Ͼ⫼᠋ৡ߯ᓎϔϾ݋ԧⱘⳂᔩˈ⫼ᴹֱᄬ䖭Ͼ⫼᠋ⱘֵᙃˈ՟བ䆹⫼᠋᠔Փ⫼ⱘ䜡㕂ǃ݋ԧՓ⫼ ೼ 12.2.1 ੠ 12.2.3 Ёˈ៥ӀᦤկњϔϾⱏᔩ⬠䴶੠ϔϾ䜡㕂ⱘ⬠䴶ˈᔧ⫼᠋䖯㸠ⱏᔩᯊˈ៥Ӏ䳔 ⱏᔩϢ⫼᠋䜡㕂᠋⫼ 12.4 /Download at http://www.pin5i.com this.smtpField.setText(ctx.getSmtpHost()); this.passwordField.setText(ctx.getPassword()); this.accountField.setText(ctx.getAccount()); //߱ྟ࣪৘Ͼ䜡㕂 ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\ui\SetupFrame.java ϔϾ MailContext ᇍ䈵DŽҹϟᰃ SetupFrame Ё߱ྟ࣪৘Ͼ䜡㕂ⱘҷⷕDŽ MailContext ᇍ䈵Ӵ㒭 SetupFrameˈ⊼ᛣˈᔧᅶ᠋ッਃࡼⱘᯊ׭ˈা㛑᳝ϔϾ䚂ㆅϞϟ᭛ˈेা㛑ᄬ೼ ᙃӴ䗦㒭䆹⬠䴶˄SetupFrame˅ˈৃҹЎ MainFrame ᦤկϔϾ getMailContext ⱘᮍ⊩ˈᇚᇍᑨⱘ ܹ䜡㕂⬠䴶ˈ䙷М৘Ͼ䜡㕂ֵᙃ䛑ᰃぎⱘˈབᵰϡᰃ㄀ϔ⃵䖯ܹ䆹⬠䴶ˈ䙷Мህ䳔㽕ᇚҹࠡ᠔䜡㕂ⱘֵ ೼ 12.2.3 ЁˈᏆᮄᓎњϔϾ⫼᠋ⱘ䜡㕂⬠䴶ˈᑊϨᦣ䗄њབԩҢЏ⬠䴶䖯ܹˈབᵰ⫼᠋ᰃ㄀ϔ⃵䖯 12.4.2 ᅲ⦄⫼᠋ֵᙃ䜡㕂 ⱘ⏏ࡴݙᆍ˄⫼⿟ᑣ⏏ࡴ˅ˈᇍ⫼᠋ⱘ䚂ㆅ䖯㸠䜡㕂DŽ ಴ℸ mail.properties ᭛ӊⱘݙᆍЎぎDŽ䖭ḋˈ⫼᠋ⱏᔩⱘࡳ㛑ህᏆᅲ⦄ˈ᥹ϟЎ៥ӀЎ mail.properties ϟህ᳝ϔϾ⫼᠋Ⳃᔩˈ⫼᠋ⳂᔩᑊϨᄬ೼Ѣϔӑ mail.properties ⱘ䜡㕂᭛ӊˈ⬅Ѣ⦄೼≵᳝ӏԩⱘ䜡㕂ˈ ᅲ⦄ҹϞⱘҷⷕৢˈ䖤㸠⿟ᑣˈ䕧ܹ⫼᠋ৡ䖯㸠ⱏᔩˈৃҹⳟࠄ乍Ⳃϟ᳝ϔϾ data Ⳃᔩˈ䆹Ⳃᔩ } public static final String CONFIG_FILE = File.separator + "mail.properties"; //ᄬᬒ݋ԧᶤϾ⫼᠋䜡㕂ⱘ properties ᭛ӊ public static final String DATE_FOLDER = "datas" + File.separator; //ᄬᬒ᠔᳝⫼᠋᭄᥂ⱘⳂᔩ public class FileUtil { ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\util\FileUtil.java FileUtil ⱘ޴Ͼᐌ䞣DŽ PropertiesUtil ᇚ⫼᠋䜡㕂᭛ӊЁⱘ޴Ͼሲᗻ䇏পˈᑊᇕ㺙៤ϔϾ MailContext ᇍ䈵䖨ಲˈҹϟᰃ | } return Integer.parseInt(s); if (s == null || s.trim().equals("")) return defaultValue; private static Integer getInteger(String s, int defaultValue) { //ᇚখ᭄ s 䕀ᤶ៤ϔϾ Integer ᇍ䈵ˈ䆹ᄫヺІЎぎ߭䖨ಲখ᭄Ёⱘ咬䅸ؐ } props.getProperty("pop3Host"), pop3Port); props.getProperty("smtpHost"), smtpPort, props.getProperty("account"), props.getProperty("password"), return new MailContext(null, Integer pop3Port = getInteger(props.getProperty("pop3Port"), 110); //བᵰ≵᳝䜡㕂 pop3 ⱘッষˈ߭Փ⫼咬䅸ⱘ 110 ッষ Integer smtpPort = getInteger(props.getProperty("smtpPort"), 25); //བᵰ≵᳝䜡㕂 smtp ⱘッষˈ߭Փ⫼咬䅸ⱘ 25 ッষ Properties props = getProperties(propertyFile); public static MailContext createContext(File propertyFile) throws IOException { //ḍ᥂䜡㕂᭛ӊⱘᇍ䈵ᴹᵘ䗴 MailContext ᇍ䈵 } return prop; prop.load(fis); FileInputStream fis = new FileInputStream(propertyFile); Properties prop = new Properties(); /Download at http://www.pin5i.com File propFile = new File(FileUtil.DATE_FOLDER + ctx.getUser() + //ᕫࠄ䜡㕂᭛ӊ mail.properties public static void store(MailContext ctx) { //ֱᄬϔϾ MailContext ᇍ䈵ˈ ᇚᅗⱘሲᗻݭܹ᭛ӊЁ ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\util\PropertiesUtil.java ҹϞⱘҷⷕЁՓ⫼ PorpertiesUtil Ϣ FileUtil ⱘϸϾᮍ⊩ˈϟ䴶ᰃ䖭ϸϾᮍ⊩ⱘᅲ⦄DŽ 䚂ӊⱘᯊ׭˄े䇗⫼ getStore ᮍ⊩˅ˈህৃҹՓ⫼ᮄⱘ䜡㕂ᴹ䞡ᮄᕫࠄ Store ᇍ䈵DŽ ⱘ৘Ͼሲᗻˈ಴ℸ䳔㽕ਞ䆝 MailContext ⫼᠋᳈ᮄњ䜡㕂˄䆒㕂 reset ؐЎ true˅ˈ䙷М⫼᠋೼ϟ⃵᥹ᬊ getMailContext ᮍ⊩ᴹ䆒㕂MailContext ⱘ৘ϾሲᗻDŽ೼䖭䞠䳔㽕⊼ᛣⱘᰃˈ⬅Ѣ䞡ᮄ䆒㕂њMailContext ⫼᠋䕧ܹњ⬠䴶Ё৘Ͼ㒘ӊⱘؐ⚍ߏ⹂ᅮৢˈⳈ᥹䇗⫼ҹϞⱘ confirm ᮍ⊩ˈconfirm ᮍ⊩䇗⫼ } return ctx; ctx.setReset(true); //⬅Ѣ䞡ᮄ䆒㕂њ䖲᥹ֵᙃ, ಴ℸ䆒㕂 MailContext ⱘ reset ؐЎ true ctx.setPop3Port(pop3Port); ctx.setPop3Host(pop3Host); ctx.setSmtpPort(smtpPort); ctx.setSmtpHost(smtpHost); ctx.setPassword(password); ctx.setAccount(account); //ⳕ⬹Ң⬠䴶ᕫࠄ৘Ͼؐˈ䆒㕂 MailContext ⱘ৘Ͼሲᗻ private MailContext getMailContext(MailContext ctx) { //ḍ᥂⬠䴶ⱘؐᇕ㺙 MailContext } } JOptionPane.OK_CANCEL_OPTION); JOptionPane.showConfirmDialog(this, e.getMessage(), "䄺ਞ", } catch (Exception e) { this.setVisible(false); FileUtil.createFolder(ctx); //߯ᓎᄬᬒ䚂ӊⱘⳂᔩ(ᑊ䴲⫼᠋ⱘⳂᔩ, ϔϾ⫼᠋ৃ㛑᳝໮Ͼ䚂ㆅഄഔ) this.mainFrame.setMailContext(ctx); //䆒㕂Џ⬠䴶ⱘ MailContext ᇍ䈵 PropertiesUtil.store(ctx); //ᇚᮄⱘϞϟ᭛ݭܹ䜡㕂᭛ӊЁ ctx.setReset(true); //䆒㕂Ꮖ㒣ᇍֵᙃ䖯㸠њ䞡ᮄ䆒ᅮ MailContext ctx = getMailContext(this.mainFrame.getMailContext()); //䞡ᮄ䆒㕂㋏㒳Ϟϟ᭛ⱘֵᙃ try { private void confirm() { //⹂ᅮᣝ䪂 ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\ui\SetupFrame.java Ёⱘ mail.properties˅DŽҹϟЎ SetupFrame 䚼ߚҷⷕDŽ ӊⱘؐDŽᔧ⫼᠋䕧ܹњ৘Ͼֵᙃৢˈ៥Ӏህ䳔㽕ᇚ䖭ѯ䜡㕂ֵᙃֱᄬࠄ䆹⫼᠋ⱘ䜡㕂᭛ӊЁ˄े 12.4.1 SetupFrame ೼ᵘ䗴ⱘᯊ׭ˈህ䳔㽕ḍ᥂ MainFrame Ӵ䗦䖛ᴹⱘ MailContext এ߱ྟ࣪৘Ͼ⬠䴶㒘 this.pop3PortField.setText(String.valueOf(ctx.getPop3Port())); this.smtpPortField.setText(String.valueOf(ctx.getSmtpPort())); this.pop3Field.setText(ctx.getPop3Host()); /Download at http://www.pin5i.com ᳔ৢݡਞ䆝 MailContext 䳔㽕᳈ᮄ䜡㕂DŽϟ䴶ᅲ⦄᥹ᬊ䚂ӊⱘࡳ㛑DŽ SetupFrame ⱘ㘠䋷Ꮖᅠ៤њˈЏ㽕⫼ᴹ䜡㕂⫼᠋ֵᙃˈݭࠄ䜡㕂᭛ӊЁᑊ⫳៤ᇍᑨⱘ⫼᠋Ⳃᔩˈ ϟ䴶߯ᓎϔϾ user1 ⱘⳂᔩˈᑊϨӮ೼ user1 ⱘⳂᔩϟ䴶ݡ߯ᓎѨϾ Box Ⳃᔩ˄ᬊӊㆅㄝ˅DŽ ⱘ createFolder ᮍ⊩Џ㽕⫼ᴹ⫳៤䋺োⱘ৘ϾⳂᔩˈ՟བ᳝ϔϾ⫼᠋ি user1ˈ䙷МᇚӮЎҪ೼ datas PropertiesUtil ⱘ store ᮍ⊩Џ㽕ᇚ MailContext ⱘ৘Ͼሲᗻݭࠄ⫼᠋ⱘ mail.properties ЁˈFileUtil } return accountRoot; File.separator + ctx.getAccount() + File.separator; String accountRoot = DATE_FOLDER + ctx.getUser() + private static String getAccountRoot(MailContext ctx) { //ᕫࠄ䚂ӊᏤোⱘḍⳂᔩ } if (!file.exists()) file.mkdir(); private static void mkdir(File file) { //߯ᓎⳂᔩⱘᎹ݋ᮍ⊩, ߸ᮁⳂᔩᰃ৺ᄬ೼ } mkdir(new File(accountRoot + FILE));//䰘ӊᄬᬒⳂᔩ mkdir(new File(accountRoot + DELETED));//ൗഒㆅ mkdir(new File(accountRoot + DRAFT));//㤝〓ㆅ mkdir(new File(accountRoot + SENT));//Ꮖথ䗕 mkdir(new File(accountRoot + OUTBOX));//থӊㆅ mkdir(new File(accountRoot + INBOX));//߯ᓎ INBOX Ⳃᔩ mkdir(new File(accountRoot)); //᳝ϔϾ䚂ӊ䋺োᰃ abc@mail.com 䙷МᇚӮ೼ datas/user1/ϟ⫳៤ϔϾ abc@mail.com Ⳃᔩ //Փ⫼⫼᠋ᔧࠡ䆒㕂ⱘᏤোᴹ⫳៤Ⳃᔩ, ՟བϔϾ⫼᠋ি user1, String accountRoot = getAccountRoot(ctx); public static void createFolder(MailContext ctx) { //߯ᓎ⫼᠋ⱘᏤোⳂᔩ੠Ⳍ݇ⱘᄤⳂᔩ public static final String FILE = "file"; //䰘ӊⱘᄬᬒⳂᔩৡ public static final String DELETED = "deleted"; //ൗഒㆅⱘⳂᔩৡ public static final String DRAFT = "draft"; //㤝〓ㆅⱘⳂᔩৡ public static final String SENT = "sent"; //Ꮖথ䗕ⱘⳂᔩৡ public static final String OUTBOX = "outbox"; //থӊㆅⱘⳂᔩৡ public static final String INBOX = "inbox"; //ᬊӊㆅⱘⳂᔩৡ ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\util\FileUtil.java } fos.close(); prop.store(fos, "These are mail configs."); FileOutputStream fos = new FileOutputStream(propFile); //ݭܹ䜡㕂᭛ӊ //ⳕ⬹䆒㕂৘Ͼሲᗻⱘҷⷕ Properties prop = getProperties(propFile); FileUtil.CONFIG_FILE); /Download at http://www.pin5i.com } } throw new LoadMailException(e.getMessage()); } catch (Exception e) { return null; inbox.close(false); } //ᠧॄԴ᠔䳔㽕ⱘֵᙃ for (Message m : messages) { Message[] messages = inbox.getMessages(); //ᕫࠄ INBOX 䞠ⱘ᠔ֵ᳝ᙃ inbox.open(Folder.READ_WRITE); //ҹ䇏ݭⱘᮍᓣᠧᓔ INBOX try { Folder inbox = getINBOXFolder(ctx); //ᕫࠄ INBOX ᇍᑨⱘ Folder public List getMessages(MailContext ctx) { //ᅲ⦄᥹ষᮍ⊩ } } throw new LoadMailException("ࡴ䕑䚂ㆅ䫭䇃ˈ䇋Ẕᶹ䜡㕂"); } catch (Exception e) { return store.getFolder("INBOX"); try { Store store = ctx.getStore(); private Folder getINBOXFolder(MailContext ctx) { //ᕫࠄ䚂ㆅ INBOX ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\mail\MailLoaderImpl.java getMessages ᮍ⊩Ё㦋ᕫ᠔᳝ⱘ䚂ӊDŽ ᥹ϟᴹˈ៥ӀЎ MailLoaderImpl ᮄᓎϔϾᎹ݋ᮍ⊩ˈ⫼Ѣ㦋ᕫ䚂ㆅЁⱘ INBOX ⱘⳂᔩᇍ䈵ˈ೼ } } return null; //᱖ᯊᦤկぎᅲ⦄ public List getMessages(MailContext ctx) { //ᅲ⦄᥹ষᮍ⊩,䖨ಲ䚂ㆅЁⱘ᠔᳝䚂ӊ public class MailLoaderImpl implements MailLoader { ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\mail\MailLoaderImpl.java ᇍ䖭ѯ䚂ӊ䖯㸠ᇕ㺙ˈᮄᓎ䆹᥹ষⱘᅲ⦄㉏ MailLoaderImplˈ݋ԧҷⷕབϟDŽ ᮄᓎ᥹ষ MailLoaderˈᑊ⏏ࡴϔϾ᥹ষᮍ⊩ˈ⫼Ѣ䖨ಲ Mail ᇍ䈵ⱘ䲚ড়ˈ䆹᥹ষ⫼ᴹ᥹ᬊ䚂ӊᑊ 12.5.1 ᥹ᬊ䚂ӊ ᇍ䈵˄Store˅ⱘᮍ⊩ˈ಴ℸৃҹⳈ᥹Փ⫼DŽټᇍ䈵ᅮНњ䖨ಲ䚂ㆅҧ Message ᇍ䈵䖯㸠䕀ᤶˈব៤៥Ӏ೼ 12.3.2 ЁᅮНⱘ Mail ᇍ䈵DŽ㗠೼ 12.3.1 ЁˈᏆ㒣Ў MailContext ᔧ៥ӀՓ⫼ JavaMail Ң䖰⿟᳡ࡵ఼ᕫࠄ䚂ӊৢˈ៥ӀᇚӮᕫࠄ Message ᇍ䈵ˈ៥Ӏ䳔㽕ᇍ䖭ѯ ᥹ᬊ䚂ӊ 12.5 /Download at http://www.pin5i.com 䈵㉏DŽMultipart Ϣ Message ϔḋˈгᰃ Part ⱘϔϾᅲ⦄㉏ˈৠḋгᰃᢑ䈵䈵㉏DŽ ЁDŽ៥ӀৃҹҢ JavaMail ⱘ API ЁᕫⶹˈMessage гᰃ Part ㉏ⱘϔϾᅲ⦄㉏ˈԚ Message ᰃϔϾᢑ ߭ݡ䇗⫼ getContent ᮍ⊩㦋ᕫⳈ᥹ݙᆍˈབᵰᰃ᱂䗮ⱘ text ㉏ൟˈ߭Ⳉ᥹ࡴࠄ㒧ᵰⱘ StringBuffer ᇍ䈵 ҹϞҷⷕⱘ咥ԧ䚼ߚˈ䗦ᔦ䇗⫼ getContent ᮍ⊩ˈ䗝ᢽ߸ᮁখ᭄ Part ⱘ㉏ൟˈབᵰᰃ Multipartˈ } return result; } result.append(part.getContent());//䘛ࠄ᭛ᴀḐᓣ៪㗙 html Ḑᓣ, Ⳉ᥹ᕫࠄݙᆍ } else if (part.isMimeType("text/*")) { } getContent(bp, result); //䗦ᔦ䇗⫼ getContent ᮍ⊩ BodyPart bp = p.getBodyPart(i); for(int i = 0; i < count; i++) { if (count > 1) count = 1; //Multipart ⱘ㄀ϔ䚼ߚᰃ text/plain, ㄀Ѡ䚼ߚᰃ text/html ⱘḐᓣ, া䳔㽕㾷ᵤ㄀ϔ䚼ߚेৃ int count = p.getCount(); Multipart p = (Multipart)part.getContent(); if (part.isMimeType("multipart/*")) { private StringBuffer getContent(Part part, StringBuffer result) throws Exception { //䖨ಲ䚂ӊℷ᭛ ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\mail\MailLoaderImpl.java ҹϟᮍ⊩䖨ಲ䚂ӊⱘℷ᭛DŽ 12.5.2 㦋প䚂ӊℷ᭛ ӊⱘℷ᭛ǃ᥹ᬊҎǃথ䗕Ҏǃ䰘ӊǃᡘ䗕ഄഔǃ᥹ᬊ᮹ᳳ੠䚂ӊ໻ᇣDŽ ᦤկϔѯᎹ݋ᮍ⊩ˈ⫼ᴹᕫࠄ䚂ˈܜ䖭ϔ㋏߫ Message ᇍ䈵ৢˈ៥Ӏ䳔㽕ᇍᅗӀ䖯㸠ᇕ㺙੠ᥦᑣDŽ佪 㒧ᵰˈབᵰϡ㛑ⱏᔩࠄ䚂ㆅ៪㗙ߎ⦄݊Ҫᓖᐌˈ䙷М䳔㽕Ẕᶹ䚂ㆅⱘ৘Ͼ䜡㕂ᰃ৺ℷ⹂DŽᕫࠄ䚂ㆅЁⱘ 䙷М⦄೼ৃҹਃࡼ⿟ᑣˈՓ⫼䜡㕂⬠䴶ᇍ䚂ㆅⱘֵᙃ䖯㸠䜡㕂ˈݡ⚍ߏᬊপ䚂ӊˈৃҹⳟࠄᠧॄⱘ } } showMessage(e.getMessage()); } catch (Exception e) { List newMails = this.mailLoader.getMessages(this.ctx); try { public void receive() { //ࠄ᳡ࡵ఼Ёᬊপ䚂ӊ }; } receive(); public void actionPerformed(ActionEvent e) { private Action in = new AbstractAction("ᬊপ䚂ӊ", new ImageIcon("images/in.gif")) { //ᬊপ䚂ӊ private MailLoader mailLoader = new MailLoaderImpl(); //䚂ㆅࡴ䕑ᇍ䈵 ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\ui\MainFrame.java ˄MainFrame˅ЁՓ⫼ MessageLoader ⱘ getMessages ᮍ⊩ˈ偠䆕䖭ѯ⿟ᑣᰃ৺ৃҹ䖲᥹Ϟ䚂ӊDŽ 䆩䖯㸠偠䆕ˈϟ䴶ˈ៥Ӏ೼Џ⬠䴶⌟ܗ៥Ӏ䳔㽕ⱘ㒧ᵰᰃ৺ℷ⹂ˈᔧ✊ˈ᳈དⱘдᛃህᰃ㓪ݭऩ ҹϞⱘҷⷕЁⱘ咥ԧ䚼ߚˈ៥Ӏৃҹᇚᕫࠄⱘ৘Ͼ Message ᇍ䈵ⱘ䚂ӊЏ乬ᠧॄߎᴹˈৃҹⳟࠄ /Download at http://www.pin5i.com ϟ䴶ᅲ⦄㦋ᕫ䚂ӊ໻ᇣⱘᮍ⊩DŽ 12.5.6 㦋প䚂ӊⱘ໻ᇣ ࡴܹ໮ϔϾথ䗕᮹ᳳDŽབᵰ Message ᇍ䈵ⱘথ䗕᮹ᳳ੠᥹ᬊ᮹ᳳ䛑Ўぎˈ䙷Мህ䖨ಲᔧࠡⱘ᮹ᳳDŽ 䖨ಲ䚂ӊⱘথ䗕᮹ᳳˈ៥Ӏⱘᅶ᠋ッ⿟ᑣЁা᳝ϔϾ᥹ᬊ᮹ᳳˈԴৃҹЎ Mail ᇍ䈵ܜ䆹ᮍ⊩ЁӬ } return new Date(); if (m.getReceivedDate() != null) return m.getReceivedDate(); if (m.getSentDate() != null) return m.getSentDate(); private Date getReceivedDate(Message m) throws Exception { 䖨ಲথ䗕᮹ᳳ, ݊⃵䖨ಲᬊֵ᮹ᳳܜ //ᕫࠄ᥹ᬊⱘ᮹ᳳ, Ӭ ϟ䴶ᅲ⦄䖨ಲ䚂ӊ᮹ᳳⱘᮍ⊩˖ 12.5.5 㦋প䚂ӊⱘথ䗕᮹ᳳ Փ⫼ MimeUtility ⱘ decodeText ᮍ⊩䖯㸠㾷ⷕDŽ ᔧ✊ˈབᵰ᳝ᖙ㽕ˈгৃҹᕫࠄ໮ϾথӊҎⱘഄഔˈৠḋഄᇕ㺙៤ᄫヺІ䲚ড়䖨ಲˈ⊼ᛣˈ䖭䞠䖬䳔㽕 ៥Ӏৃҹ䗮䖛 Message ⱘ getFrom ᮍ⊩ᕫࠄথӊҎⱘഄഔˈ៥Ӏ䖭䞠া㦋ᕫϔϾথӊҎⱘഄഔˈ } return MimeUtility.decodeText(addresses[0].toString());//Փ⫼ MimeUtility 䖯㸠㾷ⷕ Address[] addresses = m.getFrom(); private String getSender(Message m) throws Exception { //ᕫࠄথ䗕Ҏⱘഄഔ ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\mail\MailLoaderImpl.java ϟ䴶ҷⷕᕫࠄথӊҎⱘഄഔDŽ 12.5.4 㦋প䚂ӊⱘথӊҎ ݡᇚ䖭ѯഄഔᇕ㺙៤ϔϾᄫヺІ䲚ড়ᑊ䖨ಲेৃDŽ ᕫࠄϔϾ Message ᇍ䈵ˈݡ䗮䖛䖭Ͼᇍ䈵䇗⫼ getAllRecipients ᮍ⊩ᕫࠄ᠔᳝ᬊӊҎⱘഄഔˈ✊ৢ } return result; for (Address a : addresses) result.add(a.toString()); if (addresses == null) return result; List result = new ArrayList(); private List getAddresses(Address[] addresses) { //Ꮉ݋ᮍ⊩, ᇚখ᭄ⱘഄഔᄫヺІᇕ㺙៤䲚ড় } return getAddresses(addresses); Address[] addresses = m.getAllRecipients(); private List getAllRecipients(Message m) throws Exception { //ᕫࠄϔᇕ䚂ӊⱘ᠔᳝ᬊӊҎ ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\mail\MailLoaderImpl.java ϟ䴶ⱘҷⷕ੠ࠄ䚂ӊⱘᬊӊҎDŽ 㦋প䚂ӊᬊӊҎ 12.5.3 /Download at http://www.pin5i.com //ᰃ multipart/mixed ㉏ൟ, ህ䖯㸠໘⧚ List files = new ArrayList(); private List getFiles(MailContext ctx, Message m) throws Exception { //㦋ᕫ䚂ӊⱘ䰘ӊ ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\mail\MailLoaderImpl.java IO ⌕ᬒࠄ file ⳂᔩϟDŽҹϟᮍ⊩ᕫࠄ䚂ӊ䰘ӊDŽ ৢˈབᵰ䖭ѯᇍ䈵᳝䰘ӊⱘ䆱ˈህᄬᬒࠄ೒ 12.5 Ёⱘ file Ⳃᔩϟˈ៥Ӏ䖭䞠া䳔㽕ᇚ䰘ӊ䗮䖛 Java ⱘ བ೒ 12.5 ᠔⼎ˈ⫼᠋ৡⱘⳂᔩᰃᄬ೼Ѣ datas Ⳃᔩϟ䴶ⱘˈ៥ӀҢ䖰⿟䚂ӊ᳡ࡵ఼ᕫࠄ䚂ӊᇍ䈵 ೒ 12.5 ⫼᠋Ⳃᔩ㒧ᵘ ᴀഄⳂᔩⱘ⿟ᑣˈ⫳៤ⱘᴀഄⱘⳂᔩ㒧ᵘབ೒ 12.5 ᠔⼎DŽ ϟ䴶䳔㽕ᇚ䚂ӊⱘ䰘ӊᕫࠄˈᑊᇚ䖭ѯ䰘ӊᬒࠄᴀഄⱘⳂᔩЁˈ೼ 12.4.2 Ёˈ៥ӀᏆ㒣㓪ݭњ⫳៤ 12.5.7 㦋প䰘ӊᑊᄬᬒࠄᴀഄⱘⳂᔩЁ ഄഔ˄Address˅ᇍ䈵ᇕ㺙៤ᄫヺІ䲚ড়ⱘ㸠Ў䛑ᬒࠄৠϔϾᮍ⊩Ё˄getAddresses˅DŽ ҹϞҷⷕЁⱘ getAddresses ᮍ⊩ˈϢ 12.5.4 ЁᕫࠄথӊҎⱘⱘᮍ⊩䇗⫼ⱘᰃৠϔϾᮍ⊩DŽ៥Ӏᇚ } return getAddresses(addresses); Address[] addresses = m.getRecipients(Message.RecipientType.CC); //ᕫࠄ᥹ᬊҎഄഔ,᥹ᬊҎ㉏ൟᰃᡘ䗕 private List getCC(Message m) throws Exception { //ᕫࠄᡘ䗕ⱘഄഔ ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\mail\MailLoaderImpl.java ҹϟᮍ⊩ᕫࠄ Message ⱘᡘ䗕ഄഔˈᑊᇕ㺙៤ᄫヺІ䲚ড়DŽ 12.5.7 㦋প䚂ӊⱘᡘ䗕ഄഔ 䚂ӊⱘ໻ᇣህᰃ԰Ў݊ЁϔϾᵘ䗴খ᭄DŽ ߯ᓎ䖭Ͼ㉏ⱘᅲ՟ᠡৃҹˈԚᰃ៥ӀᏆ㒣Ў Mail ᇍ䈵ࡴܹњϔϾᵘ䗴఼ˈܜЎ䴭ᗕᮍ⊩ˈ䙷Мህ䳔㽕 ϔ䖛⿟Ϣ䚂ӊⳌ݇ˈ៥Ӏৃҹᇚ䖭Ͼ getSize ᮍ⊩ݭࠄ Mail ㉏Ёˈ԰Ў䆹㉏ⱘϔϾ䴭ᗕᮍ⊩ˈབᵰϡ԰ ᰃᄫ㡖ˈ៥Ӏ䳔㽕ᇚ䖭Ͼ᭄ؐ䕀ᤶЎ kˈ಴ℸϞ䴶ҷⷕЁⱘ getSize ᮍ⊩⫼Ѣ໘⧚䕀ᤶⱘ䖭ϔ䖛⿟ˈ䖭 ៥Ӏ䗮䖛 Message ⱘ getSize ᮍ⊩䖨ಲⱘᰃϔϾ int ㉏ൟⱘ᭄ؐˈ䆹᭄ؐ㸼⼎䆹䚂ӊⱘ໻ᇣˈऩԡ } return (new java.text.DecimalFormat("#.##")).format(result); double result = d / 1024; double d = Double.valueOf(size); public static String getSize(int size) { //ᇚ䚂ӊⱘ໻ᇣऩԡᅮЎ k, ᑊֱ⬭ϸԡᇣ᭄, খ᭄ऩԡЎ byte ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\object\Mail.java /Download at http://www.pin5i.com ࠄ䚂ӊ䰘ӊⱘᮍ⊩DŽ ᕫࠄ FileObject ৢˈህৃҹᇚϔᇕ䚂ӊⱘ᠔᳝䰘ӊᇕ㺙៤ϔϾ FileObject ᇍ䈵ⱘ䲚ড়ˈህᅲ⦄њᕫ ఼Ϟⱘ᭛ӊˈݭࠄᴀഄⱘ file ⳂᔩDŽ 䖛䚂ӊЁⱘ Part ᇍ䈵ˈৃҹᕫࠄϔϾ InputStream ᇍ䈵ˈ䙷МህৃҹՓ⫼ IO ⌕ᴹ䇏ݭ᭛ӊˈ䇏প᳡ࡵ ⱘ⑤᭛ӊৡ៥Ӏ䳔㽕ֱᄬˈ᠔ҹ೼᳔ৢ䖨ಲϔϾ FileObect ᇍ䈵ˈ䆹ᇍ䈵ৃҹֱᄬ᭛ӊⱘ⑤᭛ӊৡDŽ䗮 њ᭛ӊৡⱘ䞡໡ˈԚᰃ䰘ӊܡ᠔ҹ䳔㽕Փ⫼ UUID 䞡ᮄЎ᭛ӊ⫳៤ϔϾ᭛ӊৡˈݡֱᄬࠄ file Ⳃᔩˈ䙓 ˈމ⊩⫼Ѣᕫࠄ᭛ӊⱘৢ㓔DŽ಴Ў៥Ӏা᳝ϔϾ file Ⳃᔩ⫼Ѣֱᄬ䰘ӊˈ಴ℸৃ㛑Ӯথ⫳᭛ӊৡ䞡໡ⱘᚙ ҹ䇗⫼ getBoxPathˈখ᭄Ў inbox ेৃDŽҹϞҷⷕЁⳕ⬹њ getFileSufix ᮍ⊩˄咥ԧ䚼ߚ˅ⱘᅲ⦄ˈ䆹 ҹϞҷⷕЁⱘ getBoxPath ᮍ⊩ˈᰃᕫࠄ೒ 12.5 Ёⱘ݋ԧᶤϾⳂᔩˈ՟བ䳔㽕ᕫࠄ file Ϟᔧˈᕫৃ } return getAccountRoot(ctx) + folderName + File.separator; public static String getBoxPath(MailContext ctx, String folderName) { //ᕫࠄᶤϾⳂᔩৡᄫ, ՟བᕫࠄ file ⱘⳂᔩ, inbox ⱘⳂᔩ } return new FileObject(serverFileName, file); //ⳕ⬹ BufferedOutputStreamˈInputStream ੠ FileOutputStream ⱘ close ᮍ⊩ outs.write(b); is.read(b); byte[] b = new byte[size]; int size = (part.getSize() > 0) ? part.getSize() : 0; //བᵰ䰘ӊݙᆍЎぎ part.getSize Ў-1, བᵰⳈ᥹ new byte, ᇚᡯߎᓖᐌ BufferedOutputStream outs = new BufferedOutputStream(fos); InputStream is = part.getInputStream(); FileOutputStream fos = new FileOutputStream(file); //䇏ݭ᭛ӊ File file = new File(fileRepository + fileName + getFileSufix(serverFileName)); //getFileSufix ᰃᕫࠄ᭛ӊⱘৢ㓔ˈ䖭䞠ⳕ⬹݊ᅲ⦄ String fileName = UUID.randomUUID().toString(); //⫳៤ UUID ԰Ў೼ᴀഄ㋏㒳Ёଃϔⱘ᭛ӊᷛ䆚 String serverFileName = MimeUtility.decodeText(part.getFileName()); //ᕫࠄ䚂ӊ᳡ࡵ఼Ё䰘ӊⱘ᭛ӊৡˈᑊ䖯㸠㾷ⷕ String fileRepository = getBoxPath(ctx, FILE); //ᕫࠄ᭛ӊᄬᬒⱘⳂᔩ public static FileObject createFileFromPart(MailContext ctx, Part part) { //Ў䰘ӊ߯ᓎᴀഄ᭛ӊ, Ⳃᔩᰃⱏᔩ⫼᠋ⱘ䚂ㆅৡⱘ file ϟ ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\util\FileUtil.java ᅲ⦄DŽ 䈵Ӵ㒭 FileUtilˈ䅽 FileUtil এᐂ៥Ӏᕫࠄ䚂ӊⱘ䚂ӊᑊᄬᬒࠄ೒ 12.5 Ёⱘ file Ⳃᔩˈҹϟᰃ FileUtil ⱘ ⊼ᛣҹϞҷⷕⱘ咥ԧ䚼ߚˈ䇗⫼њ FileUtil ⱘ createFileFromPart ᮍ⊩ˈᇚ MailContext Ϣ Part ᇍ } return files; } } files.add(FileUtil.createFileFromPart(ctx, part)); //೼ᴀഄ߯ᓎ᭛ӊᑊ⏏ࡴࠄ㒧ᵰЁ Part part = mp.getBodyPart(i); for (int i = 1; i < count; i++) { int count = mp.getCount();//ᕫࠄ䚂ӊݙᆍⱘ Multipart ᇍ䈵ᑊᕫࠄݙᆍЁ Part ⱘ᭄䞣 Multipart mp = (Multipart)m.getContent(); if (m.isMimeType("multipart/mixed")) { /Download at http://www.pin5i.com return d2.compareTo(d1); Date d2 = m2.getReceiveDate(); Date d1 = m1.getReceiveDate();//㦋ᕫϸϾ᮹ᳳ䖯㸠↨䕗 Mail m2 = (Mail)o2; Mail m1 = (Mail)o1; //ᇚখ᭄ᔎ䕀Ў Mail ᇍ䈵 public int compare(Object o1, Object o2) { //ᅲ⦄ compare ᮍ⊩ public class MailComparator implements Comparator { ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\object\MailComparator.java ⊩ेৃDŽ receiveDate ԰Ўᥦᑣձ᥂ˈᇚ䲚ড়䖯㸠ᥦᑣˈᅲ⦄ Comparator ᥹ষˈݡ䇗⫼ Collections Ёⱘ sort ᮍ ೼ 12.5.8 Ёˈ៥ӀᏆ㒣ᕫࠄњ Mail ᇍ䈵ⱘ䲚ড়ˈԚᰃ៥Ӏ䖬䳔㽕ᐂᅗӀ䖯㸠ᥦᑣˈҹ Mail ᇍ䈵ⱘ 12.5.9 ᇍ䚂ӊ䖯㸠ᥦᑣ 䙷М䖭ᇕ䚂ӊⱘ Mail ᇍ䈵ᴹ⑤ህᰃ draft Ⳃᔩ˄㤝〓ㆅ˅DŽ ӊᰃ䗮䖛ᬊӊ㗠ᕫᴹⱘˈ಴ℸ䳔㽕ᬒࠄ inbox ⳂᔩDŽ՟བˈབᵰ೼ݭ䚂ӊⱘᯊ׭ˈᇚ䚂ӊֱᄬࠄ㤝〓ㆅˈ Ϟ䴶ⱘҷⷕϔḋˈ䖭ᇕ䚂ڣˈ᭛ӊৡDŽ঺໪ˈMail Ё᳝ϔϾ from ⱘሲᗻˈ䆹ሲᗻ㸼⼎ Mail ᇍ䈵ⱘᴹ⑤ ᭛ӊˈᑊֱᄬࠄ೒ 12.5 Ёⱘ inbox Ⳃᔩˈ᠔ҹЎњֱ䆕 XML ᭛ӊⱘଃϔˈህՓ⫼ UUID ԰Ў XML ⱘ xmlName ⱘሲᗻˈৠḋ䳔㽕Փ⫼ UUID ԰Ў᭛ӊৡˈ䖭ᰃ಴Ў៥ӀᇚӮᇚ䖭Ͼ Mail ᇍ䈵䕀ᤶ៤ XML ࠄ䚂ӊⱘ৘Ͼֵᙃˈᑊ䆒㕂ࠄ Mail ᇍ䈵ˈ᳔ৢࡴࠄ䲚ড়ᑊ䖨ಲDŽ䖭䞠䳔㽕⊼ᛣⱘᰃˈMail Ё᳝ϔϾ ҹϞҷⷕⱘ咥ԧ䚼ߚ೼߯ᓎ Mail ᇍ䈵ⱘᯊ׭ˈՓ⫼њ 12.5.2 ࠄ 12.5.7 Ё᠔㓪ݭⱘ৘ϾᎹ݋ᮍ⊩ᕫ } } throw new LoadMailException("ᕫࠄ䚂ӊⱘֵᙃߎ䫭: " + e.getMessage()); } catch (Exception e) { return result; } result.add(mail); mail.setFiles(getFiles(ctx, m)); //䆒㕂䰘ӊ䲚ড় mail.setCcs(getCC(m)); //Ў mail ᇍ䈵䆒㕂ᡘ䗕 content, FileUtil.INBOX); m.getSubject(), getReceivedDate(m), Mail.getSize(m.getSize()), hasRead(m), Mail mail = new Mail(xmlName, getAllRecipients(m), getSender(m), //ᕫࠄ䚂ӊⱘ৘Ͼؐ String content = getContent(m, new StringBuffer()).toString();//㦋ᕫݙᆍ String xmlName = UUID.randomUUID().toString() + ".xml"; //⫳៤ UUID ⱘ᭛ӊৡ for (Message m : messages) { //ᇚᕫࠄⱘ Message ᇍ䈵ᇕ㺙៤ Mail ᇍ䈵 try { List result = new ArrayList(); private List getMailList(MailContext ctx, Message[] messages) { //ᇚ javamail Ёⱘ Message ᇍ䈵䕀ᤶ៤ᴀ乍ⳂЁⱘ Mail ᇍ䈵 ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\mail\MailLoaderImpl.java ֵᙃᇕ㺙៤ϔϾ Mail ᇍ䈵ˈᑊ⏏ࡴϔϾ䲚ড়Ё䖨ಲDŽ Ң 12.5.2 ࠄ 12.5.7ˈ៥Ӏ㓪ݭњҢ Message Ёᕫࠄ䚂ӊⱘ৘Ͼֵᙃˈ䙷Мˈ៥Ӏ⦄೼䳔㽕ᇚ䖭ѯ ᇚMessageᇕ㺙ЎMail 12.5.8 /Download at http://www.pin5i.com ϔϾ᥹ষ SystemHandlerˈ䆹᥹ষ⫼Ѣ໘⧚ᴀഄⱘ䚂ӊˈࣙᣀֱᄬǃߴ䰸ǃ䚂ӊⱘ⿏ࡼㄝ᪡԰DŽ⏏ࡴϔ 䈵ֱᄬࠄᴀഄⱘⳂᔩЁˈᬊপ䚂ӊˈ䳔㽕ֱᄬࠄ inbox ⳂᔩЁˈ݋ԧ䇋ⳟ೒ 12.5 ЁⱘⳂᔩ㒧ᵘDŽᮄᓎ ೼ࠡ䴶ⱘゴ㡖Ёˈ៥ӀᏆ㒣ᕫࠄњ Mail ᇍ䈵ⱘ䲚ড়ˈ䙷М೼ᬊপ䚂ӊⱘᯊ׭ˈ䳔㽕ᇚ䖭ѯ Mail ᇍ 12.5.11 ᇚMailᇍ䈵䕀ᤶ៤XML᭛ӊᑊֱᄬࠄᴀഄⳂᔩЁ Folder ᇍ䈵ᯊˈখ᭄Ў true 㸼⼎᠔᳝ flag ؐЎ DELETED ⱘ䚂ӊߴ䰸ˈৃҹ⧚㾷៤ᦤѸ᳈ᬍDŽ ҷⷕЁⱘ咥ԧ䚼ߚˈ䇗⫼њߴ䰸䚂ӊⱘᮍ⊩䆒㕂 Message ᇍ䈵ⱘ setFlag ᮍ⊩ৢˈ݇䯁 INBOX ⱘ } return result; inbox.close(true); //ߴ䰸䚂ӊᑊᦤѸߴ䰸⢊ᗕ deleteFromServer(messages); //ߴ䰸䚂ㆅЁܼ䚼ⱘ䚂ӊ, 䙷М↣⃵Փ⫼䚂ӊ㋏㒳, াӮᣓᮄᬊࠄⱘ䚂ӊ sort(result); //ᣝ✻ᯊ䯈䰡ᑣᥦᑣ List result = getMailList(ctx, messages); //ᇚ Message ᭄㒘ᇕ㺙៤ Mail 䲚ড় Message[] messages = inbox.getMessages();//ᕫࠄ INBOX 䞠ⱘ᠔ֵ᳝ᙃ inbox.open(Folder.READ_WRITE); Folder inbox = getINBOXFolder(ctx); //ᕫࠄ INBOX ᇍᑨⱘ Folder public List getMessages(MailContext ctx) { ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\mail\MailLoaderImpl.java Ё៥ӀህᏆ㒣ᅲ⦄њϔ䚼ߚˈ⦄೼Ꮖ㒣ৃҹܼ䚼ᅲ⦄њDŽ ৃҹ䇗⫼ deleteFromServerˈ᥹ϟᴹህৃҹܼ䚼ᅲ⦄ MailLoader ⱘ᥹ষᮍ⊩ getMessagesˈ೼ 12.5.1 ᦤկϔϾ deleteFromServer ᮍ⊩ˈ䆒㕂 Message ⱘ flag Ў DELETEDˈᇕ㺙ᅠ Mail 䲚ড়ৢˈህ } for (Message m : messages) m.setFlag(Flags.Flag.DELETED, true); private void deleteFromServer(Message[] messages) throws Exception { //ᇚ䚂ӊ᭄㒘䆒㕂Ўߴ䰸⢊ᗕ ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\mail\MailLoaderImpl.java ϟ䴶ᅲ⦄ߴ䰸䚂ӊ᳡ࡵ఼ⱘ䚂ӊDŽ ϟˈгᰃՓ⫼䖭⾡ㄪ⬹DŽމৃˈFoxmail ೼ϡ䆒㕂ⱘᚙ ᗻ㛑ˈ៥ӀৃҹᇚᏆ㒣ᬊࠄⱘ䚂ӊҢ䚂ӊ᳡ࡵ఼Ёߴ䰸ˈᔧ᳝ᮄⱘ䚂ӊ䖯ᴹᯊˈা᥹ᬊ䆹ᇕᮄⱘ䚂ӊे 䙷ϔӑˈ㗠ҹࠡⱘ䙷ѯ䚂ӊˈ䛑Ꮖ㒣ֱᄬࠄњ៥ᴀഄˈ಴ℸ≵᳝ᖙ㽕ݡᬊࠄܼ䚼ⱘ䚂ӊDŽЎњ᳝᳈དⱘ ఼Ёᣓ䚂ӊˈབᵰ䚂ӊ᳡ࡵಲЁᄬ೼ᕜ໮ⱘ䚂ӊˈ䙷М䗳ᑺᇚӮ䰡Ԣˈ㗠៥Ӏᛇᕫࠄⱘাᰃ᳔ᮄᬊࠄⱘ ЎҔМ䳔㽕ᇚ䖰⿟ⱘ䚂ӊߴ䰸ਸ਼˛ᔧ✊ˈԴгৃҹ䗝ᢽϡ⫼ߴ䰸ˈ⬅Ѣ៥Ӏⱘ⿟ᑣ↣⃵䛑Ӯএ䚂ӊ᳡ࡵ ᅲ⦄ߴ䰸䚂ӊ᳡ࡵ఼ⱘ䚂ӊDŽܜ᳡ࡵ఼Ёⱘ䚂ӊ䛑ߴ䰸ˈֱᄬࠄᴀഄ᳡ࡵ఼ࡳ㛑䖬≵᳝ᅲ⦄ˈ⦄೼ৃҹ ៥ӀҢ䚂ӊ᳡ࡵ఼Ϟ䴶ᕫࠄњ᠔᳝ⱘ䚂ӊৢˈ៥Ӏᇚ䖭ѯ䚂ӊֱᄬࠄᴀഄⱘ᳡ࡵ఼ˈህৃҹᇚ䚂ӊ 12.5.10 ߴ䰸䚂ӊ᳡ࡵ఼Ϟ䴶ⱘ䚂ӊ } Collections.sort(mails, new MailComparator()); private void sort(List mails) { //䖯㸠ᯊ䯈ᥦᑣ ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\mail\MailLoaderImpl.java ✊ৢ೼ MailLoaderImpl ЁᮄᓎϔϾ sort ᮍ⊩ˈᇚᏆ㒣ᕫࠄⱘ Mail ᇍ䈵ⱘ䲚ড়䖯㸠ᥦᑣDŽ { { /Download at http://www.pin5i.com ֱᄬњ䚂ӊࠄᴀഄⱘⳂᔩৢˈ᥹ϟᴹህৃҹᇚ䖭ѯᇍ䈵ᰒ⼎ࠄ⬠䴶ⱘ߫㸼ЁDŽ saveToInBox(newMails); //ֱᄬࠄᴀഄⱘᬊӊㆅЁ List newMails = this.mailLoader.getMessages(this.ctx); //ᕫࠄ䚂ӊᇍ䈵䲚ড় ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\ui\MainFrame.java 䙷М MainFrame ᥹ᬊ䚂ӊⱘᮍ⊩˄receive˅ৃҹݭ៤DŽ } } systemHandler.saveInBox(mail, this.ctx); //⫳៤ xml ᴹᄬᬒ䖭ѯᮄⱘ䚂ӊ for (Mail mail : newMails) { private void saveToInBox(List newMails) { ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\ui\MainFrame.java ⫼Ѣֱᄬ䚂ӊᇍ䈵ࠄᴀഄⱘ inbox ⳂᔩDŽ XML ᭛ӊⱘ᭛ӊৡՓ⫼ Mail ᇍ䈵ⱘ xmlName ሲᗻDŽЎЏ⬠䴶ᇍ䈵˄MainFrame˅ᮄᓎϔϾᎹ݋ᮍ⊩ˈ ⳂᔩˈݡᇏᡒᇍᑨⱘᶤϾ Box Ⳃᔩ˄inboxǃdraft ㄝ˅ˈݡᇚᇍ䈵ݭࠄ䆹ⳂᔩЁⱘᮄ XML ᭛ӊЁˈ䆹 ᕫࠄ⫼᠋ⱘܜˈ೼ FileUtil ЁՓ⫼њ XStreamˈᇚ Mail ᇍ䈵䕀ᤶ៤ᇍᑨⱘ XML ᭛ӊˈҹϞҷⷕЁ } fos.close(); writer.close(); xstream.toXML(mail, writer); //Փ⫼ XStream ⱘ toXML ᮍ⊩ᇚ Mail ᇍ䈵䕀ᤶᑊ䕧ߎࠄ xml ᭛ӊ OutputStreamWriter writer = new OutputStreamWriter(fos, "UTF8"); FileOutputStream fos = new FileOutputStream(xmlFile); if (!xmlFile.exists()) xmlFile.createNewFile(); public static void writeToXML(File xmlFile, Mail mail) { //ᇚϔϾ mail ᇍ䈵ݭࠄ xmlFile Ё } writeToXML(xmlFile, mail); File xmlFile = new File(boxPath + xmlName); String boxPath = getAccountRoot(ctx) + boxFolder + File.separator; //ᕫࠄᇍᑨⱘⳂᔩ䏃ᕘ String xmlName = mail.getXmlName();//ᕫࠄ mail ᇍᑨⱘ xml ᭛ӊⱘ᭛ӊৡ public static void writeToXML(MailContext ctx, Mail mail, String boxFolder) { //ᇚϔϾ䚂ӊᇍ䈵Փ⫼ XStream ݭࠄ xml ᭛ӊЁ private static XStream xstream = new XStream(); //߯ᓎ XStream ᇍ䈵 ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\util\FileUtil.java ⏙᱄ˈ↣Ͼ᥹ষ䛑᳝㞾Ꮕ೎᳝ⱘ㘠䋷DŽҹϟᰃ FileUtil ⱘᅲ⦄DŽ 㸠ˈ䙷М MainFrame ЎҔМϡⳈ᥹Փ⫼ FileUtil এ߯ᓎ XML ᭛ӊਸ਼˛䖭ᰃ⬅Ѣ䖭ḋৃҹՓᕫ⿟ᑣ᳈ࡴ ষᇚ䖭ѯ Mail ᇍ䈵ҹ XML ⱘᔶᓣᄬᬒࠄᴀഄⱘⳂᔩЁˈ䖭ϔℹⱘ᪡԰ৠḋгᰃᬒ೼ MainFrame Ё䖯 MainFrame Փ⫼ˈ೼ࠡ䴶ⱘゴ㡖ˈ䗮䖛 MailLoader ᥹ষᕫࠄ Mail ⱘ䲚ড়ˈݡՓ⫼ SystemHandler ᥹ ೼ SystemHandlerImpl ЁⳈ᥹䇗⫼њ FileUtil ⱘ writeToXML ᮍ⊩ˈSystemHandler ᥹ষᰃ㒭 } } FileUtil.writeToXML(ctx, mail, FileUtil.INBOX); //䇗⫼ FileUtil ⱘᮍ⊩ public void saveInBox(Mail mail, MailContext ctx) { public class SystemHandlerImpl implements SystemHandler { ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\system\impl\SystemHandlerImpl.java Ͼ saveInBox ᮍ⊩ˈЎ䖭Ͼ᥹ষ⏏ࡴᅲ⦄㉏ˈᑊᇍ᥹ষᮍ⊩䖯㸠ᅲ⦄ेৃˈᅲ⦄㉏ⱘҷⷕབϟDŽ /Download at http://www.pin5i.com Vector views = new Vector(); private Vector createViewDatas(List mails) { //ᇚ䚂ӊ᭄᥂䲚ড়䕀ᤶ៤㾚೒ⱘḐᓣ private ImageIcon envelopClose = new ImageIcon(CLOSE_ENVELOP_PATH); //ֵᇕ݇䯁ⱘ Icon ᇍ䈵 private ImageIcon envelopOpen = new ImageIcon(OPEN_ENVELOP_PATH); //ֵᇕᠧᓔⱘ Icon ᇍ䈵 private static String OPEN_ENVELOP_PATH = "images/envelop-open.gif"; //Ꮖ㒣ᶹⳟⱘ䚂ӊᰒ⼎ᠧᓔⱘֵᇕⱘ೒⠛ഄഔ private static String CLOSE_ENVELOP_PATH = "images/envelop-close.gif"; //≵᳝ᶹⳟⱘ䚂ӊᰒ⼎݇䯁ⱘֵᇕⱘ೒⠛ഄഔ } //ⳕ⬹݊Ҫҷⷕ setTableFace();//䆒㕂䚂ӊ߫㸼ⱘḋᓣ tableMode.setDataVector(createViewDatas(this.currentMails), getListColumn()); //ⳕ⬹ getListColumn ⱘᅲ⦄ this.mailListTable = new MailListTable(tableMode); DefaultTableModel tableMode = new DefaultTableModel();//䚂ӊ߫㸼 JTable this.currentMails = this.inMails; //䆒㕂ᔧࠡᰒ⼎ⱘ䚂ӊ䲚ড়Ўᬊӊㆅⱘ䲚ড় //ⳕ⬹݊Ҫҷⷕ public MainFrame(MailContext ctx) { ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\ui\MainFrame.java ϟᴹৃҹ೼ MainFrame Ёৃҹ䖭ḋ߯ᓎ߫㸼ᇍ䈵DŽ 㓪䕥ˈᑊϨ䆒㕂া㛑ᇍ߫㸼䖯㸠ऩ䗝ˈ೼䖭䞠ϡ䌈ߎ݋ԧⱘҷⷕˈ䇋ⳟᴀк᠔䰘ⱘ MailListTable ㉏DŽ᥹ ؐᰃ৺೒⠛ˈݡ䖯㸠䆒㕂೒⠛៪㗙᭛ᴀDŽMailListTable ⱘЏ㽕԰⫼ᰃ䆒㕂⬠䴶ⱘ߫㸼≵᳝㸼Ḑ㒓੠ϡৃ ḐⱘܗMailTableCellRenderer ㉏Ё䞡ݭњ getTableCellRendererComponent ᮍ⊩ˈ߸ᮁབᵰ䆹ऩ } } return this; else this.setText(value.toString()); if (value instanceof Icon) this.setIcon((Icon)value); Ḑⱘؐᰃ৺೒⠛ܗ //߸ᮁ䆹ऩ boolean arg2, boolean arg3, int arg4, int arg5) { public Component getTableCellRendererComponent(JTable arg0, Object value, public class MailTableCellRenderer extends DefaultTableCellRenderer { ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\ui\MailTableCellRenderer.java ⱘ݋ԧҷⷕDŽ Ḑᰒ⼎ˈMailListTable 㒻ᡓѢ JTableˈ㸼⼎ϔϾ߫㸼ᇍ䈵ˈҹϟЎ MailTableCellRendererܗ㸼ⱘ↣Ͼऩ ᥹ϟᴹᮄᓎϔϾ MailTableCellRenderer ੠ MailListTable ㉏ˈMailTableCellRenderer ㉏⫼ᴹ໘⧚߫ private List currentMails; //ᔧࠡ⬠䴶߫㸼᠔ᰒ⼎ⱘᇍ䈵 private List deleteMails; //ൗഒㆅⱘ䚂ӊ䲚ড় private List draftMails; //㤝〓ㆅⱘ䚂ӊ䲚ড় private List sentMails; //៤ࡳথ䗕ⱘ䚂ӊ䲚ড় private List outMails; //থӊㆅⱘ䚂ӊ䲚ড় private List inMails; //ᬊӊㆅⱘ Mail ᇍ䈵䲚ড়ˈҷ㸼᠔᳝೼ᬊӊㆅЁⱘ䚂ӊ ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\ui\MainFrame.java 䗕ǃ㤝〓ㆅ੠ൗഒㆅⱘ䚂ӊ䲚ড়ˈݡՓ⫼ϔϾ䲚ড়ᴹ㸼⼎ᔧࠡ JTable ᠔ᰒ⼎ⱘ䲚ড়DŽ 㒭Џ⬠䴶ⱘᇍ䈵˄MainFrame˅⏏ࡴ޴Ͼ䲚ড়ᇍ䈵ˈߚ߿⫼ᴹҷ㸼ᬊӊㆅǃথӊㆅǃᏆথܜᇍ䈵ˈ៥Ӏ ᕫࠄ Mail ᇍ䈵ⱘ䲚ড়ৢˈ៥Ӏৃҹᇚ䖭ѯᇍ䈵ᬒࠄ⬠䴶Ёᰒ⼎DŽᰒ⼎ Mail ᇍ䈵䲚ড়ⱘᰃϔϾ JTable ೼⬠䴶Ёᰒ⼎䚂ӊ 12.5.12 /Download at http://www.pin5i.com ᬊӊ೒⠛ˈৃҹⳟࠄ߫㸼ⱘব࣪བ೒ 15.6 ᠔⼎DŽ ᥹ᬊ䚂ӊⱘᮍ⊩Ꮖ㒣ܼ䚼ᅲ⦄њˈ᥹ϟᴹˈৃҹՓ⫼㞾ᏅⱘϔϾ䚂ㆅ䋺ো䖯㸠⌟䆩DŽ⚍ߏЏ⬠䴶ⱘ } } showMessage(e.getMessage()); } catch (Exception e) { refreshTable();//ࠋᮄ߫㸼 saveToInBox(newMails); //ֱᄬࠄᴀഄⱘᬊӊㆅЁ this.inMails.addAll(0, newMails); //ᕫࠄ Mail ᇍ䈵, ⏏ࡴࠄ inMails 䲚ড়Ё List newMails = this.mailLoader.getMessages(this.ctx); try { public void receive() { //ࠄ᳡ࡵ఼Ёᬊপ䚂ӊ } setTableFace();//䆒㕂߫ⱘḋᓣ tableModel.setDataVector(createViewDatas(this.currentMails), getListColumn()); //Փ⫼ᔧࠡⱘ䚂ӊ䲚ড়ˈ䞡ᮄ䆒㕂߫㸼᭄᥂ DefaultTableModel tableModel = (DefaultTableModel)this.mailListTable.getModel(); public void refreshTable() { //ࠋᮄ߫㸼ⱘᮍ⊩, খ᭄ᰃϡৠⱘ᭄᥂ ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\ui\MainFrame.java ⦄DŽ ࠄ Mail ᇍ䈵ⱘ䲚ড়ৢˈህৃҹࠋᮄϔϟᔧࠡⱘ߫㸼ˈϟ䴶Ўࠋᮄ߫㸼ⱘᮍ⊩੠᥹ᬊ䚂ӊᮍ⊩ⱘܼ䚼ᅲ ҹϞҷⷕЁ߯ᓎ⬠䴶ⱘ߫㸼ᇍ䈵ˈᑊϨᇚ߫㸼ⱘ᭄᥂䆒㕂Ўᔧࠡⱘ Mail ᇍ䈵䲚ড়ˈ䙷М៥Ӏ೼ᕫ } //ⳕ⬹݊Ҫ߫ⱘ䆒㕂ҷⷕ this.mailListTable.getColumn("ᠧᓔ").setCellRenderer(new MailTableCellRenderer()); Ḑ໘⧚㉏ܗ //⬅Ѣᠧᓔ߫䳔㽕ֵ᳝ᇕᰒ⼎ˈ಴ℸՓ⫼ MailTableCellRenderer ԰Ўऩ private void setTableFace() { //䆒㕂䚂ӊ߫㸼ⱘḋᓣ } return views; } //ⳕ⬹⏏ࡴ݊Ҫᄫ↉ⱘҷⷕ else view.add(envelopClose); if (mail.getHasRead()) view.add(envelopOpen); //߸ᮁ䚂ӊᇍ䈵ᰃ৺Ꮖ㒣㹿䯙䇏ˈབᵰ㹿䯙䇏ˈ߭Փ⫼ᠧᓔⱘֵᇕ԰Ўᇣ೒ᷛ Vector view = new Vector(); for (Mail mail : mails) { /Download at http://www.pin5i.com public ReceiveTask(MainFrame mainFrame) { private MainFrame mainFrame; class ReceiveTask extends TimerTask { } //ⳕ⬹݊Ҫҷⷕ } timer.schedule(new ReceiveTask(this), 10000, this.receiveInterval); Timer timer = new Timer(); //ⳕ⬹݊Ҫҷⷕ public MainFrame(MailContext ctx) { public class MainFrame extends JFrame { ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\ui\MainFrame.java 䚂ӊ᳡ࡵ఼ϟ䕑䚂ӊˈᮄᓎϔϾӏࡵ䇗ᑺ఼ˈᑊ೼Џ⬠䴶߱ྟ࣪ⱘᯊ׭䖤㸠DŽ ᕜ໮ᯊ׭ˈ⫼᠋ᑊϡ䳔㽕᠟ࡼএ᥹ᬊ䚂ӊˈ៥ӀৃҹᦤկϔϾӏࡵ䇗ᑺ఼ˈ䱨ϔ↉ᯊ䯈䅽ᅶ᠋ッএ 12.5.13 㓪ݭӏࡵ䇗ᑺ఼᥹ᬊ䚂ӊ ЁDŽ ᇍ䈵ⱘᠧᓔ᪡԰ˈᠧᓔϔᇕ䚂ӊᯊˈֵᇕⱘ೒ᷛ䳔㽕ব៤ᠧᓔˈᑊϨ䚂ӊⱘݙᆍ䳔㽕ᰒ⼎ࠄ݋ԧⱘऎඳ ߫㸼ЁDŽ೼೒ 12.6 Ё៥ӀৃҹⳟࠄˈĀᠧᓔā߫ⱘֵᇕ೒ᷛᰃ݇䯁ⱘˈϟ䴶ゴ㡖ᇚӮᅲ⦄ᇚ䖭Ͼ Mail ᔧ⚍ߏϔ⃵ᬊপ䚂ӊᯊˈᅶ᠋ッᇚӮҢ䖰⿟䚂ӊ᳡ࡵ఼᥹ᬊ䚂ӊˈᑊ䕀ᤶ៤߫㸼ⱘ᭄᥂Ḑᓣሩ⦄ࠄ ೒ 12.6 ᥹ᬊ䚂ӊⱘᬜᵰ /Download at http://www.pin5i.com Ѣ䇏পᴀഄ㋏㒳Ёⱘ Mail ᇍ䈵ˈЎ䆹᥹ষ⏏ࡴ᥹ষᮍ⊩ᑊᦤկᅲ⦄㉏ SystemLoaderImplDŽ ݡ⃵Փ⫼ XStream 䖯㸠䕀ᤶˈᇚ XML ᭛ӊ䕀ᤶ៤ Mail ᇍ䈵DŽᮄᓎϔϾ SystemLoader ⱘ᥹ষˈϧ䮼⫼ ᔧ䇗⫼ getXMLFiles ᮍ⊩ৢˈህᕫࠄ݋ԧᶤϾ Box Ⳃᔩϟ䴶ⱘ᠔᳝ XMLˈᕫࠄ䖭ѯ XML ᭛ӊৢˈ } return result; } if (f.getName().endsWith(sufix)) result.add(f); for (File f : files) { if (files == null) return new ArrayList(); File[] files = folder.listFiles(); List result = new ArrayList(); private static List filterFiles(File folder, String sufix) { //ҢϔϾ᭛ӊⳂᔩЁ, ҹখ᭄᭛ӊৢ㓔 subfix Ўᴵӊ, 䖛Ⓒ᭛ӊ } return files; List files = filterFiles(boxFolder, ".xml");//ᇍ᭛ӊ䖯㸠ৢ㓔䖛Ⓒ File boxFolder = new File(boxPath);//ᕫࠄᶤϾ box ⱘⳂᔩ String boxPath = rootPath + box; String rootPath = getAccountRoot(ctx); public static List getXMLFiles(MailContext ctx, String box) { //ҢⳌᑨⱘ box Ёᕫࠄܼ䚼ⱘ xml ᭛ӊ ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\util\FileUtil.java Ў FileUtil ⏏ࡴᶹᡒ᭛ӊⱘᮍ⊩ˈᇚ޴Ͼ Box Ⳃᔩϟⱘ᠔᳝ XML ᭛ӊ䛑䇏পߎᴹˈ䖨ಲ᭛ӊ䲚ড়DŽ 䰘ӊᄬᬒⱘⳂᔩDŽ োⳂᔩˈ↣ϔϾ䚂ㆅⳂᔩϟ䴶᳝໮ϾⳂᔩˈߚ߿ҷⷕᬊӊㆅǃথӊㆅǃᏆথ䗕ǃ㤝〓ㆅ੠ൗഒㆅˈ䖬᳝ ⱘゴ㡖ЁᦤࠄˈϔϾ⫼᠋ⱘ݋ԧⳂᔩབ೒ 12.5 ᠔⼎ˈ⫼᠋ৡЎḍⳂᔩˈϟ䴶ᰃ䖭Ͼ⫼᠋ⱘ৘Ͼ䚂ㆅ䋺 М៥Ӏৃҹḍ᥂ MailContext ᇍ䈵এࡴ䕑⫼᠋䚂ㆅ˄ᴀഄⳂᔩ˅Ёⱘ᠔᳝䚂ӊˈݡᰒ⼎ࠄ⬠䴶DŽ೼ࠡ䴶 ᔧ⫼᠋ⱏᔩᯊˈ៥Ӏৃҹᕫࠄ⫼᠋᠔᳝ⱘ䜡㕂ֵᙃˈᑊϨৃҹ೼ MailContext ᇍ䈵Ёԧ⦄ߎᴹˈ䙷 12.6.1 䕀ᤶXML᭛ӊЎMailᇍ䈵 䙷М៥Ӏ䳔㽕ᇚ䖭ѯ XML ᭛ӊ䕀ᤶ៤㋏㒳ⱘ Mail ᇍ䈵ˈݡᰒ⼎೼ᇍᑨⱘ߫㸼ЁDŽ ⳂᔩЁˈབᵰ⫼᠋ᑊϡᰃ㄀ϔ⃵ⱏᔩˈ㋏㒳ⱘⳂᔩЁᏆ㒣ᄬ೼䆹⫼᠋ⱘⳌ݇䜡㕂ঞ䚂ӊ˄XML ᭛ӊ˅ˈ ೼ 12.5 Ёˈ៥Ӏ㓪ݭњҢ䚂ӊ᳡ࡵ఼᥹ᬊ䚂ӊˈ䕀ᤶ៤ Mail ᇍ䈵ˈᑊ೼ᴀഄⱘⳂᔩЁֱᄬᇍᑨⱘ 12.6 ߱ྟ࣪⬠䴶᭄᥂ ᬊপ䚂ӊDŽ ೼ҹϞⱘҷⷕЁˈ៥Ӏ䆒㕂↣䱨 10000 ↿⾦˄10 ⾦˅ᬊপ䚂ӊϔ⃵ˈ䙷М⫼᠋ህϡ䳔㽕᠟ࡼⱘএ } } this.mainFrame.receive(); //䇗⫼Џ⬠䴶ⱘ receive ᮍ⊩ this.mainFrame.getMailContext().getStore(); public void run() { } this.mainFrame = mainFrame; /Download at http://www.pin5i.com } return getMails(ctx, FileUtil.DELETED); public List getDeletedBoxMails(MailContext ctx) { //ᅲ⦄᥹ষᮍ⊩, ᕫࠄൗഒㆅⱘ䚂ӊ ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\mail\SystemLoaderImpl.java Ͼ Box ⱘ䚂ӊ䇏পˈЎ SystemLoader ᥹ষᮄ๲ᮍ⊩ᑊᦤկᅲ⦄DŽ ӊ˄XML ᭛ӊ˅໪ˈ䖬䳔㽕᳝݊Ҫⱘ䚂ӊˈࣙᣀথӊㆅǃᏆথ䗕ǃ㤝〓ㆅ੠ൗഒㆅDŽϟ䴶㒻㓁ᅲ⦄䖭޴ ᔧ៥Ӏᕫࠄ inbox Ⳃᔩⱘ䚂ӊᇍ䈵ᯊˈህৃҹᇚ䖭ѯᇍ䈵ᰒ⼎ࠄ⬠䴶ˈ䰸њᬊӊㆅ inbox Ⳃᔩⱘ䚂 XStream ⱘࡳ㛑䖰ϡℶѢℸˈ⬅ѢᴀゴⱘЏ㽕ݙᆍᰃ䚂ӊˈ಴ℸ XStream ᳈⏅ܹⱘՓ⫼ϡݡ䆺㒚ᦣ䗄DŽ XStream Ў៥Ӏᦤկњकߚᮍ֓ⱘ䕀ᤶᮍ⊩ˈⳈ᥹䇗⫼ϔϾᮍ⊩ህৃҹᇚ᭛ӊ䕀ᤶ៤᭄᥂ˈᔧ✊ˈ } } throw new FileException("䕀ᤶ᭄᥂ᓖᐌ: " + xmlFile.getAbsolutePath()); } catch (Exception e) { return mail; fis.close(); Mail mail = (Mail)xstream.fromXML(fis); //䇗⫼ XStream ⱘ䕀ᤶᮍ⊩ᇚ᭛ӊ䕀ᤶ៤ᇍ䈵 FileInputStream fis = new FileInputStream(xmlFile); try { public static Mail fromXML(MailContext ctx, File xmlFile) { //ᇚϔӑ xml ᭛ḷ䕀ᤶ៤ Mail ᇍ䈵 ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\util\FileUtil.java MailComparator Ꮖ㒣೼ 12.5.9 Ёᅲ⦄DŽҹϟᰃ FileUtil ⱘ fromXML ⱘᮍ⊩ᅲ⦄DŽ Ёⱘ fromXML ᭛ӊ䖯㸠䕀ᤶˈᕫࠄ᳔㒜ⱘ Mail 䲚ড়ৢˈ䇗⫼ sort ᮍ⊩䖯㸠ᯊ䯈ᥦᑣˈsort ᮍ⊩Ёⱘ ҹϞҷⷕЁˈᕫࠄ File ᇍ䈵ⱘ䲚ড়ৢˈݡ䇗⫼ convert ᮍ⊩䖯㸠䕀ᤶˈ݊Ё convert জ䇗⫼њ FileUtil } Collections.sort(mails, new MailComparator()); private void sort(List mails) { //ᣝ✻ᯊ䯈䰡ᑣᥦᑣ } return result; sort(result); } result.add(mail); Mail mail = FileUtil.fromXML(ctx, file);//ᇚ xml 䕀ᤶ៤ Mail ᇍ䈵 for (File file : xmlFiles) { List result = new ArrayList(); private List convert(List xmlFiles, MailContext ctx) { //ᇚ xml ᭛ӊ䕀ᤶ៤ Mail ᇍ䈵, ᑊᥦᑣ } return result; List result = convert(xmlFiles, ctx); //ᇚ XML ᭛ӊ䕀ᤶ៤ Mail ᇍ䈵 List xmlFiles = FileUtil.getXMLFiles(ctx, FileUtil.INBOX); ҢϢ⫼᠋ᇍᑨⱘ inbox Ёᕫࠄܼ䚼ⱘ xml ᭛ӊܜ// } ( public List getInBoxMails(MailContext ctx //ᅲ⦄᥹ষᮍ⊩ ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\mail\SystemLoaderImpl.java /Download at http://www.pin5i.com Џ⬠䴶ⱘᇍ䈵ˈᑊЎ݊ᮄᓎњᇐ㟾ᷥⱘ哴ᷛⲥ਀㉏ SailTreeListenerˈᔧᯊ䆹㉏ⱘ mousePressed ᮍ⊩ ӊ䲚ড়䕀ᤶ៤ᇍᑨⱘ䚂ӊ䲚ড়ˈݡࠋᮄ߫㸼ˈህৃҹ䖒ࠄ᭄᥂ⱘߛᤶⱘᬜᵰDŽ೼ 12.2.2 Ёˈ៥Ӏ߯ᓎњ ⱘ䚂ӊ˄䆒㕂ᔧࠡᰒ⼎ⱘ䚂ӊ䲚ড়˅DŽ䙷Мˈ⫼᠋⚍њᇐ㟾ᷥЁⱘᶤϾ㡖⚍ᯊˈህৃҹᇚᔧࠡᰒ⼎ⱘ䚂 ೼ MainFrame ᵘ䗴఼Ё߱ྟ࣪৘Ͼ䚂ӊ䲚ড়ˈ⫼᠋㄀ϔ⃵䖯ᴹᅶ᠋ッⱘᯊ׭ˈህৃҹᰒ⼎ᬊӊㆅ } this.sentMails = this.systemLoader.getSentBoxMails(this.ctx); this.outMails = this.systemLoader.getOutBoxMails(this.ctx); this.deleteMails = this.systemLoader.getDeletedBoxMails(this.ctx); this.draftMails = this.systemLoader.getDraftBoxMails(this.ctx); this.inMails = this.systemLoader.getInBoxMails(this.ctx); private void initMails() { //߱ྟ࣪ᯊ߯ᓎ৘Ͼ box Ёⱘ᭄᥂ } //ⳕ⬹݊Ҫҷⷕ this.currentMails = this.inMails; //䆒㕂ᔧࠡᰒ⼎ⱘ䚂ӊ䲚ড়Ўᬊӊㆅⱘ䲚ড় initMails(); //߱ྟ࣪৘Ͼ߫㸼䲚ড় this.ctx = ctx; public MainFrame(MailContext ctx) { //MainFrame ᵘ䗴఼ ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\ui\MainFrame.java ⱘ䲚ড়DŽ Ёᕫࠄ৘Ͼ Box ⱘ Mail ᇍ䈵ৢˈ೼Џ⬠䴶˄MailFrame˅Ёህৃҹᇚ䖭ѯ Mail ᇍ䈵Ӵ䗦㒭⬠䴶Ёᇍᑨ Ͼ JTable ᇍ䈵ˈᔧ⫼᠋⚍ߏњᇐ㟾ᷥⱘᶤϾ㡖⚍ৢˈ៥Ӏ䳔㽕ᇚϡৠⱘ᭄᥂䆒㕂ࠄ JTable ЁDŽ೼ 12.6.1 ೼৘Ͼ Box 䯈ߛᤶˈ䇈ⱑњህᰃ⚍ߏᇐ㟾ᷥˈݡ䖯㸠⬠䴶᭄᥂ⱘߛᤶˈ៥ӀⱘЏ⬠䴶߫㸼Ёা᳝ϔ 12.6.2 ೼৘ϾBox䯈ߛᤶ ⳂᔩЁ䇏প XML ᭛ӊˈݡՓ⫼ XStream 䖯㸠䕀ᤶDŽ ҹϞⱘ৘Ͼ Box ⱘ䚂ӊ䇏পҷⷕϢࠡ䴶ᬊӊㆅⱘ䇏পҷⷕ᮴ᓖˈ㗠Ϩᰒᕫ᳈ࡴㅔ⋕ˈ䛑ᰃࠄᇍᑨⱘ } return result; List result = convert(xmlFiles, ctx); List xmlFiles = FileUtil.getXMLFiles(ctx, box); private List getMails(MailContext ctx, String box) { //Ꮉ݋ᮍ⊩ } return getMails(ctx, FileUtil.SENT); public List getSentBoxMails(MailContext ctx) { //ᅲ⦄᥹ষᮍ⊩, ᕫࠄᏆথ䗕ⱘ䚂ӊ } return getMails(ctx, FileUtil.OUTBOX); public List getOutBoxMails(MailContext ctx) { //ᅲ⦄᥹ষᮍ⊩, ᕫࠄথӊㆅЁⱘ䚂ӊ } return getMails(ctx, FileUtil.DRAFT); public List getDraftBoxMails(MailContext ctx) { ᅲ⦄᥹ষᮍ⊩, ᕫࠄ㤝〓ㆅⱘ䚂ӊ// /Download at http://www.pin5i.com ⑤ˈ䙷М⚍ߏ䖬ॳᯊˈህৃҹ䖬ॳࠄⳌᑨⱘⳂᔩЁDŽ ㆅЁⱘ䚂ӊˈ೼ Mail ᇍ䈵Ёˈ៥ӀᏆ㒣᳝ϔϾ from ሲᗻ˄݋ԧ䇋ⳟ 12.3.2˅ˈ㸼ᯢ䖭Ͼ䚂ӊᇍ䈵ⱘᴹ XML ᭛ӊˈ䖬᳝䆹䚂ӊ᠔ᇍᑨⱘ᠔᳝䰘ӊˈϔᮺᕏᑩߴ䰸ˈᇚϡৃ㹿䖬ॳDŽ䖬ॳ䚂ӊা䗖ড়Ѣ᪡԰ൗഒ ӊᇍᑨⱘ XML ᭛ӊᬒࠄ deleted ⳂᔩDŽᕏᑩߴ䰸䚂ӊˈህ䳔㽕ᇚ䚂ӊҢᴀഄⱘⳂᔩЁߴ䰸ˈ䳔㽕ߴ䰸 ੠䰘ӊㄝֵᙃDŽ⫼᠋⚍ߏњߴ䰸䚂ӊᯊˈህ䳔㽕ᇚ䆹䚂ӊᬒࠄൗഒㆅ˄deleted Ⳃᔩ˅Ёˈা䳔㽕ᇚ䚂 ӊᶹⳟᯊˈ䆹䚂ӊⱘֵᙃᇚӮᰒ⼎೼݋ԧֵᙃⱘऎඳˈৃҹᰒ⼎䚂ӊⱘЏ乬ǃℷ᭛ǃᬊӊᯊ䯈ǃথ䗕Ҏ 䖭䞠᠔䆆ⱘ᪡԰䚂ӊࣙᣀᶹⳟ䚂ӊǃߴ䰸䚂ӊǃᕏᑩߴ䰸䚂ӊ੠䖬ॳ䚂ӊDŽᔧ⫼᠋䗝ᢽњᶤϔӑ䚂 12.7 ᪡԰䚂ӊ 䖤㸠⿟ᑣᑊᇍᇐ㟾ᷥ䖯㸠⚍ߏˈेৃⳟࠄᬜᵰDŽ } this.mainFrame.select(); public void mousePressed(MouseEvent e) { ఼˖ ᇍ䈵ˈݡḍ᥂䖭ѯ MailBox ⱘᅲ⦄㉏䖯㸠߸ᮁˈᰒ⼎Ⳍᑨⱘ᭄᥂DŽ᥹ϟᴹএᅲ⦄ SailTreeListener ⲥ਀ ⱘᅲ⦄㉏এ߯ᓎ㡖⚍ⱘ˄݋ԧ䇋ⳟ 12.2.2˅ˈ಴ℸᔧ⚍ߏњᷥϞⱘᶤϾ㡖⚍ᯊˈህৃҹᕫࠄⳌᑨⱘ MailBox ҹϞⱘ select ᮍ⊩ˈ೼ᇐ㟾ᷥЁᕫࠄᶤϾ MailBox ᇍ䈵ˈ⬅Ѣ೼߯ᓎᷥⱘᯊ׭ˈ៥ӀᰃՓ⫼ MailBox } return (MailBox)node.getUserObject(); DefaultMutableTreeNode node = (DefaultMutableTreeNode)treePath.getLastPathComponent(); //㦋ᕫ䗝Ёⱘ TreeNode if (treePath == null) return null; TreePath treePath = this.tree.getSelectionPath(); private MailBox getSelectBox() { //㦋ᕫᔧࠡ䗝Ёⱘ box } refreshTable(); //ࠋᮄ߫㸼 } this.currentMails = this.deleteMails; } else { this.currentMails = this.draftMails; } else if (box instanceof DraftBox) { this.currentMails = this.sentMails; } else if (box instanceof SentBox) { this.currentMails = this.outMails; } else if (box instanceof OutBox) { this.currentMails = this.inMails; if (box instanceof InBox) { //߸ᮁ MailBox ⱘ㉏, ᑊᰒ⼎ϡৠⱘ᭄᥂ MailBox box = getSelectBox(); public void select() { //ᔧ哴ᷛ⚍ߏњᇐ㟾ᷥⱘᶤϾ㡖⚍ᯊ, 㾺থ䖭Ͼᮍ⊩ ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\ui\MainFrame.java ⱘ⚍ߏDŽ Ў MainFrame ࡴܹϔϾ select ᮍ⊩ˈ⫼Ѣ໘⧚ᇐ㟾ᷥܜˈ⦃៥Ӏᦤկњぎᅲ⦄ˈ⦄೼ৃҹᇍ݊䖯㸠ᅲ /Download at http://www.pin5i.com openEnvelop();//᳈ᮄֵᇕ೒ᷛ mail.setHasRead(true);//䆒㕂䚂ӊᏆ㒣㹿ᶹⳟ if (!mail.getHasRead()) { //བᵰ䚂ӊ≵᳝㹿ᶹⳟ䖛ˈህׂᬍ೒ᷛˈᑊֱᄬᏆ㒣ᠧᓔⱘ⢊ᗕ this.currentMail = mail;//䆒㕂ᔧࠡ㹿ᠧᓔⱘ䚂ӊᇍ䈵 this.fileList.setListData(mail.getFiles().toArray());//⏏ࡴ䰘ӊ this.mailTextArea.append(mail.getContent()); this.mailTextArea.append("\n\n"); this.mailTextArea.append("䚂ӊℷ᭛˖ "); this.mailTextArea.append("\n\n"); this.mailTextArea.append("᥹ᬊ᮹ᳳ˖ " + dateFormat.format(mail.getReceiveDate())); this.mailTextArea.append("\n"); this.mailTextArea.append("Џ乬˖ " + mail.getSubject()); this.mailTextArea.append("\n"); this.mailTextArea.append("ᬊӊҎ: " + mail.getReceiverString()); this.mailTextArea.append("\n"); this.mailTextArea.append("ᡘ䗕˖ " + mail.getCCString()); this.mailTextArea.append("\n"); this.mailTextArea.append("থ䗕Ҏ˖ " + mail.getSender()); Mail mail = getSelectMail(); this.mailTextArea.setText(""); private void viewMail() { //ᶹⳟϔᇕ䚂ӊ } return null; } if (m.getXmlName().equals(xmlName))return m; for (Mail m : mails) { private Mail getMail(String xmlName, List mails) { //Ң䲚ড়Ёᡒࠄ xmlName Ϣখ᭄ϔ㟈ⱘ Mail ᇍ䈵 } return getMail(xmlName, this.currentMails); String xmlName = getSelectXmlName(); private Mail getSelectMail() { //㦋প೼߫㸼Ё᠔䗝ᢽⱘ Mail ᇍ䈵 } this.systemHandler.saveMail(this.currentMail, this.ctx); //䞡ᮄֱᄬ䚂ӊ⢊ᗕࠄ xml ᭛ӊ this.mailListTable.setValueAt(this.envelopOpen, row, column); int column = this.mailListTable.getColumn("ᠧᓔ").getModelIndex(); int row = this.mailListTable.getSelectedRow(); private void openEnvelop() { //ᔧ䇏প䚂ӊৢᇚ೒⠛ᬍবЎᠧᓔֵᇕ೒⠛ ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\ui\MainFrame.java ࠄ䖭Ͼ Mail ᇍ䈵DŽҹϟҷⷕᅲ⦄ᶹⳟ䚂ӊDŽ 䛑ᇍᑨϔϾ Mail ᇍ䈵ˈᑊϨ䖭Ͼ Mail ᇍ䈵ⱘ xmlName ሲᗻᰃଃϔⱘˈ៥Ӏህৃҹḍ᥂䖭Ͼሲᗻএᡒ ⚍ߏњ߫㸼ⱘᶤ㸠䚂ӊ䆄ᔩˈህৃҹᇚ䆹䚂ӊⱘֵᙃᰒ⼎ࠄⳌᑨⱘऎඳˈ೼߫㸼Ёⱘ↣ϔ㸠᭄᥂ˈ ᶹⳟ䚂ӊ 12.7.1 /Download at http://www.pin5i.com ᇚ∌䖰ᰃᏆ䇏ˈ䖤㸠⿟ᑣৃҹⳟࠄᬜᵰབ೒ 12.7 ᠔⼎DŽ Ͼ䲚ড়Ёˈݡᇍ䖭Ͼ᭛ӊ䲚ড়䖯㸠䘡ग़ˈᡒߎヺড়ᴵӊⱘ XML ᭛ӊDŽϔᮺ䚂ӊ㹿ᠧᓔˈ䙷Мᅗⱘ⢊ᗕ ҹϞҷⷕⱘ咥ԧ䚼ߚˈᕫࠄܼ䚼ⱘ XML ᭛ӊˈ䆹ᮍ⊩াᰃᇚ᠔᳝ Box ⳂᔩЁⱘ XML ᭛ӊࡴࠄϔ } return null; } if (f.getName().equals(xmlName)) return f; for (File f : allXMLFiles) { List allXMLFiles = getAllFiles(ctx); //Ң᠔᳝ⱘ Box ⳂᔩЁᕫࠄܼ䚼ⱘ XML ᭛ӊ, ݡ䖨ಲᇍᑨⱘ XML ᭛ӊ private File getMailXmlFile(String xmlName, MailContext ctx) { //Ң᠔᳝ⱘ䚂ӊЁᶹᡒৡᄫЎ xmlName ⱘ xml ᭛ӊ } FileUtil.writeToXML(xmlFile, mail); //ᡒࠄ䆹ӑ XML ᭛ӊৢ, ݡᇚ Mail ᇍ䈵䞡ᮄ䕀ᤶ៤ XML ᑊݭܹ File xmlFile = getMailXmlFile(mail.getXmlName(), ctx); //䳔㽕ᇏᡒ䆹 Mail ᇍ䈵᠔ᇍᑨⱘ xml ᭛ӊˈḍ᥂ id এᡒ᭛ӊ public void saveMail(Mail mail, MailContext ctx) { ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\system\impl\SystemHandlerImpl.java ៤ϔ⃵ XML ᭛ӊेৃˈҹϟᰃ SystemHandler Ёⱘ saveMail ᮍ⊩ⱘᅲ⦄DŽ trueˈ䙷Мህ㽕Ⳍᑨⱘএׂᬍ XML ᭛ӊˈᑊϡ䳔㽕ᡒࠄᇍᑨⱘ㡖⚍এׂᬍˈা䳔㽕ᇚ Mail ᇍ䈵䞡ᮄ⫳ 咥ԧ䚼ߚՓ⫼њ SystemHandler ⱘ saveMail ᮍ⊩ˈ⬅Ѣᰒ⼎ⱘ Mail ᇍ䈵ⱘ hasRead ؐᏆҢ false বЎ ᕫࠄ䆹ᇍ䈵ৢˈݡ৥ᰒ⼎ऎඳ⏏ࡴ᭛ᴀ੠⏏ࡴ䰘ӊˈ᳔ৢ䆒㕂䆹䚂ӊⱘ⢊ᗕЎᏆ㒣㹿ᠧᓔDŽҹϞҷⷕⱘ ᶹⳟ䚂ӊˈ䳔㽕ᕫࠄ䗝Ё㸠ᇍᑨ᭄᥂ⱘ xmlName ߫ⱘؐˈݡҢᔧࠡᰒ⼎ⱘ߫㸼Ёᶹᡒࠄ Mail ᇍ䈵ˈ { { /Download at http://www.pin5i.com Џ⬠䴶˄MainFrame˅ЁᰃϔϾ JList ᇍ䈵ˈ៥ӀᮄᓎϔϾ哴ᷛџӊⲥ਀఼ेৃDŽ ໛њ copy ᮍ⊩ৢˈህৃҹএᅲ⦄䰘ӊⱘ᪡԰ˈ䳔㽕Ўᄬᬒ䰘ӊⱘ߫㸼⏏ࡴⲥ਀఼ˈ䰘ӊ߫㸼೼ޚ { ;() process.waitFor //ㄝᕙੑҸᠻ㸠ᅠ៤ targetFile.getAbsolutePath() + "\""); sourceFile.getAbsolutePath() + "\" \"" + Process process = Runtime.getRuntime().exec("cmd /c copy \"" + //䇗⫼ copy ੑҸ public static void copy(File sourceFile, File targetFile) { //໡ࠊ᭛ӊⱘᮍ⊩ ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\util\FileUtil.java ᅲ⦄᭛ӊⱘ copyˈ೼ FileUtil ЁᮄᓎϔϾ copy ⱘᮍ⊩DŽܜˈϟ䴶 Փ⫼ cp ੑҸDŽ Ⳉ᥹ᠧᓔˈབᵰᰃ঺ᄬЎ᪡԰ˈгৃҹՓ⫼ cmd ⱘ copy ੑҸ䖯㸠໡ࠊˈབᵰᰃ Linux ᪡԰㋏㒳ˈৃҹ ϸϾ᪡԰ˈϔϾᰃⳈ᥹ᠧᓔ䰘ӊˈ঺໪ϔϾᰃᇚ䰘ӊ঺ᄬЎDŽབᵰⳈ᥹ᠧᓔ䰘ӊˈৃҹՓ⫼ cmd ੑҸ Мˈ៥Ӏ䳔㽕ᇍ䰘ӊ䖯㸠᪡԰ᯊˈህৃҹঠߏᶤӑ䰘ӊˈݡ䗝ᢽ݋ԧⱘ᪡԰DŽ៥Ӏ೼䖭䞠㒭⫼᠋ᅮНњ ೼ 12.7.1 ЁˈᏆ㒣ᅲ⦄њᶹⳟ䚂ӊⱘࡳ㛑ˈᶹⳟ䚂ӊᯊˈ䚂ӊⱘ䰘ӊӮᰒ⼎ࠄᰒ⼎ऎⱘ߫㸼Ёˈ䙷 12.7.2 ᶹⳟ䰘ӊ ೒ 12.7 ᶹⳟ䚂ӊ ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\ui\MainListMouseListener.java //Џ⬠䴶Ё䚂ӊ䰘ӊ߫㸼ⱘⲥ਀఼ public class MainListMouseListener extends MouseAdapter { public void mouseClicked(MouseEvent e) { if (e.getClickCount() == 2) { JList list = (JList)e.getSource(); FileObject file = (FileObject)list.getSelectedValue(); if (file == null) return; handle(file); } } //໘⧚ᮍ⊩, ৃҹ㒭ᄤ㉏এ䇗⫼ public void handle(FileObject file) { int result = JOptionPane.showOptionDialog(null, "䇋䗝ᢽ᪡԰", "䗝ᢽ", 0, JOptionPane.QUESTION_MESSAGE, null, new Object[]{"ᠧᓔ", "঺ᄬЎ", "প⍜"}, null); if (result == 0) { openFile(file);//ᠧᓔ᪡԰ } else if (result == 1) { saveAs(file);//঺ᄬЎ᪡԰ } } //ᠧᓔ᪡԰ public void openFile(FileObject file) { Runtime.getRuntime().exec("cmd /c \"" + file.getFile().getAbsolutePath() + "\""); } //঺ᄬЎ᪡԰, ᠧᓔ᭛ӊ䗝ᢽ఼ public void saveAs(FileObject file) { FolderChooser chooser = new FolderChooser(file); chooser.showSaveDialog(null); } } //᭛ӊ䗝ᢽ఼㉏ class FolderChooser extends JFileChooser { //䳔㽕঺ᄬЎⱘ᭛ӊ private FileObject sourceFile; public FolderChooser(FileObject sourceFile) { this.sourceFile = sourceFile; //া㛑䗝Ⳃᔩ this.setFileSelectionMode(DIRECTORIES_ONLY); } //೼᭛ӊ䗝ᢽ఼Ё䗝ᢽњ᭛ӊ៪㗙Ⳃᔩৢ public void approveSelection() { File targetFile = getSelectedFile(); if (targetFile.isDirectory()) { //བᵰ⫼᠋䗝ⱘᰃⳂᔩ, े≵᳝䕧ܹᮄⱘ᭛ӊৡ, ߭⫼ sourceName ԰Ў᭛ӊৡ File newFile = new File(targetFile.getAbsolutePath() + File.separator + this.sourceFile.getSourceName()); FileUtil.copy(this.sourceFile.getFile(), newFile); } else { Download at http://www.pin5i.com/ /Download at http://www.pin5i.com //ᇚ䚂ӊᇍᑨⱘ xml ᭛ӊᬒࠄ deleted ⱘⳂᔩЁ this.deleteMails.add(0, mail);//ࡴࠄൗഒㆅⱘ䲚ড়Ё this.currentMails.remove(mail);//Ңᔧࠡⱘ䲚ড়Ёߴ䰸 if (!this.deleteMails.contains(mail)) { //߸ᮁൗഒㆅЁᰃ৺᳝䆹ӑ䚂ӊ(Ꮖ㒣㹿ᬒࠄൗഒㆅЁ), ᳝ⱘ䆱ϡ䖯㸠໘⧚ Mail mail = getSelectMail(); //getSelectMail Ꮖ㒣ᅲ⦄ public void delete() { //ߴ䰸䚂ӊ ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\ui\MainFrame.java ᮍ⊩Ꮖ㒣೼ 12.7.1 Ёᅲ⦄DŽҹϟᰃЏ⬠䴶˄MainFrame˅ⱘߴ䰸䚂ӊҷⷕDŽ ᇍ䈵Ў෎⸔ˈ䞡ᮄ⫳៤ϔӑᮄⱘ XML ᭛ӊࠄ deleted ⳂᔩϟDŽҹϞҷⷕЁⱘ咥ԧ䚼ߚⱘ getMailXmlFile ߴ䰸ᮍ⊩↨䕗ㅔऩˈা㽕᳝ Mail ᇍ䈵ˈህৃҹᕫࠄᇍᑨⱘ XML ᭛ӊˈݡᇍ݊䖯㸠ߴ䰸ˈݡҹ Mail } FileUtil.writeToXML(ctx, mail, FileUtil.DELETED); //߯ᓎᮄⱘ xml ᭛ӊ file.delete(); //ߴ䰸᭛ӊ, ᑊ೼ deleted ⳂᔩЁ߯ᓎᮄⱘ᭛ӊ File file = getMailXmlFile(mail.getXmlName(), ctx); //ᡒࠄᇍᑨⱘ xml ᭛ӊ public void delete(Mail mail, MailContext ctx) { //ᅲ⦄᥹ষⱘ delete ᮍ⊩ ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\system\impl\SystemHandlerImpl.java ᥹ষ⏏ࡴϔϾ delete ᮍ⊩ˈ⫼Ѣߴ䰸䚂ӊDŽ݋ԧⱘҷⷕབϟDŽ ߴ䰸䚂ӊˈাᰃㅔऩⱘᇚ䆹䚂ӊᇍᑨⱘXML᭛ӊᬒࠄdeletedⳂᔩेৃDŽ៥Ӏ䳔㽕ЎSystemHandler 12.7.3 ߴ䰸䚂ӊ ೒ 12.8 ᶹⳟ䰘ӊ ᶹⳟ䰘ӊⱘᬜᵰབ೒ 12.8 ᠔⼎DŽ ำߑēำߑ੠ಾ FileObject ԅ sourceName ೫໿dךē༓ྑᄷ໭ಓюྡྷ܉ᅟ࿉ġၽখӉนԅನ ݡ䇗⫼ FileUtil ⱘ copy ᮍ⊩ˈᄬ೼Ѣ file ⳂᔩЁⱘ䰘ӊ໡ࠊࠄ⫼᠋䗝ᢽⱘⳂᔩЁDŽ ᔩϟˈ಴ℸৃҹⳈ᥹ᠧᓔDŽབᵰ䗝ᢽњ঺ᄬЎˈ䙷Мህᰒ⼎᭛ӊ䗝ᢽ఼ˈ䅽⫼᠋䗝ᢽ䆹᭛ӊⱘᄬᬒⳂᔩˈ ᓔˈᠻ㸠ҹϞҷⷕⱘ openFile ᮍ⊩ˈ⬅Ѣ䚂ӊᏆ㒣ϟ䕑ࠄњᴀഄˈ䖭ѯ䰘ӊᰃᄬ೼Ѣ䋺োⳂᔩⱘ file Ⳃ ᔧ⫼᠋೼䰘ӊ߫㸼ঠߏњᶤӑ䰘ӊᯊˈህᔍߎᦤ⼎ˈ䆶䯂݋ԧⱘ᪡԰ˈᰃᠧᓔ䖬ᰃ঺ᄬЎˈབᵰᠧ } } super.approveSelection(); } FileUtil.copy(this.sourceFile.getFile(), targetFile); 䕧ܹњᮄⱘ᭛ӊৡ, Ⳉ᥹໡ࠊ᠋⫼// /Download at http://www.pin5i.com //ߴ䰸䆹᭛ӊ, ݡ䖬ॳࠄॳᴹⱘⳂᔩЁ File xmlFile = getMailXmlFile(mail.getXmlName(), ctx); //ᡒࠄᇍᑨⱘ xml ᭛ӊ public void revert(Mail mail, MailContext ctx) { //ᅲ⦄᥹ষᮍ⊩ ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\system\impl\SystemHandlerImpl.java ৡ⿄ˈা䳔㽕ᕫࠄ Mail ᇍ䈵ˈህৃҹᇍ݊䖯㸠䖬ॳˈЎ SystemHandler ᥹ষ⏏ࡴᮍ⊩ᑊᇍ݊䖯㸠ᅲ⦄DŽ ⱘˈ䙷М䖭ϾؐህᰃᬊӊㆅⱘⳂᔩৡ⿄ˈབᵰᰃݭ䚂ӊᯊࡴࠄ㤝〓ㆅⱘˈ䙷М䖭Ͼؐህᰃ㤝〓ㆅⱘⳂᔩ Ꮖ㒣Ў䖭Ͼᇍ䈵ࡴܹњϔϾ from ⱘሲᗻˈ㸼⼎䖭Ͼ Mail ᇍ䈵ⱘߎ໘ˈབᵰᰃҢ䖰⿟ⱘ䚂ӊ᳡ࡵ఼᥹ᬊ ᔧ⫼᠋ߴ䰸њϔӑ䚂ӊৢˈ䳔㽕ᇍ݊䖯㸠䖬ॳˈৃҹՓ⫼䖭Ͼࡳ㛑ˈ៥Ӏ೼䆒䅵 Mail ᇍ䈵ⱘᯊ׭ˈ 12.7.5 䖬ॳ䚂ӊ } refreshTable();//ࠋᮄ߫㸼 this.currentMail = null; this.systemHandler.realDelete(mail, this.ctx); //ߴ䰸 xml ᭛ӊ੠ᇍᑨⱘ䰘ӊ this.currentMails.remove(mail); //Ңᔧࠡᰒ⼎ⱘ䲚ড়Ёߴ䰸 Mail mail = getSelectMail(); private void realDelete() { //ᕏᑩߴ䰸ϔᇕ䚂ӊ ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\ui\MainFrame.java ᔩЁⱘ᭛ӊৢˈ䖬䳔㽕᳈ᮄ߫㸼ˈҹϟᰃЏ⬠䴶˄MainFrame˅ⱘᕏᑩߴ䰸ҷⷕDŽ া㽕⚍ߏњᕏᑩߴ䰸䚂ӊˈህӮҢᴀഄⱘ㋏㒳Ёᕏᑩߴ䰸䆹䚂ӊⱘ XML ᭛ӊϢ䰘ӊˈᔧߴ䰸ᅠⳂ } if (xmlFile.exists()) xmlFile.delete(); //ߴ䰸 xml ᭛ӊ for (FileObject f : files) f.getFile().delete(); //ߴ䰸䰘ӊ List files = mail.getFiles(); //ᕫࠄ᠔᳝ⱘ䰘ӊᑊߴ䰸 File xmlFile = getMailXmlFile(mail.getXmlName(), ctx); //ᡒࠄᇍᑨⱘ xml ᭛ӊ public void realDelete(Mail mail, MailContext ctx) { //ᅲ⦄᥹ষᮍ⊩ ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\system\impl\SystemHandlerImpl.java ᠔ᇍᑨⱘ䰘ӊेৃDŽ೼ SystemHanlder Ёᮄ๲ϔϾ᥹ষᮍ⊩DŽ ᕏᑩߴ䰸䚂ӊϢߴ䰸䚂ӊϔḋˈ㗠Ϩ᳈ࡴㅔऩˈাᰃᇚ䚂ӊᇍᑨⱘ XML ᭛ӊߴ䰸ˈݡߴ䰸䆹䚂ӊ 12.7.4 ᕏᑩߴ䰸䚂ӊ } refreshTable();//ࠋᮄ߫㸼 this.currentMail = null; } this.systemHandler.delete(mail, this.ctx); /Download at http://www.pin5i.com ࡳ㛑໻㟈㉏Ԑˈབᵰᰃಲ໡ࡳ㛑ⱘ䆱ˈা䳔㽕ᇚॳ䚂ӊⱘথӊҎ԰ЎᬊӊҎᴹথᕙेৃDŽ ࡳ㛑ৢˈ៥Ӏህৃҹݡಲ䖛༈এᅲ⦄Џ⬠䴶ⱘಲ໡ࡳ㛑ǃ䕀থࡳ㛑੠থ䗕ࡳ㛑ˈ䖭ѯࡳ㛑Ϣ㓪ݭ䚂ӊⱘ ࠡ䴶ⱘゴ㡖Ꮖ㒣ᅲ⦄њϝϾ⬠䴶ⱘࡳ㛑ˈ࠽ϟⱘা᳝㓪ݭ䚂ӊⱘ⬠䴶≵᳝ᅠ៤ˈᅲ⦄њথ䗕䚂ӊⱘ 12.8 থ䗕䚂ӊ ᄯdۦނӾઇّ ၐܮನē༓ྑճ؞ဏߑԅ from ೫໿ࠩ໻૱ծēϬᄉԁ༓ྑۦނၐӾ MainFrame ᄯອ࿫ԅܮᅟ࿉ġ ⫼ SystemHandler ⱘᮍ⊩䖬ॳ XML ᭛ӊˈ᳔ৢݡ䖬ॳࠄ MainFrame ЁⳌᑨⱘ䲚ড়DŽ Ңൗഒㆅᇍᑨⱘ䚂ӊ䲚ড়Ёߴ䰸ˈݡ䇗ܜ೼ൗഒㆅЁˈབᵰϡᰃˈ䙷Мህϡ䖯㸠ӏԩ᪡԰DŽ䖬ॳ䚂ӊᯊ MainFrame Ёᅲ⦄䖬ॳ䚂ӊˈᕫࠄ䳔㽕ߴ䰸ⱘ䚂ӊᇍ䈵ৢˈݡᇍ݊䖯㸠߸ᮁˈⳟϟ䆹ᇍ䈵ᰃ৺Ꮖ㒣 } refreshTable();//ࠋᮄ߫㸼 this.currentMail = null; } revertMailToList(mail); //䖬ॳࠄ৘Ͼ䲚ড়Ё this.systemHandler.revert(mail, this.ctx); //᪡԰᭛ӊ, ᑊ deleted ⳂᔩЁⱘ xml Ё this.deleteMails.remove(mail); //Ңൗഒㆅ䲚ড়Ёߴ䰸 if (this.deleteMails.contains(mail)) { //ൗഒㆅࣙ৿䖭Ͼ Mail ᇍ䈵ᠡ䖯㸠䖬ॳ Mail mail = getSelectMail(); public void revert() { //䖬ॳ䚂ӊ } } this.outMails.add(mail); } else if (mail.getFrom().equals(FileUtil.OUTBOX)) { this.draftMails.add(mail); } else if (mail.getFrom().equals(FileUtil.DRAFT)) { this.sentMails.add(mail); } else if (mail.getFrom().equals(FileUtil.SENT)) { this.inMails.add(mail); if (mail.getFrom().equals(FileUtil.INBOX)) { private void revertMailToList(Mail mail) { //䖬ॳ Mail ᇍ䈵ࠄ৘ϾⳌᑨⱘ䲚ড় ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\ui\MainFrame.java XML ᭛ӊࠄ䚂ӊॳᴹᄬ೼ⱘⳂᔩेৃˈҹϟᰃ MainFrame ᇍ䖬ॳ䚂ӊⱘᅲ⦄DŽ 䆹ᅲ⦄↨䕗ㅔऩˈϢߴ䰸䚂ӊϔḋˈᇚᄬ೼Ѣ deleted ⳂᔩЁⱘ XML ᭛ӊߴ䰸ˈݡ䞡ᮄ⫳៤ϔӑ } FileUtil.writeToXML(ctx, mail, mail.getFrom()); xmlFile.delete(); /Download at http://www.pin5i.com //থ䗕᮹ᳳ mailMessage.setSubject(mail.getSubject()); //䆒㕂Џ乬 mailMessage.setRecipients(Message.RecipientType.CC, cc); Address[] cc = getAddress(mail.getCcs()); //䆒㕂ᡘ䗕Ҏഄഔ mailMessage.setRecipients(Message.RecipientType.TO, to); Address[] to = getAddress(mail.getReceivers()); //䆒㕂᠔᳝ᬊӊҎⱘഄഔ mailMessage.setFrom(from); Address from = new InternetAddress(ctx.getUser() + " <" + ctx.getAccount() + ">"); //䆒㕂থӊҎഄഔ Message mailMessage = new MimeMessage(session); Session session = ctx.getSession(); try { public Mail send(Mail mail, MailContext ctx) { //ᅲ⦄᥹ষᮍ⊩ ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\mail\MailSenderImpl.java ᮄᓎϔϾথ䗕䚂ӊⱘ᥹ষ MailSenderˈ⏏ࡴϔϾ᥹ষᮍ⊩ send ᑊЎ݊ᦤկᅲ⦄DŽ ҹ๲ࡴᗻ㛑DŽৃخ㕂њ䚂ㆅⱘֵᙃˈ䙷Мህ䆒㕂 Session ᇍ䈵Ўぎᑊݡ䞡ᮄ߯ᓎˈ䖭ḋ Session ᇍ䈵ⱘ㦋ᕫˈϢ᥹ᬊ䚂ӊⱘ Store ᇍ䈵㦋ᕫϔḋˈᔧЎぎⱘᯊ׭ݡ߯ᓎˈབᵰ⫼᠋䞡ᮄ䜡 } return this.session; } this.session = sendMailSession; Session sendMailSession = Session.getDefaultInstance(props, getAuthenticator()); //߯ᓎ Session ᇍ䈵 props.put("mail.smtp.auth", true); props.put("mail.smtp.port", this.getSmtpPort()); props.put("mail.smtp.host", this.getSmtpHost()); //䆒㕂 SMTP ᳡ࡵ఼੠ッষ Properties props = System.getProperties(); if (this.session == null) { } this.reset = false; this.store = null; this.session = null; if (this.reset) { //䞡㕂њֵᙃ, 䆒㕂 session Ў null public Session getSession() { //䖨ಲ Session ᇍ䈵 private Session session; ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\ui\MailContext.java ϔϾথ䗕䚂ӊⱘ Session ሲᗻˈᑊᦤկ䖨ಲ䆹ሲᗻⱘᮍ⊩˖ ӊ៥ӀՓ⫼ⱘᰃ SMTP ⱘण䆂ˈϢ᥹ᬊ䚂ӊᯊ߯ᓎⱘ Store ᇍ䈵ϔḋˈ៥Ӏ೼ MailContext ᇍ䈵Ёᮄᓎ Ꮖথ䗕ⱘ Box Ⳃᔩ˄sent˅ˈབᵰথ䗕༅䋹ˈ䳔㽕ֱᄬࠄথӊㆅЁˈ䅽⫼᠋೼ϟ⃵ݡ㒻㓁থ䗕DŽথ䗕䚂 থ䗕䚂ӊˈ៥Ӏ䳔㽕Փ⫼ JavaMail ⱘᮍ⊩䖯㸠থ䗕ˈথ䗕៤ࡳˈ䖬䳔㽕ᇚথ䗕ⱘ Mail ᇍ䈵ֱᄬࠄ ᅲ⦄থ䗕䚂ӊⱘࡳ㛑 12.8.1 /Download at http://www.pin5i.com this.mailSender.send(mail, this.mainFrame.getMailContext()); Mail mail = getMail(FileUtil.SENT); //ᕫࠄ Mail ᇍ䈵, 䆹ᇍ䈵㸼⼎ॳᴹⱘԡ㕂೼Ꮖথ䗕 if (!validateInput()) return; private void send() { //থ䗕ᮍ⊩ ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\ui\MailFrame.java ᴹ䖯㸠থ䗕ेৃˈҹϟᰃ䚂ӊ㓪ݭ⬠䴶˄MailFrame˅ⱘথ䗕䚂ӊᮍ⊩ⱘᅲ⦄DŽ ᅲ⦄њথ䗕䚂ӊⱘᮍ⊩ৢˈህৃҹᇚ⬠䴶ⱘ৘Ͼؐᇕ㺙៤ϔϾ Mail ᇍ䈵ˈݡՓ⫼ MailSender ᥹ষ 12.8.2 ⬠䴶ᇕ㺙Mailᇍ䈵䖯㸠থ䗕 Transport ㉏䖯㸠থ䗕ेৃDŽ ੠䰘ӊㄝ䚂ӊⳌֵ݇ᙃˈ᳔ৢᇚ䖭ѯֵᙃᇕ㺙៤ϔϾ JavaMail ⱘ Message ᇍ䈵ˈՓ⫼ JavaMail ⱘ ᕫࠄথ䗕䚂ӊⱘ Session ᇍ䈵ˈᕫࠄ Mail ᇍ䈵ˈህৃҹᕫࠄ䚂ӊⱘ᠔ֵ᳝ᙃˈࣙᣀЏ乬ǃℷ᭛ǃᡘ䗕 MailSender ⱘ send ᮍ⊩᳝ϔϾ MailContext ੠ Mail খ᭄ˈা㽕㛑ᕫࠄ MailContext ᇍ䈵ˈህৃҹ } return result; } result[i] = new InternetAddress(addList.get(i)); if (addList.get(i) == null || "".equals(addList.get(i))) continue; for (int i = 0; i < addList.size(); i++) { Address[] result = new Address[addList.size()]; private Address[] getAddress(List addList) throws Exception { //ᇚᄫヺІ䲚ড়䕀ᤶ៤ϔϾ Address ⱘ᭄㒘 } } throw new SendMailException("থ䗕䚂ӊ䫭䇃, 䇋Ẕᶹ䚂ㆅ䜡㕂ঞ䚂ӊⱘⳌֵ݇ᙃ"); } catch (Exception e) { return mail; Transport.send(mailMessage); mailMessage.setContent(main); //ᇚℷ᭛ⱘ Multipart ᇍ䈵䆒ܹ Message Ё } main.addBodyPart(fileBody); fileBody.setFileName(MimeUtility.encodeText(f.getSourceName())); //Ў᭛ӊৡ䖯㸠䕀ⷕ fileBody.attachFile(f.getFile()); MimeBodyPart fileBody = new MimeBodyPart(); //↣Ͼ䰘ӊⱘ body for (FileObject f : mail.getFiles()) { //໘⧚䰘ӊ main.addBodyPart(body); body.setContent(mail.getContent(), "text/html; charset=utf-8"); BodyPart body = new MimeBodyPart(); //ℷ᭛ⱘ body Multipart main = new MimeMultipart(); //ᵘᓎᭈᇕ䚂ӊⱘᆍ఼ mailMessage.setSentDate(new Date()); /Download at http://www.pin5i.com //ֱᄬ Mail ⱘ䰘ӊ public void saveOutBox(Mail mail, MailContext ctx) { } FileUtil.writeToXML(ctx, mail, FileUtil.SENT); //Ў Mail ᇍ䈵⫳៤ xml ᭛ӊ saveFiles(mail, ctx); public void saveSent(Mail mail, MailContext ctx) { ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\system\impl\SystemHandlerImpl.java ᔩЁ(outbox)ˈҹϟҷⷕᅲ⦄䖭ϸϾࡳ㛑ˈЎ SystemHandler ᮄ๲ϸϾ᥹ষᮍ⊩ᑊќҹᅲ⦄DŽ 䖬䳔㽕ᇚ Mail ᇍ䈵ֱᄬࠄᏆথ䗕ⱘ Box Ⳃᔩ˄sent˅ˈབᵰথ䗕༅䋹ˈ䙷Мህ㽕ֱᄬࠄথӊㆅⱘ Box Ⳃ 㽕݇ᖗ䆹ؐⱘ໻ᇣˈᔧথ䗕ᅠ៤ֱᄬࠄᏆথ䗕ⱘ Box ᯊˈህ㽕䆒㕂䖭Ͼ size ؐDŽথ䗕њ䚂ӊৢˈ៥Ӏ থ䗕DŽ䳔㽕⊼ᛣⱘᰃˈ೼ᵘ䗴 Mail ᇍ䈵ⱘᯊ׭ˈMail ᇍ䈵ⱘ size ሲᗻؐЎ 10ˈ⬅Ѣথ䗕ⱘᯊ׭ᑊϡ䳔 ҹϞҷⷕЁⱘ send ᮍ⊩ˈҢ⬠䴶ᕫࠄ৘Ͼؐৢˈᇕ㺙៤ Mail ᇍ䈵ˈݡՓ⫼ MailSender ᥹ষ䖯㸠 } return files; } files.add((FileObject)model.getElementAt(i)); for (int i = 0; i < model.getSize(); i++) { List files = new ArrayList(); ListModel model = this.fileList.getModel(); public List getFileListObjects() { //Ң JList Ёᕫࠄ䰘ӊⱘᇍ䈵䲚ড় } return result; } result.add(re); for (String re : all.split(",")) { if (all.equals("")) return result; List result = new ArrayList(); String all = field.getText(); private List getAddressList(JTextField field) { //䖨ಲഄഔ } return mail; mail.setFiles(getFileListObjects()); mail.setCcs(getAddressList(this.cc)); true, this.textArea.getText(), fromBox); this.subject.getText(), new Date(), "10", this.mainFrame.getMailContext().getAccount(), Mail mail = new Mail(xmlName, getAddressList(this.receiver), String xmlName = UUID.randomUUID().toString() + ".xml"; private Mail getMail(String fromBox) { //ᇚ⬠䴶ⱘ㒘ӊᇕ㺙៤ϔϾ Mail ᇍ䈵 } this.setVisible(false); clean(); //⏙ぎ⬠䴶㒘ӊⱘؐ showMessage("Դⱘ䚂ӊᏆ៤ࡳথ䗕"); this.mainFrame.addSentMail(mail); ࡴњᏆথ䗕ⱘ䲚ড়Ё⏏// /Download at http://www.pin5i.com ೒ 12.9 㓪ݭ䚂ӊᶹⳟ䰘ӊ ೼㓪ݭ䚂ӊᯊˈঠߏ⚍ߏ䰘ӊᬜᵰབ೒ 12.9 ᠔⼎DŽ } } if (result == 0) openFile(file); new Object[]{"ᠧᓔ", "প⍜"}, null); 0, JOptionPane.QUESTION_MESSAGE, null, int result = JOptionPane.showOptionDialog(null, "䇋䗝ᢽ᪡԰", "䗝ᢽ", public void handle(FileObject file) { //䞡ݭ⠊㉏ⱘ handle ᮍ⊩ेৃ, া䳔㽕ᦤկᠧᓔ᪡԰ public class SendListMouseListener extends MainListMouseListener { ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\ui\SendListMouseListener.java SendListMouseListener ㉏ˈҹϟЎ䆹㉏ⱘ݋ԧᅲ⦄DŽ 㒻ᡓ 12.7.2 Ёⱘ MainListMouseListener ㉏ˈᅲ⦄থ䗕䚂ӊⱘᶹⳟ䰘ӊࡳ㛑ˈᮄᓎ Ўˈ⬅Ѣ䖭ѯ䰘ӊᰃ⫼᠋ϞӴⱘˈ಴ℸ⫼᠋⏙Ἦ䆹䰘ӊⱘԡ㕂ˈ᠔ҹϡ䳔㽕঺ᄬЎࡳ㛑ˈ䙷М៥Ӏৃҹ ೼ݭ䚂ӊⱘ䖛⿟Ё䳔㽕ᶹⳟᏆϞӴⱘ䰘ӊⱘ䆱ˈϢ೼Џ⬠䴶Ёᶹⳟ䰘ӊ᳝ϔ⚍ϡৠˈᑊϡ䳔㽕঺ᄬ ᇍ䈵Ёߴ䰸䆹ӑ᭛ӊᇍ䈵ेৃDŽ ᭛ӊ䞡ੑҸˈՓ⫼ UUID ⫳៤ᮄⱘ᭛ӊৡDŽϞӴᅠ䰘ӊৢˈгৃҹ䖯㸠ߴ䰸䰘ӊⱘ᪡԰ˈাᰃҢ JList ӊˈᔧ䳔㽕ֱᄬࠄথӊㆅǃᏆথ䗕ⱘᯊ׭ˈህ䳔㽕ᇚ䖭ӑ݋ԧⱘ᭛ӊ໡ࠊࠄ file ⳂᔩϟˈᑊϨ㽕ᇚ䖭ӑ Ёेৃˈϡ䖛䳔㽕⊼ᛣⱘᯊˈᔧথ䗕䚂ӊᯊˈҢ JList Ёপࠄⱘ᭛ӊᇍ䈵ᣛ৥ⱘᰃᴀഄЁⱘ݋ԧᶤӑ᭛ ໘⧚䚂ӊ䰘ӊकߚㅔऩˈᦤկϔϾ᭛ӊ䗝ᢽ఼ˈ䅽⫼᠋এ䗝ᢽ᭛ӊৢˈݡ⏏ࡴࠄ䰘ӊⱘ JList ᇍ䈵 12.8.3 ໘⧚䚂ӊ䰘ӊ } } showMessage(e.getMessage()); this.systemHander.saveOutBox(mail, this.mainFrame.getMailContext()); //থ䗕༅䋹ֱᄬࠄথӊㆅ } catch (Exception e) { this.systemHander.saveSent(mail, this.mainFrame.getMailContext()); //ⳕ⬹݊Ҫҷⷕ try { private void send() { //থ䗕ᮍ⊩ ҷⷕ⏙ऩ˖code\foxmail\src\org\crazyit\foxmail\ui\MailFrame.java 䙷Мህৃҹׂᬍ send ᮍ⊩ˈথ䗕៤ࡳֱᄬࠄᏆথ䗕ˈ༅䋹ֱᄬࠄথӊㆅDŽ } FileUtil.writeToXML(ctx, mail, FileUtil.OUTBOX); saveFiles(mail, ctx); /Download at http://www.pin5i.com 䆹䚂ӊⱘⳌ݇ݙᆍ䆒㕂ࠄ䚂ӊⱘ㓪ݭ⬠䴶˄MailFrame˅ˈಲ໡ⱘᬜᵰབ೒ 12.11 ᠔⼎DŽ 䚂ӊ໘⧚ⱘ݊Ҫࡳ㛑ࣙᣀಲ໡䚂ӊǃ䕀থ䚂ӊㄝˈᔧ⫼᠋䗝ᢽњϔᇕ䚂ӊ䖯㸠ಲ໡᪡԰ᯊˈৃҹᇚ 12.8.5 ໘⧚䚂ӊⱘ݊Ҫࡳ㛑 ೒ 12.10 ⌟䆩䚂ӊথ䗕 䖤㸠⿟ᑣˈৃҹⳟࠄ䚂ӊᏆ㒣থ䗕DŽ 䚂ӊথ䗕ⱘࡳ㛑Ꮖ㒣ܼ䚼ᅲ⦄ˈ⦄೼៥Ӏৃҹথ䗕䚂ӊ䖯㸠⌟䆩ˈ೒ 12.10 Ўথ䗕䚂ӊⱘᬜᵰ೒ˈ 䆩থ䗕䚂ӊࡳ㛑⌟ 12.8.4 /Download at http://www.pin5i.com ᇍ䇏㗙ᓔথ䚂ӊᑨ⫼᳝ᕜདⱘਃথDŽ гৃҹࡴܹᦤ䝦ⱘࡳ㛑DŽབᵰ䳔㽕Փ⫼ᴀゴ⿟ᑣ䖯㸠⌟䆩ˈᓎ䆂Փ⫼ Gmail ⱘ䚂ㆅDŽᏠᳯᴀゴⱘḜ՟㛑 ᰃᴀゴⱘЏ㽕Ⳃⱘˈ಴ℸ೼ᴀ՟Ё≵᳝ᦤկᅲ⦄DŽ঺໪ˈ೼ᅶ᠋ッᬊপ䚂ӊⱘᯊ׭ˈᬊপњ໮ᇥ䚂ӊˈ ⱘഄᮍˈ՟བ䚂ӊⱘℷ᭛ৃҹࡴܹ HTML 䕀ᤶˈᇚϔѯ HTML 䕀ᤶ៤ৃ㾚ⱘ䚂ӊℷ᭛ˈԚ⬅Ѣ䖭ᑊϡ ৃҹⳈ᥹᪡԰䚂ӊᇍ䈵ˈህᅲ⦄њߴ䰸䚂ӊǃᶹⳟ䚂ӊ੠䖬ॳ䚂ӊㄝࡳ㛑ˈᴀゴḜ՟ᇮ᳝䆌໮䳔㽕ᬍ䖯 П䯈ⱘ䕀ᤶˈᑊᇚ䚂ӊᇍᑨⱘ XML ᭛ӊ੠䚂ӊⱘ䰘ӊֱᄬࠄᴀഄⱘⳌᑨⳂᔩЁˈ೼᪡԰䚂ӊⱘ䖛⿟Ёˈ ᴀゴЁЏ㽕ҟ㒡њՓ⫼ JavaMail 䖯㸠ᬊপ੠থ䗕ᏺ䰘ӊⱘ䚂ӊˈՓ⫼ XStream 䖯㸠ᇍ䈵੠ XML 12.9 ᴀゴᇣ㡖 ᅮDŽއ䕀থ䚂ӊॳ⧚гϔḋˈাᰃᬊֵҎⱘֵᙃᑊϡ㛑⹂ᅮˈ݋ԧ䕀থ㒭䇕ˈ⬅⫼᠋㞾Ꮕ ԅဏߑd؏ݍဏߑನēᅖඕ஍༓ྑަదqREġrࣿζੜუಾ؏ݍᅟ࿉ġ ೒ 12.11 ಲ໡䚂ӊ ㄀ 12 ゴ 䚂ӊᅶ᠋ッg42g /Download at http://www.pin5i.com Ͼ䖲᥹ˈ䖭ѯ䖲᥹ϟ䴶ⱘᄤ㡖⚍ህᰃ䆹䖲᥹ϟ䴶ⱘ᭄᥂ᑧDŽ ᰒ⼎ϡৠⱘ㦰ऩˈ㗠䖲᥹ㅵ⧚៥ӀৃҹՓ⫼ϔỉᷥᴹ䖯㸠ㅵ⧚ˈৃҹৠᯊᄬ೼໮މ⬠䴶ˈḍ᥂ϡৠⱘᚙ 䖛⿟੠ߑ᭄ㅵ⧚៥ӀৃҹᓎゟϔϾЏټ⿟˄ߑ᭄˅ㅵ⧚⬠䴶Ϣᶹⳟ᭄᥂⬠䴶ㄝDŽ㸼ㅵ⧚ǃ㾚೒ㅵ⧚ǃᄬ 䖛ټ໛৘Ͼ⬠䴶ˈࣙᣀ䖲᥹ㅵ⧚⬠䴶ǃ㸼ㅵ⧚⬠䴶ǃ㾚೒ㅵ⧚⬠䴶ǃᄬޚ೼㓪ݭ⿟ᑣࠡˈ៥Ӏ䳔㽕 13.2 ᓎゟ⬠䴶 䴶ˈ៥Ӏህᓔྟᅲ⦄䖭ѯ᠔ᅮНⱘࡳ㛑DŽ ᑊϨ㽕ਞ䆝៥Ӏ䖭Ͼㅵ⧚఼ˈMySQL ⱘ݋ԧⳂᔩˈ៥ӀৃҹՓ⫼⿟ᑣএ䇗⫼䖭ѯ MySQL ⱘੑҸDŽϟ ᭄᥂ⱘᇐߎϢᇐܹˈԚᰃˈࠡᦤህᰃՓ⫼ⱘᅶ᠋ッᖙ乏ᅝ㺙 MySQL ᭄᥂ᑧˈއ䖭ḋৃҹ䕏ᵒ㾷ˈއ㾷 ԰ˈ៥Ӏৃҹ㓪ݭ⿟ᑣᴹᅲ⦄ˈԚᰃˈ᳈དࡲ⊩ህᰃՓ⫼ MySQL ⱘੑҸ˄mysql ៪㗙 mysqldump˅ᴹ TABLE ᴹ䖒ࠄⳂⱘDŽ䰸њ䖭ѯࡳ㛑໪ˈ䖬䳔㽕ᇍ᭄᥂ᑧЁⱘ᭄᥂䖯㸠ᇐߎ੠ᇐϢⱘ᪡԰ˈ䖯㸠䖭ѯ᪡ ࣙᣀ߯ᓎ㸼ǃׂᬍ㸼ㄝࡳ㛑ˈ៥ӀৃҹՓ⫼ JDBC Ⳉ᥹ᠻ㸠 SQL 䇁হЁⱘ CREATE TABLE ੠ ALTER 䖛⿟੠ߑ᭄ㅵ⧚ˈ䖭ѯࡳ㛑⚍៥Ӏ䛑ৃҹՓ⫼ JDBC ᅲ⦄ˈ՟བ㸼ㅵ⧚Ёټ⧚ǃ㸼ㅵ⧚ǃ㾚೒ㅵ⧚ǃᄬ MySQL ㅵ⧚఼ˈЏ㽕ࡳ㛑ᰃ䅽⫼᠋ৃҹ䕏ᵒ䖯㸠৘⾡ⱘ MySQL ᪡԰ˈࣙᣀ䖲᥹ㅵ⧚ǃ᭄᥂ᑧㅵ 13.1 MySQLㅵ⧚఼ॳ⧚ Ϣ MySQL5.1 П䯈ⱘᖂᇣᏂ߿ˈ䖭ѯᏂ߿ᇍ៥Ӏᓔথ᠔ѻ⫳ⱘᕅડˈᇚ೼ϟ䴶ⱘゴ㡖Ё䆺㒚ҟ㒡DŽ MySQL5.0DŽ⬅Ѣ MySQL ৘Ͼ⠜ᴀ䯈䛑ᄬ೼Ꮒ߿ˈ՟བヨ㗙೼ᓔথ䖭Ͼㅵ⧚఼ⱘᯊ׭ˈህ䘛ࠄ MySQL5.0 ᴀゴ᠔ᓔথⱘ MySQL ㅵ⧚఼ᰃ෎Ѣ MySQL5.0 ᓔথⱘˈ಴ℸབᵰ㽕ᕫࠄ᳔Շⱘ䖤㸠ᬜᵰˈ䇋Փ⫼ Ѣ䖭ѯᎹ݋㒭⫼᠋ᏺᴹⱘՓ⫼ԧ偠ঞᮍ֓ᗻDŽއ㗙ⱘ䴦ⴤˈ᳈໮ⱘህᰃপ ⊼ᛣⱘᰃ೒ᔶ⬠䴶ⱘ໘⧚DŽ䖭ѯㅵ⧚Ꮉ݋ˈᅲ⦄ⱘॳ⧚ᑊ᮴໾໻Ꮒ߿ˈԚᰃાϾᎹ݋᳈㛑ᕫࠄ໮᭄Փ⫼ 㸠৘⾡ⱘ᪡԰ˈ಴ℸˈᓔথϔϾ MySQL ㅵ⧚఼ˈ䰸њ䳔㽕㓪ݭϔѯ᪡԰᭄᥂ᑧⱘ SQL ҹ໪ˈ䖬䳔㽕 ⱘ䙷ѯӬ⾔Ꮉ݋ⱘᅲ⦄ॳ⧚DŽ೼ᴀゴᓔ༈Ꮖ㒣ᦤࠄˈ䖭ѯㅵ⧚Ꮉ݋ˈᦤկњ৘⾡ⱘ೒ᔶ⬠䴶䅽៥Ӏএ䖯 ೼ᴀゴЁˈ៥Ӏᇚ㞾ᏅᓔথϔϾㅔऩⱘ MySQL ㅵ⧚఼DŽ೼ᓔথⱘ䖛⿟Ёˈ䅽໻ᆊњ㾷ࠡ䴶᠔䆆ࠄ ԩ㓪ݭ SQL 䇁হˈা䳔㽕䗮䖛᪡԰೒ᔶ⬠䴶ˈህৃҹ䖒ࠄ᪡԰᭄᥂ᑧⱘⳂⱘDŽ ៥Ӏᏺᴹњᵕ໻ⱘᮍ֓ˈৃҹ೼ϔϾᅠܼ೒ᔶ࣪ⱘ⬠䴶䖯㸠᭄᥂ᑧ໘⧚DŽՓ⫼䖭ѯᎹ݋ˈԴৃҹϡពབ 䖛⿟ㄝ˅ⱘ⬠䴶ˈ䖭ѯࡳ㛑Ўټ㋴˄㸼ǃ㾚೒ǃᄬܗ᥂ⱘ೒ᔶ⬠䴶ǃ᪡԰᭄᥂ⱘ⬠䴶ǃ᪡԰৘Ͼ᭄᥂ᑧ ೒ᔶㅵ⧚Ꮉ݋DŽ䖭ѯӬ⾔ⱘᎹ݋Ў៥Ӏᦤկњकߚᮍ֓ⱘࡳ㛑এㅵ⧚ MySQL ᭄᥂ᑧˈ՟བᦤկ⌣㾜᭄ 㞾ᏺⱘ MySQL Administrator ㄝˈ䖭ѯ䛑ᰃ៥Ӏᑇᯊ೼ᓔথ MySQL ᭄᥂ᑧᑨ⫼ᯊकߚᐌ⫼ⱘ MySQL ⬅Ѣ MySQL ᑨ⫼ᑓ⊯ˈ಴ℸ⍠⦄ߎ䆌໮ MySQL ⱘᅶ᠋ッˈ՟བ MySQL FrontǃNavicat Ϣ MySQL ᢹ᳝ԧ⿃ᇣǃ䗳ᑺᖿㄝӬ⚍ˈ㹿ᑓ⊯ⱘᑨ⫼೼ЁᇣൟӕϮⱘ IT ㋏㒳Ёˈ᳈䞡㽕ⱘϔ⚍ᰃˈᅗᰃᓔ⑤ⱘDŽ Ͼᇣൟⱘ݇㋏ൟ᭄᥂ᑧˈᓔথ㗙ᰃ⨲݌ⱘ MySQL AB ݀ৌˈѢ 2008 ᑈ 6 ᳜ 1 ᮹㹿 Sun ݀ৌᬊ䌁DŽMySQL ᔧࠡ IT 㸠Ϯ⌕㸠ⱘ݇㋏ൟ᭄᥂ᑧ᳝ OracleǃSQL ServerǃDB2 ੠ MySQL ㄝDŽ݊Ё MySQL ᰃϔ ㄀  ゴ0\64/ㅵ⧚఼ /Download at http://www.pin5i.com ೒ 13.2 Џ⬠䴶  ೒ 13.2 ᠔⼎DŽ ষ໪ˈ䖬䳔㽕ᦤկϔỉᷥˈ᳈Ⳉ㾖ⱘሩ⼎ᔧࠡ᠔Փ⫼ⱘ䖲᥹ˈҹঞ䆹䖲᥹ϟ䴶᠔᳝ⱘ᭄᥂ᑧDŽЏ⬠䴶བ Џ⬠䴶ᦤկ৘⾡ࡳ㛑ⱘܹষˈৃҹ䅽⫼᠋೼䆹⬠䴶ЁՓ⫼៪㗙䖯ܹ৘Ͼࡳ㛑ˈ䰸њ䳔㽕ᦤկ䖭ѯܹ 13.2.2 Џ⬠䴶 ೼ᴀゴҷⷕЁᇍᑨⱘᰃ ConfigFrame ㉏DŽ ৺ℷ⹂ˈৃҹ߸ᮁ೼ bin Ⳃᔩϟᰃ৺ᄬ೼ⳌᑨⱘੑҸˈ䖭ѯᇚ೼ϟ䴶ⱘゴ㡖Ёᦣ䗄DŽMySQL ᅝ㺙Ⳃᔩ mysql Ϣ mysqldump ㄝੑҸᰃᄬ೼Ѣ MySQL ᅝ㺙Ⳃᔩϟⱘ bin Ⳃᔩⱘˈ಴ℸ߸ᮁ⫼᠋᠔䗝ᢽⱘⳂᔩᰃ 䴶ˈ䖭䞠䳔㽕⊼ᛣⱘᰃˈ៥Ӏ೼ᅲ⦄ⱘᯊ׭ˈ䳔㽕߸ᮁ⫼᠋᠔䗝ᢽⱘ MySQL[ᅝ㺙Ⳃᔩᰃ৺ℷ⹂ˈ⬅Ѣ ᑊϨࡴܹϔϾ⹂ᅮϢপ⍜ᣝ䪂DŽᔧ⫼᠋䗝ᢽњ MySQL ⱘᅝ㺙Ⳃᔩˈ⚍ߏњ⹂ᅮᯊˈህᰒ⼎៥ӀⱘЏ⬠ 䅽⫼᠋䗝ᢽ MySQL ᅝ㺙ⳂᔩकߚㅔऩˈাᦤկϔϾⳂᔩ䗝ᢽᅝ㺙ҹঞᰒ⼎Ⳃᔩ䏃ᕘⱘ JTextFeildˈ ೒ 13.1 MySQL ᅝ㺙Ⳃᔩ䗝ᢽ⬠䴶  ⱘݙ㕂ੑҸˈ಴ℸ䳔㽕ᣛᅮ MySQL ⱘᅝ㺙ⳂᔩDŽ೒ 13.1 ᰃᅝ㺙Ⳃᔩ䗝ᢽ⬠䴶DŽ ᔧ䖯ܹㅵ⧚఼ᯊˈ៥Ӏህ䳔㽕䅽⫼᠋এ䗝ᢽ MySQL ⱘᅝ㺙Ⳃᔩˈॳ಴ህᰃ಴Ў៥Ӏ䳔㽕 MySQL MySQLᅝ㺙Ⳃᔩ䗝ᢽ⬠䴶 13.2.1 /Download at http://www.pin5i.com return this.connectionName; public String toString() { //䞡ݭ toString ᮍ⊩, 䖨ಲ䖲᥹ৡ⿄ } else return ImageUtil.CONNECTION_OPEN; if (super.connection == null) return ImageUtil.CONNECTION_CLOSE; public Icon getIcon() { //ᅲ⦄᥹ষ ViewObject ⱘᮍ⊩, ḍ᥂ϡৠⱘ䖲᥹⢊ᗕᰒ⼎ϡৠⱘ೒⠛ //ⳕ⬹ setter ੠ getter ᮍ⊩ private String port; //䖲᥹ッষ private String host; //䖲᥹ ip private String password; //ᆚⷕ private String username; //⫼᠋ৡ private String connectionName; //MySQL 偅ࡼ private final static String DRIVER = "com.mysql.jdbc.Driver";//MySQL 偅ࡼ public class ServerConnection extends ConnectionNode { ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\object\tree\ServerConnection.java } //ⳕ⬹ setter ੠ getter ᮍ⊩ public abstract Connection connect(); //䖲᥹ᮍ⊩ˈ⬅ᄤ㉏এᅲ⦄ protected Connection connection; //JDBC ⱘ Connection ᇍ䈵 public abstract class ConnectionNode implements ViewObject { ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\object\tree\ConnectionNode.java ᑧ㡖⚍এ㒻ᡓDŽ঺໪ˈ䖬䳔㽕ᦤկϔϾ connect ⱘᢑ䈵ᮍ⊩ˈ䳔㽕䅽ᄤ㉏এᅲ⦄DŽ Ѡ⾡ህᰃϡ䳔㽕ᏺ᳝䖲᥹ⱘ㡖⚍ˈ಴ℸ៥Ӏৃҹᇚᏺ᳝䖲᥹ⱘ㡖⚍ᢑ䈵៤ϔϾ⠊㉏ˈ䅽䖲᥹㡖⚍੠᭄᥂ Ͼ㡖⚍ᇍ䈵ˈৃҹ᳝ϸ⾡ᔶᓣˈ㄀ϔ⾡ህᰃ䳔㽕ᏺ䖲᥹ⱘ㡖⚍ˈ՟བ᭄᥂ᑧ䖲᥹㡖⚍੠᭄᥂ᑧ㡖⚍ˈ㄀ 䆹᥹ষা᳝ϔϾ getIcon ᮍ⊩ˈ䖨ಲϔϾ Icon ᇍ䈵ˈ㸼⼎䖭ѯ⬠䴶᠔ᇍᑨⱘ೒⠛ˈ঺໪ˈᷥϞⱘ৘ } Icon getIcon(); //䖨ಲᰒ⼎ⱘ೒⠛ public interface ViewObject { ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\object\ViewObject.java ㋴ㄝDŽܗϾ㡖⚍ˈЏ⬠䴶ে䖍߫㸼᠔ᰒ⼎ⱘ৘Ͼ 䈵ˈᮄᓎ৘Ͼ㾚೒ᇍ䈵ⱘ᥹ষ ViewObjectˈ䆹᥹ষᇚᰃ᠔᳝㾚೒ᇍ䈵ⱘ⠊㉏ˈ䖭ѯ㾚೒ᇍ䈵ࣙᣀᷥⱘ৘ DefaultTreeCellRenderer ᴹ䆒㕂৘Ͼ㡖⚍ⱘ೒⠛ҹঞ᭛ᄫˈᔧ✊ˈ䖬䳔㽕ᇚ৘Ͼ㡖⚍ᢑ䈵៤ϡৠⱘᇍ 䖭䞠䳔㽕⊼ᛣⱘᰃˈ៥Ӏ᳈ᤶњᷥⱘ৘Ͼ㡖⚍೒⠛ˈ಴ℸ䳔㽕Ў JTree ⏏ࡴϔϾ ᆍˈ៥Ӏ೼ᴀゴⱘ乍ⳂЁϡᦤկ䖭ѯࡳ㛑DŽ 䖛⿟ˈᔧ✊ˈ៥Ӏ೼ᑇᯊՓ⫼݊Ҫㅵ⧚Ꮉ݋ⱘᯊ׭ˈ䖬ৃҹ᳝㾺থ఼ㄝݙټϝϾᄤ㡖⚍˖㸼ǃ㾚೒੠ᄬ ㄀ϔሖ㡖⚍ህᰃ䖲᥹ˈ㄀Ѡሖ㡖⚍ህᰃ䆹䖲᥹ϟ᠔ᇍᑨⱘ᠔᳝ⱘ᭄᥂ᑧˈ↣ϔϾ᭄᥂ᑧ㡖⚍ϟ䴶ৃҹ᳝ ೼Џ⬠䴶ⱘᎺ䖍䚼ߚˈᦤկϔỉᷥ䅽⫼᠋कߚⳈ㾖ⱘⳟࠄ䖲᥹ⱘⳌֵ݇ᙃˈ䖭ỉᷥৃҹ䱤㮣ḍ㡖⚍ˈ ‰ ᠧᓔᠻ㸠 SQL ⱘ⬠䴶˖ᠧᓔϔϾᠻ㸠 SQL 䇁হⱘ⬠䴶DŽ 䖛⿟Ϣߑ᭄DŽټ䖛⿟˄ߑ᭄˅˖ᶹⳟ᭄᥂ᑧЁ᠔᳝ⱘᄬټ‰ ᶹⳟᄬ ‰ ᶹⳟ㾚೒˖ᶹⳟᔧ᭄ࠡ᥂ᑧЁ᠔᳝ⱘ㾚೒DŽ ‰ ᶹⳟ㸼˖ᶹⳟᔧ᭄ࠡ᥂ᑧЁ᠔᳝ⱘ㸼DŽ ‰ ⏏ࡴ䖲᥹˖ৃҹ䅽⫼᠋⏏ࡴϔϾ䖲᥹DŽ Џ⬠䴶⬅ϔϾᎹ݋ᷣˈϔỉᷥҹঞϔϾ JList 㒘៤ˈ݊ЁᎹ݋ᷣЁࣙᣀⱘ᪡԰བϟ˖ /Download at http://www.pin5i.com ϔϾ TableNode ᇍ䈵ҷ㸼ϔϾ㸼ⱘ㡖⚍ˈ䳔㽕ᦤկϔϾ Database ሲᗻᴹ㸼⼎䖭Ͼᇍ䈵ᰃሲѢાϾ } } return "㸼"; public String toString() { //䞡ݭ toString ᮍ⊩ } return ImageUtil.TABLE_TREE_ICON; public Icon getIcon() { //䖨ಲ㸼ⱘᷥ㡖⚍೒⠛ private Database database; //᠔ሲⱘ᭄᥂ᑧ㡖⚍ public class TableNode implements ViewObject { ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\object\tree\TableNode.java ᅮDŽއ⫣䛑䳔㽕ᬍবᅗӀⱘ೒⠛ˈ㗠ᰒ⼎ԩ⾡೒⠛ˈ⬅ getIcon ᮍ ៪㗙 Database ᇍ䈵ˈ䛑䳔㽕ᅲ⦄ ViewObject ⱘ getIcon ᮍ⊩ˈᔧ䖲᥹㡖⚍៪㗙᭄᥂ᑧ㡖⚍㹿ᠧᓔᯊˈ ᅗӀ݅ѿϔѯϡӮ㒣ᐌ߯ᓎⱘᅲ՟ˈ՟བ ConnectionDŽ঺໪ˈ䳔㽕⊼ᛣⱘᰃˈ᮴䆎 ServerConnection 䛑ᰃᶤϾ䖲᥹㡖⚍ϟ䴶ⱘᄤ㡖⚍ˈ಴ℸ䳔㽕䆄ᔩᅗⱘ⠊㡖⚍ˈᔧ✊ˈᑊϡᰃㅔऩⱘ䖯㸠䆄ᔩˈ䖬ৃҹ䅽 Database 㡖⚍ᇍ䈵ࣙᣀ᭄᥂ᑧⱘৡᄫˈ঺໪䖬䳔㽕ϔϾ ServerConnection ᇍ䈵ˈ⬅Ѣ↣Ͼ᭄᥂ᑧ } } public Connection connect() { //ᅲ⦄⠊㉏ⱘ connect ᮍ⊩ } return this.databaseName; public String toString() { //䞡ݭ toString ᮍ⊩ } return ImageUtil.DATABASE_OPEN; if (this.connection == null) return ImageUtil.DATABASE_CLOSE; public Icon getIcon() { //ᅲ⦄᥹ষⱘᮍ⊩, ߸ᮁ䆹᭄᥂ᑧᰃ৺䖲᥹, ݡ䖨ಲϡৠⱘ೒⠛ } this.serverConnection = serverConnection; this.databaseName = databaseName; public Database(String databaseName, ServerConnection serverConnection) { //䳔㽕Փ⫼᭄᥂ᑧৡ⿄Ϣ᳡ࡵ఼䖲᥹ᇍ䈵ᵘ䗴 private ServerConnection serverConnection; //᭄᥂ᑧ᠔ሲⱘ᳡ࡵ఼䖲᥹ private String databaseName; //᭄᥂ᑧৡᄫ public class Database extends ConnectionNode { ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\object\tree\Database.java ᮍ⊩ˈ䖨ಲ䖲᥹ⱘৡ⿄DŽ ᮍ⊩ˈ߸ᮁ⠊㉏ ConnectionNode ⱘ connection ሲᗻᰃ৺Ўぎᴹᰒ⼎ϡৠⱘ೒⠛ˈ䖬䳔㽕䞡ݭ toString ࣙᣀ䖲᥹ৡ⿄ǃMySQL ⫼᠋ৡǃᆚⷕǃ䖲᥹ⱘ IP ϢッষㄝֵᙃDŽ䆹ᇍ䈵ᅲ⦄њ ViewObject ⱘ getIcon ϔϾ ServerConnection ᇍ䈵㸼⼎ϔϾ䖲᥹㡖⚍ˈϔϾ䖲᥹㡖⚍ᔧ✊䳔㽕ࣙᣀϔѯ䖲᥹ⱘⳌֵ݇ᙃˈ } } public Connection connect() { ᅲ⦄⠊㉏ⱘ connect ᮍ⊩// { /Download at http://www.pin5i.com return this; } setForeground(Color.white); setBackground(Color.blue); if (isSelected) { //䆒㕂䗝Ёᯊⱘᄫԧ买㡆 label.setToolTipText(vd.toString()); label.setIcon(vd.getIcon());//䆒㕂೒⠛ ViewObject vd = (ViewObject)value; //ᕫࠄ ViewObject ᇍ䈵 value, index, isSelected, cellHasFocus); JLabel label = (JLabel)super.getListCellRendererComponent(list, int index, boolean isSelected, boolean cellHasFocus) { public Component getListCellRendererComponent(JList list, Object value, public class ListCellRenderer extends DefaultListCellRenderer { ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\list\ListCellRenderer.java ㋴໘⧚㉏ˈᴹᣛᅮᰒ⼎ⱘ᭄᥂೒⠛DŽܗկϔϾ ℸ䖭ϝϾ᭄᥂ᇍ䈵䛑䳔㽕ֱᄬϔϾ Database ሲᗻˈ㸼⼎䆹᭄᥂᠔ሲѢⱘ᭄᥂ᑧDŽϢᷥϔḋˈ䖬䳔㽕ᦤ ㋴ˈ಴ܗ䖛⿟䛑ᰃᶤϔϾ᭄᥂ᑧϟ䴶ⱘټԐˈ䛑䳔㽕ᅲ⦄ getIcon ᮍ⊩ᑊ䞡ݭ toStringDŽ㸼ǃ㾚೒੠ᄬ 䖛⿟᭄᥂ˈ䖭ϝϾᇍ䈵䛑䳔㽕ᅲ⦄ ViewObject ᥹ষˈ݋ԧⱘᅲ⦄ϢϝϾ㡖⚍ⱘᅲ⦄㉏ټ㾚೒᭄᥂੠ᄬ ೼ᴀゴⱘҷⷕЁˈ៥Ӏ߯ᓎњ TableDataǃViewData ੠ ProcedureData ϝϾ㉏ߚ߿ҷ㸼㸼᭄᥂ǃ 䖛⿟˄ߑ᭄˅ˈ៥Ӏ䳔㽕ᓎゟϝϾᇍ䈵ˈߚ߿ҷ㸼䖭ϝ⾡᭄᥂㉏ൟDŽټϝ⾡᭄᥂㉏ൟ˖㸼ǃ㾚೒੠ᄬ ᇍ䈵䛑ৃҹᇚᅗӀⱘ name ሲᗻᢑ䈵ࠄϔϾ⠊㉏Ёˈ৘Ͼᇍ䈵এ㒻ᡓᅗेৃˈ೼ᴀ՟Ёˈ៥Ӏ᠔⍝ঞ᳝ ㋴ܗ㋴ˈ䛑ᰃ ViewObject ⱘᅲ⦄㉏ˈা䳔㽕ᅲ⦄ getIcon ᮍ⊩Ϣ䞡ݭ toString ᮍ⊩ेৃDŽ↣Ͼ߫㸼ⱘܗ ᷥⱘⳌ݇໘⧚ህᅠ៤њˈЏ⬠䴶ⱘে䖍ᰃϔϾ߫㸼ˈᇍᑨⱘᰃϔϾ JList ᇍ䈵ˈJList 䞠䴶ⱘ↣ϔϾ ᑨⱘ೒⠛Ϣ᭛ᄫ˄Ң toString ᮍ⊩㦋ᕫ˅DŽ ⠛ˈ៥Ӏⱘ ViewObject ᥹ষᦤկњ getIcon ᮍ⊩ˈ᠔ҹ៥Ӏህৃҹ೼㡖⚍໘⧚㉏Ёᕫࠄ↣Ͼ㡖⚍᠔ᇍ ೼㡖⚍໘⧚㉏ TreeCellRenderer ㉏ˈᕫࠄ↣Ͼ㡖⚍ⱘ ViewObject ৢˈህৃҹЎ㡖⚍䆒㕂᭛ᄫ੠೒ } } return this; else this.setForeground(getTextNonSelectionColor()); if (sel) this.setForeground(Color.blue); //߸ᮁᰃ৺䗝ᴹ䆒㕂ᄫԧ买㡆 this.setIcon(obj.getIcon());//䆒㕂೒⠛ this.setText(obj.toString());//䆒㕂᭛ᄫ if (obj == null) return this; ViewObject obj = (ViewObject)node.getUserObject(); //㦋ᕫ↣Ͼ㡖⚍ⱘ ViewObject DefaultMutableTreeNode node = (DefaultMutableTreeNode)value; boolean hasFocus) { boolean sel, boolean expanded, boolean leaf, int row, public Component getTreeCellRendererComponent(JTree tree, Object value, public class TreeCellRenderer extends DefaultTreeCellRenderer { ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\tree\TreeCellRenderer.java DefaultTreeCellRenderer ㉏ˈ䅽݊ᕫࠄ䖭ѯ㡖⚍ᇍ䈵ˈᑊ䆒㕂Ⳍᑨⱘ᭛ᄫ੠೒⠛DŽ 䖛⿟㡖⚍ˈᅲ⦄ᮍ⊩Ϣ TableNode ㉏ԐDŽϟ䴶Ўᷥ㡖⚍⏏ࡴϔϾټ⚍ϟ䴶ⱘ㾚೒㡖⚍੠ᄬ Ϣ TableNode ϔḋˈ঺໪ݡ⃵߯ᓎϸϾᇍ䈵˖ViewNode ੠ ProcedureNodeˈߚ߿ҷ㸼᭄᥂ᑧ㡖 ৃҹ೼ে䖍ⱘ߫㸼Ёᰒ⼎ᇍᑨ᭄᥂ᑧⱘ㸼DŽ ᥂ᑧ䞠䴶䛑᳝㸼˅ˈৃҹᇚ䖭Ͼ㸼㡖⚍⧚㾷៤ᰃᇐ㟾ᷣⱘᶤϔ㒘៤䚼ߚˈᔧ⫼᠋⚍ߏњ䖭Ͼ㡖⚍ৢˈህ ᭄᥂ᑧϟ䴶ⱘᄤ㡖⚍DŽབ೒ 13.2 ᠔⼎ˈ៥ӀৃҹⳟᷥЁ↣Ͼ᭄᥂ᑧ㡖⚍ⱘ㸼㡖⚍䛑ᰃϔ㟈ⱘ˄↣Ͼ᭄ /Download at http://www.pin5i.com 䴶ᰒ⼎ᇍ䈵ᇍᑨⱘ㉏ᰃ DataFrameˈ᭄᥂ᰒ⼎⬠䴶བ೒ 13.4 ᠔⼎DŽ ℸ៥Ӏ೼㓪ݭᠧᓔ᭄᥂ᰒ⼎⬠䴶ⱘҷⷕⱘᯊ׭ˈ↣⃵䛑䳔㽕এ߯ᓎ䖭Ͼ⬠䴶ᇍ䈵ⱘᅲ՟DŽ೼ᴀゴЁˈ⬠ ಴ˈމᰒ⼎⬠䴶ˈᇚ⫼᠋ᛳ݈䍷ⱘ᭄᥂ᰒ⼎ߎᴹDŽ⬅Ѣϔ㠀Ӯᄬ೼ᠧᓔ໮Ͼ㸼៪㗙໮⃵ᠻ㸠 SQL ⱘᚙ ϔ⾡ᰃঠߏϔϾ㸼ᶹⳟ᭄᥂ⱘᯊ׭ˈ঺໪ϔ⾡ህᰃᠻ㸠 SQL ⱘᯊ׭˄ᠻ㸠ᶹ䆶ⱘ SQL˅ˈህӮᠧᓔ᭄᥂ ೼ᭈϾㅵ⧚఼Ёˈ៥Ӏ䳔㽕ϔϾ᭄᥂ᰒ⼎ⱘ⬠䴶ˈ㗠Ϩা᳝ϔϾDŽᠧᓔ᭄᥂ᰒ⼎⬠䴶ⱘ䗨ᕘ᳝ϸ⾡ˈ 13.2.3 ᭄᥂ᰒ⼎⬠䴶 ೒ 13.3 Џ⬠䴶ᬜᵰ  ೒ 13.3 ᠔⼎˖ ߯ᓎЏ⬠䴶ৢˈ៥Ӏৃҹ೼߯ᓎᷥϢ߯ᓎ߫㸼ⱘᯊ׭ࡴܹϔѯ῵ᢳ᭄᥂ᴹᶹⳟᬜᵰˈ݋ԧⱘᬜᵰབ dataList.setLayoutOrientation(JList.VERTICAL_WRAP); //dataList ᰃ⬠䴶Ёⱘ JList ᇍ䈵 ࡼˈ䇗⫼ҹϟҷⷕेৃᅲ⦄˖ Ё߯ᓎᇍᑨⱘᷥϢ߫㸼DŽ䖭䞠䳔㽕⊼ᛣⱘᰃˈᔧ߯ᓎ߫㸼˄JList˅ⱘᯊ׭ˈৃҹᇚ JList 䆒㕂Ў῾৥⒮ ࠄ䖭䞠ˈЏ⬠䴶ⱘ৘Ͼᇍ䈵䛑߯ᓎདњˈᴀゴЁᇍᑨⱘЏ⬠䴶ᇍ䈵ᰃ MainFrame ㉏ˈৃҹ೼䆹㉏ { { /Download at http://www.pin5i.com updateUI(); selectColumn = -1; public void mouseClicked(MouseEvent e) { this.addMouseListener(new MouseAdapter() { //Ў JTable ⏏ࡴ哴ᷛⲥ਀఼ }); } selectColumn = tableColumn; int tableColumn = header.columnAtPoint(e.getPoint()); header.getTable().clearSelection(); public void mouseClicked(MouseEvent e) { header.addMouseListener(new MouseAdapter() { //Ў㸼༈⏏ࡴ哴ᷛџӊⲥ਀఼ public DataTable(DefaultTableModel model) { private int selectColumn = -1; //ᔧ⚍ߏ㸼༈ᯊ, 㸼⼎ᔧࠡ᠔䗝ᢽⱘ߫ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\table\DataTable.java Ӏ䳔㽕䅽䖭Ͼ JTable ᇍ䈵ৃҹᭈ߫䗝ᢽˈህ䳔㽕㞾Ꮕ㓪ݭϔϾ㉏এ㒻ᡓ JTableDŽ ݡࡼᗕⱘ⫳៤߫Ϣ᭄᥂DŽ䰸њ䖭䞠П໪ˈ៥Ӏ䖬䳔㽕Ў䖭Ͼ JTable ᇍ䈵䖯㸠ϔѯ乱໪ⱘ໘⧚ˈ՟བ៥ ߫ˈ⬅Ѣ៥Ӏϡৃ㛑ⶹ䘧⫼᠋ᇚ㽕ᠧᓔⱘ㸼᳝໮ᇥ߫ˈ಴ℸা㛑೼⫼᠋ᠧᓔ㸼ⱘᯊ׭ˈᕫࠄ䆹㸼ⱘֵᙃ Ҫ JTable ϔḋˈᢹ᳝೎ᅮⱘ݊ڣ೼䖭Ͼ⬠䴶Ёˈ䳔㽕⊼ᛣⱘᰃˈ䖭Ͼ߫㸼ᇍᑨⱘ JTable ᇍ䈵ᑊϡ ‰ छᑣ˖᪡԰Ϣ䰡ᑣϔḋˈԚᰃᇍ᠔䗝ᄫ↉䖯㸠छᑣᥦᑣDŽ ‰ 䰡ᑣ˖ᔧ⫼᠋䗝ᢽњᶤϔ߫ᑊ⚍ߏ䆹೒ᷛⱘᯊ׭ˈህᇍ䆹߫᠔ᇍᑨⱘᄫ↉䖯㸠䰡ᑣᥦᑣDŽ ‰ ࠋᮄ˖ࠋᮄᔧࠡ⬠䴶ⱘ᭄᥂DŽ ⬠䴶↨䕗ㅔऩˈϔϾᎹ݋ᴵࡴϔϾ㸼ḐेৃˈᎹ݋ᴵЁࣙᣀⱘࡳ㛑᳝˖ ೒ 13.4 ᭄᥂ᰒ⼎⬠䴶  /Download at http://www.pin5i.com Ꮉ݋ˈህ䳔㽕ᦤկϔϾᮄ๲䖲᥹ⱘ⬠䴶ˈ䅽⫼᠋এ߯ᓎ৘Ͼ䖲᥹ˈ⬠䴶བ೒ 13.6 ᠔⼎DŽ 䖲᥹ᰃᭈϾᎹ݋ⱘ᳔෎⸔䚼ߚˈ≵᳝䖲᥹ˈ݊Ҫӏԩ᪡԰䛑ϡ㛑䖯㸠ˈ಴ℸՓ⫼䖭Ͼ MySQL ㅵ⧚ 13.2.4 ߯ᓎ䖲᥹⬠䴶 ೒ 13.5 ᭄᥂ᰒ⼎⬠䴶䗝ᢽᭈ߫  ⳟࠄᬜᵰབ೒ 13.5 ᠔⼎DŽ ᅠ䖭ѯৢˈৃҹ⚍ߏϔ߫ˈخ঺໪ˈ៥Ӏ䖬䳔㽕ᦤկϔϾ䖨ಲ selecColumn ؐⱘ public ⱘᮍ⊩DŽ } return super.isCellSelected(row, column); if (this.selectColumn == column) return true; //བᵰ᭄߫Ϣᔧࠡ䗝ᢽⱘ߫Ⳍৠ,䖨ಲ true public boolean isCellSelected(int row, int column) { Ḑᰃ৺㹿䗝Ё, 䞡ݭ JTable ⱘᮍ⊩ܗ //߸ᮁϔϾऩ ߫䆒Ў䗝Ё⢊ᗕˈҹϟᰃ isCellSelected ᮍ⊩ⱘᅲ⦄˖ 䳔㽕䞡ݭ JTable ⱘ isCellSelected ᮍ⊩ˈབᵰ selectColumn ϡᰃ-1ˈ䙷Мህ䳔㽕ᇚ⫼᠋᠔䗝ᢽⱘ߫ᭈ ⱘ߫ⱘ㋶ᓩˈᔧ೼ JTable ⱘ݊Ҫഄᮍ⚍ߏњ哴ᷛᯊˈህ䆒㕂䆹ؐЎ-1ˈ㸼⼎≵᳝䗝ᢽ㸼༈DŽ䙷М៥Ӏህ ᛣҹϞҷⷕЁⱘ㉏ሲᗻ selectColumnˈᔧ៥Ӏ⫼哴ᷛ⚍ߏњ㸼༈ⱘᯊ׭ˈህᇚ䆹ؐ䆒Ўᔧࠡ䗝ᢽ⊼ { ;({ { /Download at http://www.pin5i.com ԰ˈ᳔ৢᠻ㸠ֱᄬˈ㸼⬠䴶བ೒ 13.7 ᠔⼎DŽ 䆌ぎ੠Џ䬂ㄝֵᙃDŽ߯ᓎ㸼⬠䴶ᰃᴀゴЁ᳔Ў໡ᴖⱘ⬠䴶ˈ⫼᠋ৃҹ䱣ᛣⱘ೼㸼Ё䖯㸠᪡ܕ㉏ൟǃᰃ৺ ᔧ⫼᠋䳔㽕߯ᓎϔϾ㸼ⱘᯊ׭ˈህ䳔㽕ᦤկϔϾ⬠䴶䅽⫼᠋এ䕧ܹ㸼ⱘ৘⾡᭄᥂ˈࣙᣀᄫ↉ৡ⿄ǃ 13.2.5 ߯ᓎ㸼⬠䴶 ‰ ⹂ᅮ੠প⍜˖⚍ߏ⹂ᅮ⏏ࡴ䖲᥹ᑊ݇䯁䆹にষˈ⚍ߏপ⍜ϡֱᄬ䖲᥹ᑊ݇䯁にষDŽ ৃҹ⏏ࡴ䖭Ͼ䖲᥹DŽ ‰ ⌟䆩䖲᥹˖⌟䆩䕧ܹⱘֵᙃᰃ৺ৃҹ䖲᥹ࠄ MySQL ᳡ࡵ఼Ёˈᔧ✊ˈབᵰ⌟䆩ϡ㛑䖲᥹ˈг ‰ ᆚⷕ˖䖲᥹ MySQL ⱘᆚⷕDŽ ‰ ⫼᠋ৡ˖䖲᥹ MySQL ⱘ⫼᠋ৡˈ՟བ rootDŽ ‰ ッষ˖MySQL ⱘッষˈ咬䅸Ў 3306DŽ ‰ 䖲᥹ IP˖䳔㽕䖲᥹ࠄⱘ MySQL ᳡ࡵ఼ IPDŽ ‰ 䖲᥹ৡ⿄˖䆹ৡ⿄೼ㅵ⧚఼ⱘᷥЁᰒ⼎ˈᑊϨ䆹ৡ⿄ϡৃҹ䞡໡DŽ ㋴བϟ˖ܗ೒ 13.6 Ёᮄᓎ䖲᥹ⱘ⬠䴶↨䕗ㅔऩˈ᱂䗮ⱘϔϾ㸼ऩˈ⬠䴶Ёࣙᣀⱘ ೒ 13.6 ᮄᓎ䖲᥹⬠䴶  /Download at http://www.pin5i.com ᬍবᅗӀ೒⠛DŽЎ FieldTable ࡴܹ哴ᷛⲥ਀఼DŽ Ḑᯊˈህ䳔㽕ܗ䆌ぎ੠Џ䬂ⱘ߫ऩܕ13.7 ᠔⼎ˈԚᰃ৺䳔㽕ᇍ FieldTable ࡴܹ哴ᷛџӊ໘⧚ˈᔧ⚍ߏњ Ḑ໘⧚㉏DŽ䞡ᮄ䖤㸠⿟ᑣᯊˈህৃҹⳟࠄᬜᵰབ೒ܗ䆌ぎ੠Џ䬂ⱘ߫ৢݡ䆒㕂ऩܕᕫࠄܜҹϞҷⷕ this.getColumn(PRIMARY_KEY).setCellRenderer(this.cellRenderer); this.getColumn(ALLOW_NULL).setCellRenderer(this.cellRenderer); ҹϟҷⷕेৃᅲ⦄ᰒ⼎೒⠛DŽ FieldTableIconCellRenderer ⱘᅲ⦄कߚㅔऩˈাᰃ߸ᮁऩḐⱘؐݡ䖯㸠໘⧚DŽ೼ FieldTable Փ⫼ } } return this; this.setHorizontalAlignment(CENTER); else this.setText((String)value); if (value instanceof Icon) this.setIcon((Icon)value); Ḑⱘؐ㉏ൟˈߚ߿䇗⫼ setIcon Ϣ setText ᮍ⊩ܗ //߸ᮁऩ boolean isSelected, boolean hasFocus, int row, int column) { public Component getTableCellRendererComponent(JTable table, Object value, public class FieldTableIconCellRenderer extends DefaultTableCellRenderer { ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\table\FieldTableIconCellRenderer.java Ḑ䖯㸠໘⧚DŽܗᄤ㉏ᴹᇍऩ Ӏ㓪ݭϔϾ FieldTable ㉏ᴹ㸼⼎ᄫ↉㸼ḐˈᑊЎ䖭Ͼ FieldTable ᦤկϔϾ DefaultTableCellRenderer ⱘ Ḑ䛑䳔㽕Փ⫼೒⠛ᴹᰒ⼎DŽ៥ܗḐˈ䖭ϸϾऩܗ䆌ぎ੠Џ䬂ⱘऩܕᄫ↉㸼Ḑ䳔㽕䖯㸠⡍߿໘⧚ⱘᰃ ᬍবˈ㞾ࡼ๲䭓ⱘ໮䗝Ḛг㽕䱣ⴔᬍবDŽ೼ᴀゴЁ㸼⬠䴶ᇍᑨⱘᰃ TableFrame ㉏DŽ ᄫ↉ᰃ৺㞾ࡼ๲䭓ⱘ໮䗝ḚDŽᔧ៥Ӏ೼㸼ḐЁ䗝Ёᶤ㸠᭄᥂˄ᄫ↉˅ⱘᯊ׭ˈ咬䅸ؐህ䳔㽕থ⫳Ⳍᑨⱘ 䆌ぎ੠Џ䬂ˈ೼䆹㸼Ḑϟ䴶ˈ᳝ϔϾ䕧ܹ咬䅸ؐⱘ᭛ᴀḚˈᑊᦤկϔϾ㸼⼎ܕࣙᣀᄫ↉ৡǃ㉏ൟǃᰃ৺ ⬠䴶བ೒ 13.7 ᠔⼎ˈ䆹⬠䴶䕗Ў໡ᴖˈߚ៤ϞϟϸϾ㸼ḐˈϞ䴶ⱘ㸼ḐЏ㽕໘⧚㸼ⱘᄫ↉ֵᙃˈ ೒ 13.7 ߯ᓎ㸼⬠䴶  /Download at http://www.pin5i.com Ḑ䛑ᰃϟᢝḚˈ಴ℸ៥Ӏ䳔㽕೼ ForeignTable Ё߯ܗ߫㸼ˈ໪䬂߫㸼᳝ 5 ߫ˈ㗠Ϩ↣ϔ߫Ёⱘ↣Ͼऩ Ḑ䛑䳔㽕Փ⫼ϟᢝḚᴹҷ᳓᱂䗮ⱘ᭛ᄫDŽϢᄫ↉߫㸼ϔḋˈᮄᓎϔϾ ForeignTable ⱘ㉏ᴹ㸼⼎ϔϾ໪䬂 ܗTableFrame ϟ䴶ⱘ໪䬂߫㸼Ϣᄫ↉߫㸼ϡৠⱘᰃˈ໪䬂߫㸼ϡ䳔㽕䖯㸠೒⠛໘⧚ˈԚᰃ↣Ͼऩ ܹϔ㸠᭄᥂DŽ ᮄᄫ↉Ϣᦦܹᄫ↉ⱘऎ߿ᰃˈᮄᄫ↉೼߫㸼ⱘ᳔ৢࡴܹϔ㸠᭄᥂ˈᦦܹᄫ↉೼⫼᠋᠔䗝ᢽⱘ㸠ⱘࠡ䴶ᦦ ᅲ⦄њᄫ↉߫㸼ৢˈ䖬䳔㽕⊼ᛣⱘᰃ䆹߫㸼ϟ䴶ⱘϝϾᣝ䪂ˈߚ߿ᰃᮄᄫ↉ǃᦦܹᄫ↉੠ߴ䰸ᄫ↉ˈ ៥Ӏ೼ϟ䴶ゴ㡖ᇚӮᅲ⦄DŽ ህᰒ⼎ϡৠ೒⠛ⱘᬜᵰDŽ঺໪ˈᔧ៥Ӏ⚍ߏњᶤ㸠᭄᥂˄ᄫ↉˅ⱘᯊ׭ˈ䖬䳔㽕໘⧚咬䅸ؐϢ㞾ࡼ๲䭓ˈ ḐܗḐⱘؐ˄೒⠛˅䖯㸠ׂᬍˈህৃҹ䖒ࠄ⚍ߏऩܗ䆌ぎ੠Џ䬂˅ˈݡᇍऩܕ˄䳔㽕䖯㸠೒⠛໘⧚ⱘ߫ selectAllowNullColumn ੠ selectPrimaryKeyColumn ᮍ⊩ˈ䖭ϸϾᮍ⊩Ё߸ᮁ⫼᠋᠔䗝ᢽⱘ߫ˈᰃ৺Ў Ḑⱘᮍ⊩ˈेҹϞⱘܗ䆌ぎ੠Џ䬂ऩܕⱘ selectCell ᮍ⊩ेৃˈselectCell ᮍ⊩ݡএ䇗⫼⚍ߏ া䳔㽕೼߯ᓎ FieldTableIconCellRenderer ⱘᯊ׭Ў㸼Ḑࡴܹ哴ᷛⲥ਀఼ˈ䆹ⲥ਀఼䇗⫼ҹϞҷⷕ } } } this.setValueAt(ImageUtil.PRIMARY_KEY_BLANK, row, column); } else { this.setValueAt(ImageUtil.PRIMARY_KEY, row, column); if (ImageUtil.PRIMARY_KEY_BLANK.equals(obj)) { Object obj = this.getValueAt(row, column); if (tc.getModelIndex() == column) { TableColumn tc = this.getColumn(PRIMARY_KEY); //ᕫࠄ䳔㽕᳈ᬍ೒⠛ⱘ߫˄Џ䬂߫˅ private void selectPrimaryKeyColumn(int row, int column) { //བᵰ哴ᷛ⚍ߏⱘ߫ᰃ"Џ䬂߫"ˈএᥝ៪㗙ࡴϞ೒ᷛ } } } this.setValueAt(ImageUtil.CHECKED_ICON, row, column); } else { this.setValueAt(ImageUtil.UN_CHECKED_ICON, row, column); if (ImageUtil.CHECKED_ICON.equals(currentIcon)) { 䆌ぎⱘ೒⠛ܕ //ḍ᥂ᔧࠡ䗝Ёⱘ೒⠛ᴹ᳈ᬍ Icon currentIcon = (Icon)this.getValueAt(row, column); if (tc.getModelIndex() == column) { TableColumn tc = this.getColumn(ALLOW_NULL); 䆌ぎ߫˅ܕ˄ //ᕫࠄ䳔㽕᳈ᬍ೒⠛ⱘ߫ private void selectAllowNullColumn(int row, int column) { 䆌ぎ߫ܕḐԡѢܗ //⚍ߏⱘऩ } selectPrimaryKeyColumn(row, column); selectAllowNullColumn(row, column); //ׂᬍ೒⠛߫ if (column == -1 || row == -1) return; int row = this.getSelectedRow(); int column = this.getSelectedColumn(); private void selectCell() { //哴ᷛ೼ JTable Ё⚍ߏⱘᯊ׭㾺থ䆹ᮍ⊩ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\table\FieldTable.java /Download at http://www.pin5i.com ViewFrameDŽ ೼ᴀゴЁˈ߯ᓎ㸼ⱘ⬠䴶ϔḋˈ᮴䆎ᮄ๲㾚೒៪㗙ׂᬍ㾚೒ˈ䛑Փ⫼ⳌৠⱘϔϾ⬠䴶ˈᇍᑨⱘᰃ ೒ 13.8 㾚೒⬠䴶  13.8 ᰃ㾚೒⬠䴶DŽ ⱘ CREATE VIEW ᴹ߯ᓎˈ䙷М⫼᠋ᶹⳟ㾚೒ⱘᯊ׭ˈϢᶹⳟ㸼ϔḋˈ䛑ᰃ䳔㽕ᠧᓔ᭄᥂⌣㾜⬠䴶DŽ೒ JTextArea ेৃˈᑊ䰘ᏺֱ᳝ᄬ᪡԰DŽ䖭䞠䳔㽕⊼ᛣⱘᰃˈ⫼᠋⚍ߏֱᄬⱘᯊ׭ˈ䳔㽕ᇚ㾚೒䗮䖛 SQL ᔧ⫼᠋䳔㽕㓪ݭϔϾ㾚೒ⱘᯊ׭ˈ៥ӀৃҹᦤկϔϾ㾚೒⬠䴶DŽ㾚೒⬠䴶ᅲ⦄कߚㅔऩˈা᳝ϔϾ 13.2.6 㾚೒⬠䴶 ໮ϔѯ乱໪ⱘ߸ᮁˈᴀゴЁᮄ๲㸼Ϣׂᬍ㸼ЎৠϔϾ⬠䴶˄TableFrame˅DŽخ䳔㽕 ᮄ๲Ϣׂᬍ᪡԰ⱘᯊ׭ˈህخϞⳳᅲⱘ᭄᥂DŽᮄ๲㸼Ϣׂᬍ㸼ⱘ⬠䴶ৃҹ݅⫼ϔϾ⬠䴶ˈԚᰃৠᯊ䳔㽕 ⬅Ѣ㔎Уⳳᅲⱘ᭄᥂ˈ಴ℸ៥Ӏৃҹᦤկϔѯ῵ᢳⱘ᭄᥂ᴹᅲ⦄ᬜᵰˈࠄ䳔㽕ᅲ⦄ⱘᯊ׭ˈህৃҹ᳓ᤶ Ḑ䛑ব៤ৃҹՓϟᢝᴹ䆒ᅮؐˈ៥Ӏ೼ᓔথ⬠䴶ⱘᯊ׭ˈܗᅠ䖭ѯᎹ԰ৢˈ໪䬂߫㸼Ё᠔᳝ⱘऩخ ;( this.getColumn(FIELD_NAME).setCellEditor(this.fieldNameEditor ᥹ϟᴹˈᕫࠄⳌᑨⱘ߫ˈݡ䆒㕂㓪䕥఼ᇍ䈵ेৃ˖ this.fieldNameEditor = new DefaultCellEditor(this.fieldNameComboBox); Ḑ㓪䕥఼ᇍ䈵ⱘᯊ׭ˈህߚ߿ҹ৘ϾϟᢝḚⱘᇍ䈵԰Ўᵘ䗴খ᭄˖ܗ䙷М೼߯ᓎ䖭ѯऩ private DefaultCellEditor onUpdateEditor; //㑻㘨᳈ᮄ private DefaultCellEditor onDeleteEditor; //㑻㘨ߴ䰸 private DefaultCellEditor referenceFieldEditor; //㑺ᴳᄫ↉ private DefaultCellEditor referenceTableEditor; //㑺ᴳ㸼 private DefaultCellEditor fieldNameEditor; //ᄫ↉ৡ⿄㓪䕥఼ᇍ䈵 ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\table\ForeignTable.java Ḑ㓪䕥఼ᇍ䈵ˈҹϟᰃ ForeignTable ⱘᅲ⦄DŽܗ5 Ͼऩ Ḑ㓪䕥఼ᇍ䈵DŽܗᓎ 5 ϾϟᢝḚ˄JComboBox˅ҹঞ 5 Ͼऩ /Download at http://www.pin5i.com ᭄᥂⬠䴶ˈे 13.2.3 ⱘ⬠䴶DŽᴀゴᇍᑨⱘᶹ䆶⬠䴶㉏ᰃ QueryFrameˈᶹ䆶⬠䴶བ೒ 13.10 ᠔⼎DŽ 䖛⿟៪㗙ߑ᭄ⱘ䇁হˈ䙷Мህ䳔㽕ᇚ㒧ᵰᰒ⼎ࠄټ䇁হˈ߭ৃҹⳈ᥹᪡԰DŽབᵰᠻ㸠ⱘᰃᶹ䆶ǃ䇗⫼ᄬ ֱᄬ SQL ⱘࡳ㛑ˈᠻ㸠 SQL ⱘᯊ׭ˈབᵰᰃ᱂䗮ⱘ INSERTǃUPDATE ៪㗙݊Ҫ᮴䳔⌣㾜᭄᥂ⱘ SQL ᔧ⫼᠋䳔㽕ᠻ㸠ϔѯ SQL ⱘᯊ׭ˈৃҹᦤկϔϾᶹ䆶⬠䴶䅽⫼᠋এ䕧ܹˈ䆹⬠䴶ᦤկᠻ㸠 SQL Ϣ 13.2.8 ᶹ䆶⬠䴶 䖛⿟䖬ᰃߑ᭄DŽټ‰ ㉏ൟϟᢝḚ˖ৃҹ䗝ᢽ㓪ݭⱘ㉏ൟˈᰃᄬ ⿟ˈ߭䆹 JTextField ϡৃ⫼DŽ 䖛ټ‰ 䖨ಲؐ JTextField˖ৃҹ䕧ܹߑ᭄ⱘ䖨ಲؐˈ಴Ўߑ᭄ᠡ᳝䖨ಲؐDŽབᵰ䗝ᢽⱘ㉏ൟЎᄬ 䖛⿟៪㗙ߑ᭄ⱘখ᭄DŽټ‰ খ᭄ JTextField˖䕧ܹᄬ 䖛⿟៪㗙ߑ᭄ⱘᮍ⊩ԧDŽټ‰ 䕧ܹᮍ⊩ԧⱘ JTextArea˖⫼᠋ৃҹ೼ℸ䕧ܹᄬ ㋴䇈ᯢ˖ܗ⬠䴶 䖛⿟⬠䴶ټ೒ 13.9 ᄬ  䖛⿟⬠䴶བ೒ 13.9 ᠔⼎DŽټ⬠䴶೼ᴀゴЁᇍᑨⱘ㉏ᰃ ProcedureFrameDŽᄬ ټ䖛⿟⬠䴶䅽⫼᠋এ᪡԰DŽᄬټ䖛⿟៪㗙ߑ᭄ⱘᯊ׭ˈৃҹᦤկϔϾᮄᓎᄬټ⫼᠋䳔㽕ᮄᓎϔϾᄬ 䖛⿟⬠䴶ټᄬ 13.2.7 /Download at http://www.pin5i.com ऩ˄JMenuItem˅ˈ᭄᥂ᑧ㡖⚍ⱘᅲ⦄ᮍᓣϢПⳌৠDŽ ᇍ䈵ᴹᄬᬒ৘Ͼ㦰ऩᔧ⚍ߏњ䖲᥹㡖⚍ⱘᯊ׭ JPopupMenu ߴ䰸᠔᳝ⱘᄤ㦰ऩˈݡࡴܹ䖲᥹㡖⚍ⱘ㦰 ᦤկᠧᓔⱘ㦰ऩˈᴀゴЁՓ⫼ JPopupMenu ᴹᅲ⦄哴ᷛে䬂㦰ऩˈMainFrame ЁᦤկϔϾ JPopupMenu ⬅Ѣ៥Ӏᇍ䖲᥹㡖⚍៪㗙᭄᥂ᑧ㡖⚍䖯㸠䗝ᢽⱘᯊ׭ˈህৃҹᠧᓔ䖲᥹៪㗙᭄᥂ᑧˈ಴ℸᑊϡ䳔㽕 ೒ 13.12 ᭄᥂ᑧ㡖⚍ে䬂㦰ऩ  ⚍ߏ᭄᥂ᑧ㡖⚍ⱘে䬂㦰ऩབ೒ 13.12 ᠔⼎DŽ ೒ 13.11 䖲᥹㡖⚍㦰ऩ  ⚍ߏ䖲᥹㡖⚍ⱘে䬂㦰ऩབ೒ 13.11 ᠔⼎DŽ ህৃҹᦤկ݇䯁᭄᥂ᑧ៪㗙ߴ䰸᭄᥂ᑧㄝে䬂㦰ऩDŽ ⱘ᪡԰ˈ՟བ⚍ߏњ䖲᥹㡖⚍ˈህৃҹᦤկ݇䯁䖲᥹ǃߴ䰸䖲᥹ㄝে䬂㦰ऩˈབᵰ⚍ߏњ᭄᥂ᑧ㡖⚍ˈ ೼Џ⬠䴶ⱘ䖲᥹ᷥЁˈᔧ៥Ӏ⚍ߏњᷥⱘᶤϾ㡖⚍ⱘᯊ׭ˈৃҹᦤկϔѯে䬂㦰ऩᴹᠻ㸠ϔѯⳌ݇ 13.2.9 ᷥ㡖⚍ে䬂㦰ऩ ೒ 13.10 ᶹ䆶⬠䴶  /Download at http://www.pin5i.com 䖛⿟哴ᷛে䬂㦰ऩ䇈ᯢ˖ټᄬ 䖛⿟᭄᥂㦰ऩټ೒ 13.15 ᄬ  䖛⿟哴ᷛে䬂㦰ऩབ೒ 13.15 ᠔⼎DŽټᄬ ‰ ߴ䰸㾚೒˖ߴ䰸᠔䗝ᢽⱘ㾚೒DŽ ‰ 㓪䕥㾚೒˖ׂᬍ᠔䗝ᢽⱘ㾚೒ˈϢᮄᓎ㾚೒Փ⫼ৠϔϾ⬠䴶DŽ ‰ ᮄᓎ㾚೒˖ᠧᓔ 13.2.6 Ёⱘ㾚೒⬠䴶ˈ⫼Ѣ߯ᓎ㾚೒DŽ 㾚೒᭄᥂哴ᷛে䬂㦰ऩ䇈ᯢ˖ ೒ 13.14 㾚೒᭄᥂㦰ऩ  㾚೒᭄᥂ⱘ哴ᷛে䬂㦰ऩབ೒ 13.14 ᠔⼎DŽ ‰ ᇐߎ㸼˖ᇚϔϾ㸼ⱘ᭄᥂ᇐߎDŽ ‰ ߴ䰸㸼˖ߴ䰸߫㸼Ё䗝ᢽ᭄᥂DŽ ‰ 㓪䕥㸼˖ׂᬍϔϾ㸼ˈϢᮄᓎ㸼Փ⫼ৠϔϾ⬠䴶DŽ ‰ ᮄᓎ㸼˖ᠧᓔ߯ᓎ㸼ⱘ⬠䴶ˈे 13.2.5 Ёⱘ⬠䴶DŽ 㸼᭄᥂哴ᷛে䬂㦰ऩ䇈ᯢ˖ ೒ 13.13 㸼᭄᥂㦰ऩ  ऩDŽ೒ 13.13 ᰃ᭄᥂߫㸼ᰒ⼎㸼᭄᥂ⱘᯊ׭ⱘে䬂㦰ऩDŽ 㡖⚍ⱘᯊ׭ˈ᭄᥂߫㸼Ёህᰒ⼎ϡৠⱘ᭄᥂ˈ៥Ӏৃҹḍ᥂ᔧࠡ᠔ᰒ⼎ⱘ᭄᥂ᴹ߯ᓎϡৠⱘ哴ᷛে䬂㦰 䖛⿟ټЏ⬠䴶Ё䰸њ䖲᥹ᷥ໪ˈ䖬᳝ϔϾ᭄᥂߫㸼ˈᔧ⫼᠋೼ᷥЁ⚍ߏњ㸼㡖⚍ǃ㾚೒㡖⚍៪㗙ᄬ ᭄᥂߫㸼ে䬂㦰ऩ 13.2.10 /Download at http://www.pin5i.com //ᡒϡࠄ MySQL ⱘᅝ㺙Ⳃᔩˈᦤ⼎ File file = new File(mysqlHome + MySQLUtil.MYSQL_HOME_BIN); //ᇏᡒ⫼᠋䗝ᢽⱘⳂᔩˈ߸ᮁᰃ৺ৃҹᡒࠄ MySQL ᅝ㺙Ⳃᔩϟⱘ bin Ⳃᔩ String mysqlHome = this.mysqlHomeField.getText(); //পᕫ⫼᠋䕧ܹؐ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\ConfigFrame.java 䖯㸠߸ᮁˈҹϟҷⷕЎ⚍ߏ⹂ᅮ᠔ᠻ㸠ⱘҷⷕDŽ ⫼᠋䗝ᢽⳂᔩৢˈህᇚ݊᠔䗝ⱘⳂᔩⱘ㒱ᇍ䏃ᕘᰒ⼎ࠄ JTextField Ёˈᔧ⚍ߏ⹂ᅮⱘᯊ׭ˈህৃҹ } super.approveSelection(); this.field.setText(this.getSelectedFile().getAbsolutePath()); //䆒㕂 JTextField ⱘؐ public void approveSelection() { //䞡ݭ JFileChooser ⱘᮍ⊩ } this.setFileSelectionMode(FileChooser.DIRECTORIES_ONLY); //䆒㕂াৃҹ䗝ᢽⳂᔩ this.field = field; public FileChooser(JTextField field) { private JTextField field; ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\ConfigFrame.java ҷⷕབϟDŽ ᔧ⫼᠋䗝ᢽњᇍᑨⱘⳂᔩৢˈህৃҹᇚ݊䗝ᢽⱘⳂᔩᰒ⼎ࠄ 13.2.1 ⬠䴶ⱘ JTextField ЁDŽ᭛ӊ䗝ᢽ఼ⱘ 䗝ᢽⳂᔩᅲ⦄कߚㅔऩˈা䳔㽕ᦤկϔϾ᭛ӊ䗝ᢽ఼ेৃˈ㗠Ϩ䖭Ͼ᭛ӊ䗝ᢽ఼াৃҹ䗝ᢽⳂᔩˈ 13.3.1 ᅲ⦄Ⳃᔩ䗝ᢽ њᅝ㺙Ⳃᔩৢˈ៥Ӏⱘ⿟ᑣህ䳔㽕ᇍ᠔䗝ᢽⳂᔩ䖯㸠偠䆕ˈ߸ᮁ㛑৺ᡒࠄ bin ⳂᔩDŽ ᔩᰃϔϾᖙ㽕ⱘ᪡԰ˈᕫࠄ MySQL ᅝ㺙Ⳃᔩৢˈ៥Ӏህৃҹᡒࠄ bin Ⳃᔩϟ䴶ⱘੑҸDŽ಴ℸ⫼᠋䗝ᢽ ᢽᴀഄⱘ MySQL ᅝ㺙Ⳃᔩˈ⬅Ѣ៥Ӏ䳔㽕Փ⫼ MySQL ⱘϔѯݙ㕂ੑҸˈ಴ℸ䗝ᢽ MySQL ⱘᅝ㺙Ⳃ ᅲ⦄ MySQL ᅝ㺙Ⳃᔩ䗝ᢽࡳ㛑ˈ៥ӀՓ⫼ 13.2.1 ⱘ⬠䴶DŽᔧ⫼᠋䖯ܹㅵ⧚఼ⱘᯊ׭ˈህ䅽⫼᠋䗝 13.3 ᅲ⦄MySQLᅝ㺙Ⳃᔩ䗝ᢽࡳ㛑 ࠄℸˈㅵ⧚఼ⱘ᠔᳝⬠䴶䛑߯ᓎᅠ↩ˈ᥹ϟᴹህৃҹᅲ⦄Ⳍ݇ⱘࡳ㛑DŽ MainFrame Ёⱘ ViewObject ेৃDŽ ㉏ˈ䆺㒚䇋ⳟ 13.2.2 Ёⱘ৘Ͼ⬠䴶ᇍ䈵DŽᔧ⫼᠋⚍ߏњᎹ݋ᷣ៪㗙ᷥϞⱘᶤϾ㡖⚍ᯊˈህⳌᑨⱘᬍব 䖛⿟᭄᥂ᰃ ProcedureDataټ䳔㽕ᅲ⦄ⱘ᥹ষˈ㸼᭄᥂ᰃ TableData ㉏ˈ㾚೒᭄᥂ᰃ ViewData ㉏ˈᄬ ㋴䛑ܗ㽕೼ MainFrame ЁᦤկϔϾ ViewObject ⱘ㉏ᴹᷛ䆚ᔧࠡᰒ⼎ⱘ㉏ൟˈViewObject ᰃ᠔᳝⬠䴶 ҹϞⱘ㦰ऩഛ೼Џ⬠䴶˄MainFrame˅Ё߯ᓎˈ⿟ᑣᑊϡⶹ䘧ᔧࠡᰒ⼎ⱘᰃા⾡᭄᥂ˈ಴ℸ៥Ӏ䳔 ᑨⱘএߴ䰸 JPopupMenu ᠔᳝ⱘᄤ㦰ऩˈݡ⏏ࡴⳌᑨⱘ㦰ऩ˄JMenuItem˅ेৃDŽ ҹϞЎϝ⾡᭄᥂ⱘে䬂㦰ऩˈᅲ⦄ᮍᓣϢᷥ㡖⚍ⱘে䬂㦰ऩϔḋˈᔧ⬠䴶ⱘ᭄᥂থ⫳ᬍবᯊˈህⳌ 䖛⿟៪㗙ߑ᭄DŽټ䖛⿟˖ߴ䰸᠔䗝ᢽⱘᄬټ‰ ߴ䰸ᄬ 䖛⿟Փ⫼Ⳍৠⱘ⬠䴶DŽټ䖛⿟ˈϢᮄᓎᄬټ䖛⿟˖ׂᬍ䗝ᢽⱘᄬټ‰ 㓪䕥ᄬ 䖛⿟DŽټ䖛⿟⬠䴶ˈ߯ᓎᄬټ䖛⿟˖ᠧᓔ 13.2.7 Ёⱘᄬټᮄᓎᄬ ‰ /Download at http://www.pin5i.com ϔѯ᭄᥂ᑧ䖲᥹ⱘֵᙃˈ䆺㒚䇋ⳟ 13.2.2 Ёⱘ ServerConnection ㉏DŽҹϟҷⷕ䇏পϔӑ propertiesˈ ᥹䜡㕂᭛ӊⱘᮍ⊩ˈᑊ䖨ಲ ServerConnection 䲚ড়ˈServerConnection ҷ㸼ϔϾ䖲᥹㡖⚍ˈᑊֱᄬ᳝ ᮄᓎϔϾ PropertiesHandler ⱘ᥹ষˈϧ䮼⫼Ѣ໘⧚䖲᥹ሲᗻ᭛ӊDŽ䆹᥹ষᦤկϔϾ䇏প᭄᥂ᑧ䖲 ֱᄬ䖲᥹ⱘֵᙃ៥Ӏ೼ϟ䴶ⱘゴ㡖Ёᅲ⦄ˈ䖭䞠Џ㽕ᅲ⦄䇏পⱘᅲ⦄DŽ MySQL ᅝ㺙Ⳃᔩϔḋˈৃҹᦤկϔѯ properties ᭛ӊᴹֱᄬˈ↣ϔϾ䖲᥹԰Ўϔӑ properties ᭛ӊDŽ Џ⬠䴶Ꮊ䖍ⱘᷥˈㅵ⧚఼ᰃ䩜ᇍ MySQL ᭄᥂ᑧⱘˈԚᰃ䖭ѯ䖲᥹ֵᙃৃҹϡ䆄ᔩࠄ᭄᥂ᑧˈϢֱᄬ ೼ᕫࠄ MySQL ᅝ㺙Ⳃᔩˈ䖯ܹЏ⬠䴶ᯊˈ䖬䳔㽕ᕫࠄ⫼᠋᠔᳝ⱘ䖲᥹ֵᙃˈ䖭ѯֵᙃ⫼ᴹ߱ྟ࣪ 13.3.3 䇏প䖲᥹ֵᙃ এׂᬍ䜡㕂᭛ӊDŽ ⱘ䆱ˈህϡ䳔㽕↣ϔ⃵䖯ܹ㋏㒳䛑خ߭䳔㽕䞡ᮄᇚᮄⱘⳂᔩֵᙃֱᄬࠄ mysql.properties ᭛ӊЁDŽ䖭ѯ ⊼ᛣҹϞҷⷕⱘ߸ᮁˈབᵰ⫼᠋ࠡϔ⃵᠔䗝ᢽⱘ MySQL ᅝ㺙ⳂᔩϢ䖭ϔ⃵᠔䗝ᢽⱘⳂᔩϡϔ㟈ˈ this.setVisible(false); this.mainFrame.setVisible(true); this.mainFrame = new MainFrame(ctx); GlobalContext ctx = new GlobalContext(mysqlHome); } FileUtil.saveMysqlHome(this.mysqlHomeField.getText()); if (!mysqlHome.equals(FileUtil.getMySQLHome())) { //བᵰ䜡㕂᭛ӊⱘؐϢ⫼᠋䕧ܹⱘؐϡⳌㄝˈ߭䞡ᮄݭܹ䜡㕂᭛ӊЁ //ⳕ⬹݊Ҫҷⷕ... ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\ConfigFrame.java ⱘϔѯֵᙃˈ՟བ䖭䞠ⱘ mysql.home ሲᗻDŽҹϟҷⷕᅲ⦄ֱᄬ䜡㕂ⱘࡳ㛑DŽ ᥹ϟᴹᅲ⦄ֱᄬᅝ㺙Ⳃᔩⱘࡳ㛑ˈ೼䖭ПࠡˈᮄᓎϔϾ GlobalContext ⱘ㉏ˈ⫼Ѣֱᄬㅵ⧚఼ܼሔ ⫼ҹϞⱘᮍ⊩এ㦋ᕫ MySQL ᅝ㺙ⳂᔩⱘؐDŽ ᭛ӊৢˈህ䇏পᅗⱘ mysql.home ሲᗻDŽ䙷М⫼᠋೼䖯ܹ MySQL ᅝ㺙Ⳃᔩ䗝ᢽ⬠䴶ⱘᯊ׭ˈህৃҹ䇗 ҹϞҷⷕЁⱘ MYSQL_PROPERTIES_FILE ህᰃ mysql.properties 䜡㕂᭛ӊⱘⳌᇍ䏃ᕘˈᡒࠄ䆹 } return props.getProperty(MYSQL_HOME); Properties props = getProperties(configFile); File configFile = new File(MYSQL_PROPERTIES_FILE); public static String getMySQLHome() { //䖨ಲ䜡㕂᭛ӊⱘ MYSQL_HOME 䜡㕂 ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\util\FileUtil.java ⱘ䇏পҷⷕDŽ 䆱ˈህৃҹϡᖙ↣ϔ⃵䛑এ䖯㸠Ⳃᔩ䗝ᢽDŽᦤկϔӑ mysql.properties ⱘ䜡㕂᭛ӊˈҹϟЎ䆹䜡㕂᭛ӊ ⱘخ⫼᠋䗝ᢽњ MySQL ⱘᅝ㺙Ⳃᔩৢˈ៥Ӏ䳔㽕ᇚⳂᔩⱘ㒱ᇍ䏃ᕘֱᄬࠄϔӑ䜡㕂᭛ӊЁˈ䖭ḋ 13.3.2 䇏প੠ֱᄬᅝ㺙Ⳃᔩ䏃ᕘ ㅵ⧚఼ⱘ䜡㕂᭛ӊDŽ ߭㸼⼎⫼᠋᠔䗝ᢽⱘⳂᔩᰃ䫭䇃ⱘˈᔍߎᦤ⼎ᑊ䖨ಲDŽབᵰ⫼᠋䗝ᢽⱘⳂᔩᰃℷ⹂ⱘ䆱ˈህ䳔㽕এ䇏প ҹϞҷⷕⱘ咥ԧ䚼ߚˈ䳔㽕এ߸ᮁ MySQL ᅝ㺙Ⳃᔩϟⱘ bin Ⳃᔩᰃ৺ᄬ೼ˈབᵰ≵᳝ᄬ䆹Ⳃᔩˈ } return; showMessage("䇋䗝ᢽℷ⹂ MySQL ᅝ㺙Ⳃᔩ", "䫭䇃"); if (!file.exists()) { /Download at http://www.pin5i.com for (ServerConnection conn : conns) ctx.addConnection(conn); List conns = ctx.getPropertiesHandler().getServerConnections(); //䇏পܼ䚼ⱘ᳡ࡵ఼䖲᥹䜡㕂 ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\ConfigFrame.java 㺙Ⳃᔩˈ⚍ߏ⹂ᅮৢˈህৃҹᇚ䖲᥹ࡴܹࠄ GlobalContext Ёˈ⫼᠋⚍ߏ⹂ᅮᣝ䪂ᠻ㸠ⱘ䚼ߚҷⷕDŽ 䆌ߎ⦄䞡ৡⱘ䖲᥹DŽ䙷М೼⫼᠋䗝ᢽ MySQL ᅝܕᅮњ೼ㅵ⧚఼Ёϡއ䖲᥹ⱘৡ⿄԰Ў key ⱘˈ᠔ҹህ ೼ GlobalContext ЁᓎゟϔϾ Map ᴹֱᄬ䖭ѯ䖲᥹ֵᙃˈᑊᦤկ add ᮍ⊩ˈ⬅Ѣ䖭Ͼ Map ᰃՓ⫼ } this.connections.put(connection.getConnectionName(), connection); public void addConnection(ServerConnection connection) { //⏏ࡴϔϾ䖲᥹ࠄ Map Ё private Map connections = new HashMap(); //ᄬᬒ᠔᳝᳡ࡵ఼䖲᥹ⱘ䲚ড় ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\object\GlobalContext.java ⱘϞϟ᭛ˈৃҹᇚ䖭ѯ䖲᥹ֵᙃᬒࠄ䆹㉏ЁDŽ ⦄Ёˈ䖭ѯ㉏៪㗙䖲᥹ֵᙃ䳔㽕㒣ᐌՓ⫼ࠄDŽ೼ 13.3.2 ЁᦤկњϔϾ GlobalContext ⱘ㉏ᴹ㸼⼎ㅵ⧚఼ ϡ䳔㽕߱ྟ࣪ᷥˈ䳔㽕ᇚ䖭ѯֵᙃᄬᬒࠄϔϾᇍ䈵Ёˈ಴Ў೼ϟ䴶ⱘᅲܜˈᕫࠄ᠔᳝ⱘ䖲᥹ֵᙃৢ } return conn; prop.getProperty(FileUtil.PORT)); prop.getProperty(FileUtil.HOST), prop.getProperty(FileUtil.PASSWORD), prop.getProperty(FileUtil.USERNAME), ServerConnection conn = new ServerConnection(FileUtil.getFileName(file), Properties prop = FileUtil.getProperties(file); private ServerConnection createServerConnection(File file) { //ᇚϔӑ properties ᭛ӊᇕ㺙៤ ServerConnection ᇍ䈵 } return result; } result.add(conn); ServerConnection conn = createServerConnection(file); for (File file : propertyFiles) { List result = new ArrayList(); File[] propertyFiles = getPropertyFiles(); public List getServerConnections() { //ᕫࠄ᠔᳝ⱘ䖲᥹ֵᙃ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\system\PropertiesHandlerImpl.java 䙷М೼ PropertiesHandler ᅲ⦄㉏Ёˈህৃҹ䇏পⳌᑨⳂᔩϟⱘ᠔᳝ properties ᭛ӊDŽ } return prop; fis.close(); prop.load(fis); FileInputStream fis = new FileInputStream(propertyFile); Properties prop = new Properties(); public static Properties getProperties(File propertyFile) throws IOException { //ḍ᥂᭛ӊᕫࠄᇍᑨⱘ properties ᭛ӊ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\util\FileUtil.java ᑊ䖨ಲϔϾ Properties ᇍ䈵DŽ /Download at http://www.pin5i.com } this.tree = tree; tree.add(this.treeMenu); //⏏ࡴে䬂㦰ऩ tree.setRootVisible(false); tree.addMouseListener(new TreeListener(this)); //䆒㕂ⲥ਀఼㉏ tree.setCellRenderer(cr); TreeCellRenderer cr = new TreeCellRenderer(); //䆒㕂㡖⚍໘⧚㉏ JTree tree = new JTree(this.treeModel); //ᵘ䗴ᷥ this.treeModel = new DefaultTreeModel(root); createNodes(root); //߯ᓎ䖲᥹㡖⚍ DefaultMutableTreeNode root = new DefaultMutableTreeNode(new RootNode()); private void createTree() { //߯ᓎᷥ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\MainFrame.java MainFrame Ё߯ᓎᷥⱘᮍ⊩DŽ } } root.add(conntionNode); DefaultMutableTreeNode conntionNode = new DefaultMutableTreeNode(conn); //߯ᓎ䖲᥹㡖⚍ ServerConnection conn = conns.get(key); for (String key : conns.keySet()) { Map conns = this.ctx.getConnections(); private void createNodes(DefaultMutableTreeNode root) { //߯ᓎᷥЁ᳡ࡵ఼䖲᥹ⱘ㡖⚍ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\MainFrame.java MainFrame Ё߯ᓎ䖲᥹㡖⚍ⱘᮍ⊩DŽ 䯂䆹䖲᥹ϟ䴶ⱘ᭄᥂ᑧֵᙃˈℷᐌᕫࠄ䖭ѯ᭄᥂ᑧৢˈݡ߯ᓎ᭄᥂ᑧ㡖⚍DŽ ㄀ϔሖ㡖⚍ˈ㗠ϡ䳔㽕ݡএ߯ᓎϟ䴶ⱘ޴ሖ㡖⚍ˈᔧ⫼᠋⚍ߏ㄀ϔሖ㡖⚍˄䖲᥹㡖⚍˅ⱘᯊ׭ˈݡএ䆓 ҹḍ᥂䖭ѯ䖲᥹ֵᙃᴹ߯ᓎᷥDŽ೼߯ᓎᷥⱘᯊ׭ˈ䳔㽕⊼ᛣⱘᰃˈ៥Ӏা䳔㽕ḍ᥂䖭ѯ䖲᥹ֵᙃᴹ߯ᓎ 䖯ܹЏ⬠䴶ᯊˈ៥ӀᏆ㒣ৃҹᕫࠄ GlobalContext ᇍ䈵ˈ৘Ͼ䖲᥹ֵᙃ䛑ֱᄬ೼䆹ᇍ䈵Ёˈ಴ℸৃ 13.4.1 ߯ᓎ䖲᥹㡖⚍ ेৃDŽᴀᇣ㡖ᇚᅲ⦄䖲᥹Ⳍ݇ⱘࡳ㛑ˈ䖭ѯࡳ㛑ࣙᣀ߯ᓎ䖲᥹㡖⚍ǃᠧᓔ䖲᥹ǃߴ䰸䖲᥹ㄝDŽ ݇DŽᷥⱘ㄀ϔሖ㡖⚍ᰃㅵ⧚఼Ёⱘ৘Ͼ䖲᥹ˈা䳔㽕ᕫࠄ৘Ͼ䖲᥹ৢˈҹ䖭ѯ䖲᥹ᇍ䈵߯ᓎ㄀ϔሖ㡖⚍ 䖯ܹЏ⬠䴶ৢˈ៥Ӏ䳔㽕ᇚ৘Ͼ䖲᥹ֵᙃ߯ᓎϔỉᷥˈ⫼᠋ᕔৢⱘ৘Ͼ᪡԰ˈ䛑Ϣ䖭ѯỉᷥᙃᙃⳌ 13.4 䖲᥹ㅵ⧚ ߯ᓎᷥⱘࡳ㛑DŽ ࠄℸˈMySQL ᅝ㺙Ⳃᔩⱘࡳ㛑Ꮖ㒣ᅲ⦄ˈᕫࠄ⫼᠋ⱘ৘Ͼ䖲᥹ֵᙃৢˈህৃҹḍ᥂䖭ѯ䖲᥹ᅲ⦄ /Download at http://www.pin5i.com //߸ᮁ⚍ߏ㡖⚍ⱘ㉏ൟ if (selectNode == null) return; DefaultMutableTreeNode selectNode = getSelectNode(); //㦋ᕫ䗝Ёⱘ㡖⚍ public void viewTreeDatas() { //⚍ߏᷥ㡖⚍ⱘ᪡԰ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\MainFrame.java } } this.mainFrame.viewTreeDatas(); //Ꮊ䬂⚍ߏˈᶹⳟᷥⱘ㡖⚍ˈ䇗⫼ MainFrame ⱘᠧᓔ㡖⚍ᮍ⊩ if (e.getModifiers() == MouseEvent.BUTTON1_MASK) { public void mousePressed(MouseEvent e) { ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\tree\TreeListener.java াᰃ䖲᥹㡖⚍㹿⚍ߏⱘᯊ׭ᠡএ䖯㸠䖲᥹DŽ ⚍ⲥ਀఼ˈᔧ㡖⚍㹿䗝Ёৢˈህৃҹᠻ㸠 connect ᮍ⊩ˈԚᰃ䳔㽕⊼ᛣⱘᰃˈᑊϡᰃ↣Ͼ㡖⚍䛑Ⳍৠˈ ߭䖯㸠߯ᓎDŽ⊼ᛣ createConnection ᮍ⊩ˈ䆹ᮍ⊩ໄᯢЎ publicˈৃҹ䅽໪䚼এՓ⫼DŽϟ䴶ᅲ⦄ᷥⱘ㡖 ߸ᮁ ServerConnection ⱘ connection ሲᗻᰃ৺Ўぎˈབᵰ䆹ሲᗻЎぎ˄≵᳝䖲᥹˅ܜҹϞⱘҷⷕЁ } return conn; this.username, this.password); Connection conn = DriverManager.getConnection(getConnectUrl() + database, Class.forName(DRIVER); public Connection createConnection(String database) throws Exception { //߯ᓎ䖲᥹, খ᭄ᰃ᭄᥂ᑧৡ⿄ } return super.connection; super.connection = conn; Connection conn = createConnection(""); Class.forName(DRIVER); if (super.connection != null) return super.connection; //Connection ೼ᴀ㉏Ёা᳝ϔϾᅲ՟ public Connection connect() { //ᅲ⦄⠊㉏ⱘᮍ⊩ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\object\tree\ServerConnection.java ᮍ⊩ⱘᅲ⦄DŽ ˄ConnectionNode˅ⱘ connect ᮍ⊩ˈϟ䴶ҷⷕᰃ ServerConnection ᇍ ConnectionNode ⱘ connect ᰃ߸ᮁ ConnectionNode ⱘ Connection ᇍ䈵ᰃ৺Ўぎˈ㗠Ϩ ServerConnection Ё䳔㽕ᅲ⦄⠊㉏ ޚConnectionNode ЁֱᄬњϔϾ JDBC ⱘ Connection ᇍ䈵ˈServerConnection Ё߸ᮁᰃ৺䖲᥹ⱘᷛ ᥂ᑧ㡖⚍˅ˈབᵰϡ㛑៤ࡳ䖲᥹ˈ߭ᔍߎᦤ⼎DŽServerConnection 㒻ᡓњ ConnectionNode 䖭Ͼᢑ䈵㉏ˈ 䖯㸠᳡ࡵ఼䖲᥹ˈՓ⫼ JDBC 䖯㸠䖲᥹ेৃDŽབᵰ៤ࡳ䖯㸠䖲᥹ˈህ偀Ϟ߯ᓎ䆹䖲᥹㡖⚍ⱘᄤ㡖⚍˄᭄ ᄬ䖲᥹ⱘֵᙃˈᑊՓ⫼䆹ᇍ䈵ᴹ߯ᓎᷥЁⱘ䖲᥹㡖⚍ˈᠧᓔ䖲᥹ⱘᯊ׭ˈህৃҹḍ᥂䖲᥹ⱘֵᙃএᇱ䆩 ᔧ⫼᠋⚍ߏ䖲᥹㡖⚍ৢˈህ䳔㽕ゟेᠧᓔ䖭Ͼ䖲᥹ˈ៥ӀՓ⫼њϔϾ ServerConnection ᇍ䈵ᴹֱ 13.4.2 ᠧᓔ䖲᥹ ϟ䴶ⱘゴ㡖Ёᅲ⦄DŽ ⱘᅲ⦄DŽTreeListener ᰃϔϾ哴ᷛџӊⲥ਀఼ˈᔧ⫼᠋⚍ߏњᷥⱘ䖲᥹㡖⚍ৢˈ䳔㽕ᓎゟ䖲᥹ˈ៥Ӏ೼ ҹϞҷⷕЁⱘ TreeCellRenderer Ў㡖⚍ⱘ໘⧚㉏ˈ݋ԧⱘᅲ⦄䇋ⳟ 13.2.2 Ё TreeCellRenderer ㉏ /Download at http://www.pin5i.com } buildServerChild(server, selectNode); //߯ᓎ᳡ࡵ఼ᄤ㡖⚍ validateConnect(selectNode, server); //偠䆕ᰃ৺ৃҹ䖯㸠䖲᥹ ServerConnection server = (ServerConnection)selectNode.getUserObject(); public void clickServerNode(DefaultMutableTreeNode selectNode) { //⚍ߏ᳡ࡵ఼㡖⚍ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\MainFrame.java ⊩DŽ ҷ㸼ϔϾ᭄᥂ᑧ㡖⚍DŽ䙷М⦄೼ህৃҹᅲ⦄ clickServerNode ᮍ⊩ˈ⚍ߏњ䖲᥹㡖⚍ৢˈህӮᠻ㸠䆹ᮍ ᇚ Database ߫ⱘؐᇕ㺙៤ϔϾ Database ᇍ䈵ˈࡴܹࠄ㒧ᵰ䲚ЁेৃDŽ೼ᴀゴЁˈϔϾ Database ᇍ䈵 ᇍ䈵ेৃˈᑊϡ䳔㽕ݡএ䞡ᮄ߯ᓎ JDBC ⱘ Connection ᇍ䈵DŽᶹ䆶ࠄ᭄᥂ᑧⱘ ResultSet ᇍ䈵ৢˈህ ᰃˈ៥Ӏ䳔㽕ᕫࠄ Statement ᇍ䈵ˈⳈ᥹Փ⫼ ConnectionNode ⱘ connection ሲᗻএ߯ᓎ Statement Փ⫼ Statement ᠻ㸠 show databases ህৃҹᕫࠄ᠔᳝ⱘ᭄᥂ᑧ ResultSet ᇍ䈵ˈ䖭䞠䳔㽕⊼ᛣⱘ } return stmt.executeQuery(sql); Statement stmt = getStatement(); public ResultSet query(String sql) throws Exception { //ᶹ䆶ᑊ䖨ಲ ResultSet ᇍ䈵 } } return result; } catch (Exception e) { return result; rs.close(); } result.add(db); Database db = new Database(databaseName, this); String databaseName = rs.getString("Database"); while (rs.next()) { ResultSet rs = query("show databases"); //㦋ᕫϔϾ䖲᥹ϟ䴶᠔᳝ⱘ᭄᥂ᑧ try { List result = new ArrayList(); public List getDatabases() { //㦋ᕫϔϾ᳡ࡵ఼䖲᥹ϟ䴶᠔᳝ⱘ᭄᥂ᑧ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\object\tree\ServerConnection.java 䴶᠔᳝ⱘ᭄᥂ᑧˈᠻ㸠 MySQL ⱘϔহ show databases ህৃҹᕫࠄ᠔᳝ⱘ᭄᥂ᑧDŽ ৺ৃҹ䖯㸠䖲᥹ˈݡ߯ᓎ䆹䖲᥹㡖⚍ⱘᄤ㡖⚍˄᭄᥂ᑧ㡖⚍˅DŽ㽕߯ᓎ᭄᥂ᑧ㡖⚍ˈህ㽕ᕫࠄ䆹䖲᥹ϟ এ偠䆕㹿䗝Ёⱘ㡖⚍ᰃܜⱘᰃخ䖲᥹㡖⚍㹿⚍ߏৢˈህӮᠻ㸠 clickServerNode ᮍ⊩ˈ䆹ᮍ⊩䳔㽕 } //᱖ᯊϡᅲ⦄ public void clickServerNode(DefaultMutableTreeNode selectNode) { //⚍ߏ᳡ࡵ఼㡖⚍ } } clickServerNode(selectNode);//᳡ࡵ఼䖲᥹㡖⚍ if (selectNode.getUserObject() instanceof ServerConnection) { /Download at http://www.pin5i.com ᇍ䖭ѯ䖲᥹䖯㸠ֱᄬˈҹϟЎ⚍ߏ⹂ᅮᠻ㸠ⱘᮍ⊩DŽ ᭛ӊЁेৃDŽ䙷М೼ ConnectionFrame˄ᮄᓎ䖲᥹⬠䴶˅Ёˈᔧ⫼᠋䕧ܹ৘Ͼֵᙃ⚍ߏ⹂ᅮৢˈህৃҹ saveServerConnection ᅲ⦄ㅔऩˈা䳔㽕ᇚ ServerConnection ᇍ䈵Ёⱘ৘Ͼሲᗻݭࠄ properties } conn.getConnectionName() + " config."); FileUtil.saveProperties(connConfigFile, props, "Connection " + //ᇚሲᗻݭܹ䜡㕂᭛ӊ props.setProperty(FileUtil.PASSWORD, conn.getPassword()); props.setProperty(FileUtil.USERNAME, conn.getUsername()); props.setProperty(FileUtil.PORT, conn.getPort()); props.setProperty(FileUtil.HOST, conn.getHost()); Properties props = new Properties(); FileUtil.createNewFile(connConfigFile); //߯ᓎ᭛ӊ File connConfigFile = new File(configFileName); //߯ᓎ properties ᭛ӊ conn.getConnectionName() + ".properties"; String configFileName = FileUtil.CONNECTIONS_FOLDER + //ᕫࠄ䜡㕂᭛ӊৡ, 䖭ѯ properties ᭛ӊᄬᬒѢ connections Ⳃᔩϟ public void saveServerConnection(ServerConnection conn) { ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\system\PropertiesHandlerImpl.java PropertiesHandler ᅲ⦄㉏ᇍ saveServerConnection ⱘᅲ⦄DŽ ЁᏆ㒣߯ᓎDŽ ϔϾ saveServerConnection ⱘᮍ⊩ˈPropertiesHandler ᰃ⫼Ѣ໘⧚ properties ᭛ӊⱘ᥹ষˈ೼ 13.3.3 ֱᄬࠄϔӑ properties ᭛ӊЁˈݡ৥ᷥЁ⏏ࡴϔϾ䖲᥹㡖⚍ेৃDŽ៥ӀЎ᥹ষ PropertiesHandler ⏏ࡴ ೼ 13.2.4 Ёˈ៥ӀᏆ㒣ᦤկњϔϾ߯ᓎ䖲᥹ⱘ⬠䴶ˈᅲ⦄ᮄᓎ䖲᥹ˈা䳔㽕ᇚ⫼᠋䕧ܹⱘ䖲᥹ֵᙃ 13.4.3 ᮄᓎ䖲᥹ ᥹੠߯ᓎ᭄᥂ᑧ㡖⚍DŽ ⱘᰃ˖偠䆕䖲خᠧᓔ䖲᥹ⱘࡳ㛑Ꮖ㒣ᅲ⦄ˈৃҹ䖤㸠⿟ᑣᶹⳟᬜᵰDŽᘏⱘᴹ䇈ˈᠧᓔϔϾ䖲᥹䳔㽕 } node.connect(); //䖯㸠䖲᥹ private void validateConnect(DefaultMutableTreeNode selectNode, ConnectionNode node) { //߸ᮁ䖲᥹ᰃ৺ߎ䫭ˈ䗖⫼Ѣ᳡ࡵ఼㡖⚍੠᭄᥂ᑧ㡖⚍ } } this.treeModel.insertNodeInto(databaseNode, conntionNode, conntionNode.getChildCount()); //ᇚ᭄᥂ᑧ㡖⚍ࡴܹࠄ䖲᥹㡖⚍Ё DefaultMutableTreeNode databaseNode = new DefaultMutableTreeNode(database); for (Database database : databases) { //ݡ߯ᓎ䖲᥹㡖⚍ϟ䴶ⱘ᭄᥂㡖⚍ List databases = server.getDatabases(); if (conntionNode.getChildCount() != 0) return; //བᵰ᳝ᄤ㡖⚍ˈ߭ϡݡ߯ᓎ DefaultMutableTreeNode conntionNode) { public void buildServerChild(ServerConnection server, ߯ᓎ᭄᥂ᑧϔሖⱘ㡖⚍˄ᷥⱘ㄀Ѡሖ˅// /Download at http://www.pin5i.com ᄬᯊ׭ⱘ ServerConnection ᰃϸϾᇍ䈵DŽ ׭ˈ៥ӀӮݡএҢ⬠䴶㦋পϔϾ ServerConnection ᇍ䈵ˈ಴ℸ⌟䆩䖲᥹ⱘ ServerConnection ᇍ䈵Ϣֱ Ϣᠧᓔ䖲᥹ϔḋˈ䛑ᰃՓ⫼ ServerConnection ⱘ connect ᮍ⊩䖯㸠䖲᥹ˈݡᤩ㦋ᓖᐌDŽֱᄬⱘᯊ } } showMessage(e.getMessage(), "䄺ਞ"); } catch (Exception e) { showMessage("៤ࡳ䖲᥹", "៤ࡳ"); conn.connect(); try { ServerConnection conn = getDataConnectionFromView(); //Ң⬠䴶Ёᕫࠄ䖲᥹ֵᙃ private void checkConnection() { //⌟䆩䖲᥹ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\ConnectionFrame.java 䆩䖲᥹ᣝ䪂㾺থⱘᮍ⊩DŽ 䆩ϔϟ᳡ࡵ఼ᰃ৺ৃҹ䖲᥹DŽҹϟᰃ⚍ߏ⌟⌟ܜ⬠䴶Ё䖬᳝ϔϾ⌟䆩䖲᥹ⱘࡳ㛑ˈ೼⏏ࡴ䖲᥹ࠡˈৃҹ ServerConnection 㡖⚍DŽ䖤㸠⿟ᑣᑊ䖯㸠⏏ࡴϔϾ䖲᥹ˈৃҹⳟࠄ݋ԧⱘᬜᵰDŽ䰸њ⏏ࡴ䖲᥹ⱘࡳ㛑໪ˈ addConnection ᮍ⊩Ⳉ᥹Փ⫼ DefaultTreeModel ⱘ insertNodeInto ᮍ⊩৥ᷥ⏏ࡴϔϾ } if (root.getChildCount() == 1) this.tree.updateUI(); this.treeModel.insertNodeInto(newChild, root, root.getChildCount()); //৥㽕㡖⚍⏏ࡴ䖲᥹㡖⚍ DefaultMutableTreeNode newChild = new DefaultMutableTreeNode(sc); DefaultMutableTreeNode root = (DefaultMutableTreeNode)this.treeModel.getRoot(); //ᕫࠄ㽕㡖⚍ public void addConnection(ServerConnection sc) { //೼⏏ࡴ䖲᥹⬠䴶⏏ࡴњϔϾ䖲᥹ৢᠻ㸠ⱘᮍ⊩, ৥ᷥЁ⏏ࡴϔϾ䖲᥹ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\MainFrame.java MainFrame ⱘ addConnection ᮍ⊩ˈҹϟᰃ addConnection ᮍ⊩ⱘᅲ⦄DŽ ServerConnection ᇍ䈵ˈབᵰ㛑ᕫࠄˈ߭㸼⼎Ꮖ㒣ᄬ೼Ⳍৠৡᄫⱘ䖲᥹DŽֱᄬࠄሲᗻ᭛ӊৢˈህ䇗⫼ এ GlobalContext ⱘ䖲᥹ Map Ё㦋পܜˈ⊼ᛣҹϞҷⷕⱘ咥ԧ䚼ߚˈ䳔㽕䖯䖲᥹ⱘৡᄫ䖯㸠߸ᮁ } this.setVisible(false); this.mainFrame.addConnection(conn); this.ctx.getPropertiesHandler().saveServerConnection(conn); //ֱᄬࠄሲᗻ᭛ӊ this.ctx.addConnection(conn); //Ⳉ᥹ֱᄬ, ϡ䳔㽕߯ᓎӏԩⱘ䖲᥹, ⏏ࡴࠄ GlobalContext ⱘ䖲᥹ Map Ё } return; showMessage("Ꮖ㒣ᄬ೼Ⳍৠৡᄫⱘ䖲᥹", "䫭䇃"); if (this.ctx.getConnection(conn.getConnectionName()) != null) { //߸ᮁ䖲᥹ৡ⿄ᰃ৺䞡໡ ServerConnection conn = getDataConnectionFromView(); //ᕫࠄ⫼᠋䕧ܹⱘֵᙃᑊ䖨ಲϔϾ ServerConnection ᇍ䈵 private void saveConnection() { //ֱᄬ䖲᥹ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\ConnectionFrame.java /Download at http://www.pin5i.com } } this.treeModel.removeNodeFromParent((DefaultMutableTreeNode)node.getLastChild()); //Ң᳔ৢϔϾᓔྟߴ䰸 for (int i = 0; i < childCount; i++) { int childCount = this.treeModel.getChildCount(node); //㦋প㡖⚍᭄䞣 private void removeNodeChildren(DefaultMutableTreeNode node) { //ߴ䰸ϔϾ㡖⚍ⱘ᠔᳝ᄤ㡖⚍ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\MainFrame.java ᐂ ServerConnection 㡖⚍ߴ䰸ᅗⱘܼ䚼ᄤ㡖⚍DŽ 㞾✊Ӯব៤݇䯁ⱘ೒ᷛˈ಴Ў ServerConnection Ёᅲ⦄њ ViewObject ⱘ getIcon ᮍ⊩DŽ঺໪ˈ䖬䳔㽕 ሲᗻֱᄬ೼ ServerConnection ⱘ⠊㉏ ConnectionNode Ёˈ䆒㕂䆹ሲᗻЎ null ৢˈ䖲᥹㡖⚍ⱘ೒ᷛህ ݇䯁ϔϾ᳡ࡵ఼ⱘ䖲᥹ˈ䳔㽕ᇚ ServerConnection ᇍ䈵ⱘ connection ሲᗻ䆒㕂Ў trueˈconnection 13.4.5 ݇䯁䖲᥹ ServerConnection ᇍ䈵DŽ ߴ䰸 properties ᭛ӊˈݡҢ Map Ёߴ䰸䆹ܜˈ⫣GlobalContext Ёⱘ removeConnection ᮍ } this.connections.remove(connection.getConnectionName()); configFile.delete(); connection.getConnectionName() + ".properties"); File configFile = new File(FileUtil.CONNECTIONS_FOLDER + //ߴ䰸䆹䖲᥹ⱘ䜡㕂᭛ӊ public void removeConnection(ServerConnection connection) { //Ң Map Ёߴ䰸ϔϾ䖲᥹ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\object\GlobalContext.java ϟᰃ GlobalContext Ёߴ䰸 ServerConnection ⱘᮍ⊩˄ҹϞҷⷕⱘ咥ԧ䚼ߚ˅DŽ ᥹ᮍ⊩ᇚ ServerConnection ҢܼሔϞϟ᭛Ёߴ䰸ˈݡՓ⫼ DefaultTreeModel ᇚ䆹㡖⚍ҢᷥϞߴ䰸DŽҹ 䇗⫼ GlobalContext ⱘߴ䰸䖲ܜremoveConnection ᮍ⊩ˈা䳔㽕Ў㦰ऩᇍ䈵⏏ࡴ ActionListener ेৃDŽ ᔧ⫼᠋䗝ᢽњᶤϾ䖲᥹㡖⚍ⱘᯊ׭ˈ䗝ᢽে䬂㦰ऩЁⱘߴ䰸䖲᥹ˈህӮ㾺থϞ䴶ⱘ } this.treeModel.removeNodeFromParent(selectNode); //Ңᷥ㡖⚍Ёߴ䰸 this.ctx.removeConnection(conn); //ҢϞϟ᭛ӊЁߴ䰸 ServerConnection conn = (ServerConnection)selectNode.getUserObject(); DefaultMutableTreeNode selectNode = getSelectNode(); private void removeConnection() { //ߴ䰸ϔϾ䖲᥹ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\MainFrame.java ݡҢᷥЁߴ䰸䖭Ͼ䖲᥹㡖⚍ˈ᳔ৢ䖬䳔㽕Ң GlobalContext ⱘ䖲᥹ Map Ёߴ䰸䆹䖲᥹DŽ ៥Ӏ䳔㽕ᯢⱑⱘᰃˈߴ䰸ϔϾ䖲᥹ˈህᰃҢㅵ⧚఼Ёᕏᑩߴ䰸䖭Ͼ䖲᥹ֵᙃˈܜᅲ⦄ߴ䰸䖲᥹ⱘࡳ㛑DŽ佪 䬂㦰ऩЁᦤկˈᔧ⫼᠋䗝ᢽњᶤϾ䖲᥹㡖⚍ⱘᯊ׭ˈህᔍߎ䆹㦰ऩˈ䆹㦰ऩᏆ㒣೼ 13.2.9 Ёᅲ⦄ˈϟ䴶 ⫼᠋䗝ᢽњϔϾ䖲᥹䳔㽕ߴ䰸ⱘᯊ׭ˈህ䳔㽕ᦤկϔϾߴ䰸䖲᥹ⱘࡳ㛑ˈߴ䰸䖲᥹ⱘࡳ㛑៥Ӏ೼ে ߴ䰸䖲᥹ 13.4.4 /Download at http://www.pin5i.com if (databaseNode.getChildCount() != 0) return; //߸ᮁབᵰᏆ㒣䖲᥹ˈ߭ϡ߯ᓎ㡖⚍ DefaultMutableTreeNode databaseNode) { private void buildDatabaseChild(Database database, //߯ᓎ᭄᥂ᑧ㡖⚍ᄤ㡖⚍ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\MainFrame.java MainFrame ⱘ clickDatabaseNode ᮍ⊩DŽ 䖛⿟˅DŽټᑧ㡖⚍ৢˈ䳔㽕䖯㸠᭄᥂ᑧ䖲᥹ˈݡЎ᭄᥂ᑧ㡖⚍⏏ࡴϝϾᄤ㡖⚍˄㸼ǃ㾚೒੠ᄬ ҹϞⱘҷⷕ߸ᮁњ⫼᠋⚍ߏ㡖⚍ⱘ㉏ൟˈҹϟᰃϞ䴶ҷⷕЁ clickDatabaseNode ⱘᅲ⦄ˈ⚍ߏ᭄᥂ } clickDatabaseNode(selectNode);//᭄᥂ᑧ䖲᥹㡖⚍ } else if (selectNode.getUserObject() instanceof Database) { clickServerNode(selectNode);//᳡ࡵ఼䖲᥹㡖⚍ˈ೼ 13.4.2 ЁᏆ㒣ᅲ⦄ if (selectNode.getUserObject() instanceof ServerConnection) { //߸ᮁ⚍ߏ㡖⚍ⱘ㉏ൟ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\MainFrame.java MainFrame ⱘ viewTreeDatas ᮍ⊩DŽ ߏ߸ᮁˈ߸ᮁ⫼᠋⚍ߏⱘᰃા⾡㉏ൟ㡖⚍ˈݡ䖯㸠໘⧚DŽ 䖛⿟㡖⚍ⱘ⚍ټᔧ✊ˈ䆹ᮍ⊩䖬䳔㽕ࡴܹ㸼㡖⚍ǃ㾚೒㡖⚍੠ᄬˈމᐂ䆹ᮍ⊩ࡴܹ߸ᮁ᭄᥂ᑧ㡖⚍ⱘᚙ ϟ䴶ݡˈމMainFrame ⱘ viewTreeDatas ᮍ⊩ˈ೼ 13.4.2 Ёˈ៥Ӏা߸ᮁњ⫼᠋⚍ߏ᳡ࡵ఼㡖⚍ⱘᚙ ᔧ᭄᥂ᑧ㡖⚍㹿⚍ߏৢˈህৃҹ䖯㸠ᠧᓔ᪡԰ˈ೼ 13.4.2 ЁˈᔧᷥⱘᶤϾ㡖⚍㹿⚍ߏৢˈህӮ䇗⫼ 13.5.1 ᠧᓔ᭄᥂ᑧ ㉏ԐDŽ ሲᗻ㞾Ꮕⱘ Connection ᇍ䈵DŽᅲ⦄ᠧᓔ᭄᥂ᑧ៪㗙݇䯁᭄᥂ᑧࡳ㛑ᯊˈ䛑Ϣ ServerConnection ⱘᅲ⦄ ServerConnection ᇍ䈵䛑ᰃ㒻ᡓѢ ConnectionNode ⱘˈ಴ℸ䛑䳔㽕এᅲ⦄ connect ᮍ⊩ˈᑊ䛑᳝ϔϾ Ͼ Database ᇍ䈵ᴹҷ㸼ϔϾ᭄᥂ᑧ㡖⚍ˈ݋ԧ䇋ⳟ 13.2.2 Ёⱘ Database ㉏DŽDatabase ᇍ䈵Ϣ 䖛⿟㡖⚍ˈ㗠䖲᥹߭ᰃḍ᥂᭄᥂ᑧᴹ߯ᓎᄤ㡖⚍ⱘDŽ೼ᴀゴЁˈ៥ӀՓ⫼ϔټᰃ㸼㡖⚍ǃ㾚೒㡖⚍੠ᄬ 㛑㉏Ԑˈ՟བᠧᓔ䖲䖲Ϣᠧᓔ᭄᥂ᑧˈ䛑䳔㽕߯ᓎᄤ㡖⚍ˈԚᰃ↣Ͼ᭄᥂ᑧⱘᄤ㡖⚍䛑া᳝ϝϾˈߚ߿ ᭄᥂ᑧㅵ⧚ࡳ㛑ϡ໮ˈࣙᣀᠧᓔ᭄᥂ᑧǃ݇䯁᭄᥂ᑧ੠ߴ䰸᭄᥂ᑧˈ䖭ϝϾࡳ㛑Ϣ䖲᥹ㅵ⧚Ёⱘࡳ 13.5 ᭄᥂ᑧㅵ⧚ ݡ䖯㸠ߴ䰸㡖⚍ৢህӮ㾺থ closeConnection ᮍ⊩DŽ ҹϞҷⷕⱘ removeNodeChildren ᮍ⊩ˈҢᷥЁߴ䰸ϔϾ㡖⚍ⱘ᠔᳝ᄤ㡖⚍ˈ⫼᠋䗝ᢽњᶤϾ㡖⚍ } this.tree.setSelectionPath(null); //䆒㕂ᷥϡ䗝Ё removeNodeChildren(selectNode); //ߴ䰸᠔᳝ⱘᄤ㡖⚍ sc.setConnection(null); //ᇚ ServerConnection ⱘ䖲᥹ᇍ䈵䆒Ў null ServerConnection sc = (ServerConnection)selectNode.getUserObject(); DefaultMutableTreeNode selectNode = getSelectNode(); private void closeConnection() { 䯁᳡ࡵ఼䖲᥹݇// /Download at http://www.pin5i.com Ў DatabaseFrame ⱘ⹂ᅮᣝ䪂ࡴܹⲥ਀఼ᑊ䇗⫼ Database ⱘ create ᮍ⊩ेৃˈ䳔㽕⊼ᛣⱘᰃˈ } stmt.execute("create database " + this.databaseName); Statement stmt = this.serverConnection.getStatement(); public void create() { //߯ᓎ᭄᥂ᑧ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\object\tree\Database.java ᇍ䈵ᮄᓎϔϾ create ᮍ⊩ˈ䆹ᮍ⊩⫼Ѣ߯ᓎ᭄᥂ᑧDŽ 䴶া᳝ϔϾ JTextFieldˈ㒭⫼᠋এ䕧᭄ܹ᥂ᑧৡ⿄ˈ೼ᴀゴᇍᑨⱘᰃ DatabaseFrame ㉏DŽЎ Database ᯊ׭ˈህৃҹ䗝ᢽᔍߎⱘে䬂㦰ऩᴹ߯ᓎ᭄᥂ᑧˈབ೒ 13.11 ᠔⼎ˈ᥹ϟᴹᰒ⼎᭄᥂ᑧ߯ᓎ⬠䴶ˈ䆹⬠ ᮄᓎϔϾ᭄᥂ᑧˈՓ⫼ JDBC ᠻ㸠 CREATE DATABASE ेৃᅲ⦄ˈᔧ⫼᠋䗝ᢽњϔϾ䖲᥹㡖⚍ⱘ 13.5.2 ᮄᓎ᭄᥂ᑧ ЁᏆ㒣ᅲ⦄DŽ ServerConnection ⱘ createConnection ᮍ⊩এ߯ᓎ Connection 䖲᥹ˈcreateConnection ᮍ⊩೼ 13.4.2 ϔϾ ServerConnection ᇍ䈵ˈ㸼ᯢϔϾ Database ᠔ሲⱘ᳡ࡵ఼䖲᥹ˈ಴Ў៥ӀৃҹⳈ᥹Փ⫼ ៥Ӏ೼ 13.2.2 Ё߯ᓎ Database ᇍ䈵ᯊˈЎ䆹ᇍ䈵ᣛᅮњϔϾᵘ䗴఼ˈᵘ䗴 Database ᇍ䈵ᖙ乏㽕 } return super.connection; super.connection = this.serverConnection.createConnection(this.databaseName); //߯ᓎ᭄᥂ᑧ䖲᥹ if (super.connection != null) return super.connection; //བᵰᏆ㒣䖲᥹ˈ ߭䖨ಲ public Connection connect() { //߯ᓎᴀ㉏ⱘ䖲᥹ᇍ䈵 ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\object\tree\Database.java Ͼᄤ㡖⚍DŽҹϟᰃ Database ᇍ⠊㉏ⱘ connect ᮍ⊩ⱘᅲ⦄DŽ 䖛⿟ˈҹϞҷⷕⱘ咥ԧ䚼ߚ߯ᓎ䖭ϝټݡ䖯㸠߯ᓎ㡖⚍ˈ᭄᥂ᑧ㡖⚍ⱘᄤ㡖⚍া᳝ϝϾ˖㸼ǃ㾚೒੠ᄬ ⱘ connect ᮍ⊩䖯㸠ˈ㗠䖭Ͼᮍ⊩䛑⬅ ServerConnection ੠ Database ߚ߿䖯㸠ᅲ⦄DŽ偠䆕њ䖲᥹ৢˈ 䖯㸠偠䆕䖲᥹ˈ偠䆕䛑ᰃ䇗⫼ ConnectionNodeܜ⚍ߏ᭄᥂ᑧ㡖⚍Ϣ⚍ߏ䖲᥹㡖⚍ⱘᅲ⦄㉏Ԑˈ䛑ᰃ } buildDatabaseChild(database, selectNode); //߯ᓎ㡖⚍ validateConnect(selectNode, database); Database database = (Database)selectNode.getUserObject(); //㦋প⚍ߏᷥ㡖⚍ⱘᇍ䈵 public void clickDatabaseNode(DefaultMutableTreeNode selectNode) { //⚍ߏ᭄᥂ᑧ㡖⚍ } this.treeModel.insertNodeInto(procedureNode, databaseNode, databaseNode.getChildCount()); this.treeModel.insertNodeInto(viewNode, databaseNode, databaseNode.getChildCount()); this.treeModel.insertNodeInto(tableNode, databaseNode, databaseNode.getChildCount()); //ᦦܹᷥЁ DefaultMutableTreeNode procedureNode = new DefaultMutableTreeNode(pNode); ProcedureNode pNode = new ProcedureNode(database); DefaultMutableTreeNode viewNode = new DefaultMutableTreeNode(new ViewNode(database)); DefaultMutableTreeNode tableNode = new DefaultMutableTreeNode(new TableNode(database)); 䖛⿟˅ټ߯ᓎϝϾᄤ㡖⚍˄㸼ǃ㾚೒ǃᄬ// /Download at http://www.pin5i.com ݋ᷣⱘ㾚೒㦰ऩ៪㗙⚍ߏ㾚೒㡖⚍ˈህৃҹᶹ䆶ܼ䚼ⱘ㾚೒ˈݡ䗝ᢽᶤϾ݋ԧⱘ㾚೒ˈ⚍ߏ哴Ⳍᔧ㾘῵ 㾚೒ㅵ⧚Џ㽕ࣙᣀ䇏প㾚೒ǃᮄᓎ㾚೒ǃׂᬍ㾚೒੠ᶹ䆶㾚೒DŽᔧ⫼᠋䗝ᢽњᶤϾ᭄᥂ᑧˈᑊ⚍Ꮉ 13.6 㾚೒ㅵ⧚ ᑧˈৃҹՓ⫼ ALTER DATABASE ᅲ⦄DŽ Ⳍᇍ↨䕗ㅔऩˈা䳔㽕Փ⫼ CREATE DATABASE ੠ DROP DATABASE ेৃᅲ⦄ˈབᵰ䳔㽕ׂᬍ᭄᥂ ҹϞҷⷕⱘ咥ԧ䚼ߚᏆ㒣೼ 13.4.5 Ёᅲ⦄DŽࠄℸˈ᭄᥂ᑧⱘⳌ݇ㅵ⧚ࡳ㛑Ꮖ㒣ᅲ⦄ˈ᭄᥂ᑧⱘࡳ㛑 } this.tree.setSelectionPath(null); //䆒㕂ᷥϡ䗝Ё removeNodeChildren(selectNode); //ߴ䰸᠔᳝ⱘᄤ㡖⚍ db.setConnection(null); Database db = (Database)selectNode.getUserObject(); DefaultMutableTreeNode selectNode = getSelectNode(); private void closeDatabase() { //݇䯁᭄᥂ᑧ䖲᥹ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\MainFrame.java ҹϟᰃ݇䯁᭄᥂ᑧⱘᅲ⦄DŽ Ϣ 13.4.5 Ё݇䯁䖲᥹ϔḋˈ䛑ᰃᇚᴀ㉏Ёⱘ connection ሲᗻ䆒㕂Ў nullˈݡᇚᄤ㡖⚍ܼ䚼ߴ䰸DŽ 13.5.4 ݇䯁᭄᥂ᑧ } this.treeModel.removeNodeFromParent(selectNode); db.remove(); Database db = (Database)selectNode.getUserObject(); DefaultMutableTreeNode selectNode = getSelectNode(); //ᕫࠄ䗝ᢽЁⱘ㡖⚍ private void removeDatabase() { //ߴ䰸ϔϾ᭄᥂ᑧ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\MainFrame.java ߴ䰸᭄᥂ᑧৢˈ䖬䳔㽕ᇚ䆹㡖⚍ҢᷥϞߴ䰸DŽ } stmt.execute("drop database " + this.databaseName); Statement stmt = this.serverConnection.getStatement(); public void remove() { //ߴ䰸ϔϾ᭄᥂ᑧ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\object\tree\Database.java ߴ䰸᭄᥂ᑧˈা䳔㽕Փ⫼ JDBC ᠻ㸠 DROP DATABASE 䇁হेৃᅲ⦄ˈҹϟᰃߴ䰸᭄᥂ᑧⱘᅲ⦄DŽ 13.5.3 ߴ䰸᭄᥂ᑧ DatabaseFrame ЁDŽ߯ᓎњ᭄᥂ᑧৢˈҹ Database ᇍ䈵ᴹ߯ᓎϔϾᷥ㡖⚍ˈ⏏ࡴࠄⳌᑨⱘ䖲᥹㡖⚍ϟDŽ ᰒ⼎ DatabaseFrame ⱘᯊ׭ˈ䳔㽕ᇚᔧࠡ䗝ᢽⱘ䖲᥹㡖⚍ᇍ䈵˄ServerConnection˅гӴ䗦ࠄ /Download at http://www.pin5i.com ᇍ䈵ᇕ㺙៤ ViewData 䲚ড়DŽ ሲᗻҷ㸼䖭Ͼ ViewData ᇍ䈵᠔ሲⱘ᭄᥂ᑧˈcontent ሲᗻ㸼⼎䖭Ͼ㾚೒ⱘݙᆍˈҹϟҷⷕᇚ ResultSet ᇍ䈵೼ 13.2.2 ЁᏆ㒣߯ᓎˈ䆹ᇍ䈵ࣙ৿ϔϾ database ੠ϔϾ content˄String ㉏ൟ˅ሲᗻˈdatabase ResultSet ᇍ䈵䕀ᤶ៤⬠䴶ᰒ⼎ⱘ᭄᥂ḐᓣDŽ೼߫㸼Ёˈ៥ӀՓ⫼ϔϾ ViewData ҷ㸼ϔϾ㾚೒ˈViewData ҹϞҷⷕᠻ㸠ϔহᶹ䆶ⱘ SQL ᑊ䖨ಲ ResultSet ᇍ䈵ˈԚᰃ䖭ḋᑊϡ⒵䎇㽕∖ˈ៥Ӏ䳔㽕ᇚ } return rs; ResultSet rs = stmt.executeQuery(sql); "sc.TABLE_SCHEMA='" + this.databaseName + "'"; String sql = "SELECT * FROM information_schema.VIEWS sc WHERE " + //ࠄ information_schema ᭄᥂ᑧЁⱘ VIEWS 㸼ᶹ䆶 Statement stmt = getStatement(); private ResultSet getViewsResultSet() throws Exception { //䖨ಲ᭄᥂ЎЁ᠔᳝ⱘ㾚೒ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\object\tree\Database.java Database Ёᶹ䆶㾚೒ҷⷕDŽ ៥Ӏ䳔㽕ࡴ᭄ܹ᥂ᑧৡ⿄԰Ўᶹ䆶ᴵӊDŽ ᭄᥂ᑧ information_schema Ёⱘ VIEWS 㸼ᶹ䆶ˈ䆹㸼ֱᄬњ MySQL Ё᠔᳝ⱘ㾚೒ˈ಴ℸᶹ䆶ⱘᯊ׭ˈ Ёˈ៥Ӏᇚ䇏প᭄᥂ᑧ㾚೒ⱘᮍ⊩ݭ೼ Database ЁDŽ㽕ᶹ䆶᠔᳝ⱘ㾚೒ˈ៥Ӏ䳔㽕ࠄ MySQL ݙ㕂ⱘ clickViewNode ᮍ⊩˄䆹ᮍ⊩೼ϟ䴶ᅲ⦄˅ˈ䆹ᮍ⊩Џ㽕এ䇏প᭄᥂ᑧЁⱘ᠔᳝㾚೒ˈݡᇚ᭄᥂ᬒܹ JList 䳔㽕ᇚ JList Ёॳ᳝ⱘ᭄᥂⏙ぎ˄ҹϞҷⷕⱘ咥ԧ䚼ߚ˅DŽᔧ⫼᠋䗝ᢽⱘᰃϔϾ㾚೒㡖⚍ˈ䙷МህӮᠻ㸠 ೼ᴀゴЁˈ᭄᥂߫㸼⬅ϔϾ JList ᅲ⦄ˈ⬅Ѣ⚍ߏњ㾚೒㡖⚍Ӯ೼ JList Ёᰒ⼎㾚೒᭄᥂ˈ಴ℸ៥Ӏ } } clickViewNode(db);//㾚೒㡖⚍ Database db = getDatabase(selectNode); } else if (selectNode.getUserObject() instanceof ViewNode) { clickDatabaseNode(selectNode);//᭄᥂ᑧ䖲᥹㡖⚍, ೼ 13.5.1 Ёᅲ⦄ } else if (selectNode.getUserObject() instanceof Database) { clickServerNode(selectNode);//᳡ࡵ఼䖲᥹㡖⚍, ೼ 13.4.2 Ёᅲ⦄ if (selectNode.getUserObject() instanceof ServerConnection) { //߸ᮁ⚍ߏ㡖⚍ⱘ㉏ൟ this.dataList.setListData(this.emptyData); //⏙ぎ߫㸼᭄᥂ if (selectNode == null) return; DefaultMutableTreeNode selectNode = getSelectNode(); //㦋ᕫ䗝Ёⱘ㡖⚍ public void viewTreeDatas() { //⚍ߏᷥ㡖⚍ⱘ᪡԰ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\MainFrame.java 㾚೒㡖⚍ⱘᅲ⦄DŽ ϔϾ viewTreeDatas ᮍ⊩ˈ䆹ᮍ⊩೼ 13.5.1 Ёгᅲ⦄њ᭄᥂ᑧ㡖⚍ⱘ⚍ߏˈ⦄೼ݡЎ䆹ᮍ⊩ࡴܹ⚍ߏ 㡖⚍ˈህৃҹᶹ䆶䖭Ͼ᭄᥂ᑧЁ᠔᳝ⱘ㾚೒DŽ೼ 13.4.2 Ёˈᔧ⚍ߏњᷥЁⱘᶤϾ㡖⚍ᯊˈ៥ӀህӮᠻ㸠 ⫼᠋䗝ᢽњᶤϾ᭄᥂ᑧ㡖⚍ৢˈህৃҹᠧᓔ䖭Ͼ᭄᥂ᑧⱘ䖲᥹ˈݡ⚍ߏ䖭Ͼ᭄᥂ᑧ㡖⚍ϟ䴶ⱘ㾚೒ 13.6.1 䇏প㾚೒߫㸼 ে䬂ˈህᔍߎⳌ݇ⱘে䬂㦰ऩˈ݋ԧⱘ㦰ऩ೼ 13.2.10 ЁᏆ㒣ᦤկ˄೒ 13.14˅DŽ /Download at http://www.pin5i.com ᵰབ೒ 13.16 ᠔⼎DŽ ⊼ᛣ᳔ৢ䖬䳔㽕ᇚᔧࠡⱘ ViewObject 䆒㕂Ў㾚೒ᇍ䈵ˈ⫼Ѣᷛ䆚ᔧࠡ᠔⌣㾜ⱘ᭄᥂㉏ൟDŽᅲ⦄ᬜ } this.currentView = new ViewData(db, null); //䆒㕂ᔧࠡᰒ⼎ⱘ᭄᥂㉏ൟЎ㾚೒ createViewMenu(); //ᰒ⼎㾚೒ৢˈ߯ᓎে䬂㦰ऩ this.dataList.setListData(datas.toArray()); List datas = db.getViews(); private void clickViewNode(Database db) { //⚍ߏ㾚೒㡖⚍ˈᶹᡒܼ䚼ⱘ㾚೒ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\MainFrame.java ⊩˅ˈᇚᕫࠄⱘ㾚೒䲚ড়ᬒܹ JList Ёˈᑊ߯ᓎⳌᑨⱘে䬂㦰ऩ˄೒ 13.14˅DŽ ᕫࠄњ㾚೒ᇍ䈵ⱘ䲚ড়ৢˈ៥Ӏህৃҹᅲ⦄⚍ߏ㾚೒㡖⚍ⱘᮍ⊩˄ᴀᇣ㡖ࠡ䴶ⱘ clickViewNode ᮍ } return result; rs.close(); } result.add(td); td.setName(rs.getString(TABLE_NAME)); //ᕫࠄ㾚೒ৡ⿄ ViewData td = new ViewData(this, content); String content = rs.getString("VIEW_DEFINITION"); //ᕫࠄ㾚೒ⱘᅮНݙᆍ while (rs.next()) { ResultSet rs = getViewsResultSet(); List result = new ArrayList(); public List getViews() { //䖨ಲ䖭Ͼ᭄᥂ᑧ䞠ⱘ᠔᳝㾚೒ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\object\tree\Database.java /Download at http://www.pin5i.com //ׂᬍ㾚೒ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\object\list\ViewData.java ेৃˈЎ ViewData ᇍ䈵ࡴׂܹᬍ㾚೒ⱘᮍ⊩DŽᔧׂᬍᅠ㾚೒ৢˈ䇗⫼ׂᬍᮍ⊩ेৃDŽ Ϣ߯ᓎ㾚೒ϔḋˈՓ⫼ৠḋⱘ⬠䴶ˈাᰃᠻ㸠ϡৠⱘ SQL 䇁হˈׂᬍ㾚೒ৃҹՓ⫼ ALTER VIEW 13.6.3 ׂᬍ㾚೒Ϣߴ䰸㾚೒ ҹ߯ᓎ㾚೒ˈ ⫼᠋⚍ߏֱᄬˈᔍߎ঺໪ϔϾにষ䅽⫼᠋䕧ܹ㾚೒ৡ⿄ˈ᳔ৢ䇗⫼Ϟ䴶ⱘ createView ᮍ⊩ˈेৃ } database.getStatement().execute(sql); MySQLUtil.AS + " " + content; String sql = MySQLUtil.CREATE_VIEW + name + " " + //ᣐ㺙 CREATE VIEW 䇁হ public void createView() { //߯ᓎ㾚೒ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\object\list\ViewData.java 䈵ࡴܹϔϾ߯ᓎ㾚೒ⱘᮍ⊩DŽ ࠄ⫼᠋೼㾚೒⬠䴶䕧ܹⱘ㾚೒ᅮНৢˈህৃҹᠻ㸠 CREATE VIEW 䇁হ䖯㸠߯ᓎ㾚೒DŽЎ ViewData ᇍ Ӏ߯ᓎ㾚೒Ϣׂᬍ㾚೒䛑ᰃՓ⫼ৠϔϾ⬠䴶ˈ಴ℸ೼ᠻ㸠ֱᄬⱘᯊ׭ˈህ䳔㽕߸ᮁᮄ๲䖬ᰃׂᬍDŽᔧᕫ ߯ᓎ㾚೒Փ⫼ JDBC ᠻ㸠 CREATE VIEW 䇁হेৃᅲ⦄ˈ㾚೒⬠䴶াᦤկϔϾֱᄬࡳ㛑ˈ⬅Ѣ៥ 13.6.2 ᮄᓎ㾚೒ ೒ 13.16 㾚೒߫㸼  /Download at http://www.pin5i.com ᕫࠄ Statement ᇍ䈵ˈݡᠻ㸠 SQL 䇁হDŽ ϸϾᇍ䈵䛑ӮֱᄬϔϾ᭄᥂ᑧⱘሲᗻˈⳈ᥹ህৃҹ䗮䖛᭄᥂ᑧᇍ䈵˄Database˅ⱘ getStatement ᮍ⊩ 䖛⿟ᇍ䈵˄ProcedureData˅Ϣ㾚೒ᇍ䈵˄ViewData˅ϔḋˈ䛑ᰃሲѢᶤϾ᭄᥂ᑧⱘˈ಴ℸ䖭ټᄬ } this.database.getStatement().execute(sql); " (" + this.arg + ") returns " + this.returnString + " " + this.content; String sql = MySQLUtil.CREATE_FUNCTION + this.name + public void createFunction() { //߯ᓎߑ᭄ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\object\list\ProcedureData.java ProcedureData Ё߯ᓎߑ᭄ᮍ⊩DŽ } this.database.getStatement().execute(sql); " (" + this.arg + ") " + this.content; String sql = MySQLUtil.CREATE_PROCEDURE + this.name + public void createProcedure() { 䖛⿟ټ //߯ᓎᄬ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\object\list\ProcedureData.java 䖛⿟ᮍ⊩DŽټProcedureData ߯ᓎᄬ ߑ᭄ࡴܹࠄ䆹㉏ЁेৃDŽ 䖛⿟៪㗙ټ䖛⿟ⱘ᭄᥂ᇍ䈵ˈ಴ℸᇚ߯ᓎᄬټ13.2.2 Ёг߯ᓎњϔϾ ProcedureData ᇍ䈵ᴹ㸼⼎ϔϾᄬ ᓎߑ᭄Փ⫼ CREATE FUNCTIONDŽ೼ᴀゴЁˈ㾚೒᭄᥂Փ⫼ⱘᰃϔϾ ViewData ᇍ䈵˄13.6 ゴ㡖˅ˈ೼ 䖛⿟Փ⫼ CREATE PROCEDUREˈ߯ټ䖛⿟៪㗙ߑ᭄DŽ߯ᓎᄬټϢৡ⿄ৢˈህৃҹՓ⫼ੑҸএ߯ᓎᄬ 䖛⿟៪㗙ߑ᭄ⱘᅮНǃখ᭄ǃ䖨ಲؐ˄ߑ᭄˅ټ䖛⿟੠ߑ᭄ⱘ⬠䴶Ꮖ㒣೼ 13.2.7 Ё߯ᓎˈᕫࠄᄬټᄬ 䖛⿟੠ߑ᭄ټ13.7.1 ᮄ๲ᄬ 䖛ϔѯ⬠䴶߸ᮁेৃᅲ⦄DŽϢ㾚೒ㅵ⧚ϔḋˈ䛑ᰃ᳝ᮄ๲ǃׂᬍ੠ߴ䰸ࡳ㛑DŽ 䖛⿟Ϣߑ᭄ㅵ⧚Փ⫼Ⳍৠⱘ⬠䴶ˈ಴ℸ៥Ӏৃҹϔ䍋ᅲ⦄ˈᅗӀⱘऎ߿೼Ѣᰃ৺᳝䖨ಲؐˈ䗮ټᄬ 䖛⿟Ϣߑ᭄ㅵ⧚ټ13.7 ᄬ ࡳ㛑៥Ӏᇚ೼ϟ䴶ⱘゴ㡖Ёᅲ⦄DŽ ࡳ㛑ˈህᰃᶹⳟ㾚೒DŽᔧ៥Ӏ䗝ᢽњᶤϾ㾚೒䖯㸠ঠߏ᪡԰ⱘᯊ׭ˈህ䳔㽕⌣㾜䆹㾚೒ⱘ᭄᥂ˈ䖭ϔϾ ࠄℸˈᶹ䆶ܼ䚼㾚೒ǃ߯ᓎ㾚೒ǃׂᬍ㾚೒੠ߴ䰸㾚೒ࡳ㛑Ꮖ㒣ᅲ⦄ˈԚᰃˈ䖬㔎ᇥϔϾ᳔䞡㽕ⱘ } database.getStatement().execute(sql); String sql = MySQLUtil.DROP_VIEW + this.name; public void dropView() { //ߴ䰸㾚೒ ViewData˖ ৠḋഄˈߴ䰸㾚೒Փ⫼ JDBC ᠻ㸠 DROP VIEW ेৃᅲ⦄DŽ } database.getStatement().execute(sql); + " " + content; String sql = MySQLUtil.ALTER_VIEW + name + " " + MySQLUtil.AS public void alterView() { /Download at http://www.pin5i.com ForeignTableˈᅗӀ䛑㒻ᡓѢ JTableDŽᄫ↉߫㸼᳝ҹϟ᪡԰˖ ㉏ˈ⬠䴶Ёᄬⱘ᳝ϸϾ߫㸼ᇍ䈵ˈߚ߿ᰃᄫ↉߫㸼੠໪䬂ᄫ↉߫㸼ˈᇍᑨⱘ㉏ᰃ FieldTable Ϣ ೼ 13.2.5 Ёˈ៥ӀᏆ㒣߯ᓎⱘ㸼ㅵ⧚⬠䴶˄བ 13.2.5 Ёⱘ೒ 13.7 ᠔⼎˅ˈᇍᑨⱘᰃ TableFrame ⱘ᭄᥂ˈݡ䖯㸼ⱘׂᬍDŽ ೼ׂᬍ㸼ⱘᯊ׭ˈ⫼᠋೼⬠䴶ⱘ㸼ḐЁӮ䖯㸠৘⾡᪡԰ˈ᪡԰ᅠৢ䖯㸠ֱᄬˈህ䳔㽕ᬊ䲚䖭ѯ㹿᪡԰䖛 㸼ㅵ⧚೼ᴀゴЁⳌᇍ䕗䲒ˈ៥Ӏ䳔㽕Ң⬠䴶Ёᕫࠄ߯ᓎ㸼ⱘֵᙃˈ՟བᄫ↉ֵᙃǃ໪䬂ᄫ↉ֵᙃㄝDŽ 13.8 㸼ㅵ⧚ 䇗⫼ˈ೼ᅲ⦄њ SQL ᶹ䆶ࡳ㛑ৢˈህৃҹᠻ㸠ϔহ CALL ⱘ SQL ᴹ䇗⫼ᶹⳟᬜᵰDŽ 䖛⿟៪㗙ߑ᭄ⱘ䇗⫼ৃҹՓ⫼ CALL ᴹټ䖛⿟੠ߑ᭄ϡݡ䆺㒚ᦣ䗄DŽᄬټ䖛⿟੠ߑ᭄ˈߴ䰸ᄬټϔϾᄬ 䖛⿟Ϣߑ᭄ⱘᯊ׭ˈ៥ӀህՓ⫼њ DROP PROCEDURE ੠ DROP FUNCTION ᴹߴ䰸ټ೼ׂᬍᄬ ৃᅲ⦄DŽ 䖛⿟៪㗙ߑ᭄ⱘৡ⿄Փ⫼ ALTER PROCEDURE ៪㗙 ALTER FUNCTION ेټ䖛⿟DŽׂᬍᄬټᴹⱘᄬ 䖛⿟ᬍಲᴹˈ䖭ḋህৃҹ⹂ֱ䫭䇃থ⫳ৢ᮴⊩ᘶ໡ॳټ䖛⿟ˈབᵰ߯ᓎ༅䋹ˈህᇚᬍৡৢⱘᮻⱘᄬټᄬ 䖛⿟ᬍৡˈݡ߯ᓎϔϾׂᬍৢⱘټ䖭Ͼ䯂乬ˈৃҹᇚॳᴹⱘᄬއ䖛⿟៪㗙ߑ᭄ߴ䰸ˈЎњ㾷ټॳᴹⱘᄬ 䖛⿟៪㗙ߑ᭄㓪ݭ᳝䇃ˈ䙷МህӮᇚټ䖛⿟੠ߑ᭄ˈԚᰃབᵰϔᮺᄬټҹϞϸϾᮍ⊩ህৃҹׂᬍᄬ } this.database.getStatement().execute(createSQL); " (" + this.arg + ") returns " + this.returnString + " \n" + this.content; String createSQL = MySQLUtil.CREATE_FUNCTION + this.name + this.database.getStatement().execute(dropSQL); String dropSQL = MySQLUtil.DROP_FUNCTION + this.name; ߴ䰸ܜᡞॳᴹⱘܜ //ׂᬍ䳔㽕 public void updateFunction() { //ׂᬍߑ᭄ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\object\list\ProcedureData.java } this.database.getStatement().execute(createSQL); " (" + this.arg + ") " + this.content; String createSQL = MySQLUtil.CREATE_PROCEDURE + this.name + //߯ᓎ䇁হ this.database.getStatement().execute(dropSQL); String dropSQL = MySQLUtil.DROP_PROCEDURE + this.name; //ߴ䰸䇁হ ߴ䰸ܜᡞॳᴹⱘܜ䖛⿟䳔㽕ټ //ׂᬍᄬ public void updateProcedure() { 䖛⿟ټ //ׂᬍᄬ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\object\list\ProcedureData.java 䖛⿟DŽټProcedureData Ёׂᬍᄬ ᓎDŽ ߴ䰸ˈݡ䞡ᮄ߯ܜ䖛⿟៪㗙ߑ᭄ټ䖛⿟៪㗙ߑ᭄ⱘᮍ⊩ԧϢখ᭄ˈ಴ℸˈᅲ⦄ᯊ䳔㽕ᇚॳᴹⱘᄬټᬍᄬ FUNCTION˅ᴹᅲ⦄ˈ䖭ᰃ⬅Ѣ MySQL Ёⱘ ALTER PROCEDURE ੠ ALTER FUNCTION ᑊϡ㛑ׂ 䖛⿟៪㗙ߑ᭄ˈϡՓ⫼ ALTER PROCEDURE˄ALTERټ߸ᮁDŽϢׂᬍ㾚೒ϡৠⱘᰃˈׂᬍᄬ 䖛⿟˄ߑ᭄˅Փ⫼ⱘᰃⳌৠⱘ⬠䴶ˈ಴ℸ೼ֱᄬⱘᯊ׭䳔㽕԰ߎټ䖛⿟˄ߑ᭄˅Ϣᮄ๲ᄬټׂᬍᄬ 䖛⿟Ϣߑ᭄ټᬍᄬׂ 13.7.2 /Download at http://www.pin5i.com return datas; } datas.add(data); data.add(getPrimaryKeyIcon(field));//㦋ᕫЏ䬂೒⠛ 䆌ぎⱘ೒⠛ܕ data.add(getNullIcon(field));//㦋ᕫᰃ৺ data.add(field.getType());//ᄫ↉㉏ൟ data.add(field.getFieldName());//ᄫ↉ৡ⿄ Vector data = new Vector(); Field field = this.fields.get(i); for (int i = 0; i < this.fields.size(); i++) { Vector datas = new Vector(); public Vector getFieldDatas() { //ᕫࠄᄫ↉߫㸼᭄᥂ } this.fieldTable.setTableFace(); //䆒㕂߫㸼ḋᓣˈࣙᣀ㸠催ǃ߫ᆑㄝ tableModel.setDataVector(getFieldDatas(), this.fieldTable.getFieldTableColumn()); //䆒㕂᭄᥂ DefaultTableModel tableModel = (DefaultTableModel)this.fieldTable.getModel(); public void refreshFieldTable() { //ࠋᮄᄫ↉߫㸼 ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\TableFrame.java ᮄᄫ↉ǃׂᬍᄫ↉៪㗙ߴ䰸ᄫ↉ৢˈ䛑䳔㽕ᇚ߫㸼䖯㸠ϔ⃵ࠋᮄDŽ 䙷Мབᵰ䖯㸠ᮄᓎᄫ↉᪡԰ˈህৃҹᇍ䆹䲚ড়䖯㸠᪡԰њDŽϟ䴶ᅲ⦄ࠋᮄᄫ↉߫㸼ⱘᮍ⊩ˈ⬅Ѣ೼ࡴܹ ᥹ϟᴹˈ೼ TableFrame˄㸼ㅵ⧚⬠䴶˅Ёˈ߯ᓎϔϾᄫ↉䲚ড়⫼ᴹֱᄬᔧࠡ⬠䴶᠔ᰒ⼎ⱘᄫ↉ˈ //ⳕ⬹ setter ੠ getter ᮍ⊩ private String uuid; //ᷛ䆚䖭Ͼᄫ↉ⱘ uuid private TableData table; //䆹ᄫ↉᠔ሲⱘ㸼 private boolean autoIncrement = false; //ᰃ৺㞾ࡼ๲䭓 private String defaultValue; //咬䅸ؐ private boolean isPrimaryKey = false; //ᰃ৺Џ䬂ˈᰃЏ䬂Ў trueˈ৺߭Ў false 䆌Ўぎܕ䆌ぎˈ咬䅸ܕ// ; private boolean allowNull = true private String type; //ᄫ↉㉏ൟ private String fieldName; //ᄫ↉ৡ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\table\object\Field.java 䭓ᑺㄝϔ㋏߫ⱘᄫ↉ֵᙃDŽ Ўњ㛑ԧ⦄ϔϾᄫ↉ˈ៥ӀᮄᓎϔϾᄫ↉ᇍ䈵 Fieldˈ䆹ᇍ䈵ֱᄬϔϾᄫ↉ⱘ᠔ֵ᳝ᙃˈࣙᣀৡ⿄ˈ 13.8.1 ᮄᄫ↉ ‰ ߴ䰸໪䬂˖ߴ䰸ϔϾ䗝ᢽⱘ໪䬂DŽ ‰ ᮄ໪䬂˖৥㸼໪ሒ䚼䗑ࡴϔϾᮄⱘ໪䬂DŽ ໪䬂ᄫ↉᳝ҹϟ᪡԰˖ ‰ 䆒㕂㞾ࡼ๲䭓˖䆒㕂ᄫ↉ᰃ৺ৃҹ㞾ࡼ๲䭓DŽ ‰ 䆒㕂咬䅸ؐ˖೼᭛ᴀḚЁ䕧ܹ䆹ᄫ↉ⱘ咬䅸ؐDŽ ‰ ߴ䰸ᄫ↉˖ߴ䰸᠔䗝ⱘ㸠DŽ ‰ ᦦܹᄫ↉˖೼᠔䗝ᢽⱘ㸠ࠡ䴶ᦦܹϔϾᄫ↉㸠DŽ ᮄᄫ↉˖৥ᄫ↉߫㸼ⱘሒ䚼䗑ࡴϔϾᮄⱘᄫ↉㸠DŽ ‰ /Download at http://www.pin5i.com //Ңᄫ↉䲚ড়Ёߴ䰸 if (field == null) return; Field field = this.fields.get(selectRow); //ᕫࠄ⫼᠋᠔䗝ᢽⱘ Field ᇍ䈵 if (selectRow == -1) return; int selectRow = this.fieldTable.getSelectedRow(); //ᕫࠄ䗝Ёⱘ㸠 private void deleteField() { //ߴ䰸ᄫ↉ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\TableFrame.java ⱘ䆱ˈህ䳔㽕ࡴܹ঺໪ⱘ᪡԰ˈ೼ϟ䴶ׂᬍ㸼ⱘゴ㡖Ёᦣ䗄DŽ ߴ䰸ᄫ↉ᅲ⦄Ϣᦦܹᄫ↉ϔḋˈা䳔㽕ᇚᄫ↉Ң䲚ড়Ёߴ䰸ᑊࠋᮄ߫㸼ेৃˈ঺໪ˈབᵰᰃׂᬍ㸼 } if (this.table.getName() != null) this.addFields.add(field); //བᵰᰃׂᬍ⢊ᗕˈ߭⏏ࡴ addFields 䲚ড়Ё refreshFieldTable(); //ࠋᮄᄫ↉߫㸼 this.fields.add(selectRow, field); Field field = new Field(); } return; newField(); //≵᳝䗝Ёˈ䇗⫼ࡴᮄᄫ↉ᮍ⊩ˈࡴܹᮄᄫ↉ if (selectRow == -1) { int selectRow = this.fieldTable.getSelectedRow(); //ᕫࠄ䗝ᢽЁⱘ㸠㋶ᓩ private void insertField() { //ᦦܹᮄᄫ↉ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\TableFrame.java ᰃˈᔧ⫼᠋≵᳝䗝ᢽӏᛣϔ㸠ⱘᯊ׭ˈህৃҹ䇗⫼ᮄᄫ↉ⱘᮍ⊩ˈे 13.8.1 Ё߯ᓎᮄᄫ↉ⱘᮍ⊩DŽ ᦦܹᮄᄫ↉ˈা䳔㽕ᕫࠄ⫼᠋ᔧࠡ᠔䗝ᢽⱘ㸠ˈᑊ೼䆹㸠ⱘࠡ䴶ࡴܹϔϾ᭄᥂㸠ेৃˈ䳔㽕⊼ᛣⱘ 13.8.2 ᦦܹᄫ↉Ϣߴ䰸ᄫ↉ ᠋⏏ࡴ䖛ⱘᄫ↉˄ׂᬍⱘᯊ׭˅DŽ䆹䲚ড়ⱘ԰⫼ˈ៥Ӏᇚׂᬍ㸼ⱘᯊ׭䆺㒚ᦣ䗄DŽ ҹϞⱘ咥ԧҷⷕˈᰃ೼⫼᠋ᰃ㸠ׂᬍ㸼ⱘᯊ׭ᠡ䳔㽕ˈ៥Ӏ߯ᓎϔϾ addFields 䲚ড়ˈ⫼ᴹֱᄬ⫼ } if (this.table.getName() != null) this.addFields.add(field); //བᵰᰃׂᬍ⢊ᗕˈ߭⏏ࡴ addFields 䲚ড়Ё refreshFieldTable(); //ࠋᮄᄫ↉߫㸼 this.fields.add(field); Field field = new Field(); private void newField() { //ࡴܹᮄᄫ↉ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\TableFrame.java refreshFieldTable ᮍ⊩ᇍᄫ↉߫㸼䖯㸠ࠋᮄDŽ ҹϞҷⷕЁⱘ咥ԧ䚼ߚˈህᰃᔧࠡ⬠䴶Ёⱘᄫ↉䲚ড়DŽ೼ᮄᓎϔϾᄫ↉ৢˈህৃҹՓ⫼ { /Download at http://www.pin5i.com 䆌ぎā੠ĀЏ䬂ā߫ˈህ䳔㽕ৠℹএׂᬍ䲚ড়Ёⱘ Field ᇍ䈵DŽׂᬍ FieldTableܕ঺໪ˈབᵰ⫼᠋⚍ߏњĀ 䙷М⫼᠋ᇍ߫㸼䖯㸠ׂᬍৢˈህৃҹৠℹⱘএׂᬍ TableFrame ЁⱘⳌᑨᇍ䈵ⱘᄫ↉ৡ੠ᄫ↉㉏ൟDŽ } field.setType(value); if (field == null) return; Field field = this.fields.get(row); //ᕫࠄⳌᑨⱘ Field ᇍ䈵 public void changeFieldType(int row, String value) { //ᄫ↉߫㸼ⱘᄫ↉㉏ൟˈৠℹএׂᬍᄫ↉䲚ড়Ёⱘᄫ↉㉏ൟؐ } field.setFieldName(value); if (field == null) return; Field field = this.fields.get(row); //ᕫࠄⳌᑨⱘ Field ᇍ䈵 public void changeFieldName(int row, String value) { //ᄫ↉߫㸼ⱘᄫ↉ৡ⿄ˈৠℹএׂᬍᄫ↉䲚ড়Ёⱘᄫ↉ৡ⿄ؐ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\TableFrame.java ᮍ⊩䖯㸠ׂᬍᄫ↉ৡ੠ᄫ↉㉏ൟDŽ Ḑⱘ㸠㋶ᓩ੠㓪䕥ৢⱘؐˈݡ䇗⫼ TableFrame ⱘܗℶ㓪䕥ⱘᯊ׭ˈᕫࠄ⫼᠋᠔㓪䕥ⱘऩذ೼߫㸼 } } this.tableFrame.changeFieldType(row, value); //᳈ᬍᄫ↉㉏ൟ } else if (columnName.equals(FIELD_TYPE)) { this.tableFrame.changeFieldName(row, value); //᳈ᬍᄫ↉ৡ⿄ if (columnName.equals(FIELD_NAME)) { String value = (String)this.getValueAt(row, column); Ḑⱘؐܗ //ᕫࠄ㓪䕥ৢⱘऩ String columnName = model.getColumnName(column); DefaultTableModel model = (DefaultTableModel)this.getModel(); //㦋ᕫᔧࠡ㓪䕥ⱘ߫ৡ super.editingStopped(e); int row = this.getEditingRow(); int column = this.getEditingColumn(); public void editingStopped(ChangeEvent e) { ℶ㓪䕥ⱘᯊ׭㾺থ䆹ᮍ⊩ذ //䞡ݭ JTable ⱘᮍ⊩, ߫㸼 ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\table\FieldTable.java 䆌ぎ੠Џ䬂˅䖯㸠䗝ᢽᠡӮথ⫳ؐⱘᬍবDŽܕϸϾሲᗻ˄ᰃ৺ ׂᬍᇍᑨⱘ Field ᇍ䈵ˈা䳔㽕ׂᬍ䆹ᇍ䈵ⱘᄫ↉ৡ⿄Ϣᄫ↉㉏ൟˈ಴Ў䖭ϸϾሲᗻᠡৃҹ䕧ܹˈ݊Ҫ ℶ㓪䕥ⱘᯊ׭ˈ䳔㽕ذℶ㓪䕥ⱘᯊ׭ˈህৃҹ㾺থⳌᑨⱘᮍ⊩DŽ䖭䞠䳔㽕⊼ᛣⱘᰃˈᔧذᔧ⫼᠋ᇍ߫㸼 ᔧ⫼᠋೼ᄫ↉߫㸼Ёᇍᄫ↉ⱘֵᙃ䖯㸠ׂᬍᯊˈህ䳔㽕ᕫࠄⳌᑨⱘ Field ᇍ䈵ˈᑊ䆒㕂ᮄⱘֵᙃDŽ 13.8.3 㓪䕥ᄫ↉ } refreshFieldTable(); //ࠋᮄ߫㸼 this.fields.remove(field); /Download at http://www.pin5i.com field.setDefaultValue(defaultValue); //䆒㕂ᄫ↉咬䅸ؐ Field field = this.fields.get(selectRow); //পᕫᔧࠡ㓪䕥ⱘ Field ᇍ䈵 String defaultValue = this.defaultField.getText(); //পᕫ咬䅸ؐ if (selectRow == -1) return; int selectRow = this.fieldTable.getSelectedRow(); //ᕫࠄ䗝Ёⱘ㸠 public void changeDefaultValue() { //ᬍবᄫ↉ⱘ咬䅸ؐ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\TableFrame.java 䅸ؐDŽ ҹ㞾ࡼ๲䭓DŽ៥ӀৃҹЎ᭛ᴀḚࡴܹᣝ䬂џӊˈᔧ⫼᠋೼᭛ᴀḚЁ䖯㸠䕧ܹᯊˈህৃҹᬍব䆹ᄫ↉ⱘ咬 ⬠䴶ЁᦤկњϔϾ᭛ᴀḚˈৃҹ䆒㕂ᶤϾᄫ↉ⱘ咬䅸ؐˈᦤկњϔϾ໮䗝Ḛˈৃҹ䆒㕂ᄫ↉ᰃ৺ৃ 13.8.4 䆒㕂咬䅸ؐϢ㞾ࡼ๲䭓 Ёⱘᮍ⊩এׂᇍᑨ Field ᇍ䈵ⱘሲᗻؐDŽ 䆌ぎā៪㗙ĀЏ䬂ā߫ˈህৃҹ䇗⫼ TableFrameܕḐ᠔ሲⱘ߫ˈབᵰᰃĀܗ߸ᮁ⫼᠋᠔⚍ߏⱘऩ } } this.tableFrame.changeAllowNullValue(row); if (allowNullColumn.getModelIndex() == column) { TableColumn allowNullColumn = this.getColumn(ALLOW_NULL); 䆌ぎ߫ܕ //ᕫࠄ } this.tableFrame.changePrimaryKeyValue(row); if (primaryColumn.getModelIndex() == column) { TableColumn primaryColumn = this.getColumn(PRIMARY_KEY); //ᕫࠄЏ䬂߫ private void changeClickValue(int row, int column) { 䆌ぎ੠Џ䬂߫ܕḐџӊⱘᯊ׭ˈᬍবؐˈϔ㠀াᬍবܗ //ᔧথ⫳哴ᷛ⚍ߏऩ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\table\FieldTable.java ؐˈᔧ⫼᠋⚍ߏњ䖭ϸ߫ⱘᯊ׭ˈህ䳔㽕ৠℹׂᬍ TableFrame ⱘ fields 䲚ড়DŽ 䆌ぎāϢĀЏ䬂ā䖭ϸ߫ⱘܕҹϞⱘ咥ԧҷⷕЎᮄࡴⱘҷⷕDŽ݊Ёⱘ changeClickValue ЎᬍবĀ } changeClickValue(row, column); 䆌ぎ੠Џ䬂ܕ //䆒㕂⚍ߏৢӮᬍবⱘؐˈ⊼ᛣᰃ⚍ߏˈᑊϡᰃ䕧ܹˈ಴ℸাӮ᳈ᬍ selectPrimaryKeyColumn(row, column); selectAllowNullColumn(row, column); //ׂᬍ೒⠛߫ if (column == -1 || row == -1) return; int row = this.getSelectedRow(); int column = this.getSelectedColumn(); private void selectCell() { //哴ᷛ೼ JTable Ё⚍ߏⱘᯊ׭㾺থ䆹ᮍ⊩ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\table\FieldTable.java ⱘ selectCell ᮍ⊩ˈ䆹ᮍ⊩೼ 13.2.5 ЁᏆ㒣ᅲ⦄њ䚼ߚˈᔧ⫼᠋䗝ᢽњ߫㸼ⱘᯊ׭ˈህӮ㾺থ䆹ᮍ⊩DŽ /Download at http://www.pin5i.com tableModel.setDataVector(getForeignDatas(), this.foreignTable.getForeignColumns()); DefaultTableModel tableModel = (DefaultTableModel)this.foreignTable.getModel(); //䆒㕂໪䬂᭄᥂ public void refreshForeignFieldTable() { //ࠋᮄ໪䬂ᄫ↉߫㸼 ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\TableFrame.java ೼ 13.2.5 ЁᏆ㒣߯ᓎњ໪䬂߫㸼ᇍ䈵˄ForeignTable˅ˈᮄᓎࠋᮄ໪䬂߫㸼ⱘᮍ⊩DŽ //ⳕ⬹ setter ੠ getter ᮍ⊩ private String uuid; //ᄫ↉ⱘ uuid private String referenceFieldName; //㑺ᴳᄫ↉ⱘৡ⿄ private String referenceTableName; //㑺ᴳ㸼ⱘৡ⿄ private String onUpdate; //㑻㘨᳈ᮄㄪ⬹ private String onDelete; //㑻㘨ߴ䰸ㄪ⬹ private Field referenceField; //໪䬂ⱘᄫ↉ˈৃҹḍ᥂ℸሲᗻᡒߎ䆹݇㋏Ёⱘ໪䬂㸼 private Field field; //㹿㑺ᴳⱘᄫ↉ˈḍ᥂䆹ᄫ↉ৃҹᡒߎ䆹໪䬂ᇍ䈵᠔ሲѢⱘ㸼 private String constraintName; //㑺ᴳⱘৡ⿄ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\table\object\ForeignField.java ড়ᴹֱᄬᔧࠡ⬠䴶Ёⱘ໪䬂DŽ Ϣᅲ⦄ᮄᄫ↉ϔḋˈᮄᓎϔϾ FoeignField ᇍ䈵ᴹҷ㸼ϔϾ໪䬂ˈᑊ೼ TableFrame Ё߯ᓎϔϾ䲚 13.8.5 ᮄ໪䬂 ⚍ߏњ߫㸼ᶤ㸠ⱘᯊ׭ˈህৃҹᕫࠄⳌᑨⱘ Field ᇍ䈵ˈݡ䆒㕂⬠䴶ⱘ᭛ᴀḚ੠໮䗝ḚेৃDŽ } this.tableFrame.setIsAutoIncrement(row); //ׂᬍᰃ৺㞾ࡼ๲䭓ⱘ checkbox this.tableFrame.setDefaultValue(row); //ׂᬍ咬䅸ؐ //ⳕ⬹݊Ҫҷⷕ private void selectCell() { //哴ᷛ೼ JTable Ё⚍ߏⱘᯊ׭㾺থ䆹ᮍ⊩ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\table\FieldTable.java ㋴ЁDŽׂᬍ FieldTable ⱘ selectCell ᮍ⊩ˈ߫㸼㹿⚍ߏᯊˈӮ㾺থ䆹ᮍ⊩DŽܗ⦄ࠄ⬠䴶ⱘ TableFrame ⱘ fields 䲚ড়ЁDŽԚᰃˈ䖬䳔㽕এׂᬍ⚍ߏᄫ↉߫㸼ⱘᮍ⊩ˈᇚ咬䅸ؐϢ㞾ࡼ๲䭓ⱘؐሩ 䙷М⫼᠋೼䆒㕂咬䅸ؐ៪㗙㞾ࡼ๲䭓ⱘᯊ׭ˈህৃҹৠℹᇚ咬䅸ؐϢ㞾ࡼ๲䭓ⱘᷛ䆚ֱᄬࠄ } else field.setAutoIncrement(false); if (this.isAutoIncrementBox.isSelected()) field.setAutoIncrement(true); //䆒㕂 Field ᇍ䈵Ёⱘ㞾ࡼ๲䭓ሲᗻ Field field = this.fields.get(row); //ᕫࠄᔧࠡ᠔䗝ᢽњ Field ᇍ䈵 if (row == -1) return; int row = this.fieldTable.getSelectedRow(); //ᕫࠄᄫ↉߫㸼Ё᠔䗝Ёⱘ㸠㋶ᓩ private void clickIsAutoIncrementBox() { //⚍ߏ㞾ࡼ๲䭓 checkBox ⱘᮍ⊩ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\TableFrame.java ৠḋഄˈϢ䆒㕂咬䅸ؐϔḋˈгৃҹЎ໮䗝Ḛࡴܹⲥ਀఼ˈབᵰথ⫳⚍ߏџӊᯊˈህᠻ㸠ᶤϾᮍ⊩DŽ { /Download at http://www.pin5i.com private String getFieldSQL() { //㦋ᕫᶹ䆶ᄫ↉ⱘ SQL ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\object\list\TableData.java TableData ЁDŽ Ёࣙ৿њϔϾ Database ᇍ䈵ˈDatabase ᇍ䈵ৃҹপࠄ᭄᥂ᑧⱘ䖲᥹ֵᙃˈৃҹᇚᶹ䆶ᄫ↉ⱘᮍ⊩ݭࠄ ᠔᳝ⱘ㸼ᄫ↉ᑊᇕ㺙៤ϔϾ Field 䲚ড়ेৃDŽ೼ᴀゴЁˈϔϾ㸼⬅ϔϾ TableData ᇍ䈵ᴹҷ㸼ˈTableData Ӏ೼ TableFrame ЁᓎゟњϔϾᄫ↉䲚ড়ᴹֱᄬᔧࠡ⬠䴶Ёⱘᄫ↉ֵᙃˈ಴ℸˈা䳔㽕Ң᭄᥂ᑧЁᶹ䆶 ᶹ䆶ϔϾ㸼ⱘֵᙃ䳔㽕ࠄ MySQL ⱘ㋏㒳㸼Ёᶹ䆶ˈ䖭ѯֵᙃࣙᣀᄫ↉ֵᙃϢ໪䬂ֵᙃㄝDŽ⬅Ѣ៥ 13.8.7 ᶹ䆶ᄫ↉ֵᙃ ᶹ䆶㸼ֵᙃǃֱᄬ㸼੠ׂᬍ㸼DŽ ࠡ䴶޴Ͼᇣ㡖Ёˈ៥䛑䆆㾷њབԩᅲ⦄㸼ㅵ⧚Ёⱘϔѯ⬠䴶᪡԰ˈ᥹ϟᴹᅲ⦄݋ԧⱘ㸼ㅵ⧚ˈࣙᣀ } this.foreignFields.remove(field); //Ңᄫ↉䲚ড়Ёߴ䰸 if (field == null) return; ForeignField field = this.foreignFields.get(selectRow); //ᕫࠄ䗝Ёⱘ໪䬂ᇍ䈵 if (selectRow == -1) return; int selectRow = this.foreignTable.getSelectedRow(); //ᕫࠄ䗝Ёⱘ㸠 private void deleteForeignField() { //ߴ䰸ϔϾᄫ↉ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\TableFrame.java ߴ䰸໪䬂ᅲ⦄↨䕗ㅔऩˈা䳔㽕Ң TableFrame ⱘĀ໪䬂ā䲚ড়Ёߴ䰸ेৃDŽ 13.8.6 ߴ䰸ϔϾ໪䬂 ForeignField ⱘ constraintName ሲᗻ䆒㕂ЎଃϔⱘDŽ 㸼ⱘゴ㡖Ёࡴҹᦣ䗄DŽ䖭䞠䳔㽕⊼ᛣⱘᰃˈЎњᷛ䆚ᮄࡴⱘĀ໪䬂ā೼᭄᥂ᑧЁⱘଃϔᗻˈ಴Ў䳔㽕ᇚ ҹϞⱘ咥ԧҷⷕˈϢ 13.8.1 Ёᮄᄫ↉ϔḋˈ䛑ᰃ೼ׂᬍ㸼ⱘᯊ׭䳔㽕Փ⫼ࠄⱘҷⷕˈᇚ೼ϟ䴶ׂᬍ } if (this.table.getName() != null) this.addForeignFields.add(foreignField); //བᵰᰃׂᬍ⢊ᗕˈࡴࠄ⏏ࡴⱘ໪䬂䲚ড়Ё refreshForeignFieldTable(); //ࠋᮄ໪䬂߫㸼 foreignField.setConstraintName(UUID.randomUUID().toString()); //䆒㕂䆹໪䬂ⱘ constraintNameˈ⫼ UUID 䆒㕂 this.foreignFields.add(foreignField); ForeignField foreignField = new ForeignField(); private void newForeignField() { //ᮄ๲ϔϾ໪䬂ᄫ↉ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\TableFrame.java ࡴܹϔϾ໪䬂ⱘᯊ׭ˈህৃҹ䇗⫼ refreshForeignFieldTable ᮍ⊩ࠋᮄ໪䬂߫㸼DŽ } this.foreignTable.setTableFace(); 䆒㕂໪䬂߫㸼ⱘḋᓣ// /Download at http://www.pin5i.com private String getOnValue(String createSQL, ForeignField foreignField, //䖨ಲ ON DELETE ៪㗙 ON UPDATE ⱘؐ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\object\list\TableData.java ҹϟᮍ⊩ߚᵤ߯ᓎ㸼ⱘ SQLˈᑊᕫࠄ ON DELETE ੠ ON UPDATE ֵᙃDŽ ⦄DŽ ህৃҹⳈ᥹ࠄ㋏㒳㸼Ёᶹ䆶DŽᕫࠄ߯ᓎ㸼ⱘ SQLˈৃҹՓ⫼ JDBC ᠻ㸠 SHOW CREATE TABLE ᴹᅲ Ўњᕫࠄ䖭Ͼሲᗻˈ៥Ӏ䳔㽕ᕫࠄ߯ᓎ㸼ⱘ SQLˈᑊᇍ䆹হ SQL 䖯㸠ߚᵤDŽབᵰԴՓ⫼ⱘᰃ MySQL5.1ˈ ህৃҹ߯ᓎϔϾ ForeignField ᇍ䈵ˈԚᰃˈForeignField Ё䖬ࣙ৿᳝ onDelete ੠ onUpdate ϸϾሲᗻˈ ᕫࠄ㑺ᴳⱘᄫ↉ৡৢˈህৃҹݡ⃵ࠄ㋏㒳㸼˄COLUMNS˅Ёᶹ䆶㑺ᴳⱘᄫ↉DŽᕫࠄ䖭ѯֵᙃৢˈ ‰ REFERENCED_COLUMN_NAME˖㑺ᴳⱘᄫ↉ৡDŽ ‰ REFERENCED_TABLE_NAME˖㑺ᴳⱘ㸼ৡDŽ ‰ CONSTRAINT_NAME˖㑺ᴳⱘৡ⿄DŽ ‰ COLUMN_NAME˖໪䬂߫ৡDŽ ᕫࠄⱘ ResultSet 䞠䴶ࣙ৿᳝བϟᄫ↉˖ AND sc.TABLE_NAME='㸼ৡ' AND sc.REFERENCED_COLUMN_NAME <> '' ORDER BY sc.COLUMN_NAME SELECT * FROM information_schema.KEY_COLUMN_USAGE sc WHERE sc.TABLE_SCHEMA='᭄᥂ᑧৡ' ᠻ㸠ϟ䴶ⱘ SQL ህৃҹ䖨ಲ໪䬂ᄫ↉ֵᙃⱘ ResultSet˖ ⱘᏂ߿ᇍ៥Ӏᓔথ䖭Ͼㅵ⧚఼᠔ѻ⫳ⱘᕅડDŽ 䖯㸠ߚᵤˈᕫࠄ໪䬂ⱘ ON DELETE ੠ ON UPDATEˈ䖭ህᰃᴀゴᓔ༈᠔䆆ⱘ MySQL5.0 Ϣ MySQL5.1 ⠜ᴀᰃ 5.0ˈ಴ℸ㽕ᕫࠄ ON DELETE ੠ ON UPDATE ⱘؐˈህ䳔㽕ᕫࠄᓎ㸼ᯊⱘ SQLˈᑊᇍ䆹হ SQL ⱘؐDŽབᵰՓ⫼ⱘᰃ MySQL5.1ˈህৃҹࠄ㋏㒳㸼Ёᶹ䆶ࠄϔϾᄫ↉ⱘ䖭ϸϾؐDŽᴀゴՓ⫼ⱘ MySQL ᶹ䆶ˈԚᰃˈབᵰՓ⫼ⱘ MySQL5.0ˈ߭ϡ㛑ࠄ㋏㒳㸼Ёᶹ䆶ࠄϔϾ໪䬂ⱘ ON DELETE ੠ ON UPDATE ᶹ䆶໪䬂ֵᙃϢᶹ䆶ᄫ↉ֵᙃϔḋˈ䛑ᰃࠄ MySQL ⱘ㋏㒳㸼˄KEY_COLUMN_USAGE˅Ё䖯㸠 13.8.8 ᶹ䆶໪䬂ֵᙃ Ͼ䲚ড়ᴹᰒ⼎ᄫ↉᭄᥂DŽ ᕫࠄҹϞⱘؐˈህৃҹᇕ㺙៤ϔϾᄫ↉ᇍ䈵ˈᑊࡴࠄ㒧ᵰ䲚ড়Ёˈ䙷М TableFrame ህৃҹḍ᥂䖭 ‰ EXTRA˖བᵰ䆹ؐЎ auto_incrementˈ߭㸼⼎ᰃ㞾ࡼ๲䭓DŽ ‰ COLUMN_DEFAULT˖䆹ᄫ↉ⱘ咬䅸ؐDŽ ‰ COLUMN_KEY˖བᵰᰃЏ䬂ˈ䙷М䆹ؐЎĀPRIāDŽ 䆌ぎDŽܕ‰ IS_NULLABLE˖ᰃ৺ ‰ COLUMN_TYPE˖ᄫ↉㉏ൟDŽ ‰ COLUMN_NAME˖ᄫ↉ৡDŽ ៥Ӏ䳔㽕Ңᶹ䆶ࠄⱘ ResultSet Ёᕫࠄҹϟؐ˖ getFieldSQL ᮍ⊩䖨ಲⱘ SQLˈህৃҹᕫࠄ ResultSet ᇍ䈵ˈݡᕫࠄ৘Ͼ߫ⱘؐDŽ getFieldSQL ᮍ⊩ᰃ䖨ಲϔহᶹ䆶ⱘ SQLˈࠄ㋏㒳㸼Ёᶹ䆶ϔϾ㸼ⱘ᠔᳝ᄫ↉ˈՓ⫼ JDBC ᠻ㸠 } return sql.toString(); .append("ORDER BY sc.ORDINAL_POSITION"); .append(this.name + "' ") .append(" AND sc.TABLE_NAME='") .append(this.database.getDatabaseName() + "' ") .append(" WHERE sc.TABLE_SCHEMA='") sql.append("SELECT * FROM information_schema.COLUMNS sc") StringBuffer sql = new StringBuffer(); /Download at http://www.pin5i.com 䆌Ўぎ, ࡴܹ NOT NULLܕ //䆹ᄫ↉ϡ if (f.isAutoIncrement()) sql.append(AUTO_INCREMENT + " "); //㞾๲䭓ࡴܹ AUTO_INCREMENT .append(f.getType()); sql.append("`" + f.getFieldName() + "` ") for (Field f : fields) { private void createField(StringBuffer sql, List fields) { //ḍ᥂ᄫ↉߯ᓎ SQL ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\object\list\TableData.java ᣐ㺙ᄫ↉ SQL ⱘᮍ⊩DŽ ߯ᓎЏ䬂ⱘ SQLDŽ ᣐ㺙 CREATE TABLE ੑҸˈݡձ⃵ᣐ㺙ᄫ↉ⱘ SQLǃ໪䬂ᄫ↉ⱘ SQL ੠ܜ⫣ᮍ⊩ˈgetTableSQL ᮍ ݋ԧᅲ⦄೼ TableData Ёⱘ addTable ᮍ⊩ˈᣐ㺙 SQL 䇁হⱘᮍ⊩ᰃ TableData Ёⱘ getTableSQL UPDATE CASCADE ,PRIMARY KEY(`field1`)) 'test' ,`field3` int(10),FOREIGN KEY (`field3`) REFERENCES `table_2` (`field_3`) ON DELETE CASCADE ON CREATE TABLE IF NOT EXISTS `table3` (`field1` int(10)AUTO_INCREMENT ,`field2` varchar(10)DEFAULT 㓪ݭᣐ㺙 SQL ⱘ⿟ᑣˈ᳔㒜ᕫߎⱘ SQL 䇁হབϟ˖ ⫼ JDBC ᠻ㸠ेৃDŽ ៥Ӏ೼߯ᓎ㸼ⱘᯊ׭ˈህৃҹḍ᥂䖭ѯ Field ᇍ䈵ⱘሲᗻᴹᣐ㺙 CREATE TABLE ⱘ SQL 䇁হˈ✊ৢՓ ៥Ӏৃҹ TableFrame˄㸼ㅵ⧚⬠䴶˅Ёᕫࠄᄫ↉ⱘ䲚ড়ˈ䲚ড়Ёֱᄬњϔ㋏߫ⱘ Field ᇍ䈵ˈ䙷М 13.8.9 ᮄᓎ㸼 ೼ TableFrame Ё߱ྟ࣪߫㸼ˈՓ⫼ DefaultTableModel ⱘ setDataVector ᮍ⊩ेৃ߱ྟ߫㸼᭄᥂DŽ ЁֱᄬњϔϾ໪䬂ᄫ↉ⱘ䲚ড়ˈ೼ 13.8.5 ᮄ໪䬂Ё߯ᓎ䆹䲚ড়DŽᕫࠄᄫ↉Ϣ໪䬂ᄫ↉ⱘ䲚ড়ৢˈህৃҹ ᕫࠄ৘Ͼֵᙃᑊᇕ㺙៤ ForeignField ⱘ䲚ড়ৢˈህৃҹᇚ䖭Ͼ䲚ড়ᬒࠄ TableFrame ЁDŽTableFrame } return null; } } } else if (value.indexOf("SET NUL") != -1)return "SET NULL"; else if (value.indexOf("CASCADE") != -1) return "CASCADE"; else if (value.indexOf("RESTRIC") != -1)return "RESTRICT"; if (value.indexOf("NO ACTI") != -1) return "NO ACTION"; String value = tempString.substring(onIndex, onIndex + 7); int onIndex = tempString.indexOf(on) + on.length() + 1; //ᕫࠄ ON DELETE ៪㗙 ON UPDAT ⱘԡ㕂 if (tempString.indexOf(on) != -1) { ⱘؐ //བᵰ䘛ࠄ ON DELETE ៪㗙 ON UPDATEˈ߭䖯㸠໘⧚ˈ䖨ಲ ON DELETE ៪㗙 ON UPDATE if (tempString.indexOf("CONSTRAINT `" + constraintName + "`") != -1) { //བᵰ䘛ࠄ໪䬂ⱘᄫヺІˈ߭䖯㸠໘⧚ String tempString = temp[i]; for (int i = 0; i < temp.length; i++) { String[] temp = createSQL.split(","); //ҹ䗫োᇚ݊ߚ䱨 String constraintName = foreignField.getConstraintName(); String on) { /Download at http://www.pin5i.com ᕫࠄ⫼᠋᪡԰䖛ⱘ᭄᥂䲚ড়ৢˈ៥ӀህৃҹЎ TableData ࡴܹϔϾׂᬍ㸼ⱘᮍ⊩ˈᇚ䖭ѯ䲚ড়䞠䴶 䖬䳔㽕೼ׂᬍ੠ߴ䰸᪡԰ⱘᯊ׭ˈֱᄬ⫼᠋᠔᪡԰ⱘ᭄᥂DŽ ׂᬍDŽ೼ 13.8.1 ᮄᄫ↉Ёˈ߯ᓎϔϾᄫ↉ⱘᯊ׭ˈ঺໪ᓎゟњϔϾ䲚ড়ᴹֱᄬ⫼᠋᠔⏏ࡴⱘᄫ↉ˈ಴ℸ 䛑䳔㽕ᇍ⫼᠋᠔᪡԰ⱘ᭄᥂䖯㸠ֱᄬˈ᳔ৢˈᕫࠄ䖭ѯ᭄᥂ৢˈህৃҹՓ⫼ ALTER TALBE ᴹ䖯㸠㸼ⱘ ߴ䰸њાѯᄫ↉Ϣ໪䬂ᄫ↉DŽгህᰃ䇈ˈ⫼᠋↣⃵䖯㸠⏏ࡴǃׂᬍǃߴ䰸ᄫ↉Ϣ໪䬂ᄫ↉ⱘᯊ׭ˈ៥Ӏ ↉ǃ⏏ࡴ໪䬂ᄫ↉ǃׂᬍ໪䬂ᄫ↉੠ߴ䰸໪䬂ᄫ↉ˈ಴ℸׂᬍ㸼ⱘ䆱ˈ៥Ӏ䳔㽕ⶹ䘧⫼᠋⏏ࡴǃׂᬍǃ ᅲ⦄ׂᬍ㸼ⱘࡳ㛑䕗Ў໡ᴖˈ⬅Ѣֱᄬ೼⬠䴶Ёৃ㛑⍝ঞⱘ᪡԰ࣙᣀ⏏ࡴᄫ↉ǃׂᬍᄫ↉ǃߴ䰸ᄫ 13.8.10 ׂᬍ㸼 ᕫࠄ߯ᓎ㸼ⱘ SQL ৢˈՓ⫼ JDBC ᠻ㸠ेৃDŽབᵰ߯ᓎ༅䋹ˈৃҹᇚᓖᐌֵᙃᰒ⼎ࠄ⬠䴶ЁDŽ } } } sql.append(PRIMARY_KEY).append("(`" + f.getFieldName() + "`)").append(","); if (f.isPrimaryKey()) { for (Field f : fields) { private void createPrimary(StringBuffer sql, List fields) { //߯ᓎЏ䬂 SQL ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\object\list\TableData.java ᣐ㺙Џ䬂ⱘ SQLDŽ } } sql.append(","); } sql.append(ON_UPDATE + " " + f.getOnUpdate() + " "); if (f.getOnUpdate() != null) { } sql.append(ON_DELETE + " " + f.getOnDelete() + " "); if (f.getOnDelete() != null) { .append("(`" + f.getReferenceField().getFieldName() + "`) "); .append(" `" + f.getReferenceField().getTable().getName() + "` ") .append(REFERENCES) .append(" (`" + f.getField().getFieldName() + "`) ") sql.append(FOREIGN_KEY) for (ForeignField f : foreignFields) { List foreignFields) { private void createForeignFields(StringBuffer sql, //߯ᓎ໪䬂 SQL ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\object\list\TableData.java ᣐ㺙໪䬂ᄫ↉ⱘ SQLDŽ } } sql.append(","); } if (f.getDefaultValue() != null) sql.append(DEFAULT + " '" + f.getDefaultValue() + "' "); if (!f.isAutoIncrement()) { //䆹ᄫ↉᳝咬䅸ؐ,ᑊϨϡᰃ㞾ࡼ๲䭓 if (!f.isAllowNull()) sql.append(NOT_NULL + " "); /Download at http://www.pin5i.com if (!f.isAllowNull()) sql.append(" NOT NULL"); .append(" " + f.getType()); sql.append("ALTER TABLE " + this.name).append(" ADD " + f.getFieldName()) StringBuffer sql = new StringBuffer(); for (Field f : addFields) { List result = new ArrayList(); private List getAlterAddFieldSQL(List addFields) { //䖨ಲܼ䚼ⱘ ALTER TABLE ADD ᄫ↉䇁হ,খ᭄Ў⫼᠋⏏ࡴⱘᄫ↉ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\object\list\TableData.java ᇚ Field ᇍ䈵䕀ᤶ៤ׂᬍⱘ SQLDŽ ҹϞⱘҷⷕᇚ⫼᠋᪡԰䖛ⱘᇍ䈵䕀ᤶ៤ SQLˈ՟བˈ⫼᠋Ў㸼⏏ࡴњ㢹ᑆϾᄫ↉ˈ䙷Мህ䳔㽕䖭ḋ } } throw new QueryException("᳈ᬍ㸼䫭䇃˖" + e.getMessage()); } catch (Exception e) { stmt.executeBatch(); for (String s : dropFFSQL) stmt.addBatch(s); for (String s : updateFFSQL) stmt.addBatch(s); for (String s : addFFSQL) stmt.addBatch(s); for (String s : dropFieldSQL) stmt.addBatch(s); for (String s : updateFieldSQL) stmt.addBatch(s); for (String s : addFieldSQL) stmt.addBatch(s); Statement stmt = database.getStatement(); try { List dropFFSQL = getAlterDropForeignFieldSQL(dropFF); //ᕫࠄߴ䰸໪䬂ⱘ SQL List updateFFSQL = getAlterUpdateForeignFieldSQL(updateFF); //ᕫࠄׂᬍ໪䬂ⱘ SQL List addFFSQL = getAlterAddForeignFieldSQL(addFF); //ᕫࠄ⏏ࡴ໪䬂ⱘ SQL List dropFieldSQL = getAlterDropFieldSQL(dropFields); //ᕫࠄߴ䰸ᄫ↉ⱘ SQL List updateFieldSQL = getAlterUpdateFieldSQL(updateFields); //ᕫࠄׂᬍᄫ↉ⱘ SQL List addFieldSQL = getAlterAddFieldSQL(addFields); //ᕫࠄ⏏ࡴᄫ↉ⱘ SQL List updateFF, List dropFF) { List dropFields, List addFF, public void updateTable(List addFields, List updateFields, */ * @param dropFF ߴ䰸ⱘ໪䬂 * @param updateFF ׂᬍⱘ໪䬂 * @param addFF ⏏ࡴⱘ໪䬂 * @param dropFields ߴ䰸ⱘᄫ↉ * @param updateFields ׂᬍⱘᄫ↉ * @param addFields 䳔㽕⏏ࡴⱘᄫ↉ * ׂᬍϔϾ㸼 /** ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\object\list\TableData.java ⱘᇍ䈵䛑䕀ᤶ៤ SQLˈ✊ৢՓ⫼ JDBC ᠻ㸠DŽ /Download at http://www.pin5i.com public interface QueryObject { ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\object\QueryObject.java ϟᮍ⊩DŽ ᓎϔϾ QueryObject ⱘ᥹ষˈ䅽 ViewData˄㾚೒ᇍ䈵˅੠ TableData এᅲ⦄䖭Ͼ᥹ষˈ䆹᥹ষᅮНབ ViewData ᇍ䈵ᴹ㸼⼎ˈ㸼Փ⫼ϔϾ TableData ᇍ䈵㸼⼎ˈԚᰃ䖭ѯᶹ䆶䛑ᰃ݅ϔϾ⬠䴶ˈ಴ℸ៥Ӏᮄ ৃҹᠧᓔ᭄᥂ᰒ⼎⬠䴶ⱘഄᮍ᳝໮Ͼˈࣙᣀᠧᓔ㾚೒ǃᠧᓔ㸼ǃᠻ㸠ᶹ䆶ˈ㾚೒೼ᴀゴЁՓ⫼ϔϾ 13.9.1 ⌣㾜᭄᥂ 䰡ᑣ੠छᑣࡳ㛑DŽ ᰒ⼎᭄᥂ⱘ⬠䴶Ꮖ㒣೼ 13.2.3 Ё߯ᓎ˄DataFrame ㉏˅ˈབ 13.2.3 Ёⱘ೒ 13.4 ᠔⼎DŽ䆹⬠䴶ࣙᣀࠋᮄǃ ߏϔϾ㾚೒៪㗙ϔϾ㸼ⱘᯊ׭ˈ៥Ӏ䳔㽕ᇚ㾚೒៪㗙㸼ᇍᑨⱘ᭄᥂ሩ⦄㒭⫼᠋ˈᑊ೼⬠䴶Ёᰒ⼎ˈ᭄᥂ ೼ 13.6 Ёˈ៥Ӏᅲ⦄њ㾚೒ㅵ⧚ࡳ㛑ˈ೼ 13.8 Ёᅲ⦄њ㸼ㅵ⧚ࡳ㛑ˈ೼⫼᠋ⱘᅲ䰙᪡԰Ёˈᔧঠ 13.9 ᭄᥂⌣㾜 ᳝ᠧᓔ㸼ǃᇐߎ㸼᭄᥂ⱘࡳ㛑ˈᇚ೼ϟ䴶ⱘゴ㡖Ёᦣ䗄DŽ JDBC ᴹᠻ㸠ϔѯ SQL 䇁হेৃˈԚᰃ⬠䴶ⱘѸѦ↨䕗໡ᴖˈ೼ᅲ⦄ⱘᯊ׭䳔㽕⡍߿⊼ᛣDŽᴀゴЁ䖬 㸼ㅵ⧚ⱘ໻䚼ߚࡳ㛑䛑Ꮖ㒣ᅲ⦄ˈҹϞᅲ⦄ⱘ䖭ѯࡳ㛑Ёˈ໻䚼ߚᅲ⦄ॳ⧚䛑कߚㅔऩˈাᰃՓ⫼ } stmt.execute(sql.toString()); Statement stmt = database.getStatement(); sql.append("DROP TABLE IF EXISTS " + this.name); StringBuffer sql = new StringBuffer(); public void dropTable() { //ߴ䰸ϔϾᴀ㉏ᇍᑨⱘ㸼 ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\object\list\TableData.java ᮍ⊩DŽ ߴ䰸㸼ᅲ⦄कߚㅔऩˈা䳔㽕ᠻ㸠 DROP TABLE ेৃᅲ⦄ˈ೼ TableData ЁࡴܹϔϾ dropTable 13.8.11 ߴ䰸㸼 䎥ˈ᜶᜶ഄϔϾϔϾᴹᅲ⦄DŽ ׂᬍ㸼᮴䆎Ң⬠䴶᪡԰ࠄᣐ㺙 SQL 䛑↨䕗໡ᴖˈᅲ⦄䍋ᴹ↨䕗咏⚺DŽ೼ᅲ⦄ⱘ䖛⿟Ёৃҹᇣℹᖿ TABLE DROP FOREIGN KEYDŽ ᴹⱘ໪䬂ߴ䰸ˈݡ䞡ᮄᓎϔ⃵໪䬂ˈ䞡ᮄᓎ໪䬂ⱘᮍ⊩Ϣ⏏ࡴ໪䬂ⱘ乎ᑣϔ㟈DŽߴ䰸໪䬂Փ⫼ ALTER ᇚॳܜTABLE ADD INDEX˅ˈݡՓ⫼ ALTER TABLE ADD FOREIGN KEYDŽབᵰᰃׂᬍ໪䬂ˈህ䳔㽕 Ў㑺ᴳᄫ↉ࡴ㋶ᓩ˄ALTERܜߴ䰸ᄫ↉Փ⫼ ALTER TALBE DROP COLUMNDŽབᵰ⏏ࡴ໪䬂ˈ䳔㽕 Փ⫼ ALTER TABLE ADD ᴹ⏏ࡴᄫ↉ˈ䙷Мབᵰׂᬍњᄫ↉ˈህՓ⫼ ALTER TABLE CHANGEˈ } return result; } result.add(sql.toString()); if (f.isPrimaryKey()) sql.append(", ADD PRIMARY KEY (" + f.getFieldName() + ")"); if (f.isAutoIncrement()) sql.append(" AUTO_INCREMENT"); if (f.getDefaultValue() != null) sql.append(" DEFAULT '" + f.getDefaultValue() + "'"); /Download at http://www.pin5i.com ঺໪ˈ䳔㽕Ў DataCell ᇍ䈵䞡ݭ toString ᮍ⊩ˈ䖨ಲ䆹ᇍ䈵ⱘ value ؐDŽ䙷М៥ӀৃҹՓ⫼ҹϟҷ private String value; Ḑⱘؐܗ //䆹ऩ private DataColumn column; Ḑ᠔೼ⱘ߫ܗ //䆹ऩ private int row; Ḑ᠔೼ⱘ㸠ܗ //䆹ऩ DataCell ࣙᣀབϟሲᗻ˖ private String text; //䆹߫ⱘৡ⿄ private int index; //䆹߫೼ JTable Ёⱘ㋶ᓩ DataColumn ࣙᣀབϟሲᗻDŽ Ḑᇍ䈵DŽܗᓎϔϾ DataColumn ᇍ䈵ˈ㸼⼎ϔϾ᭄᥂߫ˈᮄᓎϔϾ DataCell ᇍ䈵ˈ㸼⼎ϔϾ᭄᥂ऩ ᴀゴՓ⫼ϔϾ DataTable ᇍ䈵㸼⼎ϔϾ᭄᥂߫㸼ˈ䆹ᇍ䈵㒻ᡓ JTableˈ೼ 13.2.3 ЁᏆ㒣߯ᓎDŽᮄ getColumnCount ᮍ⊩ᴹᕫࠄ᭄߫DŽ ᅮⱘˈᕫࠄϔϾ ResultSet ⱘ᭄߫ˈৃҹՓ⫼ ResultSetMetaData ᇍ䈵ⱘއ᭄߫⬅ ResultSet ᴹ г᳝ৃ㛑ᰃ㾚೒ⱘ᭄᥂DŽDataFrame Ё໘⧚᭄᥂ⱘᯊ׭䳔㽕⊼ᛣⱘᰃˈJTable ⱘ߫гᰃࡼᗕⱘˈᅗⱘ ᴹ⑤ˈা䳔㽕䋳䋷᭄᥂ᰒ⼎ህৃҹњˈ䖭ѯ᭄᥂ᴹ⑤াᇕ㺙೼ϔϾ QueryObject Ёˈ᳝ৃ㛑ᰃ㸼ⱘ᭄᥂ˈ Ў DataFrame ᦤկϔϾᵘ䗴఼ˈখ᭄ᰃ᥹ষ QueryObjectˈ䙷М DataFrame ህৃҹϡ⫼݇ᖗ᭄᥂ } //ⳕ⬹݊Ҫҷⷕ this.queryObject = queryObject; public DataFrame(QueryObject queryObject) { ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\DataFrame.java DataFrame ЁDŽ 㓪ݭᅠ ViewDataǃTableData ੠ QueryData ᇍ᥹ষ QueryObject ⱘᮍ⊩ৢˈህৃҹᇚ᭄᥂ᰒ⼎ࠄ } this.database = database; this.sql = sql; public QueryData(Database database, String sql) { private Database database; //ᇍᑨⱘ᭄᥂ᑧᇍ䈵 private String sql; //ℸ⃵ᶹ䆶ⱘ SQL 䇁হ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\object\list\QueryData.java ಴ℸ䳔㽕Ў䆹㉏ᦤկᵘ䗴఼DŽ Database ᇍ䈵ˈ಴ℸৃҹⳈ᥹Փ⫼ JDBC এᠻ㸠ˈԚᰃᮄᓎⱘ QueryData ᇍ䈵ህϡሲѢӏԩⱘ᭄᥂ᑧˈ 䇁হህৃҹҢ㾚೒੠㸼ЁᕫࠄⳌᑨⱘ ResultSet ᇍ䈵DŽ೼ᴀゴЁˈViewData ੠ TableData 䛑ֱᄬϔϾ ᥹ষDŽQueryObject ᥹ষা䳔㽕ᅲ⦄ᕫࠄ᭄᥂ⱘᮍ⊩ˈ೼ ViewData ੠ TableDataˈা䳔㽕Փ⫼ SELECT Ёˈ಴ℸ䖬䳔㽕ᓎゟϔϾ QueryData ⱘᇍ䈵ˈ㸼⼎ϔ⃵ᶹ䆶᠔ᰒ⼎ⱘ᭄᥂ᇍ䈵ˈৠḋএᅲ⦄ QueryObject 䰸њ ViewData Ϣ TableData ໪ˈᔧᠻ㸠ϔѯ SQL 䇁হⱘᯊ׭ˈг䳔㽕ᇚ᭄᥂ᰒ⼎ࠄ DataFrame } String getQuerySQL(String orderString); //䖨ಲᶹ䆶ⱘ SQL String getQueryName(); //ᕫࠄᶹ䆶ⱘৡ⿄ ResultSet getDatas(String orderString); ᕫࠄ᭄᥂// /Download at http://www.pin5i.com private String getSelectColumnIdentifier() { //䖨ಲ߫ⱘৡ⿄ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\table\DataTable.java Փ⫼ SQL Ёⱘ ORDER BY 䇁হᇍ᭄᥂䖯㸠ᥦᑣDŽ ׭ˈselectColumn ህㄝѢ݋ԧⱘᶤ߫㋶ᓩˈ಴ℸህৃҹᕫࠄ䖭ϔ߫ⱘৡ⿄˄᭄᥂ᑧⱘᄫ↉ৡ˅ˈ✊ৢݡ ׭ˈDataTable ЁֱᄬϔϾ selectColumn ⱘؐˈ㸼⼎⫼᠋ᔧࠡ᠔䗝ᢽⱘ߫㋶ᓩˈᔧ⫼᠋䗝ᢽњᭈ߫ⱘᯊ ៥Ӏ೼ 13.2.3 ЁˈᏆ㒣ᅲ⦄њ DataTable ⱘᭈ߫䗝ᢽ˄݋ԧ䇋ⳟ 13.2.3 ゴ㡖˅ˈᅲ⦄ᭈ߫䗝ᢽⱘᯊ 13.9.3 ᭄᥂ᥦᑣ refresh ᮍ⊩ᇍ߫㸼䖯㸠ࠋᮄDŽ 䰸њࠋᮄ᭄᥂໪ˈ䖬᳝䰡ᑣϢछᑣⱘࡳ㛑ˈᔧ⫼᠋ᇍ᭄᥂䖯㸠њ䰡ᑣ៪㗙छᑣ᪡԰ᯊˈህৃҹ䇗⫼ } setTableColumn(this.table); //䆒㕂↣ϔ߫ⱘᆑ this.model.setDataVector(getDatas(), this.columns.toArray()); //䆒㕂᭄᥂ࠄ߫㸼Ё this.columns = getColumns(this.rs); //ᕫࠄܼ䚼߫ this.rs = this.queryObject.getDatas(this.orderString); //᳈ᮄ᭄᥂ private void refresh() { //ࠋᮄ᭄᥂ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\DataFrame.java 䳔㽕䞡ᮄ䇏পϔ⃵᭄᥂ˈेᠻ㸠 QueryObject ⱘ getDatas ᮍ⊩ˈݡ䞡ᮄᇚ᭄᥂䆒㕂ࠄ߫㸼ЁेৃDŽ ೼ DataFrame ЁˈᏆ㒣ֱᄬ᳝ϔϾ QueryObject ⱘᇍ䈵ˈབᵰ䳔㽕ᇍ⬠䴶Ёⱘ᭄᥂䖯㸠ࠋᮄˈা 13.9.2 ࠋᮄ᭄᥂ ҔМᮍᓣ䖯᭄ܹ᥂ᰒ⼎⬠䴶ˈ䛑䳔㽕䞡ᮄ߯ᓎϔϾ DataFrame ᇍ䈵DŽ DefaultTableModel ⱘ setDataVector ᮍ⊩ᇚ᭄᥂Ϣ߫ⱘֵᙃ䆒㕂ࠄ߫㸼ЁDŽ䖭䞠䳔㽕⊼ᛣⱘᰃˈϡㅵҹ ㋴ᰃ DataCell ᭄㒘DŽ᳔ৢ೼䆒㕂߫㸼᭄᥂ᯊ׭ˈৃҹܗ⊼ᛣ㒧ᵰ䲚ড়ᰃϔϾ List ᇍ䈵ˈList ᇍ䈵䞠䴶ⱘ 䘡ग़ϔϾ ResultSet ᇍ䈵ˈ䘡ग़᠔᳝ⱘ߫ˈ᳔ৢᵘ䗴ϔϾ DataCell ᇍ䈵ˈᑊᄬᬒࠄ㒧ᵰⱘ䲚ড়Ёˈ } datas.add(data); } data[j] = dc; this.rs.getString(column.getText())); DataCell dc = new DataCell(i, column, Ḑᇍ䈵ܗ //߯ᓎऩ DataColumn column = this.columns.get(j); //ᕫࠄ݋ԧⱘᶤϔ߫ for (int j = 0; j < columnCount; j++) { Ḑᇍ䈵ܗ //䘡ग़߫, ߯ᓎ↣ϔϾऩ DataCell[] data = new DataCell[columnCount]; while (this.rs.next()) { ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\DataFrame.java ⷕᴹ߯ᓎ߫㸼ⱘ᭄᥂DŽ /Download at http://www.pin5i.com showMessage(e.getMessage(), "䫭䇃"); e.printStackTrace(); } catch (Exception e) { dataFrame.setVisible(true); DataFrame dataFrame =new DataFrame(queryData); //ᰒ⼎ DataFrame QueryData queryData = new QueryData(this.database, sql); //ᇕ㺙ϔϾ QueryData ᇍ䈵 try { String sql = this.editArea.getText(); //ᕫࠄ SQL private void query() { //ᶹ䆶 ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\QueryFrame.java DataFrameˈQueryData ᰃ QueryObject ᥹ষⱘ݊ЁϔϾᅲ⦄DŽ ᇚᶹ䆶ᇕ㺙៤ϔϾ QueryData ᇍ䈵˄13.9.1 Ё߯ᓎ˅ˈݡᇚ QueryData ᇍ䈵԰Ўᵘ䗴খ᭄Ӵ䗦㒭 䖛⿟៪㗙ߑ᭄˅䇁হˈ䙷Мህ䳔㽕ټ⼎ߎᴹˈབᵰ⫼᠋䕧ܹⱘᰃ SELECT˄ᶹ䆶䇁হ˅ǃCALL˄䇗⫼ᄬ ᠔⼎DŽབᵰ⫼᠋䕧ܹⱘᰃ INSERTǃUPDATE ㄝ䇁হˈ䙷Мህৃҹᇚᠻ㸠㒧ᵰⳈ᥹Փ⫼᱂䗮ⱘᦤ⼎Ḛᰒ ೼ 13.2.8 Ёˈ⫼᠋া䳔㽕䕧ܹⳌ݇ⱘ SQL 䇁হˈህৃҹᠻ㸠䆹 SQL 䇁হˈབ 13.2.8 Ёⱘ೒ 13.l0 13.10.1 䖤㸠SQL ⱘ⬠䴶೼ᴀゴЁЎ QueryFrame ㉏DŽ 㸠 SQL Ϣֱᄬ SQL ⱘࡳ㛑ˈ⬅Ѣ៥ӀᏆ㒣ᅲ⦄њ᭄᥂⌣㾜ⱘᰒ⼎ˈ಴ℸᅲ⦄䍋ᴹकߚㅔऩDŽᠻ㸠 SQL ᠻ㸠 SQL ⱘ⬠䴶Ꮖ㒣೼ 13.2.8 Ёᅲ⦄ˈাᰃϔϾㅔऩⱘ᭛ᴀඳ䅽⫼᠋䕧ܹ SQLˈ✊ৢᦤկϔϾᠻ 13.10 ᠻ㸠SQL ϔ㟈ˈাᰃՓ⫼ SQL ⱘ ORDER BY ASC ेৃᅲ⦄DŽ 䇁হˈ಴ℸা䳔㽕䞡ᮄᠻ㸠 QueryObject ⱘ getDatas ᮍ⊩ेৃᕫࠄᥦᑣৢⱘ᭄᥂DŽछᑣⱘᅲ⦄Ϣ䰡ᑣ ҹϞҷⷕᅲ⦄䰡ᑣDŽ೼᥹ষ QueryObject ⱘ getDatas ᮍ⊩Ёˈ䳔㽕ᦤկখ᭄ orderStringˈेᥦᑣ } refresh(); //ࠋᮄ߫㸼 this.orderString = column + " " + MySQLUtil.DESC; if (column == null) return; //≵᳝䗝Ёᭈ߫, ϡᥦᑣ String column = getSelectColumnIdentifier(); //ᕫࠄᄫ↉ৡ⿄ private void desc() { //䰡ᑣ } return (String)colModel.getColumn(selectIndex).getIdentifier(); DefaultTableColumnModel colModel = (DefaultTableColumnModel)this.table.getColumnModel(); if (selectIndex == -1) return null; int selectIndex = this.table.getSelectColumn(); ᕫࠄ䗝Ё߫㋶ᓩ// /Download at http://www.pin5i.com Փ⫼ mysql ੑҸᠻ㸠 SQL ᭛ӊˈmysql ੑҸ᳝䆌໮ⱘখ᭄ˈҹϞ䇁Փ⫼њ-uǃ-pǃ-h ੠-D খ᭄ˈ "MySQL ᅝ㺙Ⳃᔩ\bin\mysql" –u ⫼᠋ৡ –p ᆚⷕ –h 䖲᥹ IP –D ᭄᥂ᑧৡ⿄ < "SQL ᭛ӊ㒱ᇍ䏃ᕘ" ՟བ MySQL Ёᄬ೼ϔϾ᭄᥂ᑧˈབᵰ䳔㽕ᠻ㸠ᶤӑ SQL ᭛ӊˈህ䳔㽕ᠻ㸠ҹϟ䇁হ˖ ᠻ㸠ⱘᮍᓣˈᇚ೼ϟ䴶䆆㾷DŽ GlobalContext ⱘᇍ䈵Ёˈ䙷Мབᵰ䳔㽕Փ⫼ mysql ੑҸᠻ㸠 SQL ᭛ӊˈⳈ᥹ᣐ㺙ੑҸᠻ㸠ेৃDŽ㟇Ѣ ೼ 13.3 ᇣ㡖Ёˈ៥Ӏᅲ⦄њ⫼᠋䗝ᢽ MySQL ᅝ㺙Ⳃᔩⱘࡳ㛑ˈᑊϨᇚ⫼᠋䗝ᢽⱘⳂᔩᄬᬒࠄϔϾ ੑҸЏ㽕⫼Ѣᇐߎ SQL ᭛ӊˈmysql ੑҸЏ㽕⫼Ѣᠻ㸠 SQL ᭛ӊDŽ ৃҹᡒࠄ MySQL ᅝ㺙Ⳃᔩϟ䴶ⱘ bin Ⳃᔩˈ✊ৢᡒࠄ mysqldump Ϣ mysql 䖭ϸϾੑҸDŽmysqldump 䖯ܹㅵ⧚఼ⱘ㄀ϔϾ⬠䴶ˈህ䳔㽕⫼᠋䗝ᢽ MySQL ⱘᅝ㺙Ⳃᔩˈḍ᥂⫼᠋᠔䗝ᢽⱘⳂᔩˈ៥Ӏህ 13.11.1 ᠻ㸠SQL᭛ӊ ᦤկ哴ᷛে䬂㦰ऩˈᦤկᇐߎࡳ㛑DŽে䬂㦰ऩᏆ㒣೼ 13.2.9 Ϣ 13.2.10 Ёᅲ⦄DŽ ህᦤկ哴ᷛে䬂㦰ऩˈ䅽⫼᠋ৃҹᠻ㸠᭄᥂ᑧⱘᇐߎϢᇐܹ᪡԰DŽᔧ⫼᠋䗝ᢽњᶤϾ㸼ⱘᯊ׭ˈህৃҹ SQL ᭛ӊⱘᇐܹϢᇐߎˈࣙᣀ᭄᥂ᑧⱘᇐܹϢᇐߎǃ㸼ⱘᇐߎDŽᔧ⫼᠋䗝ᢽњᶤϾ᭄᥂ᑧⱘᯊ׭ˈ 13.11 SQL᭛ӊⱘᇐܹϢᇐߎ ᓣˈህᰃ䗮䖛ᇐܹ᭛ӊᴹᠻ㸠ϔӑ SQL ᭛ӊˈϟϔᇣ㡖ᇚ䆆㾷བԩᅲ⦄ SQL ᭛ӊⱘᇐܹϢᇐߎDŽ ᠻ㸠 SQL ⱘࡳ㛑Ꮖ㒣ᅠ៤ˈ䖭ᰃ⫼᠋䕧ܹ SQL ݡ䖤㸠ⱘϔ⾡᪡԰ᔶᓣˈ⫼᠋䖬᳝঺໪ϔ⾡᪡԰ᔶ } FileUtil.writeToFile(file, content); //ᇚݙᆍݭࠄ᭛ӊЁ String content = this.editArea.getText(); public void writeToFile(File file) { //ݭܹ᭛ӊ ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\ui\QueryFrame.java } writer.close(); writer.write(content); FileWriter writer = new FileWriter(file); //ݭܹ᭛ӊ createNewFile(file); //߯ᓎᮄ᭛ӊ public static void writeToFile(File file, String content) { //߯ᓎ᭛ӊᑊᇚ content ݭࠄ File Ё ҷⷕ⏙ऩ˖code\mysql-manager\src\org\crazyit\mysql\util\FileUtil.java ৢᦤկϔϾ᭛ӊ䗝ᢽ఼䅽⫼᠋䗝ᢽˈ᳔ৢՓ⫼ IO ⌕ᇚݙᆍ䕧ܹࠄ᭛ӊህৃҹᅲ⦄DŽ ֱᄬ SQLˈাᰃᇚ⫼᠋䕧ܹⱘ SQL 䇁হֱᄬࠄϔӑ᭛ӊЁेৃDŽҢ⬠䴶ᕫࠄ⫼᠋䕧ܹⱘ᭄᥂ˈ✊ 13.10.2 ֱᄬSQL ᥹ᔍߎᠻ㸠㒧ᵰⱘᦤ⼎˄៤ࡳϢ৺˅DŽ ߭ⳈމҹϞⱘ query ᮍ⊩ˈা᳝ᔧ⫼᠋䕧ܹ᳝ SELECT ៪㗙 CALL ݇䬂ᄫⱘᯊ׭ᠡ䇗⫼ˈ݊Ҫᚙ { { /Download at http://www.pin5i.com ߎࡳ㛑᳈ࡴᔎ໻ⱘ MySQL ㅵ⧚఼DŽ ᑊϡᰃϢ䖭ѯଚϮㅵ⧚఼ゲѝˈ㗠ᰃ䅽䇏㗙њ㾷ㅵ⧚఼ᅲ⦄ⱘॳ⧚DŽᏠᳯ໻ᆊ㛑೼ᴀゴⱘ෎⸔Ϟˈᓔথ ៥ӀгৃҹՓ⫼⿟ᑣᴹᇍ᭄᥂ᑧ䖯㸠ߚᵤˈݡݭࠄ SQL ᭛ӊЁˈ䖭ḋгᰃϔ⾡ᅲ⦄䗨ᕘDŽᴀゴⱘⳂⱘ Ⳃᔩ˄៪㗙ḍᴀϡ䳔㽕ᅝ㺙 MySQL˅ˈՓ⫼⿟ᑣᴹᇐߎ SQL ᭛ӊDŽᔧ⫼᠋䳔㽕ᇐߎ SQL ᭛ӊⱘᯊ׭ˈ ⳂⱘЎњՓ⫼ MySQL ⱘϔѯݙ㕂ੑҸˈԚᰃ՟བᑇᐌՓ⫼ⱘ Navicatˈᅗᑊϡ䳔㽕ⶹ䘧 MySQL ⱘᅝ㺙 ѯ⌕㸠ⱘㅵ⧚఼᳝ⴔ䚼ߚⱘऎ߿ˈ՟བ៥Ӏ೼䖯ܹㅵ⧚఼ⱘᯊ׭ˈ䳔㽕⫼᠋䗝ᢽ MySQL ⱘᅝ㺙Ⳃᔩˈ ㅵ⧚఼ࡳ㛑ᑊϡೄ䲒ˈೄ䲒ⱘᰃϔѯ⬠䴶ⱘѸѦˈ⡍߿ᰃ㸼ㅵ⧚⬠䴶DŽᴀゴⱘ䖭Ͼ MySQL ㅵ⧚఼Ϣϔ SQL ᭛ӊⱘᇐߎ੠ᇐߎDŽ៥Ӏৃҹ೼ㅵ⧚఼ᅲ⦄ⱘ䖛⿟Ёˈњ㾷 MySQL ㅵ⧚఼ⱘᅲ⦄ॳ⧚DŽᅲ⦄ MySQL ㋴ⱘㅵ⧚ǃ᭄᥂⌣㾜Ϣܗᴀゴᅲ⦄њϔϾ MySQL ㅵ⧚఼ˈ䖭Ͼㅵ⧚఼Ё᳝໮Ͼࡳ㛑ˈࣙᣀ᭄᥂ᑧ 13.12 ᴀゴᇣ㡖 ⦄˄⫳៤ϔӑ bat ᭛ӊᑊᠻ㸠ǃ᳔ৢߴ䰸˅DŽ ⳳℷᠻ㸠䖭ѯ䇁হ⬅ CommandUtil Ёⱘ executeCommand ᮍ⊩ᠻ㸠ˈ䆹ᮍ⊩ᦤկњ Windows ϟⱘᅲ BackupHandlerImpl ⱘᅲ⦄㉏ˈᏆ㒣ᇍ SQL ᭛ӊⱘᇐߎ੠ᇐߎ䖯㸠ᅲ⦄ˈ䖭ѯᅲ⦄াᰃᣐ㺙ϔѯ䇁হˈ ೼ᴀゴЁˈ໘⧚ SQL ᭛ӊⱘᇐܹϢᇐߎ⬅ BackupHandler ᥹ষᅠ៤ˈ䆹᥹ষ᳝ϔϾ ᇐߎⱘ SQL ᭛ӊֱᄬⳂᔩ" "MySQL ᅝ㺙Ⳃᔩ\bin\mysqldump" -u ⫼᠋ৡ -p ᆚⷕ -h 䖲᥹ IP --databases ᭄᥂ᑧৡ⿄ --tables 㸼ϔ 㸼 N > " 䳔㽕ᇐߎⱘ㸼ेৃDŽ໮Ͼ㸼П䯈Փ⫼ぎḐᇚ㸼ৡߚ䱨DŽᇐߎ㸼Փ⫼ⱘ mysqldump 䇁হḐᓣབϟ˖ ᇐߎ㸼Ϣᇐߎ᭄᥂ᑧᅲ⦄ϔ㟈ˈা䳔㽕೼ᇐߎ᭄᥂ᑧⱘখ᭄ⱘ෎⸔ϞˈݡࡴϞ—tables খ᭄ᴹໄᯢ sh ᭛ӊЁˈݡՓ⫼ Runtime ⱘ exec ᮍ⊩ᠻ㸠DŽ ⱘ᭄᥂ᑧৡ⿄DŽᇐߎ SQL ᭛ӊϢᠻ㸠 SQL ᭛ӊϔḋˈ䛑ᰃᇚᣐ㺙ⱘੑҸՓ⫼ IO ⌕ݭࠄϔӑ bat ៪㗙 ᠋ৡǃᆚⷕ੠᭄᥂ᑧ᳡ࡵ఼ IPˈ--force খ᭄㸼⼎থ⫳䫭䇃ᇚ㒻㓁ᠻ㸠ˈ--database খ᭄㸼⼎䳔㽕ᇐߎ ҹϞՓ⫼њ mysqldump 䲚ড়ⱘ-uǃ-pǃ-hǃ--force ੠—databases খ᭄ˈ-uǃ-p ੠-h ߚ߿ҷ㸼⫼ ᄬⳂᔩ" "MySQL ᅝ㺙Ⳃᔩ\bin\mysqldump" -u ⫼᠋ৡ -p ᆚⷕ -h 䖲᥹ IP --force --databases ᭄᥂ᑧৡ > "ᇐߎⱘ SQL ֱ ᇐߎ᭄᥂ᑧϢᠻ㸠 SQL ᭛ӊϔḋˈՓ⫼ mysqldump ੑҸेৃᅲ⦄DŽmysqldump 䇁হḐᓣབϟ˖ 13.11.2 ᇐߎ᭄᥂ᑧϢ㸼 ēࢶྻಓю sh ำߑdܥರဈ Linux ԅڴಾၽ Windows Ѐᆴຂහຏēఢ ರဈ Runtime ԅ exec ֺ֥ᄓ໻ġqcmd /c bat ำߑൎၽrࢶ່҂Ӑ๢dӲ௶ēუ܊௶bat ำߑᄯē.ךྡྷ ୯ࣿēӬಾᄐࠄರဈ Runtime ԅ exec ᄓ໻ఏ௶ݖѻӐēแਬࢶྻߜଳᅭԅઊࡳ੡গēರဈ IO ঠໟదӾ ೎ຕ༓ྑߜ mysql ੡গď̝ͧࣳᅭંৃĐರဈഀ࿤ۜࣳ୯ࣿēၼߜ SQL ำߑԅࢇճাࡅರဈഀ࿤ۜࣳ MySQL ̝ᅭંৃပࣂٌēݧრ SQL ำߑԅࢇճাࡅပࣂٌē ڴᅟ࿉ġუऺ༓ྑඋιഊੜԅಾēఢ ᣐ㺙དҹϞⱘ䇁হৢˈৃҹՓ⫼ Runtime ㉏ⱘ exec ᮍ⊩ᠻ㸠DŽ -u খ᭄㸼⼎᭄᥂ᑧⱘ⫼᠋ৡˈ-p 㸼⼎ᆚⷕˈ-h 㸼⼎䖲᥹ⱘ᳡ࡵ఼ IPˈ-D 㸼⼎䳔㽕ᠻ㸠᭛ӊⱘ᭄᥂ᑧˈ ㄀ 13 ゴ MySQL ㅵ⧚఼g48g /Download at http://www.pin5i.com ᑊЎ䖭ѯᅲ՟䆒㕂Ⳍᑨⱘሲᗻˈ಴ℸ៥Ӏ䳔㽕Փ⫼ Java ⱘডᇘᴎࠊDŽ ೼ᴀゴЁˈ៥Ӏ䳔㽕೼䖤㸠ᯊএࡼᗕⱘࡴ䕑䜡㕂᭛ӊˈݡḍ᥂䖭ѯ䜡㕂᭛ӊএ߯ᓎᶤϔϾ㉏ⱘᅲ⦄ˈ ϔϾ㉏ⱘᅲ՟ˈ೼䖤㸠ᯊ䇗⫼ӏᛣϔϾᇍ䈵ⱘᮍ⊩DŽ 䆹ᴎࠊৃҹ䅽៥ӀࡼᗕⱘᕫࠄᶤϾ㉏ൟⱘሲᗻǃᵘ䗴఼੠ᮍ⊩DŽJava ⱘডᇘᴎࠊৃҹ೼䖤㸠ᯊᵘ䗴ᶤ ೼ Java 䖤㸠ᯊ⦃๗Ёˈབᵰ៥Ӏ䳔㽕ᕫࠄᶤϔϾ㉏ⱘ݋ԧֵᙃˈ䙷МህৃҹՓ⫼ Java ⱘডᇘᴎࠊˈ 14.2.1 Javaডᇘㅔҟ ‰ Junit ‰ dom4j ‰ Java ⱘডᇘᴎࠊ ೼ᴀゴᓔথ IoC ᆍ఼ᯊˈ᠔⍝ঞⱘᡔᴃ᳝བϟ޴⾡˖ 14.2 Փ⫼ᡔᴃㅔҟ ϔϾᭈড়ⱘ՟ᄤˈᴹ䇈ᯢ IoC ᆍ఼㒭៥ӀᏺᴹҔМḋⱘད໘DŽ ݭϔϾㅔऩⱘ IoC ᆍ఼ˈᴹ䇈ᯢ IoC ᆍ఼ᰃབԩᐂ៥Ӏ߯ᓎ㉏ⱘᅲ՟ǃབԩᅲ⦄ձ䌪⊼ܹˈ᳔ৢᑊ䗮䖛 བᵰϔ䯂 IoC ᆍ఼ᰃབԩᐂ៥Ӏ߯ᓎ㉏ᅲ՟ⱘˈᕜ໮䛑ϡⶹ䘧བԩಲㄨDŽ೼ϟ䴶ゴ㡖Ёˈ៥Ӏᇚ䗮䖛㓪 ヨ㗙䴶䆩䖛ϔѯᎹ԰њ޴ᑈⱘ⿟ᑣਬˈ䖭ѯ⿟ᑣਬᕜ໮䛑䇈㞾Ꮕ㊒䗮 Springˈ㊒䗮 IoC ᆍ఼ˈԚᰃ ఼ǃgoogle-guiceˈapache ⱘ HiveMind ㄝDŽ ⦄ˈ㗠ϡ䳔㽕݇⊼৘Ͼ㒘ӊⱘձ䌪ⱘ݇㋏DŽᐌ⫼ⱘ IoC ᆍ఼᳝ Spring ⱘ IoC ᆍ఼ǃwebwork ⱘ IoC ᆍ ЁՓ⫼ IoC ⱘὖᗉˈৃҹᇚ৘Ͼࡳ㛑ϡৠⱘ㒘ӊ㒳ϔᬒࠄ IoC ᆍ఼Ёˈ⿟ᑣਬা䳔㽕݇⊼৘Ͼ㒘ӊⱘᅲ ᇚ৘Ͼ㉏П䯈ⱘձ䌪݇㋏ড᯴ࠄ IoC ᆍ఼Ёˈৃҹ᳈ࡼᗕǃ♉⌏੠䗣ᯢⱘㅵ⧚৘Ͼᇍ䈵DŽ೼ J2EE 乚ඳ ᮴䆎ᰃ᥻ࠊড䕀䖬ᰃձ䌪⊼ܹˈ䞛⫼䖭⾡ᮍᓣᴹ߯ᓎ㉏ⱘᅲ՟ˈৃҹՓᕫ៥Ӏⱘҷⷕ᳈ࡴ⏙᱄ᯢњˈ 䙷Мৃҹ೼ IoC ᆍ఼Ё৥䆹㉏⊼ܹ䳔㽕㹿䇗⫼ⱘᅲ՟DŽ Ϣ᥻ࠊড䕀ᰃৠϔϾὖᗉˈ៥Ӏ߯ᓎᶤϾ㉏ⱘᅲ՟⬅ IoC ᆍ఼ᅠ៤ˈབᵰ䆹㉏䳔㽕Փ⫼঺໪ⱘ㉏ᅲ՟ˈ ᧲⏙Ἦњ᥻ࠊড䕀ⱘὖᗉৢˈ៥Ӏݡᴹњ㾷ҔМিձ䌪⊼ܹDŽձ䌪⊼ܹ˄Dependency Injection˅ ᅮDŽއᅮˈ㗠ϡᰃ⬅䆹㉏އ఼এ Ͼ㉏䳔㽕঺໪ϔϾ㉏ⱘᅲ՟ˈৃҹ䗮䖛ᶤϾᆍ఼㦋ᕫˈ㗠ϡ೼㉏ⱘݙ䚼߯ᓎˈᕫࠄҔМḋⱘᅲ՟ˈ⬅ᆍ ໪ⱘ㉏ᯊˈৃҹ䗮䖛ϔѯ䜡㕂ᴹᕫࠄ䆹㉏ⱘᅲ⦄DŽIoC ᰃ Inversion of Control ㅔ⿄ˈজ⿄᥻ࠊড䕀ˈϔ њ䖭Ͼ䯂乬ˈᔧ៥Ӏ䳔㽕೼ϔϾ㉏ЁՓ⫼঺އ㉏ˈህৃҹᕫࠄ䆹ᅲ՟ਸ਼˛IoC ⱘὖᗉᕜདⱘᐂࡽ៥Ӏ㾷 䗮䖛 new ⱘ݇䬂ᴹ߯ᓎ䆹㉏ⱘᅲ՟ˈ䙷М᳝≵᳝᳈དⱘᮍᓣˈৃҹϡ೼៥ӀⱘҷⷕЁⳈ᥹এ new 䖭Ͼ ೼ᑇᯊⱘᓔথЁˈᔧ៥Ӏℷ೼㓪ݭⱘᶤϔϾ㉏䳔㽕⫼ࠄ঺໪ⱘϔϾ㉏˄㒘ӊ˅ⱘᯊ׭ˈ៥Ӏ䛑䳔㽕 14.1 IoCㅔҟ ㄀  ゴ㞾Ꮕᓔথ,R&ᆍ఼ /Download at http://www.pin5i.com ׭ˈህ䞡ᮄ߯ᓎϔϾDŽ៥ӀЎ bean ⱘ䜡㕂⏏ࡴϔϾ singleton ሲᗻˈ⫼ᴹໄᯢ bean ᰃ৺ЎऩᗕDŽ ᑊᇚ䆹ᅲ՟㓧ᄬ䍋ᴹˈབᵰ䆹 bean 䜡㕂៤䴲ऩᗕⱘ䆱ˈ䙷Мህϡ䖯㸠㓧ᄬˈ↣⃵䇋∖䖭Ͼ bean ⱘᯊ ᰃ৺Ўऩᗕˈབᵰ䆹 bean ᰃऩᗕⱘ䆱ˈ䙷М IoC ᆍ఼೼ਃࡼⱘᯊ׭ˈህাӮЎ䆹 bean ߯ᓎϔϾᅲ՟ˈ ᅮНϔϾ bean 䰸њ䳔㽕ᰒᓣໄᯢ䆹 bean ⱘৡ⿄੠㉏ൟ໪ˈ䖬䳔㽕ਞ䆝៥Ӏⱘ IoC ᆍ఼ˈ䆹 bean 14.3.2 ໄᯢऩᗕⱘbean java.util.DateDŽ ҹϞⱘ bean 䜡㕂㸼⼎៥Ӏ೼ IoC ᆍ఼Ё߯ᓎњϔϾ myDate ⱘ beanˈ䆹 bean ⱘ㉏ൟᰃ Ϣ㉏ൟˈ䖭ѯ៥Ӏⱘ IoC ᆍ఼ᕫࠄ䖭ѯ䜡㕂ৢˈህৃҹᐂ៥Ӏ߯ᓎ䖭ѯ㉏ⱘᅲ՟DŽ݋ԧⱘ䜡㕂བϟ˖ ᅮНདњ bean 㡖⚍དˈ៥Ӏ䳔㽕Ў bean 㡖⚍ᣛᅮϔϾৡᄫ੠㉏ৡˈ㸼⼎䖭Ͼ bean ᠔ᇍᑨⱘৡ⿄ Ͼ beanˈৃҹ䅽៥Ӏ䜡㕂݋ԧⱘᶤϾ㉏DŽXML ᭛ӊⱘ݋ԧ䜡㕂བϟ˖ ೼ᭈϾ IoC ᆍ఼Ёˈ↣ϔϾ bean ҷ㸼݋ԧⱘᶤϾ㉏ˈ಴ℸ៥Ӏⱘḍ㡖⚍Ў beansˈbeans ϟ᳝໮ 14.3.1 ໄᯢbean bean ᰃ৺Ўऩᗕˈᰃ৺䳔㽕ᓊ䖳ࡴ䕑ᅮDŽ⹂ᅮњ䜡㕂᭛ӊⱘݙᆍৢˈ៥Ӏᓔྟⴔ᠟㓪ݭ DTD ᭛ӊDŽ ᥂䖭ѯֵᙃএ߯ᓎⳌᑨⱘ beanDŽ䰸њᅮН bean ⱘৡᄫ੠㉏ৡ໪ˈ䖬䳔㽕ᅮНϔѯ݊Ҫⱘሲᗻˈ՟བ䆹 ՟བໄᯢ៥Ӏ䳔㽕߯ᓎાѯᇍ䈵˄bean˅ˈЎ䖭ѯᇍ䈵˄bean˅ᦤկৡᄫ੠㉏ৡˈ䅽៥Ӏⱘ IoC ᆍ఼ḍ ೼㓪ݭ IoC ᆍ఼Пࠡˈ៥Ӏ䳔㽕㓪ݭ XML ᭛ӊˈ䖭ѯ XML ᭛ӊ⫼ᴹᅮНҎӀ IoC ᆍ఼ⱘϔѯ䜡㕂ˈ 14.3 ⹂ᅮ䜡㕂᭛ӊݙᆍǃ㓪ݭDTD ᵰˈ៥Ӏᔧࠡ᠔Փ⫼ⱘᰃ Junit4DŽ ៥Ӏ乍Ⳃⱘথሩ᳝䭓䖰ⱘ߽ⲞDŽᴀゴⱘ䞡⚍ᰃ IoC ᆍ఼ˈՓ⫼䖭Ͼ⌟䆩ḚᶊˈⳂⱘᰃሩ⼎៥Ӏҷⷕⱘᬜ ໂˈ᳈ϡᚻӏԩⱘব᳈ˈᇍعབᵰキ೼ XP˄ᵕ䰤㓪⿟˅ⱘ㾦ᑺᴹ䆆ˈ㓪ݭ⌟䆩ৃҹ䅽៥Ӏⱘҷⷕ᳈ࡴ 䆩ˈϔᴹৃҹሩ⼎៥Ӏ᠔㓪ݭ⿟ᑣⱘᬜᵰˈѠᴹৃҹֱ䆕៥Ӏⱘ⿟ᑣⱘ䋼䞣DŽ⌟ܗ䳔㽕ᇍ䆹ࡳ㛑䖯㸠ऩ 䆩DŽ೼ᴀゴЁˈ៥Ӏ↣㓪ݭϔϾࡳ㛑⚍ˈህ⌟ܗ䆩Ḛᶊˈկ Java ⿟ᑣਬ㓪ݭऩ⌟ܗJunit ᰃϔϾऩ 14.2.3 Junit Ӏⱘ IoC ᆍ఼ⱘ䜡㕂᭛ӊˈ಴ℸՓ⫼ dom4j ህ䴲ᐌⱘড়䗖DŽ 䕏ᵒⱘ䇏পࠄⳌᑨⱘ XML ᭛ӊˈᑊৃҹᕫࠄ䖭ѯ xml ⱘⳌ݇ݙᆍDŽᴀゴЁ៥Ӏ䞛⫼њ XML ᭛ӊ԰Ў៥ ⫼ DomǃSAX ᴹ㾷ᵤ XML ᭛ӊDŽDom4j ⱘՓ⫼䴲ᐌㅔऩˈা䳔㽕Փ⫼䆹乍Ⳃ᠔ᦤկⱘ㾷ᵤ㉏ˈህৃҹ dom4j ᰃϔϾᐌ⫼ⱘ XML 㾷ᵤ乍Ⳃˈ䆹乍ⳂᰃϔϾᯧ⫼ⱘᓔ⑤乍Ⳃˈᅗᑨ⫼Ѣ Java ᑇৄˈᬃᣕՓ dom4j 14.2.2 /Download at http://www.pin5i.com 18 ⱘ bean ໪ˈ䖬ৃҹ৥ bean Ё⊼ܹϔѯ᱂䗮ⱘሲᗻ˖ ሲᗻˈproperty 㡖⚍ϟ䴶᳝ϔϾ ref ᄤ㡖⚍ˈref ᄤ㡖⚍ᣛ৥ᶤϔϾᆍ఼Ёⱘ beanDŽ䰸њ⊼ܹ঺໪ᅮН ⱘ䆒ؐ⊼ܹDŽ䙷М៥Ӏⱘ䜡㕂᭛ӊЁ䳔㽕Ў bean ࡴܹϔϾ property ⱘᄤ㡖⚍ˈ䆹㡖⚍䞠䴶᳝ϔϾ name student 䖭ϸϾ bean ህѻ⫳њձ䌪݇㋏ˈ䖭ḋⱘ⊼ܹ៥Ӏህিձ䌪⊼ܹˈҹϞⱘ⊼ܹᮍᓣᰃձ䌪⊼ܹЁ ҹϞⱘ䜡㕂Ёⱘ咥ԧ䚼ߚˈ៥ӀЎ student ⱘ bean ⊼ܹњϔϾ school ⱘ beanˈ䖭ḋˈschool ੠ ݡ㦋ᕫ䆹 bean 䜡㕂ⱘϔѯሲᗻˈ䗮䖛䆹 bean 䞠䴶ⱘ setter ᮍ⊩ˈ䆒ؐࠄ䆹 bean ⱘᅲ՟ЁDŽ ؐˈг᳝ৃ㛑ᰃ঺໪ⱘᅮНⱘ beanDŽ೼䖭䞠៥ӀՓ⫼䆒ؐ⊼ܹˈ䆒ؐ⊼ܹˈህᰃ߯ᓎњ bean ⱘᅲ՟ৢˈ ៥Ӏ䳔㽕Ў↣Ͼ bean ⊼ܹϔѯሲᗻˈ䅽䆹 bean ⱘᅲ՟ᕫࠄ䖭ѯሲᗻˈ䖭ѯሲᗻ᳝ৃ㛑ᰃϔѯ᱂䗮ⱘ IoC ᆍ఼䰸њৃҹᐂ៥Ӏ߯ᓎ bean П໪ˈ䖬ৃҹᐂࡽ៥Ӏᇚᇍᑨⱘሲᗻ䆒㕂ࠄ䆹 bean ⱘᅲ՟䞠䴶ˈ 14.3.4 ໄᯢ䆒ؐ⊼ܹࠄbeanⱘሲᗻ ϡᰒᓣᣛᅮⱘ䆱ˈৃҹՓ⫼ false ԰Ў咬䅸ؐDŽ Ѣ beans ⱘ䜡㕂ˈ㗠 beans ⱘ default-lazy-init ሲᗻއᣛᅮЎ defaultˈ㸼⼎䖭Ͼ bean ᰃ৺ᓊ䖳ࡴ䕑প ᅮᰃ৺䳔㽕ᓊ䖳ࡴ䕑DŽབᵰᇍ bean ⱘ lazy-init ሲᗻϡᣛᅮؐⱘ䆱ˈ䙷МህЎ䖭Ͼ lazy-init ሲᗻއᗻᴹ ҹϞⱘ䜡㕂ˈབᵰ myDate ⱘ bean ໄᯢЎ default ⱘ䆱ˈ䙷Мህᣝ✻ beans ⱘ default-lazy-init ሲ default ⱘᯊ׭ˈህՓ⫼ beans 㡖⚍˄ḍ㡖⚍˅ⱘ default-lazy-init ᠔䜡㕂ⱘؐ˖ ৃҹᦤկϔϾ default-lazy-init ⱘሲᗻˈ㸼⼎䖭ӑ䜡㕂᭛ӊϟⱘ᠔᳝ beanˈབᵰ lazy-init ሲᗻⱘؐЎ ҹϞⱘ䜡㕂ҷⷕЁˈໄᯢњ myDate 䳔㽕ᓊ䖳ࡴ䕑ˈЎ bean 㡖⚍ࡴњ䖭Ͼሲᗻৢˈbeans 㡖⚍г 䅽ᆍ఼ⶹ䘧៥Ӏ೼ᆍ఼߱ྟ࣪ⱘᯊ׭ˈᰃ৺䳔㽕߯ᓎDŽ៥ӀЎ bean ᦤկϔϾ lazy-init ⱘሲᗻ˖ ᔧ IoC ᆍ఼ਃࡼⱘᯊ׭ˈᆍ఼ᰃ৺䳔㽕偀Ϟ߯ᓎ䖭Ͼ beanˈ៥ӀৃҹЎ䜡㕂᭛ӊᦤկϔϾሲᗻˈ 14.3.3 ໄᯢᓊ䖳ࡴ䕑 ໛ DTD ᭛ӊЁ᳝䆺㒚䇈ᯢDŽޚ ϟⱘؐЎ trueˈ㒭䜡㕂㡖⚍ᦤկ咬䅸ؐˈ೼ 14.4މᅮ singleton ሲᗻˈ䅽 singleton ሲᗻ೼ϡᰒᓣໄᯢⱘᚙ ϟˈ៥ӀৃҹϡᣛމҹϞ䜡㕂ⱘ咥ԧ䚼ߚˈ៥Ӏ䜡㕂њ myDate 䖭Ͼ bean Ў䴲ऩᗕˈ೼ϔ㠀ⱘᚙ beans>> /Download at http://www.pin5i.com ⱘ㑺ᴳ᭛ӊ˄DTD˅DŽ ⹂ᅮњ IoC ⱘ䜡㕂᭛ӊ䳔㽕䜡㕂ⱘݙᆍˈ䙷М᥹ϟᴹˈ៥Ӏህৃҹḍ᥂Ϟ䗄ⱘ䜡㕂ݙᆍˈ㓪ݭⳌ݇ ⼎ϡ䳔㽕㞾ࡼ㺙䜡DŽbean ⱘ autowire ሲᗻ咬䅸ؐЎ defaultDŽ 䆌Ў no ៪㗙 byNameDŽབᵰ autowire ⱘؐЎ no ⱘ䆱ˈ㸼ܕ೼ᴀ՟Ё autowire ⱘؐ៥ӀᅮНЎা Ў defaultˈ䙷Мህৃҹ䖯㸠㞾ࡼ㺙䜡DŽ 㡖⚍ᦤկϔϾ default-autowire ሲᗻˈໄᯢ䆹 beans ḍ㡖⚍ϟ䴶᠔᳝ⱘ bean 㡖⚍ˈབᵰ autowire ⱘؐ ህ䳔㽕㞾ࡼⱘᇚ School ⱘ bean 䗮䖛䆒ؐ⊼ܹ䆒㕂ࠄ Student ЁDŽϢᓊ䖳ࡴ䕑ϔḋˈгৃҹЎ beans student Ёˈབᵰ Student ㉏Ё᳝ϔϾ setSchool ⱘ setter ᮍ⊩ˈᑊϨখ᭄Ў Schoold ᇍ䈵ˈ䙷Мᆍ఼ ҹϞⱘ student ᦤկњ autowire ሲᗻˈ䆹ሲᗻؐЎ byNameˈ㸼⼎ḍ᥂ bean ⱘৡ⿄㞾ࡼ⊼ܹࠄ ⱘ bean ҹ䆒ؐ⊼ܹⱘᮍᓣձ䌪⊼ܹࠄⳂᷛⱘ bean ЁDŽ 㞾ࡼ㺙䜡ˈህᰃϡ䳔㽕ᣛᅮ bean ⱘሲᗻˈҢ IoC ᆍ఼Ёᶹᡒ bean ᠔䳔㽕ⱘሲᗻˈᇚ䖭ѯᶹᡒࠄ 14.3.6 㞾ࡼ㺙䜡 ㉏ൟⱘᵘ䗴খ᭄DŽ 䖭ḋⱘ䜡㕂ˈ៥ӀህৃҹЎ Student 䖯㸠ᵘ䗴⊼ܹˈᑊ೼߯ᓎ bean ⱘᅲ՟ᯊˈᦤկϔϾ Integer 20 ㋴ˈᵘ䗴⊼ܹϔѯ঺໪ⱘ᱂䗮ሲᗻDŽܗ ㋴໪ˈ䖬ৃҹᦤկ valueܗ এ䇗⫼ Student ⱘᵘ䗴఼DŽϢ 14.3.4 Ёⱘ䆒ؐ⊼ܹϔḋˈ䰸њৃҹᦤկ ref 䗴఼ˈᵘ䗴఼ⱘখ᭄Ў School ㉏ൟˈ䙷М IoC ᆍ఼೼߯ᓎᯊˈህӮḍ᥂ constructor-arg ໄᯢⱘሲᗻˈ ҹϞ䜡㕂ⱘ咥ԧ䚼ߚˈໄᯢњ߯ᓎ student 䖭Ͼ bean ⱘᯊ׭ˈ៥Ӏ䳔㽕Ў Student ㉏ᦤկϔϾᵘ ѯখ᭄䗮䖛䇗⫼䆹 bean ⱘᵘ䗴఼ˈᇚ䖭ѯ䜡㕂ⱘሲᗻӴ䗦㒭䆹 beanDŽ 䗴⊼ܹⱘ㡖⚍DŽᵘ䗴⊼ܹˈгህᰃ䗮䖛 bean ЁᅮНⱘᵘ䗴খ᭄ˈ೼߯ᓎ䖭Ͼ bean ⱘᅲ՟ᯊˈህᇚ䖭 14.3.4 ᇣ㡖Ёˈ៥ӀᅮН䜡㕂᭛ӊЁЎ bean ᦤկ property 㡖⚍䖯㸠䆒ؐ⊼ܹˈᴀᇣ㡖៥ӀᅮНᵘ 14.3.5 ໄᯢᵘ䗴⊼ܹࠄbeanⱘሲᗻ value 㡖⚍ˈ಴Ў៥ӀϔϾ property াӮ䆒㕂ϔϾሲᗻDŽ property 㡖⚍ϟ䴶ৃҹߎ⦄ ref 㡖⚍੠ value 㡖⚍ˈԚᰃˈ↣ϔϾ property া㛑ߎ⦄ϔ⃵ ref 㡖⚍៪㗙 ҹϞⱘ䜡㕂㸼⼎৥ student 䖭Ͼ bean Ё⊼ܹњϔϾ Integer ⱘؐˈᇍᑨ Student ⱘ age ሲᗻDŽ಴ℸˈ property>/> /Download at http://www.pin5i.com 䅸ؐ˄ϡ䳔㽕ᰒᓣᦤկ˅Ў false ੠ noDŽ ‰ beans 㡖⚍˖ḍ㡖⚍Ў beansˈbeans ⱘሲᗻ᳝ default-lazy-init ੠ autowireˈ䖭ϸϾሲᗻⱘ咬 䗮䖛ҹϞⱘҷⷕˈ䙷М䆹ӑ DTD ⱘ৘Ͼ㡖⚍ৃҹὖᣀЎབϟ޴Ͼ˖ ㋴ -->ܗ ㋴ -->ܗ )> (ref | value | null)? ܗ㋴ⱘᄤܗ ܗܗ ܗ㋴ⱘᄤܗ ҹϟЎ⌟䆩ⱘ䜡㕂˖ 䖛䇗⫼ bean ⱘ᮴খ᭄ᵘ䗴఼ᴹ߯ᓎᅲ՟DŽϟ䴶៥Ӏ⌟䆩 bean ⱘ singleton ሲᗻ˄ᰃ৺ऩᗕ˅DŽ class ሲᗻᴹᅮН beanˈ䖭䞠≵᳝䜡㕂ӏԩⱘᵘ䗴খ᭄ˈᑊϨ≵᳝䜡㕂ӏԩሲᗻˈ಴ℸ៥Ӏⱘ⿟ᑣᰃ䗮 ៥Ӏⱘ getBean ᮍ⊩ৃҹᐂ៥Ӏ߯ᓎ bean њˈԚᰃˈ៥Ӏᦤկⱘ䜡㕂᭛ӊЁˈা᳝ㅔऩⱘ id ੠ org.crazyit.ioc.context.object.XmlApplicationContextObject1@56a499 䕧ߎ㒧ᵰ˖ System.out.println(obj1); XmlApplicationContextObject1 obj1 = (XmlApplicationContextObject1)ctx.getBean("test1"); //ᣓࠄ test1, Փ⫼᮴খᵘ䗴఼߯ᓎ 䆩ⱘҷⷕ˖ ⦄೼ৃҹ䇗⫼ getBean ᮍ⊩㦋ᕫ test1 ⱘ beanˈᇚᕫࠄⱘᇍ䈵ᡒॄߎᴹᶹⳟᰃ৺᳝ؐˈҹϟЎ⌟ (new String[]{"/resources/context/XmlApplicationContext1.xml"}); ApplicationContext ctx = new XmlApplicationContext ϟ䴶ⱘҷⷕՓ⫼ XmlApplicationContext ᴹ߯ᓎ IoC ᆍ఼˖ ҹϞⱘ䜡㕂ᅮНњϔϾ test1 ⱘ beanˈ㉏ XmlApplicationContextObject াᰃϔϾ᱂䗮ⱘ Java ㉏DŽ ໛᭄᥂˖ޚㅔऩⱘ⌟䆩 getBean ᮍ⊩ˈҹϟᰃ⌟䆩ⱘܜ佪 ݇ⱘҷⷕᇍ䖭ѯࡳ㛑䖯㸠⌟䆩DŽ ໪⬠䇗⫼ getBean ᮍ⊩ⱘᯊ׭ݡ߯ᓎDŽࠄњ⦄೼ˈᭈϾ IoC ᆍ఼ⱘࡳ㛑䛑Ꮖ㒣ᅲ⦄њˈ៥Ӏৃҹ㓪ݭⳌ ᆍ఼᳝ϸ⾡߯ᓎᮍᓣˈৃҹ೼߱ྟ࣪ᆍ఼ⱘᯊ׭ˈህ߯ᓎ᠔᳝ⱘ beanˈгৃҹϡ߯ᓎӏԩⱘ beanˈ䅽 ೼Ϟϔᇣ㡖Ёˈ៥ӀᏆ㒣⹂ᅮњ AbstractApplicationContext ᳝ϸϾᄤ㉏ˈ䙷Мህҷ㸼៥Ӏⱘ IoC 14.7.5 ⌟䆩IoCᆍ఼ⱘ߯ᓎ } } setUpElements(xmlPaths); //া߱ྟ࣪᭛ḷ, ϡ߯ᓎӏԩ bean public XmlBeanFactory(String[] xmlPaths) { public class XmlBeanFactory extends AbstractApplicationContext { ҷⷕ⏙ऩ˖code\IoC\main\org\crazyit\ioc\beans\factory\XmlBeanFactory.java 䋳䋷䇏প XML 䜡㕂᭛ӊˈϡ߱ྟ࣪ᆍ఼ˈབᵰ䇗⫼ getBean ᮍ⊩ᯊˈᠡ߯ᓎ beanDŽ ݡЎ AbstractApplicationContext ᮄᓎϔϾᄤ㉏ XmlBeanFactoryˈ䆹㉏ⱘ԰⫼Џ㽕ᰃᔧᵘ䗴ⱘᯊ׭ˈা ㋴ˈᑊ߱ྟ࣪ IoC ᆍ఼DŽ⦄೼ܗ䖭ḋˈᔧ XmlApplicationContext 䖯㸠ᵘ䗴ᯊˈህӮ߱ྟ࣪᭛ḷ੠ } createBeans(); //߱ྟ࣪ᆍ఼ setUpElements(xmlPaths); ㋴ܗ߱ྟ࣪᭛ḷ੠// /Download at http://www.pin5i.com ϔḋDŽ ᮍ⊩ᕫࠄ test2 ⱘᅲ՟ˈ䖭䞠ݡ䗮䖛 test2 㦋ᕫᅗⱘ object1 ሲᗻˈৃҹⳟࠄ䆹ሲᗻⱘؐϢ test1 ⱘᅲ՟ ৃҹⳟࠄ㒧ᵰⱘ᳔ৢϸ㸠ˈ៥ӀҢᆍ఼ЁⳈ᥹䗮䖛 getBean ᮍ⊩ᕫࠄ test1 ⱘᅲ՟ˈݡ䗮䖛 getBean org.crazyit.ioc.context.object.XmlApplicationContextObject1@183f74d org.crazyit.ioc.context.object.XmlApplicationContextObject1@183f74d 10 yangenxiong ৃҹⳟࠄᬜᵰབϟ˖ System.out.println(obj2.getObject1()); //ᠧॄ test2 ⱘ object1 ሲᗻ System.out.println(obj1); //ᠧॄ test1 ⱘ bean System.out.println(obj2.getAge()); //ᠧॄ test2 ⱘ age System.out.println(obj2.getName()); //ᠧॄ test2 ⱘ name XmlApplicationContextObject2 obj2 = (XmlApplicationContextObject2)ctx.getBean("test2"); //ᣓࠄ㄀ѠϾ bean, Փ⫼໮খ᭄ᵘ䗴఼߯ᓎ XmlApplicationContextObject1 obj1 = (XmlApplicationContextObject1)ctx.getBean("test1"); ⊼ᛣҹϞ䜡㕂ⱘ咥ԧ䚼ߚˈtest2 ݊ЁⱘϔϾᵘ䗴খ᭄Փ⫼ⱘᰃ test1 ⱘ beanˈҹϟЎ⌟䆩ҷⷕ˖ 10 yangenxiong ҹϟЎ⌟䆩ⱘ䜡㕂˖ ᑊϡϔ㟈ˈϟ䴶⌟䆩ᵘ䗴⊼ܹDŽ Ң㒧ᵰЁৃҹⳟࠄ test3 ⱘ bean ᰃ䴲ऩᗕⱘˈ಴ℸᠧॄ㒧ᵰⱘ᳔ৢϸ㸠ˈϸϾᇍ䈵ⱘ hashCode org.crazyit.ioc.context.object.XmlApplicationContextObject1@fa7e74 org.crazyit.ioc.context.object.XmlApplicationContextObject1@a1807c org.crazyit.ioc.context.object.XmlApplicationContextObject1@1e51060 org.crazyit.ioc.context.object.XmlApplicationContextObject1@1e51060 ᠧॄ㒧ᵰ˖ System.out.println(obj4); System.out.println(obj3); XmlApplicationContextObject1 obj4 = (XmlApplicationContextObject1)ctx.getBean("test3"); XmlApplicationContextObject1 obj3 = (XmlApplicationContextObject1)ctx.getBean("test3"); //test3 ϡᰃऩᗕ bean System.out.println(obj2); System.out.println(obj1); /Download at http://www.pin5i.com 10 yangenxiong id="object1"> ϟ䴶⌟䆩ϡ䗮䖛㞾ࡼ㺙䜡䖯㸠䆒ؐ⊼ܹˈҹϟЎ⌟䆩ⱘ䜡㕂˖ ఼Ёⱘ bean 䆒㕂ࠄᅲ՟䞠䴶DŽ ҢҹϞⱘ㒧ᵰৃҹⳟߎˈtest4 ⱘ bean ≵᳝䜡㕂ӏԩⱘሲᗻˈህৃҹ䗮䖛ৡᄫ䖯㸠㞾ࡼ㺙䜡ˈᇚᆍ org.crazyit.ioc.context.object.XmlApplicationContextObject1@1fdc96c org.crazyit.ioc.context.object.XmlApplicationContextObject1@1fdc96c org.crazyit.ioc.context.object.XmlApplicationContextObject3@124bbbf Ⳍৠ˖ XmlApplicationContextObject1 ᇍ䈵ˈҹϟЎᠧॄ㒧ᵰˈৃҹⳟࠄҹϞⱘ咥ԧⱘϸϾᅲ՟ⱘ hashCode getObject1 ᮍ⊩ᕫࠄ XmlApplicationContextObject1 ᇍ䈵ˈݡҢᆍ఼Ё䗮䖛 getBean ᮍ⊩ᕫࠄ Ңᆍ఼ЁᕫࠄϔϾ XmlApplicationContextObject3 ᇍ䈵ˈݡ䗮䖛䆹ᇍ䈵ⱘܜˈҹϞⱘ⌟䆩ҷⷕЁ System.out.println(obj3); XmlApplicationContextObject1 obj3 = (XmlApplicationContextObject1)ctx.getBean("object1"); System.out.println(obj2); XmlApplicationContextObject1 obj2 = obj1.getObject1(); System.out.println(obj1); XmlApplicationContextObject3 obj1 = (XmlApplicationContextObject3)ctx.getBean("test4"); ⱘ object1 ⳟϟᰃ৺᳝ؐˈҹϟЎ⌟䆩ҷⷕ˖ ៥Ӏ䜡㕂њ test4 Ў㞾ࡼ㺙䜡ˈ಴ℸᕫࠄ test4 ⱘᅲ⦄ᯊˈህৃҹᠧॄ XmlApplicationContextObject3 } private XmlApplicationContextObject1 object1; private int age; private String name; public class XmlApplicationContextObject3 { 䴶≵᳝ӏԩⱘሲᗻˈXmlApplicationContextObject3 ⱘሲᗻབϟ˖ ҹϞⱘ䜡㕂Ёˈobject1 ϡ㞾ࡼ㺙䜡ˈtest4 Փ⫼㞾ࡼ㺙䜡ˈXmlApplicationContextObject1 ᇍ䈵䞠 autowire="byName"> class="org.crazyit.ioc.context.object.XmlApplicationContextObject3" id="object1"> ϟ䴶㒻㓁⌟䆩㞾ࡼ㺙䜡ˈҹϟЎ⌟䆩䜡㕂˖ ճເēXmlApplicationContextObject2 ᄯပྡྷّ೫໿ࠁ object1ēಾ XmlApplicationContextObject1 फ໸d ᅟġუऺԅ XmlApplicationContextObject1 ճເۤ XmlApplicationContextObject2 ճເ՛ಾ୔ඹԅ Java /Download at http://www.pin5i.com //䇗⫼ getBean ᮍ⊩, ᠡӮ߯ᓎ System.out.println(obj); Object obj = ctx.getBeanIgnoreCreate("test1"); //ᆍ఼߱ྟ࣪ⱘᯊ׭ϡӮ߯ᓎ ApplicationContext ctx = new XmlBeanFactory(new String[]{"/resources/factory/XmlBeanFactory.xml"}); 㹿߯ᓎˈҹϟЎ⌟䆩ҷⷕ˖ Ͼ bean ೼ᆍ఼ਃࡼⱘᯊ׭ህӮ㹿߯ᓎˈԚᰃབᵰՓ⫼ XmlBeanFactory ߯ᓎᆍ఼ⱘ䆱ˈ䆹 bean ϡӮ ҹϞᅮНњϔϾৡᄫি test1 ⱘ beanˈབᵰՓ⫼ XmlApplicationContext ᴹ߯ᓎᆍ఼ⱘ䆱ˈ䙷М䖭 ᓎˈҹϟЎ⌟䆩䜡㕂˖ ϟ䴶៥Ӏৃҹ⌟䆩 XmlBeanFactory ㉏ˈབᵰՓ⫼䆹㉏߱ྟ࣪ᆍ఼ˈ䙷М᠔᳝ⱘ bean 䛑ϡӮ㹿߯ org.crazyit.ioc.context.object.XmlApplicationContextObject3@1c39a2d null test5 ᯊˈৃҹথ⦄ getBean ᮍ⊩ᅲ䰙ϞᏆ㒣ᐂ៥Ӏ߯ᓎњᅲ՟ˈҹϟЎᠧॄ㒧ᵰ˖ ೼߯ᓎᆍ఼ⱘᯊ׭ test5 ᑊϡӮ㹿߯ᓎˈ಴ℸᅗᰃᓊ䖳ࡴ䕑ⱘˈᔧ៥Ӏݡ⃵䇗⫼ getBean ᮍ⊩ᕫࠄ System.out.println(obj); obj = ctx.getBean("test5"); System.out.println(obj); Object obj = ctx.getBeanIgnoreCreate("test5"); //test5 ᰃᓊ䖳ࡴ䕑ⱘ, ≵᳝䇗⫼䖛 getBean ᮍ⊩, 䙷Мᆍ఼ЁህϡӮ߯ᓎ䖭Ͼ bean ᑊϡӮᐂ៥Ӏএ߯ᓎˈҹϟЎ⌟䆩ҷⷕ˖ getBeanIgnoreCreate ᮍ⊩ᕫࠄ beanˈ಴ℸ䆹ᮍ⊩ӮҢᆍ఼Ёᶹᡒ beanˈབᵰϡ㛑ᡒࠄˈ߭䖨ಲ nullˈ ҹϞⱘ䜡㕂া᳝ϔϾ beanˈ䆹 bean ᰃ䳔㽕ᓊ䖳ࡴ䕑ⱘˈ៥Ӏৃҹ䇗⫼ ApplicationContext ⱘ ⌟䆩ᓊ䖳ࡴ䕑ˈҹϟЎ≵᳝ⱘ䜡㕂˖ ࠄᕫࠄⱘ hashCode ϢⳈ᥹ࠄᆍ఼Ё getBean ᕫࠄⱘ hashCode ϔ㟈DŽ 䗮䖛 XmlApplicationContextObject3 ⱘᅲ՟ᕫࠄ XmlApplicationContextObject1 ⱘᅲ՟ᯊˈৃҹⳟ org.crazyit.ioc.context.object.XmlApplicationContextObject1@13bad12 org.crazyit.ioc.context.object.XmlApplicationContextObject1@13bad12 10 yangenxiong ҹϟЎᠧॄ㒧ᵰ˖ System.out.println(obj2); System.out.println(obj1.getObject1()); System.out.println(obj1.getAge()); System.out.println(obj1.getName()); XmlApplicationContextObject1 obj2 = (XmlApplicationContextObject1)ctx.getBean("object1"); XmlApplicationContextObject3 obj1 = (XmlApplicationContextObject3)ctx.getBean("test6"); ⱘ⌟䆩ҷⷕऎ߿ϡ໻˖ ៥Ӏ೼ࠡ䴶Ꮖ㒣⌟䆩њḍ᥂ৡᄫ㞾ࡼ㺙䜡ˈ៥Ӏ೼䖭䞠⌟䆩ϡՓ⫼㞾ࡼ㺙䜡䖯㸠䆒ؐ⊼ܹˈϢࠡ䴶 XmlApplicationContextObject3 ճເ՛ಾලྡྷّd ᅟ࿉ġแਬၽ·ࠋᄯൎರဈԅ XmlApplicationContextObject1cXmlApplicationContextObject2 ူ property name="object1">> /Download at http://www.pin5i.com Ϯࡵሖⱘᅲ⦄Ϣ᭄᥂䆓䯂ሖⱘ݇㋏ˈབ೒ 14.2 ᠔⼎DŽ ៤ beanˈᑊ⊼ܹࠄ MainFrame ЁDŽخᇍ䈵 ໪ˈ৘Ͼ㾚೒ሖⱘ JPanel ᇍ䈵ˈ䛑ᰃ㒭Џ⬠䴶 MainFrame ᴹՓ⫼ⱘˈ಴ℸгৃҹᇚ䖭ѯ㾚೒ሖⱘ JPanel ᇍ䈵ˈ䙷Мབᵰ៥ӀՓ⫼ IoC ᴹ߯ᓎϮࡵሖᇍ䈵ⱘ䆱ˈ䙷М೼㾚೒ህৃҹϡᖙ᠟ࡼএ߯ᓎ䖭ѯᇍ䈵DŽ঺ Ң೒ 14.1 Ё៥Ӏৃҹⳟࠄˈ೒к䖯ᄬ䫔㋏㒳Ёˈ৘Ͼ㾚೒ᇍ䈵䞠䴶䛑᳝ϔϾ៪㗙໮ϾϮࡵሖ᥹ষ ೒ 㾚೒ሖϢϮࡵሖ㉏೒  㾚೒ሖϢϮࡵሖⱘ݇㋏ˈ䇋ⳟ೒ 14.1DŽ ⱘ䆓䯂᥹ষ੠ᅲ⦄DŽ 䈵ˈϮࡵ໘⧚ሖ˄Service˅᳝৘ϾϮࡵ໘⧚ⱘ᥹ষ੠ᇍᑨⱘᅲ⦄㉏ˈ᭄᥂䆓䯂ሖ˄DAO˅᳝৘㉏᭄᥂ ⿟ᑣ䖯㸠ߚሖˈ᳝㾚೒ሖǃϮࡵ໘⧚ሖ੠᭄᥂䆓䯂ሖDŽ㾚೒ሖࣙᣀ৘Ͼ JPanel ᇍ䈵੠ϸϾ JFrame ᇍ ೼ᭈড়ࠡˈ៥Ӏ䳔㽕ᯢⱑાѯᇍ䈵䳔㽕㒭 IoC ᆍ఼䖯㸠ㅵ⧚ⱘDŽ㄀ 9 ゴ೒⠛䖯ᄬ䫔㋏㒳Ёˈ៥Ӏᇍ 14.8.1 䳔㽕ㅵ⧚ⱘᇍ䈵 এњ㾷㄀ 9 ゴ೒к䖯ᄬ䫔㋏㒳ⱘߚሖ㒧ᵘDŽܜ໘DŽ೼䯙䇏ᴀ㡖ࠡˈৃҹ ៥Ӏৃҹϡ䳔㽕Փ⫼ new ݇䬂ᄫᴹ߯ᓎ㉏ⱘᅲ՟ˈ䖭ϔ㡖ˈ៥ӀᴹԧӮ䖭Ͼ IoC ᆍ఼ᏺ៥Ӏᏺᴹⱘད ៥Ӏৃҹᇚ৘Ͼᇍ䈵԰Ўᆍ఼ЁⱘϔϾ beanˈ䅽 IoC ᆍ఼ᐂ៥Ӏএ߯ᓎᅗӀⱘᅲ՟੠ᇍᅗӀ䖯㸠ㅵ⧚ˈ ៥Ӏ೼ࠡ䴶㓪ݭњϔϾㅔऩⱘ IoC ᆍ఼ˈ៥Ӏৃҹ䜡㕂৘Ͼᇍ䈵ˈᑊЎᅗӀ䜡㕂ሲᗻ៪㗙ᵘ䗴খ᭄ˈ 14.8 IoCϢ೒к䖯ᄬ䫔㋏㒳ⱘᭈড় Ӏ㓪ݭⱘ䖭Ͼ IoC ᠔ᏺᴹⱘད໘DŽ ᮍ֓ਸ਼ˈ೼ϟϔ㡖ˈ៥ӀՓ⫼೼䖭ϔ㡖Ё㓪ݭⱘ IoC[ᆍ఼Ϣ㄀ 9 ゴ೒к䖯ᄬ䫔㋏㒳䖯㸠ᭈড়ˈᴹԧӮ៥ ࠄℸˈ៥Ӏ IoC ⱘⳌ݇ࡳ㛑䛑Ꮖ㒣ᅲ՟њˈᑊϨ䛑㒣䖛њ⌟䆩ˈ䙷М䖭Ͼ IoC ৃҹ㒭៥ӀᏺᴹҔМ org.crazyit.ioc.context.object.XmlApplicationContextObject1@1754ad2 null ৃҹⳟࠄᠧॄ㒧ᵰབϟ˖ System.out.println(obj); obj = ctx.getBean("test1"); /Download at http://www.pin5i.com г↨䕗⏙᱄ˈҹϞⱘ䜡㕂ЁˈⳈ᥹ᅮН৘Ͼ DAO ⱘ beanˈᣛᅮ id ੠ class ेৃDŽ ⬅Ѣ៥Ӏⱘ IoC ᆍ఼ᬃᣕ໮ӑ䜡㕂᭛ӊˈ಴ℸৃҹᇚ᭄᥂䆓䯂ሖǃϮࡵሖ੠㾚೒ሖߚᓔ䜡㕂ˈ䖭ḋ ҷⷕ⏙ऩ˖code\book(IoC)\src\resource\daoContext.xml ⱘݙᆍབϟDŽ Ёˈ೼乍Ⳃⱘ src Ⳃᔩϟᮄᓎ resources Ⳃᔩˈᮄᓎ daoContext.xml 䜡㕂᭛ӊˈdaoContext.xml ᭛ӊ ៥Ӏ䳔㽕ᇚᴀゴ᠔㓪ݭⱘ IoC ᆍ఼ᠧ៤ϔϾ jar ࣙˈ✊ৢᬒࠄ೒к䖯ᄬ䫔㋏㒳ⱘ classpathˈܜ佪 14.8.2 ߯ᓎ᭄᥂䆓䯂ሖⱘbean 䖭᭄᥂䆓䯂ሖ៥Ӏ䛑ৃҹѸ㒭 IoC ᆍ఼এㅵ⧚DŽ Ң೒ 14.2 ৃҹⳟࠄˈϮࡵሖⱘ৘Ͼᅲ⦄㉏ˈ䞠䴶䛑Փ⫼ࠄϔϾ៪㗙໮Ͼⱘ᭄᥂䆓䯂ሖ᥹ষˈ಴ℸˈ ೒ Ϯࡵሖᅲ⦄Ϣ᭄᥂䆓䯂ሖⱘ㉏೒  /Download at http://www.pin5i.com private BookDao bookDao; ׂᬍৢⱘϮࡵሖҷⷕ˖ } this.concernDao = concernDao; this.typeDao = typeDao; this.bookDao = bookDao; ConcernDao concernDao) { public BookServiceImpl(BookDao bookDao, TypeDao typeDao, private ConcernDao concernDao; private TypeDao typeDao; private BookDao bookDao; ॳᴹⱘϮࡵሖҷⷕ˖ ᮍ⊩ⱘৡᄫᖙ乏ᰃ set ᠧ༈ˈৢ䴶䎳 DAO bean ⱘ idDŽ 䗴఼ⱘᮍᓣ䖯㸠䆒ؐˈ⦄೼ৃҹϡՓ⫼ᵘ䗴఼䆒ؐˈা䳔㽕Ў৘Ͼ DAO ⱘሲᗻᦤկ setter ᮍ⊩ˈsetter ЎϮࡵሖⱘ৘Ͼ㒘ӊᅮНњ䜡㕂ৢˈ៥Ӏ䖬䳔㽕ׂᬍᅲ⦄㉏ⱘҷⷕ˄ServiceImpl˅ˈॳᴹᰃՓ⫼ᵘ ဈᆑՎᅭଅෳēୣ൐ԅ bean ՛ರဈॴᆑՎᅭଅd ᅟ࿉ēนॴႺವแਬԅಁᄔᅟదٟટē౨ੋԅྜ๠Њԅّْ bean ଅᄢᄯē҂ॴ userService ਠပರ class="org.crazyit.book.service.impl.InRecordServiceImpl" autowire="byName"> class="org.crazyit.book.service.impl.SaleRecordServiceImpl" autowire="byName"> class="org.crazyit.book.service.impl.BookServiceImpl" autowire="byName"> class="org.crazyit.book.service.impl.ConcernServiceImpl" autowire="byName"> class="org.crazyit.book.service.impl.TypeServiceImpl" autowire="byName"> ҷⷕ⏙ऩ˖code\book(IoC)\src\resource\serviceContext.xml ˄autowire=”byname”˅ˈ݋ԧ䜡㕂བϟDŽ 㗙໮Ͼⱘ᭄᥂䆓䯂ሖ᥹ষˈ಴ℸ៥Ӏ೼䜡㕂Ϯࡵሖⱘ bean ⱘᯊ׭ˈৃҹՓ⫼ḍ᥂ৡᄫ㞾ࡼ㺙䜡 bean ᥹ϟᴹᅮНϮࡵሖⱘ䜡㕂ˈϮࡵሖ䳔㽕⊼ܹ৘Ͼ DAO ⱘ beanˈϮࡵሖ᥹ষⱘᅲ⦄㉏Փ⫼њϔϾ៪ ߯ᓎϮࡵሖⱘbean 14.8.3 /Download at http://www.pin5i.com 䳔㽕߱ྟ᭄࣪᥂ˈ᭄᥂ᰃҢϮࡵሖᕫࠄⱘ --> 䰸њ SalePanel ໪ˈ݊Ҫ⬠䴶ᇍ䈵䗮䖛㞾ࡼ㺙䜡߱ྟ࣪ ⬅Ѣ߱ྟ࣪⬠䴶ⱘᯊ׭ˈ䳔㽕䆒㕂ᔧࠡ⬠䴶Ў䫔ଂ⬠䴶ˈ Џ⬠䴶 MainFrame ⱘ beanˈ䳔㽕䖯㸠ᵘ䗴⊼ܹˈ ҷⷕ⏙ऩ˖code\book(IoC)\src\resource\uiContext.xml ᕫࠄᶤϾϮࡵሖᇍ䈵ˈ䇗⫼Ϯࡵሖⱘᇍ䈵䖯㸠߱ྟ᭄࣪᥂ˈ಴ℸ䳔㽕Փ⫼ᵘ䗴⊼ܹˈҹϟЎ݋ԧ䜡㕂DŽ ᥹ϟᴹ߯ᓎ㾚೒ሖⱘ䜡㕂᭛ӊ uiContext.xmlˈ೼䖭䞠䳔㽕⊼ᛣˈ৘Ͼ㾚೒ሖ೼ᵘ䗴ⱘᯊ׭ˈህ䳔㽕 14.8.4 ߯ᓎ㾚೒ሖⱘbean ᅟ࿉ setter ֺ֥ԅ੡੠ēsetďDAO bean nameĐd } this.concernDao = concernDao; public void setConcernDao(ConcernDao concernDao) { } this.typeDao = typeDao; public void setTypeDao(TypeDao typeDao) { } this.bookDao = bookDao; public void setBookDao(BookDao bookDao) { private ConcernDao concernDao; private TypeDao typeDao; /Download at http://www.pin5i.com BookSaleRecordDao bookSaleRecordDao = new BookSaleRecordDaoImpl(); SaleRecordDao saleRecordDao = new SaleRecordDaoImpl(); BookDao bookDao = new BookDaoImpl(); ConcernDao concernDao = new ConcernDaoImpl(); TypeDao typeDao = new TypeDaoImpl(); //߯ᓎ DAO ᇍ䈵 public MainFrame() { ҷⷕ⏙ऩ˖code\book(IoC)\src\org\crazyit\book\ui\MainFrame ߯ᓎˈॳᴹⳈ᥹Փ⫼ new ݇䬂ᄫ೼ MainFrame Ё߯ᓎ৘Ͼ㒘ӊDŽ 䴶ˈ಴ℸ៥Ӏᇚ䫔ଂ⬠䴶ⱘ bean ᵘ䗴⊼ܹࠄ MainFrame ЁDŽ⦄೼ৃҹׂᬍ MainFrame Ё৘Ͼᇍ䈵ⱘ ⊼ᛣҹϞ䜡㕂Ё mainFrame ⱘ beanˈ⬅Ѣ MainFrame ೼߯ᓎⱘᯊ׭ˈህ䳔㽕偀Ϟᰒ⼎䫔ଂⱘ⬠ constructor-arg>/> /Download at http://www.pin5i.com LoginFrame loginFrame = (LoginFrame)ctx.getBean("loginFrame"); //ᕫࠄ LoginFrame ⱘᅲ՟ "/resource/serviceContext.xml", "/resource/uiContext.xml"}); ApplicationContext ctx = new XmlApplicationContext(new String[]{"/resource/daoContext.xml", //߯ᓎᆍ఼ public static void main(String[] args) { Ёࡴܹ߱ྟ࣪ IoC ᆍ఼ⱘҷⷕ˖ 䳔㽕ׂᬍ MainFrame ⱘᵘ䗴఼ˈߴ䰸 this.setVisible(true)ϔহDŽ᳔ৢˈ៥Ӏ೼ Main ㉏˄⿟ᑣਃࡼ㉏˅ ⬅Ѣ៥Ӏ೼ LoginFrame ⱘ login ᮍ⊩Ё䆒ᅮњˈབᵰⱏᔩ៤ࡳⱘ䆱ˈᠡᰒ⼎ MainFrameˈ䙷Мህ } showWarn(e.getMessage()); e.printStackTrace(); } catch (Exception e) { this.setVisible(false); //䱤㮣 LoginFrame this.mainFrame.setVisible(true); //៤ࡳⱏᔩ, ᰒ⼎ MainFrame userService.login(name, password.toString()); try { ϟ䴶ᰃ LoginFrame ⱘ login ᮍ⊩ⱘ䚼ߚҷⷕ˖ ᬍ LoginFrameˈЎ LoginFrame ᵘ䗴⊼ܹϔϾ UserService ⱘ beanˈ䆒ؐ⊼ܹϔϾ MainFrame ⱘ beanˈ ᮍ⊩ⱘੑৡϢϮࡵሖᅲ⦄㉏Ёⱘ setter ᮍ⊩ੑৡϔḋˈset˄㾚೒ሖ bean ⱘ id˅DŽݡҹৠḋⱘᮍ⊩এׂ ׂᬍњ MainFrame ⱘᵘ䗴఼ৢˈ䖬䳔㽕Ў݊Ҫⱘ㾚೒ሖᇍ䈵˄JPanel˅⏏ࡴ setter ᮍ⊩ˈsetter } //ⳕ⬹݊Ҫҷⷕ this.salePanel = salePanel; public MainFrame(SalePanel salePanel) { 䙷М⦄೼ৃҹׂᬍ៤˖ } //ⳕ⬹݊Ҫҷⷕ typePanel = new TypePanel(this.typeService); //߱ྟ࣪⾡㉏ㅵ⧚⬠䴶 concernPanel = new ConcernPanel(this.concernService); //߱ྟ࣪ߎ⠜⼒ㅵ⧚⬠䴶 this.concernService); bookPanel = new BookPanel(this.bookService, this.typeService, //߱ྟ࣪кᴀㅵ⧚⬠䴶 repertoryPanel = new RepertoryPanel(this.inRecordService, this.bookService); //߱ྟ࣪ᑧᄬㅵ⧚⬠䴶 this.salePanel = new SalePanel(this.bookService, this.saleRecordService); //䅽䫔ଂ⬠䴶԰Ў㄀ϔᰒ⼎⬠䴶 this.inRecordService = new InRecordServiceImpl(inRecordDao, bookInRecordDao, bookDao); bookSaleRecordDao, bookDao); this.saleRecordService = new SaleRecordServiceImpl(saleRecordDao, this.bookService = new BookServiceImpl(bookDao, typeDao, concernDao); this.concernService = new ConcernServiceImpl(concernDao); this.typeService = new TypeServiceImpl(typeDao); //߯ᓎ Service BookInRecordDao bookInRecordDao = new BookInRecordDaoImpl(); InRecordDao inRecordDao = new InRecordDaoImpl(); /Download at http://www.pin5i.com ᇍ IoC ⱘ䅸䆚DŽ ᆊ乚Ӯࠄ IoC ⱘᎹ԰ॳ⧚ˈ೼Փ⫼݊Ҫ⌕㸠ⱘ IoC ᆍ఼ⱘᯊ׭ˈ䰸њӮՓ⫼໪ˈ䖬ৃҹពᕫॳ⧚ˈࡴ⏅ ᴀࡳ㛑ᰃ䖰䖰ϡ໳ⱘˈԚᰃ䗮䖛ᴀゴ᠔ҟ㒡ⱘϔѯ IoC ᳔෎ᴀⱘࡳ㛑ˈᏠᳯৃҹ㒭䇏㗙ᏺᴹਃথˈ䅽໻ ᓣ˄ᴀゴা᳝ byName 㞾ࡼ㺙䜡˅ㄝㄝϔ㋏߫ࡳ㛑ˈⳌᇍѢᔧࠡ⌕㸠ⱘϔѯ IoC ᆍ఼ˈᴀゴ᠔ᅲ⦄ⱘ෎ 㸠ᭈড়ˈሩ⼎ IoC ᆍ఼㒭៥ӀᏺᴹⱘӋؐDŽϔϾᔎ໻ⱘ IoC ᆍ఼䖬ࣙᣀ bean ⱘ㒻ᡓǃ໮⾡㞾ࡼ㺙䜡ᮍ ೼ᴀゴ៥ӀՓ⫼њ dom4jǃJava ⱘডᇘᴎࠊ㓪ݭњϔϾㅔऩⱘ IoC ᆍ఼ˈ᳔ৢᑊϢ⦄೼᳝㋏㒳䖯 14.9 ᴀゴᇣ㒧 IoC ᆍ఼এᅲ⦄ˈ䅽ᆍ఼Ў៥Ӏএㅵ⧚৘Ͼ㋏㒳Ёⱘ㒘ӊˈᇚҷⷕⱘ㗺ড়䰡ࠄњ䜡㕂᭛ӊЁDŽ ߯ᓎᅲ՟ⱘҷⷕৃҹܼ䚼ⳕ⬹ˈ៥Ӏৃҹϡݡ݇ᖗ৘Ͼᅲ՟ⱘ߯ᓎˈ䖭ѯ߯ᓎᅲ՟ⱘ㸠Ўˈܼ䚼Ѹ⬅㒭 ࠄ⦄೼ˈϢ೒к䖯ᄬ䫔㋏㒳ⱘᭈড়Ꮖ㒣ᅠ៤ˈ೼ᭈড়ⱘ䖛⿟Ёˈ៥Ӏৃҹⳟࠄˈҹࠡ᠔㓪ݭⱘϔѯ } loginFrame.setVisible(true); //䆒㕂 LoginFrame ৃ㾕 ㄀ 14 ゴ㞾ᏅᓔথIoC ᆍ఼ g43g /Download at http://www.pin5i.com //খ᭄߫㸼 public class Request { ҷⷕ⏙ऩ˖code\GameHall-Commons\src\org\crazyit\gamehall\commons\Request.java ᅠ៤њ᳡ࡵ఼ⱘ᪡԰ৢˈህᇚ䖭ѯֵᙃ䗮䖛ϔϾ Response ᇍ䈵䖨ಲ㒭ᅶ᠋ッDŽ ಲϔϾ Response ᇍ䈵ˈᇚ Request Ϣ Response ᇍ䈵ᔧ԰খ᭄Ӵ䗦㒭᳡ࡵ఼໘⧚㉏ˈᔧ᳡ࡵ఼໘⧚㉏ ᣀখ᭄߫㸼ǃ᳡ࡵ఼໘⧚㉏ǃᅶ᠋ッ໘⧚㉏ㄝֵᙃˈᔧ᳡ࡵ఼ッ᥹ᬊࠄ䆹䇋∖ᇍ䈵ৢˈህᇚ䖭ѯֵᙃ䖨 ᔧᅶ᠋ッথ䗕ϔѯֵᙃ㒭᳡ࡵ఼ⱘᯊ׭ˈ៥Ӏህᡞ䖭ѯֵᙃᇕ㺙ࠄϔϾ Request ᇍ䈵Ёˈ䆹ᇍ䈵ࣙ ѯᇍ䈵䖯㸠Ⳍᑨⱘ໘⧚DŽ ҹᇚϔ↉ XML ᠧॄࠄ䕧ߎ⌕Ёˈᅶ᠋ッᕫࠄ䖭ѯ XML ᄫ↉Іৢˈህᇚ݊䕀࣪Ў⡍ᅮⱘᇍ䈵ˈݡḍ᥂䖭 ᄫヺІⱘḐᓣDŽᴀゴЁՓ⫼ XML ԰ЎᅗӀП䯈ⱘ䕀䕧Ḑᓣˈ՟བ᳡ࡵ఼৥ᅶ᠋ッ䕧ߎֵᙃⱘᯊ׭ˈৃ ⹂ᅮ䖭ѯܜ៥ӀᏆ㒣ⶹ䘧њ᳡ࡵ఼䯈䗮䖛 PrintStream ᴹ৥䕧ߎ⌕ᠧॄᄫヺІֵᙃˈ䙷М៥Ӏৃҹ 15.2.1 ⹂ᅮӴ䕧Ḑᓣ ᶊЁ䖤㸠ˈᔧ✊ˈ䖭ѯ␌៣䛑ᖙ乏䙉ᅜḚᶊⱘ㾘߭DŽ ࡵ఼ッϢᅶ᠋ッⱘҷⷕ䛑ϡ䳔㽕㓪ݭӏԩⱘϮࡵ䘏䕥ˈབᵰ㓪ݭњᮄⱘ␌៣ˈৃҹᇚ䆹␌៣ⱘࣙᬒࠄḚ ⱘ䕧ߎ⌕ЁDŽ᮶✊↣Ͼ᪡԰䛑བℸˈ៥Ӏৃҹ㓪ݭϔϾ␌៣໻ख़ⱘᇣḚᶊˈϧ䮼⫼ѢЁ䕀ˈḚᶊЁⱘ᳡ 㒭᳡ࡵ఼៪㗙᳡ࡵ఼ݡ䕀থ㒭݊Ҫ⥽ᆊˈ䛑ৃҹՓ⫼ PrintStream ⱘ print ᮍ⊩ᴹᇚֵᙃᠧॄࠄ Socket ᪡԰ˈ䛑ৃ㛑Ӯᇚ㞾Ꮕ᠔᪡԰ⱘֵᙃথ䗕ࠄ᳡ࡵ఼ˈݡ⬅᳡ࡵ఼䕀থ㒭݊Ҫ⥽ᆊˈ಴ℸ೼⥽ᆊথ䗕ֵᙃ Փ⫼ҹϞⱘᮍ⊩ˈৃҹᇚϔѯֵᙃҢϔৄᴎ఼থ䗕ࠄ঺໪ϔৄᴎ఼ЁDŽ៥Ӏ೼␌៣໻ख़Ёⱘ↣ϔϾ ps.println("䖭ᰃᠧॄֵᙃ"); PrintStream ps = new PrintStream(socket.getOutputStream()); ՟བˈ៥ӀৃҹՓ⫼ҹϟҷⷕᕫࠄ䕧ߎ⌕ˈᑊᇚϔѯֵᙃᠧॄࠄ䕧ߎ⌕Ё˖ Socket Ёࣙ৿ϔϾ䕧ߎ⌕ᇍ䈵ˈϔৄᴎ఼ৃҹ䗮䖛䖭Ͼ䕧ߎ⌕ˈᇚֵᙃথ䗕ࠄ঺໪ϔッⱘᴎ఼ЁDŽ Socket ᴹ䖯㸠᳡ࡵ఼ッϢᅶ᠋ッП䯈ⱘ䗮ֵˈSocket ህᰃϸৄᴎ఼Ϟⱘ䗮ֵッ⚍DŽ ህ䳔㽕ᇚ䖭ѯֵᙃ˄⥽ᆊതϟⱘֵᙃ˅ਞ䆝݊Ҫ⥽ᆊˈᑊ᳈ᮄ݊Ҫ⥽ᆊⱘ⬠䴶㒘ӊDŽ೼ᴀゴЁ៥ӀՓ⫼ ህ䳔㽕ᇚ䆹⫼᠋䖯ܹⱘֵᙃথ䗕㒭݊ҪᏆ㒣䖯ܹ໻ख़ⱘ⥽ᆊˈབᵰ⥽ᆊതϟࠄ໻ख़ⱘᶤϾḠᄤⱘᯊ׭ˈ њ㾷ϟ␌៣໻ख़ⱘॳ⧚DŽ՟བˈϔϾ⥽ᆊⱏᔩ䖯ܹ␌៣໻ख़ˈ䙷Мܜ೼ᓔথ␌៣໻ख़ࠡˈ៥Ӏ䳔㽕 15.2 㓪ݭ␌៣໻ख़Ḛᶊ ៣໻ख़ˈ䅽໻ᆊ೼ᓔথⱘ䖛⿟Ёˈњ㾷䖭ѯ␌៣໻ख़ⱘᅲ⦄ॳ⧚DŽ Փ⫼៥Ӏ᠔ᄺⱘ Java ⶹ䆚এᅲ⦄䖭ѯ␌៣໻ख़ˈᴀゴЏ㽕ҟ㒡བԩՓ⫼ Java এᓔথϔℒሲѢ㞾Ꮕⱘ␌ ৃҹ䖯㸠㔥㒰ⱘ␌៣ᇍ៬ˈᑊҢ␌៣Ёᕫࠄ⿃ߚDŽ೼⥽䖭ѯ␌៣໻ख़ⱘᯊ׭ˈ៥Ӏϡོৃҹ㗗㰥ϔϟˈ կњ৘ᓣ৘ḋⱘ␌៣ˈ՟བ᭫ഄЏǃ⊵⊵啭ǃ֘㔫ᮃᮍഫㄝˈ៥Ӏা㽕ϟ䕑ϔѯ␌៣໻ख़ⱘᅶ᠋ッˈህ ख़ˈ䖭ѯ␌៣໻ख़Ў៥Ӏᦤկњ␌៣ⱘᑇৄˈৃҹ䅽៥Ӏ೼㔥㒰Ϟ䖯㸠৘⾡␌៣ᇍ៬DŽ䖭ѯ␌៣໻ख़ᦤ ៥Ӏ᳒㒣㾕䖛䆌໮ⱘ␌៣໻ख़ˈヨ㗙Ң᳔ᓔྟ᥹㾺ⱘ㘨ӫ␌៣໻ख़ࠄ⦄೼कߚ໮Ҏ⥽ⱘ QQ ␌៣໻ 15.1 ␌៣໻ख़ㅔҟ ㄀  ゴӓ44␌៣໻ख़ /Download at http://www.pin5i.com ҷⷕ⏙ऩ˖code\GameHall-Commons\src\org\crazyit\gamehall\commons\ServerAction.java ᮄᓎ ServerAction ᥹ষDŽ ݭ᳡ࡵ఼໘⧚㉏ⱘ᥹ষDŽ 㓪ܜᔧ᳡ࡵ఼᥹ᬊࠄᅶ᠋ッথ䗕ⱘϔ⃵䇋∖ৢˈህৃҹḍ᥂ Request ᇍ䈵ᕫࠄ᳡ࡵ఼໘⧚㉏ˈ៥Ӏ 15.2.2 ᓎゟ໘⧚㉏᥹ষ 䳔㽕⊼ᛣⱘᰃˈᇚᇍ䈵䕀ᤶ៤ XML ⱘᯊ׭ˈ䳔㽕ᇚ⫳៤ⱘ XML ᄫヺІ䖯㸠໘⧚ˈএᥝᤶ㸠DŽ } } return s; String s = a.replaceAll("\r", ""); String a = xml.replaceAll("\n", ""); //এᥝᤶ㸠 String xml = xstream.toXML(obj); public static String toXML(Object obj) { //ᇚᇍ䈵䕀ᤶ៤ XML ᄫ↉І } return xstream.fromXML(xml); public static Object fromXML(String xml) { //ᇚ XML 䕀ᤶ៤ᇍ䈵 private static XStream xstream = new XStream(); public class XStreamUtil { ҷⷕ⏙ऩ˖code\GameHall-Commons\src\org\crazyit\gamehall\util\XStreamUtil.java 㓪ݭϔϾ XStreamUtil ⱘ㉏ˈ⫼Ѣ໘⧚ᇍ䈵Ϣ XML П䯈ⱘ䕀ᤶDŽ ᅲ⦄ˈXStream ৃҹ䕏ᵒⱘᐂ៥Ӏ೼䖭ϸ㗙П䯈䖯㸠䕀ᤶDŽ ᄫヺІৢˈህৃҹ䕀࣪Ў Request ៪㗙 Response ᇍ䈵DŽᇍ䈵Ϣ XML П䯈ⱘ䕀ᤶ៥ӀՓ⫼ XStream ᴹ ఼԰ЎડᑨᯊˈህৃҹᇚϔϾ Response ᇍ䈵䕀࣪ЎϔϾ XML ᄫヺІˈ᳡ࡵ఼៪㗙ᅶ᠋ッᕫࠄ䆹 XML ᄫ↉Іᯊ˄೼ᇚ XML ᠧॄࠄ Socket 䕧ߎ⌕˅ˈህৃҹᇚ Request ᇍ䈵䕀࣪ЎϔϾ XML ᄫヺІˈ᳡ࡵ ᴹ⹂ᅮ໘⧚㉏DŽ⹂ᅮњ䇋∖ⱘᇍ䈵Ў Requestˈ᳡ࡵ఼ડᑨⱘᇍ䈵Ў Response ৢˈᅶ᠋ッথ䗕䇋∖ⱘ 䖭ḋህད↨៥Ӏ೼ᓔথ web ᑨ⫼ⱘᯊ׭ˈᅶ᠋ッথ䗕ϔϾ䇋∖ⱘ urlˈ᳡ࡵ఼ህৃҹḍ᥂䖭Ͼ url 㽕⏙Ἦⱘⶹ䘧᳡ࡵ఼໘⧚㉏ⱘ݋ԧ㉏ৡDŽ ϔ⃵ⱘ䇋∖DŽRequest ᇍ䈵Ёⱘ᳡ࡵ఼໘⧚㉏Ϣᅶ᠋ッ໘⧚㉏⬅থ䗕䇋∖ⱘᅶ᠋ᴹ⹂ᅮˈ಴ℸᅶ᠋ッ䳔 ᔧᅶ᠋ッথ䗕ϔ⃵䇋∖ⱘᯊ׭ˈህᇕ㺙ϔϾ Request ᇍ䈵ˈਞ䆝᳡ࡵ఼⬅ાϾ᳡ࡵ఼໘⧚㉏໘⧚䖭 } private String actionClass; //ᅶ᠋ッ໘⧚㉏, 䆹ֱؐᄬ೼ Request ⱘ䇋∖খ᭄ⱘ Map Ё private String errorCode; //䫭䇃ҷⷕ private Map datas; //᳡ࡵ఼䖨ಲⱘ৘Ͼ᭄ؐ public class Response { ҷⷕ⏙ऩ˖code\GameHall-Commons\src\org\crazyit\gamehall\commons\Response.java } private String clientActionClass; //ᅶ᠋ッ໘⧚㉏ private String serverActionClass; //᳡ࡵ఼໘⧚㉏ private Map parameters; /Download at http://www.pin5i.com void start(User user); //ᓔྟ␌៣ⱘᮍ⊩ public interface Game { ҷⷕ⏙ऩ˖code\GameHall-Commons\src\org\crazyit\gamehall\commons\Game.java ↣ϔϾᬒ೼䆹ḚᶊЁⱘ␌៣ˈ䛑䳔㽕এᅲ⦄䖭Ͼ␌៣᥹ষˈᑊᅲ⦄ start ᮍ⊩DŽ ᥹ϟᴹᮄᓎϔϾ␌៣ⱘ᥹ষˈ䆹᥹ষা䳔㽕ᦤկϔϾ start ⱘᮍ⊩ˈ㸼⼎␌៣ⱘᓔྟࡼ԰ˈ಴ℸˈ ᴹ㒻ᡓ User ㉏DŽމᅲ䰙ᚙ ␌៣䛑Ӯ᳝ϡৠⱘ⥽ᆊᇍ䈵ˈ಴ℸᔧ៥Ӏ㓪ݭϔϾᮄⱘ␌៣ᑊᬒࠄ䖭ϾḚᶊЁⱘᯊ׭ˈৃҹḍ᥂␌៣ⱘ Ёˈ↣ϾމSocketˈḍ᥂䖭Ͼ Socket ህৃҹ৥ᅶ᠋ッথ䗕Ⳍ݇ⱘֵᙃ˄Response ᇍ䈵˅DŽ೼ᅲ䰙ⱘᚙ 䖭ϔ㋏߫ⱘ⥽ᆊᇍ䈵䛑ֱᄬ䍋ᴹˈᔧḍ᥂⥽ᆊⱘ id ᕫࠄᶤϾ⥽ᆊᇍ䈵ৢˈህৃҹⳈ᥹ᕫࠄ䆹⥽ᆊᇍᑨⱘ User ㉏Ё䰸њࣙ৿⥽ᆊⱘϔѯ෎ᴀֵᙃ໪ˈ䖬䳔㽕ֱᄬϔϾ Socket ᇍ䈵ˈ೼᳡ࡵ఼Ё៥Ӏ䳔㽕ᇚ } private Socket socket; //⥽ᆊᇍᑨⱘ Socket private int sex; //0 ⬋, 1 ཇ private String name; //⥽ᆊৡ⿄ private String headImage; ೒⠛ڣ //༈ private String id; //⥽ᆊⱘଃϔᷛ䆚 public class User { ҷⷕ⏙ऩ˖code\GameHall-Commons\src\org\crazyit\gamehall\commons\User.java ೒⠛ǃ䆹⥽ᆊⱘᷛ䆚ㄝDŽڣ༈ ៥ӀЎ␌៣໻ख़ᮄᓎϔϾ⥽ᆊ㉏ˈ⫼ᴹҷ㸼ϔϾ⥽ᆊˈ䆹㉏Ёֱᄬњ⥽ᆊⱘ໻໮᭄ֵᙃˈࣙᣀৡ⿄ǃ 15.2.2 ᓎゟ⥽ᆊ㉏Ϣ␌៣᥹ষ ᅲ՟ˈݡ䇗⫼ execute ᮍ⊩ेৃDŽ ࠄ䖭Ͼ໘⧚㉏ⱘᄫヺІৢˈৠḋഄˈ៥Ӏህৃҹḍ᥂䖭Ͼ㉏ৡᴹᕫࠄ݋ԧⱘᶤϾ ClientClass ⱘᅲ⦄㉏ བ᳈ᮄ⬠䴶㒘ӊㄝDŽResponse ᇍ䈵Ёࣙ৿ϔϾ actionClass ⱘ໘⧚㉏ˈ䆹㉏ህᰃᅶ᠋ッⱘ໘⧚㉏DŽᕫ ᳡ࡵ఼ッথ䗕ડᑨࠄᅶ᠋ッᯊˈᅶ᠋ッህḍ᥂ડᑨ˄Response˅Ёⱘ໘⧚㉏ᴹᠻ㸠ᶤѯ᪡԰ˈ՟ } void execute(Response response); //ᅶ᠋ッ໘⧚㉏ᠻ㸠 public interface ClientAction { //ᅶ᠋ッ໘⧚᳡ࡵ఼ડᑨⱘ᥹ষ ҷⷕ⏙ऩ˖code\GameHall-Commons\src\org\crazyit\gamehall\commons\ClientAction.java ৠḋⱘˈᮄᓎϔϾ ClientClass ᥹ষˈ㸼⼎ᅶ᠋ッ໘⧚㉏DŽClientActionDŽ ⱘ㸠ЎDŽ ⧚㉏䛑ᖙ乏ᅲ⦄ ServerAction ᥹ষˈ಴ℸᅲ⦄࣪䆹㉏ৢˈህৃҹⳈ᥹䇗⫼ execute ᮍ⊩ᴹᠻ㸠ᶤѯ݋ԧ 䗮䖛 Request ݋ԧⱘᶤϾ᳡ࡵ఼໘⧚㉏ৢˈህৃҹՓ⫼ Java ⱘডᇘᴹᅲ⦄࣪䆹㉏ˈ⬅Ѣ᳡ࡵ఼໘ } void execute(Request request, Response response, Socket socket); //Action ⱘᠻ㸠ᮍ⊩ public interface ServerAction { ᳡ࡵ఼໘⧚䇋∖ⱘ᥹ষ// /Download at http://www.pin5i.com ҹϞҷⷕЁⱘ getRequest ᮍ⊩ˈা㽕䗮䖛 XStreamUtil Ёⱘ toObject ᮍ⊩ेৃҹᇚᅶ᠋䇋∖ⱘᄫ } action.execute(request, response, this.socket); ServerAction action = getAction(request.getServerActionClass()); //ᕫࠄ Server ໘⧚㉏ copyParameters(request, response); //ᇚ䇋∖ⱘখ᭄䛑䆒㕂ࠄ Response Ё Response response = new Response(request.getClientActionClass()); //Ң request Ёᕫࠄᅶ᠋ッ໘⧚㉏, ᑊϨᵘ䗴 Response ᇍ䈵 Request request = getRequest(this.line); //ᕫࠄ䇋∖ᇍ䈵 while((this.line = br.readLine()) != null) { this.br = new BufferedReader(new InputStreamReader(this.socket.getInputStream())); ҷⷕ⏙ऩ˖code\GameHall-Server\src\org\crazyit\gamehall\server\ServerThread.java ServerThread Ёⱘ run ᮍ⊩DŽ ᕫࠄ Request ᇍ䈵ৢˈህৃҹᅲ՟࣪ Request Ё᠔ࣙ৿ⱘ᳡ࡵ఼໘⧚㉏DŽ ᇚḍ᥂䆹 Socket ᠔ᕫࠄⱘֵᙃ˄ᅶ᠋ッথ䗕ⱘֵᙃ˅䕀࣪Ў݋ԧⱘᶤϾ Request ᇍ䈵ˈ᳡ࡵ఼ⱘ㒓⿟ ServerThread ㉏㒻ᡓ Thread 㒓⿟㉏ˈ䖬䳔㽕䞡ݭ Thread ⱘ run ᮍ⊩ˈ೼ run ᮍ⊩Ёˈ៥Ӏ䳔㽕 } this.socket = socket; public ServerThread(Socket socket) { private Socket socket; ҷⷕ⏙ऩ˖code\GameHall-Server\src\org\crazyit\gamehall\server\ServerThread.java 㒓⿟ህৃҹϔⳈⲥ਀ᓎゟ䖭Ͼ Socket ⱘᅶ᠋ッ᠔থ䗕ⱘֵᙃDŽ 䆹㉏᥹ᬊࠄ Socket ৢˈህਃࡼ᳡ࡵ఼㒓⿟ˈ⫼Ѣ໘⧚᳡ࡵ఼ッ Socket ᥹ᬊࠄⱘֵᙃDŽ䙷М᳡ࡵ఼ } } } new ServerThread(s).start(); //ਃࡼ᳡ࡵ఼㒓⿟ Socket s = this.serverSocket.accept(); //ⲥ਀ Socket ⱘ䖲᥹ while(true) { this.serverSocket = new ServerSocket(12000); //߯ᓎ ServerSocket ᇍ䈵, ッষЎ 12000 public Server() { ServerSocket serverSocket; //᳡ࡵ఼ Socket ᇍ䈵 public class Server { ҷⷕ⏙ऩ˖code\GameHall-Server\src\org\crazyit\gamehall\server\Server.java ੠᥹ᬊ䖲᥹ࠄ᳡ࡵ఼ⱘ Socketˈݡҹ䆹 Socket ᴹਃࡼϔᴵ㒓⿟ᴹⲥ਀ᅶ᠋ッ᠔থ䗕ⱘ䇋∖DŽ ᳡ࡵ఼ッ㓪ݭ䕗Ўㅔऩˈা䳔㽕߯ᓎϔϾ ServerSocket ᇍ䈵ˈݡՓ⫼䆹ᇍ䈵ⱘ accept ᮍ⊩ᴹⲥ਀ 15.2.3 㓪ݭḚᶊ᳡ࡵ఼ ⦄ Game ᥹ষⱘ␌៣ᅲ⦄㉏ˈЎ␌៣ᦤկϔϾܹষDŽ Request ੠ Response ᇍ䈵᠔⫳៤ⱘ XML ᄫヺІ˗㄀Ѡˈ↣Ͼᬒ೼䆹ḚᶊЁⱘ␌៣䛑ᖙ乏ᦤկϔϾᅲ ࠄ䖭䞠ˈ៥Ӏⱘ䖭ϾḚᶊᅮНњϸϾ㾘㣗ˈ㄀ϔˈᅶ᠋ッϢ᳡ࡵ఼䖯㸠ֵᙃӴ䕧ⱘᯊ׭ˈা㛑Փ⫼ { /Download at http://www.pin5i.com བ೒ 15.1 ᠔⼎DŽ 䕧ֵܹᙃⱘϔϾ⬠䴶ˈ಴ℸৃҹᇚⱏᔩ⬠䴶ⱘⳌ݇㉏ᬒࠄᅶ᠋ッⱘ῵ഫ GameHall-Client ЁDŽⱏᔩ⬠䴶 ໻ख़˄ᶤϾ␌៣ⱘ໻ख़˅੠䕧ϢⳌ݇ⱘⱏᔩֵᙃDŽ⬅Ѣⱏᔩ⬠䴶ᑊϡϢᶤϾ⡍ᅮⱘ␌៣Ⳍ݇ˈাᰃ⫼᠋ Ḛᶊⱘᅶ᠋ッ䰸њᦤկϔϾᅶ᠋ッ㒓⿟㉏П໪ˈ䖬䳔㽕ᦤկϔϾⱏᔩ⬠䴶ˈ䅽⫼᠋䗝ᢽ䖯ܹⱘ␌៣ 15.2.5 ᓎゟⱏᔩ⬠䴶 ԩⱘϮࡵˈ䖭ѯϮࡵ䛑Ѹ⬅ᅶ᠋ッ៪㗙᳡ࡵ఼ッ໘⧚㉏䖯㸠DŽ೼ᴀゴЁᅶ᠋ッ῵ഫЎ GameHall-ClientDŽ ᶤϾ ClientAction ⱘᅲ⦄㉏ˈݡ䇗⫼ execute ᮍ⊩DŽϢ᳡ࡵ఼ッⱘᅲ⦄ϔḋˈᅶ᠋ッгᑊϡ䳔㽕໘⧚ӏ ḍ᥂᳡ࡵ఼䖨ಲⱘડᑨᄫヺІˈᇚ䖭ѯᄫヺІ䕀ᤶ៤ϔϾ Response ᇍ䈵ˈݡ䗮䖛䆹ᇍ䈵ᕫࠄ݋ԧ } action.execute(response); //ᠻ㸠ᅶ᠋ッ໘⧚㉏ ClientAction action = getClientAction(response.getActionClass()); //ᕫࠄᅶ᠋ッⱘ໘⧚㉏ Response response = getResponse(this.line); while ((this.line = br.readLine()) != null) { BufferedReader br = new BufferedReader(new InputStreamReader(is)); InputStream is = this.user.getSocket().getInputStream(); ҷⷕ⏙ऩ˖code\GameHall-Client\src\org\crazyit\gamehall\client\ClientThread.java 㓪ݭᅶ᠋ッ㒓⿟㉏ ClientThreadˈ䆹㉏ⱘЏԧҷⷕབϟDŽ ᗻ䆒㕂Ў Response ⱘ actionClassDŽ↣Ͼᅶ᠋ッ໘⧚㉏䛑ᖙ乏ᅲ⦄ ClientAction ᥹ষDŽ ᯢᅶ᠋ッ໘⧚㉏ˈгህᰃ䆒㕂 Request ᇍ䈵Ёⱘ clientActionClassˈ㗠೼᳡ࡵ఼໘⧚ᯊˈህӮᇚ䖭Ͼሲ ህ㾘ᅮњথ䗕䇋∖ᯊˈ䳔㽕ໄˈخ⫼ডᇘᴹᕫࠄᶤϾᅶ᠋ッ໘⧚㉏ⱘᅲ՟ˈݡ䇗⫼ execute ᮍ⊩DŽ䖭ḋ Response ᇍ䈵ⱘ XML˅ˈህⳈ᥹ḍ᥂ Response ᇍ䈵Ёⱘ actionClass ᴹᕫࠄᅶ᠋ッ໘⧚㉏ˈৠḋݡՓ ᠋ッࡴ䕑Ⳍᑨⱘ㉏DŽϢ᳡ࡵ఼ッⱘᅲ⦄ϔḋˈ䛑ᰃ䋳䋷ϔϾЁ䕀ⱘࡳ㛑ˈᔧ᥹ᬊࠄ᳡ࡵ఼ડᑨᯊ˄ᕫࠄ ⦄෎ᴀ㉏ԐDŽᔧ⫼᠋㓪ݭњϔϾᮄⱘ␌៣ᯊˈህৃҹⳈ᥹ᇚ␌៣ⱘࣙᬒ㕂ࠄᅶ᠋ッⱘⳂᔩЁˈ⫼Ѣ㒭ᅶ ⬅Ѣ៥Ӏ䖭Ͼ␌៣໻ख़ⱘḚᶊˈᑊϡ䳔㽕㓪ݭӏԩⱘϮࡵ䘏䕥ˈ಴ℸᅶ᠋ッⱘᅲ⦄Ϣ᳡ࡵ఼ッⱘᅲ 15.2.4 㓪ݭḚᶊᅶ᠋ッ 䛑ձ䌪Ѣ GameHall-Commons ῵ഫDŽ ClientClassǃUser ੠ Game ㄝ᥹ষϢ㉏ᬒࠄ GameHall-Commons ῵ഫЁˈᅶ᠋ッϢ᳡ࡵ఼ッⱘ῵ഫ ⫼ϔϾ GameHall-Commons ⱘ῵ഫᴹᄬᬒˈ՟བৃҹᇚ RequestǃResponseǃServerClassǃ ѯᮄⱘ␌៣ᯊˈ៥Ӏህ䳔㽕ᇚ䖭ѯ␌៣ⱘ῵ഫࡴܹࠄ⦃๗ব䞣ЁDŽϔѯ݀⫼ⱘ᥹ষ៪㗙㉏ˈ៥ӀৃҹՓ ⱘϮࡵ䘏䕥DŽ⬅Ѣ೼ ServerThread Ёˈ៥Ӏ䳔㽕Փ⫼ডᇘᴹᕫࠄᶤϾ᳡ࡵ఼໘⧚㉏ˈ಴ℸˈᔧࡴܹϔ ҹϡ⫼䖯㸠ҷⷕᬍবDŽGameHall ῵ഫাᰃ᳡ࡵ఼ッ䋳䋷໘⧚䕀থⱘϔϾЁ䯈㾦㡆ˈᅗϡ䋳䋷໘⧚ӏԩ ᣝ✻ҹϞⱘҷⷕᅲ⦄њ䆹῵ഫৢˈ䖭Ͼ᳡ࡵ఼῵ഫህৃҹϡݡ䳔㽕԰ᬍࡼˈेՓࡴܹᮄⱘ␌៣ˈᅗгৃ ࠄ䖭䞠ˈ៥ӀḚᶊⱘ᳡ࡵ఼ッᏆ㒣ܼ䚼ᅲ⦄њˈ೼ᴀゴⱘҷⷕЁˈᇍᑨⱘᰃ GameHall-Server ῵ഫˈ 䙷Мᔧ⫼᠋໮⃵䇋∖ৠϔϾ Action ⱘᯊ׭ˈህৃҹϡݡ䳔㽕䞡໡߯ᓎDŽ serverActionClass ሲᗻᕫࠄᶤϾᅲ⦄㉏ⱘᯊ׭ˈৃҹᇚᕫࠄⱘᅲ⦄㉏ᅲ՟ᬒࠄϔϾ Map Ё䖯㸠ֱᄬˈ ᕫࠄ݋ԧⱘᶤϾ᳡ࡵ఼໘⧚ Action ㉏ˈᠻ㸠 execute ᮍ⊩DŽ䳔㽕⡍߿⊼ᛣⱘᰃˈ䗮䖛 Request ⱘ ህৃҹᮄᓎϔϾ Response ᇍ䈵԰Ў᳡ࡵ఼ⱘડᑨˈᑊḍ᥂ Request ᇍ䈵Ёⱘ serverActionClass ሲᗻ ヺІ䕀ᤶ៤ϔϾ Request ᇍ䈵ˈXStreamUtil Ꮖ㒣೼ 15.2.1 Ёᅲ⦄DŽᔧ᳡ࡵ఼㒓⿟ᕫࠄ Request ᇍ䈵ৢˈ /Download at http://www.pin5i.com this.headSelect.setRenderer(new HeadComboBoxRenderer(this.heads)); this.headSelect.setMaximumRowCount(5); this.headSelect = new JComboBox(this.heads.keySet().toArray()); private void buildHeadSelect() { 䗝ᢽϟᢝڣ //߯ᓎ༈ ҷⷕ⏙ऩ˖code\GameHall-Client\src\org\crazyit\gamehall\client\LoginFrame.java ᭛ӊⱘⳌᇍ䏃ᕘ԰Ўؐᴹ߯ᓎϔϾ JComboBoxDŽҹϟᮍ⊩߯ᓎϔϾ JComboBox ᇍ䈵DŽڣⴔ䳔㽕ᇚ༈ ⊼ᛣҹϞҷⷕЁˈ䳔㽕ᕫࠄϟᢝḚ᠔䗝Ёⱘؐˈ䖭Ͼؐᖙ乏ᰃ Map ЁⱘᶤϾ keyˈ䖭ḋⱘ䆱ˈᛣੇ } return this; if (icon != null) setFont(list.getFont()); setIcon(icon); Icon icon = this.heads.get(selectValue); ೒⠛ڣⱘ Map Ёᕫࠄᔧࠡ䗝ᢽⱘ༈ڣ //Ң༈ } setForeground(list.getForeground()); setBackground(list.getBackground()); } else { setForeground(list.getSelectionForeground()); setBackground(list.getSelectionBackground()); if (isSelected) { //䆒㕂㚠᱃买㡆 String selectValue = (String)value; int index, boolean isSelected, boolean cellHasFocus) { public Component getListCellRendererComponent(JList list, Object value, ҷⷕ⏙ऩ˖code\GameHall-Client\src\org\crazyit\gamehall\client\HeadComboBoxRenderer.java ᦤկϔϾ ListCellRenderer ⱘᅲ⦄㉏DŽ ᇍ䏃ᕘˈvalue ህᰃ䆹೒⠛ⱘ ImageIcon ᇍ䈵DŽЎњ䅽ϟᢝḚЁ㛑ᰒ⼎೒⠛ˈ䳔㽕ЎϟᢝḚ JComboBox ೒⠛ⱘⳌڣ᭛ӊᇕ㺙៤ϔϾ Map ᇍ䈵ˈ䆹ᇍ䈵䞠䴶 key Ў༈ڣ᭛ӊⱘᯊ׭ˈ៥Ӏ䳔㽕ᇚ䇏পࠄⱘ༈ڣ ᭛ӊˈ䆹Ⳃᔩᄬ೼Ѣ GameHall-Client ῵ഫDŽ೼䇏প༈ڣᅲ⦄ˈ䳔㽕ࠄ⡍ᅮⱘᶤϾⳂᔩЁ䇏প᠔᳝ⱘ༈ ⱘϟᢝḚڣ೼ᴀゴЁˈⱏᔩ⬠䴶ᰃ GameHall-Client ⱘ LoginFrame ㉏DŽⱏᔩ⬠䴶䳔㽕⊼ᛣⱘᰃ༈ ೒ 15.1 ⱏᔩ⬠䴶  /Download at http://www.pin5i.com ҹϞⱘҷⷕᅲ⦄њⱏᔩࡳ㛑ˈҢ⬠䴶ⱘ␌៣ϟᢝḚЁᕫࠄ݋ԧⱘᶤϾ␌៣ˈ⬅Ѣ␌៣䗝ᢽϟᢝḚᰃ this.setVisible(false); thread.start(); ClientThread thread = new ClientThread(this.user); //ਃࡼ㒓⿟ game.start(this.user); Game game = (Game)this.gameSelect.getSelectedItem(); //ᕫࠄ⫼᠋᠔䗝ᢽⱘ␌៣ this.user.setSocket(createSocket(this.connectionField.getText(), 12000)); //߯ᓎ Socket ᑊЎ User ᇍ䈵䆒㕂 Socket ҷⷕ⏙ऩ˖code\GameHall-Client\src\org\crazyit\gamehall\client\LoginFrame.java ҹϟЎ LoginFrame Ёⱘ login ᮍ⊩DŽ ⱘᅲ⦄㉏˅ⱘ start ᮍ⊩DŽ ˄ᶤϾ␌៣ⱘܹষ㉏˅ˈᑊᇚᔧࠡⱘ⥽ᆊֵᙃᇕ㺙៤ϔϾ User ᇍ䈵ˈ԰Ўখ᭄䇗⫼␌៣ܹষ㉏˄Game ⥽ᆊ䕧ܹњⱏᔩⱘⳌֵ݇ᙃৢˈህৃҹ⚍ߏ䖯㸠ⱏᔩˈ೼䖯㸠ⱏᔩᯊˈ䳔㽕ᕫ␌៣䗝ᢽϟᢝḚⱘؐ 15.2.6 ᅲ⦄ⱏᔩࡳ㛑 ࠄ䖭䞠ˈ␌៣໻ख़ⱘⱏᔩ⬠䴶Ꮖ㒣ᅲ⦄ˈϟ䴶ᇣ㡖ᅲ⦄⥽ᆊⱘⱏᔩࡳ㛑DŽ ሲᗻˈ䖭ϾሲᗻᅮН䆹␌៣ⱘܹষ㉏DŽ ࡴ䕑ⱘࣙࡴܹњ䰤ࠊˈབᵰᰃϔϾ␌៣ᅶ᠋ッⱘࣙˈህᖙ乏೼ MANIFEST.MF ᭛ӊЁໄᯢ Game-Class ᮴ᔶПЁЎ៥ӀḚᶊ᠔خᶤϾ Game ⱘᅲ⦄㉏DŽ䖭Ͼ Game-Class ሲᗻⱘؐህᰃ䆹␌៣ⱘܹষ㉏DŽ䖭ḋ ˄MANIFEST.MF ᭛ӊ˅ˈᕫࠄ䞠䴶ໄᯢⱘ Game-Class ሲᗻˈݡ䗮䖛ডᇘᇚ䖭Ͼሲᗻؐ䕀ᤶ៤݋ԧⱘ ᭄᥂᭛ӊܗҹϞⱘҷⷕˈࠄ GameHall-Client Ёⱘ game Ⳃᔩ䇏প᠔᳝ⱘ jar ࣙˈݡ䇏প৘Ͼ } } } this.gameSelect.addItem(game); Game game = (Game)Class.forName(gameClass).newInstance(); if (gameClass != null) { String gameClass = gameClassAttrs.getValue("Game-Class"); //ᶹᡒ Game-Class ሲᗻ Attributes gameClassAttrs = mf.getMainAttributes(); //㦋ᕫ৘Ͼሲᗻ Manifest mf = jar.getManifest(); ᭄᥂᭛ӊܗ //ᕫࠄ JarFile jar = new JarFile(file); //ᕫࠄ jar ᭛ӊ if (isJar(file.getName())) { for (File file : folder.listFiles()) { File folder = new File("game"); ҷⷕ⏙ऩ˖code\GameHall-Client\src\org\crazyit\gamehall\client\LoginFrame.java ҹϟҷⷕ䇏প jar ࣙDŽ পֵࣙᙃⱘҷⷕDŽ 䴶䳔㽕ࠄᶤϾⳂᔩএ䇏পϔѯⳌ݇ⱘࣙˈᇚ䖭ѯࣙ᠔ҷ㸼ⱘ␌៣ᰒ⼎ࠄ␌៣ϟᢝЁDŽ಴ℸ䖬䳔㽕㓪ݭ䇏 㽕⡍߿⊼ᛣⱘᰃ␌៣ⱘ䗝ᢽϟᢝDŽ೼ᴀゴЁˈ៥ӀᏆ㒣⹂ᅮњ␌៣໻ख़াᰃϔϾㅔऩⱘḚᶊˈ೼ⱏᔩ⬠ ⱘ䗝ᢽ໪ˈ䖬䳔ڣMap ⱘ key ᴹ߯ᓎ JComboBox ⱘ䗝ᢽ乍DŽ䰸њ༈ ڣҹϞⱘ咥ԧҷⷕˈՓ⫼༈ { /Download at http://www.pin5i.com //Ḡᄤ೒⠛೼໻ख़Ёⱘᓔྟ X ᑻᷛ ҷⷕ⏙ऩ˖code\fivechess-commons\src\org\crazyit\gamehall\fivechess\commons\Table.java Table ᇍ䈵ࣙᣀབϟሲᗻDŽ public final static String RIGHT = "right"; public final static String LEFT = "left"; public final static int SEAT_HEIGHT = 30; //ᑻԡ催ᑺ public final static int SEAT_WIDTH = 30; //ᑻԡᆑᑺ private String side; //ᑻԡ䖍, া㛑Ўᴀ㉏ⱘ LEFT ੠ RIGHT ሲᗻؐ private Rectangle range; //ᑻԡⱘᑻᷛ㣗ೈ private ChessUser user; //䆹ᑻԡⱘ⥽ᆊ ҷⷕ⏙ऩ˖code\fivechess-commons\src\org\crazyit\gamehall\fivechess\commons\Seat.java Seat ᇍ䈵ࣙᣀབϟሲᗻDŽ ⱘḠᄤᇍ䈵DŽϔϾḠᄤЁ᳝ϸϾᑻԡˈ៥Ӏᓎゟ Seat ᇍ䈵ᴹ㸼⼎݋ԧⱘᶤϾᑻԡDŽ ⬅ѢѨᄤẟЁ↣ϾḠᄤা᳝ϸϾᑻԡˈ಴ℸ៥ӀЎѨᄤẟ␌៣ᮄᓎϔϾ Table ᇍ䈵ᴹ㸼⼎䖭Ͼ␌៣ private boolean ready; ໛␌៣ޚ //ᰃ৺Ꮖ㒣 ChessUser ࣙᣀҹϟሲᗻ˖ ᇍ䈵䛑ࣙᣀϡৠⱘݙᆍDŽᮄᓎϔϾ ChessUser ㉏ˈ㒻ᡓ User ㉏ˈ䆹㉏ҷ㸼ϔϾѨᄤẟ⥽ᆊᇍ䈵DŽ ⱘḠᄤৃ㛑Ӯ᳝ಯϾᑻԡˈ㗠Ѩᄤẟা䳔㽕ϸϾᑻԡˈ㗠Ϩ↣Ͼ␌៣䛑Ӯ᳝㞾Ꮕⱘ⥽ᆊᇍ䈵ˈ䖭ѯ⥽ᆊ ␌៣໻ख़Ё᳔෎ᴀⱘऩԡህᰃḠᄤˈԚᰃ↣⾡␌៣ⱘ␌៣໻ख़䛑ᑊϡϔ㟈ˈ՟བ᭫ഄЏ␌៣໻ख़Ё 15.3.1 㓪ݭ␌៣໻ख़ⱘᇍ䈵 ЁDŽ 䖭ѯᇍ䈵ৃ㛑೼ᅶ᠋ッˈгৃ㛑೼᳡ࡵ఼ッՓ⫼ࠄˈ಴ℸ៥Ӏᇚ䖭ѯᇍ䈵ᦤপߎᴹᬒࠄϔϾ݀݅ⱘ῵ഫ ᣀϔ㋏߫ⱘᇍ䈵ˈࣙᣀḠᄤǃԡ㕂ㄝDŽ៥Ӏᇚ䖭ѯᇍ䈵ᬒࠄϔϾ fivechess-commons ⱘ῵ഫЁDŽ⬅Ѣ 㓪ݭ␌៣໻ख़Ḛᶊⱘᯊ׭ˈህᅮϟ㾘߭ˈᖙ乏Ў␌៣ᓎゟϔϾܹষ㉏DŽ঺໪ˈѨᄤẟⱘ␌៣໻ख़Ё䖬ࣙ ៥ӀᏆ㒣೼ 15.2 Ё㓪ݭњ␌៣໻ख़ⱘ෎ᴀḚᶊˈ䙷Мᴀᇣ㡖ᓔྟ㓪ݭϔϾѨᄤẟⱘ␌៣໻ख़DŽ೼ 15.3 ᓎゟѨᄤẟ␌៣໻ख़ 㓪ݭᅠ᳔෎ᴀⱘḚᶊৢˈϟ䴶ゴ㡖㓪ݭϔϾѨᄤẟ␌៣໻ख़DŽ ‰ ᳡ࡵ఼৥ᅶ᠋ッথ䗕ֵᙃᯊˈᖙ乏ᰃҷ㸼ϔϾ Response ᇍ䈵ⱘ XML ᄫヺІDŽ ‰ ᅶ᠋ッ৥᳡ࡵ఼থ䗕ֵᙃᯊˈᖙ乏ᰃҷ㸼ϔϾ Request ᇍ䈵ⱘ XML ᄫヺІ˗ ষ㉏˗ ‰ ↣Ͼ␌៣ᠧ៤ jar ࣙৢˈ䛑䳔㽕೼ MANIFEST.MF ᭛ӊЁࡴܹ Game-Class ሲᗻᴹໄᯢ␌៣ܹ ‰ ↣Ͼ␌៣䛑ᖙ乏ᦤկϔϾ␌៣ܹষ㉏˄Game ᥹ষⱘᅲ⦄㉏˅˗ 乏䙉ᅜ䆹ḚᶊᅮНⱘϔѯ㾘߭DŽ䖭ѯ㾘߭ࣙᣀ˖ ࠄℸˈ␌៣໻ख़ⱘ෎ᴀḚᶊᏆ㒣ᅠ៤ˈ៥Ӏৃҹ೼䖭ϾḚᶊⱘ෎⸔Ϟᓔথ৘⾡␌៣ˈԚᰃࠡᦤᰃᖙ ᅶ᠋ッ㒓⿟DŽ Փ⫼݋ԧⱘᶤϾ␌៣㉏ᴹ߯ᓎⱘˈ಴ℸᕫࠄ䆹㉏ৢˈⳈ᥹ᔎࠊ䕀ᤶ៤ Gameˈݡᠻ㸠 start ᮍ⊩ᑊਃࡼ /Download at http://www.pin5i.com for (int j = 0; j < tables[i].length; j++) { for (int i = 0; i < tables.length; i++) { int tableNumber = 0; tables = new Table[TABLE_COLUMN_SIZE][TABLE_ROW_SIZE]; //߱ྟ࣪Ḡᄤֵᙃ static { public static Table[][] tables = new Table[TABLE_COLUMN_SIZE][TABLE_ROW_SIZE]; //ֱᄬḠᄤֵᙃ ҷⷕ⏙ऩ˖code\fivechess-server\src\org\crazyit\gamehall\fivechess\server\ChessContext.java ⫼ϔϾ ChessContext ⱘ㉏ᴹֱᄬ䖭ѯֵᙃDŽ ಴ℸ fivechess-server ῵ഫӮձ䌪Ѣ fivechess-commons ῵ഫDŽ␌៣໻ख़᭄㒘ֱᄬ೼᳡ࡵ఼ッˈ៥ӀՓ ッ೼ᴀゴЁՓ⫼ⱘᰃ fivechess-server ῵ഫˈ␌៣໻ख़ⱘ޴Ͼᇍ䈵䛑ֱᄬ೼ fivechess-commons ῵ഫˈ ⹂ᅮњ␌៣໻ख़ⱘ޴Ͼᇍ䈵ৢˈ៥Ӏህৃҹ೼᳡ࡵ఼Ё߯ᓎ␌៣໻ख़ⱘ᭄㒘ˈѨᄤẟ␌៣ⱘ᳡ࡵ఼ 15.3.2 ᳡ࡵ఼߯ᓎ␌៣໻ख़᭄㒘 䖭䞠䳔㽕⊼ᛣⱘᰃˈԡ㕂ⱘ݋ԧԡ㕂⬅ᅗ᠔೼ⱘḠᄤ⹂ᅮDŽ public Seat(ChessUser user, Rectangle range, String side) ҷⷕ⏙ऩ˖code\fivechess-commons\src\org\crazyit\gamehall\fivechess\commons\Seat.java Seat ᇍ䈵ⱘᵘ䗴఼DŽ Seat.SEAT_WIDTH, Seat.SEAT_HEIGHT), Seat.RIGHT); this.rightSeat = new Seat(null, new Rectangle(getRightSeatBeginX(), getRightSeatBeginY(), Seat.SEAT_WIDTH, Seat.SEAT_HEIGHT), Seat.LEFT); this.leftSeat = new Seat(null, new Rectangle(getLeftSeatBeginX(), getLeftSeatBeginY(), //߯ᓎḠᄤᇍ䈵ⱘᯊ׭ህ߯ᓎᎺেⱘ Seat ᇍ䈵 ҷⷕ⏙ऩ˖code\fivechess-commons\src\org\crazyit\gamehall\fivechess\commons\Table.java Table ⱘᵘ䗴఼DŽ Ϩ䆒㕂Ⳍᑨⱘതᷛԡ㕂DŽ Ў Table ᇍ䈵ᦤկϔϾᵘ䗴఼ˈ೼߯ᓎ Table ⱘᯊ׭ˈህ䳔㽕ৠᯊ߯ᓎᎺ䖍Ϣে䖍ⱘᑻԡᇍ䈵ˈᑊ Seat ᇍ䈵䛑ᦤկњ range ሲᗻˈ㸼⼎Ḡᄤ៪㗙ԡ㕂ⱘ೒⠛೼໻ख़Ё݋ԧԡ㕂㣗ೈDŽ Ḡᄤᇍ䈵Ё߭ᦤկњϸϾᑻԡᇍ䈵˖leftSeat ੠ rightSeatˈ㸼⼎ϔᓴḠᄤЁা㛑᳝ϸϾԡ㕂DŽTable Ϣ 䖭ḋˈ៥ӀህᅮНњḠᄤ੠ԡ㕂ⱘᇍ䈵ˈ䖭䞠䳔㽕⊼ᛣⱘᰃˈԡ㕂ᑊϡ䳔㽕ⶹ䘧㞾ᏅሲѢાᓴḠᄤˈ private Seat rightSeat; //ে䖍ⱘᑻԡ private Seat leftSeat; //Ꮊ䖍ⱘᑻԡ private Rectangle range; //䆹 Table ᇍᑨⱘ㣗ೈ public final static int DEFAULT_IMAGE_HEIGHT = 140; //咬䅸ⱘ೒⠛催 public final static int DEFAULT_IMAGE_WIDTH = 140; //咬䅸ⱘ೒⠛ᆑ private int tableNumber; //Ḡᄤো private String tableImage; //Ḡᄤⱘ೒⠛ private int beginY; //Ḡᄤ೒⠛೼໻ख़Ёⱘᓔྟ Y ᑻᷛ private int beginX; /Download at http://www.pin5i.com ChessContext.users.put(cu.getId(), cu); //ࡴܹࠄ᠔᳝⥽ᆊЁ cu.setSocket(socket); ChessUser cu = (ChessUser)request.getParameter("user"); //Ң䇋∖খ᭄Ёᕫࠄ ChessUser public void execute(Request request, Response response, Socket socket) { code\fivechess-server\src\org\crazyit\gamehall\fivechess\server\action\LoginAction.java ҷⷕ⏙ऩ˖ ࡵ఼Ёˈݡᇚ␌៣໻ख़ᔧࠡ᠔᳝ⱘֵᙃথ䗕㒭ⱏᔩⱘ⥽ᆊDŽ ᖙ乏ᇚ⥽ᆊⱘֵᙃֱᄬࠄ᳡ܜࠡˈ៥Ӏ䳔㽕ᯢⱑ䖭 Action 䳔㽕䖯㸠ⱘϔѯࡼ԰ˈ⥽ᆊ䖯ܹ␌៣໻ख़ˈ佪 ᰃ LoginAction ԡ㕂 fivechess-server ῵ഫЁˈ㸼⼎䆹㉏ᰃሲѢ᳡ࡵ఼ᠻ㸠ⱘ㉏DŽ೼㓪ݭ LoginAction ࠄ݋ԧⱘᶤϾ ServerAction ᅲ⦄㉏Ёˈ䖭䞠ⱘ LoginAction ህᰃ៥Ӏⱘ᳡ࡵ఼໘⧚㉏DŽ䖭䞠䳔㽕⊼ᛣⱘ ໘⧚䇋∖ⱘᰃ Game-Server ῵ഫˈ䆹῵ഫᇚ⥽ᆊⱘ䇋∖䕀থܜᔧ⥽ᆊথ䗕њ䇋∖ࠄ᳡ࡵ఼ৢˈ᳔ LoginActionˈᅶ᠋ッ໘⧚㉏ᰃ ClientInActionDŽ ਞ䆝᳡ࡵ఼ˈᮄⱘ⥽ᆊ䖯ܹњѨᄤẟ␌៣໻ख़DŽ೼ҹϞҷⷕЁˈໄᯢњ᳡ࡵ఼໘⧚㉏ᰃ᳡ࡵ఼ッⱘ ݡᇚᔧࠡⱘѨᄤẟ⥽ᆊᇍ䈵䆒㕂ࠄѨᄤẟ␌៣ⱘϞϟ᭛ᇍ䈵Ёˈ᳔ৢˈᵘ䗴ϔ⃵䇋∖ˈথ䗕ࠄ᳡ࡵ఼ˈ ᇚ⥽ᆊᇍ䈵˄User˅ᇍ䈵䕀ᤶ៤ϔϾѨᄤẟ⥽ᆊᇍ䈵˄ChessUser˅ˈܜChessGame ⱘ start ᮍ⊩ˈ佪 } cu.getPrintStream().println(XStreamUtil.toXML(req)); //ᕫࠄ⥽ᆊⱘ Socket ᑊথ䗕䇋∖, ਞ䆝᳡ࡵ఼⥽ᆊ䖯ܹњ໻ख़ req.setParameter("user", cu); "org.crazyit.gamehall.fivechess.client.action.ClientInAction"); Request req = new Request("org.crazyit.gamehall.fivechess.server.action.LoginAction", //ᵘ䗴ϔ⃵䇋∖, ਞ䆝᳡ࡵ఼⥽ᆊ䖯ܹ໻ख़, ᳡ࡵ఼ડᑨ໘⧚㉏ᰃ LoginAction ChessClientContext.chessUser = cu; ChessUser cu = convertUser(user); //ᕫࠄ䖯ܹ␌៣ⱘ⥽ᆊֵᙃ public void start(User user) { ҷⷕ⏙ऩ˖code\fivechess-client\src\org\crazyit\gamehall\fivechess\client\action\ChessGame.java ᅶ᠋ッᦤկϔϾܹষ㉏ˈᅲ⦄ Game ᥹ষDŽ 㽕೼ MANIFEST.MF ᭛ӊЁໄᯢϔϾ Game-Class ⱘሲᗻˈ㸼⼎䆹␌៣ⱘܹষ㉏DŽϟ䴶ЎѨᄤẟⱘ␌៣ ϔϾ fivechess-client ⱘ῵ഫˈ㸼⼎Ѩᄤẟ␌៣໻ख़ⱘᅶ᠋ッDŽᣝ✻Пࠡⱘ㾘߭ˈᦤկ␌៣ᅶ᠋ッˈ䳔 䇗⫼ Game ᅲ⦄㉏ⱘ start ᮍ⊩DŽ៥Ӏᮄᓎܜ⥽ᆊ䗮䖛ⱏᔩ⬠䴶ˈ䗝ᢽ݋ԧ䖯ܹⱘᶤϾ␌៣ˈህ佪 15.3.3 ⥽ᆊ䖯ܹ␌៣໻ख़ ᕅડӴ䕧ᗻ㛑DŽ Փ⫼ϔѯ໻ᇍ䈵ˈ՟བ Image ᇍ䈵ㄝˈ৺߭ᇚᇍ䈵䕀ᤶ៤ XML ⱘᯊ׭ˈᇚ⫳៤໻䞣ⱘ XML ᄫヺІˈܡ 㒘䆒㕂ࠄ Response ⱘ᭄᥂߫㸼Ёˈ䖨ಲ㒭᠔᳝ⱘ⫼᠋DŽᇍ䈵Ϣ XML ᄫヺІ䖯㸠Ѧ䕀ⱘᯊ׭ˈሑ䞣䙓 ఼ッህֱᄬњϔϾḠᄤⱘѠ㓈᭄㒘ˈᔧ᳝ᮄⱘ⥽ᆊ䖯ܹѨᄤẟ␌៣໻ख़ⱘᯊ׭ˈህᇚ䖭ϾḠᄤⱘѠ㓈᭄ ᓎゟϔϾ Table ⱘѠ㓈᭄㒘ˈᑊ೼ ChessContext ㉏ⱘ߱ྟ࣪ഫЁᓎゟ䖭Ͼ᭄㒘DŽህ䖭ḋˈ೼᳡ࡵ } } } tableNumber++; tables[i][j] = table; Table.DEFAULT_IMAGE_HEIGHT*j, tableNumber); Table table = new Table(Table.DEFAULT_IMAGE_WIDTH*i, /Download at http://www.pin5i.com ᬜᵰབ೒ 15.2 ᠔⼎DŽ ⱘخ␌៣໻ख़⬠䴶೼ᴀゴᇍᑨⱘᰃ GameHallFrame ㉏ˈ䆹㉏೼ fivechess-client ῵ഫЁDŽ៥Ӏ䳔㽕 15.3.4 ߯ᓎ␌៣໻ख़⬠䴶 ␌៣໻ख़ⱘ৘Ͼ⬠䴶DŽ ⱘડᑨЁᏆ㒣ࣙᣀ߯ᓎ␌៣໻ख़᠔䳔ⱘ৘ϾֵᙃˈࣙᣀḠᄤˈ⥽ᆊㄝDŽ៥Ӏ᱖ᯊϡᦤկᅲ⦄ˈϟ䴶߯ᓎ ⦄㉏䖯㸠໘⧚DŽClientInAction ᥹ᬊࠄ᳡ࡵ఼ⱘડᑨৢˈህ䳔㽕Ў߮ⱏᔩⱘ⥽ᆊ߯ᓎ␌៣໻ख़ˈ᳡ࡵ఼ ClientThread ㉏䖯㸠໘⧚ˈ䆹㉏ৠḋഄ䋳䋷䕀থˈএᇏᡒ݋ԧⱘᶤϾᅶ᠋ッ໘⧚㉏˄ClientAction˅ⱘᅲ ᰃথ䗕㒭 fivechess-client ῵ഫⱘܜ˄ClientInAction˅䋳䋷໘⧚䖭ϔ⃵᳡ࡵ఼ડᑨDŽ᳡ࡵ఼ડᑨ佪 䖭ϔ⃵䇋∖ˈ✊ৢᇚ Response ᇍ䈵䕀ᤶ៤ XML ᄫヺІ䖨ಲ㒭ᅶ᠋ッˈ᥹ϟᴹˈህᰃᅶ᠋ッ໘⧚㉏ ԧⱘᶤϾѨᄤẟ⥽ᆊᇍ䈵DŽ⥽ᆊ䗮䖛 ChessGame ᴹথ䗕㄀ϔ⃵䇋∖㒭᳡ࡵ఼ˈLoginAction 䋳䋷໘⧚ ೼᳡ࡵ఼Ϟϟ᭛ЁՓ⫼ϔϾ Map ᴹֱᄬ⥽ᆊֵᙃˈ䖭Ͼ Map ⱘ key ᰃ⥽ᆊᇍ䈵ⱘ idˈvalue ᰃ݋ public static Map users = new HashMap(); ֱᄬ⥽ᆊֵᙃ˖ ៥Ӏᇚ䖯ܹ␌៣໻ख़ⱘ⥽ᆊֱᄬࠄ᳡ࡵ఼Ϟϟ᭛Ёˈ಴ℸ䳔㽕೼ ChessContext Ё⏏ࡴϔϾሲᗻᴹ } cu.getPrintStream().println(XStreamUtil.toXML(response)); //䖨ಲ㒭ⱏᔩ⥽ᆊ, ⱏᔩ៤ࡳ response.setData("tableRowSize", ChessContext.TABLE_ROW_SIZE); response.setData("tableColumnSize", ChessContext.TABLE_COLUMN_SIZE); //ᇚ໻ख़ЁḠᄤⱘ᭄߫੠㸠᭄䖨ಲࠄᅶ᠋ッ response.setData("tables", ChessContext.tables); //ᇚ᠔᳝ⱘḠᄤֵᙃ䖨ಲࠄᅶ᠋ッ response.setData("users", ChessContext.users); //ᇚ᠔᳝⥽ᆊֵᙃ䆒㕂ࠄડᑨЁ response.setData("user", cu); ᇚ⥽ᆊ䆒㕂ࠄડᑨЁ// /Download at http://www.pin5i.com table.getLeftSeatBeginY(), this); g.drawImage(head, table.getLeftSeatBeginX(), Image head = getHeadImage(leftSeat.getUser().getHeadImage()); if (leftSeat.getUser() != null) { //⬏Ꮊ䖍ᑻԡⱘ⥽ᆊ table.getBeginY(), this); g.drawImage(tableImage, table.getBeginX(), //⬏Ḡᄤⱘ೒⠛ Seat rightSeat = table.getRightSeat(); Seat leftSeat = table.getLeftSeat(); Table table = this.tables[i][j]; for (int j = 0; j < this.tables[i].length; j++) { for (int i = 0; i < this.tables.length; i++) { public void paint(Graphics g) { ҷⷕ⏙ऩ˖code\fivechess-client\src\org\crazyit\gamehall\fivechess\client\ui\HallPanel.java ҹϟᰃ HallPanel ⱘ paint ᮍ⊩ˈЏ㽕⫼Ѣ㒬⬏⬠䴶ЁⱘḠᄤϢ⥽ᆊDŽ ៥Ӏৃҹᕫࠄ᠔᳝ⱘḠᄤֵᙃˈ✊ৢݡḍ᥂䖭ѯḠᄤⱘֵᙃএ߯ᓎ HallPanelDŽ ⃵䇋∖ࠄ᳡ࡵ఼পܼ䚼ⱘḠᄤֵᙃˈݡ⬅ᅶ᠋ッⱘ ClentInAction 䋳䋷߯ᓎ␌៣⬠䴶DŽ೼᳡ࡵ఼ડᑨЁ Ӯ䇗⫼ ChessGame ⱘ start ᮍ⊩ˈ䆹ᮍ⊩Ӯথ䗕ϔܜᔧⱏᔩ䖯ܹѨᄤẟ␌៣໻ख़ⱘᯊ׭ˈ៥Ӏ佪 ⱘ HallPanel ᇍ䈵DŽ JPanelˈ⥽ᆊ߫㸼ⱘ JTable ᇍ䈵Ϣ㘞໽ᇍ䈵⬠䴶䕗Ўㅔऩˈ໡ᴖⱘᰃ໻ख़ᇍ䈵ˈ䆹ᇍ䈵೼ᴀゴЁᇍᑨ Ѩᄤẟ␌៣໻ख़Џ㽕ࣙᣀϔϾᄬᬒḠᄤⱘ JPanelˈᄬᬒ᠔᳝⥽ᆊⱘ JTable ᇍ䈵ˈЏ㽕⫼Ѣ㘞໽ⱘ ೒ 15.2 ␌៣໻ख़⬠䴶  /Download at http://www.pin5i.com 䰈ᕅ೒⠛Ϣ哴ᷛᣛ䩜ˈ݋ԧⱘᬜᵰབ೒ 15.3 ᠔⼎DŽ ᔧ哴ᷛ⿏ࡼࠄ᳝Ҏⱘԡ㕂ᯊˈህϡ᳈ᬍ䰈ᕅ೒⠛ˈབᵰ哴ᷛ⿏ࡼࠄ≵᳝Ҏⱘᑻԡᯊˈᠡ᳈ᤶᑻԡⱘ } this.repaint(); this.setCursor(DEFAULT_CURSOR); } else { } table.getRightSeatBeginY(), this); g.drawImage(seatSelectImage, table.getRightSeatBeginX(), if (table.getRightSeat().getUser() == null) { //བᵰԡ㕂Ϟ≵᳝Ҏᠡ᳈ᤶ೒⠛ this.setCursor(HAND_CURSOR); //ে䖍ᑻԡ } else if (table.getRightSeat().getRange().contains(x, y)) { } table.getLeftSeatBeginY(), this); g.drawImage(seatSelectImage, table.getLeftSeatBeginX(), if (table.getLeftSeat().getUser() == null) { //བᵰԡ㕂Ϟ≵᳝Ҏᠡ᳈ᤶ೒⠛ this.setCursor(HAND_CURSOR); //Ꮊ䖍ᑻԡ if (table.getLeftSeat().getRange().contains(x, y)) { ҷⷕ⏙ऩ˖code\fivechess-client\src\org\crazyit\gamehall\fivechess\client\ui\HallPanel.java ⥽ᆊ⿏ࡼ哴ᷛⱘᯊ׭ˈህ㾺থ mouseMove ᮍ⊩ˈ䆹ᮍ⊩ⱘЏԧҷⷕབϟDŽ }); } moveMouse(e); public void mouseMoved(MouseEvent e) { this.addMouseMotionListener(new MouseMotionAdapter() { ҷⷕ⏙ऩ˖code\fivechess-client\src\org\crazyit\gamehall\fivechess\client\ui\HallPanel.java Ў哴ᷛ⿏ࡼ⏏ࡴџӊDŽ ࠄ᳝䰈ᕅⱘᬜᵰDŽخ䴶ⱘᯊ׭ˈህ䳔㽕ᐂ䖭Ͼԡ㕂᳈ᤶ೒⠛ˈ䅽݊ DŽ⬏ᅠḠᄤৢˈ៥Ӏ䳔㽕໘⧚哴ᷛџӊˈᔧ哴ᷛⱘᣛ䩜⿏ࡼࠄᶤϾԡ㕂Ϟڣⱘ䆱ˈህ⬏ϞⳌᑨⱘ⥽ᆊ༈ HallPanel ᕫࠄ৘ϾḠᄤⱘѠ㓈᭄㒘ৢˈḍ᥂䖭ϾѠ㓈᭄㒘এ㒬⬏ḠᄤˈབᵰḠᄤЁⱘᑻԡ᳝⥽ᆊ } } } } table.getRightSeatBeginY(), this); g.drawImage(head, table.getRightSeatBeginX(), Image head = getHeadImage(rightSeat.getUser().getHeadImage()); if (rightSeat.getUser() != null) { ⬏ে䖍ᑻԡⱘ⥽ᆊ// { /Download at http://www.pin5i.com 䖭ᰃ⬅Ѣབᵰ䖯㸠㘞໽ˈህ䳔㽕䅽⥽ᆊ䗝ᢽ㘞໽ⱘᇍ䈵ˈݡথ䗕㒭᳡ࡵ఼DŽ 䗕ⱘᣝ䪂DŽ䳔㽕␌៣ⱘᰃˈᵘᓎ ChatPanel ᯊˈг䳔㽕ᇚ᠔᳝ⱘ⥽ᆊϢᔧࠡⱘ⥽ᆊ䛑԰Ўᵘ䗴খ᭄Ӵܹˈ ChatPanel Ёˈা䳔㽕߯ᓎϔѯ෎ᴀⱘ⬠䴶㒘ӊेৃˈࣙᣀϔϾ JTextAreaǃϔϾ JTextField ੠ϔϾথ བ೒ 15.2 ᠔⼎ˈ䰸њ⥽ᆊ߫㸼໪ˈ䖬᳝㘞໽ⱘ⬠䴶ˈ㘞໽⬠䴶೼ᴀゴЁՓ⫼ ChatPanel 㸼⼎DŽ೼ } return result; } result.add(v); v.add(getSex(user.getSex())); v.add(user.getName()); v.add(getHead(user.getHeadImage())); v.add(user.getId()); Vector v = new Vector(); for (ChessUser user : this.users) { Vector result = new Vector(); private Vector getDatas() { //ᕫࠄ⥽ᆊ߫㸼ⱘ᭄᥂Ḑᓣ ҷⷕ⏙ऩ˖code\fivechess-client\src\org\crazyit\gamehall\fivechess\client\ui\UserTable.java List ᴹֱᄬ᠔᳝ⱘ⥽ᆊֵᙃˈᦤկϔϾ getDatas ⱘᮍ⊩ᴹ䕀ᤶ⥽ᆊ䲚ড়DŽ ᳔ৢᇚ৘Ͼ⥽ᆊⱘֵᙃ䕀ᤶ៤߫㸼ᰒ⼎ⱘ᭄᥂㉏ൟᰒ⼎ࠄ߫㸼Ёˈ೼ UserTable Ёˈ៥ӀՓ⫼ϔϾ } return this; else this.setBackground(Color.WHITE); if (isSelected) this.setBackground(Color.YELLOW); Ḑⱘ࣫᱃买㡆ܗ //䆒㕂ऩ else this.setText(value.toString()); if (value instanceof Icon) this.setIcon((Icon)value); //䆒㕂ᰒ⼎೒⠛ setHorizontalAlignment(SwingConstants.CENTER); boolean isSelected, boolean hasFocus, int row, int column) { public Component getTableCellRendererComponent(JTable table, Object value, code\fivechess-client\src\org\crazyit\gamehall\fivechess\client\ui\UserTableCellRenderer.java ҷⷕ⏙ऩ˖ ೒⠛˖ڣⱘ೒⠛DŽ㓪ݭϔϾ DefaultTableCellRenderer ⱘᄤ㉏ᴹ໘⧚ᰒ⼎༈ ⬅Ѣ⥽ᆊ߫㸼Ё⍝ঞ೒⠛ⱘᰒ⼎ˈৠḋгᰃ䳔㽕ᦤկϔϾ DefaultTableCellRenderer ᴹᰒ⼎Ⳍᑨ߫ Ḑᑊϡৃҹ㓪䕥DŽ䳔㽕ᇚ㞾Ꮕᬒࠄ᠔᳝⥽ᆊⱘ᳔ࠡ䴶DŽܗ߫㸼䳔㽕⊼ᛣⱘᰃˈ߫㸼ⱘऩ UserTable ⱘᅲ⦄䕗Ўㅔऩˈা䳔㽕ᕫࠄ݋ԧⱘѨᄤẟ⥽ᆊ߫㸼ˈህৃҹḍ᥂䖭ѯ⥽ᆊᴹ߯ᓎ߫㸼DŽ䆹 䖭ֵ᳝ᙃৢˈህᇚ᠔᳝ⱘ⥽ᆊ䆒㕂ࠄ⥽ᆊ߫㸼ЁDŽ೼ᴀゴˈ⥽ᆊ߫㸼Փ⫼ⱘᰃ UserTable ᴹ㸼⼎DŽ ⥽ᆊⱏᔩ䖯ܹ␌៣໻ख़ⱘᯊ׭ˈ᳡ࡵ఼䳔㽕ᇚ᠔᳝⥽᳝ⱘֵᙃথ䗕㒭ⱏᔩⱘ⥽ᆊˈ⬠䴶ᕫࠄ䖭ѯ⥽ 15.3.5 ߯ᓎ⥽ᆊ߫㸼Ϣ㘞໽⬠䴶 哴ᷛᣛ䩜⿏ࡼࠄぎᑻԡϞᯊ 15.3  /Download at http://www.pin5i.com //ϡᖙথ䗕㒭㞾Ꮕ ChessUser hasLogin = ChessContext.users.get(id); for (String id : ChessContext.users.keySet()) { //৥᠔᳝⥽ᆊথ䗕ֵᙃ response.setData("newUser", user); //ᇚᮄ⥽ᆊֵᙃᬒࠄડᑨЁ ChessUser user = ChessContext.users.get(userId); String userId = (String)request.getParameter("userId"); //ᕫࠄᮄⱏᔩⱘ⥽ᆊ public void execute(Request request, Response response, Socket socket) { ҷⷕ⏙ऩ˖code\GameHall-Server\src\org\crazyit\gamehall\server\action\NewUserInAction.java ৢḍ᥂䖭Ͼ䇋∖ᕫࠄⳌᑨⱘ⥽ᆊ idˈݡਞ䆝䖭Ͼ໻ख़Ёⱘ݊Ҫ⥽ᆊˈ᳝ᮄⱘ⥽ᆊ䖯ܹњ␌៣໻ख़њDŽ 㒭ᅶ᠋ッ໘⧚㉏ᰃ ReceiveInActionDŽNewUserInAction ᰃ᳡ࡵ఼᥹ᬊᮄ⥽ᆊ䖯ܹ␌៣໻ख़ⱘ䇋∖ˈ✊ sendUserIn ᮍ⊩䞡ᮄᵘ䗴ϔ⃵䇋∖ˈᑊথ䗕㒭᳡ࡵ఼ˈ᳡ࡵ఼໘⧚㉏ᰃ NewUserInActionˈ䖨ಲ this.user.getPrintStream().println(XStreamUtil.toXML(req)); //ᕫࠄ⫼᠋ⱘ Socket ᑊথ䗕䇋∖, ਞ䆝᳡ࡵ఼⫼᠋䖯ܹњ໻ख़ req.setParameter("userId", this.user.getId()); "org.crazyit.gamehall.fivechess.client.action.ReceiveInAction"); Request req = new Request("org.crazyit.gamehall.fivechess.server.action.NewUserInAction", //ᵘ䗴ϔ⃵䇋∖, ਞ䆝᳡ࡵ఼⫼᠋䖯ܹ໻ख़, ᳡ࡵ఼ડᑨ໘⧚㉏ᰃ ReceiveInAction ҷⷕ⏙ऩ˖code\fivechess-client\src\org\crazyit\gamehall\fivechess\client\ui\GameHallFrame.java ܹ␌៣໻ख़њDŽҹϟᰃ sendUserIn ᮍ⊩ⱘЏ㽕ҷⷕDŽ sendUserIn ᮍ⊩ˈ䖭Ͼᮍ⊩Џ㽕⫼Ѣᅶ᠋ッথ䗕䇋∖ࠄ᳡ࡵ఼ˈਞ䆝᳡ࡵ఼ˈᔧࠡⱘ⥽ᆊᏆ㒣៤ࡳ䖯 ᅶ᠋ッᇚ᥹ᬊࠄⱘ᭄᥂߯ᓎ␌៣໻ख़ৢˈ䖬䳔㽕䇗⫼␌៣໻ख़ᇍ䈵˄GameHallFrame˅ⱘ ⱘ LoginAction ЁᏆ㒣䆒㕂DŽ 䞠ˈ಴ℸা䳔㽕೼ᅶ᠋ッ໘⧚㉏Ё䗮䖛 getDatas(key)ᮍ⊩ህৃҹᕫࠄ䖭ѯ᭄᥂ˈ䖭ѯ᭄᥂೼ 15.3.3 Ё ⱘֵᙃˈ⬅Ѣ᳡ࡵ఼໘⧚㉏೼᥹ᬊ⫼᠋䖯ܹ␌៣໻ख़ֵᙃⱘᯊ׭ˈህᏆ㒣ᇚ䖭ѯֵᙃ䆒㕂ࠄ᳡ࡵ఼ડᑨ ҹϞҷⷕⱘ getGameHallInfo ᮍ⊩Ϣ getUsers ᮍ⊩ˈҢ᳡ࡵ఼ડᑨЁᕫࠄḠᄤⱘֵᙃϢ᠔᳝⫼᠋ } mainFrame.sendUserIn(); GameHallFrame mainFrame = new GameHallFrame(hallInfo, cu, users); //߯ᓎ⬠䴶 GameHallFrame ChessUser cu = ChessClientContext.chessUser; //ᕫࠄ䖯ܹ␌៣ⱘ⥽ᆊֵᙃ List users = getUsers(response); //ᕫࠄܼ䚼⥽ᆊⱘֵᙃ GameHallInfo hallInfo = getGameHallInfo(response); //Ң᳡ࡵ఼Ёᕫࠄ໻ख़ֵᙃᑊᇕ㺙៤ϔϾ GameHallInfo ᇍ䈵 public void execute(Response response) { code\fivechess-client\src\org\crazyit\gamehall\fivechess\client\action\ClientInAction.java ҷⷕ⏙ऩ˖ ៣໻ख़ⱘ⬠䴶DŽ 䖭Ͼᅶ᠋ッ໘⧚㉏Џ㽕⫼Ѣᕫࠄ᳡ࡵ఼Ӵ䖛ᴹⱘ໻ख़ֵᙃǃ⥽ᆊֵᙃˈ៥Ӏৃҹḍ᥂䖭ѯֵᙃᴹ߯ᓎ␌ ᓎゟ␌៣໻ख़ⱘ৘Ͼ⬠䴶ˈ䛑⾏ϡᓔ⥽ᆊⱘֵᙃDŽ೼ 15.3.3 Ёˈ៥Ӏ䖬≵᳝ᅲ⦄ ClientInActionˈ 15.3.6 Փ⫼᳡ࡵ఼ⱘ᭄᥂߯ᓎ␌៣໻ख़ 䴶ゴ㡖䆆䗄DŽ ᆊ߫㸼Ϣ㘞໽⬠䴶Ꮖ㒣߯ᓎᅠ៤ˈ䖭ϸϾ⬠䴶㒘ӊৃҹ೼␌៣⬠䴶䞡⫼ˈ␌៣⬠䴶ⱘᅲ⦄ᇚ೼ϟ⥽ /Download at http://www.pin5i.com ChatPanel ⏏ࡴ⍜ᙃᦤ⼎DŽ݋ԧⱘᬜᵰབ೒ 15.4 ᠔⼎DŽ Ӯ৥᠔᳝⥽ᆊথ䗕⍜ᙃˈ᳝ᮄⱘ⥽ᆊ䖯ܹˈ䳔㽕᳈ᮄ⥽ᆊ߫㸼ˈ᳈ᮄ㘞໽⬠䴶㒘ӊⱘϟᢝḚˈ᳔ৢ೼ ReceiveInAction ᰃ⥽ᆊ⫼⥽᥹ᬊ݊Ҫ⥽ᆊ䖯ܹ␌៣໻ख़ⱘ⍜ᙃˈϔᮺ᳝ᮄⱘ⥽ᆊ䖯ܹˈ᳡ࡵ఼ህ ৡ⿄DŽ 䈵ᴹֱᄬ⬠䴶㒘ӊˈᔧ᳝ᮄⱘ⬠䴶㒘ӊ㹿߯ᓎᯊˈህ䳔㽕ࡴܹࠄ Map ЁˈᑊЎ䆹㒘ӊᦤկϔϾଃϔⱘ ҹϞҷⷕ䳔㽕⊼ᛣⱘᰃˈᮄᓎϔϾ UIContext ᴹֱᄬ৘Ͼ⬠䴶㒘ӊˈUIContext ЁᦤկϔϾ Map ᇍ } chatPanel.refreshJComboBox(); chatPanel.appendContent(newUser.getName() + " 䖯ᴹњ"); ChatPanel chatPanel = (ChatPanel)UIContext.modules.get(UIContext.HALL_CHAT_PANEL); //৥㘞໽ݙᆍЁ⏏ࡴ userTable.addUser(newUser); UserTable userTable = (UserTable)UIContext.modules.get(UIContext.HALL_USER_TABLE); //৥⥽ᆊ߫㸼ЁࡴܹϔϾᮄ⥽ᆊ ChessUser newUser = (ChessUser)response.getData("newUser"); //ᕫࠄᮄ䖯ܹⱘ⥽ᆊ public void execute(Response response) { code\fivechess-client\src\org\crazyit\gamehall\fivechess\client\action\ReceiveInAction.java ҷⷕ⏙ऩ˖ ッᠻ㸠ⱘ ActionDŽ ϔϾ ReceiveInActionˈ⫼Ѣᅶ᠋ッ᥹ᬊ᳡ࡵ఼থ䗕Ā᳝ᮄ⥽ᆊ䖯ܹāⱘֵᙃDŽReceiveInAction ᰃᅶ᠋ NewUserInAction ᬒ೼ fivechess-server ῵ഫЁˈ㸼⼎䆹 Action ⬅᳡ࡵ఼ᠻ㸠DŽϟ䴶Ўᅶ᠋ッ߯ᓎ } } hasLogin.getPrintStream().println(XStreamUtil.toXML(response)); if (id.equals(user.getId())) continue; /Download at http://www.pin5i.com //ᕫࠄথ䗕ⱘݙᆍ public void send() { //থ䗕ֵᙃ ҷⷕ⏙ऩ˖code\fivechess-client\src\org\crazyit\gamehall\fivechess\client\ui\ChatPanel.java ҹϟᰃ ChatPanel থ䗕㘞໽ݙᆍⱘᮍ⊩DŽ ៥䕀থDŽ᳡ࡵ఼ᕫࠄ䖭Ͼ䇋∖ৢˈህᠻ㸠ᶤϾ᳡ࡵ఼໘⧚㉏ˈ䆹໘⧚㉏ህᇚ㘞໽ݙᆍ䕀থ㒭Ⳍᑨⱘ⥽ᆊDŽ ҹ߯ᓎϔ⃵䇋∖ˈᇚ䆹䇋∖থ䗕ࠄ᳡ࡵ఼ˈਞ䆝᳡ࡵ఼˖៥ᇍᶤϾҎ˄᠔᳝Ҏ˅থ䗕њ㘞໽ݙᆍˈ䇋ᐂ ೼㘞໽⬠䴶˄ChatPanel˅Ёˈᔧ⥽ᆊ䗝ᢽњᶤϾ㘞໽ⱘᇍ䈵ˈ䕧ܹݙᆍݡ⚍ߏথ䗕ᣝ䪂ৢˈህৃ 15.4.1 থ䗕㘞໽ֵᙃ ᴀᇣ㡖ᅲ⦄␌៣໻ख़Ёⱘ㘞໽ࡳ㛑DŽ 䳔㽕⊼ᛣⱘᰃˈ㘞໽⬠䴶ᰃৃҹ݅⫼ⱘˈ䰸њ೼␌៣໻ख़ЁՓ⫼㘞໽⬠䴶໪ˈ䖬ৃҹ೼␌៣⬠䴶ЁՓ⫼DŽ ೼␌៣໻ख़Ёˈ៥ӀᦤկњϔϾ㘞໽ⱘ⬠䴶ˈ⥽ᆊৃҹ೼Ϟ䴶䖯㸠㘞໽ˈথ䗕៪㗙᥹ᬊ㘞໽ֵᙃˈ 15.4 ᅲ⦄㘞໽ࡳ㛑 ϟ䴶ゴ㡖ˈ៥Ӏᇚᅲ⦄␌៣໻ख़ⱘ݊Ҫࡳ㛑DŽ ೼ᴀᇣ㡖Ёˈ៥Ӏ㓪ݭњ␌៣໻ख़ⱘ৘Ͼ⬠䴶㒘ӊϢᇍ䈵ˈᑊᅲ⦄њ⫼᠋䖯ܹ␌៣໻ख़ⱘࡳ㛑ˈ೼ ೒ 15.4 ᮄⱘ⥽ᆊ䖯ܹ␌៣໻ख़  /Download at http://www.pin5i.com ᥹ᬊ㘞໽ֵᙃ⬅ ReceiveMessageAction 䋳䋷໘⧚ˈ䆹㉏ሲѢᅶ᠋ッ໘⧚㉏ˈ೼ fivechess-client 15.4.2 ᥹ᬊ㘞໽ֵᙃ 䆱ˈ䙷Мህᛣੇⴔ৥᠔᳝Ҏথ䗕㘞໽ݙᆍDŽ SendMessageAction Ёḍ᥂䇋∖ⱘখ᭄ᴹথ䗕Ⳍᑨⱘֵᙃˈ䳔㽕⊼ᛣⱘᰃˈབᵰ᥹ᬊҎ id Ўぎⱘ } } receiver.getPrintStream().println(XStreamUtil.toXML(response)); response.setData("content", sender.getName() + " ᇍԴ䇈˖" + content); if (receiver.getId().equals(sender.getId())) return; ChessUser receiver = ChessContext.users.get(receiverId); //ᕫࠄ᥹ᬊҎ } else { } cu.getPrintStream().println(XStreamUtil.toXML(response)); response.setData("content", sender.getName() + " ᇍ᠔᳝Ҏ䇈˖" + content); ChessUser cu = ChessContext.users.get(id); if (id.equals(senderId)) continue; for (String id : ChessContext.users.keySet()) { //৥᠔᳝Ҏথ if (receiverId == null) { String content = (String)request.getParameter("content"); ChessUser sender = ChessContext.users.get(senderId); String senderId = (String)request.getParameter("senderId"); String receiverId = (String)request.getParameter("receiverId"); public void execute(Request request, Response response, Socket socket) { code\GameHall-Server\src\org\crazyit\gamehall\server\action\SendMessageAction.java ҷⷕ⏙ऩ˖ 䋳䋷໘⧚DŽ ᙃˈ⬅᳡ࡵ఼ⱘ SendMessageAction ᥹ᬊˈݡ⬅䆹 Action 䕀থˈ䅽ᅶ᠋ッⱘ ReceiveMessageAction ֵᙃৢˈህৃҹᇚݙᆍথ䗕㒭Ⳍ݇ⱘ⥽ᆊˈҹϟᰃ SendMessageAction ⱘ݋ԧᅲ⦄DŽ⥽ᆊথ䗕㘞໽ֵ Request Ёࣙᣀњ᥹ᬊҎ idǃথ䗕Ҏ id ੠㘞໽ݙᆍⱘֵᙃDŽ᳡ࡵ఼Ёⱘ SendMessageAction ᕫࠄ䖭ѯ Ёথ䗕㘞໽䇋∖ⱘ᳡ࡵ఼໘⧚㉏ᰃ SendMessageActionˈᅶ᠋ッ໘⧚㉏ᰃ ReceiveMessageActionDŽ ⬅Ѣ䖭Ͼ ChatPanel ᰃৃҹ䞡⫼ⱘˈ಴ℸ serverAction Ϣ clientAction гᰃ⬅Փ⫼㗙ᴹᦤկDŽ␌៣໻ख़ ҹϞⱘҷⷕЁˈᵘ䗴 Request ᇍ䈵ᯊˈ៥ӀՓ⫼ⱘᰃ serverAction Ϣ clientAction 䖭ϸϾ㉏ሲᗻˈ } appendContent("Դᇍ " + receiver.getName() + " 䇈: " + content); this.user.getPrintStream().println(XStreamUtil.toXML(request)); //থ䗕䇋∖ request.setParameter("content", content); request.setParameter("senderId", this.user.getId()); request.setParameter("receiverId", receiver.getId()); //䆒㕂খ᭄ Request request = new Request(this.serverAction, this.clientAction); //ᵘ䗴䇋∖ ChessUser receiver = (ChessUser)this.target.getSelectedItem(); //ᕫࠄ᥹ᬊ⥽ᆊ String content = this.conentField.getText(); /Download at http://www.pin5i.com ᆊˈህৃҹതࠄԡ㕂Ϟᑊሩ⦄␌៣⬠䴶DŽ ⱘ⢊ᗕ䖯㸠߸ᮁˈ߸ᮁ݊ᰃ৺Ꮖ㒣തϟњˈབᵰ⥽ᆊ≵᳝തࠄӏԩԡ㕂ϞᑊϨᔧࠡ᠔䗝ᢽⱘԡ㕂≵᳝⥽ ⥽ᆊ䗝ᢽњᶤϔϾԡ㕂തϟⱘᯊ׭ˈ䳔㽕ᇍԡ㕂䖯㸠߸ᮁˈⳟϟ䆹ԡ㕂ᰃ৺᳝Ҏˈ䖬㽕ᇍ⥽ᆊᔧࠡ 15.5 ਃࡼ␌៣ ೒ 15.5 㘞໽ᬜᵰ  㘞໽ⱘ݋ԧᬜᵰབ೒ 15.5 ᠔⼎DŽ } chatPanel.appendContent(content); String content = (String)response.getData("content"); //Ң᳡ࡵ఼ડᑨЁᕫࠄݙᆍ ChatPanel chatPanel = (ChatPanel)UIContext.modules.get(UIContext.HALL_CHAT_PANEL); //ᕫࠄ㘞໽ⱘ⬠䴶㒘ӊ public void execute(Response response) { code\fivechess-client\src\org\crazyit\gamehall\fivechess\client\action\ReceiveMessageAction.java ҷⷕ⏙ऩ˖ ᴀඳЁDŽ ῵ഫЁˈᶤϾ⥽ᆊᕫࠄ㘞໽ݙᆍৢˈህৃҹ㦋ᕫ⬠䴶㒘ӊˈݡᇚ䖭ѯ㘞໽ݙᆍ䗑ࡴࠄ㘞໽⬠䴶㒘ӊⱘ᭛ /Download at http://www.pin5i.com //⬏Ꮊ䖍ⱘ⥽ᆊ g.drawImage(this.rightUserHead, 645, 300, this); g.drawImage(this.leftUserHead, 30, 300, this); ڣ //⬏༈ this.rightUserHead = getUserHead(this.rightUserHead, ru); ڣ //䆒㕂ে䖍⥽ᆊⱘ༈ this.leftUserHead = getUserHead(this.leftUserHead, lu); ڣ //䆒㕂Ꮊ䖍⥽ᆊⱘ༈ ChessUser ru = this.table.getRightSeat().getUser(); ChessUser lu = this.table.getLeftSeat().getUser(); g.drawImage(chessboard, 85, 80, this); g.drawImage(background, 0, 0, this.getWidth(), this.getHeight(), this); code\fivechess-client\src\org\crazyit\gamehall\fivechess\client\ui\game\GamePanel.java ҷⷕ⏙ऩ˖ ⱘ paint ᮍ⊩ⱘЏԧҷⷕDŽ ǃᎹ݋ᷣ೒⠛㒬⬏ߎᴹेৃˈҹϟᰃ GamePanelڣ␌៣ऎ˄GamePanel˅া䳔㽕ᇚẟⲬ೒⠛ǃ⥽ᆊ༈ ೒ 15.6 ␌៣⬠䴶  ໽⬠䴶ᇍ䈵˄ChatPanel˅ˈ䖭䞠Џ㽕ᅲ⦄ GamePanelDŽ␌៣⬠䴶ⱘᬜᵰབ೒ 15.6 ᠔⼎DŽ 㒘ӊˈϔϾᰃ␌៣ऎඳⱘ GamePanelˈ঺໪ϸϾህᰃ៥ӀᏆ㒣ᅲ⦄ⱘ⫼᠋߫㸼˄UserTable ᇍ䈵˅੠㘞 ␌៣⬠䴶ⱘにষ೼ᴀゴЁՓ⫼ ChessFrame ㉏ˈ䆹㉏㒻ᡓѢ JFrameˈChessFrame ࣙᣀϝϾ⬠䴶 ᓎゟ␌៣⬠䴶 15.5.1 /Download at http://www.pin5i.com Seat ⱘ user ሲᗻDŽ᳔ৢ䇗⫼ sendServerSitDown ᮍ⊩ਞ䆝᳡ࡵ఼DŽ њϔ㋏߫߸ᮁৢˈ⥽ᆊህৃҹതࠄԡ㕂Ϟˈᕫࠄ䆹ԡ㕂ⱘᇍ䈵˄Seat˅ৢˈݡ䆒㕂䆹ԡ㕂ⱘ⥽ᆊˈخ { { { { ;(() sendServerSitDown(table, seat.getSide //≵Ҏ৥᳡ࡵ఼থ䗕䇋∖ seat.setUser(this.user); } else { return; //ᑻԡϞ᳝Ҏˈৃҹᦤ⼎⥽ᆊ if (seat.getUser() != null) { } return; //ৃҹᦤ⼎⥽ᆊ if (this.user.hasSitDown(this.tables)) { //߸ᮁ⥽ᆊᰃ৺Ꮖ㒣തϟ if (seat != null) { Seat seat = getSeat(table, x, y); //ᕫࠄᑻԡ if (table != null) { Table table = getTable(x, y); //㦋ᕫḠᄤ int y = e.getY(); int x = e.getX(); private void sitDown(MouseEvent e) { //തϟⱘḠᄤⱘᮍ⊩ ҷⷕ⏙ऩ˖code\fivechess-client\src\org\crazyit\gamehall\fivechess\client\ui\HallPanel.java ҹϟᰃ HallPanel ⱘ sitDown ᮍ⊩DŽ ԧⱘᶤϾԡ㕂Ϟˈݡ৥␌៣໻ख़Ё᠔᳝ⱘ⥽ᆊথ䗕ֵᙃ˖䆹⥽ᆊᏆ㒣ത೼ᶤᓴḠᄤЁњDŽ 㒣ത೼݊Ҫԡ㕂Ϟˈᔧᴵӊ䛑ϡ៤ゟᯊˈህৃҹ৥᳡ࡵ఼থ䗕ֵᙃˈਞ䆝᳡ࡵ఼ᔧࠡⱘ⥽ᆊᏆ㒣തࠄ݋ ⥽ᆊ䗝ᢽњ݋ԧⱘϔϾԡ㕂⚍ߏᯊˈ䳔㽕䖯㸠ϔ㋏߫ⱘ߸ᮁˈ߸ᮁԡ㕂ᰃ৺᳝Ҏˈ߸ᮁ⥽ᆊᰃ৺Ꮖ 15.5.2 ⥽ᆊതϟ } this.user = user; this.table = table; public GamePanel(Table table, ChessUser user) { GamePanel ⱘᵘ䗴఼ⱘ䚼ߚҷⷕ˖ GamePanel ЁDŽ߯ᓎϔϾ GamePanel ᇍ䈵ˈ䳔㽕ᕫࠄ݋ԧⱘϔϾḠᄤᇍ䈵੠ᔧࠡⱘ⥽ᆊᇍ䈵ˈҹϟᰃ ㋴ⱘതᷛˈḍ᥂䖭ѯതᷛؐᇚⳌᑨⱘ೒⠛⬏ࠄܗᎹ݋ᷣ೒⠛DŽ㒬⬏䖭ѯ೒⠛Ϣ᭛ӊা䳔㽕⊼ᛣ৘Ͼ⬠䴶 ೒⠛Ϣ⥽ᆊৡ⿄ǃڣ೒⠛Ϣ⥽ᆊৡ⿄ǃে䖍⥽ᆊⱘ༈ڣ೼␌៣ऎ䳔㽕⬏ⱘ᳝ẟⲬ೒⠛ǃᎺ䖍⥽ᆊⱘ༈ g.drawImage(this.currentToolImage, 160, 630, this); //⬏Ꮉ݋ᷣ drawRightUser(g, ru); //⬏ে䖍ⱘ⥽ᆊ drawLeftUser(g, lu); /Download at http://www.pin5i.com ҷⷕ⏙ऩ˖ ㉏ᰃ EnterGameActionDŽ ᆊതϟⱘֵᙃৢˈᅶ᠋ッ໘⧚㉏ᰃ ReceiveUserSitDownActionˈᔧࠡ⥽ᆊਃࡼ␌៣⬠䴶ⱘᅶ᠋ッ໘⧚ ᔧࠡⱘ⥽ᆊതࠄᶤϾԡ㕂Ϟњˈ㄀Ѡ⃵ᰃਞ䆝ᔧࠡ⥽ᆊˈ䳔㽕ਃࡼ␌៣⬠䴶㉏DŽ᠔᳝⥽ᆊ᥹ᬊࠄᔧࠡ⥽ UserSitDownAction Ёⱘ execute ᮍ⊩৥ᅶ᠋ッথ䗕њϸ⃵᳡ࡵ఼ડᑨˈϔ⃵ᰃਞ䆝᠔᳝ⱘ⥽ᆊˈ user.getPrintStream().println(XStreamUtil.toXML(response)); //ਞ䆝ᅶ᠋ッ, 䳔㽕ਃࡼ␌៣⬠䴶 response.setActionClass(beginClass); String beginClass = (String)request.getParameter("beginClass"); //ᕫࠄਃࡼ␌៣ⱘᅶ᠋ッ㉏ printResponse(user, response); //৥᠔᳝⥽ᆊথ䗕ֵᙃ, ᳝ᮄ⥽ᆊതϟ response.setData("user", user.getId()); response.setData("side", side); response.setData("tableNumber", table.getTableNumber()); //ਞ䆝᠔᳝ⱘᅶ᠋ッ, ߮തϟⱘ⥽ᆊ೼ાᓴḠᄤાϾԡ㕂തϟњ seat.setUser(user); Seat seat = table.getSeat(side); //ᕫࠄᑻԡ ChessUser user = ChessContext.users.get(userId); String userId = (String)request.getParameter("userId"); //ᕫࠄ߮തϟⱘ⥽ᆊ Table table = Table.getTable(tableNumber, ChessContext.tables); String side = (String)request.getParameter("side"); Integer tableNumber = (Integer)request.getParameter("tableNumber"); //ᕫࠄḠᄤ㓪ো ҷⷕ⏙ऩ˖code\GameHall-Server\src\org\crazyit\gamehall\server\action\UserSitDownAction.java UserSitDownAction ⱘ execute ᮍ⊩DŽ beginClass ᰃᅶ᠋ッ໘⧚㉏ EnterGameActionDŽ 䴶ˈ಴ℸ䳔㽕Ў Request ݡ䆒㕂໮ϔϾখ᭄ؐ beginClassˈ㸼⼎ᔧࠡ⥽ᆊਃࡼ␌៣⬠䴶ⱘ㉏ˈ䖭Ͼ ⊼ᛣⱘᰃˈ⬅Ѣ䖭⃵䇋∖䳔㽕ਞ䆝݊Ҫ⥽ᆊˈᔧࠡⱘ⥽ᆊതϟњˈ䖬䳔㽕ਞ䆝ᔧࠡⱘ⥽ᆊএਃࡼ␌៣⬠ 䳔㽕ֱᄬḠᄤⱘḠᄤোǃ⥽ᆊ id ੠ᑻԡԡ㕂ⱘֵᙃˈ᳡ࡵ఼ッ໘⧚㉏ᰃ UserSitDownActionDŽ䖭䞠䳔㽕 sendServerSitDown ᮍ⊩ᵘ䗴ϔϾ Request ᇍ䈵ˈ৥᳡ࡵ఼থ䗕⥽ᆊതࠄԡ㕂ϞⱘֵᙃˈRequest } this.repaint(); this.user.getPrintStream().println(XStreamUtil.toXML(request)); "org.crazyit.gamehall.fivechess.client.action.game.EnterGameAction"); request.setParameter("beginClass", //䆒㕂ਃࡼ␌៣ⱘ㉏ request.setParameter("userId", this.user.getId()); request.setParameter("side", side); request.setParameter("tableNumber", table.getTableNumber()); "org.crazyit.gamehall.fivechess.client.action.ReceiveUserSitDownAction"); Request request = new Request("org.crazyit.gamehall.fivechess.server.action.UserSitDownAction", //߯ᓎ Request ᇍ䈵ᑊ䆒㕂Ⳍ݇ⱘখ᭄ private void sendServerSitDown(Table table, String side) { //৥᳡ࡵ఼থ䗕䇋∖, ਞ䆝᳡ࡵ఼㞾ᏅᏆ㒣തϟ ҷⷕ⏙ऩ˖code\fivechess-client\src\org\crazyit\gamehall\fivechess\client\ui\HallPanel.java sendServerSitDown ᮍ⊩DŽ /Download at http://www.pin5i.com "org.crazyit.gamehall.fivechess.server.action.OpponentEnterAction", Request request = new Request( //᳝ᇍ᠟, ৥᳡ࡵ఼থ䗕䇋∖ if (otherSeat.getUser() != null) { Seat otherSeat = table.getAnotherSeat(seat); //ᕫࠄᇍᮍᑻԡ Seat seat = table.getSeat(side); //ਞ䆝ᇍᮍ䖯ܹ␌៣(བᵰ᳝ᇍᮍ⥽ᆊⱘ䆱) cf.setVisible(true); ChessFrame cf = new ChessFrame(table, ChessClientContext.chessUser); //ᰒ⼎⬠䴶 Table table = Table.getTable(tableNumber, hallPanel.getTables()); //ḍ᥂Ḡᄤ㓪োᕫࠄḠᄤֵᙃ String side = (String)response.getData("side"); Integer tableNumber = (Integer)response.getData("tableNumber"); //Ң᳡ࡵ఼੐ᑨЁᕫࠄḠᄤ㓪ো HallPanel hallPanel = (HallPanel)UIContext.modules.get(UIContext.HALL_PANEL); a code\fivechess-client\src\org\crazyit\gamehall\fivechess\client\action\game\EnterGameAction.jav ҷⷕ⏙ऩ˖ থ䗕ࠄ᳡ࡵ఼Ёˈ䅽᳡ࡵ఼এਞ䆝ᔧࠡ⥽ᆊⱘᇍ᠟DŽ ⱘḠᄤᰃ৺᳝Ҏ˄ᇍ᠟˅ˈབᵰ᳝ᇍ᠟ⱘ䆱ˈህ㽕ਞ䆝ᇍᮍ㞾Ꮕ䖯ᴹњˈ䖭ᯊг䳔㽕ᵘ䗴ϔϾᮄⱘ䇋∖ ˄EnterGameAction˅DŽ೼䖭䞠䳔㽕⊼ᛣⱘᰃˈབᵰᔧࠡ⥽ᆊ䖯ܹњ␌៣Ёˈ䳔㽕߸ᮁᔧࠡ⥽ᆊ᠔തϟ ఼ડᑨᯊˈህ䳔㽕ᬍব Response ⱘ actionClass ሲᗻˈ䅽থ䗕തϟ䇋∖ⱘ⥽ᆊਃࡼ␌៣⬠䴶 ReceiveSitDownAction Ё৥݊Ҫ⥽ᆊথ䗕ⱘ㄀ϔ⃵᳡ࡵ఼ડᑨˈ䆹 Action ৥ᔧࠡ⥽ᆊথ䗕㄀Ѡ⃵᳡ࡵ া㽕ⶹ䘧⥽ᆊത೼ાᓴḠᄤⱘાϔ䖍ˈህৃҹ᳈ᮄᔧࠡⱘ Table Ѡ㓈᭄㒘ˈݡ䖯㸠 repaintDŽ䖭ᰃ } this.repaint(); seat.setUser(newUser); ChessUser newUser = getUser(userId); //ᕫࠄതϟⱘ⥽ᆊ Seat seat = table.getSeat(side); //ᕫࠄᑻԡ Table table = getTable(tableNumber); //ᕫࠄḠᄤᇍ䈵 public void newUserSitDown(int tableNumber, String side, String userId) { //ᮄ⥽ᆊതϟ, tableNumber ЎḠᄤ㓪ো, side ЎᎺেԡ㕂 ҷⷕ⏙ऩ˖code\fivechess-client\src\org\crazyit\gamehall\fivechess\client\ui\HallPanel.java newUserSitDown ⱘ݋ԧᅲ⦄DŽ 䴶㒘ӊ㉏ HallPanel ⱘ newUserSitDown ᮍ⊩໘⧚⥽ᆊതϟⱘџӊDŽҹϟᰃ HallPanel ⱘ ReceiveUserSitDownAction ᰃᅶ᠋ッ໘⧚㉏ˈ᥹ᬊ݊Ҫ⥽ᆊതϟњⱘֵᙃˈ᳔ৢ䇗⫼␌៣໻ᷥ⬠ hallPanel.newUserSitDown(tableNumber, side, userId); String userId = (String)response.getData("userId"); String side = (String)response.getData("side"); int tableNumber = (Integer)response.getData("tableNumber"); //᳝ᮄⱘ⥽ᆊതϟ, 䖭䞠⬅᠔᳝⥽ᆊ(ϡࣙᣀথ䗕Ҏ)ᠻ㸠 HallPanel hallPanel = (HallPanel)UIContext.modules.get(UIContext.HALL_PANEL); //ᕫࠄ⬠䴶ᇍ䈵 .java code\fivechess-client\src\org\crazyit\gamehall\fivechess\client\action\ReceiveUserSitDownAction /Download at http://www.pin5i.com ⼎DŽ newUserIn ᮍ⊩৥⬠䴶Ёⱘ⥽ᆊ䲚ড়⏏ࡴϔϾᮄⱘ⥽ᆊᑊ᳈ᮄ⬠䴶㒘ӊDŽ݋ԧⱘᬜᵰབ೒ 15.7 ᠔ } refreshUI(); this.users.add(user); public void newUserIn(ChessUser user) { //৥⥽ᆊ䲚ড়Ё⏏ࡴϔϾᮄ⥽ᆊ, 㸼⼎᳝ᮄ⥽ᆊ䖯ܹ code\fivechess-client\src\org\crazyit\gamehall\fivechess\client\ui\game\ChessFrame.java ҷⷕ⏙ऩ˖ ChessFrame ⱘ newUserIn ᮍ⊩DŽ 䖯ܹḠᄤᯊˈ᳡ࡵ఼ህӮਞ䆝៥ˈ៥ⱘᇍ᠟䖯ᴹњˈ᳔ৢ䇗⫼ ChessFrame ⱘ newUserIn ᮍ⊩DŽ ᅶ᠋ⱘ OpponentEnterAction Џ㽕⫼Ѣ໘⧚᥹ᬊᇍ᠟䖯ܹ៥᠔തⱘḠᄤⱘ᳡ࡵ఼ડᑨˈᔧ៥ⱘᇍ᠟ cf.newUserIn(opponent); ChessFrame cf = (ChessFrame)UIContext.modules.get(UIContext.GAME_FRAME); ChessUser opponent = gameHall.getUser(opponentId); //Ң໻ख़Ёᕫࠄᇍ᠟ⱘֵᙃ String opponentId = (String)response.getData("opponentId"); //ᕫࠄᇍ᠟ⱘ ChessUser ᇍ䈵 HallPanel gameHall = (HallPanel)UIContext.modules.get(UIContext.HALL_PANEL); //ᕫࠄ໻ख़ᇍ䈵 .java code\fivechess-client\src\org\crazyit\gamehall\fivechess\client\action\game\OpponentEnterAction ҷⷕ⏙ऩ˖ ᅶ᠋ッⱘ OpponentEnterActionDŽ ᳡ࡵ఼ડᑨDŽ ៣DŽ಴Ў᳡ࡵ఼ડᑨЁህ䳔㽕ᇚᔧࠡ⥽ᆊⱘֵᙃ䆒㕂ࠄડᑨ᭄᥂Ёˈݡᡒࠄᔧࠡ⥽ᆊⱘᇍ᠟ˈ৥݊䕧ܹ ᳡ࡵ఼ッⱘ Action Џ㽕ḍ᥂খ᭄ᡒࠄথ䗕䖭⃵䇋∖ⱘ⥽ᆊⱘᇍ᠟ˈਞ䆝Ҫᔧࠡ⥽ᆊᏆ㒣䖯ܹњ␌ firstUser.getPrintStream().println(XStreamUtil.toXML(response)); //ਞ䆝㄀ϔϾ䖯ܹ␌៣ⱘ⥽ᆊ, ᳝ᇍ᠟䖯ܹ response.setData("opponentId", secondUserId); String secondUserId = (String)request.getParameter("secondUserId"); //ᕫࠄ㄀ѠϾ⥽ᆊⱘᇍ䈵 ChessUser firstUser = ChessContext.users.get(firstUserId); String firstUserId = (String)request.getParameter("firstUserId"); //ᕫࠄ㄀ϔϾ⥽ᆊⱘᇍ䈵(থ䗕䇋∖ⱘҎⱘᇍ᠟) code\GameHall-Server\src\org\crazyit\gamehall\server\action\OpponentEnterAction.java ҷⷕ⏙ऩ˖ ᳡ࡵ఼ッⱘ OpponentEnterActionDŽ ⱘᅶ᠋ッ໘⧚㉏ᰃ fivechess-client ⱘ OpponentEnterActionDŽ ਞ䆝ᇍ᠟㞾Ꮕ䖯ܹњ␌៣ˈ᳡ࡵ఼໘⧚㉏ᰃ fivechess-server ⱘ OpponentEnterActionˈᇍ᠟ᠻ㸠 } ChessClientContext.chessUser.getPrintStream().println(XStreamUtil.toXML(request)); request.setParameter("secondUserId", seat.getUser().getId()); //secondUserId ᰃ㞾Ꮕⱘ ID(ৢ䖯ܹ␌៣ⱘ⥽ᆊ) request.setParameter("firstUserId", otherSeat.getUser().getId()); //firstUserId ᰃᇍ᠟ⱘ ID(㄀ϔϾ䖯ܹ␌៣ⱘ⥽ᆊ) org.crazyit.gamehall.fivechess.client.action.game.OpponentEnterAction");"  ೒ 15.7 ঠᮍ⥽ᆊതࠄḠᄤϞ 15.5.3 ᅲ⦄␌៣㘞໽ ៥ӀᏆ㒣೼ 15.4 Ёᅲ⦄њ␌៣໻ख़ⱘ㘞໽ࡳ㛑ˈ೼␌៣⬠䴶䖯㸠㘞໽ᯊˈՓ⫼ⱘᰃৠϔϾ⬠䴶㒘 ӊ˄ChatPanel˅ˈ಴ℸ೼␌៣Ё㘞໽ˈা䳔㽕䞡ᮄ߯ᓎϔϾ ChatPanel ᇍ䈵ेৃˈԚᰃ೼߯ᓎ䆹ᇍ䈵 ᯊˈ䳔㽕ᦤկϡৠⱘ㘞໽᳡ࡵ఼໘⧚㉏੠ᅶ᠋ッ໘⧚㉏DŽ೼␌៣Ё㘞໽ⱘ᳡ࡵ఼໘⧚㉏ᰃ GameMessageActionˈᅶ᠋ッ໘⧚㉏ᰃ ReceiveMessageActionDŽ ҷⷕ⏙ऩ˖ code\GameHall-Server\src\org\crazyit\gamehall\server\action\GameMessageAction.java String senderId = (String)request.getParameter("senderId"); ChessUser sender = ChessContext.users.get(senderId); String content = (String)request.getParameter("content"); //ᕫࠄথ䗕Ҏ᠔೼ⱘḠᄤ Table table = ChessContext.getTable(senderId); if (table != null ) { //৥ᇍ᠟থ䗕 ChessUser receiver = table.getAnotherUser(sender); if (receiver != null) { response.setData("content", sender.getName() + " ᇍԴ䇈˖" + content); receiver.getPrintStream().println(XStreamUtil.toXML(response)); } } Download at http://www.pin5i.com/ /Download at http://www.pin5i.com //䆒㕂⥽ᆊⱘ⢊ᗕ if (this.user.isReady()) return; private void ready() { ໛␌៣ޚ //⥽ᆊ code\fivechess-client\src\org\crazyit\gamehall\fivechess\client\ui\game\GamePanel.java ҷⷕ⏙ऩ˖ ໛ⱘ䇋∖ˈҹϟᰃ GamePanel Ёⱘ ready ᮍ⊩DŽޚᔧ⥽ᆊ⚍ߏњᓔྟᯊˈህ䳔㽕৥᳡ࡵ఼থ䗕 ೒ ␌៣ЁⱘᎹ݋ᷣ೒⠛  ೒ 15.10 ᰃ␌៣ЁⱘᎹ݋ᷣ೒⠛ ໛␌៣ⱘᎹ݋ᷣ೒⠛ޚ ೒ 15.9  ໛␌៣ⱘᎹ݋ᷣ೒⠛ޚ೒ 15.9 ᰃ ೒ 15.8 ᓔྟ␌៣ⱘᎹ݋ᷣ೒⠛  ೒ 15.8 ᰃᓔྟ␌៣ⱘᎹ݋ᷣDŽ 䳔㽕᳈ᤶ঺໪ⱘᎹ݋ᷣ೒⠛DŽ ໛ⱘ⢊ᗕˈ䙷Мህ䳔㽕᳈ᤶᎹ݋ᷣ೒⠛ˈབᵰᏆ㒣೼␌៣⢊ᗕˈৠḋഄгޚᗕDŽབᵰ⥽ᆊᏆ㒣໘⧚␌៣ ᓣᓔྟDŽЎњ㸼⼎␌៣Ꮖ㒣ᓔྟˈ៥Ӏ೼␌៣ऎ˄GamePanel˅ЁֱᄬϔϾᏗᇨؐˈ㸼⼎ᔧࠡ␌៣ⱘ⢊ ໛དњˈ䙷М␌៣ℷޚ໛དњˈབᵰᇍ᠟䛑Ꮖ㒣ޚ䰸њ䳔㽕ᬍবҪⱘ⢊ᗕ໪ˈ䖬䳔㽕ਞ䆝ᇍ᠟ˈҪᏆ㒣 ໛Ёⱘ⢊ᗕDŽ⥽ᆊ⚍ߏњᓔྟˈޚ໛⢊ᗕˈChessUser ЁᦤկњϔϾ ready ⱘᏗᇨؐᴹ㸼⼎䆹⥽ᆊᰃ৺ޚ ϔᮍ⥽ᆊതࠄḠᄤϞⱘᯊ׭ˈᎹ݋ᷣЁাৃҹ⚍ߏᓔྟˈᔧ⥽ᆊ⚍ߏњᓔྟⱘᯊ׭ˈ䆹⥽ᆊህ໘Ѣ ໛ޚ15.6.1 ␌៣ ϟẟˈে䖍ⱘ⥽ᆊՓ⫼ⱑẟDŽܜӀ⹂ᅮᎺ䖍ԡ㕂ⱘ⥽ᆊՓ⫼咥ẟˈᑊৃҹ ᔧঠᮍ⥽ᆊതࠄৠϔᓴḠᄤϞˈᑊϨ䛑⚍ߏњᎹ݋ᷣЁⱘᓔྟᯊˈ␌៣ህℷᓣᓔྟˈ೼ᴀゴЁˈ៥ 15.6 ᓔྟ␌៣ ᠡৃҹՓ⫼ UIContext ᴹ㦋ᕫⳌᑨⱘᅲ՟DŽ 䴶㒘ӊˈ಴ℸ㱑✊䞡⫼њ ChatPanelˈԚᰃᅗӀᰃϸϾᅲ՟ˈ಴ℸ䳔㽕Ў䖭ϸϾᅲ՟ᦤկϡৠⱘৡ⿄DŽ ⱘ᭛ᴀඳ䗑ࡴ᭛ᴀDŽԚᰃ䳔㽕⊼ᛣⱘᰃˈ⬅Ѣ៥ӀՓ⫼њϔϾ UIContext ЁⱘϔϾ Map ᇍ䈵ᴹֱᄬ⬠ ␌៣໻ख़Ёⱘ㘞໽㉏ԐDŽReceiveMessageAction Ϣ␌៣໻ख़Ё᠔԰ⱘ໘⧚ϔḋˈ䛑ᰃ৥ ChatPanel Ё GameMessageAction ᥹ᬊࠄ⥽ᆊথ䗕ⱘ㘞໽ֵᙃৢˈህ৥ᅗⱘᇍ᠟থ䗕㘞໽ֵᙃˈ᠔԰ⱘ໘⧚Ϣ /Download at http://www.pin5i.com } } opponent.getPrintStream().println(XStreamUtil.toXML(response)); response.setData("userId", userId); response.setActionClass(opponentAction); String opponentAction = (String)request.getParameter("opponentAction"); ໛ⱘᅶ᠋ッ໘⧚㉏ޚ໛དњ, Փ⫼ᇍ᠟᥹ᬊޚ //ਞ䆝ᇍ᠟㞾Ꮕ } user.getPrintStream().println(XStreamUtil.toXML(response)); opponent.getPrintStream().println(XStreamUtil.toXML(response)); //৥ঠᮍ⥽ᆊথ䗕ડᑨ, ␌៣ᓔྟ createChessArray(table); //߯ᓎẟⲬ᭄㒘 if (opponent.isReady()) { ໛དњޚ //ᇍ䴶ᑻԡ᳝Ҏ, ݡ߸ᮁᇍ᠟ᰃ৺Ꮖ㒣 if (opponent != null) { ChessUser opponent = table.getAnotherSeat(seat).getUser(); //ᕫࠄᇍ᠟ Seat seat = table.getUserSeat(user); Table table = Table.getTable(tableNumber, ChessContext.tables); //ᕫࠄḠᄤᇍ䈵 ໛␌៣ޚ //߸ᮁᇍᮍᰃ৺Ꮖ㒣 user.setReady(true); ChessUser user = ChessContext.users.get(userId); //ᕫࠄ⥽ᆊ Integer tableNumber = (Integer)request.getParameter("tableNumber"); //ᕫࠄḠᄤ㓪ো String userId = (String)request.getParameter("userId"); ໛␌៣ⱘ⥽ᆊޚ //ᕫࠄ public void execute(Request request, Response response, Socket socket) { ҷⷕ⏙ऩ˖code\GameHall-Server\src\org\crazyit\gamehall\server\action\ReadyAction.java ໛དњDŽޚ఼ਞ䆝ᇍ᠟ˈ㞾Ꮕ ໛␌៣ⱘ䆱ˈህՓ⫼䇋∖খ᭄Ёⱘ OpponentReadyAction ԰Ўᅶ᠋ッ໘⧚㉏ˈ䅽᳡ࡵޚབᵰᇍ᠟≵᳝ ໛ᓔྟ␌៣ⱘ䆱ˈህՓ⫼ᅶ᠋ッⱘ StartGameAction ໘⧚㉏ˈޚᇍ䴶ԡ㕂᳝⥽ᆊˈᑊϨ䆹⥽ᆊৠḋᏆ㒣 ໛␌៣ᯊˈབᵰޚ㽕⊼ᛣⱘᰃˈ៥Ӏ৥䇋∖খ᭄ЁࡴܹњϔϾ opponentAction ⱘᅶ᠋ッ໘⧚㉏ˈ㞾Ꮕ ␌៣ⱘ⥽ᆊ idˈḠᄤ㓪োˈ᳡ࡵ఼໘⧚㉏ᰃ ReadyActionˈᅶ᠋ッ໘⧚㉏ᰃ StartGameActionˈ䖭䞠䳔 ໛ޚ໛␌៣њˈ䇋∖খ᭄ࣙᣀޚҹϞⱘ ready ᮍ⊩৥᳡ࡵ఼থ䗕њϔ⃵䇋∖ˈਞ䆝᳡ࡵ఼㞾ᏅᏆ㒣 } this.user.getPrintStream().println(XStreamUtil.toXML(request)); "org.crazyit.gamehall.fivechess.client.action.game.OpponentReadyAction"); request.setParameter("opponentAction", ໛, ߭䆒㕂ᇍ᠟ⱘ໘⧚㉏ޚ //བᵰᇍ᠟≵᳝ request.setParameter("tableNumber", this.table.getTableNumber()); request.setParameter("userId", this.user.getId()); "org.crazyit.gamehall.fivechess.client.action.game.StartGameAction"); Request request = new Request("org.crazyit.gamehall.fivechess.server.action.ReadyAction", ໛དњޚ //থ䗕ֵᙃ㒭᳡ࡵ఼, ਞ䆝᳡ࡵ఼Ꮖ㒣 this.repaint(); this.currentToolImage = tool_ready; this.user.setReady(true); /Download at http://www.pin5i.com Ͼᄫ԰ߎࡼ⬏ⱘᬜᵰDŽᓔྟ␌៣ⱘ݋ԧᬜᵰབ೒ 15.11 ᠔⼎DŽ ⱘᏗᇨؐˈᷛ䆚ᰃ৺䕂ࠄᔧࠡ⥽ᆊϟẟˈ೼ startGame ᮍ⊩ⱘ᳔ৢˈਃࡼϔϾ Timerˈᇚᓔྟ␌៣ⱘ޴ ᓔྟ␌៣䳔㽕䆒㕂 gaming Ў trueˈ㸼⼎ᔧࠡℷ೼␌៣Ёˈ೼ GamePanel Ё䖬䳔㽕ᦤկϔϾ myTure } timer.schedule(this.startGameTask, 0, 20); this.timer = new Timer(); this.startGameTask = new StartGameTask(this); this.selectImage = getSelectImage(); } this.gameStartImage = ImageUtil.getImage("images/fivechess/start-game-opponent-first.gif"); ϟẟܜ } else {//ᇍ᠟ this.gameStartImage = ImageUtil.getImage("images/fivechess/start-game-you-first.gif"); this.myTurn = true; ϟẟܜ if (getUserSide().equals(Seat.LEFT)) {//㞾Ꮕ //䆒㕂ᓔྟ␌៣ⱘᦤկ this.currentToolImage = tool_drawAndLost; this.gaming = true; public void startGame() { //䆒㕂␌៣⢊ᗕ code\fivechess-client\src\org\crazyit\gamehall\fivechess\client\ui\game\GamePanel.java ҷⷕ⏙ऩ˖ ҹϟᰃ GamePanel ⱘ startGame ᮍ⊩DŽ 㒭ঠᮍˈ䅽ϸ䖍ⱘᅶ᠋ッএ߯ᓎẟⲬⱘѠ㓈᭄㒘ˈ᳔ৢ䇗⫼ GamePanel ⱘ startGame ᮍ⊩ᴹᓔྟ␌៣DŽ ໛њ␌៣ˈ䙷М᳡ࡵ఼ህ䳔㽕থ䗕ֵᙃޚᇍ᠟ⱘ⢊ᗕˈݡᇍ⬠䴶㒘ӊ䖯㸠ϔ⃵ repaintDŽབᵰঠᮍ䛑Ꮖ㒣 ໛␌៣ⱘ᳡ࡵ఼ડᑨৢˈাᰃ䆒㕂ޚᑊϡ⬅ᅶ᠋ッ䖯㸠䕧䌶ⱘ߸ᮁDŽOpponentReadyAction ᥹ᬊࠄᇍᮍ ⥽ᆊ೼ϟẟⱘᯊ׭ˈ᳡ࡵ఼ݡᇍ䖭ϾѠ㓈᭄㒘䖯㸠䘡ग़ˈ߸ᮁ䕧䌶ˈ಴ℸ߸ᮁ䕧䌶ⱘ᪡԰⬅᳡ࡵ఼䖯㸠ˈ 䖭Ͼ Map ⱘ key ᰃḠᄤ㓪োˈvalue ᰃ Chess ⱘѠ㓈᭄㒘˖MapDŽ䳔㽕⊼ᛣⱘᰃˈ ᭄㒘ᬒࠄ ChessContext ЁˈгህᰃᇚẟⲬⱘѠ㓈᭄㒘ᬒࠄ᳡ࡵ఼Ё䖯㸠ֱᄬˈֱᄬⱘ᭄᥂㒧ᵘᰃ Mapˈ ῵ഫЁDŽ᳡ࡵ఼߯ᓎњ᭄㒘ৢˈህৃҹਞ䆝ঠᮍ␌៣ᓔྟDŽ೼߯ᓎẟⲬ᭄㒘ᯊˈ៥Ӏ䳔㽕ᇚ߯ᓎⱘẟⲬ ⬅Ѣ Chess ᇍ䈵ᰃᅶ᠋ッϢ᳡ࡵ఼ッ݅ৠՓ⫼ⱘ㉏ˈ಴ℸৃҹᇚ䖭Ͼᇍ䈵ᬒࠄ fivechess-commons private Rectangle range; //䆹ẟᄤⱘऎඳ private String color; //ẟᄤ买㡆 private int j; //೼Ѡ㓈᭄㒘ЁⱘѠ㓈ؐ private int i; //೼Ѡ㓈᭄㒘Ёⱘϔ㓈ؐ private int beginY; //ẟᄤⱘᓔྟ Y തᷛ private int beginX; //ẟᄤⱘᓔྟ X തᷛ བϟ˖ ϔϾẟᄤՓ⫼ϔϾ Chess ᇍ䈵ᴹ㸼⼎ˈChess ᇍ䈵ᬒ೼ fivechess-commons ῵ഫˈChess ᇍ䈵ⱘሲᗻ ໛དњˈ䙷Мህ߯ᓎẟⲬⱘѠ㓈᭄㒘ˈẟⲬѠ㓈᭄㒘䞠䴶ᄬᬒⱘᰃ Chess ᇍ䈵ˈ೼ᴀゴЁˈޚ䛑Ꮖ㒣 ໛དњˈབᵰঠᮍޚ໛ⱘ⥽ᆊৢˈݡਞ䆝ᇍ᠟ˈᔧࠡⱘ⥽ᆊᏆ㒣ޚReadyAction ЁᕫࠄḠᄤϢ␌៣ /Download at http://www.pin5i.com if (chess != null) { Chess chess = getSelectChess(x, y); private void takeChess(int x, int y) { //ϟẟⱘᮍ⊩ code\fivechess-client\src\org\crazyit\gamehall\fivechess\client\ui\game\GamePanel.java ҷⷕ⏙ऩ˖ ҹϟᰃ⥽ᆊϟẟᯊ᠔ᠻ㸠ⱘᮍ⊩˖ ህৃҹᇚ↣ϔϾ Chess 䛑⬏ࠄ⬠䴶ЁDŽ ⱘ买㡆䆒㕂ࠄ Chess ᇍ䈵ЁˈChess ᇍ䈵Ё᳝ϔϾ color ⱘሲᗻˈ䙷М೼ GamePanel ⱘ paint ᮍ⊩Ёˈ ࣪ᯊাӮ䌟ѢᅗӀⳌᑨⱘതᷛϢ㣗ೈˈᔧ⥽ᆊ䖯㸠њϟẟⱘ᪡԰ৢˈህᇚ⥽ᆊᇍᑨⱘẟ˄咥ẟ៪㗙ⱑẟ˅ ߱ྟ࣪䖭ϾѠ㓈᭄㒘˄߯ᓎ᭄㒘Ёⱘ᠔᳝ Chess ᇍ䈵˅ˈ䆹᭄㒘Ё᠔᳝ⱘ Chess ᇍ䈵䛑≵᳝೒⠛ˈ߱ྟ Ѡ㓈᭄㒘Ёᕫࠄ݋ԧⱘᶤϾ Chess ᇍ䈵DŽ៥ӀẟⲬЁᰃϔϾ Chess ⱘѠ㓈᭄㒘ˈ೼␌៣ᓔྟᯊˈህӮ ϟẟⱘ⥽ᆊ೼ẟⲬⱘᶤϾऎඳ⚍ߏњϟẟৢˈ៥Ӏ⬠䴶㒘ӊህ䳔㽕ᕫࠄ哴ᷛ⚍ߏⱘതᷛˈݡҢẟⲬ 15.6.2 ⥽ᆊϟẟ ᯊࡴܹ䗝ᢽⱘ೒⠛ᑊ೼ paint ᮍ⊩Ёࡴܹ߸ᮁेৃᅲ⦄DŽ ᷛˈা䳔㽕೼哴ᷛ⿏ࡼܝ໛ϟẟⱘ೒⠛䎳䱣ⴔ哴ᷛⱘޚ⊼ᛣⱘᰃˈϟẟⱘϔᮍˈ哴ᷛ⿏ࡼᯊˈӮ᳝ϔᓴ Ўᅶ᠋ッ߯ᓎẟⲬⱘѠ㓈᭄㒘DŽᅶ᠋ッ߯ᓎẟⲬѠ㓈᭄㒘ⱘᮍ⊩Ϣ᳡ࡵ఼ッ߯ᓎⱘᮍᓣϔḋDŽ䖭䞠䳔㽕 ϟẟāDŽ೼ᓔྟ␌៣ⱘᯊ׭ˈህ䳔㽕ܜᯢϟẟⱘ乎ᑣDŽ䙷МⱑẟϔᮍⳟࠄⱘᬜᵰህᰃĀ␌៣ᓔྟˈᇍᮍ ϟẟā޴ϾᄫӮ᜶᜶৥ϟˈ᳔ৢ⍜༅ˈᦤ⼎⥽ᆊ␌៣ᓔྟˈᑊϨ䇈ܜ೒ 15.11 ЁⱘĀ␌៣ᓔྟˈԴ ೒ 15.11 ␌៣ᓔྟ  /Download at http://www.pin5i.com ҷⷕ⏙ऩ˖code\GameHall-Server\src\org\crazyit\gamehall\server\action\TakeChessAction.java ҹϟᰃ᳡ࡵ఼ッⱘ TakeChessAction ߸ᮁᰃ৺㚰߽ⱘҷⷕDŽ 㒘䖯㸠䘡ग़ˈ߸ᮁᰃ৺㚰߽DŽ ᳡ࡵ఼ッⱘ TakeChessAction 䳔㽕ᇚᔧࠡ⥽ᆊϟⱘẟᄤֵᙃֱᄬࠄ᳡ࡵ఼ッⱘѠ㓈᭄㒘ˈݡᇍ䆹᭄ ᠟ᠻ㸠 LostActionDŽབᵰ≵᳝㚰߽ⱘ䆱ˈᇍ᠟ህᠻ㸠ᅶ᠋ッ໘⧚㉏ TakeChessActionDŽ 㟈⥽ᆊ␌៣㚰߽ⱘ䆱ˈ䙷Мህᰃᔧࠡⱘ⥽ᆊᠻ㸠 WinActionˈ⬅Ѣ㞾Ꮕ㚰߽њˈᇍ᠟㞾✊ህᰃ༅䋹ˈᇍ 䳔㽕⊼ᛣⱘᰃˈҹϞⱘҷⷕ䆒㕂њ໘⧚㚰߽ⱘ Action ੠໘⧚༅䋹ⱘ Actionˈབᵰᔧࠡ⥽ᆊϟⱘẟᇐ } this.user.getPrintStream().println(XStreamUtil.toXML(request)); "org.crazyit.gamehall.fivechess.client.action.game.LostAction"); request.setParameter("lostAction", //䆒㕂໘⧚䕧ⱘ Action "org.crazyit.gamehall.fivechess.client.action.game.WinAction"); request.setParameter("winAction", //䆒㕂໘⧚㚰߽ⱘ Action request.setParameter("color", chess.getColor()); request.setParameter("tableNumber", this.table.getTableNumber()); request.setParameter("userId", this.user.getId()); request.setParameter("j", chess.getJ()); request.setParameter("i", chess.getI()); //䆒㕂䇋∖ⱘ৘Ͼখ᭄ "org.crazyit.gamehall.fivechess.client.action.game.TakeChessAction"); "org.crazyit.gamehall.fivechess.server.action.TakeChessAction", Request request = new Request( private void requestTakeChess(Chess chess) { //ਞ䆝᳡ࡵ఼㞾Ꮕϟẟњ ߽њ˅ˈህ䳔㽕䅽ᇍ᠟এ໘⧚༅䋹ⱘࡼ԰DŽ ḠᄤЁϟⱘẟˈᑊϨϟẟⱘ买㡆ᰃҔМˈ䰸њ䖭ѯֵᙃ໪ˈབᵰ⥽ᆊⱘ䖭ϔℹẟᕅડњ␌៣ⱘ㒧ᵰ˄㚰 ᇍ䈵೼Ѡ㓈᭄㒘Ёⱘϔ㓈ؐ˄i˅ϢѠ㓈ؐ˄j˅԰Ў䇋∖খ᭄ˈ঺໪䖬㽕ਞ䆝᳡ࡵ఼ᰃાϾ⥽ᆊ೼ાᓴ ᭄㒭᳡ࡵ఼ˈ⬅Ѣ⥽ᆊ೼ᶤϾԡ㕂ϟẟˈ䆹ԡ㕂ᇍᑨⱘᰃᶤϾ Chess ᇍ䈵ˈ಴ℸ៥Ӏৃҹᇚ䖭Ͼ Chess 䅽ᇍ᠟এ᳈ᮄҪ㞾Ꮕⱘ⬠䴶㒘ӊᑊϨ䕂ࠄᇍ᠟ϟẟDŽ䖭䞠䳔㽕㗗㰥ⱘᰃ䇋∖Ёᑨ䆹ᄬᬒϔѯҔМḋⱘখ ֵᙃֱᄬࠄ᳡ࡵ఼ⱘѠ㓈᭄㒘Ёᑊ߸ᮁᰃ৺㚰߽ˈབᵰ≵᳝㚰߽ˈ䖬䳔㽕ᇚ⥽ᆊⱘϟẟֵᙃথ䗕㒭ᇍ᠟ˈ ⱘᰃᇚ⥽ᆊϟẟⱘخ⥽ᆊϟᅠẟˈ䖬䳔㽕থ䗕ϔ⃵䇋∖ࠄ᳡ࡵ఼ˈਞ䆝᳡ࡵ఼ϟẟњˈ᳡ࡵ఼䳔㽕 } } } this.repaint(); requestTakeChess(chess); //৥᳡ࡵ఼থ䗕䇋∖ this.selectImage = null; //䆒㕂䗝ᢽ೒⠛Ўぎ this.myTurn = false; //䕂ࠄᇍᮍϟẟ chess.setColor(getChessColor()); //䆒㕂买㡆 } else { UIContext.showMessage("䆹ԡ㕂Ꮖ㒣᳝ẟᄤ"); if (chess.getColor() != null) { ᔧࠡԡ㕂᳝ẟᄤ// /Download at http://www.pin5i.com ⥽ᆊϟẟⱘҷⷕᏆ㒣ᅲ⦄ˈ݋ԧᬜᵰབ೒ 15.12 ᠔⼎DŽ 䆌⚍ߏᓔྟ˅ܕ‰ 䆒㕂Ꮉ݋ᷣ೒⠛Ў೒ 15.8˄া ໛ᷛ䆚˄ChessUser ⱘ ready ሲᗻ˅䆒㕂Ў falseޚ‰ ᇚᅶ᠋ッঠᮍ⥽ᆊⱘ ‰ ᇚ䕂ࠄ㞾Ꮕϟẟⱘᷛ䆚˄GamePanel ⱘ myTurn ሲᗻ˅䆒㕂Ў false ‰ ᇚ␌៣⢊ᗕ˄GamePanel ⱘ gaming ሲᗻ˅䆒㕂Ў false ‰ ᇚϟẟᯊ׭ⱘ䗝ᢽ೒⠛䆒㕂Ў null ៣㒧ᵰDŽ␌៣߱ྟ࣪䳔㽕䖯㸠བϟ᪡԰˖ Ӯᠻ㸠ᅶ᠋ッ໘⧚㉏ LostActionDŽWinAction Ϣ LostAction 䛑ᰃᇚ␌៣䞡ᮄ䖯㸠߱ྟ࣪ˈݡਞ䆝⥽ᆊ␌ 䌶њˈ䇗⫼ tellLost ᮍ⊩ਞ䆝ᇍᮍ˖Դ䕧њDŽ䌶ⱘϔᮍህӮᠻ㸠ᅶ᠋ッ໘⧚㉏ WinActionˈ䕧ⱘϔᮍህ ϟⱘ䆹ẟᄤᇐ㟈␌៣㒧ᴳ˄ϟẟⱘ⥽ᆊ㚰߽њ˅DŽ⥽ᆊ㚰߽ৢˈህ䇗⫼ tellWin ᮍ⊩ਞ䆝ϟẟⱘϔᮍ˖Դ བᵰ Chess ᇍ䈵ⱘ买㡆Ϣ⥽ᆊ߮ᠡ᠔ϟⱘẟᄤ买㡆ϔ㟈ᑊϨৃҹ䖲៤ѨϾˈ䙷Мህ䖨ಲ trueˈ㸼 ⼎ } return false; if (count >= 5) return true; } } } } count++; if (c.getColor() != null && c.getColor().equals(chess.getColor())) { Chess c = chessArray[i][j]; for (int j = 0; j < chessArray[i].length; j++) { if (i == chess.getI()) { for (int i = 0; i < chessArray.length; i++) { int count = 0; //䖲㓁ẟᄤⱘᘏ᭄ private boolean vertical(Chess[][] chessArray, Chess chess) { //㒉৥䘡ग़ ҷⷕ⏙ऩ˖code\GameHall-Server\src\org\crazyit\gamehall\server\action\TakeChessAction.java ⱘҷⷕDŽ ᄤDŽ䘡ग़ⱘᯊ׭ˈ䳔㽕㒉৥䘡ग़ǃ῾৥䘡ग़ǃҢϞᕔϟ᭰৥䘡ग़ǃҢϟᕔϞ᭰৥䘡ग़DŽҹϟᰃ㒉৥䘡ग़ ᳡ࡵ఼ⱘѠ㓈᭄㒘ˈ䘡ग़ϔϾѠ㓈᭄㒘Ёⱘ↣ϔϾ Chess ᇍ䈵ˈ߸ᮁᰃ৺ৃҹ῾ǃオǃ᭰䖲៤ѨϾẟ Ѣ validateWin ᮍ⊩ˈvalidateWin ᮍ⊩⫼Ѣ䘡ग़އҹϞⱘҷⷕЁՓ⫼њϔϾ win ⱘᏗᇨؐˈ䆹ؐপ } user.setReady(false); opponent.setReady(false); //䆒㕂᳡ࡵ఼Ёঠᮍⱘ⢊ᗕ tellLost(request, opponent, response); //ਞ䆝䕧ⱘϔᮍ tellWin(request, user, response); //ਞ䆝䌶ⱘϔᮍ if (win) { //ਞ䆝ঠᮍ, 䌶њ if (opponent == null) win = true; boolean win = validateWin(chessArray, chessArray[i][j]); ߸ᮁᰃ৺㚰߽// /Download at http://www.pin5i.com ໘⧚䗗䎥ⱘ Action Ϣ䅸䕧ϔḋˈাᰃᔧ⫼᠋݇䯁␌៣にষᯊᠻ㸠থ䗕䇋∖ˈ᳡ࡵ఼ッϢᅶ᠋ッ໘⧚ 䳔㽕ᇚ䅸䕧⥽ᆊⱘ idˈḠᄤ㓪ݭㄝֵᙃਞ䆝᳡ࡵ఼ˈ಴ℸ䳔㽕⏏ࡴⳌ݇ⱘ䇋∖খ᭄DŽ 䇋∖ৢˈህᇚ䆹⥽ᆊⱘ᠔᳝⢊ᗕ˄᳡ࡵ఼Ёⱘ⢊ᗕ˅䛑䖬ॳˈথ䗕ֵᙃ㒭ᇍ᠟˖Դ䌶њDŽ䇋∖䅸䕧៥Ӏ OpponentLostActionˈᅶ᠋ッ໘⧚㉏ᰃ⬅䌶ⱘϔᮍ˄䅸䕧ᮍⱘᇍ᠟˅ᠻ㸠ⱘDŽᅶ᠋ッ໘⧚㉏ᕫࠄ䅸䕧ⱘ ҹϞҷⷕᵘ䗴ϔ⃵䇋∖থ䗕ࠄ᳡ࡵ఼ˈ᳡ࡵ఼ⱘ໘⧚㉏ᰃ LostActionˈᅶ᠋ッ໘⧚㉏ᰃ } this.user.getPrintStream().println(XStreamUtil.toXML(request)); request.setParameter("tableNumber", this.table.getTableNumber()); request.setParameter("userId", this.user.getId()); "org.crazyit.gamehall.fivechess.client.action.game.OpponentLostAction"); "org.crazyit.gamehall.fivechess.server.action.LostAction", Request request = new Request( public void sendLostRequest() { //থ䗕䅸䕧ⱘ䇋∖ code\fivechess-client\src\org\crazyit\gamehall\fivechess\client\ui\game\GamePanel.java ҷⷕ⏙ऩ˖ ҹϟᰃ GamePanel ⱘ䅸䕧ᮍ⊩DŽ ᇍᮍ㚰߽ˈ䅸䕧Ϣ݇ᥝ␌៣ˈ䛑ৃҹথ䗕ϔ⃵䇋∖ࠄ᳡ࡵ఼ˈ䅽᳡ࡵ఼䖯㸠Ⳍ݇ⱘ໘⧚DŽ ϔᮍ⥽ᆊ⬅Ѣ⡍⅞ॳ಴ˈ⚍ߏњ䅸䕧៪㗙Ⳉ᥹݇ᥝ␌៣ˈ䙷М᳡ࡵ఼ህⳈ᥹߸ᅮ䆹⥽ᆊ䕧ˈᑊᦤ⼎ 15.6.3 䗗䎥Ϣ䅸䕧 ೒ 15.12 ϟẟ␌៣㚰߽  /Download at http://www.pin5i.com 䗕ϔ⃵䇋∖ࠄ᳡ࡵ఼ˈਞ䆝᳡ࡵ఼ৠᛣ∖੠DŽ᳡ࡵ఼໘⧚㉏ᰃ AgreeDrawActionˈᅶ᠋ッ໘⧚㉏˄থ䗕 ҹϞⱘ咥ԧҷⷕ䇗⫼њ draw ᮍ⊩ˈ䆹ᮍ⊩াᰃ᱂䗮ⱘᔍߎᦤ⼎Ϣ߱ྟ࣪␌៣ˈᦤ⼎੠ẟৢˈݡথ } this.user.getPrintStream().println(XStreamUtil.toXML(request)); request.setParameter("tableNumber", this.table.getTableNumber()); request.setParameter("userId", this.user.getId()); "org.crazyit.gamehall.fivechess.client.action.game.AgreeDrawAction"); Request request = new Request("org.crazyit.gamehall.fivechess.server.action.AgreeDrawAction", //ਞ䆝᳡ࡵ఼ৠᛣ∖੠ draw(); private void agreeDraw() { //ৠᛣ∖੠ code\fiveche ҷⷕ⏙ऩ˖ ҹϟᰃ GamePanel Ёৠᛣ∖੠ⱘᮍ⊩DŽ Ѣ fivechess-server ῵ഫˈ᠔᳝ⱘᅶ᠋ッ໘⧚㉏䛑ᄬ೼Ѣ fivechess-client ῵ഫDŽ ᰃ DrawActionˈ䖭ϸϾ Action ৡᄫⳌৠˈԚᰃᄬ೼ⱘ῵ഫϡϔḋDŽᴀゴЁ᠔᳝ⱘ᳡ࡵ఼໘⧚㉏䛑ᄬ೼ ẟ䇋∖ⱘ⥽ᆊⱘᇍ᠟ˈݡ৥䆹ᇍ᠟থ䗕੠ẟ䆶䯂DŽ੠ẟⱘ᳡ࡵ఼໘⧚㉏ᰃ DrawActionˈᅶ᠋ッ໘⧚㉏ ࠄখ᭄Ёˈ䙷М᳡ࡵ఼ህৃҹⶹ䘧ાᓴḠᄤⱘાϾ⥽ᆊ䇋∖੠ẟˈህৃҹḍ᥂䖭ѯֵᙃᕫࠄথ䗕੠ ss-client\src\org\crazyit\gamehall\fivechess\client\ui\game\GamePanel.java 䆒㕂 ҹϞҷⷕᵘ䗴ϔϾ Request ᇍ䈵৥᳡ࡵ఼থ䗕∖੠䇋∖ˈ䳔㽕ᇚ䇋∖Ҏⱘ id˄⥽ᆊ id˅ϢḠᄤ㓪ো } } return; } else { this.user.getPrintStream().println(XStreamUtil.toXML(request)); request.setParameter("tableNumber", this.table.getTableNumber()); request.setParameter("userId", this.user.getId()); "org.crazyit.gamehall.fivechess.client.action.game.DrawAction"); "org.crazyit.gamehall.fivechess.server.action.DrawAction", Request request = new Request( //৥᳡ࡵ఼থ䗕∖੠䇋∖ if (result == 0) { int result = UIContext.showConfirm("Դ⹂ᅮ㽕∖੠৫?"); //䆶䯂ᰃ৺∖੠ private void requestDraw() { //䇋∖∖੠ code\fiveche ҷⷕ⏙ऩ˖ ҹϟᰃ Gam ˄Response˅DŽ ࠄ᳡ࡵ఼ˈਞ䆝᳡ࡵ఼ᑨ䆹԰བԩ໘⧚ˈ᳡ࡵ఼ݡᡒࠄⳌ݇ⱘᅶ᠋ッᑊথ䗕Ⳍᑨⱘ᳡ࡵ఼ડᑨ 䆝੠ẟ䇋∖Ҏˈᇍᮍᢦ㒱੠੠ẟDŽࠄⳂࠡЎℶˈ␌៣໻ख़Ё᠔᳝ⱘⳌ݇䇋∖䛑䗮䖛 Request ᴹথ䗕ֵᙃ 䅽ঠᮍᰒ⼎੠ẟⱘᦤ⼎DŽབᵰᇍ᠟ᢦ㒱੠ẟˈৠḋഄˈгӮথ䗕ϔ⃵䇋∖ࠄ᳡ࡵ఼ˈ䅽᳡ࡵ఼䕀থˈਞ ᇍ᠟ৠᛣ੠ẟˈ䙷Мህথ䗕ֵᙃࠄ᳡ࡵ఼ˈਞ䆝᳡ࡵ఼ҪӀ੠ẟњˈ᳡ࡵݡ৥ঠᮍথ䗕੠ẟⱘֵᙃˈ 㸠ⱘ᪡԰Ϣ䅸䕧㉏ԐDŽ ss-client\src\org\crazyit\gamehall\fivechess\client\ui\game\GamePanel.java ePanel Ё䇋∖੠ẟⱘᮍ⊩DŽ བᵰ 䇋∖੠ẟⱘথ䗕ᮍ৥᳡ࡵ఼থ䗕ϔ⃵䇋∖ˈ䅽᳡ࡵ఼ᡒࠄҪⱘᇍ᠟ˈ৥ᇍ᠟䆶䯂˖ᰃ৺ৠᛣ੠ẟDŽ 15.6.4 䇋∖੠ẟ ㉏ᠻ /Download at http://www.pin5i.com ‰ lib Ⳃᔩ˖ᄬᬒϔѯᅶ᠋ッ᠔ᖙ䳔ⱘࣙDŽ ‰ images Ⳃᔩ˖৘Ͼ␌៣᠔䳔㽕ⱘ೒⠛ᄬ ‰ game Ⳃᔩ˖ᄬᬒ␌៣ᅶ᠋ⱘ jar ࣙ˗ ‰ bin Ⳃᔩ˖ࣙᣀϔѯਃࡼ crazyit-gamehall-client˖ ‰ lib Ⳃᔩ˖ᄬᬒϔѯ᳡ࡵ఼ッ᠔ᖙ䳔ⱘࣙˈ՟ ‰ game Ⳃᔩ˖ᄬᬒ␌៣᳡ࡵ఼ッⱘ jar ࣙ˗ ‰ bin Ⳃᔩ˖ࣙ৿ϔѯਃࡼ crazyit-gamehall-server˖ crazyit-gamehall-client/bin ϟⱘ startup.bat ህৃҹਃࡼᅶ᠋ッDŽҹϟᰃ᳡ࡵ఼ッϢᅶ᠋ッⱘⳂᔩ䇈ᯢDŽ crazyit-gamehall-server/bin ϟⱘ s artup.bat ˈህৃҹਃࡼ᳡ࡵ఼ˈ䖤㸠ᅶ᠋ッⱘ䆱ˈᠧᓔ ᠧᓔܜˈ೼᳝ crazyit-gamehall-client Ϣ crazyit-gamehall-server Ⳃᔩˈ䇏㗙೼䖤㸠␌៣ⱘᯊ׭ ೼ᴀゴⱘḜ՟Ёˈ⑤᭛ӊ᠔ᄬᬒⱘᰃ gamehall-src Ⳃᔩˈ␌៣䖤㸠ⱘⳂᔩᰃ gamehall-runˈ䆹 ԩ䖯㸠䗮ֵˈা䳔㽕݇ᖗ݋ԧⱘϮࡵᅲ⦄ˈࠡᦤᰃᖙ乏䙉ᅜ䖭Ͼ␌៣໻ख़Ḛᶊ᠔ᅮⱘϔѯ㾘߭DŽ fivechess-server Ёˈᇚ␌៣ⱘᅶ᠋ッⱘࣙᬒࠄ fivechess-client Ёˈ㓪ݭ␌៣ⱘᯊ׭ˈᑊϡ䳔㽕݇ᖗབ ᅶ᠋ッᰃ fivechess-client ῵ഫDŽབᵰ៥Ӏ㓪ݭњᮄⱘ␌៣ˈህৃҹⳈ᥹ᇚ␌៣ⱘ᳡ࡵ఼ࣙᬒࠄ ໘⧚㉏ˈ䅽ᅶ᠋ッএᇏᡒᅶ᠋ッ໘⧚㉏DŽ೼ᴀゴЁˈḚᶊⱘ᳡ࡵ఼ッᰃ fivechess-server ῵ഫˈḚᶊⱘ 㽕ᵘ䗴ϔ⃵䇋⼎ˈᇚ᳡ࡵ఼໘⧚㉏Ϣᅶ᠋ッ໘⧚㉏ᬒࠄ䇋∖Ёˈ䅽៥Ӏⱘ᳡ࡵ఼এᐂࡽ៥Ӏᇏᡒ᳡ࡵ఼ 㕂ࠄ䖭ϸϾᇍ䈵Ёˈ៥Ӏ೼ᓔথѨᄤẟ␌៣໻ख़ⱘᯊ׭ˈ෎ᴀϞ≵᳝᥹㾺䖛ӏԩⱘ Socket ᇍ䈵ˈা䳔 䗮ֵˈ䛑ᰃՓ⫼ Request ᇍ䈵ˈ᳡ࡵ఼Փ⫼ Response ᇍ䈵԰ߎડᑨˈ᠔᳝ⱘ䇋∖খ᭄Ϣડᑨখ᭄䛑䆒 ⼎ㄝDŽѨᄤẟ␌៣໻ख़Փ⫼њ៥Ӏ೼ 15.2 Ё㓪ݭⱘ␌៣໻ख़Ḛᶊᴹᅲ⦄ˈ᠔᳝ⱘᅶ᠋ッϢ᳡ࡵ఼ ݇ⱘֵᙃDŽ Դⱘ∖੠㽕∖DŽথ䗕ⱘϔᮍᕫࠄ᳡ࡵ఼ડᑨৢˈህᠻ㸠ᅶ᠋ッ໘⧚㉏ RefuseDrawActionˈᦤ⼎Ⳍ ᬒⳂᔩ˗ ᅶ᠋ⱘੑҸ˗ བ gamehall-server ῵ഫⱘࣙǃxstream ⱘࣙㄝDŽ ᳡ࡵ఼ⱘੑҸ˗ t ϟᄬ Ⳃᔩ ߎᦤ ᕫ᳈དˈ՟བᦤ⼎ֵᙃϡᖙՓ⫼ᔍخࠄℸˈѨᄤẟ␌៣໻ख़Ꮖ㒣ܼ䚼ᅠ៤ˈৃ㛑䖬᳝ϔѯ㒚㡖ৃҹ 15.7 Ѩᄤẟ␌៣໻ख़ᘏ㒧 㒱њ ৠḋഄˈᢦ㒱∖੠гᰃᵘ䗴ϔ⃵䇋∖থ䗕ࠄ᳡ࡵ఼ˈ䅽᳡ࡵ఼এਞ䆝থ䗕∖੠ֵᙃⱘϔᮍ˖ᇍᮍᢦ } this.user.getPrintStream().println(XStreamUtil.toXML(request)); request.setParameter("tableNumber", this.table.getTableNumber()); request.setParameter("userId", this.user.getId()); "org.crazyit.gamehall.fivechess.client.action.game.RefuseDrawAction"); Request request = new Request("org.crazyit.gamehall.fivechess.server.action.RefuseDrawAction", //ਞ䆝᳡ࡵ఼ᢦ㒱੠ẟ private void refuseDraw() { //ᢦ㒱∖੠ code\fiveche ҷⷕ⏙ऩ˖ ss-client\src\org\crazyit\gamehall\fivechess\client\ui\game\GamePanel.java ҹϟᰃ GamePanel Ёᢦ㒱∖੠ⱘᮍ⊩DŽ ߱ྟ࣪⢊ᗕˈৃҹ㒻㓁ϟϔሔⱘ␌៣DŽ њ੠ẟˈথ䗕∖੠ֵᙃⱘϔᮍᕫࠄ᳡ࡵ఼ⱘડᑨৢˈৠḋⱘᡞথ䗕∖੠ֵᙃϔᮍⱘ᠔᳝⢊ᗕ䛑ᬍব៤Ў ᳡ࡵ఼Ёⱘ⢊ᗕˈࣙᣀ䆒㕂⥽ᆊ˄ChessUser˅ⱘ ready ሲᗻˈݡਞ䆝থ䗕∖੠ֵᙃⱘϔᮍˈᇍᮍৠᛣ ੠䇋∖ϔᮍ˅ᰃ AgreeDrawActionDŽᔧ᳡ࡵ఼ッ᥹ᬊࠄৠᛣ∖੠ⱘ䇋∖ৢˈህৃҹ߱ྟ࣪␌៣ঠᮍ೼∖ /Download at http://www.pin5i.com 㘞໽ᅸⱘ݋ԧᬜᵰབ೒ 15.14 ᠔⼎DŽ ડᑨᯊˈህӮᠻ㸠䖭ѯᅶ᠋ッ໘⧚㉏ˈ✊ৢݡᇍ⬠䴶㒘ӊ䖯㸠Ⳍ݇ⱘ᥻ࠊDŽ ᓔথᅶ᠋ッ໘⧚㉏ϢѨᄤẟϔḋˈ䛑ᰃ䗮䖛ϔϾ⬠䴶ⱘϞϟ᭛㉏ᴹᕫࠄ⬠䴶㒘ӊᇍ䈵ˈ᳡ࡵ఼԰ߎ ᇍ䈵ᑊথ䗕㒭᳡ࡵ఼ेৃᅲ⦄ˈ᳡ࡵ఼ݡ԰ߎⳌ݇ⱘડᑨˈ೼ JTextArea Ёᰒ⼎Ⳍ݇ⱘֵᙃDŽ Ӏህ৥݊Ҫ೼㒓ⱘ⫼᠋থ䗕ֵᙃDŽ䖭ḋⱘϔϾ䖛⿟ˈ೼៥Ӏ㓪ݭⱘḚᶊЁˈা䳔㽕ᵘ䗴ϔϾ Request ఼ッ῵ഫЁࡴܹϔϾ㘞໽ᅸϞϟ᭛ˈ⫼Ѣֱᄬ䖯ܹ㘞໽ᅸⱘܼ䚼⫼᠋DŽᔧ᳝ᮄⱘ⫼᠋䖯ܹ㘞໽ᅸᯊˈ៥ 㘞໽ᅸ᠔ࣙ৿ⱘࡼ԰᳝˖⫼᠋䖯ܹ㘞໽ᅸǃথ䗕㘞໽ֵᙃǃ᥹ᬊ㘞໽ֵᙃㄝDŽ៥Ӏ೼㘞໽ᅸⱘ᳡ࡵ 15.8.2 ᅲ⦄㘞໽ᅸ ೒ 15.13 㘞໽ᅸ⬠䴶  ೒ 15.13 ᠔⼎DŽ 㘞໽ᅸ⬠䴶कߚㅔऩˈϔϾ JTextAreaǃJTextFieldǃথ䗕⍜ᙃⱘᣝ䪂Ϣ⫼᠋߫㸼ेৃDŽ݋ԧᬜᵰབ 15.8.1 ᓎゟ㘞໽ᅸ⬠䴶 ఼ッᠧ៤ jar ࣙˈߚ߿ᬒࠄḚᶊⱘ᳡ࡵ఼ⳂᔩϢᅶ᠋ッⳂᔩˈ⌟䆩៥ӀⱘḚᶊᰃ৺ᬃᣕ␌៣ⱘᠽሩDŽ ៥Ӏ㓪ݭњϔϾ␌៣໻ख़ⱘḚˈ៥Ӏݡ⃵㓪ݭϔϾㅔऩⱘ㘞໽ᅸˈᑊᇚ䖭Ͼ㘞໽ᅸⱘᅶ᠋ッϢ᳡ࡵ 㓪ݭϔϾ⌟䆩㘞໽ᅸ 15.8 /Download at http://www.pin5i.com ⱏᔩ⬠䴶ⱘ݋ԧᬜᵰབ೒ 15.15 ᠔⼎DŽ MANIFEST.MF ᭛ӊЁⱘ Game-Class ሲᗻˈ䖭ḋᠡӮ৥ⱏᔩ⬠䴶ⱘ␌៣ϟᢝḚᶊЁࡴܹϔϾᮄⱘ␌៣ˈ ഫਃࡼⱏᔩ⬠䴶ⱘᯊ׭ˈህӮࠄᅶ᠋ッⱘ game Ⳃᔩএࡴ䕑᠔᳝ⱘ␌៣ᅶ᠋ッⱘࣙˈᑊ䇏প䖭ѯࣙЁⱘ 㘞໽ᅸⱘ MANIFEST.MF ᭛ӊໄᯢњ⬅ ChatIndex ㉏԰Ў㘞໽ᅸⱘܹষ㉏ˈᔧ GameHall-Client ῵ Game-Class: org.crazyit.gamehall.chatroom.client.ChatIndex Manifest-Version: 1.0 ҷⷕ⏙ऩ˖code\chat-room-client\src\META-INF\MANIFEST.MF ሲᗻˈᴹໄᯢᅶ᠋ッⱘ␌៣ܹষ㉏ˈ಴ℸ㘞໽ᅸⱘ MANIFEST.MF ᭛ӊݙᆍབϟDŽ ៥ӀⱘḚᶊЁ᳝ϔϾ᳔෎ᴀⱘ㾘߭ˈህᰃ䳔㽕೼ᅶ᠋ッⱘࣙⱘ MANIFEST.MF Ёࡴܹ Game-Class 15.8.3 ᇚ㘞໽ᅸᬒ㕂ࠄḚᶊЁ⌟䆩 ῵ഫϢ chat-room-server ῵ഫDŽ Ḛᶊⱘ޴Ͼ㾘߭ेৃˈ಴ℸ݋ԧⱘ㓪ݭҷⷕ೼ℸϡ䆺㒚ᦣ䗄ˈৃҹ݋ԧখⳟ⑤ҷⷕЁⱘ chat-room-client Action ㉏ेৃˈ៥Ӏৃҹϡ⫼এ݇ᖗབԩ䖯㸠ֵᙃӴ䕧DŽ⬅Ѣ㘞໽ᅸⱘᅲ⦄Ⳍᇍ䕗Ўㅔऩˈা䳔㽕䙉ᅜ ҹⳟࠄ៥Ӏ᠔㓪ݭⱘḚᶊ݋᳝ᠽሩᗻˈབᵰ䳔㽕㓪ݭ݊Ҫ␌៣ࡴܹࠄ䆹ḚᶊЁˈা䳔㽕㓪ݭⳌ݇ⱘ ෎Ѣ៥Ӏ᠔㓪ݭⱘḚᶊᴹᅠ៤ϔϾ㘞໽ᅸकߚㅔऩˈা䳔㽕ㅔऩⱘ⬠䴶ҷⷕ੠޴Ͼ໘⧚㉏ेৃˈৃ ೒ 15.14 㘞໽ᅸᅠ៤ᬜᵰ  /Download at http://www.pin5i.com Ꮰᳯ㛑㒭䇏㗙ᏺᴹϔѯ㓪⿟Ϟⱘਃথˈᓔথߎ᳈໮Ӭ⾔ⱘ෎Ѣ㔥㒰ⱘ␌៣DŽ ϡ䳔㽕ᇍॳᴹⱘҷⷕ䖯㸠ׂᬍˈᣝ✻ϔᅮⱘ㾘߭ህৃҹࡴܹᮄⱘ␌៣DŽᴀゴᓔথⱘ䖭Ͼ␌៣໻ख़Ḛᶊˈ ࠄࡼᗕⱘࡴ䕑␌៣ˈ䅽⥽ᆊ䗝ᢽ䖯ܹⱘ␌៣ˈᔧࡴܹ݊Ҫ␌៣ᯊˈ䆹Ḛᶊᑊخⱘ䖭Ͼ␌៣໻ख़Ḛᶊৃҹ ᄤẟ␌៣໻ख़ᯊˈЏ㽕䆆㾷њѨᄤẟ␌៣໻ख़ⱘᅲ⦄ॳ⧚DŽᴀゴⱘ䞡⚍ᰃ␌៣໻ख़Ḛᶊⱘᓔথˈ䅽៥Ӏ ෎⸔ϞᓔথњϔϾѨᄤẟⱘ␌៣໻ख़ϢϔϾㅔऩⱘ㘞໽ᅸDŽ䆆㾷 Sokcet 㓪⿟ⱘⳌ݇ⶹ䆚⚍DŽ೼ᓔথѨ ᴀゴᓔথњϔϾ␌៣໻ख़ⱘḚᶊˈ䆹ḚᶊЏ㽕⫼Ѣ໘⧚᳡ࡵ఼Ϣᅶ᠋ッП䯈ⱘֵᙃӴ䕧ˈᑊϨ೼ѯ 15.9 ᴀゴᇣ㡖 ࡴܹϔϾᮄⱘ jar ࣙህৃҹᅲ⦄ࡴܹ␌៣DŽ 䖭ḋˈ៥ӀⱘḚᶊህৃҹᬃᣕ␌៣ⱘࡴܹߴ䰸ˈা䳔㽕೼ crazyit-gamehall-client ⱘ game Ⳃᔩϟ ೒ 15.15 ⱏᔩ⬠䴶䗝ᢽ␌៣  ㄀ 15 ゴӓQQ ␌៣໻ख़ g37g
还剩483页未读

继续阅读

下载pdf到电脑,查找使用更方便

pdf的实际排版效果,会与网站的显示效果略有不同!!

需要 8 金币 [ 分享pdf获得金币 ] 0 人已下载

下载pdf

pdf贡献者

dy4n

贡献于2014-07-28

下载需要 8 金币 [金币充值 ]
亲,您也可以通过 分享原创pdf 来获得金币奖励!
下载pdf