How to implement my code in MVC Java? - java

I am making a game which will have three games implemented in it. I started off my code in a single class, now that I want to move it around into a MVC format I am having errors. I started off with writing all the code in GameView Class, I now want to move some of the code from this class to a SView Class. The problem is that I have intialised the JFrame in GameView class and it cannot find the variable frame when I move my code to SView class. So then I have been told to make it into a panel and then move it but it's just messing up my game.

You have to organize things a little differently. Some things to consider:
Is GameView your main window? Make it so GameView extends JFrame.
Is SView a panel? Make it so SView extends Panel.
Another thing to think about: SView is some panel that you want to add to your GameView, but it's not the responsibility of SView to add itself. Instead, simply instantiate and assemble these components somewhere outside both of these classes, f.e.:
GameView gameView = new GameView();
gameView.add(new SView());
You could also have GameView add a new SView() in its constructor code instead.
Anyway, the main point here is that a component should not concern itself with adding itself to its parent, just with creating its own content/children.
Good luck.

Some suggestions:
The main GUI would use a CardLayout so you can swap JPanels easily.
The main function of the select game menu will be to swap JPanels, so the game selected is visualized and then initialize the selected game.
Work on each game individually. Gear each individual game class towards produding a JPanel to hold the game. Give each game class a main method for testing that would create a JFrame, create a game class instance, place the instance's JPanel into the JFrame, initialize the game and display it.
Once the game is working on its own, try adding it to your larger GUI, the one that can show multiple games.
Consider having each game class implement a common interface for common functionality. For example you could give the interface an initialize() method that each game class would override.
Each game class would need to be MVC-based so that it has a model, a control class(es) to interact with the model and a view class that displays the state of the model on the JPanel as noted above.

Related

How to use CardLayout with JPanels being created in different classes?

I am trying to create a simple Java Swing GUI application. I basically want a main menu with buttons that will display the rules and the games. Say I have 3 classes: Menu, Game, and Rules. Now, I want to create a JPanel inside each class for that specific GUI. I am having trouble figuring out how to use CardLayout to switch between the JPanels using the same JFrame. Does someone have a clear explanation for this? Should I create another class for the CardLayout? Thanks

How do I create a JPanel and put it on a JFrame from another class?

I am trying to learn some Java GUI stuff. I am trying to create a special jPanel, which I have created in another class [it is a Panel with two labels on it] inside one class, and using a passed JFrame add it to that JFrame from the other class. I have been trying myJFrame.add(thePanel) and such, but it never seems to actually do anything. I have tried doubly making sure it is sset to visible, is at a reasonable location, ect.
I am trying to create the panel in one class, and .add it to the given jframe. There is probably something I am not understanding.
I am not the best at the java GUI stuff... so please be kind :) Any advice you can give would be wonderful.
I am using Netbeans. If I drag the special Panel I created over to my panel in the Design editor, it adds an instance of that panel, but I can't seem to make my own instance of this and have it appear.

Basic Java GUI design

In the GUI book we use in class there are many examples of how graphical user interfaces are made in Java. So many examples, that I'm very confused regarding which one should be used when it comes down to a big application.
So I've seen examples
in which the main class extends JFrame
where the JFrame object is created inside the main method
where the main class extends JFrame AND implements ActionEvent interface
where Listener classes are declared inside the main class
Sure, I can work with all of these, but right now, as I don't have any kind of experience, I don't see the benefit of using any of them. Is actually one of them the correct way to do it or it depends on my sittuation?
Thank you!
"Is A" or "Has A"? This is the question that should be asked when considering extending a class. If the new class "Is A" frame, extend frame, but if the class just needs a reference to a frame, don't extend.
In fact, if a custom component is required, extend a JComponent or JPanel, then add that to a frame, ..applet, window, JInternalFrame, dialog, constraint of a layout, part of a split pane..
Listeners
As to the listeners. Rather than traverse a huge if/else structure in the single actionPerformed() method to determine the required action, it is more optimal to either:
Create a listener for each control that needs it.
Create an instance of an AbstractAction that might be used for multiple controls ('copy' button, menu item etc.).
Summary
So (generally) for the:
JFrame, don't extend.
Listeners, create and add as needed.
Honestly, it depends on the situation. One basic rule when coding is to "code to abstract classes or interfaces".
So, in a nutshell, have a class extending (or implementing) a JFrame (or whatever interface or class) and/or have one doing the same thing with ActionListener.
It is all about the maintainability, flexibility and cleanness of your code.
Standard approach: use EventQueue in method main, that creates main form. In that case all your operations will be asynchronous
in which the main class extends JFrame
the main calss doesn't have to extend JFrame. if it doesn't you should create a JFrame object like you do with any other class
where the JFrame object is created inside the main method
If the MainClass extend JFrame it created inside the c'tor (in the super() ).

Creating GUI for Bantumi game

I've written backend for simple Bantumi game. Now I'd like to create a simple GUI for it, so that it would look like this :
How to start ? What layout should I use, and what type of component each element should be?
Classes :
Basket
Player
Game
Main
Shared
For the layout, I would suggest that you have an outer JPanel that used something like a BorderLayout. It would contain the two end baskets and in the cetner another JPanel that employs a GridLayout to hold all of the playing baskets.
As for the classes, it looks like your Main class is superfluous - you can just put the main method in your Game class and call that. The Shared class and the Player class do not need to be represented as GUI classes, so they can remain as is. I would suggest that you have your Basket method extend JPanel and override the paintComponent() method to allow for custom painting of the beans. You have a choice with Game, you could make it extend JFrame or have it contain a JFrame.
If you want it to be online game you can look at JavaScript canvas or Flash. Of course you can also create Java applet that uses something like combination of Swing and Java 2d

Where do i implement the main() function in a Swing program?

I was just curious where i should place the main function in a Java Swing program. It seems as If it's just way too short too create a brand new class for.
I would not recommend putting the main method inside of any of your Swing components. It doesn't fit well inside a Swing component because it has nothing to do with the components themselves.
Just create a main method in a separate class. It is alright that it is short.
Mushing the logic for running your program into the display logic seems like too much coupling.
I would not put it in the View class. If you're using MVC, and Swing is the View, then main belongs with Controller. That's the class responsible for starting the app, instantiating the View, and collaborating with Model objects to fulfill the use cases.
The Controller should implement the Listener interfaces, because it responds to Swing events as they occur.
I would not have your View extend JFrame. Make the working bits of Swing extend JPanel. When the Controller instantiates the View, it should create a JFrame, add in the JPanels it needs, register itself as the Listener for all Swing events, and make the JFrame visible. At that point your app is up, running, and ready to go.
Put it into your main JFrame class
public class MyFrame extends JFrame {
public static void main(String args[]) {
new MyFrame();
}
MyFrame() {
// ...
}
}
You can put it in any class. It makes sense to put it in the class that represents your main dialog in your app. There is no need to create a class just for main.

Categories

Resources