存入一组人名及照片,开始快速自动播放姓名和照片,按空格键停止。输出中奖!在此伴有音乐。

avatar 2017年5月14日22:38:34 评论 83 views

 

1、题目

点名(或抽奖)

存入一组人名及照片,开始快速自动播放姓名和照片,按空格键停止。输出中奖!在此伴有音乐。

 

2、代码如下

  1. //文件名:Demo4.java
  2. package com.liuyanzhao;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.util.jar.Attributes.Name;
  6. import javax.sound.midi.MidiEvent;
  7. import javax.sound.midi.MidiSystem;
  8. import javax.sound.midi.Sequence;
  9. import javax.sound.midi.Sequencer;
  10. import javax.sound.midi.ShortMessage;
  11. import javax.sound.midi.Track;
  12. import javax.swing.*;
  13. public class Demo4 {
  14.     public static void main(String[] args) {
  15.         new GUI();
  16.     }
  17. }
  18. class GUI extends JFrame {
  19.     MyJPanel mp;
  20.     Timer timer;
  21.     JTextArea text;
  22.     int index;
  23.     int count = 0;
  24.     ImageIcon[] imgs = { new ImageIcon("src/images/1.jpg"), new ImageIcon("src/images/2.jpg"),
  25.             new ImageIcon("src/images/3.jpg"), new ImageIcon("src/images/4.jpg"), new ImageIcon("src/images/5.jpg"),
  26.             new ImageIcon("src/images/6.jpg"), new ImageIcon("src/images/7.jpg"), new ImageIcon("src/images/8.jpg"), };
  27.     String[] players = { "刘邦""赵匡胤""朱元璋""项羽""李世民""曹操""康熙""武则天" };
  28.     public GUI() {
  29.         mp = new MyJPanel();
  30.         JPanel panel2 = new JPanel();
  31.         this.add(mp);
  32.         this.setLayout(new GridLayout(21));
  33.         this.setExtendedState(JFrame.MAXIMIZED_BOTH);
  34.         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  35.         this.setTitle("窗口");
  36.         this.addKeyListener(new keyListener());
  37.         this.setVisible(true);
  38.         text = new JTextArea("");
  39.         text.setFont(new Font("宋体", Font.BOLD, 34));
  40.         panel2.add(text);
  41.         this.add(panel2);
  42.         timer = new Timer(50new ActionListener() {
  43.             @Override
  44.             public void actionPerformed(ActionEvent e) {
  45.                 mp.repaint();
  46.             }
  47.         });
  48.         timer.start();
  49.     }
  50.     class MyJPanel extends JPanel {
  51.         @Override
  52.         public void paint(Graphics g) {
  53.             super.paint(g);
  54.             g.drawImage(imgs[index % imgs.length].getImage(), 7700this);
  55.             text.setText(players[index % players.length]);
  56.             index++;
  57.         }
  58.     }
  59.     class keyListener implements KeyListener {
  60.         @Override
  61.         public void keyPressed(KeyEvent e) {
  62.             // TODO Auto-generated method stub
  63.             if(count<1) {
  64.                 if (KeyEvent.getKeyText(e.getKeyCode()).equals("空格")) {
  65.                     timer.stop();
  66.                     text.insert("恭喜"0);
  67.                     text.append("获得本次大奖");
  68.                     new Music();
  69.                     count++;
  70.                 }
  71.            }
  72.         }
  73.         @Override
  74.         public void keyReleased(KeyEvent e) {
  75.             // TODO Auto-generated method stub
  76.         }
  77.         @Override
  78.         public void keyTyped(KeyEvent e) {
  79.             // TODO Auto-generated method stub
  80.         }
  81.     }
  82. }
  83. class Music {
  84.     public Music() {
  85.         try {
  86.             Sequencer sequencer = MidiSystem.getSequencer();
  87.             sequencer.open();
  88.             Sequence seq = new Sequence(Sequence.PPQ, 4);
  89.             Track track = seq.createTrack();
  90.             for (int i = 0; i < 60; i += 4) {
  91.                 track.add(makeEvent(1441, i, 100, i));
  92.                 track.add(makeEvent(1281, i, 100, i + 2));
  93.             }
  94.             sequencer.setSequence(seq);
  95.             sequencer.setTempoInBPM(220);
  96.             sequencer.start();
  97.         } catch (Exception e) {
  98.             e.printStackTrace();
  99.         }
  100.     }
  101.     public static MidiEvent makeEvent(int comd, int chan, int one, int two, int tick) {
  102.         MidiEvent event = null;
  103.         try {
  104.             ShortMessage a = new ShortMessage();
  105.             a.setMessage(comd, chan, one, two);
  106.             event = new MidiEvent(a, tick);
  107.         } catch (Exception e) {
  108.             e.printStackTrace();
  109.         }
  110.         return event;
  111.     }
  112. }

 

3、效果图如下

4、补充

音乐的播放是通过音阶组成的队列,图片需要在src目录新建images文件夹,文件名如上。

 

本文地址:https://liuyanzhao.com/4187.html

转载请注明

  • 微信
  • 交流学习,有偿服务
  • weinxin
  • 博客/Java交流群
  • 资源分享,问题解决,技术交流。群号:590480292
  • weinxin

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: