Print a coordinates marker in 2D array of objects. Java

Print a coordinates marker in 2D array of objects. Java

java 2d graphics pdf
java graphics2d
java graphics2d example
java drawing code
how to create graphics object in java
computer graphics in java
shapes in java
java graphics programming

I'm trying to print out an indicator of coordinates on a 2D array... There is another class that I've used to instantiate objects on the array.

I need to the store coordinates in two (local?) variables, and then display the position of those coordinates in the printed array. (that has already been instantiated with various objects)

This is a snippet of code I have so far, but I can't get the 'C' to print in the right spot on the array. The few options that I tired, either doesn't print it at all, or prints the 'C' in the top left hand corner.

This is one option that I've tired: This option doesn't print the 'C' at all.

   private int cX=0;
   private int cY=0;

     //Randomly set coordinates on array.

       for(int i=0; i<array.length; i++){
            for(int j=0; j<array[i].length; j++){
                int x = randGen.nextInt(9);
                int y = randGen.nextInt(9);
                if (array [x][y].display()=='.'){
                    x=cX;
                    y=cY;
                }
             }
         }


    // print array

    private void displayArray()
    {

      for (int i = 0; i < array.length; i++){ 
        for (int j = 0; j < array[i].length; j++){
           if ((array [i][j].display()==cX)&&
                (array [i][j].display()==cY))
                System.out.print("C");
                System.out.print(board [i][j].display()+"\t")   
         }
            System.out.println("\n");
      }
    }

If I understood you right, you'd like to save coordinates to some kind of data structure and later display them?

Wouldn't it be suitable to create a class for this purpose? E.g. A class called Coordinator, that holds the X & Y values.

Later create a object of the class you've made with X & Y values and put it to a ArrayList.

Code Example:

//Creating the a object holding 2 values
Coordinator cords = new Coordinator(randGen.nextInt(9), randGen.nextInt(9));

//Putting in the object to the data structure
List<Coordinator> list = new ArrayList<>();
list.add(cords);

The explanation and code example should help you solve this issue on your own hopefully.

Printing 2d array with markers set by coordinates on Java, It is quite obvious why this happens of course: for each coordinate you print a character for each marker. This is solved by getting rid of the loops on h and w . Java 8 Object Oriented Programming Programming In this post we will try to print an array or matrix of numbers at console in same manner as we generally write on paper. For this the logic is to access each element of array one by one and make them print separated by a space and when row get to emd in matrix then we will also change the row.


This option doesn't print the 'C' at all.

Your current code example will indeed never print a 'C'.

for (int j = 0; j < array[i].length; j++){
if ((array [i][j].display()==cX)&&
       (array [i][j].display()==cY))

This if condition will be true only if cX equals cY and both are equal to the ascii code value assigned to the array (for '.' this is the value 46). But you may just want to find the cX,cY position and print a 'C'. You should try to campare your position (cX, cY) with the current i and j values.

if ((i==cX)&&(j==cY))

prints the 'C' in the top left hand corner

You never change the content of array at the position. You set x and y which will be overwritten and are later no accessible

int x = randGen.nextInt(9);
int y = randGen.nextInt(9);
if (array [x][y].display()=='.'){
      x=cX;
      y=cY;
}

Do you want to find a random position for your 'C' to appear, like in a board game? Then you need to assign your found x and y coordinate to cX and cY. The right value gets assigned to the left variable.

cX = x;
cY = y;

I hope I understood you right and this helps.

Java Xy Coordinates, JPEG 2000 coordinate system and the values recorded in the JPEG 2000 markers. array for objects class student to process data of n students coordinate print This class represents one coordinate (x,y) in 2d-space. java:7: cannot find� Arrays.toString () We know that a two dimensional array in Java is a single-dimensional array having another single-dimensional array as its elements. We can use Arrays.toString () function to print string representation of each single-dimensional array in the given two dimensional array.


For completeness: I think I worked it out. (Well it seems to be working anyway). Thanks for the help!

  private int cX=0;
  private int cY=0;


  //Randomly set coordinates on the array. 
 for(int i=0; i<1; i++){
  int x = randGen.nextInt(9);
  int y = randGen.nextInt(9);
  if (board [x][y].display()=='.'){
   CX=x;
   CY=y;
  }
 }  


 //Print array
private void displayArray(){
 for (int i = 0; i < board.length; i++){ 
  for (int j = 0; j < board[i].length; j++){
   if ((i==CX)&&(j==CY))
    System.out.print("C"+"\t");
   else
    System.out.print(board [i][j].display()+"\t");
  }
 System.out.println("\n");
 }
}

[PDF] Java 2D Graphics, Array types are always written with the square brackets immediately following the array type. This keeps all Richard Blanchard for pointing out that Swing components print much better with double buffering Java 2D objects live in a plane defined by Cartesian coordinates. picture on the side of the stack with a marker. Prerequisites : Arrays in Java, Array Declarations in Java (Single and Multidimensional) Method 1 (Simple Traversal) We can find number of rows in a matrix mat[][] using mat.length.


  private double[][] mLatLng = {
                {19.01062463, 73.01141475},
                {19.02369039, 73.00778391},};
    public void onMapReady(GoogleMap googleMap) { googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_icon);
    for (int i = 0; i < mLatLng.length; i++) {
    for (int j = 0; j < mLatLng[i].length; j++) {
    googleMap.addMarker(new MarkerOptions()
    .position(new LatLng(mLatLng[i][j], mLatLng[i][j]))
    .title("Location" + i).icon(bitmapDescriptorFromVector(context,R.drawable.ic_icon)));

Coordinate Grid Java, In this example, I will draw in java Cartesian two-dimensional coordinate system with A datum transformation grid is a regular, rectangular array of height values that SkyView-in-a-Jar settings: Grid, GridLabels Checking the 'Display Grid' box will A two-dimensional array can also be used to store objects, which is� The question I have deals with coordinate pairs in 2d arrays in java. I am supposed to Design a class named MyPoint to represent a point. values 0.0 upon object


Plot 2d Array Python, Biggles is another plotting library that supports multiple output formats, as is Piddle. We are working in 2D, so we will need X and Y coordinates for each of our In many languages (Java, COBOL, BASIC) this notion of multi- dimensionality is a Numpy library is the array object or the ndarray object (n- dimensional array). Hi, I am trying to write some code to insert coordinates which are taken from the mouseClick position and then added to a 2D array. The user should click 6 positions on a panel and these positions should be stored in a 2D array.


how to initialize an array in java Code Example, Get code examples like "how to initialize an array in java" instantly right from your google search results with the Grepper Chrome Extension. I am creating a Video class, then creating objects to place into an array. Then I want to print the contents of the array: [CODE=java] public class Video


Does this simulation follow abstraction and encapsulation?, Map markers can access the map, yes, map markers can pathfind, yes, I think you'll want to make a new 2D array of objects to place on the cells. The second paragraph continues on stating that the grid has x and y coordinates, how you use your own LCG for some things, but then you use java.util. However, the data associated with certain systems (a digital image, a board game, etc.) lives in two dimensions. To visualize this data, we need a multi-dimensional data structure, that is, a multi-dimensional array. A two-dimensional array is really nothing more than an array of arrays (a three-dimensional array is an array of arrays of arrays).