How to check when two object collisions [duplicate] - java

This question already has answers here:
Collision Detection with MANY objects
(3 answers)
Closed 9 years ago.
I have two moving image views in a little game I'm making,
anyway they are always in the same Y so im just want to check when they are in the same x
any help?

int [] object1Position = new int[2]; // declare int array for x,y object position
int [] object2Position = new int[2]; // the same
imageView1.getLocationOnScreen(object1Position); // get imageView1 position
imageView2.getLocationOnScreen(object2Position); // get imageView2 position
public boolean checkColisionOnXAxis()
{
if (object1Position[0] == object2Position[0] )
return true;
return false;
}
just an idea.. but you need to get at each time the current location of the objects..
so you need timer or something like that to get the current position

Related

How to get and compare integer array from a list in a list? [duplicate]

This question already has answers here:
Comparing two integer arrays in Java
(10 answers)
Closed 1 year ago.
I have a program that uses a java.awt robot to get a picture of the screen and then cycle through the pixels and categorize certain colors in blocks in a list of lists of integer arrays. I have a List<List<int[]>> blocks. The first List is a list of all the blocks, the second list is a list of all the pixels in the blocks, and the integer array is 2 value that are the x and y of the pixel. I first tried to use a for loop through all the blocks with list.contain's to find the array, but that always returned false. I am trying now to use a loop like this to find them.
boolean continuer = false;
boolean found = false;
for (List<int[]> thisBlock : blocks) {
int[] newInt = new int[2];
newInt[0] = i;
newInt[1] = j;
for (int[] thisInt : thisBlock) {
if (thisInt == newInt) {
continuer = false;
found = true;
System.out.println("Found pixel in block");
break;
}
if (found) {
break;
}
}
if (found) {
break;
}
}
if (!found) {
continuer = true;
}
This also returned false always. Any help would be greatly appreciated.
Arrays cannot be compared with == operator. There are methods that Arrays Class provides to perform operations on Arrays. Use Arrays.equals(thisInt,newInt) instead of thisInt == newInt

How to fix ArrayIndexOutOfBoundsException in Java? [duplicate]

This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 4 years ago.
I get an ArrayIndexOutOfBoundsException, but only when the getSubImage() function is there.
This is to loop through and get sprites.I've tried it without getsubimage and i've tried using different for loops though nothing has helped.
This is not the same as the thread I am being compared to, as I am using a for loop inside of another. The program works just fine without getSubimage, and the for loops work just fine. I do not know where the problem lies or what it is.
try {
BufferedImage spritesheet = ImageIO.read(
getClass().getResourceAsStream(
"/Sprites/SpriteSheet.gif"
)
);
sprites = new ArrayList<BufferedImage[]>();
for(int i = 0; i < 6; i++) {
BufferedImage[] bi =
new BufferedImage[i];
for(int j = 0; j < numFrames[i]; j++) {
bi[j] = spritesheet.getSubimage(
i * width,
j* height,
width,
height
);
}
sprites.add(bi);
}
}
catch(Exception e) {
e.printStackTrace();
}
I expect it to loop through this and get all the sprites. What actually happens is it doesnt get the subimage and it just throws an out of bounds exception.
Your program is trying to retrieve an element with index not available from an array. Check the line 15 (is numFrames already created some where?). It possible that you are not using a right array or numFrames does not have an element with that specific index. Try running a debugger and see which array is out of index.

Integer Calculation always returns 0 [duplicate]

This question already has answers here:
Why does the division of two integers return 0.0 in Java? [duplicate]
(6 answers)
Integer division: How do you produce a double?
(11 answers)
Closed 6 years ago.
In this block of code which is inside a broadcast receiver I have a calculation.
My goal is to get a battery bar with the correct amount of Green inside it. So for the Green I have an image which is essentially a block of solid green. I am not sure if this is the best way to do this but I decided to resize the block using this calculation:
(Height of Block) * (Battery Level/100)
The actual code:
BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
#Override
public void onReceive(Context ctxt, Intent intent) {
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 100);
percentageView.setText(String.valueOf(level)+"%");
int height_val = (363) * (level / 100); //<-- That's the int which always returns 0
Log.e("MyTag", String.valueOf(height_val).toString());
Log.e("MyTAgg", String.valueOf(363 * (level/100)));
bar.requestLayout();
bar.getLayoutParams().height= height_val;
}
};
And it is inside a class. It's registered in the class not in the manifest. My Text View shows me the battery percentage but my Tags always show me that my calculations returns 0. Please do help me understand. Why does this calculation return 0?
Thank you.

Random string from List<String> [duplicate]

This question already has answers here:
Randomly select an item from a list
(5 answers)
Closed 8 years ago.
Alright, I keep trying to oogle this but It keeps showing answers with a string array which I can't use. This is currently my code:
List<String> Shuffle = new ArrayList<String>();
if(EAmount == finalk){
Shuffle.add("Emerald");
}
if(DAmount == finalk){
Shuffle.add("Diamond");
}
if(GAmount ==finalk){
Shuffle.add("Gold");
}
if(IAmount == finalk){
Shuffle.add("Iron");
}
I can't find out how to get a random string from Shuffle. Please help!
BTW: The Amounts and finalk are integers
You can generate a random integer between zero and the size of the collection (exclusive), and then use it as an index to refer to the corresponding item of the collection.
int randIndex = new Random().nextInt(Shuffle.size()); //generate rand int [0, size[
String randString = Shuffle.get(randIndex); //get random string
It is the same as with Arrays, you just need to use the get Method:
Shuffle.get(random)

Arrays and searching in Java [duplicate]

This question already has answers here:
Arrays.asList() not working as it should?
(12 answers)
Closed 9 years ago.
So I'm just learning about arrays in Java (interesting stuff) but I'm having some problems getting my head around the contains() method.
I tried:
Random rn = new Random();
int first = 12;
int[] tab = new int[first];
for (int i = 0; i <= first - 1; i++) {
tab[i] = rn.nextInt(10);
Which seemed to work fine for filling in my Array, but then I tried a:
System.out.println(Arrays.asList(tab).contains(9));
And no matter what, even if I fill the array manually with 9's, it'll still only print up "false".
What am I doing wrong?
Try this
Integer[] tab = new Integer[]{9};
System.out.println(java.util.Arrays.asList(tab).contains(9));
I get
true

Categories