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

📄 mainwindow.java

📁 进入cmd 执行java WindowGrid进入游戏界面
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import java.util.*;

/******************************************************************
 *  超级俄罗斯方块				
 *版本: 1.0
 *作者:金永哲
 *最后修改日期:2005-9-18
 ******************************************************************/
public class MainWindow extends JFrame
{
	public static void main(String[] args) throws Exception
	{
		MainWindow mw = new MainWindow();
		mw.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	//	mw.setVisible(true);	 	 	 
	}
	
	private WindowGrid winGrid = null;
	
	
	public MainWindow() throws BlockException
	{
		//设置框架属性
		this.setTitle("俄罗斯方块  作者:金永哲");
		this.setBounds(110, 30, 600, 510);
		this.getContentPane().setBackground(Color.GRAY);
		this.setLayout(null); 
		
		//添加面板-------------------------------------
		//信息面板
		Container contentPane = this.getContentPane();
		InfoPanel infoPanel = new InfoPanel();
		contentPane.add(infoPanel);				
		//游戏面板		
		winGrid =new WindowGrid();		
		winGrid.setBackground(Color.BLACK);		
		contentPane.add(winGrid);		
		
		//显示框架--------------------------------------
		ImageIcon image = new ImageIcon("gif\\main.gif");
		this.setIconImage(image.getImage() );
		this.setVisible(true);	
		
		//调整面板尺寸----------------------------------		
		winGrid.setBounds(0, 0, 300, 480);							 
		infoPanel.setBounds(300, 0, 300, 480);
		infoPanel.init();
		
										 	
	}
	
	
	
/************************************************************************
 * 信息面板
 *
 ***********************************************************************/
class InfoPanel extends JPanel
{
	private JPanel  palNextType  = null; //下次方块组类型
	private JLabel  lblScore     = null; //分数标签
	private JLabel  lblSpeed     = null; //速度标签
	private JLabel  lblTime      = null; //时间标签
	private JEditorPane edtScore = null; //分数文本框
	private JEditorPane edtSpeed = null; //速度文本框
	private JEditorPane edtTime  = null; //时间文本框
	private JButton btnStart     = null; //开始按钮
	private JButton btnPause     = null; //暂停按钮
	private Font fontLabel       = null; //标签字体
	private Font fontEdit        = null; //编辑框字体
	private long seconds         = 0;    //游戏开始的秒数
	private TNewTimer tmrPassed  = null; //计算游戏经过的时间的时钟 
	private javax.swing.Timer tmrSetTitle    = null;

	
	//构造函数==========================================================
	public InfoPanel()
	{
		//设置面板参数
		this.setLayout(null);
		this.setBackground(Color.BLUE);					
	}
	public void init()
	{
		int nWidthParent = this.getSize().width;//但前面板宽度
				
		//添加控件------------------------------------------						
		//创建控件------------------------
		palNextType = new JPanel();	
		lblScore    = new JLabel("分数:");
		lblSpeed    = new JLabel("速度:");
		lblTime     = new JLabel("时间:");			
		edtScore    = new JEditorPane();
		edtSpeed    = new JEditorPane();
		edtTime     = new JEditorPane();				
		btnStart    = new JButton(new Action_btnStart("新游戏", 
								"重新开始一个新游戏") );
		btnPause    = new JButton(new Action_btnPause("暂停", 
								"暂停游戏", "继续", "继续游戏") );
		fontLabel   = new Font("宋体", Font.BOLD, 15);
		fontEdit    = new Font("宋体", Font.BOLD, 30);
				
		//设置字体-----------------------
		lblScore.setFont(fontLabel);
		lblSpeed.setFont(fontLabel);
		lblTime.setFont(fontLabel);
		edtScore.setFont(fontEdit);
		edtSpeed.setFont(fontEdit);
		edtTime.setFont(fontEdit);	
		
		//设置字体颜色-------------------
		edtScore.setForeground(new Color(255, 0, 0) );
		edtSpeed.setForeground(new Color(255, 0, 0) );
		edtTime.setForeground(new Color(255, 0, 0) );
		
		//设置位置,尺寸------------------
		palNextType.setBackground(Color.BLUE);
		palNextType.setBounds((nWidthParent-120)/2, 10, 120, 120);				
		lblScore.setBounds(50, 140, 50, 30);
		lblSpeed.setBounds(50, 220, 50, 30);
		lblTime.setBounds(50, 300, 50, 30);		
		edtScore.setBounds(50, 170, 150, 40);
		edtSpeed.setBounds(50, 250, 150, 40);
		edtTime.setBounds(50, 330, 150, 40);		
 		btnStart.setBounds(50, 390, 80, 25);
 		btnPause.setBounds(140, 390, 80, 25);

		//设置控件不可接受焦点, 文本框为只读--
		edtScore.setEditable(false);
		edtSpeed.setEditable(false);
		edtTime.setEditable(false);
		edtScore.setFocusable(false);
		edtSpeed.setFocusable(false);
		edtTime.setFocusable(false);
		btnStart.setFocusable(false);
		btnPause.setFocusable(false);
		
		//设置文本框初始值--------------	
		edtScore.setText("0");
		edtSpeed.setText("1");
		edtTime.setText("00:00:00");
		
		//添加到InfoPanel面板------------
		this.add(palNextType);
		this.add(lblScore);
		this.add(lblSpeed);
		this.add(lblTime);
		this.add(edtScore);
		this.add(edtSpeed);
		this.add(edtTime);		 
		this.add(btnStart);
		this.add(btnPause);
		this.repaint();
		
		btnPause.setEnabled(false);		
		tmrPassed = new TNewTimer(1000);
		tmrPassed.addNewTimerListener(new NewTimerListener_tmrPassed() );	
		
		//设置winGrid显示分数, 速度的文本框---------------
		winGrid.setScoreEditor(this.edtScore);
		winGrid.setSpeedEditor(this.edtSpeed);
		winGrid.setPassedTimer(this.tmrPassed);
		palNextType.setLayout(null);		
		winGrid.setNextTypeContainer(palNextType);
		
		tmrSetTitle = new javax.swing.Timer(10, new ActionListener_tmrSetTitle()) ;
		
	}
	
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D)g;
		if(this.getSize().width == 0 )
			return;
			
		ImageIcon imageIcon = new ImageIcon("gif\\bqb.jpg");
		g2.drawImage(imageIcon.getImage(), 0, 0, 300, 490,
			Color.BLUE, null  );
	}
/***********************************************************************
 *开始按钮的动作
 **********************************************************************/
class Action_btnStart extends AbstractAction
{
	public Action_btnStart(String name, String description)
	{
		this.putValue(Action.NAME, name);
		this.putValue(Action.SHORT_DESCRIPTION, description);
	}
	public void actionPerformed(ActionEvent event)
	{
		winGrid.start();
		if(!btnPause.isEnabled())
			btnPause.setEnabled(true);
		winGrid.requestFocus();
		seconds = 0;
		tmrPassed.start();
		edtScore.setText("0");
		edtSpeed.setText("1");
		tmrSetTitle.start();
	}
}	
/***********************************************************************
 *暂停按钮的动作
 **********************************************************************/
class Action_btnPause extends AbstractAction
{
	private String namePause;
	private String nameContinue;
	private String descriptionPause;
	private String descriptionContinue;
	private boolean isPauseButton = true;
	
	//此按钮有两个状态,暂停/继续,分别对应不同外观
	public Action_btnPause(String namePause, String descriptionPause,
					String nameContinue, String descriptionContinue)
	{
		this.namePause = namePause;
		this.nameContinue = nameContinue;
		this.descriptionPause = descriptionPause;
		this.descriptionContinue = descriptionContinue;
		this.putValue(Action.NAME, namePause);
		this.putValue(Action.SHORT_DESCRIPTION, descriptionPause);							
	}
	public void actionPerformed(ActionEvent event)
	{
		if(isPauseButton)//当前为暂停按钮,暂停游戏,然后变成继续按钮
		{
			winGrid.pause();
			isPauseButton = false;
			this.putValue(Action.NAME, nameContinue);
			this.putValue(Action.SHORT_DESCRIPTION, descriptionContinue);
			tmrPassed.stop();
			tmrSetTitle.stop();
		}
		else //当前为继续按钮, 继续游戏, 然后变成暂停按钮
		{
			winGrid.continueRun();
			isPauseButton = true;
			this.putValue(Action.NAME, namePause );
			this.putValue(Action.SHORT_DESCRIPTION, descriptionPause);
			tmrPassed.start();
			tmrSetTitle.start();
		}
		
	}
} 
 
/***********************************************************************
 *时钟动作, 此时钟为计算游戏开始至今的时间
 *seconds中记录着秒数, 并将其转换为时间格式,显示在edtTime中
 **********************************************************************/
class NewTimerListener_tmrPassed implements TNewTimerListener
{
	public void action(TNewTimerEvent event)
	{		
		seconds++;
		int hour = (int)seconds / 3600;		
		int minute = (int)((seconds - hour * 3600) / 60);
		int second = (int)((seconds - hour * 3600) % 60);
		String chour, cminute, csecond;
		if(hour < 10)
			chour = "0" + hour;
		else
			chour = "" + hour;
		if(minute < 10)
			cminute = "0" + minute;
		else
			cminute = "" + minute;
		if(second < 10)
			csecond = "0" + second;
		else
			csecond = "" + second;			
		
		edtTime.setText(chour + ":" + cminute + ":" + csecond);						
	}
}

class ActionListener_tmrSetTitle implements ActionListener
{
	public void actionPerformed(ActionEvent event)
	{
		String strNum = "";
		int[] num = new int[27];
		for(int i=0; i<27; i++)
		{
			num[i] = (int)(Math.random()*10);			
			strNum += "" + num[i];
		}
		String strCaption = "俄罗斯方块 作者:金永哲   电脑正在计算:" + strNum;
		MainWindow.this.setTitle(strCaption);
	}
}

}
 
}

⌨️ 快捷键说明

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