Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (590)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Newbie & Debugging Questions / Re: Java Sound API, Java Full Screen Exclusive Mode API & Linux on: 2010-07-13 18:22:35
After upgrading my system from Linux Mint 8 to Linux Mint 9, Full Screen Exclusive Mode API seems to be working fine (apart from the taskbar issue in XFCE at least).

 Huh


Anyway, seeing how Java game related APIs are dependent on system configuration, I think I will stick with Slick 2D for now and come back in the future.

Thanks everyone for your time and help provided.

Regards,
Charalampos
2  Game Development / Newbie & Debugging Questions / Re: Java Sound API, Java Full Screen Exclusive Mode API & Linux on: 2010-07-13 08:41:14
Thanks nsigma.
I will give it a shot and see what I can come up with  Smiley

Regards,
Charalampos
3  Game Development / Newbie & Debugging Questions / Re: Java Sound API, Java Full Screen Exclusive Mode API & Linux on: 2010-07-12 09:37:12
@nsigma
I am pretty sure there is an option somewhere but it's definitely not Compiz's option, as Compiz is not installed on my system.
It does seem to be an issue with XFCE. Fluxbox does not have that issue. I don't know about Gnome or KDE as they are not installed on my system.

@bobjob
Your last code runs much much better. Thank you.
Though I still believe full exclusive mode is the way to go. If not through pure Java, then through native code.

I gave Slick2D a try and the full screen works without any other hassle.
I think, for now, I will give up on Java2D and focus on creating games using a ready made game engine and later on after I gain some experience I will come back to do my own engine.

Regards,
Charalampos
4  Game Development / Newbie & Debugging Questions / Re: Java Sound API, Java Full Screen Exclusive Mode API & Linux on: 2010-07-10 23:16:22
Thanks for the above code bobjob.
In order to get decent FPS I have to turn off rendering hints (~10 FPS with rendering hints and ~80 FPS without) but I still get jerky move.
 Undecided
5  Game Development / Newbie & Debugging Questions / Re: Java Sound API, Java Full Screen Exclusive Mode API & Linux on: 2010-07-10 22:29:09
In Linux (Linux Mint 8 - XFCE), the above test case gives even worse results.
~50FPS in fake fullscreen, jerky movement and the task bar is still on top of the frame.
 Cry
6  Game Development / Newbie & Debugging Questions / Re: Java Sound API, Java Full Screen Exclusive Mode API & Linux on: 2010-07-09 20:25:15
Well...
I tried the scaling on the fly but I didn't like the results.

Here is the code I tried
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;
import java.awt.image.MemoryImageSource;
import java.awt.image.VolatileImage;

public class FakeFullscreen {
   static boolean isRunning;

   public static void main(String[] args) {
      final Toolkit toolkit = Toolkit.getDefaultToolkit();

      final int[] pixels = new int[16 * 16];
      final Image image = toolkit.createImage(new MemoryImageSource(16, 16, pixels, 0, 16));
      final Cursor invisibleCursor = toolkit.createCustomCursor(image, new Point(0, 0), "invisibleCursor");

      final Dimension screenSize = toolkit.getScreenSize();
      final Dimension gameSize = new Dimension(640, 480);
      final Rectangle gameArea = new Rectangle(0, 80, 640, 400);

      final Canvas canvas = new Canvas();
      canvas.setIgnoreRepaint(true);
      canvas.setSize(screenSize);

      final Frame frame = new Frame();
      frame.setAlwaysOnTop(true);
      frame.setCursor(invisibleCursor);
      frame.setIgnoreRepaint(true);
      frame.setLocation(0, 0);
      frame.setResizable(false);
      frame.setSize(screenSize);
      frame.setUndecorated(true);

      frame.addKeyListener(new KeyAdapter() {
         @Override
         public void keyPressed(KeyEvent event) {
            if (event.getKeyCode() == KeyEvent.VK_ESCAPE) {
               isRunning = false;
            }
         }
      });

      frame.add(canvas);
      frame.pack();
      frame.setVisible(true);

      canvas.createBufferStrategy(2);
      final BufferStrategy buffer = canvas.getBufferStrategy();

      final VolatileImage offScreen = canvas.createVolatileImage(gameSize.width, gameSize.height);
      Graphics2D offScreenGraphics = null;

      Graphics2D graphics = null;
      isRunning = true;

      int fps = 0;
      int frames = 0;
      long totalTime = 0;
      long curTime = System.nanoTime() / 1000000L;
      long lastTime = curTime;

      final int TICKS_PER_SECOND = 25;
      final int SKIP_TICKS = 1000 / TICKS_PER_SECOND;
      final int MAX_FRAMESKIP = 5;

      long nextGameTick = System.nanoTime() / 1000000L;
      int loops;
      float interpolation;

      int ballWidth = 8;
      int ballHeight = 8;
      int ballX = gameArea.width / 2 - ballWidth / 2;
      int ballY = gameArea.y + (gameArea.height / 2 - ballHeight / 2);
      int ballDx = 10;
      int ballDy = 10;

      while (isRunning) {
         loops = 0;
         while ((System.nanoTime() / 1000000L) > nextGameTick && loops < MAX_FRAMESKIP) {
            ballX += ballDx;
            ballY += ballDy;

            if (ballX <= gameArea.x) {
               ballX = gameArea.x;
               ballDx = -ballDx;
            } else if (ballX >= gameArea.width - ballWidth) {
               ballX = gameArea.width - ballWidth;
               ballDx = -ballDx;
            }

            if (ballY <= gameArea.y) {
               ballY = gameArea.y;
               ballDy = -ballDy;
            } else if (ballY >= gameArea.y + gameArea.height - ballHeight) {
               ballY = gameArea.y + gameArea.height - ballHeight;
               ballDy = -ballDy;
            }

            nextGameTick += SKIP_TICKS;
            loops++;
         }

         lastTime = curTime;
         curTime = System.nanoTime() / 1000000L;
         totalTime += curTime - lastTime;
         if (totalTime > 1000) {
            totalTime -= 1000;
            fps = frames;
            frames = 0;
         }
         ++frames;

         interpolation = (float) ((System.nanoTime() / 1000000L) + SKIP_TICKS - nextGameTick) / (float) (SKIP_TICKS);

         try {
            offScreenGraphics = offScreen.createGraphics();
            offScreenGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            offScreenGraphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
            offScreenGraphics.setColor(Color.BLACK);
            offScreenGraphics.fillRect(0, 0, screenSize.width, screenSize.height);
            offScreenGraphics.setColor(Color.WHITE);
            offScreenGraphics.drawString(String.format("FPS: %s", fps), 20, 20);

            int renderX = (int) (ballX + (ballDx * interpolation));
            int renderY = (int) (ballY + (ballDy * interpolation));

            offScreenGraphics.drawLine(gameArea.x, gameArea.y - ballHeight, gameArea.width, gameArea.y - ballHeight);
            offScreenGraphics.fillRect(renderX, renderY, ballWidth, ballHeight);

            graphics = (Graphics2D) buffer.getDrawGraphics();
            if (!offScreen.contentsLost()) {
               graphics.drawImage(offScreen, 0, 0, screenSize.width, screenSize.height, 0, 0, gameSize.width, gameSize.height, null);
            }

            if (!buffer.contentsLost()) {
               buffer.show();
            }

            Thread.yield();
         } finally {
            if (offScreenGraphics != null) {
               offScreenGraphics.dispose();
            }

            if (graphics != null) {
               graphics.dispose();
            }
         }
      }

      frame.setVisible(false);
      System.exit(0);
   }
}


I get about 200FPS on my laptop which runs on Windows XP but I get jerky move.
I haven't tested it yet on my desktop which has Linux, but I don't expect too much of a difference.

If I replace

1  
final Dimension screenSize = toolkit.getScreenSize();


with

1  
final Dimension screenSize = new Dimension(640, 480);


i.e. so as to have a normal frame and not a 'fake' fullscreen, I get ~700FPS and much better movement.
Either there is something wrong in my code or I didn't fully understand how to use scaling on the fly.

Perhaps giving out instructions on how to change xorg.conf so Java can use the Full Screen Exclusive Mode API might be a better idea than faking fullscreen mode?

Regards,
Charalampos
7  Game Development / Newbie & Debugging Questions / Re: Java Sound API, Java Full Screen Exclusive Mode API & Linux on: 2010-07-06 06:50:37
That didn't sound as hard as I was expecting  Tongue
Thanks for the tip  Smiley
8  Game Development / Newbie & Debugging Questions / Re: Java Sound API, Java Full Screen Exclusive Mode API & Linux on: 2010-07-05 18:58:56
Thanks for your reply.

Fullscreen 'cheat' is 'hard' to implement. If you design a game to work in, say, 640x480 mode and the current mode is 1024x768 you have to increase the size of the sprites or provide sprites for that mode. I think I will pass for now. I am too new to games development to go that route.

I have started to look at OpenGL. I was wondering though, can OpenGL handle fullscreen even if xorg.conf doesn't contain the necessary mode lines?

Regards,
Charalampos
9  Game Development / Newbie & Debugging Questions / Java Sound API, Java Full Screen Exclusive Mode API & Linux on: 2010-06-28 08:39:38
Dear all,

I suppose that Linux needs a 'specific' configuration in order for Sound & Full Screen Exclusive Mode APIs to work.
Specifically, when using Sound API I get a MidiUnavailableException because Java is trying to play sound through OSS and not through ALSA (I think).
Also, Full Screen Exclusive Mode API, reports that it can enter full screen but cannot change the display mode, which (again I think) there must be something 'wrong' with the configuration of X.

So, does anybody know how to make Sound API & Full Screen Exclusive Mode API work in Linux?

Regards,
Charalampos
10  Game Development / Newbie & Debugging Questions / Re: KeyListener and Linux 'erroneous' behaviour on: 2010-06-21 23:17:59
Trying to create a test case to report the bug I came up with... well... a working version...   Cheesy

This was the test case I created, which works fine!
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.awt.event.WindowListener;

public class TestCaseFrame extends Frame implements FocusListener, WindowFocusListener, WindowListener {
   TestCaseCanvas canvas;
   
   public TestCaseFrame() {
      addWindowListener(this);
     
      canvas = new TestCaseCanvas();
     
      setTitle("TestCaseFrame");
      setBounds(0, 0, 640, 480);
      setResizable(false);
      setLocationRelativeTo(null);
     
      add(canvas);
     
      pack();
      setVisible(true);
   }

   public static void main(String[] args) {
      EventQueue.invokeLater(new Runnable() {
         public void run() {
            new TestCaseFrame();
         }
      });
   }

   @Override
   public void focusGained(FocusEvent e) {
      canvas.requestFocusInWindow();
   }

   @Override
   public void focusLost(FocusEvent e) {}

   @Override
   public void windowGainedFocus(WindowEvent e) {
      canvas.requestFocusInWindow();
   }

   @Override
   public void windowLostFocus(WindowEvent e) {}

   @Override
   public void windowActivated(WindowEvent e) {
      canvas.requestFocusInWindow();
   }

   @Override
   public void windowClosed(WindowEvent e) {}

   @Override
   public void windowClosing(WindowEvent e) {}

   @Override
   public void windowDeactivated(WindowEvent e) {}

   @Override
   public void windowDeiconified(WindowEvent e) {
      canvas.requestFocusInWindow();
   }

   @Override
   public void windowIconified(WindowEvent e) {}

   @Override
   public void windowOpened(WindowEvent e) {}

}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.LinkedList;

public class TestCaseCanvas extends Canvas implements FocusListener, KeyListener, Runnable {
   final int MAX_FRAMESKIP;
   final int PHEIGHT;
   final int PWIDTH;
   final int SKIP_TICKS;
   final int TICKS_PER_SECOND;
   
   boolean running;
   Graphics dbg;
   Image dbImg;
   int loops;
   LinkedList<KeyEvent> keyEvents = new LinkedList<KeyEvent>();
   long nextGameTick = nextGameTick();
   Thread animator;
   
   public TestCaseCanvas() {
      TICKS_PER_SECOND = 300;
      SKIP_TICKS = 1000 / TICKS_PER_SECOND;
      MAX_FRAMESKIP = 5;
      PWIDTH = 640;
      PHEIGHT = 480;
     
      setPreferredSize(new Dimension(PWIDTH, PHEIGHT));
      setBounds(0, 0, PWIDTH, PHEIGHT);
      setIgnoreRepaint(true);
      setFocusable(true);
     
      addFocusListener(this);
      addKeyListener(this);
   }
   
   public void addNotify() {
      super.addNotify();

      if (animator == null) {
         animator = new Thread(this);
      }
      startAnimator();
   }
   
   @Override
   public void focusGained(FocusEvent e) {
      System.out.println("I received focus");
   }

   @Override
   public void focusLost(FocusEvent e) {}

   @Override
   public void keyPressed(final KeyEvent e) {
      EventQueue.invokeLater(new Runnable() {
         public void run() {
            keyEvents.add(e);
         }
      });
   }

   @Override
   public void keyReleased(KeyEvent e) {}

   @Override
   public void keyTyped(KeyEvent e) {}
   
   static long nextGameTick() {
      return System.nanoTime() / 1000000L;
   }
   
   void processKeyEvents() {
      KeyEvent e = keyEvents.poll();
      if (e != null) {
         System.out.println("Processing key event: " + e);
      }
   }
   
   void render() {
      try {
         if (dbImg == null) {
            dbImg = getGraphicsConfiguration().createCompatibleImage(PWIDTH, PHEIGHT);
         }
         
         if (dbg == null) {
            dbg = dbImg.getGraphics();
         }
         
         dbg.setColor(Color.BLACK);
         dbg.fillRect(0, 0, PWIDTH, PHEIGHT);
         dbg.setColor(Color.WHITE);
         dbg.drawString("This is a test case", 20, 20);
         
         Graphics g = getGraphics();
         g.drawImage(dbImg, 0, 0, PWIDTH, PHEIGHT, this);
         Toolkit.getDefaultToolkit().sync();
         g.dispose();
      }
      catch (Exception e) {
         System.out.println("Graphics error: " + e.getLocalizedMessage());
      }
   }
   
   public void run() {
      while (running) {
         loops = 0;
         while (nextGameTick() > nextGameTick && loops < MAX_FRAMESKIP) {
            if (keyEvents.size() > 0) {
               processKeyEvents();
            }
            nextGameTick += SKIP_TICKS;
            loops++;
         }
         
         render();
         
         Thread.yield();
      }
     
      animator = null;
   }
   
   void startAnimator() {
      running = true;
      animator.start();
   }
}


Going through the previous test case, I had a thought that the FocusListener was not working as it should, hence the KeyListener was never getting the KeyEvents.
Changing the PongPanel class with the following code, it fixes the 'erroneous' behaviour
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
public class PongPanel extends BasePanel implements FocusListener {
   public PongPanel(int width, int height, int updatesPerSecond, int maxFrameSkip) {
      super(width, height, updatesPerSecond, maxFrameSkip);
      addFocusListener(this);
   }

   @Override
   public void focusGained(FocusEvent e) {
      System.out.println("I have the focus");
      super.requestFocusInWindow();
   }

   @Override
   public void focusLost(FocusEvent e) {
      System.out.println("I lost the focus");
   }
}


Do I feel a little tiny bit wiser or do I?  Tongue

Onwards to make Java play sounds in Linux  persecutioncomplex

Regards,
Charalampos
11  Game Development / Newbie & Debugging Questions / Re: KeyListener and Linux 'erroneous' behaviour on: 2010-06-20 17:23:01
Does it really sound like a Java bug?
I'm not 100% sure.
It might be my code.
12  Game Development / Newbie & Debugging Questions / Re: KeyListener and Linux 'erroneous' behaviour on: 2010-06-20 07:50:05
Looks like if I remove the key listener from the canvas/panel when the top frame is deactivated and then add it again when the top frame is activated then the 'erroneous' behaviour disappears.

Regards,
Charalampos
13  Game Development / Newbie & Debugging Questions / KeyListener and Linux 'erroneous' behaviour on: 2010-06-15 20:09:41
Hello everyone,

I am trying to develop a pong clone, using pure Java 2D (i.e. no additional libraries) and Swing/AWT.
Everything works fine for now apart from the following:
When I minimize the game's frame (a JFrame and no full screen exclusive mode yet), and leave it minimized for, say, 20 minutes, after restoring the frame it looks like the JPanel inside the JFrame does not receive key events through KeyListener.
I have also tried to use Frame as the top most window and Canvas instead of a JPanel, but I get the same behaviour.
The 'erroneous' (?) behaviour exists only in linux (specifically Linux Mint 8 with XFCE).

The KeyListener (which is added to the panel/canvas) adds key events to a queue (implemented with LinkedList) and my game loop retrieves the first key event in queue and processes it (if exists) in each game update cycle.
I added a FocusListener, a WindowFocusListener and a WindowListener to the frame so as when it gets the focus or gets activated (or deiconified), I transfer the focus to the panel/canvas (with requestFocusInWindow()).
I also added a FocusListener on the panel/canvas to see if it does gets the focus.

What I have seen as an 'erroneous' behaviour is that, after the frame has been minimized for 20 minutes and then restored, the focus seems to be transferred to the panel/canvas but the KeyListener does not seem to get any key events.

I would really appreciate if you can help me to solve this problem.
Also, I would really like not to use any game engine as of now.

Here is some code that might help you understand a little bit more of how my game works.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
public class Pong extends Frame implements FocusListener, WindowListener, WindowFocusListener {
   private final PongPanel pp;
   
   public Pong() {
      addFocusListener(this);
      addWindowListener(this);
      addWindowFocusListener(this);
     
      pp = new PongPanel(640, 480, 300, 5);
     
      setBounds(0, 0, 640, 480);
      setResizable(false);
      setLocationRelativeTo(null);
     
      add(pp);
     
      pack();
      setVisible(true);
     
      pp.requestFocusInWindow();
   }
   
   @Override
   public void focusGained(FocusEvent e) {
      EventQueue.invokeLater(new Runnable() {
         public void run() {
            pp.requestFocusInWindow();
         }
      });
   }

   @Override
   public void focusLost(FocusEvent e) {}

   public static void main(String[] args) {
      EventQueue.invokeLater(new Runnable() {
         public void run() {
            new Pong();
         }
      });
   }

   @Override
   public void windowGainedFocus(WindowEvent e) {
      EventQueue.invokeLater(new Runnable() {
         public void run() {
            pp.requestFocusInWindow();
         }
      });
   }

   @Override
   public void windowLostFocus(WindowEvent e) {}

   @Override
   public void windowActivated(WindowEvent e) {
      EventQueue.invokeLater(new Runnable() {
         public void run() {
            pp.requestFocusInWindow();
         }
      });
   }

   @Override
   public void windowClosed(WindowEvent e) {}

   @Override
   public void windowClosing(WindowEvent e) {}

   @Override
   public void windowDeactivated(WindowEvent e) {}

   @Override
   public void windowDeiconified(WindowEvent e) {
      EventQueue.invokeLater(new Runnable() {
         public void run() {
            pp.requestFocusInWindow();
         }
      });
   }

   @Override
   public void windowIconified(WindowEvent e) {}

   @Override
   public void windowOpened(WindowEvent e) {
      EventQueue.invokeLater(new Runnable() {
         public void run() {
            pp.requestFocusInWindow();
         }
      });
   }
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
public class PongPanel extends BasePanel implements FocusListener {
   public PongPanel(int width, int height, int updatesPerSecond, int maxFrameSkip) {
      super(width, height, updatesPerSecond, maxFrameSkip);
      addFocusListener(this);
   }

   @Override
   public void focusGained(FocusEvent e) {
      System.out.println("I have the focus");
   }

   @Override
   public void focusLost(FocusEvent e) {
      System.out.println("I lost the focus");
   }
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
public class BasePanel extends Canvas implements KeyListener, Runnable {   
   protected final int MAX_FRAMESKIP;
   protected final int PHEIGHT;
   protected final int PWIDTH;
   protected final int SKIP_TICKS;
   protected final int TICKS_PER_SECOND;

   protected boolean running;
   protected int loops;
   protected LinkedList<KeyEvent> keyEvents = new LinkedList<KeyEvent>();
   protected long nextGameTick = nextGameTick();
   protected Thread animator;
   
   public BasePanel(int width, int height, int updatesPerSecond, int maxFrameSkips) {
      super();
     
      TICKS_PER_SECOND = updatesPerSecond;
      SKIP_TICKS = 1000 / TICKS_PER_SECOND;
      MAX_FRAMESKIP = maxFrameSkips;
      PWIDTH = width;
      PHEIGHT = height;
     
      super.setPreferredSize(new Dimension(PWIDTH, PHEIGHT));
      super.setBounds(0, 0, PWIDTH, PHEIGHT);
      super.setIgnoreRepaint(true);
      super.setFocusable(true);
     
      super.addKeyListener(this);
   }
   
   public void addNotify() {
      super.addNotify();

      if (animator == null) {
         animator = new Thread(this);
      }
      startAnimator();
   }
   
   private void baseRender(float interpolation) {}
   private void baseUpdate() {}
   
   @Override
   public void keyPressed(final KeyEvent e) {
      EventQueue.invokeLater(new Runnable() {
         public void run() {
            keyEvents.add(e);
         }
      });
   }

   @Override
   public void keyReleased(KeyEvent e) {}

   @Override
   public void keyTyped(KeyEvent e) {}
   
   public static final long nextGameTick() {
      return System.nanoTime() / 1000000L;
   }
   
   protected void processKeyEvents() {}
   protected void render(float interpolation) {}
   
   public void run() {
      while (running) {
         loops = 0;
         while (nextGameTick() > nextGameTick && loops < MAX_FRAMESKIP) {
            if (keyEvents.size() > 0) {
               processKeyEvents();
            }
            baseUpdate();
         }
         
         baseRender(0.0f);

         
         Thread.yield();
      }
   }
   
   private void startAnimator() {
      running = true;
      animator.start();
   }
   
   protected void update() {}
}


Kind Regards,
Charalampos
Pages: [1]
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

trollwarrior1 (28 views)
2014-11-22 12:13:56

xFryIx (70 views)
2014-11-13 12:34:49

digdugdiggy (49 views)
2014-11-12 21:11:50

digdugdiggy (43 views)
2014-11-12 21:10:15

digdugdiggy (37 views)
2014-11-12 21:09:33

kovacsa (61 views)
2014-11-07 19:57:14

TehJavaDev (65 views)
2014-11-03 22:04:50

BurntPizza (63 views)
2014-11-03 18:54:52

moogie (79 views)
2014-11-03 06:22:04

CopyableCougar4 (78 views)
2014-11-01 23:36:41
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!