Creating the Tetris shapes in Java as classes extending Area or Shape -


i working on writing tetris game myself using java. not using tutorials containing code, because want try decompose myself , see how far can take it.

so far, not good. have stumbled upon creation on shapes. idea either: 1. have each basic shape (like l, cube, boat) separate classes extending or implementing area or shape can use argument g2.fill(lshape). 2. each class have sort of state variable describing rotation position, that's next challenge, figuring out rotation..

so step 1, have written far following drafts of 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 shape's methods i'm not overriding cause don't know how 

in main class calling in paint() method: g2.fill(lshape); // lshape lshape object; , trouble exception thrown 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 case when call g2.fill(lshape) there no exception , shape drawn, don't know how move it. parts of area brick objects rectangles, try access setlocation method on each brick in area, don't know how access it.

so guess need either figure out how make shape implementation of tetris shape not throw exceptions, meaning implementing required methods , showing on jpanel..and worry rotation. or figure out how make area extension of tetris shape move around.

thanks,

when rendering shapes, can use graphics#translate or affinetransform on graphics2d instance or use shape#getpathiterator(affinetransform), takes more work need wrap shape

for example...

// call me lazy, preserves state of current graphics // context , makes easy "restore" it, disposing // of copy... graphics2d g2d = (graphics2d)g.create(); g2d.translate(x, y); g2d.fill(shape); // restores state of `graphics` object... g2d.dispose(); 

if want continue using area, take @ area#createtransformedarea should allow use affinetransform transform area, returns area making easier use shape#getpathiterator.

it means can generate compound transformation (rotate, translate, etc) , generate area represents transformation...


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -