Java : dynamic matrix for cinema project - java

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;

Related

How do you set up grid tables in 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

Associate a Jbutton with object values of an ArrayList. Swing

I am working on a mahjong project.
My code contains a dynamic List<List<Tiles>> board.
The abstract class Tiles gets extended by the different kind of Tiles the game has.
Now im interesting on extending this small project with GUI componenets.
My thinking is to create the grid with a GridBagLayout cause my Array is dynamic sized.
A simmilar question has been asked in the past but i had hard time understanding anything from the answers and its a bit old also. old post
What i need is: when im creating an array of Jbuttons i want uponcreation of each button to place inside of them the info of each cell of the array.
Here is for example what my board array looks like: (one cell example)
specialvalue= 1x
coordinates = {0,0}
colour = blue
how i can hold this information in a Jbutton ?
And if its not possible , whats the best way to go from here?
Thanks in advance,
I can post some code of the project upon needed.
As i come back to my question i see that there was never an exact answer so here is how i did it at last and worked.
I used an abstract class for the Tiles , then i created a hashmap at my GUI class and i made key-value pairs with keys:jbuttons and values: all the info of Tiles i needed.
That was the main key to the solution.

How I can get an array with 2 items from a selected item in a JComboBox?

I have an ArrayList<ArrayList<Object>> Suppliers. Each ArrayList<Object> has 2 elements, an int (an id and the id may not be sequential with each other.) and a string (a name).
Now in a JComboBox, I create a DefaultComboBoxModel, containing the main ArrayList 'Suppliers'.
jcbSuppliers.setModel (new DefaultComboBoxModel (suppliersdata.Suppliers.toArray ()));
The JComboBox shows me each element such as [1, Local Supplier], [2, External Supplier], [4, Other Supplier].
Capture 1
In NetBeans debug, i put a Watch on jcbSuppliers. The variables window shows me that each element of jcbSuppliers is an ArrayList with 2 Objects.
Capture 2
Now my question is:
How I can get these 2 items or the array of these 2 objects, but of the selected object or item?
I would appreciate the help. Thank you very much.
First I do have to give kudos to Flextra and his comment as Spring can help you decouple your code, making it much easier to enhance and debug.
Next I suggest that you re-think your current model set up. Rather than begin with nested ArrayLists, I suggest that you create a custom class to hold the two pieces of data each supplier will need.
Then create a non-nested ArrayList of this custom class.
Then you can tell the JComboBox how to display an object of your either by giving it a toString() method that displays the information as you'd like it,
or give the JComboBox a custom cell renderer that displays the information as you'd like it.
Then when the combo box is selected, if you get the selectedItem, it will be an object of your class, and it will be easy to extract the two pertinent pieces of information.

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.

Dynamic JavaFX objects

I'm new to Java and JavaFX I have php,javascript,HTML experience... I'm trying to create a GUI for doing math and showing the results using javaFX text objects. I have a constructor method to create each text object based on its input. Then I have another constructor method which would call the previous one several times creating multiple text objects, but I can't get this to work because it won't let me define the new text objects based on the methods input ex:
public function(variable)
{
Text variable = new Text();
}
The problem is that each time the program is run there would be a different amount of text objects on screen, so I'm not sure how to accomplish the naming convention when I won't know the total # of objects beforehand.
Also how would I add them all to the scene at the end. Would the constructor method add each to a group as they are created and then the whole group would get added with the get children method?
A more general idea of what I want to create:
The program would start with 10 text items on the screen. The user could click on two objects and choose to add/subtract/multiply them together, and then another text label would be created with the answer after processing the event. The new one could then be used to create another answer, and another and so on...
Maybe I'm approaching this totally wrong, let me know how you would approach it.
-back to my initial question, I could name the starting 10 statically if I wanted, but I'll need to know how to do it dynamically to add more I think... ...or maybe I'm waaay off...
Thanks in advance,
Brad
You want to use some sort of simple collection, like an Array or List. You may want to read through the Collections tutorials for Java. For your program, the simplest approach would probably be an array of Text objects:
Text[] textObjects = new Text[10];//Replace 10 with however many you will need
for(int i = 0; i < 10; i++) {
textObjects[i] = new Text();
}

Categories

Resources