I'm new to Java and have just started making JFrames. I wrote a program for a game project I'm working on and would like help on where I can improve. I'm not new to programming, I also know HTML and Python well.

I believe one of the goals for all programmers is to make a useful/fun program as simple and short as possible.

I would like pointers/ideas/improvements for my Java program on how I can make it shorter, where I should aim for and how I can make it simpler.

package main;

import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class Game {

    //Set Variables to make it easier for myself
    public static double VER = 1.0;
    public static String STATE = "ALPHA";
    public static int WIDTH = 400;
    public static int HEIGHT = 400;
    public static int SCALE = 1;

    public static void main(String [ ] args) {
        //Create JFrame
        JFrame launcher = new JFrame("Infinite Doom " + STATE + " " + VER);     

        //Settings for JFrame
        launcher.setIconImage(new ImageIcon("Logo.png").getImage());  
        launcher.setSize(WIDTH, HEIGHT);

        //Check Screen Res and centre window
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        launcher.setLocation(dim.width/2-launcher.getSize().width/2, dim.height/2-launcher.getSize().height/2);
  • 3
    \$\begingroup\$ "as simple and short as possible", pffft... you apparently don't know me... \$\endgroup\$ Dec 17 '13 at 22:24

One way to improve your game is to actually have a game. =)

You should probably think about the design of your game a little more.

  1. What type of game are you actually going to make?
  2. What user interface elements does your game require?

Putting the JFrame logic into your main method seems like a bad idea no matter what you're planning on. You should either create a class that has a JFrame as a member data field or a class that extends from JFrame. This would make your code more modular.

  • \$\begingroup\$ Yeah am working on that XD \$\endgroup\$
    – genfy
    Dec 18 '13 at 17:47

Just about the only thing I could comment on is your indentation the last two brackets

        launcher.setLocation(dim.width/2-launcher.getSize().width/2, dim.height/2-launcher.getSize().height/2);

should be:

        launcher.setLocation(dim.width/2-launcher.getSize().width/2, dim.height/2launcher.getSize().height/2);

Your variables: VER, STATE, WIDTH, HEIGHT and SCALE could be set to final.

And last but maybe irrelevant. SCALE is not used, but maybe you're going to use it later. What do it know. :)

  • \$\begingroup\$ Yeah am planning to implement scale later in the project and i will fix the brackets and set the variables to final, thanks for the help! \$\endgroup\$
    – genfy
    Dec 18 '13 at 19:34
  • \$\begingroup\$ So I looked again at the project and the brackets where fine. Must have been a copy and paste thing. \$\endgroup\$
    – genfy
    Dec 19 '13 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.