I need some advices about how to build code correctly.
I creating a chess viewer using javafx.
1) As a new one I don't exactly know what is the best way to make correct hierarchy.
I think to create abstract class Figure and the problem is it seems I need overide own behavior.
I tried extends Node but it seems wrong.
What is correct way to extends to make custom element in javafx?
Maybe you know good link anout creating custom element (not necessary chess) where described this process.
2) I looked at both (browser chess/desktop programms) and one thing about I'm thinking is when you grab & hold chess piece icon and dragging around the board the image icon is moving after your cursor. So in javafx it can be done with MouseEvent that's I understand.
But the question is how to redrawing it? Calculating coordinats?
3) About board. I think it is good way to make some class Cell based on Rectangle ovveriding behavior and class Field which include array of Cell(s)?
Related
I currently have a rather large class that extends JPanel. The class is about 2000 lines of code although it is broken up into many mostly small functions. I want to add a second mode which will render something totally different in the panel. For organization, I am thinking of simply passing the panel to another class and have the other class render the panel. Or I could just add onto the first class. What would be the best approach? The only ugliness with the second approach is that the main class would have to check what mode it is in and if it is in the second mode, it would have to pass mouse and key movements to the secondary class.
If it's something completely different, keep them in two different classes so if anyone else were to look at your code, they'll understand better of what's going on.
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.
I am currently working through the "Providing a Custom Renderer" example on this page. And now I wanted to create more than just one of these boxes. I did this by creating six renderer classes, one for each box.
And now for my question. Is it possible to just have one renderer class for all six boxes? For that purpose I tried to parse two variables to the constructor of the CustomBoxRenderer, like this.
public ComboBoxRenderer(ImageIcon[] currentImage, String[] currentString)
But due to how the programm seems to work, the currentImage array is null until a certain point, so I get a exception.
But let's assume this would work how I expected it to work, I still would have to create six seperate instances of the renderer for each box, which I'd like to avoid aswell.
I hope this is enough information, I could also provide my full code, but I think that'd be too much for this page here, if not, let me know.
If i'm reading correctly you could create a class that extends combobox and just adjust it so that it automatically uses your custom renderer, then all you have to is create a normal instance of your custom combobox and use it as normal except it will use your renderer without any hassle.
e.g. in your constructor you would just have this line
this.setRenderer(new ComboBoxRenderer(currentImage, currentString));
Im unsure why you think you would need to create six instances as the renderer deals with each box.
Hope this helps.
Doing it with the Robot class doesnt seem to work for me, unless there is a way without moving the cursor.
So what i need is a way to get the Component of another program (by creating a new MouseEvent) or just another way to use the Robot class.
Thanks already.
Use MouseInfo.getPointerInfo().getLocation() to get the mouse position right before you move it, then move it (and do whatever you'd like), and then return the mouse to its previous location.
The alternative is fairly complicated, and even more complicated if it's not a Java program - you'd need to provide much more information about that program, and your odds of getting a clean answer would be slim.
I'm trying to implement a small-scale strategy, taking turns game
implemented in Java, GUI is made with JFace and SWT.
My challenge is to write a GUI implementation of the world map,
where countries will act as clickable buttons. However, countries
have no fixed boundaries, no rectangular shape, and simply no way
I can think of to be described in a grid layout.
This is my first time trying to implement a project of this type,
please advise
If it's a tile based map (like at Civilization) or it's displayed as pixmap, you could save the ownership of each tile/pixel in a two-dimensional array. Just display the map a a simple, clickable pixmap in a canvas an add a MouseListener. If you get a click event at the coordinates (X,Y), you can just get your country like:
Country clickedCountry = myCountriesOnMap[X][Y];
... in your Listener implementing the MouseListener interface. myCountriesOnMap would be of type Country[][].
Of course, you will need an algorithm that will resolve the ownership for each tile/pixel at startup or if a territory gets conquered (I don't know, if this may happen). May be you will have to define your countries as polygons (like you would do it for a HTML map). I cannot help you on this, as I haven't done anything similar jet, but I'm sure you will find something on Google.
Greetings
Sacher
Try to use the OpenStreetMap data. It contains exact country borders and good image export possibilities.
The Key:border tag will show you all borders. You could extract it and calculate your clickable areas.