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

📄 breakoutjpanel.java

📁 一款小游戏 希望能帮助初学者 一些基本的知识 是作业作品
💻 JAVA
字号:
/*
	JPanel define for BREAK OUT game
	Author: Junfeng Huang
	ID: 4283757
	Login Name: Jhua098
*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class BreakOutJPanel extends JPanel implements KeyListener, ActionListener{

	private boolean showingRect = false;
	private static final int DEFAULT_PAUSE = 20;
	private Ball ballA;
	private Rectangle gameBall;
	private Rectangle gameRect;
	private Timer t;

	public BreakOutJPanel() {
		ballA =new Ball();
		gameBall = ballA.ball;
		gameRect = BreakOutConstants.GAME_AREA;
		t = new Timer(300 , this);
		addKeyListener(this);
	}

	public void keyPressed(KeyEvent e){
		showingRect = true;
		if (e.getKeyCode() == KeyEvent.VK_UP){
			t.start();
		}
		repaint();
	}

	public void actionPerformed(ActionEvent e){
		//Ball.move(gameBall);
		//repaint();
	}


//-------------------------------------------------------------------
//-------- Paints the BreakOut game area, the paddle ----------------
//-------- and the bricks. ------------------------------------------
//-------------------------------------------------------------------
	public void paintComponent(Graphics g) {
		super.paintComponent(g);

//draw the game panel , game area, the ball's start position, the paddle and the bricks

		if (  showingRect ) {

			g.setColor(Color.BLACK);
			g.fillRect(0 , 0 , BreakOutConstants.FRAME_WIDTH , BreakOutConstants.FRAME_HEIGHT);
			g.setColor(Color.GREEN);
			g.fillRect(gameRect.x , gameRect.y , gameRect.width , gameRect.height);

			ballA.drawBall(g);

//draw title screen

		} else {
			drawTitleScreen(g);
		}

	}

	private void drawTitleScreen(Graphics g){
		g.setFont( new Font	("TIMES", Font.BOLD, 24));
		g.drawString("BREAK OUT", 20, 100);
		g.setFont( new Font ("TIMES", Font.BOLD,12));
		g.drawString(" jhua098",20,180);
		g.setFont( new Font ("TIMES", Font.BOLD,18));
		g.drawString("Press any key to see the game area",20,270);
		g.drawString("Press the UP key to start the game",20,300);
		g.drawString("Press the RIGHT or the LEFT key",20,520);
		g.drawString("to move the paddle",65,560);
	}

	public void keyReleased(KeyEvent e) {}
	public void keyTyped(KeyEvent e) {}

}

⌨️ 快捷键说明

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