4K - how do I do?

I have now decided to make a 4k game, but I dont know how to make the gui or what to call it.

I usually run a main method which creates a new JFrame.
Then I make a class file name GUI extending JPanel an add it to the frame.

the GUI controls the entire game then.

But how is the cheapest way (of size) to do this?

And is it alowed to extend other classes?

This is how I have made the basics of my 4k game. What should I do better since its already at 1.87k?



public class A extends JPanel implements ActionListener, KeyListener{
      int i = 0;
      public static void main(String[] args) {
            
            A a = new A();
            a.addKeyListener(a);
            a.setPreferredSize(new Dimension(600,400));
            a.setBackground(Color.black);
            a.setFocusable(true);
            
            JFrame frame = new JFrame("");
            frame.getContentPane().add(a);
            frame.pack();
            frame.setVisible(true);
            frame.setResizable(false);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            Timer timer = new Timer(20,a);
            timer.start();
      }
      public void paint(Graphics g){
            super.paint(g);
            g.drawLine(i,3,100,100);
      }
      public void keyTyped(KeyEvent arg0) {
      }
      public void keyPressed(KeyEvent arg0) {
            //Keys
            i-=100;
      }
      public void keyReleased(KeyEvent arg0) {
      }
      public void actionPerformed(ActionEvent arg0) {
            //Game loop
            i++;
            repaint();
      }
}


The i variable is just for testing the game loop and the keys

http://indiegamedev.tucows.com/blog/_archives/2005/1/18/263754.html

Just look into the 2d section of Andrew’s online book and use his 2d game engine. Its more then enough for a 4k game.

[quote]I have now decided to make a 4k game, but I dont know how to make the gui or what to call it.

I usually run a main method which creates a new JFrame.
Then I make a class file name GUI extending JPanel an add it to the frame.

the GUI controls the entire game then.

But how is the cheapest way (of size) to do this?

And is it alowed to extend other classes?

This is how I have made the basics of my 4k game. What should I do better since its already at 1.87k?



public class A extends JPanel implements ActionListener, KeyListener{
      int i = 0;
      public static void main(String[] args) {
            
            A a = new A();
            a.addKeyListener(a);
            a.setPreferredSize(new Dimension(600,400));
            a.setBackground(Color.black);
            a.setFocusable(true);
            
            JFrame frame = new JFrame("");
            frame.getContentPane().add(a);
            frame.pack();
            frame.setVisible(true);
            frame.setResizable(false);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            Timer timer = new Timer(20,a);
            timer.start();
      }
      public void paint(Graphics g){
            super.paint(g);
            g.drawLine(i,3,100,100);
      }
      public void keyTyped(KeyEvent arg0) {
      }
      public void keyPressed(KeyEvent arg0) {
            //Keys
            i-=100;
      }
      public void keyReleased(KeyEvent arg0) {
      }
      public void actionPerformed(ActionEvent arg0) {
            //Game loop
            i++;
            repaint();
      }
}


The i variable is just for testing the game loop and the keys
[/quote]
Have as little code in the main method as possible. I found that method calls from that method were more expensive (I think because main is static, it has to store more class info to make the call).

Reduce the amount of calls you are making – is frame.pack() needed for example?

Extending JPanel and implementing your listeners is good, that’s what I did.

Will.

re: As little as possable in main
Odd, as I’ve found I gained a good 28 bytes when I moved the game code into the ctor, changed all the methods to none-static, and changed main to new Mlk3D().

[quote]re: As little as possable in main
Odd, as I’ve found I gained a good 28 bytes when I moved the game code into the ctor, changed all the methods to none-static, and changed main to new Mlk3D().
[/quote]
I assume by gained you mean increased (I normally take gained to mean I’ve got an extra 28 bytes to play with :)).

I guess it depends on what the method calls were. In my case, my class implements JPanel, and it is far more expensive calling those JPanel methods from the main method than it is from my class.

Will.