Java 如何使圆圈可点击
我正在用画布开发游戏。我已经用loop画出了圆的网格。我想得到圆的x,y坐标。为此,我必须使这些圆圈可以点击,这样每当玩家点击一个圆圈时,它就会返回它的坐标。我将把这些坐标传递给lineDraw()方法,以便在这些圆之间绘制直线。 此循环用于定义圆的栅格:Java 如何使圆圈可点击,java,android,android-canvas,actionlistener,Java,Android,Android Canvas,Actionlistener,我正在用画布开发游戏。我已经用loop画出了圆的网格。我想得到圆的x,y坐标。为此,我必须使这些圆圈可以点击,这样每当玩家点击一个圆圈时,它就会返回它的坐标。我将把这些坐标传递给lineDraw()方法,以便在这些圆之间绘制直线。 此循环用于定义圆的栅格: for (int y=0;y<rows;y++) { for (int x=0;x<cols;x++) { canvas.drawCir
for (int y=0;y<rows;y++)
{
for (int x=0;x<cols;x++)
{
canvas.drawCircle((x + 1) * dw, (y + 1) *(3* dh), 20, pDot);
}
}
for(int y=0;y将圆的数据(例如位置和半径)存储在列表中,并检查每个圆是否存在鼠标碰撞(注册鼠标事件列表器)
公共类圈子
{
私有整数半径;
私人int positionX;
私人内部职位;
公共圆圈(内部位置X、内部位置Y、内部半径)
{
这个半径=半径;
此位置x=位置x;
这个位置y=位置y;
}
public int getPositionX()
{
返回此.x;
}
公共int getPositionY()
{
返回此.positionY;
}
公共布尔包含(int-posX,int-posY)
{
int distanceX=this.positionX-posX;
int distanceY=this.positionY-posY;
返回Math.sqrt((distanceX*distanceX)+(distanceY*distanceY))在你之前的问题之后,我玩了一点这个项目。我确信这不是最优化的方法,但在不知道你计划如何实现游戏逻辑的情况下,我认为这是足够灵活的,可以适应。下面是你的自定义视图类,我已将其重命名为Board
,以与Java命名修道院保持一致离子
public class Board extends View
{
Paint pBack = new Paint();
Paint pDot = new Paint();
Paint pLine = new Paint();
int cols = 5;
int rows = 6;
// Default initialization = false
boolean[][] dots = new boolean[cols][rows];
int canWidth = 0;
int canHeight = 0;
float xStep = 0;
float yStep = 0;
float[] xCoords = new float[cols];
float[] yCoords = new float[rows];
public Board(Context context)
{
super(context);
pBack.setARGB(255, 255, 102, 0);
pDot.setARGB(255, 255, 255, 255);
pLine.setStrokeWidth(5);
pLine.setARGB(255, 90, 10, 0);
}
public void setDots(boolean[][] dotSelected)
{
this.dots = dotSelected;
}
public boolean[][] getDots()
{
return dots;
}
protected void onSizeChanged(int w, int h, int oldw, int oldh)
{
canWidth = w;
canHeight = h;
xStep = canWidth / (cols + 1);
yStep = canHeight / (rows + 1);
for (int y = 0; y < rows; y++)
{
yCoords[y] = (y + 1) * yStep;
}
for (int x = 0; x < cols; x++)
{
xCoords[x] = (x + 1) * xStep;
}
}
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
canvas.drawPaint(pBack);
// Grid, lines and box markings
for (int y = 0; y < rows; y++)
{
canvas.drawLine(xStep, yCoords[y], cols * xStep, yCoords[y], pDot);
for (int x = 0; x < cols; x++)
{
if (y == 0)
{
canvas.drawLine(xCoords[x], yStep, xCoords[x], rows * yStep, pDot);
}
if (dots[x][y])
{
boolean left = x > 0 && dots[x - 1][y];
boolean up = y > 0 && dots[x][y - 1];
if (left)
{
canvas.drawLine(xCoords[x], yCoords[y], xCoords[x - 1], yCoords[y], pLine);
}
if (up)
{
canvas.drawLine(xCoords[x], yCoords[y], xCoords[x], yCoords[y - 1], pLine);
}
if (left && up && dots[x - 1][y - 1])
{
canvas.drawCircle(xCoords[x] - xStep / 2, yCoords[y] - yStep / 2, 10, pLine);
}
}
}
}
// Dots
for (int y = 0; y < rows; y++)
{
for (int x = 0; x < cols; x++)
{
canvas.drawCircle(xCoords[x], yCoords[y], 20, pDot);
if (dots[x][y])
{
canvas.drawCircle(xCoords[x], yCoords[y], 15, pBack);
}
}
}
}
public boolean onTouchEvent(MotionEvent event)
{
super.onTouchEvent(event);
if (event.getAction() != MotionEvent.ACTION_DOWN)
return true;
int xNear = 0;
int yNear = 0;
float xMin = canWidth;
float yMin = canHeight;
for (int x = 0; x < cols; x++)
{
if (Math.abs(xCoords[x] - event.getX()) < xMin)
{
xMin = Math.abs(xCoords[x] - event.getX());
xNear = x;
}
}
for (int y = 0; y < rows; y++)
{
if (Math.abs(yCoords[y] - event.getY()) < yMin)
{
yMin = Math.abs(yCoords[y] - event.getY());
yNear = y;
}
}
dots[xNear][yNear] = !dots[xNear][yNear];
invalidate();
return true;
}
}
公共类板扩展视图
{
Paint pBack=新油漆();
油漆pDot=新油漆();
油漆线=新油漆();
int cols=5;
int行=6;
//默认初始化=false
布尔[][]点=新布尔[cols][行];
int canWidth=0;
int canHeight=0;
浮点xStep=0;
float-yStep=0;
float[]xCoords=新的float[cols];
float[]yCoords=新的float[行];
公共董事会(背景)
{
超级(上下文);
pBack.setARGB(255、255、102、0);
pDot.setARGB(255、255、255、255);
基准线设定行程宽度(5);
pLine.setARGB(255,90,10,0);
}
公共无效设置点(布尔值[][]点选定)
{
this.dots=dotSelected;
}
公共布尔值[][]getDots()
{
返回点;
}
已更改尺寸的受保护空心(整数w、整数h、整数oldw、整数oldh)
{
宽度=w;
canHeight=h;
xStep=canWidth/(cols+1);
yStep=canHeight/(行数+1);
对于(int y=0;y0&&dots[x-1][y];
布尔向上=y>0&&dots[x][y-1];
如果(左)
{
画布绘制线(xCoords[x],yCoords[y],xCoords[x-1],yCoords[y],pLine);
}
如果(向上)
{
画布.抽绳(xCoords[x],yCoords[y],xCoords[x],yCoords[y-1],pLine);
}
if(左、上、点[x-1][y-1])
{
画布画圈(xCoords[x]-xStep/2,yCoords[y]-yStep/2,10,pLine);
}
}
}
}
//圆点
对于(int y=0;y
如果我没有弄错,希望这个链接能给你一些想法。这个方法将检测运动事件,并返回x,y。虽然我已经用fix x和y绘制了圆,但我想让这些圆可以点击。我不想移动这些圆。我唯一想要的是有x和y坐标。我会将这些坐标传递给直线绘制方法画两个圆圈之间的线。我以前问过这样的问题,为了更好地理解代码,你可以查看此链接。你能从游戏商店下载此游戏吗。它会让你清楚我的游戏想法。我正在复制此想法。我测试了你的代码。它允许对角线选择。这是不允许的。它应该只允许两个圆圈一次画一条线
public class Board extends View
{
Paint pBack = new Paint();
Paint pDot = new Paint();
Paint pLine = new Paint();
int cols = 5;
int rows = 6;
// Default initialization = false
boolean[][] dots = new boolean[cols][rows];
int canWidth = 0;
int canHeight = 0;
float xStep = 0;
float yStep = 0;
float[] xCoords = new float[cols];
float[] yCoords = new float[rows];
public Board(Context context)
{
super(context);
pBack.setARGB(255, 255, 102, 0);
pDot.setARGB(255, 255, 255, 255);
pLine.setStrokeWidth(5);
pLine.setARGB(255, 90, 10, 0);
}
public void setDots(boolean[][] dotSelected)
{
this.dots = dotSelected;
}
public boolean[][] getDots()
{
return dots;
}
protected void onSizeChanged(int w, int h, int oldw, int oldh)
{
canWidth = w;
canHeight = h;
xStep = canWidth / (cols + 1);
yStep = canHeight / (rows + 1);
for (int y = 0; y < rows; y++)
{
yCoords[y] = (y + 1) * yStep;
}
for (int x = 0; x < cols; x++)
{
xCoords[x] = (x + 1) * xStep;
}
}
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
canvas.drawPaint(pBack);
// Grid, lines and box markings
for (int y = 0; y < rows; y++)
{
canvas.drawLine(xStep, yCoords[y], cols * xStep, yCoords[y], pDot);
for (int x = 0; x < cols; x++)
{
if (y == 0)
{
canvas.drawLine(xCoords[x], yStep, xCoords[x], rows * yStep, pDot);
}
if (dots[x][y])
{
boolean left = x > 0 && dots[x - 1][y];
boolean up = y > 0 && dots[x][y - 1];
if (left)
{
canvas.drawLine(xCoords[x], yCoords[y], xCoords[x - 1], yCoords[y], pLine);
}
if (up)
{
canvas.drawLine(xCoords[x], yCoords[y], xCoords[x], yCoords[y - 1], pLine);
}
if (left && up && dots[x - 1][y - 1])
{
canvas.drawCircle(xCoords[x] - xStep / 2, yCoords[y] - yStep / 2, 10, pLine);
}
}
}
}
// Dots
for (int y = 0; y < rows; y++)
{
for (int x = 0; x < cols; x++)
{
canvas.drawCircle(xCoords[x], yCoords[y], 20, pDot);
if (dots[x][y])
{
canvas.drawCircle(xCoords[x], yCoords[y], 15, pBack);
}
}
}
}
public boolean onTouchEvent(MotionEvent event)
{
super.onTouchEvent(event);
if (event.getAction() != MotionEvent.ACTION_DOWN)
return true;
int xNear = 0;
int yNear = 0;
float xMin = canWidth;
float yMin = canHeight;
for (int x = 0; x < cols; x++)
{
if (Math.abs(xCoords[x] - event.getX()) < xMin)
{
xMin = Math.abs(xCoords[x] - event.getX());
xNear = x;
}
}
for (int y = 0; y < rows; y++)
{
if (Math.abs(yCoords[y] - event.getY()) < yMin)
{
yMin = Math.abs(yCoords[y] - event.getY());
yNear = y;
}
}
dots[xNear][yNear] = !dots[xNear][yNear];
invalidate();
return true;
}
}