How do you set up grid tables in Java - java

I need a hint, a big hint.
In a grid, say 500 by 500, I need to know the value of that square. The answer would be yes or no. Is something there or not. Think of it as piece of graph paper; has the square been colored in, or not?
How would I set up this table up in Java? An example would be; I want to see if at location, x=400, y=400; is this square occupied or not.
I am just confused by how that’s done. Is that what arrays are used for?
If you would please include an example of how I would declare this table, how I would read from this table and how I would write to this table.
Thank you for helping out.

You can use a multi-dimensional array to hold the rows and columns of a table. Then you can insert data accordingly and check whether a value is true or false using indexes. Here is a link. https://www.geeksforgeeks.org/multidimensional-arrays-in-java/
it will make sense if you make one(multidimensional array) then print it to your screen. then you can populate it and check for values by the indexes. example [x][y] returns true

Related

Java : dynamic matrix for cinema project

im working on a cinema project and i want to implement a hall creation, thing is i want it to be dynamic, so i put the values i want in a JPanel and it pops me out a theatre with the rows and columns i entered, with some kind of icon that changes color when i click it, that way i can create a theatre with the seats and aisles the way i want. I have read that it can be done with a bidimensional array (or matrix), thing is, when i want to get the values from the JPanel in the matrix, it says i cant get the value from a non-static to a static reference. Is it possible to do? Any help is appreciated. Thanks a lot in advance!
Arrays are inherently static, once they are set, their length cannot be changed. So with a two dimensional array, the width and height are fixed. Try using a two dimensional ArrayList instead of an array.
ArrayList<ArrayList<Seats>> theater = new ArrayList<ArrayList<Seats>>();
Now, this functions that same way as the array's you're using, except now the ArrayLists are non static.
To navigate
theater.get(index); \\Returns the ArrayList(the row) at index
theater.get(index).get(seatNumber); \\Returns the seat at row index and seat number;

Customize x axis with JfreeChart XYLineChart

I'm displaying the results of a clustering operation on a XYLineChart using JFreeChart. For now the plot I get is this one:
I need to change the x axis in order to get this display:
So basically I first need to get rid of the tick and numbers (this I already searched and found a way to manage how to do it). The problem for me is to add my own custom String values on the x axis AND to have these positionned at specific position. I already have an arraylist with the Strings and another one with the position values, no need to calculate them, i just want to know how to use them with the plot.
Thank you in advance and, as usual, please tell me if my explanation needs to be clarified.
in order to change the content of the display, you need to change your query to have the output you like to see.
to change the position you may need to use something like:
categoryPlot.getDomainAxis().setCategoryLabelPositions(CategoryLabelPositions.UP_90);
you need to show some of your code so that you can get the proper advice

How to make multiple references to same object in Java

I am implementing a crossword puzzle backend.
Crossword is composed of slots is composed of cells.
Lets say cell at (3,14) has a value of "a"
I want crossword.changeCellValueByCoordinate(3,14,"x") to do "a"->"x"
Lets say some Slot slot has a head cell at coordinate (3,14)
Slots head can be identified by (2,across) == Cell(3,14)
So when I call slot.getCellValueByNumberAndDirection(2,across) next, it already changed from "a" to "x".
And another method (for the same cell) crossword.getSlot(2,across).getHead also changed from "a" to "x".
How do I make it so all three objects of different types (Crossword, Slot, Cell) has an attribute (perhaps of same name, lets call it "content") that is shared/referenced by all Classes?
There are lots of ways to handle this. Here's one way:
I don't care too much for the "Slot" class. This does nothing to help with the intersections between the clues.
When you look at a crossword puzzle, what do you see? I see a board of empty squares. Some have little numbers in the corners. I also see dark blocks I know I can't write in. I see a list of "down" clues, and a list of "across" clues.
No matter what I do, I write on that board of empty squares. So I'd make a grid-like class called Board that would allow me to write on it. The little number in some squares would be how specific words are indexed. The contents of each cell could be a space, a letter, or a -1 if the square isn't writable.
You might choose to implement a Square (or Cell) class. It would include a flag stating the instance's ability to be written to. It could include a number signifying the cell to which a clue applies. There are more elegant ways to do this. Implement this, and we can discuss more advanced class hierarchies.
The Board class would contain an NxN structure (probably an array) of Cell instances.
Another class would be called something like "Clues" that corresponds to the down or across list of clues. Clues includes a list of Clue instances.
Finally I'd have a Clue class where each instance includes an ID (the 15 in "15 down" for example), and the clue text to be displayed.
I think once you initialize the Board, you'll have all you need to make a nice crossword program.

How would I programmatically store and apply a selection in a JTable

I have a program that creates a huge jtable with CellSelection enabled to copy to clipboard. I also have an Action that copies the complete table.
It does so by:
correspondentTable.selectAll();
// then copy
correspondentTable.clearSelection();
It would be very nice if after that the "complex" selection would be restored after this action.
So far I found table.changeselection(row, col, toggle, extend) but I was wondering if there is a "more elegant" way of doing this.
If you are looking for individual cells, you could iterate over the table cells and check them using isCellSelected. Actually, I see no "more elegant" solution than that.
It should be possible to limit the range you need to check:
Use getSelectedRow() to get the minimum index, getSelectedRows() gets you all indices. There are similar functions for columns.
You store these values somewhere and use changeSelection to restore the selection.
Edit: You can get the column model (getColumnModel()) and the selection model (getSelectionModel()), which store the selection value. If you can copy them, you could reapply there values afterwards.

How to implement jtable with variable row-height

None of the answers to two previous questions (here and here) resolve my problem.
I have a multi-column jtable for which I want to display string-content of some columns over more than one line within the cell based on newline char's ("\n") within the string. The number of newlines per string is random, known only at run-time. Only the affected row must be adjusted across all columns to the new height. There may be a different number of lines per affected column, and the row-height needs to be adjusted to the maximum height of these, across the columns.
How do I do this?If possible some sample code would be very much appreciated.TIA
If I got you right, I think you need a MultilineCellRenderer . There are already plenty of examples around. Normally they are based on a JTextArea to get the line wrap functionality.
I haven't used it myself yet, but here is an example, which looks kinda good at first view:
MultilineCellRenderer

Categories

Resources