- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作一款会在屏幕上创建随机圆圈的游戏。随机创建的圆圈的值为红色或绿色。
我的问题是,我不仅希望能够确定用户何时单击其中一个圆圈,还希望能够确定他们最终单击的圆圈(红色或绿色)。我的主要问题是试图找到将要绘制的圆的 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;
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
// code below works when its here
float randomWidth =(int) (random.nextInt(Math.abs(getWidth()-radius/2)) + radius/2f);
float randomHeight = (random.nextInt((int)Math.abs((getHeight()-radius/2 + radius/2f))));
canvas.drawCircle(randomWidth, randomHeight + radius/2f, radius, paint);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
int x = event.getX();
int y = event.getY();
if(isInsideCircle(x, y)){
//Do your things here
}
return true;
}
private boolean isInsideCircle(int x, int y){
if (((x - randomWidth)^2 + (y - randomHeight)^2) < radius^2)
return true;
return false;
}
}
下面的代码不起作用
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;
//its right below here the code compiles perfectly, but gets errors in logcat
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);
}
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
if(isInsideCircle(x, y)){
//Do your things here
}
return true;
}
private boolean isInsideCircle(int x, int y){
if (((x - randomWidth)^2 + (y - randomHeight)^2) < radius^2)
return true;
return false;
}
}
这是日志猫
04-01 03:41:48.856: E/AndroidRuntime(4010): FATAL EXCEPTION: main
04-01 03:41:48.856: E/AndroidRuntime(4010): Process: com.Tripps.thesimplegame, PID: 4010
04-01 03:41:48.856: E/AndroidRuntime(4010): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Tripps.thesimplegame/com.Tripps.thesimplegame.Main}: java.lang.IllegalArgumentException: n <= 0: 0
04-01 03:41:48.856: E/AndroidRuntime(4010): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
04-01 03:41:48.856: E/AndroidRuntime(4010): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
04-01 03:41:48.856: E/AndroidRuntime(4010): at android.app.ActivityThread.access$800(ActivityThread.java:144)
04-01 03:41:48.856: E/AndroidRuntime(4010): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
04-01 03:41:48.856: E/AndroidRuntime(4010): at android.os.Handler.dispatchMessage(Handler.java:102)
04-01 03:41:48.856: E/AndroidRuntime(4010): at android.os.Looper.loop(Looper.java:135)
04-01 03:41:48.856: E/AndroidRuntime(4010): at android.app.ActivityThread.main(ActivityThread.java:5221)
04-01 03:41:48.856: E/AndroidRuntime(4010): at java.lang.reflect.Method.invoke(Native Method)
04-01 03:41:48.856: E/AndroidRuntime(4010): at java.lang.reflect.Method.invoke(Method.java:372)
04-01 03:41:48.856: E/AndroidRuntime(4010): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
04-01 03:41:48.856: E/AndroidRuntime(4010): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
04-01 03:41:48.856: E/AndroidRuntime(4010): Caused by: java.lang.IllegalArgumentException: n <= 0: 0
04-01 03:41:48.856: E/AndroidRuntime(4010): at java.util.Random.nextInt(Random.java:182)
04-01 03:41:48.856: E/AndroidRuntime(4010): at com.Tripps.thesimplegame.DrawingView.<init>(DrawingView.java:38)
04-01 03:41:48.856: E/AndroidRuntime(4010): at com.Tripps.thesimplegame.Main.onCreate(Main.java:30)
04-01 03:41:48.856: E/AndroidRuntime(4010): at android.app.Activity.performCreate(Activity.java:5933)
04-01 03:41:48.856: E/AndroidRuntime(4010): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
04-01 03:41:48.856: E/AndroidRuntime(4010): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
04-01 03:41:48.856: E/AndroidRuntime(4010): ... 10 more
最佳答案
您所说的randomWidth
和randomheight
实际上是圆的x和y坐标。
这是方法标志:
canvas.drawCircle(x, y, radius, paint);
要检查用户是否在圆圈内单击,您可以执行以下操作:
public boolean onTouch(View v, MotionEvent event) {
int x = (int)event.getX();
int y = (int)event.getY();
if(isInsideCircle(x, y)){
//Do your things here
}
return true;
}
private boolean isInsideCircle(int x, int y){
if (((x - center_x)^2 + (y - center_y)^2) < radius^2)
return true;
return false;
}
关于java - 尝试获取在android中绘制的每个随机圆的x,y坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29385506/
以下是我所拥有的。 我唯一需要的是让顶部填充。所以 12 点钟它应该是一个填充,广告 6 点钟它应该以渐变结束。 实现这一目标的最佳方法是什么? (这个想法是让它在下一步中旋转。) Codepen
我用 Canvas 绘制了倒计时 工作代码:http://jsfiddle.net/ajFsx/ window.onload = function() { canvas = document
我是stagexl的新手,我知道这是非常基本的问题,但是我找不到真正快速的答案,因此我认为将这个答案提供给与我处于同一职位的任何人都很好。 如何在stagexl中创建从x到y的线? 以及如何创建一个以
我想知道以编程方式为图像制作圆 Angular 的最佳方法是什么。这可以使用 PHP 或 javascript。一个算法也可以做同样的事情,我可以用 Image::Magick 或 GD 对其进行编码
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 8 年前。 Improve this q
我有一组二维点。我想找到: 包含所有点的最小三角形 包含所有点的最小圆。 是否有任何算法可以做到这一点?我遇到了 Convex Hull 来为一组点拟合凸多边形。但我想要一个圆形和三角形。 提前致谢
如何计算两个圆的交点。我希望在所有情况下都有两个、一个或没有交点。 我有中心点的 x 和 y 坐标,以及每个圆的半径。 Python 中的答案是首选,但任何可用的算法都是可以接受的。 最佳答案 Int
我需要用 QPainter 画一个圆。当我像这样使用 drawEllipse 函数时: void UserClass::Draw(QPainter &painter) { painter.sa
计算几何问题: 在多边形(例如BCDE)的边(例如EB)上随机选择点P0,以找到可能的点(即, P1,P2,P3,...) 基于给定距离(即 r)在其他边上。下面的演示展示了一个解决方案,它通过找到以
这个问题在这里已经有了答案: 关闭 13 年前。 重复: What is the best way to create rounded corners How to make a cross bro
我有一个 ionic4 应用程序,我需要在其中实现类似于下面卡片中的 img 效果。在边缘模糊到中心,然后在中心用另一个白色边框清除 我怎样才能做到这一点?请忽略编辑图标 最佳答案 .card
我想旋转一个 SVG 圆圈,同时保持其他元素不旋转 当我尝试使用 rotateZ(15deg) 旋转圆(白色)时,这就是我得到的: 这是我目前的进展: https://jsfiddle.net/41h
我正在尝试根据时间戳实现 LineString 挤压。正如 github 中提到的,它应该被实现,但事实并非如此。它应该类似于下面的屏幕截图。 到目前为止,我发现可以对多边形使用挤压,但随后我必须以某
我用了this question我创建了像this这样的形状但现在我不知道如何在第一次单击时为每个圆圈设置文本? (如井字棋) 最佳答案 给你! - 为了方便起见,我合并了它。只需单击圆圈即可查看其上
如何判断圆和矩形在二维欧几里得空间中是否相交? (即经典的二维几何) 最佳答案 这是我的做法: bool intersects(CircleType circle, RectType rect) {
圆 A 沿 x 轴向右移动。圆 B 沿 y 轴向上移动。我想知道他们是否会发生碰撞。 (不是何时,只是如果。) 半径相同,恒速度不同。 This answer似乎解决了这个问题,我的问题最好应该是这个
Relevant Codesandbox 我一直在我的应用程序中看到一种模式,当我创建圆形的div时,当它们的尺寸较小时,它们有时似乎具有边缘。请参见下面突出显示的代码的图像。为什么会发生这种情况,有
目前,我在 c3.js 中生成的图表图例是颜色矩形,我想将其更改为圆形。我该怎么做? var chart = c3.generate({ data: { columns: [
我需要显示带有圆 Angular 的图像。很久以前,我看到一个网站使用 javascript 库执行此操作,该库将圆 Angular 图像覆盖在普通图像上。 我们是否有任何 javascript 库(
在我的程序中,我使用 css 设计了我的按钮样式。我正在使用“-fx-background-radius”来圆 Angular ,并注意到当我将鼠标悬停在原来的 Angular 上时,它允许我单击按钮
我是一名优秀的程序员,十分优秀!