Javaでパズルアプリを本を見て写経しました。
長いのでコピペします。
なのでインデントはめちゃくちゃですが...
そしてまだ途中なのであしからず...
Project Name:ImagePuzzle
ImagePuzzleActivity.java
package jp.rutles.puzzle;
import android.app.Activity;
import android.os.Bundle;
import android.view.*;
public class ImagePuzzleActivity extends Activity {
public float disp_w,disp_h;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
Window window = getWindow();
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
WindowManager manager = window.getWindowManager();
Display disp = manager.getDefaultDisplay();
disp_w = disp.getWidth();
disp_h = disp.getHeight();
setContentView(R.layout.main);
}
}
PuzzleBoard.java
package jp.rutles.puzzle;
import java.util.*;
import android.graphics.*;
public class PuzzleBoard {
public static final int CENTER = 0;
public static final int NORTH = 1;
public static final int SOUTH = 2;
public static final int EAST = 3;
public static final int WEST = 4;
private Bitmap image;
private float x,y;
private int[] data;
public int place;
private static final int row = 6;
private static final int col = 4;
private float pW = 100f;
private float pH = 100f;
public int count = 0;
public PuzzleBoard(float x, float y, float dw, float dh, Bitmap image) {
super();
this.x = x;
this.y = y;
pW *= dw;
pH *= dh;
this.image = image;
data = new int[]{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,-1};
place = 23;
}
public void init() {
for (int i = 0;i < 200;i++){
Random r = new Random(new Date().getTime());
int a = r.nextInt(data.length);
int b = r.nextInt(data.length);
int val = data[a];
data[a] = data[b];
data[b]
= val;
}
for (int i = 0;i < data.length;i++)
if (data[i] == -1) place = i;
count = 0;
}
public void draw(Canvas canvas){
int n = 0;
for (int i = 0;i < row;i++){
for (int j = 0;j <col;j++){
int c = data[n] % col;
int r = (int)(data[n] / col);
if (data[n] != -1){
canvas.drawBitmap(image,
new Rect(x + c * pW , y + r * pH, x + c * pW +pW, y + r * pH + pH),
new Rect(x + j * pW, y + i * pH, x + j * pW + pW,y + i * pH + pH),
new Paint());
}
n++;
}
}
}
public void move(int move){
int c =place % col;
int r = (int)(place / col);
int c2 = c;
int r2 = r;
switch(move){
case NORTH:
if (r2 < row - 1) r2++;
break;
case SOUTH:
if (r2 > 0) r2--;
break;
case WEST:
if (c2 < col - 1) c2++;
break;
case EAST:
if (c2 > 0) c2--;
break;
}
int n = data[r * col + c];
data[r * col + c] = data[r2 *col + c2];
data[r2 * col + c2] = n;
for (int i = 0;i <data.length;i++)
if (data[i] == -1) place = i;
count++;
}
public boolean checkFinish(){
boolean flg = true;
for (int i = 0;i < data.length - 1;i++)
if (data[i] != i) flg = false;
return flg;
}
}
PuzzleView.java
package jp.rutles.puzzle;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.*;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.*;
import android.widget.Toast;
public class PuzzleView extends View {
private static final int btn_x = 50; // ボタンの横位置
private static final int btn_y = 768; // ボタンの縦位置
private static final int btn_w = 390; // ボタンの横幅
private static final int btn_h = 40; // ボタンの縦幅
private static final int board_x = 40; // ゲーム版の横位置
private static final int board_y = 126; // ゲーム版の縦位置
private static final int score_x = 60; // テキストの表示横位置
private static final int score_y = 73; // テキストの表示縦位置
private ImagePuzzleActivity puzzle; // Puzzleクラス
private PuzzleBoard board; // PuzzleBoardクラス
private Drawable back,btn1,btn2; // 使用するイメージ
private booleanbtn_down,isPlaying; // ボタンの状態、プレイ中の状態
private int pressX,pressY,upX,upY; // ボタンを押した時、話した時の位置
public PuzzleView(Context context) {
super(context);
init(context);
}
public PuzzleView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context){
puzzle = (ImagePuzzleActivity)context;
Resources resources = context.getResources();
back = resources.getDrawable(R.drawable.back);
btn1 = resources.getDrawable(R.drawable.start);
btn1.setBounds(btn_x, btn_y, btn_x + btn_w, btn_y + btn_h);
btn2 = resources.getDrawable(R.drawable.start2);
btn2.setBounds(btn_x, btn_y, btn_x + btn_w, btn_y + btn_h);
Bitmap img = BitmapFactory.decodeResource(resources, R.drawable.image1);
board = new PuzzleBoard(board_x, board_y, img);
btn_down = false;
isPlaying = false;
}
@Override
protected void onDraw(Canvas c) {
c.drawColor(Color.BLACK);
int w = this.getWidth();
int h = this.getHeight();
back.setBounds(0, 0, w, h);
back.draw(c);
board.draw(c);
if (btn_down){
btn2.draw(c);
} else {
btn1.draw(c);
}
Paint p = new Paint();
p.setTextSize(30f);
c.drawText("count: " + board.count, score_x, score_y, p);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
int x = (int)event.getX();
int y = (int)event.getY();
switch(action){
case MotionEvent.ACTION_DOWN:
pressX = x;
pressY = y;
if (isIn(x,y,btn1.getBounds())){
btn_down = true;
isPlaying = true;
board.init();
Toast toast = Toast.makeText(puzzle, "スタート!", Toast.LENGTH_LONG);
toast.show();
}
break;
case MotionEvent.ACTION_UP:
btn_down = false;
upX = x;
upY = y;
if (isPlaying) checkMove();
break;
}
invalidate();
return true;
}
public boolean isIn(int x, int y,Rect rect){
return x > rect.left && x < rect.right && y > rect.top && y <rect.bottom;
}
public void checkMove(){
int dx = upX - pressX;
int dy = upY - pressY;
if (dx < -100) board.move(PuzzleBoard.WEST);
if (dx > 100) board.move(PuzzleBoard.EAST);
if (dy < -100) board.move(PuzzleBoard.NORTH);
if (dy > 100) board.move(PuzzleBoard.SOUTH);
isPlaying = false;
Toast toast = Toast.makeText(puzzle, "おめでとう!", Toast.LENGTH_LONG);
toast.show();
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.donroid.com/apk/res/jp.rutles.puzzle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<jp.rutles.puzzle.PuzzleView
android:id="@+id/PuzzleView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</FrameLayout>
うん、全然理解はしてません...;
まだ写経も全部終わってません!;