1
\$\begingroup\$

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);     
        launcher.pack();

        //Settings for JFrame
        launcher.setVisible(true);
        launcher.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        launcher.setResizable(false);
        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);
         }
    }
\$\endgroup\$
1
  • 3
    \$\begingroup\$ "as simple and short as possible", pffft... you apparently don't know me... \$\endgroup\$ Commented Dec 17, 2013 at 22:24

2 Answers 2

3
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ Yeah am working on that XD \$\endgroup\$
    – genfy
    Commented Dec 18, 2013 at 17:47
4
\$\begingroup\$

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. :)

\$\endgroup\$
2
  • \$\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
    Commented Dec 18, 2013 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
    Commented Dec 19, 2013 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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