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.
Related
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)?
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.
I have some questions regarding the representation of the gui objects in uml class diagrams.
For example if a have a class which extends the JFrame, then i will design the UML class diagram with the inheritance symbol, but in the JFrame, i do not need to write down all of it's class variables and methods, but only those whose my class will use right??
Second how will i represent that my class will use a specific layout manager? With the association symbol, i quess but i am not sure.
Say for example I have a package named gr.mydomain.exampleproject, and I have a class extending the JFrame.
Is the following approach correct or do I need to put the JFrame in a separate package (javax.swing)?
Yes, you must draw the inheritance symbol to the JFrame class, but leave the JFrame class empty, don't put any fields or methods in it. Everybody knows or can look at the API to see what JFrame contains. Besides, you would fill up the space with the multitude of methods present in JFrame.
Do it like this:
As for layout managers: I believe the dependency relationship is the correct one in this situation. The association relationship would be correct if you would call methods on the layout manager's class. But you're probably just doing something like frame.setLayout (new LayoutManagerClass ()); (a.k.a. just creating the object). In this case, it's a dependency relationship.
Please bear with me as I've just started using NetBeans for the first time! Basically what I'm trying to do is create different panels that fit inside one frame, except that only one panel will be visible at a time. It'll start with one panel, and depending on what the user inputs, the panel that corresponds to what the user puts in pops up.
I've tried looking into utilizing LayeredPanes since that's what I've come after hours of researching this only.. I don't understand how to do it! I think using different panels would be much easier than using different frames, so that's why I'm just going to stick with layering panels.
If anyone could explain LayeredPanes, I'd be very grateful! I'm not sure my coding will help here, but if anyone needs it I'll put it up.
This tutorial should point you in the right direction, however, if you want to have items positioned over each other, you might also want to take a look at the CardLayout:
The CardLayout class manages two or more components (usually JPanel
instances) that share the same display space.
Reading your question I dont think that what you need is a LayeredPane,
Basically what I'm trying to do is create different panels that fit inside one frame, except that only one panel will be visible at a time.
Tell exactly what you want to achieve, I mean on what you are working on.
...and depending on what the user inputs, the panel that corresponds to what the user puts in pops up.
I'd like to suggest a JDialog , show dialogs depending on the user inputs. To make sure if this is what you need, you want to provide us with more information. :)
Till now, I have developed simple swing applications so there was no need to break the GUI code into diff. classes but as this application is going to be very large, I decided to break the code into diff. classes so as to make the code more manageable.
But before proceeding, I have some doubts in my mind which are as follows:
Brief description of GUI
It will have a main JFrame (MainFrame). On that a JPanel(MainJPanel) is set whose layout is to be CardLayout. It will contain 25 cards (each card is in the form of JPanel which contains its own swing components).
Q1. I have decided to make 25 classes (each for one JPanel card). Is it correct approach?
Q2. If the above answer is correct, then how can I write code of xxxxActionPerformed() methods of buttons which are on those cards (25 cards) as these methods need access to the object of MainJPanel
e.g.
public void buttonActionPerformed(ActionEvent evt) {
java.awt.CardLayout c = (java.awt.CardLayout) mainJPanel.getLayout();
c.show(mainJPanel, "card1"); // card1 is this card
mainJPanel.updateUI();
}
I googled for swing examples but almost all of them shows the use of diff. swing components. Can you also please suggest me a link that shows some swing examples that contain GUI codes in diff. classes.
Q1) That sounds like quite a lot of classes. While it's possible that each class has distinct functionality I find it more likely that you could combine some of those into more common classes. For example instead of YellowCard and BlueCard you could simply have ColorCard where color is a parameter.
Q2) Model View Presenter (MVP) and Model View Controller (MVC) are two (or one, depending on your view) common design patterns which help design GUIs so that everyone has the data they need.
More specifically, you might not need all cards to have a reference to the parent panel. For example, if you have a BurgerPanel which allows the user to order burgers and a StatusPanel which shows how many burgers have been ordered you can communicate between them as follows...
Create a StoreStatus object and pass it to both BurgerPanel and StatusPanel. When the user orders a burger with burger panel it updates the store status. The store status notifies the StatusPanel of this update via the observer pattern and then the StatusPanel reflects the change.
UPDATE: In regards to your specific example you would either some kind of reference to the parent class or you could notify it of updates with the observer patterns. (The advantage of the observer pattern is that any changes to the parent class couldn't create changes in the child classes.)
I would say you are correct in creating a class for each card. This is a logical way to split up the code.
If you need to reference the MainJPanel then simply pass it into the constructor of each card class and keep a reference to it.