0
package com.example.applecounter;
    import java.util.Random;

    import android.media.MediaPlayer;
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Context;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup.LayoutParams;
    import android.widget.FrameLayout;
    import android.widget.ImageButton;
    import android.widget.RelativeLayout;
    import android.widget.TextView;
    import android.widget.Toast;
    public class Play extends Activity implements OnClickListener {
        final Context context = this;
        int counter = 0;
        private MediaPlayer mplayer;
        private TextView txt;
        int left = 0;
        int top = 0;
        int right= 0;
        int bottom=0;
        private ImageButton bt[];

        /*Here i have my OnCreate function*/
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_play);
            RelativeLayout rl = (RelativeLayout) findViewById(R.id.RelativeLayout1);
            rl.setBackgroundResource(R.drawable.background);
            /*Here i have which creates my Imagebutton*/
            for (int i = 0; i <2; i++) {
                ImageButton btn = new ImageButton(this);
                btn.setBackgroundResource(R.drawable.apple);
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                btn.setLayoutParams(params);
                btn.setId(1);
                params.width = 83;
                params.height = 80;
            /*Here i random my object ,but its overlapping too much*/
              int left = (int) (Math.random() * 1024 + 1);
            int right = (int) (Math.random() * 600 + 1);

            if (left == 1024 && right == 600) {
                params.setMargins(left, 0, right, 0);
            }
            else
                        params.setMargins(left,0,right,0);
            btn.setLayoutParams(params);
            rl.addView(btn);
            btn.setId(i);
                btn.setLayoutParams(params);
                rl.addView(btn);
                btn.setOnClickListener(this);
    /*Here i have both addview and onclicklistener*/
            }
        }
        /*Here i have OnClick view for mhy button*/
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
// Here i have switch case//
            case 1:
                v.setVisibility(View.GONE);
                mplayer = MediaPlayer.create(this, R.raw.sound);
                //txt.setText("" + counter);
                //counter++;
                break;
            default:
            }
        }
    }
Aleks G
  • 56,435
  • 29
  • 168
  • 265
  • I don't completely understand why you care about overlap, but I can tell you that you are very heavily limiting yourself to 1024 values on left and only 600 on right. Math.Random is also not considered a GREAT source of true randomness. It might pay to use separate "Random" objects instead of a single Math.Random() set of calls, look here: http://docs.oracle.com/javase/6/docs/api/java/util/Random.html – trumpetlicks Feb 17 '14 at 16:52
  • Ok but my screen resolution is 1024X600 and i have build a read and learn application i random positions of my object to prevent overlapping have u any idea. – user3248914 Feb 17 '14 at 16:55
  • Is there anything else in here besides the code? What exactly are you trying to achieve, why, what's not working? – Aleks G Feb 17 '14 at 16:55
  • The code is working but when i change psoition of my objects randomöy so its overalpping too much and you will see that i have vaiable int right and respektive left for random it. – user3248914 Feb 17 '14 at 16:58
  • You need a bit more complex of an algorithm to make the decisions you are trying to make. What you really need is a Boolean 2D matrix that is 1024 x 600 large, where for each randomly place object you fill in the 2D matrix with a filled or not (true or false). When you choose another location, you can then look to see if the placement is on top or not of other objects, if placed there. It is a bit more complex than this, but overall this is what you need. – trumpetlicks Feb 17 '14 at 17:00
  • @user3248914 - I think this is a potential repeat of this question: http://stackoverflow.com/questions/9613123/random-placement-of-rectangles-with-no-overlaps – trumpetlicks Feb 17 '14 at 17:01
  • So can you give me a example and need i a fuction for it? – user3248914 Feb 17 '14 at 17:01
  • Because i am new and i don't know how to write the code for this type of code can you please give me suggestion? – user3248914 Feb 17 '14 at 17:05

0 Answers0