Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create jigsaw puzzle pieces without using mask?

I am trying to create a jigsaw puzzle game, and I would like to know of alternative ways of creating puzzle pieces without using mask. Currently I have jigsaw pieces by taking a full image, breaking that image up into four pieces (lets say the puzzle is 2x2) and then storing and applying a mask to each piece. It looks like the below

    // create standard puzzle pieces
    arryPieceEndPos = new int[mCols][mRows];
    arryPieceImg = new Bitmap[mCols * mRows];
    arryIsPieceLocked = new boolean[mCols * mRows];

    int pos = 0;
    for (int c = 0; c < mCols; c++) {
        for (int r = 0; r < mRows; r++) {
            arryPieceImg[pos] = Bitmap.createBitmap(mBitmap,
            c * mPieceWidth, r * mPieceHeight,
            mPieceWidth, mPieceHeight);

            arryIsPieceLocked[pos] = false;
            arryPieceEndPos[c][r] = pos;
            pos++;
        }
    }

I then use a helper method to apply a mask to each piece

private Bitmap maskMethod(Bitmap bmpOriginal, Bitmap bmpMask) {

    // adjust mask bitmap if size is not the size of the puzzle piece
    if (bmpMask.getHeight() != mPieceHeight ||
        bmpMask.getWidth() != mPieceWidth) {
        Log.e("TEST", "Resize Error :: H (mask): " + bmpMask.getHeight() + " // W (mask): " +
            bmpMask.getWidth());
        Log.d("TEST", "Resize Error :: H (norm): " + mPieceHeight + " // W (norm): " +
            mPieceWidth);

    }

    Canvas canvas = new Canvas();
    Bitmap combine = Bitmap.createBitmap(bmpOriginal.getWidth(), bmpOriginal.getHeight(), Bitmap.Config.ARGB_8888);
    canvas.setBitmap(combine);
    Paint paint = new Paint();
    paint.setFilterBitmap(false);

    canvas.drawBitmap(bmpOriginal, 0, 0, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
    canvas.drawBitmap(bmpMask, 0, 0, paint);
    paint.setXfermode(null);

    return combine;
}

I saw this post > http://java.dzone.com/news/connect-pictures-android for connecting pieces together, however, this does not go over generating pieces programmatically without masks. Can anyone provide code examples of how this can be accomplished? The only clue I have is that I should be using Path, however, I am still not sure how. Thanks in advance!

like image 905
portfoliobuilder Avatar asked Mar 13 '15 18:03

portfoliobuilder


1 Answers

A puzzle piece is a pretty complex view to create, but I can help you understand how to use path. Here is the link to the developer website: http://developer.android.com/reference/android/graphics/Path.html

Look into this link. I made a small thing for you to start. The one thing you need to figure out is how to cut a small circle out of the path, which I wouldn't know. I think you have to look into clipping to have your path follow a circle (you could also do clipping for creating the circle outside the piece, I just haven't done clipping before).

private Bitmap getPuzzleBitmap(Bitmap bitmap)
{
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

    calculatePuzzlePath(bitmap.getWidth(), bitmap.getHeight());

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawPath(puzzlePath, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;
}

private void calculatePuzzlePath(int width, int height)
{
    float radius = (height / 2) - 5;
    float smallRadius = radius / 3;
    radius -= smallRadius * 2;
    float centerX = width/2;
    float centerY = height/2;
    puzzlePath = new Path();
    // Bottom right
    puzzlePath.moveTo(centerX + radius, centerY + radius);
    // Top right
    puzzlePath.lineTo(centerX + radius, centerY - radius);
    // Center top
    puzzlePath.lineTo(centerX, centerY - radius);
    // Add outside circle to center top
    puzzlePath.addCircle(centerX, centerY - radius - ((radius / 3) / 2), radius / 3, Path.Direction.CCW);

    // Top left
    puzzlePath.lineTo(centerX - radius, centerY - radius);
    // Bottom left
    puzzlePath.lineTo(centerX - radius, centerY + radius);
    //Bottom right
    puzzlePath.lineTo(centerX + radius, centerY + radius);
}

I hope this is sufficient to get started with this.

Good luck!

like image 179
Kevin van Mierlo Avatar answered Dec 17 '22 00:12

Kevin van Mierlo