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

📄 main.java

📁 SWT编写的俄罗期方块
💻 JAVA
字号:
package eric.block;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;

/**
 * Class/Interface description
 * 
 * @author Eric
 * @see Another Class
 * @since 0.1
 */
public class Main
{

	protected Shell shell;

	protected Menu mainMenuBar;

	protected MenuItem fileMenuHeader;

	protected MenuItem helpMenuHeader;

	protected Canvas currentBoardCanvas;

	protected Canvas nextBoardCanvas;

	protected Label lbScore;

	protected Label lbLevel;

	protected int score;

	protected int level;

	protected Button btStartPause;

	protected Game game;

	protected Display display;

	private GC gameGC;

	private GC nextGC;

	/**
	 * Launch the application
	 * 
	 * @param args
	 */
	public static void main(String[] args)
	{
		try
		{
			Main window = new Main();
			window.open();
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * Open the window
	 */
	public void open()
	{
		display = Display.getDefault();
		createContents(display);
		shell.open();
		shell.layout();
		while (!shell.isDisposed())
		{
			if (!display.readAndDispatch())
				display.sleep();
		}
	}

	/**
	 * Create contents of the window
	 */
	protected void createContents(Display display)
	{
		shell = new Shell();
		shell.setSize(600, 600);
		shell.setText("Russion Block");

		// Create menu bar
		createMenuBar();

		GridLayout shellLayout = new GridLayout(2, true);

		initLocation(display, shellLayout);

		createCanvas(display);

		score = 0;
		lbScore = new Label(shell, SWT.NONE);
		lbScore.setText("score:  " + score);

		GridData lbScoreGridData = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
		lbScoreGridData.widthHint = 150;
		lbScoreGridData.horizontalIndent = 80;
		lbScoreGridData.verticalIndent = 20;
		lbScore.setLayoutData(lbScoreGridData);

		level = 1;
		lbLevel = new Label(shell, SWT.NONE);
		lbLevel.setText("level:  " + level);
		GridData lbLevelGridData = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
		lbLevelGridData.widthHint = 150;
		lbLevelGridData.horizontalIndent = 80;
		lbLevelGridData.verticalIndent = 20;
		lbLevel.setLayoutData(lbLevelGridData);

		btStartPause = new Button(shell, SWT.PUSH);
		btStartPause.setText("Start");
		GridData startPauseButtonGridData = new GridData(GridData.HORIZONTAL_ALIGN_CENTER);
		startPauseButtonGridData.widthHint = 100;
		startPauseButtonGridData.verticalIndent = 50;
		btStartPause.setLayoutData(startPauseButtonGridData);

		game = new Game(10, 20, display, this);
		gameGC = new GC(currentBoardCanvas);
		nextGC = new GC(nextBoardCanvas);

		addListener();

		shell.pack();
		shell.open();
	}

	/**
	 * @param display
	 * @param shellLayout
	 */
	private void initLocation(Display display, GridLayout shellLayout)
	{
		Rectangle splashRect = shell.getBounds();
		Rectangle displayRect = display.getBounds();
		int x = (displayRect.width - splashRect.width) / 2;
		int y = (displayRect.height - splashRect.height) / 2;
		shell.setLocation(x, y);
		shell.setLayout(shellLayout);
	}

	/**
	 * @param display
	 */
	private void createCanvas(Display display)
	{
		/*
		 * final SashForm sashForm = new SashForm(shell, SWT.NONE); sashForm.setLayoutData(new GridData());
		 */

		currentBoardCanvas = new Canvas(shell, SWT.NONE);
		GridData gd = new GridData(GridData.FILL_BOTH);
		gd.verticalSpan = 5;
		gd.heightHint = 600;
		gd.widthHint = 400;
		currentBoardCanvas.setBackground(new Color(display, 0, 0, 0));
		currentBoardCanvas.setLayoutData(gd);

		nextBoardCanvas = new Canvas(shell, SWT.NONE);
		gd = new GridData(GridData.HORIZONTAL_ALIGN_CENTER);
		gd.heightHint = 150;
		gd.widthHint = 150;
		nextBoardCanvas.setBackground(new Color(display, 0, 0, 0));
		nextBoardCanvas.setLayoutData(gd);

		/*
		 * // set two composite high sashForm.setWeights(new int[] { 200,107});
		 */
	}

	/**
	 * Create menu bar
	 */
	private void createMenuBar()
	{
		// Create mainMenu
		mainMenuBar = new Menu(shell, SWT.BAR);

		// Create file menu item header
		fileMenuHeader = new MenuItem(mainMenuBar, SWT.CASCADE);
		fileMenuHeader.setText("File(&F)");

		// create file menu
		Menu fileMenu = new Menu(shell, SWT.DROP_DOWN);
		MenuItem restartMenuItem = new MenuItem(fileMenu, SWT.PUSH);
		restartMenuItem.setText("Restart(&R)");
		new MenuItem(fileMenu, SWT.SEPARATOR);
		MenuItem exitMenuItem = new MenuItem(fileMenu, SWT.PUSH);
		exitMenuItem.setText("Exit(&E)");
		exitMenuItem.addSelectionListener(new SelectionAdapter()
		{
			public void widgetSelected(SelectionEvent e)
			{
				shell.dispose();
			}
		});

		// Create help menu item header
		helpMenuHeader = new MenuItem(mainMenuBar, SWT.CASCADE);
		helpMenuHeader.setText("Help(&H)");

		// create help menu item
		Menu helpMenu = new Menu(shell, SWT.DROP_DOWN);
		MenuItem aboutMenuItem = new MenuItem(helpMenu, SWT.PUSH);
		aboutMenuItem.setText("About(&A)");
		new MenuItem(helpMenu, SWT.SEPARATOR);
		MenuItem helpMenuItem = new MenuItem(helpMenu, SWT.PUSH);
		helpMenuItem.setText("Help(&U)");

		fileMenuHeader.setMenu(fileMenu);
		helpMenuHeader.setMenu(helpMenu);

		shell.setMenuBar(mainMenuBar);
	}

	/**
	 * @return Canvas
	 */
	public Canvas getCurrentBoardCanvas()
	{
		return currentBoardCanvas;
	}

	/**
	 * @return Canvas
	 */
	public Canvas getNextBoardCanvas()
	{
		return nextBoardCanvas;
	}

	/**
	 * Add listener
	 */
	public void addListener()
	{
		currentBoardCanvas.addKeyListener(new ControlGameByKey());
		currentBoardCanvas.addPaintListener(new PaintGameBoard());
		nextBoardCanvas.addPaintListener(new PaintNextBoard());
		btStartPause.addSelectionListener(new StartPauseAdapter());
	}

	/**
	 * @return Display
	 */
	public Display getDisplay()
	{
		return display;
	}

	class PaintGameBoard implements PaintListener
	{
		public void paintControl(PaintEvent e)
		{
			GameBoard gameBoard = game.gameBoard;
			MyGC.setMyGC(gameGC);
			if (!game.isPlaying())
			{
				if (!game.isGameOver())
				{
					gameBoard.drawMessage("Press Start");
				} else
				{
					gameBoard.draw();
					gameBoard.drawMessage("Game Over");
				}
			} else
			{
				if (game.isPaused())
				{
					gameBoard.draw();
					game.currentBlock.draw();
					gameBoard.drawMessage("Paused");
				} else
				{
					gameBoard.draw();
					
					//Draw current block
					//game.currentBlock.draw();
				}
			}
		}
	}

	// Next canvas paint
	class PaintNextBoard implements PaintListener
	{

		public void paintControl(PaintEvent e)
		{
			MyGC.setMyGC(nextGC);
			if (game.isGameOver())
			{
				game.nextBoard.clear();
			} else if (!game.isPlaying())
			{
				game.nextBoard.clear();
			} else
			{
				game.nextBoard.draw();
			}
			MyGC.setMyGC(gameGC);
		}

	}

	class SwtCloseAdapter extends SelectionAdapter implements Listener
	{
		public void handleEvent(Event event)
		{
			display.dispose();
			game.quit();
			gameGC.dispose();
			nextGC.dispose();
		}

	}

	class ControlGameByKey extends KeyAdapter
	{
		public void keyPressed(KeyEvent e)
		{
			game.controlGame(e);
		}
	}

	class StartPauseAdapter extends SelectionAdapter
	{
		public void widgetSelected(SelectionEvent arg0)
		{
			if (!game.isPlaying())
			{
				if (!game.isGameOver())
				{
					game.startGame();
					btStartPause.setText("Pause");
				} else
				{
					game.gameOver();
					if (game.isTryAgain())
					{
						btStartPause.setText("Pause");
						game.startGame();
					}
				}
			} else
			{
				if (!game.isPaused())
				{
					btStartPause.setText("Resume");
					game.pauseGame();
				}else
				{
					btStartPause.setText("Pause");
					game.resumeGame();
				}
			}
			currentBoardCanvas.forceFocus();
			currentBoardCanvas.redraw();
		}
	}

	/**
	 * @param level
	 */
	public void setLevelText(int level)
	{
		lbLevel.setText("level:  " + level);
	}

	/**
	 * @param text
	 */
	public void setBtStartPauseText(String text)
	{
		btStartPause.setText(text);
	}

	/**
	 * @param score
	 */
	public void setScoreText(int score)
	{
		lbScore.setText("score:  " + score);
	}

}

⌨️ 快捷键说明

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