I am trying to store X axis position of mouse and Y axis position of mouse separately inside a two dimentional array and use it later but getting errors. I am new to arrays and i am trying to initialize a two dimentional array using for loops and instance variables/global variables. The error which i am getting says that "array constants can only be used in initializers". I went through the documentetions and changed the code but not got a solution.
Questions and documentations which i read to get a solution
- Create a 2 dimentional array
- Initialize a two dimentional array
- Java: Declaring a multidimensional array without specifying the size of the array ( eg. new int[10][] )
- casting Object array to Integer array error
- Array 2 Dimentional
- java convert integer to int array
My code:
package com.selenium;
import java.awt.*;
public class MouseAxis {
int[][] mouseLocation ;
int AxisX;
int AxisY;
public void getAxisXAndY(int x, int y){
AxisX=x;
AxisY=y;
} // getAxisXAndY
public void mouseAxisPosition() throws AWTException{
int x , y;
PointerInfo pointer = MouseInfo.getPointerInfo();
Point point = pointer.getLocation();
x=(int)point.getX();
y=(int)point.getY();
getAxisXAndY(x,y);
} // mouseAxisPosition
public void storeMouseLocation(){
for(int i=0; i<=20; i++){
for(int j=i; j<=1; j++){
int[][] temp = new int[j][i];
temp[j][i]={AxisX,AxisY} ;
mouseLocation[j][i]=temp[j][i];
Thread.sleep(4000);
mouseAxisPosition();
} // for_2
} // for_1
} // storeMouseLocation
} // MouseAxis