试图获得在x,y各自随机圆是在屏幕上绘制的坐标 [英] Trying to get the x, y coordinate of each random circle that is drawn on the screen

查看:121
本文介绍了试图获得在x,y各自随机圆是在屏幕上绘制的坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

IM喜做一个游戏,将在屏幕上创建随机圆。随机生成的圆圈将具有值红色或绿色。我的问题是,我想是不仅能够确定当用户点击某个圈子中,但确定哪些圈子,他们最终点击(红色或绿色)。下面是我下面的code。我的主要问题是试图找到将要绘制的圆的x和y

Hi im making a game that will create random circles on the screen. The circles created randomly will have the value red or green. My question is that i would like to be able to not only determine when a user clicks on one of the circles but determine what circle they have ultimately clicked on (red or green). Here is my code below. My main problem is trying to find the x and y of the circle that will be drawn

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;

    }


}

我不知道,如果即时通讯接近或真的不远了。

I dont know if im close or really far away.

推荐答案

有许多办法来解决这类问题。你可以自己开车坚果只是想一一列举。其中最厉害的是在创建时分配每个形状独特的色彩。使用该颜色查找的形状。换句话说...

There are many ways to solve this kind of problem. You can drive yourself nuts just trying to list them all. One of the most powerful is to assign each shape a unique color when it is created. Use that color to find the shape. In other words...

当他们点击这样的:

从这个样本的颜色:

请一个HashMap从这些独特的颜色,形状的对象引用。所有的形状绘制其独特的色彩上隐藏的位图,你从来没有把在屏幕上。当用户点击,彩色抬头看看上隐藏的位图XY。运行该颜色直通HashMap中,你有一个参考,他们点击的形状。现在你不需要对重叠,甚至使用不同的形状不是圆的后顾之忧。这使你不仅x和形状,但一切你知道的外形年。

Keep a hashmap from these unique colors to the shapes object reference. Have all the shapes paint their unique color on a hidden bitmap that you never put on the screen. When the user clicks, look up the color at that x y on the hidden bitmap. Run that color thru the hashmap and you have a reference to the shape they clicked on. Now you don't need to worry about overlaps or even using different shapes than circles. This gives you not only the x and y of the shape but everything you know about that shape.

当然这个来工作,你必须使自己的类的形状。我做了独特的色彩非常鲜明用于说明目的。在现实中,你可以只是你增加一个int。这既可以通过构造函数或增加更好的有一个工厂方法注入它。

Of course for this to work you have to make your own class for the shapes. I made the unique colors very distinct for illustration purposes. In reality you could just have an int that you increment. This could either be incremented by the constructor or better yet have a factory method inject it.

这是足够强大,它渲染3D时,即使工作。只要保持可见和隐藏的位图同步。这原来是容易的,因为你只需使用相同的渲染code。循环每个形状,并要求它来绘图,以它独特的颜色,在一个位图,你的手吧。只要没有额外的效果,会改变颜色(阴影,照明,光线追踪)作品。请务必关闭它们。

This is powerful enough that it works even when rendering 3D. Just keep the visible and hidden bitmaps in sync. That turns out to be easy since you just reuse the same rendering code. Loop each shape and ask it to paint itself, with it's unique color, on a bitmap you hand to it. Works as long as there are no additional effects that would change the color (shading, lighting, ray tracing). Make sure to turn those off.

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

By the way, I don't think this:

if(randomHeight > maxY && < minY){

做什么,你认为它。当我想测试的东西是两个值之间我用:

does what you think it does. When I want to test that something is between two values I use:

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

,因为它让我想起了数学不等式像3≤x≤15

because it reminds me of mathematical inequalities like 3 ≤ x ≤ 15.

这篇关于试图获得在x,y各自随机圆是在屏幕上绘制的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆