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
Related
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
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.
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.
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() ).
I'm making a physics simulator for fun and I was looking up graphics tutorials when I tried to figure out the difference between all these J's.
Can somebody elaborate on them or perhaps provide a link to a helpful source?
Those classes are common extension points for Java UI designs. First off, realize that they don't necessarily have much to do with each other directly, so trying to find a relationship between them might be counterproductive.
JApplet - A base class that let's you write code that will run within the context of a browser, like for an interactive web page. This is cool and all but it brings limitations which is the price for it playing nice in the real world. Normally JApplet is used when you want to have your own UI in a web page. I've always wondered why people don't take advantage of applets to store state for a session so no database or cookies are needed.
JComponent - A base class for objects which intend to interact with Swing.
JFrame - Used to represent the stuff a window should have. This includes borders (resizeable y/n?), titlebar (App name or other message), controls (minimize/maximize allowed?), and event handlers for various system events like 'window close' (permit app to exit yet?).
JPanel - Generic class used to gather other elements together. This is more important with working with the visual layout or one of the provided layout managers e.g. gridbaglayout, etc. For example, you have a textbox that is bigger then the area you have reserved. Put the textbox in a scrolling pane and put that pane into a JPanel. Then when you place the JPanel, it will be more manageable in terms of layout.
JFrame and JApplet are top level containers. If you wish to create a desktop application, you will use JFrame and if you plan to host your application in browser you will use JApplet.
JComponent is an abstract class for all Swing components and you can use it as the base class for your new component. JPanel is a simple usable component you can use for almost anything.
Since this is for a fun project, the simplest way for you is to work with JPanel and then host it inside JFrame or JApplet. Netbeans has a visual designer for Swing with simple examples.
You might find it useful to lookup the classes on Oracle's website, for example:
https://docs.oracle.com/en/java/javase/15/docs/api/java.desktop/javax/swing/JFrame.html
There you can see that JFrame extends JComponent and JContainer.
JComponent is a a basic object that can be drawn. JContainer extends this so that you can add components to it. JPanel and JFrame both extend JComponent so that you can add things to them. JFrame provides a window, whereas JPanel is just a panel that goes inside a window.