CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2008
    Posts
    16

    Overriding color of drawn rectangle: how?

    I have this tetris game, and i used a drawrect() method to draw a border around the playing area. However, the colors of my pieces are overridden by the drawrect() method. how do i make the color of the piece dominant?

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Overriding color of drawn rectangle: how?

    You'll need to be a little more specific about the classes and techniques involved, such as are you using Swing or AWT, what class does the drawRect method belong to, where are you calling it, etc.

    Best if you can post the relevant code with an explanation.

    Everyone knows that debugging is twice as hard as writing a program in the first place. So if you are as clever as you can be when you write it, how will you ever debug it?
    B. Kernighan
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Sep 2008
    Posts
    16

    Re: Overriding color of drawn rectangle: how?

    Code:
    import java.awt.*; import javax.swing.*;
    import java.util.*; import java.awt.event.*;
    import javax.swing.event.*;
    public class JTetris extends JComponent {
    	public final int DELAY = 400;
    	protected Color color;
    	protected Board board; protected Piece[] pieces;
    	protected Piece currentPiece; protected int currentX;
    	protected javax.swing.Timer timer; protected JSlider speed;
    	protected JButton startButton; protected JButton stopButton;
    	protected int newX; protected int newY; protected boolean gameOn;
    	protected int count; protected long startTime; protected Random random;
    	
    	//used to set the current piece in play
    
    	public int setCurrent(Piece piece, int x, int y) {
    		int result = board.place(piece, x, y);
    		if (result <= Board.PLACE_ROW_FILLED) {
    			if (currentPiece != null) repaintPiece(currentPiece, currentX, currentY);
    			currentPiece = piece; currentX = x; currentY = y;
    			repaintPiece(currentPiece, currentX, currentY);
    		} else board.undo(); return result;
    	}
    	
    	public void tick(int verb) {
    		if (!gameOn) return;
    		if (currentPiece != null) board.undo();
    		computeNewPosition(verb);
    		int result = setCurrent(newPiece, newX, newY);
    		if (result ==  Board.PLACE_ROW_FILLED) repaint();
    		boolean failed = (result >= Board.PLACE_OUT_BOUNDS);
    		if (failed) 
    			if (currentPiece != null) board.place(currentPiece, currentX, currentY);
    		if (failed && verb==DOWN && !moved) {
    			if (board.clearRows())  repaint();
    			if (board.getMaxHeight() > board.getHeight() - TOP_SPACE)  stopGame();
    			else addNewPiece();
    		} moved = (!failed && verb!=DOWN);
    	}
    
    	public void repaintPiece(Piece piece, int x, int y) {
    		if (DRAW_OPTIMIZE) {
    			int px = xPixel(x); int py = yPixel(y + piece.getHeight() - 1);
    			int pwidth = xPixel(x+piece.getWidth()) - px;
    			int pheight = yPixel(y-1) - py;
    			color = piece.colSel;
    			repaint(px, py, pwidth, pheight);
    		} else  repaint();
    	}
    	
    	public void paintComponent(Graphics g) {
    		g.drawRect(0, 0, getWidth()-1, getHeight()-1);
    		int spacerY = yPixel(board.getHeight() - TOP_SPACE - 1);
    		g.drawLine(0, spacerY, getWidth()-1, spacerY);
    		Rectangle clip = null;
    		if (DRAW_OPTIMIZE)  clip = g.getClipBounds();
    		
    		final int dx = Math.round(dX()-2);
    		final int dy = Math.round(dY()-2);
    		final int bWidth = board.getWidth();
    		int x, y;
    		for (x=0; x<bWidth; x++) {
    			int left = xPixel(x); int right = xPixel(x+1) -1;
    			if (DRAW_OPTIMIZE && clip!=null) {
    				if ((right<clip.x) || (left>=(clip.x+clip.width))) continue;
    			}
    			final int yHeight = board.getColumnHeight(x);
    			for (y=0; y<yHeight; y++) {
    				if (board.getGrid(x, y)) {
    					final boolean filled = (board.getRowWidth(y)==bWidth);
    					if (filled) g.setColor(Color.green);
    					g.fillRect(left+1, yPixel(y)+1, dx, dy);	
    					if (filled) g.setColor(Color.black);
    				}
    			}
    		}
    	}
    }
    I hope somebody could understand this.

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Overriding color of drawn rectangle: how?

    I can't see where the pieces get drawn/painted, but in your repaintPiece method, you get the piece color and never use it - that looks like a bug. Other than that, all the repaintPiece method seems to do is to ask the component to refresh the area where the piece is supposed to be, which will presumably clear any piece that was drawn there - assuming a piece was drawn there in the first place...

    The ability to simplify means to eliminate the unnecessary so that the necessary may speak...
    H. Hofmann
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  5. #5
    Join Date
    Apr 2007
    Posts
    442

    Re: Overriding color of drawn rectangle: how?

    Hhhmmm...

    the colors of my pieces are overridden by the drawrect() method
    and where exactly is that happening? I dont see any calls to drawRect(), I see you placing the paint back to black, but that matters not, since there is no further drawing since.

    Rest is given with the understanding, that I do not know, how close this is to the standard implementation of Tetris.

    May be a bit out of original scope, but in my most humbled opinion, you are attempting to do things a bit more difficult than you would need to. Each piece in a traditional tetris, while being of variable shape, consists of blocks of equal size. Thus you could see your board, as a two dimensional array. Each Piece would know what color they are and that they occupy these "points" in the grid. "Board" keeps understanding of all the pieces in the grid, which Pieces themselves call upon and update.

    Point of the whole sermon, is... do always try to avoid, as much as possible, needing to place actual progman logic in graphics painting. Do your utmost to keep the painting part as simple as you can.

    And as to the color, both line color and fill color, they should be properties of the piece. Upon painting the board would simply ask for them, then fill / outline the parts of the grid they Piece occupies. Collision detection and all such are simple to handle outside pixel swetting paint.

    http://java.sun.com/products/jfc/tsc...ing/index.html

  6. #6
    Join Date
    Feb 2008
    Posts
    966

    Re: Overriding color of drawn rectangle: how?

    Just out of curiosity: is this for a class? Is the instructor named Andree? I am not asking because I'm gonna tell, you are allowed to ask the questions you have, I just personally know this guy and he does a tetris game every other year as an assignment

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured