I am new to making a simple, offline Java game which consists of a few mini games with basic GUI (Swing). How does one go about loading different mini games through a main menu? For example, if I was making a website, I can put hyperlinks on the main page to link to different pages. Would one clear the screen first and then load the class which contains the game and its graphical contents?
Thank you for your help.
These can be achieved by the following steps, you need to code yourself !
You can keep your different games in different classes and these classes would have a particular call methods which must be returning a PANEL/Jpanel with the game in it !
Now in your main method, there must be a Main Panel which would have different buttons/links to load different games and a space to load the game on the main panel.
The action of these buttons would call the respective methods of the game and load the panel return by the method in the space !
Thats the simplest explanation !
Related
This might be either a too basic and a too huge question to ask.
Although I study business management as my discipline, by some chance I am taking a Java module this year and am currently working on building a 'Connect Four' game. So far, I have succeeded making the game work in the console, however, I have utterly no idea about how to present the game through GUI. I mean, I could build a GUI window in a separate class but could you help me extending the class methods I created before to the GUI class that I don't have to write the whole programme again? Many thanks.
All of the codes I wrote are here: https://github.com/aca16kd/java-connect-four/tree/master/Assignment/src. The classes live in assignment2017 folder as well as the main class, PlayConnect4, and the EasyReader class which could be used as exactly same as java.util.Scanner lives in sheffield folder.
The simplest way (I can think of) is having a separate GUI class which extends Jframe and implements Action Listeners. This way you could have a simple text box in a frame with a couple of buttons.
My question is more about general design and code flow, rather than a specific issue like syntax. How can I take an existing game (a fighting game, kind of like Pokemon) and add a GUI to it? I've done very simple GUIs before, so I understand the concepts of adding a frame, selecting a layout, and adding panels, buttons, images, etc. I'm not stuck on the details. Rather, I don't know where to implement the GUI.
Is it best to create a class or classes for the GUI, and then create instances of those classes within my existing code? Or do I go the other way around, and have the code within the GUI drive the game forward?
I also haven't used event driven programming before. How does this fit into the structure?
You might want to read up on the Model-View-Controller pattern.
Take your existing game and turn it into a library of objects that maintain the game state through the various actions the players can make. This will be your "Model".
Then create some graphical elements that will display the game state to the user. This will be your "View".
Finally, you will need some user interface elements that allow the user to update the game state (buttons and such). These will have callback or events in which you will put the code that calls the methods in your "Model" object to update the game state, and to update the "View" objects as necessary. That's the Controller.
I am currently learning java and wanted to give myself a project that was both challenging and fun. I decided to build a game that I remember playing when I was a kid called dopewars.
This is my second attempt at this game. When I began my first attempt, all went well. After a short while my source code began to fill wildly out of control until I could continue no more as I kept getting lost within mountains of code.
I then decided to begin again, only this time I wanted to seperate the gui from the logic (2 different .java files). This is where my problem lies. Previously this would work fine. Since seperating my java files the functionality has stopped.
When I press jbutton b1, my program is supposed to take the price value of cocaine and the units value entered into the jtextfield by the user, perform a calculation by accessing a method within Buy.java, and then update the appropriate JLabels within the s jpanel of GUI.java.
For example, user x wants to buy cocaine at the price indicated, so he enters a value representing the quantity he would like. He then presses the buy button which ultimately deducts the money from his pocket which is shown on the left side of the program window by using a method within the Buy class.
I hope you can understand my explanation and I hope to hear from you soon. Thanks.in advance. My Source code is below.
Maybe you should make a main.java (with GUI, etc.) and a logic.java (or whatever you want to call it) and make the main class Extend the logic class. to solve your jbutton problem, you should probably make the Main class extend the JFrame class, and implement the ActionListener interface, and then in the ActionPreformed method, call a method to do the logic from the logic class. I hope this helped.
I'm building a game using Java for a university project. The game had to use a textual interface at first, and only later we had to add a GUI.
I have a thread running the game logic and updating the game model, and a separate thread, the one with the main function, having the JFrame instantiated on, where I then switch panels according to the game state (the controller tells the gui which frame to display with a setPage method).
Getting the user input, however, is what is driving me crazy now.
With the textual interface, all I had to do to get the user input was to call a method, something like getPlayerNum, and after the user put in the number and pressed enter I would have the input back to the controller.
All the game logic is implemented in a game loop, asking for input when it needs it and notifying the output to the view.
How can I emulate a similar behavior now that I'm forced to use a event-based gui?
How can I block the method call until the user clicks a button, and then have the method return the correct value?
Kudos for developing an independent game model. As a prelude to designing your GUI, start with this very simple game. Add a TextView that listens to the model and reports on game progress. This will help you see how the GUI works, and it may help you decide what additional features you may need in your model.
So I'm new to Android, and have been having some difficulties with understanding threading/Android UI updating and such. I have some code for a simple game engine I made for an AP computer science final project and I have been trying to make it into an Android app. Coming from the java world I'm not used to threading or worrying about how much time or where a calculation is taking place so I've been having some difficulties getting my game playable. After a splash screen and a main menu, I have it set up so an activity named "Play" starts. In this activity I have found that I can initialize my game engine object(which gets passed from class to class), create an object I created to make an AsyncTask("GuiThreader" in the code linked below) but as soon as I throw in some code in "Play" to do anything more than that (like initialize a button, or start the threader helper class) I get an "Activity has Stopped Unexpectedly" error meaning I'm doing something wrong. I've been looking at a lot of the Android example code but its making little sense to me. So I guess with all that background my bigger question is how can I get this code working? More specifically where should I have my loop that checks when the game is over, and how can I update my button colors outside of the "main" threader to keep it from crashing.
Here is my code:
"Play" Activity (with lines of code commented that I was testing with):
http://pastebin.com/K5kFsMvG
"GuiThreader" used to make the game calculations an AsyncTask: http://pastebin.com/306eUYfq
"GUIdriver" used to call a class that updates the button colors: http://pastebin.com/RANZBH38
"ButtonColorUpdate" saves a value for buttons and updates their colors: http://pastebin.com/qN2fw1RC
If you need anything else just comment and I'll put it up. Thanks in advance for any help!
Start by reading this. Then I'd recommend looking at the code from Replica Island. There's a ton you can learn from studying the code of this project. If you want to interact with GUI "stuff" from a separate thread, a Handler (see the API) can be useful for sending messages between the two.