課程
/移動開發
/Android
/Android-五子連珠
aaaaaa
2016-12-14
源自:Android-五子連珠
正在回答
private void drawPieces(Canvas canvas) {
for(int i=0,n=mWhiteArray.size();i<n;i++)
{
Point whitePoint = mWhiteArray.get(i);
canvas.drawBitmap(mWhitePiece,
(whitePoint.x+(1-ratioPieceOfLineHeight)/2)*mLineHeight,
(whitePoint.y+(1-ratioPieceOfLineHeight)/2)*mLineHeight,null);
}
for(int i=0,n=mBlackArray.size();i<n;i++)
Point blackPoint = mWhiteArray.get(i);????????//這里應該是?Point blackPoint = mBlackArray.get(i);?????
canvas.drawBitmap(mBlackPiece,????????????????
(blackPoint.x+(1-ratioPieceOfLineHeight)/2)*mLineHeight,
(blackPoint.y+(1-ratioPieceOfLineHeight)/2)*mLineHeight,null);
大神們幫忙看看哪里錯了?
package com.example.wuziqipanel;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class WuziqiPanel extends View {
private int mPanelWidth;
private float mLineHeight;
private int MAX_LINE=10;
private Paint mPaint = new Paint();
private Bitmap mWhitePiece;
private Bitmap mBlackPiece;
private float ratioPieceOfLineHeight = 3*1.0f/4;
//白棋先手,當前輪到白棋
private boolean mIsWhite = true;
private List<Point> mWhiteArray = new ArrayList<Point>();
private List<Point> mBlackArray = new ArrayList<Point>();
private boolean mIsGameOver;
private boolean mIsWhiteWinner;
public WuziqiPanel(Context context, AttributeSet attrs) {
super(context, attrs);
setBackgroundColor(0x44ff0000);
init();
private void init() {
// TODO Auto-generated method stub
mPaint.setColor(0x88000000);
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setStyle(Paint.Style.STROKE);
mWhitePiece = BitmapFactory.decodeResource(getResources(), R.drawable.stone_w2);
mBlackPiece = BitmapFactory.decodeResource(getResources(), R.drawable.stone_b1);
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthSize =MeasureSpec.getSize(widthMeasureSpec);
int widthMode =MeasureSpec.getSize(widthMeasureSpec);
int heidthSize =MeasureSpec.getSize(heightMeasureSpec);
int heidthMode =MeasureSpec.getSize(heightMeasureSpec);
int width = Math.min(widthSize,heidthSize);
if(widthMode == MeasureSpec.UNSPECIFIED)
width = heidthSize;
}else if(heidthMode == MeasureSpec.UNSPECIFIED)
width = widthSize;
setMeasuredDimension(width, width);
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mPanelWidth = w;
mLineHeight = mPanelWidth *1.0f/MAX_LINE;
int pieceWidth=(int) (mLineHeight*ratioPieceOfLineHeight);
mWhitePiece = Bitmap.createScaledBitmap(mWhitePiece, pieceWidth, pieceWidth, false);
mBlackPiece = Bitmap.createScaledBitmap(mBlackPiece, pieceWidth, pieceWidth, false);
public boolean onTouchEvent(MotionEvent event) {
int action=event.getAction();
if(action==MotionEvent.ACTION_UP)
int x = (int) event.getX();
int y = (int) event.getY();
Point p = getValidPoint(x,y);
if(mWhiteArray.contains(p) || ?mBlackArray.contains(p))
return false;
if(mIsWhite){
mWhiteArray.add(p);
}else
mBlackArray.add(p);
invalidate();
mIsWhite = !mIsWhite;
return true;
private Point getValidPoint(int x, int y) {
return new Point((int)(x / mLineHeight),(int)(y / mLineHeight));
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawBoard(canvas);
drawPieces(canvas);
Point blackPoint = mWhiteArray.get(i);
canvas.drawBitmap(mBlackPiece,
private void drawBoard(Canvas canvas) {
int w = mPanelWidth;
float lineHeight = mLineHeight;
for(int i =0 ; i<MAX_LINE ; i++){
int startX = (int) (lineHeight/2);
int endX = (int) (w-lineHeight/2);
int y =(int) ((0.5+i)*lineHeight);
canvas.drawLine(startX, y, endX, y, mPaint);
canvas.drawLine(y, startX, y, endX, mPaint);
舉報
Android游戲開發-五子連珠,本教程通過UI與邏輯實現雙人對戰
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2017-02-11
private void drawPieces(Canvas canvas) {
for(int i=0,n=mWhiteArray.size();i<n;i++)
{
Point whitePoint = mWhiteArray.get(i);
canvas.drawBitmap(mWhitePiece,
(whitePoint.x+(1-ratioPieceOfLineHeight)/2)*mLineHeight,
(whitePoint.y+(1-ratioPieceOfLineHeight)/2)*mLineHeight,null);
}
for(int i=0,n=mBlackArray.size();i<n;i++)
{
Point blackPoint = mWhiteArray.get(i);????????//這里應該是?Point blackPoint = mBlackArray.get(i);?????
canvas.drawBitmap(mBlackPiece,????????????????
(blackPoint.x+(1-ratioPieceOfLineHeight)/2)*mLineHeight,
(blackPoint.y+(1-ratioPieceOfLineHeight)/2)*mLineHeight,null);
}
}
2016-12-14
大神們幫忙看看哪里錯了?
2016-12-14
package com.example.wuziqipanel;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class WuziqiPanel extends View {
private int mPanelWidth;
private float mLineHeight;
private int MAX_LINE=10;
private Paint mPaint = new Paint();
private Bitmap mWhitePiece;
private Bitmap mBlackPiece;
private float ratioPieceOfLineHeight = 3*1.0f/4;
//白棋先手,當前輪到白棋
private boolean mIsWhite = true;
private List<Point> mWhiteArray = new ArrayList<Point>();
private List<Point> mBlackArray = new ArrayList<Point>();
private boolean mIsGameOver;
private boolean mIsWhiteWinner;
public WuziqiPanel(Context context, AttributeSet attrs) {
super(context, attrs);
setBackgroundColor(0x44ff0000);
init();
}
private void init() {
// TODO Auto-generated method stub
mPaint.setColor(0x88000000);
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setStyle(Paint.Style.STROKE);
mWhitePiece = BitmapFactory.decodeResource(getResources(), R.drawable.stone_w2);
mBlackPiece = BitmapFactory.decodeResource(getResources(), R.drawable.stone_b1);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthSize =MeasureSpec.getSize(widthMeasureSpec);
int widthMode =MeasureSpec.getSize(widthMeasureSpec);
int heidthSize =MeasureSpec.getSize(heightMeasureSpec);
int heidthMode =MeasureSpec.getSize(heightMeasureSpec);
int width = Math.min(widthSize,heidthSize);
if(widthMode == MeasureSpec.UNSPECIFIED)
{
width = heidthSize;
}else if(heidthMode == MeasureSpec.UNSPECIFIED)
{
width = widthSize;
}
setMeasuredDimension(width, width);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
// TODO Auto-generated method stub
super.onSizeChanged(w, h, oldw, oldh);
mPanelWidth = w;
mLineHeight = mPanelWidth *1.0f/MAX_LINE;
int pieceWidth=(int) (mLineHeight*ratioPieceOfLineHeight);
mWhitePiece = Bitmap.createScaledBitmap(mWhitePiece, pieceWidth, pieceWidth, false);
mBlackPiece = Bitmap.createScaledBitmap(mBlackPiece, pieceWidth, pieceWidth, false);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action=event.getAction();
if(action==MotionEvent.ACTION_UP)
{
int x = (int) event.getX();
int y = (int) event.getY();
Point p = getValidPoint(x,y);
if(mWhiteArray.contains(p) || ?mBlackArray.contains(p))
{
return false;
}
if(mIsWhite){
mWhiteArray.add(p);
}else
{
mBlackArray.add(p);
}
invalidate();
mIsWhite = !mIsWhite;
}
return true;
}
private Point getValidPoint(int x, int y) {
return new Point((int)(x / mLineHeight),(int)(y / mLineHeight));
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
drawBoard(canvas);
drawPieces(canvas);
}
private void drawPieces(Canvas canvas) {
for(int i=0,n=mWhiteArray.size();i<n;i++)
{
Point whitePoint = mWhiteArray.get(i);
canvas.drawBitmap(mWhitePiece,
(whitePoint.x+(1-ratioPieceOfLineHeight)/2)*mLineHeight,
(whitePoint.y+(1-ratioPieceOfLineHeight)/2)*mLineHeight,null);
}
for(int i=0,n=mBlackArray.size();i<n;i++)
{
Point blackPoint = mWhiteArray.get(i);
canvas.drawBitmap(mBlackPiece,
(blackPoint.x+(1-ratioPieceOfLineHeight)/2)*mLineHeight,
(blackPoint.y+(1-ratioPieceOfLineHeight)/2)*mLineHeight,null);
}
}
private void drawBoard(Canvas canvas) {
// TODO Auto-generated method stub
int w = mPanelWidth;
float lineHeight = mLineHeight;
for(int i =0 ; i<MAX_LINE ; i++){
int startX = (int) (lineHeight/2);
int endX = (int) (w-lineHeight/2);
int y =(int) ((0.5+i)*lineHeight);
canvas.drawLine(startX, y, endX, y, mPaint);
canvas.drawLine(y, startX, y, endX, mPaint);
}
}
}