《仙劍奇俠傳柔情版》Java的簡單實現(二)
阿新 • • 發佈:2018-12-03
基於《仙劍奇俠傳柔情版》Java的簡單實現(二)
2018-12-02 by louis
需要上次的GameFrame.class中視窗框架承載:https://www.cnblogs.com/louis6575/p/10051873.html
當時沒有學過JButton,於是當時就把未選中的【開發團隊】【舊的回憶】【新的開始】Ps到背景層,然後把選中狀態存為三張圖片,並使用以下程式碼進行讀取:
1 package firstDemo; 2 3 // 為更好說明解決方式,現已在原始碼上進行關鍵部分抽取 4 5 import javax.imageio.ImageIO;6 import javax.swing.*; 7 import java.awt.*; 8 import java.awt.event.KeyEvent; 9 import java.awt.event.KeyListener; 10 import java.io.File; 11 import java.io.IOException; 12 13 public class GamePanel extends JPanel implements Runnable, KeyListener { 14 15 int startX = 0,startY = 0;16 int startButtonX = 250,startButtonY = 125; 17 Image[] startButtonImages = new Image[3]; 18 Image startImage; 19 boolean isStartShow = true; 20 boolean isstartButtonShow = true; 21 22 int startButtonIndex = 0; 23 private int start_DIST = 120,resume_DIST = 109,limUP = 240,limDOWN = 566,limRIGHT = 250,limLEFT = 10;24 25 public GamePanel() { 26 // 載入背景層,測試圖片要求:圖片畫素大小為1024x768 27 try { 28 startImage = ImageIO.read(new File("仙劍奇俠傳遊戲素材/開始遊戲/start0.png")); 29 } catch (IOException e) { 30 e.printStackTrace(); 31 } 32 // 載入自制按鈕圖示,測試圖片要求:圖片大小187x630 33 for (int i = 0; i < startButtonImages.length; i++) { 34 try { 35 startButtonImages[i] = ImageIO.read(new File("仙劍奇俠傳遊戲素材/開始遊戲/"+i+".png")); 36 } catch (IOException e) { 37 e.printStackTrace(); 38 } 39 } 40 } 41 42 //繪畫方法 43 public void paint(Graphics g) { //繪畫 44 super.paint(g); //清空原有畫面,重新畫dialogImage 45 if(isStartShow){ 46 g.drawImage(startImage,startX,startY,this); 47 if(isstartButtonShow){ 48 g.drawImage(startButtonImages[startButtonIndex],startButtonX,startButtonY,this); 49 } 50 } 51 } 52 53 @Override 54 public void keyTyped(KeyEvent e) { 55 56 } 57 58 @Override 59 public void keyPressed(KeyEvent e) { 60 int KeyCode = e.getKeyCode(); 61 if(KeyCode == KeyEvent.VK_UP){ 62 63 } else if(KeyCode == KeyEvent.VK_DOWN){ 64 65 66 } else if(KeyCode == KeyEvent.VK_LEFT){ 67 if(isStartShow){ 68 startButtonX -= start_DIST; 69 startButtonIndex++; 70 if(startButtonIndex > startButtonImages.length-1){ 71 startButtonIndex = 0; 72 startButtonX = limRIGHT; 73 } 74 } 75 } else if(KeyCode == KeyEvent.VK_RIGHT){ 76 if(isStartShow){ //RIGHT按鈕切換開始遊戲介面選項 77 startButtonX += start_DIST; 78 startButtonIndex--; 79 if(startButtonIndex < 0){ 80 startButtonIndex = 2; 81 startButtonX = limLEFT; 82 } 83 } 84 } repaint(); 85 } 86 87 @Override 88 public void keyReleased(KeyEvent e) { 89 90 } 91 92 @Override 93 public void run() { 94 while(true) { 95 try { 96 Thread.sleep(200); 97 } catch (InterruptedException e) { 98 e.printStackTrace(); 99 } repaint(); 100 } 101 } 102 }
原素材:
背景:https://www.cnblogs.com/images/cnblogs_com/louis6575/1354506/t_start0.png
新的開始:https://www.cnblogs.com/images/cnblogs_com/louis6575/1354506/t_0.png
舊的回憶:https://www.cnblogs.com/images/cnblogs_com/louis6575/1354506/t_1.png
開發團隊:https://www.cnblogs.com/images/cnblogs_com/louis6575/1354506/t_2.png