/** * Copyright 1999-2009 The Pegadi Team * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ //======================================================================= // File : TetrisGame.java (derived from...) // Software : Tetris // Author : Steve Fu (steve@intrinsa.com foureyes@aimnet.com) // Date : 04/22/96 // Version : 1.1 ( JDK 1.0) // // Copyright (c) 1996 Steve Fu. All Rights Reserved. // // Permission to use, modify, copy, distribute this software for // NON-COMMERCIAL or COMMERCIAL purposes is hereby granted subject // to the following exceptions: // 1. This copyright information must remain as whole or part of // header of this file or its modified versions. // 2. Credits must be given to the original author, Steve Fu, for // any redistribution of the software. // // AUTHOR MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF // THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED // TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A // PARTICULAR PURPOSE, OR NON-INFRINGEMENT. AUTHOR SHALL NOT BE LIABLE FOR // ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR // DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. // //======================================================================= /** * @author HÃ¥vard Wigtil * @version 0.0 ($Revision$, $Date$) */ package org.pegadi.games.tetris; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.swing.*; import java.awt.*; public class PreviewWindow extends JPanel { private Image mOffScrnImage = null ; //Double Buffer screen image private int mWidth; /** Width of display. */ private int mHeight; /** Height of display. */ protected Brick brick; private final Logger log = LoggerFactory.getLogger(getClass()); public PreviewWindow() { mWidth = (16 * 4); // FIXME: Remove hardcoded tile size (16) mHeight = (16 * 2); // FIXME: Remove hardcoded tile size (16) } public void init() { mOffScrnImage = createImage(mWidth, mHeight); try{ while( (checkImage(mOffScrnImage,null)&ALLBITS) == 0 ) Thread.sleep(100); Graphics g0 = mOffScrnImage.getGraphics(); g0.setColor(getBackground()); g0.fillRect(0,0, mWidth, mHeight); } catch(Exception e) { log.error("Could not init", e); } } /** * Set a new Block as the preview block. * * @since 0.1 */ public void drawNextBlock(Brick brick) { this.brick = brick; log.debug("next = {}", brick); repaint(); // Use paintImmediately if this method is too slow. } /** * Repaints the component. If <code>mFallingBlock</code> is null this will * do nothing. * * @param g The context to paint on. */ public void paintComponent(Graphics g) { super.paintComponent(g); if (brick == null) { log.warn("Brick is NULL, do nothing..."); return; } int imgIndex = brick.getFallingBlock().getType(); if (imgIndex > TetrisGame.BRICKTYPE) { imgIndex = TetrisGame.BRICKTYPE; } Image brickImage = brick.getTile(); int fbkX, fbkY, fbkW, fbkH; fbkX = brick.getFallingBlock().getX(); fbkY = brick.getFallingBlock().getY(); fbkW = brick.getFallingBlock().getW(); fbkH = brick.getFallingBlock().getH(); g.setColor(Color.black); for ( int i = 0; i< fbkH; i++ ) { for ( int j = 0; j< fbkW; j++ ) { if (brick.getFallingBlock().getMapEntry(i,j) == 1 ) { int x = j * TetrisGame.BRICKSIZE; int y = i * TetrisGame.BRICKSIZE; g.fill3DRect(x+2, y+2, 12, 12, true); g.drawImage(brickImage, x,y,null); } } } } public Dimension getMinimumSize(){ return new Dimension(mWidth, mHeight); } public Dimension getPreferredSize(){ return new Dimension(mWidth, mHeight); } }