0

I am working on writing the Tetris game myself using Java. I am not using any tutorials already containing the code, because I want to try to decompose it myself and see how far I can take it.

So far, not so good. I have stumbled upon the creation on the Shapes. My idea would be to either: 1. have each basic shape (like the L, the cube, the boat) as separate classes extending or implementing Area or Shape so that I can use it as an argument for g2.fill(LShape). 2. each class would have some sort of state variable describing the rotation position, but that's next challenge, figuring out the rotation..

So for Step 1, I have written so far the following drafts of the LShape class:

Draft a):

public class LShape implements Shape{
private Rectangle[][] poc;
private int rotationState;  

public LShape() {
    rotationState = 0;
    poc = new Rectangle[3][3];
    poc[0][0] = new Brick(INITIAL_X - BRICK, INITIAL_Y, BRICK); 
    poc[1][0] = new Brick(INITIAL_X - BRICK, INITIAL_Y + BRICK, BRICK);
    poc[2][0] = new Brick(INITIAL_X - BRICK, INITIAL_Y + 2 * BRICK, BRICK);
    poc[2][1] = new Brick(INITIAL_X, INITIAL_Y + 2 * BRICK, BRICK);
}      
}
//.....all the Shape's methods which I'm not overriding cause I don't know how

In the main class I am calling in the paint() method: g2.fill(lShape); // where lShape is a LShape object; And the trouble is an exception is thrown about the getPathIterator()

OR Draft b):

public class LShape extends Area{

public LShape () {

    add(new Area(new Brick(INITIAL_X - BRICK, INITIAL_Y, BRICK)));
    exclusiveOr(new Area(new Brick(INITIAL_X - BRICK, INITIAL_Y + BRICK, BRICK)));
    exclusiveOr(new Area(new Brick(INITIAL_X - BRICK, INITIAL_Y + 2 * BRICK, BRICK)));
    exclusiveOr(new Area(new Brick(INITIAL_X, INITIAL_Y + 2 * BRICK, BRICK)));
}
}

In this case when I call the g2.fill(lShape) there is no exception and the Shape is drawn, only that I don't know how to move it. Parts of the Area are the Brick objects which are Rectangles, so I could try to access the setLocation method on each Brick in the Area, but I don't know how to access it.

So I guess I need help either to figure out how to make the Shape implementation of a Tetris Shape not to throw the exceptions, meaning implementing all the required methods and actually showing up on the JPanel..and then I will worry about the rotation. OR figure out how to make the Area extension of a Tetris Shape to move around.

Thanks,

iulikia
  • 115
  • 1
  • 6

1 Answers1

1

When rendering shapes, you can to use Graphics#translate or AffineTransform on a Graphics2D instance or use Shape#getPathIterator(AffineTransform), but that takes more work as you need to wrap back into a shape

For example...

// Call me lazy, this preserves the state of the current Graphics
// context and makes it easy to "restore" it, simply by disposing
// of this copy...
Graphics2D g2d = (Graphics2D)g.create();
g2d.translate(x, y);
g2d.fill(shape);
// Restores the state of the `Graphics` object...
g2d.dispose();

If you want to continue using Area, take a look at Area#createTransformedArea which should allow you to use an AffineTransform to transform the Area, but it returns an Area making it significantly easier to use then Shape#getPathIterator.

It also means you can generate a compound transformation (rotate, translate, etc) and generate a Area that represents the transformation...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Ok, so I guess I will go with the Area extension of a Tetris Shape and rely on the Graphics#translate. I gave it a go and it did move it. I will play some more and experiment, but I should be good. Thanks a lot – iulikia Feb 06 '14 at 05:23