Is it a bad idea to create a game in pure java? | SoloLearn: Learn to code for FREE!

+17

Is it a bad idea to create a game in pure java?

I watched a game tutorial for eclipse and it took 200 lines of code just to display a blank window, the code consisted of threads, passing objects to methods, using keywords "this" and "new here there and everywhere, inserting peices of code which he didnt really understand but made the code work, it was just horrible.... I have studied java for 2 years and made nothing but a few sl codes I studied javascript for 2 weeks and made a game and published to playstore I'm I wasting me time with java? I cant seem to get the whole picture of the language in my mind just seems overly complicated and not always 100% sure what going on behind the scenes with without have to read source code. Sorry for the rant lol but just wondering if anyone find this language a never ending story.๐Ÿ˜… My js game https://play.google.com/store/apps/details?id=com.chaosgamez.magmabat

1/25/2021 6:27:19 PM

D_Stark

31 Answers

New Answer

+7

lmao at "200 lines for basic window" because I believe that's true! In my experience, Java has been the most heavily typed (physically speaking) for game programming (including Dalvik Java). I used awt and swing btw The good of using Java is it already has everything you need. Threads, timers, window, graphics library, file reader, etc. The bad is it requires a lot of typing just for basics (yup even for the basic game loop and window). I believe this also has to do with the fact that Java forces us to use classes and class techniques. Starting out with game dev i strictly used classes and OOP but now I'm liking the functional approach. One reason why i quit Java a few years ago. Finally, Java isn't as slow as people make it out to be; sure it's slower than others but if you code it right/optimal you won't have a problem. TL;DR From my experience, Java isn't a bad language for games, but there are way better alternatives.

+14

Java is perfectly capable of creating games without Spring MVC. Have you ever heard of Minecraft? If you have studied Java for two years you must have come across the Java Swing and JavaFX APIs for GUI development. The use of "this" and "new" keywords are standard programming practices in Java. I have no idea which video you were watching but I can put a basic Window (JFrame) on the screen in a few lines of code. They were more than likely starting a thread to build the GUI to keep the application responsive. If as you say they were "inserting peices of code which he didnt really understand but made the code work" then whoever they are they don't know what they are doing. They have probably just copied and pasted code from something that worked and pasted it into their own code without knowing what it does. Java has 2D and 3D graphics APIs in the Java Standard Edition. There is also the JMonkey Engine, a game engine written in Java. Sounds like you spent two years learning the wrong stuff. https://docs.oracle.com/javase/tutorial/uiswing/ https://docs.oracle.com/javafx/2/get_started/jfxpub-get_started.htm https://docs.oracle.com/javase/8/javase-clienttechnologies.htm https://docs.oracle.com/javase/8/docs/technotes/guides/2d/ https://www.oracle.com/java/technologies/javase/java-3d.html https://jmonkeyengine.org/ However, if you're more comfortable with JavaScript then you do you Boo.

+7

Avinesh, I apologise for misquoting you but saying "Java in itself is not capable of doing a lot of stuff without supporting API's" is just misrepresentation of the language. The Java Standard Edition Software Development Kit contains the standard APIs. They are part of the language not third party addons like jQuery, backbone, express, and Spring are with JavaScript. You can do a lot more with Java "out of the box" without resorting to additional libraries than you can do with JavaScript. Though to be fair JavaScript was never designed to be as scalable as Java. The two languages are not even designed for the same purpose. As you yourself say "JavaScript is weird". This is due to having to maintain backwards compatibility with older code because of design decisions made in the past. I recommend "JavaScript: The Good Parts" by Douglas Crockford. https://www.oreilly.com/library/view/javascript-the-good/9780596517748/

+6

Martin Taylor thanks, I will try this tommrow I have watched a few tutorials now and they have all diffrent ways of creating the window, I think my problem is understanding how threads work I understand that the main method is called by a thread but then I'm thinking why do I need a thread to call my method if i can just call it my self ๐Ÿ˜… because in javascript this is all done on one thread were methods just call them selfs. did you say 200 lines of code indeed because it's true?

+6

D_Stark, Yes I was being sarcastic. I don't doubt that 200 lines of code were used to create an apparently blank window but it isn't necessary to get a window onto the screen. The code in the video could be setting a lot of other things up in the background, such as a game engine, for all I know. However, this is the absolute basics. Start simple and build on that. Your confusion is understandable. Java is a big language and there are multiple ways of doing things. Basically there are two ways of creating your window (JFrame class or subclass). 1. Create a class that extends the JFrame class as we have done here. public class GUITest001 extends JFrame 2. Create an instance of the JFrame class from within your class and display that. There are also two methods for calling the constructor. A. Invoking the constructor directly as I have done in the example above. new GUITest001(); B. Invoking the constructor in it's own thread, which is presumably what is happening in the video you mentioned. This gives four possible combinations. 1A - derived class constructed in main thread. As in GUITest001. 1B - derived class constructed in separate thread. 2A - JFrame constructed in main thread. 2B - JFrame constructed in separate thread. With this size of a program it makes no difference which way you do it so don't sweat it for now. I will put some more examples together for you tomorrow to demonstrate 1B, 2A, and 2B.

+6

Most of my university course work to date has involved using Java Swing. Iโ€™ve had to build several games with Swing for various assignments, Tetris, Tic-Tac-Toe, Clue, etc. Game development with Java is absolutely something you can do. Martin Taylor is 100% correct and has given you some excellent examples to get started with. If you need more examples I can share some of my assignments with you as well.

+5

D_Stark, this is how simple a GUI application is in Java... import javax.swing.*; public class GUITest001 extends JFrame { public GUITest001() { // Default constructor super("GUI Test 001"); setSize(640, 480); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public static void main(String[] args) { // Initiate program new GUITest001(); } } Save the above code in a Java source file called GUITest001.java compile it to produce GUITest001.class then invoke it with java GUITest001 or javaw GUITest001 200 lines of code indeed! Edit: Don't try this in the SoloLearn Code Playground. It won't work. This code is intended for desktop class machines only. Can somebody test it on a Raspberry Pi, I don't know if they support this.

+5

D_Stark, no I have not made any apps with Java. I lost interest in Android development after Android 4.x messed up access to external sdcards. It just isn't suitable for most of the stuff I do. I've messed around with it but my enthusiasm has gone. I'm from an electronics background so most of the code I write is either embedded code or desktop programs for interfacing with hardware. I used to write all of my interface applications in Java to make them cross platform but even that got hurt when the javax.comms package got dropped by Sun microsystems. Yes I know there are alternatives but once your product has to rely on open source projects management get twitchy, no matter how much you explain it to them. Yes I also know the weebs want everything to be IoT and connected by wifi but in the cost sensitive world of commercial products having to add a wifi interface and the additional ram and cpu power to implement a wifi stack will have the accounts manager foaming at the mouth. You can only get away with that in very high volumes.

+5

Java GUI Example 2. Creating a JFrame from the main thread. import javax.swing.*; public class GUITest002 { public GUITest002() { // Default Constructor JFrame frame = new JFrame("GUI Test 002"); frame.setSize(320, 240); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public static void main(String[] args) { // Initiate program GUITest002 gt = new GUITest002(); } }

+5

Java GUI Example 3. Creating a derived class in a separate thread. import javax.swing.*; public class GUITest003 extends JFrame { public GUITest003() { // Default constructor super("GUI Test 003"); setSize(640, 480); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public static void main(String[] args) { // Initiate program javax.swing.SwingUtilities.invokeLater(new Runnable(){ public void run(){ GUITest003 gt = new GUITest003(); } }); } }

+5

Java GUI Example 4. Creating a JFrame in a separate thread. import javax.swing.*; public class GUITest004 { public GUITest004() { // Default Constructor JFrame frame = new JFrame("GUI Test 004"); frame.setSize(640, 480); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public static void main(String[] args) { // Initiate program javax.swing.SwingUtilities.invokeLater(new Runnable(){ public void run(){ GUITest004 gt = new GUITest004(); } }); } }

+5

Anonyme Anonyme, If high performance 3D graphics are required, which a lot of mobile games do not require, you have two additional options. 1. Java OpenGL (JOGL) https://jogamp.org/jogl/www/ 2. C/C++ via the Java Native Interface (JNI) https://docs.oracle.com/javase/8/docs/technotes/guides/jni/ . Android provides the Native Development Kit (NDK) for this purpose https://developer.android.com/ndk Most people requiring high performance 3D graphics are not going to write their own engine. Instead they will use an off the shelf game engine that supports their target platform, for example Unity https://docs.unity3d.com/Manual/android-sdksetup.html this usually involves using the JNI/NDK as can be seen in the link provided.

+5

D_Stark Can I have the link of your game published to play store? I want to play your game~ It's a webview or coded with ReactNative?

+3

Avinesh, the Java Standard Edition API contains over 5,000 classes. If you think Java in itself can't do much you need to read the API documentation. The Java APIs cover GUI frameworks, 2D and 3D graphics, databases, encryption, networking, file I/O, compression and much more. There is more to Java than the basic keywords taught on SoloLearn.

+3

Instead java use kotlin As kotlin is now the official language for android

+3

D_Stark Sir, can you please share the link of your Javascript game ? I also want to make one ๐Ÿ™„ Haven't you use HTML and CSS in it ? I have not much know on Java, so I also don't know it ๐Ÿ˜…

+3

Hey โ„๐•’๐•ฃ๐•ฃ๐•ช ๐Ÿ’• ๐•ป๐–”๐–™๐–™๐–Š๐–— โšกโœจ Gordon , if your following this thread I put a link to the game in the question, I made in pure javascript no css then uploaded to playstore.

+2

IMHO Java is better for enterprise software and Android development. If your game is for Android I don't think it's a bad idea but you also have other alternatives like ReactNative if JS is your thing.

+2

Martin Taylor quote me right. I said use Spring MVC for web applications. When I said pure Java, I only meant OOP. Also I have clearly stated regarding the supporting API's in the very 1st sentence. You might know the language a lot better than I do but I try my best to not put forward misleading answers.

+2

Martin Taylor have you made any apps with java?