Java貪吃蛇小遊戲原始碼系列之輔助類
阿新 • • 發佈:2019-01-01
圖片音樂類素材
連結:https://pan.baidu.com/s/1-RMnKVGlRSq3il0-Bsd3qg
提取碼:qa3w
Java貪吃蛇小遊戲之音樂類
package snakeGame; /* * 設定程式執行背景音樂,包括 點選按鈕音樂,鼓勵音,撞牆音樂,吃食物音樂等等, * 音樂類的設定可以在snakeGame類中實現,由於音樂太多,故而集中到一起,可以在需要新增的地方例項化即可。 */ import java.applet.AudioClip; import java.net.MalformedURLException; import java.net.URL; import javax.swing.JApplet; import javax.swing.JPanel; //新增鼓勵音 class applauseMusic { AudioClip christmas = loadSound ("F:\\MYJAVA\\Myprogram\\Snakeexample\\src\\Music\\applauseMusic.wav"); public applauseMusic () { christmas.play ();//設定為播放 } public AudioClip loadSound ( String filename )//獲得播放路徑 { URL url = null; try { url = new URL ("file:" + filename); } catch (MalformedURLException e) {} return JApplet.newAudioClip (url); } } //新增吃到食物的音樂 class eatFoodMusic { AudioClip christmas = loadSound ("F:\\MYJAVA\\Myprogram\\Snakeexample\\src\\Music\\eatFoodMusic.wav"); public eatFoodMusic () { christmas.play (); } public AudioClip loadSound ( String filename ) { URL url = null; try { url = new URL ("file:" + filename); } catch (MalformedURLException e) {} return JApplet.newAudioClip (url); } } //新增撞牆音樂 class DeadMusic { AudioClip christmas = loadSound ("F:\\MYJAVA\\Myprogram\\Snakeexample\\src\\Music\\DeadMusic.wav"); public DeadMusic () { christmas.play (); } public AudioClip loadSound ( String filename ) { URL url = null; try { url = new URL ("file:" + filename); } catch (MalformedURLException e) {} return JApplet.newAudioClip (url); } } //新增 按鍵音樂 class pushButtonMusic { AudioClip christmas = loadSound ("F:\\MYJAVA\\Myprogram\\Snakeexample\\src\\Music\\pushButtonMusic.wav"); public pushButtonMusic () { christmas.play (); } public AudioClip loadSound ( String filename ) { URL url = null; try { url = new URL ("file:" + filename); } catch (MalformedURLException e) {} return JApplet.newAudioClip (url); } } // 新增速度控制音樂 class speedButtonMusic{ AudioClip christmas = loadSound ("F:\\MYJAVA\\Myprogram\\Snakeexample\\src\\Music\\speedButtonMusic.wav"); public speedButtonMusic() { christmas.play (); } public AudioClip loadSound ( String filename ) { URL url = null; try { url = new URL ("file:" + filename); } catch (MalformedURLException e) {} return JApplet.newAudioClip (url); } }
Java貪吃蛇小遊戲之 圓角按鈕類
package snakeGame; /*在Java swing中的button無法設定圓角按鈕,為了一定程度上完善遊戲介面,故而設定了圓角按鈕。 * 定義圓角按鈕; */ import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.awt.GradientPaint; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.Shape; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.geom.RoundRectangle2D; import javax.swing.JButton; public class RButton extends JButton { private static final long serialVersionUID = 39082560987930759L; public static final Color BUTTON_COLOR1 = new Color(205, 255, 205); //設定按鈕的第一種顏色 public static final Color BUTTON_COLOR2 = new Color(51, 55, 47); //設定按鈕的第二種顏色 // public static final Color BUTTON_COLOR1 = new Color(125, 161, 237); // public static final Color BUTTON_COLOR2 = new Color(91, 118, 173); public static final Color BUTTON_FOREGROUND_COLOR = Color.WHITE; private boolean hover; public RButton(String name) { this.setText(name); //新增名字 setFont(new Font("system", Font.PLAIN, 12)); //設定字型 setBorderPainted(false); //設定邊界可見否 setForeground(BUTTON_COLOR2); //設定前景色 setFocusPainted(false); setContentAreaFilled(false); //定義滑鼠事件,即當滑鼠進入到按鈕介面時,按鈕顏色會發生變化,滑鼠離開按鈕區域時也會發生變化 addMouseListener(new MouseAdapter() { @Override public void mouseEntered(MouseEvent e) { setForeground(BUTTON_FOREGROUND_COLOR); hover = true; repaint(); } @Override public void mouseExited(MouseEvent e) { setForeground(BUTTON_COLOR2); hover = false; repaint(); } }); } //設定按鈕圓角 @Override protected void paintComponent(Graphics g) { Graphics2D g2d = (Graphics2D) g.create(); int h = getHeight(); int w = getWidth(); float tran = 1F; if (!hover) { tran = 0.3F; } g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); GradientPaint p1; GradientPaint p2; //當點選按鈕之後,按鈕顏色和字型會發生變化 if (getModel().isPressed()) { p1 = new GradientPaint(0, 0, new Color(0, 0, 0), 0, h - 1, new Color(100, 100, 100)); p2 = new GradientPaint(0, 1, new Color(0, 0, 0, 50), 0, h - 3, new Color(255, 255, 255, 100)); } else { p1 = new GradientPaint(0, 0, new Color(100, 100, 100), 0, h - 1, new Color(0, 0, 0)); p2 = new GradientPaint(0, 1, new Color(255, 255, 255, 100), 0, h - 3, new Color(0, 0, 0, 50)); } g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, tran)); RoundRectangle2D.Float r2d = new RoundRectangle2D.Float(0, 0, w - 1, h - 1, 20, 20); Shape clip = g2d.getClip(); g2d.clip(r2d); GradientPaint gp = new GradientPaint(0.0F, 0.0F, BUTTON_COLOR1, 0.0F, h, BUTTON_COLOR2, true); g2d.setPaint(gp); g2d.fillRect(0, 0, w, h); g2d.setClip(clip); g2d.setPaint(p1); g2d.drawRoundRect(0, 0, w - 1, h - 1, 20, 20); g2d.setPaint(p2); g2d.drawRoundRect(1, 1, w - 3, h - 3, 18, 18); g2d.dispose(); super.paintComponent(g); } }