Jump to content
  • Advertisement
Sign in to follow this  
dav0012

jigsaw puzzle with java graphics2D

This topic is 2009 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hello all, i am a noob who is programming a jigsaw puzzle game, am pretty much done with piece creation (which took me hell of time) and clipping its associate piece of image by using paint, and paint context.

 

i created the piece using bezier technique (generalPath), used it as clip then i filled it with the image as a paint type.

 

now i dont knw how to proceed to select the drawn piece. (the graphic object)

can some1 please give am idea.

image.png  image.png

 

my codes are as follows :

 

all the piece are stored into an array : JigsawPiece jpArray[nColumn ][nRow ]

my piece class

public class JigsawPiece {

int pID;
int[] arrayLocation= new int[2];
int topTab=0;
int rightTab=0 ;
int bottomTab=0;
int leftTab=0;
int tileWidth= 50;
int tileHeight= 100;
double wTileRatio= (double)tileWidth/100;
double hTileRatio= (double)tileHeight/100;
Point p1 = new Point();
Point p2 = new Point();
Point p3 = new Point();
Point pieceLocation = new Point(0,0);
Point pieceBoundOffset = new Point(0,0);
Point pieceBoundCoordinates = new Point(0,0);
Rectangle pieceBound ;
BufferedImage img=null;
JigsawPaint jPaint;
GeneralPath jPiece= new GeneralPath();
JigsawPiece northPiece = null;
JigsawPiece eastPiece = null;
JigsawPiece southPiece = null;
JigsawPiece westPiece = null;
}

/*
SKIPPING THE SETTER AND GETTER METHODS to avoid filling the screen for nothing.... they wrk totally fine ;)
*/

JigsawPaint class: 


public class JigsawPaint implements Paint, PaintContext{

	protected BufferedImage bImg ;
	protected Rectangle recClip ;
	protected Rectangle userBounds;
	protected Point initialBound;
	public JigsawPaint(){
	
	}//close public declaration
	
	public JigsawPaint(BufferedImage img, Rectangle r, Point p){
		bImg = null;
		recClip = new Rectangle();
		userBounds = new Rectangle();
		bImg = img;
		recClip.setRect(r);
		initialBound = new Point(p);
	}//close public JigsawPaint with arguments 
	public void setRecClip (Rectangle r){
		
		recClip.setRect(r);
	}//close setRecClop
	
public ColorModel getColorModel(){
	
	return bImg.getColorModel();
	
}//close ColorModel

public PaintContext createContext(ColorModel colormodel, Rectangle rectangle, Rectangle2D rectangle2d, AffineTransform affinetransform, RenderingHints renderinghints)
{
    userBounds.setRect(rectangle);
    return this;
}

public int getTransparency()
{
    return 1;
}
public Raster getRaster(int i , int j, int k , int l){
	
	int i1 = (i - userBounds.x) + initialBound.x;
    int j1 = (j - userBounds.y) + initialBound.y;
	
    return bImg.getRaster().createChild(i1, j1, k, l, 0, 0, null);
    
    
}//close getRaster

my paintPuzzle method found in my jPanel :
 

public void paintPuzzles(Graphics2D g2){
		
	g2.setStroke(new BasicStroke(2F));
	System.out.println("PUzzle painting ...");
	if (jpArray!=null){
		int count=0;
		for(int i =0;i<nRow;i++){
			
			
			for(int j =0;j<nColumn;j++){
				count++;
				System.out.print(count+" - ");
				System.out.println(jpArray[i][j].pieceLocation);
				g2.translate(jpArray[i][j].pieceLocation.getX(), jpArray[i][j].pieceLocation.getY());
				g2.setPaint(Color.white);
				
				g2.draw(jpArray[i][j].jPiece);
				g2.setPaint(jpArray[i][j].jPaint);
				g2.fill(jpArray[i][j].jPiece);
				
				
				g2.translate(-jpArray[i][j].pieceLocation.getX(), -jpArray[i][j].pieceLocation.getY());
				
				
			
			}//close j loop
		}//close i loop
	}//close if statement

	}//close paintPuzzle

Guys plz help, how can i select these graphic objects ?? 
google's result is pretty confusing

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!