⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jgameframe.java

📁 一个JAVA写的连连看游戏,非常不错,是学习JAVA的好资料,值得下载
💻 JAVA
字号:
package jcase.lianliankan;

import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Color;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;

import javax.swing.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/**
 * @author javacat
 *
 * The main game frame
 */
public class JGameFrame extends JFrame implements ActionListener,StageListener {	
		
	Timer t;	
	Stage stage;
	File [] MyFile=new File[3];
	int level;	
	//Font MyFont =new Font("Dialog",Font.ITALIC|Font.BOLD,72);
	JLabel infoLabel ;
	List models; 
	
	JMenuBar menuBar;
	JGameFrame()
	{
		super("连连看");			
		Container cp=this.getContentPane();
		cp.setBackground(Color.WHITE);
		
		infoLabel = new JLabel("初始化>>>>>>>>>>>>>-------------");
		infoLabel.setAlignmentX(JLabel.LEFT_ALIGNMENT);
		cp.setLayout(new BorderLayout());
		cp.add(infoLabel,BorderLayout.CENTER);
		pack();
		centralFrame();
		//init();
		setVisible(true);
		addWindowListener(new WindowCloser());
		new Thread(new ImagePreLoader()).start();
	}
	private void centralFrame(){
		Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
		int w=getSize().width;
		int h=getSize().height;
		if(w<240)
			w=240;
		if(h<120)
			h=120;
		if(w>d.width)
			w=d.width;
		if(h>d.height)
			h=d.height;
		this.setBounds((d.width-w)/2,(d.height-h)/2,w,h);
	}
	public void init()//菜单条
	{
		info("加载菜单");
		menuBar=new JMenuBar();
		
		JMenu menuFile=new JMenu("文件");
		JMenuItem miStart=new JMenuItem("开始");
		miStart.setAccelerator(KeyStroke.getKeyStroke("F1"));
		//JMenuItem miNext=new JMenuItem("下一关");
		//miNext.setAccelerator(KeyStroke.getKeyStroke("F3"));
		JMenuItem miExit=new JMenuItem("退出");		
		menuFile.add(miStart);
		//menuFile.add(miNext);
		for(int l=0;l < Configuration.getLevels();l++){
			menuFile.add(createLevelMenuItem(l));
		}
		menuFile.add(miExit);
		menuBar.add(menuFile);
		JMenu menuOpe=new JMenu("操作");
		JMenuItem miHint=new JMenuItem("提示");
		miHint.setAccelerator(KeyStroke.getKeyStroke("F5"));
		JMenuItem miWash=new JMenuItem("洗牌");
		miWash.setAccelerator(KeyStroke.getKeyStroke("F6"));
		menuOpe.add(miHint);
		menuOpe.add(miWash);
		menuBar.add(menuOpe);
		JMenu menuRecord=new JMenu("记录");
		JMenuItem miRecord=new JMenuItem("查看记录");
		menuRecord.add(miRecord);
		menuBar.add(menuRecord);
		miStart.addActionListener(this);
		
		miExit.addActionListener(this);
		miWash.addActionListener(this);
        	miRecord.addActionListener(this);
        	miHint.addActionListener(this);		
		this.setJMenuBar(menuBar);
					
		info("点击\"文件\"菜单\"开始\"项开始游戏");
		pack();
		centralFrame();
		//this.setResizable(false);
		this.validate();
	}

	public void restart()
	{		
		
		Container cp = getContentPane();
		cp.removeAll();
		nextStage();
		cp.add(stage,BorderLayout.CENTER);	
		if(t!=null){
			t.stop();
		}
		t = createTimer();
		cp.add(t,BorderLayout.SOUTH);		
		pack();
		centralFrame();
		this.validate();
		t.start();
	}
	
	
	
	/**
	 * @return
	 */
	private void nextStage() {		
		int[] d = Configuration.getDimension(level);		
		stage = new Stage(Model.createRandomModel(d[0],d[1]));
		stage.setListener(this);
		stage.wash(false);
	}	
	
	public void actionPerformed(ActionEvent e)
	{
		String command = e.getActionCommand();
		if (command.equals("开始"))
		{
			level=0;			
			restart();
		}		
		else if (command.equals("退出"))
		{
			dispose();
			System.exit(0);
		}
		else if (command.equals("洗牌"))
		{
			stage.wash(true);
		}
		else if (command.equals("提示"))
		{			
			stage.hint();
		}		
	}
	
	void info(String message){
		infoLabel.setText(message);
	}
	private void preloadImages(){
		MediaTracker tracker = new MediaTracker(this);
		int id=0;
		if(Configuration.bg!=null){
			try{
				info("加载背景...");
			tracker.addImage(Configuration.bg,id);
			tracker.waitForID(id);
			}catch(InterruptedException ie){
				Log.debug("interrupted while loading background image.",ie);
			}
		}
		try{
			id=1;
			Image icon = Toolkit.getDefaultToolkit().getImage(
					getClass().getResource("/icons/icon.png"));
			tracker.addImage(icon,id);
			tracker.waitForID(id);
			this.setIconImage(icon);
		}catch(Exception e){
			Log.debug("Can not set icon image.",e);
		}
		for(char c='0';c<'Z';c++){
			String s=Configuration.getConfig(c+".icon");
			if(s!=null && s.trim().length()>0){
				try{
					info("加载"+s+"...");
				id=c-29;
				tracker.addImage(Configuration.getImage(c),id);
				tracker.waitForID(id);
				}catch(InterruptedException ie){
					Log.debug("Interrupted while loading "+s,ie);
				}
			}
		}		
	}
	/**
	 * 从文件加载游戏,这个方法现在已经不使用,而用随机生成model的方法。
	 * 这个方法在前期开发中测试工作中使用。
	 */
	private void loadGames(){
		info("加载游戏");
		try{
			String gamefilesrc = Configuration.getConfig("game.file");
			File file;
			if(gamefilesrc!=null){
				file = new File(gamefilesrc);
				if(!file.exists()){
					file = new File("games.txt");
				}
			}else{
				file=new File("games.txt");
			}
			FileReader filereader = new FileReader(file);
			BufferedReader reader = new BufferedReader(filereader);
			models = new ArrayList();
			String line = reader.readLine();
			ArrayList list = new ArrayList();
			while(line!=null && !(line.equals(".EOF"))){
				if(line.equals(".")){
					String[] lines = new String[list.size()];
					list.toArray(lines);
					models.add(Model.createModel(lines));
					list.clear();
				}else{
					list.add(line);
				}				
				line=reader.readLine();
			}
		}catch(IOException ioe){
			Log.debug("Error reading games!",ioe);
		}
	}
	private Timer createTimer(){
		int seconds = Configuration.getConfigedInt("base.seconds",180);
		seconds+= level*Configuration.getConfigedInt("level.seconds",60);
		Timer timer = new Timer(seconds);
		timer.setListener(this);
		return timer;
	}
	
	private JMenuItem createLevelMenuItem(int l){
		JMenuItem item = new JMenuItem(Configuration.getConfig("level."+l+".name"));
		item.setActionCommand(Integer.toString(l));
		l++;
		item.setAccelerator(KeyStroke.getKeyStroke("alt "+l));
		item.addActionListener(ll);
		return item;
	}
	/** 
	 * 过关,显示信息提醒用户。 
	 * @see jcase.lianliankan.StageListener#congratulate()
	 */
	public void congratulate() {
		JOptionPane.showMessageDialog(this,"恭喜您!");
		restart();
	}
	/*
	 * @see jcase.lianliankan.StageListener#credit(int)
	 */
	public void credit(int seconds) {
		t.credit(seconds);

	}
	/* (non-Javadoc)
	 * @see jcase.lianliankan.StageListener#timedOut()
	 */
	public void timedOut() {
		JOptionPane.showMessageDialog(this,"时间到","超时",JOptionPane.ERROR_MESSAGE);
		restart();
	}
	private LevelListener ll = new LevelListener();
	class LevelListener implements ActionListener{		 
		public void actionPerformed(ActionEvent e){
			level = Integer.parseInt(e.getActionCommand());
		    restart();
		}
	}
	class ImagePreLoader implements Runnable{		
		public void run(){
			preloadImages();
			//loadGames();
			init();			
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -