2017/07/02 《JAVA程序设计》 实训课第四天
今天的主要收获:基本编写好了游戏的代码,只是在运行上还没有达到作业的要求。不过,在这个学习的过程中,我还是学到了知识,比如:如何找一些你想要编制的代码——去专业的网站寻找如CSDN、谷歌等,而绝非是只有百度! 在编制猜猜看游戏的时候,我还去借鉴网站上的小游戏,如托马斯猜猜看游戏,看它是如何去构造游戏背景等结构。
在编辑代码的时候也有参考老师、同学的资料,非常感谢你们!
在设计游戏的过程中,也不忘了添加一些玩意儿,比如JMF的应用,在游戏里面加入了MP3播放音乐,使游戏更加趣味,主要需要解决的问题是要下载一个JMF的JAVA插件,安装后,进行环境变量的修改,最重要的一点是在所属代码文件中,在你的project JAVA中右键找到“bulid path"添加C:\Program Files\JMF2.1.1e\lib 加进sound和jmf进去即可,再加上简单的代码进去就可以运行了。
import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.EndOfMediaEvent;
import javax.media.PrefetchCompleteEvent;
import javax.media.RealizeCompleteEvent;
import java.io.*;
import java.util.*;
public class PlayerMusic implements ControllerListener {
//播放对象
private Player player;
//是否循环播放
private boolean first,loop;
//文件路径
private String path;
//存放MP3文件
private List<String> mp3List;
//当前MP3文件数量
private int mp3NO=0;
PlayerMusic(List<String> mp3List)
{
this.mp3List=mp3List;
}
//播放方法
public void start()
{
try {
player = Manager.createPlayer(new File(mp3List.get(mp3NO)).toURI().toURL());
} catch (NoPlayerException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
System.out.println("不能播放此文件!");
return;
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
return;
}
if(player==null)
{
System.out.println("播放文件为空!");
return;
}
player.addControllerListener(this);
//提取媒体内容
player.prefetch();
}
public void controllerUpdate(ControllerEvent e) {
//当媒体播放结束时,循环播放
if(e instanceof EndOfMediaEvent)
{
mp3NO++;
System.out.println(mp3NO);
if(mp3NO<mp3List.size())
{
this.start();
}
return;
}
//当提取媒体的内容结束
if (e instanceof PrefetchCompleteEvent) {
System.out.println("内容结束");
player.start();
return;
}
//当实例化后
if (e instanceof RealizeCompleteEvent) {
System.out.println("实例化");
//pack(); //执行pack()操作
return;
}
}
public static void main(String[] args)
{
List<String> path=new ArrayList<String>();
path.add("E:\\1.mp3");
PlayerMusic play=new PlayerMusic(path);
play.start();
}
}
另外,设计的猜猜看游戏已经可以运行了,所以就将代码发上博文,如下所示。
import java.awt.EventQueue;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Image;
import java.awt.SystemColor;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.Random;
public class Guess01<lblImg1> extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
String strPath = ""; //文件夹路径 <img src="https://img-blog.csdn.net/20150703103839275?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzkxNDM5Ng==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
String strFileName = ""; //文件名称
File[] fileArray; // 文件夹下所有文件
int NUM_IMG = 0; // 文件总数目
int index = 0; // 当前文件的序号
int index1 =0;
int index2 =0;
private JPanel contentPane;
private JTextField tfDir;
private JTextField tfClass;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Guess01 frame = new Guess01();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Guess01() {
setTitle("\u731C\u731C\u770B\u6E38\u620FV0.1");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 645, 409);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
// 选择目录 按钮的处理程序
final JButton btnDir = new JButton("\u9009\u62E9\u76EE\u5F55");
btnDir.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JFileChooser jfc=new JFileChooser("E:/");
//jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
jfc.showDialog(new JLabel(), "选择");
File file=jfc.getSelectedFile();
tfDir.setText(file.getAbsolutePath());
strPath = file.getAbsolutePath();
strFileName = jfc.getSelectedFile().getName();
if(file!=null && file.isDirectory()){
// 获取文件夹下所有的文件
fileArray = file.listFiles();
NUM_IMG = fileArray.length;
}
}
});
btnDir.setBounds(26, 26, 93, 23);
contentPane.add(btnDir);
// 文本框,显示目录
tfDir = new JTextField();
tfDir.setEditable(false);
tfDir.setBounds(125, 27, 363, 21);
contentPane.add(tfDir);
tfDir.setColumns(10);
// 选择班级 按钮的处理程序
final JButton btnClass = new JButton("\u9009\u62E9\u73ED\u7EA7");
btnClass.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser jfc=new JFileChooser("E:/");
//jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
jfc.showDialog(new JLabel(), "选择");
File file=jfc.getSelectedFile();
strPath = file.getAbsolutePath();
strFileName = jfc.getSelectedFile().getName();
tfClass.setText(jfc.getSelectedFile().getName());
{
}
}
});
btnClass.setBounds(26, 59, 93, 23);
contentPane.add(btnClass);
// 文本框,显示班级文件
tfClass = new JTextField();
tfClass.setEditable(false);
tfClass.setBounds(125, 60, 363, 21);
contentPane.add(tfClass);
tfClass.setColumns(10);
// 标签,显示带猜测学生姓名
final JLabel lbGuessName = new JLabel("\u59D3\u540D");
lbGuessName.setBounds(259, 91, 200, 23);
contentPane.add(lbGuessName);
// 标签,显示第一个学生相片
final JLabel lblImg1 = new JLabel("\u56FE\u72471");
lblImg1.setBackground(Color.RED);
this.add(lblImg1);
lblImg1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
if(arg0.getSource()==lblImg1){
if((lblImg1.getText().equals(lbGuessName.getText()))){
JOptionPane.showMessageDialog(null,"是的没错");
}
else {
JOptionPane.showMessageDialog(null,"不不是的");
}
}
}
});
lblImg1.setBounds(26, 151, 183, 178);
contentPane.add(lblImg1);
// 标签,显示第二个学生相片
final JLabel lblImg2 = new JLabel("\u56FE\u72472");
lblImg2.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg1) {
if(arg1.getSource()==lblImg2){
if((lblImg2.getText().equals(lbGuessName.getText()))){
JOptionPane.showMessageDialog(null,"是的没错");
}
else {
JOptionPane.showMessageDialog(null,"不不是的");
}
}
}
});
lblImg2.setForeground(Color.BLACK);
lblImg2.setBackground(SystemColor.inactiveCaption);
lblImg2.setBounds(241, 155, 183, 172);
contentPane.add(lblImg2);
// 标签,显示第三个学生相片
final JLabel lblImg3 = new JLabel("\u56FE\u72473");
lblImg3.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg2) {
if(arg2.getSource()==lblImg3){
if((lblImg3.getText().equals(lbGuessName.getText()))){
JOptionPane.showMessageDialog(null,"是的没错");
}
else {
JOptionPane.showMessageDialog(null,"不不是的");
}
}
}
});
lblImg3.setBounds(434, 155, 185, 172);
contentPane.add(lblImg3);
// 再猜一次 按钮,点击则更新相应的三张图片 与 带猜测学生姓名
JButton btnGuessAgain = new JButton("\u518D\u731C\u4E00\u6B21");
btnGuessAgain.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ImageIcon icon = null;
File f = new File(strPath);
String[] names = f.list(new FilenameFilter(){
@Override
public boolean accept(File f, String name) {
// TODO Auto-generated method stub
return name.endsWith(".jpg");
}
});
Random r = new Random();
int []nArrey ={r.nextInt(NUM_IMG),r.nextInt(NUM_IMG),r.nextInt(NUM_IMG)};
String imageSrc = strPath+"/"+names[nArrey[0]];
String imageSrc1 = strPath+"/"+names[nArrey[1]];
String imageSrc2 =strPath+"/"+names[nArrey[2]];
//随机数
lblImg1.setIcon(new ImageIcon(imageSrc));
try {
icon = new ImageIcon(ImageIO.read(new File(imageSrc)));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Image image = icon.getImage(); // 从图表中获取到图片
Image smallImage = image.getScaledInstance(200,200,Image.SCALE_FAST);// 缩放图像
icon = new ImageIcon(smallImage);//把Image文件转化为ImageIcon
lblImg1.setIcon(icon);
lblImg1.setText(names[nArrey[0]]);
lblImg2.setIcon(new ImageIcon(imageSrc1));
try {
icon = new ImageIcon(ImageIO.read(new File(imageSrc1)));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Image image1 = icon.getImage(); // 从图表中获取到图片
Image smallImage1 = image1.getScaledInstance(200,200,Image.SCALE_FAST);// 缩放图像
icon = new ImageIcon(smallImage1);//把Image文件转化为ImageIcon
lblImg2.setIcon(icon);
lblImg2.setText(names[nArrey[1]]);
lblImg3.setIcon(new ImageIcon(imageSrc2));
try {
icon = new ImageIcon(ImageIO.read(new File(imageSrc2)));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Image image2 = icon.getImage(); // 从图表中获取到图片
Image smallImage2 = image2.getScaledInstance(2000,200,Image.SCALE_FAST);// 缩放图像
icon = new ImageIcon(smallImage2);//把Image文件转化为ImageIcon
lblImg3.setIcon(icon);
lblImg3.setText(names[nArrey[2]]);
Random l=new Random();
int s= l.nextInt(3)-1;
//取值随机数3个 减一可选择0、1、2数组数
lbGuessName.setText(names[nArrey[s]]);
}});
btnGuessAgain.setBounds(223, 337, 93, 23);
contentPane.add(btnGuessAgain);
}
private void add(JLabel lblImg1) {
// TODO Auto-generated method stub
}
}