I'm new to Java and have just started making JFrame
s. 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);
}
}
-
3\$\begingroup\$ "as simple and short as possible", pffft... you apparently don't know me... \$\endgroup\$Simon Forsberg– Simon Forsberg2013年12月17日 22:24:57 +00:00Commented Dec 17, 2013 at 22:24
2 Answers 2
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.
- What type of game are you actually going to make?
- 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– genfy2013年12月18日 17:47:06 +00:00Commented Dec 18, 2013 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– genfy2013年12月18日 19:34:37 +00:00Commented 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– genfy2013年12月19日 16:28:23 +00:00Commented Dec 19, 2013 at 16:28