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

📄 jpiecetest.java

📁 一个java的俄罗斯方块实现
💻 JAVA
字号:
package cs111.tetris.testing;import java.awt.Color;import java.awt.Dimension;import java.awt.Graphics;import java.awt.Point;import java.awt.Rectangle;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import javax.swing.BoxLayout;import javax.swing.JComponent;import javax.swing.JFrame;import cs111.tetris.data.Piece;/** * Debugging client for the Piece class. The JPieceTest component draws all the * rotations of a tetris piece. JPieceTest.main() creates a frame with one * JPieceTest for each of the 7 standard tetris pieces. * <p> * Below is a screenshot of what the display should look like if the Piece  * class is correctly implemented.  * <p> * <IMG SRC="doc-files/JPieceTest-screenshot.gif" ALT="Screenshot"> * <p> * Note: the yellow blocks indicate the "skirt" of the pieces. *  * @author Nick Palante (Revised in 2007 by Kris De Volder) */public class JPieceTest extends JComponent {	protected Piece root;	/**	 * Create a JPieceTest component for a given piece.	 * 	 * @param piece  The piece.	 * @param width  Width in pixels for the component on the screen.	 * @param height Height in pixels for the component on the screen.	 */	public JPieceTest(Piece piece, int width, int height) {		super();		setPreferredSize(new Dimension(width, height));		root = piece;	}	public final int BLOCKSIZE = 16;	/**	 * Draws the rotations from left to right. Each piece goes in its own little	 * box.	 */	protected void paintComponent(Graphics g) {		final int MAX_ROTATIONS = 6; 		// A safety limit to avoid an infinite loop		// This isn't truly necessary if Piece class is correctly implemented.		// However, having this bound, will make the code a little more robust		// when there are possible bugs in the Piece class.		Piece piece = root;		int rectsize = piece.getBody().length * BLOCKSIZE + 2;		int left = 0;		int rot = 0;		do {			rot++;			drawPiece(g, piece, new Rectangle(left, 0, rectsize, rectsize));			piece = piece.nextRotation();			left += rectsize;		} while (piece != root && rot < MAX_ROTATIONS);	}	/**	 * Draw the piece inside the given rectangle.	 */	private void drawPiece(Graphics g, Piece piece, Rectangle r) {		g.drawRect(r.x, r.y, r.width, r.height);		Point[] points = piece.getBody();		for (int i = 0; i < points.length; i++) {			drawBlock(g, r, points[i].x, points[i].y);		}		g.setColor(Color.YELLOW);		int[] skirt = piece.getSkirt();		if (skirt!=null) // only run this code if student implemented skirt already.			for (int i = 0; i < skirt.length; i++) {				drawBlock(g, r, i, skirt[i]);			}		g.setColor(Color.RED);		g.drawString(" w: " + piece.getWidth() + " h: " + piece.getHeight(),				r.x, BLOCKSIZE);		g.setColor(Color.BLACK);	}	private void drawBlock(Graphics g, Rectangle r, int x, int y) {		g.fillRect(r.x + x * BLOCKSIZE + 2, r.y + r.height - (y + 1)				* BLOCKSIZE, BLOCKSIZE - 1, BLOCKSIZE - 1);	}	/**	 * Draws all the pieces by creating a JPieceTest for each piece, and putting	 * them all in a frame.	 */	static public void main(String[] args) {		JFrame frame = new JFrame("Piece Tester");		JComponent container = (JComponent) frame.getContentPane();		// Put in a BoxLayout to make a vertical list		container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));		Piece[] pieces = Piece.getPieces();		for (int i = 0; i < pieces.length; i++) {			JPieceTest test = new JPieceTest(pieces[i], 375, 75);			container.add(test);		}		// Size the window and show it on screen		frame.pack();		frame.setVisible(true);		// Quit on window close		frame.addWindowListener(new WindowAdapter() {			public void windowClosing(WindowEvent e) {				System.exit(0);			}		});	}}

⌨️ 快捷键说明

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