1、题目
点名(或抽奖)
存入一组人名及照片,开始快速自动播放姓名和照片,按空格键停止。输出中奖!在此伴有音乐。
2、代码如下
- //文件名:Demo4.java
- package com.liuyanzhao;
- import java.awt.*;
- import java.awt.event.*;
- import java.util.jar.Attributes.Name;
- import javax.sound.midi.MidiEvent;
- import javax.sound.midi.MidiSystem;
- import javax.sound.midi.Sequence;
- import javax.sound.midi.Sequencer;
- import javax.sound.midi.ShortMessage;
- import javax.sound.midi.Track;
- import javax.swing.*;
- public class Demo4 {
- public static void main(String[] args) {
- new GUI();
- }
- }
- class GUI extends JFrame {
- MyJPanel mp;
- Timer timer;
- JTextArea text;
- int index;
- int count = 0;
- ImageIcon[] imgs = { new ImageIcon("src/images/1.jpg"), new ImageIcon("src/images/2.jpg"),
- new ImageIcon("src/images/3.jpg"), new ImageIcon("src/images/4.jpg"), new ImageIcon("src/images/5.jpg"),
- new ImageIcon("src/images/6.jpg"), new ImageIcon("src/images/7.jpg"), new ImageIcon("src/images/8.jpg"), };
- String[] players = { "刘邦", "赵匡胤", "朱元璋", "项羽", "李世民", "曹操", "康熙", "武则天" };
- public GUI() {
- mp = new MyJPanel();
- JPanel panel2 = new JPanel();
- this.add(mp);
- this.setLayout(new GridLayout(2, 1));
- this.setExtendedState(JFrame.MAXIMIZED_BOTH);
- this.setDefaultCloseOperation(EXIT_ON_CLOSE);
- this.setTitle("窗口");
- this.addKeyListener(new keyListener());
- this.setVisible(true);
- text = new JTextArea("");
- text.setFont(new Font("宋体", Font.BOLD, 34));
- panel2.add(text);
- this.add(panel2);
- timer = new Timer(50, new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- mp.repaint();
- }
- });
- timer.start();
- }
- class MyJPanel extends JPanel {
- @Override
- public void paint(Graphics g) {
- super.paint(g);
- g.drawImage(imgs[index % imgs.length].getImage(), 770, 0, this);
- text.setText(players[index % players.length]);
- index++;
- }
- }
- class keyListener implements KeyListener {
- @Override
- public void keyPressed(KeyEvent e) {
- // TODO Auto-generated method stub
- if(count<1) {
- if (KeyEvent.getKeyText(e.getKeyCode()).equals("空格")) {
- timer.stop();
- text.insert("恭喜", 0);
- text.append("获得本次大奖");
- new Music();
- count++;
- }
- }
- }
- @Override
- public void keyReleased(KeyEvent e) {
- // TODO Auto-generated method stub
- }
- @Override
- public void keyTyped(KeyEvent e) {
- // TODO Auto-generated method stub
- }
- }
- }
- class Music {
- public Music() {
- try {
- Sequencer sequencer = MidiSystem.getSequencer();
- sequencer.open();
- Sequence seq = new Sequence(Sequence.PPQ, 4);
- Track track = seq.createTrack();
- for (int i = 0; i < 60; i += 4) {
- track.add(makeEvent(144, 1, i, 100, i));
- track.add(makeEvent(128, 1, i, 100, i + 2));
- }
- sequencer.setSequence(seq);
- sequencer.setTempoInBPM(220);
- sequencer.start();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static MidiEvent makeEvent(int comd, int chan, int one, int two, int tick) {
- MidiEvent event = null;
- try {
- ShortMessage a = new ShortMessage();
- a.setMessage(comd, chan, one, two);
- event = new MidiEvent(a, tick);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return event;
- }
- }
3、效果图如下
4、补充
音乐的播放是通过音阶组成的队列,图片需要在src目录新建images文件夹,文件名如上。
本文地址:https://liuyanzhao.com/4187.html
转载请注明
您可以选择一种方式赞助本站
支付宝扫一扫赞助
微信钱包扫描赞助
赏