Making a basic GUI for a Java 'Connect Four' game - java

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.

Related

How do I bring a GUI into an existing object-oriented project?

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.

How to load different games through a main menu (Java)

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 !

Problems separating GUI from logic

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.

Creating screens(windows) without JFrame

I have been looking at the source code of Notch's game Metagun as a guide to making my own. I noticed he never implements JFrame as a means of making a screen for the game to run in. I can't make any sense of his Screen class because I only see a drawImage method for some strings. What other ways can you create a window besides using JFrame in Java?
There are several ways to create windows in Java, but for a game, I believe you're looking for a window without decoration. I'll point you then to JWindow, but it's more rudimentary...
Looking at the source I found on github, he extends Applet and Runnable and creates a generic JFrame within Metagun.main.

Web applet game design [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I making web games in java and having troubles each time when getting to the GUI design, usually I'm looking at others codes and copy most of the design, and I can't decide which design I should use. I used Applet, Canvas, JFrame. What I need is the main loop and draw function that changing between different games. So what class should I use and what design, which is better, why, maybe links to useful tutorials and examples... Please explain your suggestion. Thanks.
If you want some code to dissect, you should check out Killer Game Programming in Java
It's a good book, even if it now uses old libraries. I bought it a few years ago. If you don't fancy buying it, you can still download all the source code from the website
You can pretty easily abstract away the choice between using an Applet or Java Web Start. If made to choose, I'd go with JWS, purely because I find it easier to setup a development environment to debug.
These two links demonstrate how to use JFrame v Canvas:
JFrame http://download.oracle.com/javase/tutorial/uiswing/components/frame.html
Canvas http://en.wikibooks.org/wiki/Java_Programming/Canvas
An Applet (or JApplet) is embedded in the web-page, and thus has only the space given to it by the browser. It stays there as long as the page is shown.
A proper (J)Frame is its own window, thus can be as big as the windowing system (and screen size) allows - and it can be closed without closing the browser page (and the applet in it).
This is the main difference. You could simply do your drawing in a own Canvas (for AWT) or JPanel (for Swing) and put this inside the (J)Applet or (J)Frame, thus abstracting the difference between both away until you are ready to decide.
I would develop your application using a JFrame that contains JPanels in it.
When you're ready to go to the web, you can use java web start to launch your JFrame.
I used the java tutorials from Sun and they were very effective.
You can write a wrapper class that enables your game to operate either as an applet (JApplet) or as a JFrame-based application.
Then I would write all your game-specific code on a JPanel that is added as a child of either the JApplet or the JFrame. You can use a JPanel in exactly the same way that you previously used a Canvas.
Sample code below:
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class AppletApp extends JApplet {
public void init() {
// do any applet-specific initialisation here
getContentPane().add(new AppPanel());
}
private static final class AppPanel extends JPanel {
public AppPanel() {
// do any common initialisation here
add(new JButton("Hello World!"));
}
}
public static void main(String[] args) {
// do any frame-specific initialisation here
JFrame f=new JFrame("Frame");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
AppPanel appPanel=new AppPanel();
f.getContentPane().add(appPanel);
f.pack();
f.setVisible(true);
}
}
Obviously this is just a toy example - normally you'd want to separate your AppPanel out into a separate class file etc.....
Like PaĆ­lo Ebermann said developing your own canvas or any other component for the display is the best approach (according to my opinion).
Which component you choose also depends on the libraries you choose. If you only develop using Java2D standard Java components are enough. If you switch to LWJGL, JOGL or other libraries they often provide their own components.
Especially JOGL also provides an specific way to implement the game loop because it is driven by an Thread called Animator. If you want to have your design compatible with JOGL you will have to make your game loop compatible with this thread.

Categories

Resources