gpt4 book ai didi

java - 尝试获取屏幕上绘制的每个随机圆圈的 x、y 坐标

转载 作者:行者123 更新时间:2023-12-01 22:22:52 25 4
gpt4 key购买 nike

嗨,我正在制作一款游戏,该游戏将在屏幕上创建随机圆圈。随机创建的圆圈的值为红色或绿色。我的问题是,我希望不仅能够确定用户何时单击其中一个圆圈,而且还能够确定他们最终单击的圆圈(红色或绿色)。下面是我的代码。我的主要问题是试图找到将要绘制的圆的 x 和 y

public class DrawingView extends View {



public DrawingView(Context context) {
super(context);
// TODO Auto-generated constructor stub

}
RectF rectf = new RectF(0, 0, 200, 0);

private static final int w = 100;
public static int lastColor = Color.BLACK;
private final Random random = new Random();
private final Paint paint = new Paint();
private final int radius = 230;
private final Handler handler = new Handler();
public static int redColor = Color.RED;
public static int greenColor = Color.GREEN;
int randomWidth =(int) (random.nextInt(Math.abs(getWidth()-radius/2)) + radius/2f);
int randomHeight = (random.nextInt((int) Math.abs((getHeight()-radius/2 + radius/2f))));


private final Runnable updateCircle = new Runnable() {
@Override
public void run() {
lastColor = random.nextInt(2) == 1 ? redColor : greenColor;
paint.setColor(lastColor);
invalidate();
handler.postDelayed(this, 1000);

}
};



@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
handler.post(updateCircle);
}

@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
handler.removeCallbacks(updateCircle);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// your other stuff here

canvas.drawCircle(randomWidth, randomHeight + radius/2f, radius, paint);
}



@Override
public boolean onTouchEvent (MotionEvent event) {

double r = Math.sqrt(((randomWidth^2)+(randomHeight^2)));
int maxX = (int) (((randomWidth)*(randomWidth)) + r);
int minX = (int) ((((randomWidth)*(randomWidth))) - r);
int maxY = (int) (((randomHeight)*(randomHeight)) + r);
int minY = (int) ((((randomHeight)*(randomHeight))) - r);

public int xCoordinateOfRedColor(){
if(redColor == lastColor){
if(randomWidth > maxX && < minX){
event.getX();
}
};
}
public int yCoordinateOfRedColor(){
if(redColor == lastColor){
if(randomHeight > maxY && < minY){
event.getX();
}
};
}
if(redColor == lastColor){
if(randomHeight > maxY && < minY){
event.getY();
}
};
if(greenColor == lastColor){

};
if(greenColor == lastColor){

};

switch (event.getAction()) {
case MotionEvent.ACTION_DOWN :
randomWidth = (int) event.getX();
randomHeight = (int) event.getY();

invalidate();
break;
case MotionEvent.ACTION_POINTER_UP :


break;
}

return true;

}


}

我不知道我是接近还是很远。

最佳答案

解决此类问题的方法有很多种。如果你想把它们全部列出来,你可能会把自己逼疯。最强大的方法之一是在创建每个形状时为其指定唯一的颜色。使用该颜色来找到形状。换句话说...

当他们点击此按钮时:

enter image description here

从中取样颜色:

enter image description here

保留从这些唯一颜色到形状对象引用的 HashMap 。让所有形状在您从未放在屏幕上的隐藏位图上绘制其独特的颜色。当用户单击时,在隐藏位图上查找 x y 处的颜色。通过 HashMap 运行该颜色,您就可以引用他们单击的形状。现在您无需担心重叠,甚至无需使用与圆形不同的形状。这不仅为您提供了形状的 x 和 y,还为您提供了有关该形状的所有信息。

当然,要实现此功能,您必须为形状创建自己的类。为了说明的目的,我使独特的颜色非常明显。实际上,您可以只增加一个 int 值。这可以由构造函数增加,或者更好的是通过工厂方法注入(inject)它。

它的功能足够强大,即使在渲染 3D 时也能正常工作。只需保持可见和隐藏位图同步即可。事实证明这很容易,因为您只需重用相同的渲染代码。循环每个形状并要求它在您交给它的位图上用其独特的颜色绘制自己。只要没有会改变颜色的附加效果(阴影、光照、光线追踪),就可以工作。确保将其关闭。

顺便说一句,我不这么认为:

if(randomHeight > maxY && < minY){

做你认为它做的事。当我想测试某个值是否介于两个值之间时,我使用:

if(minY <= randomHeight && randomHeight <= maxY){

因为它让我想起数学不等式,例如 3 ≤ x ≤ 15。

关于java - 尝试获取屏幕上绘制的每个随机圆圈的 x、y 坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29383335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com