Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (778)
Games in Android Showcase (231)
games submitted by our members
Games in WIP (856)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Need help to structure my "laggy" graphic interface SWING  (Read 4008 times)
0 Members and 1 Guest are viewing this topic.
Offline ZyuR

Junior Newbie

« Posted 2012-05-23 06:57:16 »

Hello everybody,

first time posting on this forum, so please be nice   Cheesy.

I am programming a 2D tiles gamebut i have some probelms of lags. Ok, my computer is quite old, and the game works perfectly fine on gamer computer, but i amsure i have a problem in my interface architecture.

I tried to improve it several time and as far as i am, my architecture is the one below :

JFrame <- JLayeredPane <- JPanel

I use the JFrame to display the whole game. It implements a double buffering hardware based on a JLayeredPane.getGraphics()...

I use the JLayeredPaneto organize all my interface : show or hide the character inventory, craft interface, overview map, etc.

JPanel are used to paint all the interface components such as the global view, the charcater inventory, overview map, etc.
Each JPanel implements a software double buffering to update their image. They are also implementing the museListener for all the player actions.

Despite all that buffers, it is still laggy and not fluent.So i am sure there is a problem coming from the way i structured everything (for example, i guess the JLayredPane.getGraphics is not very fine because it use the EDT... but i don't know any other way to do it.)

Tkank's for you'r help and advices.

Please also excuse my english... i am doing my best
Offline 65K
« Reply #1 - Posted 2012-05-23 07:38:12 »

Do you use Swing components for the interface and let Swing repaint it all ?
Why additional panel double buffering if its enabled for the JFrame ?
Any complex logic anywhere ?

More overall information needed, I'd say.

Lethal Running - a RPG about a deadly game show held in a futuristic dystopian society.
Offline ZyuR

Junior Newbie

« Reply #2 - Posted 2012-05-23 12:52:04 »

Thank you for your answer.

There were a lot of lags at start (VisualVM showed that it was because of ImageFetcher and AWT EDT), and that is why I decided to improve the JPanel paint by using a double buffering software on it...
However, the problems remains the same without this doubleBuffering. If we forget about it you'll find below part of the code. I think the main problem come from calling the swing painting with layeredPane.getGraphics.
How could i do to avoid it ?

JFrame : 

 BufferStrategy strategy = w.getBufferStrategy();
RenderingThreatHardBuffer renderingThread;

JLayeredPane layeredPane;


RenderingThreadHardBuffer :

 while (true) {
    Graphics g = strategy.getDrawGraphics();

JPanel map;
JPanel interfaceComponent1;
JPanel interfaceComponent2;
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 65K
« Reply #3 - Posted 2012-05-23 13:43:45 »

Double buffering is to avoid flickering, not lagging.

while (true) {
    Graphics g = strategy.getDrawGraphics();

Is that original code ? Cause lines 2+3 have no effect.

Again, how do you draw the interface ? Swing, pixel juggling ? What are the panels for ?
Set ignore repaint on JFrame ?

Lethal Running - a RPG about a deadly game show held in a futuristic dystopian society.
Offline ZyuR

Junior Newbie

« Reply #4 - Posted 2012-06-04 18:08:58 »

Thank you for all your answers.
To simplify the code and take the problems stepby step, i tried to kept only the Frame and the buffer strategy.
Unfortunately, it seems i have another problem : the code behind should normally be working (as far I know...) unfortunatelly the line appears with a probability of 50%...

I tried it on a friend's computer and it is working perfectly fine... How is that possible ?

The main class
public class main {
   public static void main(String[] args) {
      Fenetre fen = new Fenetre();

The Frame class, "Fenetre"

   import java.awt.Cursor;
   import java.awt.Frame;
   import java.awt.Graphics;

   import java.awt.Image;
   import java.awt.Insets;
   import java.awt.Point;
   import java.awt.RenderingHints;
   import java.awt.Shape;
   import java.awt.Toolkit;
   import java.awt.Window;
   import java.awt.event.ActionEvent;
   import java.awt.event.ActionListener;
   import java.awt.event.ComponentEvent;
   import java.awt.event.ComponentListener;
   import java.awt.event.ItemEvent;
   import java.awt.event.WindowAdapter;
   import java.awt.event.WindowEvent;
   import java.awt.image.BufferStrategy;
   import java.util.Iterator;

   import javax.imageio.ImageIO;

   public class Fenetre extends Frame implements ComponentListener {

      int fenHaut = 0;
      int fenLarg = 0;
       BufferStrategy strategy;
       private Graphics           backBuffer          = null;
       private boolean            initiated           = false;

      public Fenetre() {
         fenHaut = 720;
         fenLarg = 950;
         this.setSize(fenLarg, fenHaut);

         this.addWindowListener(new WindowAdapter() {
              public void windowClosing(WindowEvent e) {

      public void setVisible(boolean b) {
           if (b & !initiated)

        * initialise l'affichage
       private void init() {
           strategy = getBufferStrategy();
           initiated = true;
      public void componentHidden(ComponentEvent arg0) {

      public void componentMoved(ComponentEvent arg0) {

      public void componentResized(ComponentEvent arg0) {

      public void componentShown(ComponentEvent arg0) {
      public void graphicalRender(){
         backBuffer = strategy.getDrawGraphics();

         backBuffer.drawString("Can you see me ?", 100, 200 );


Offline 65K
« Reply #5 - Posted 2012-06-04 19:10:55 »

You need a periodically called render loop.
Currently, you only render once right after setVisible().
At that time the UI stuff might not be necessarily ready to use, depending on your computer speed. Something like that, I' say.

Lethal Running - a RPG about a deadly game show held in a futuristic dystopian society.
Offline BoBear2681

JGO Coder

Medals: 19

« Reply #6 - Posted 2012-06-06 00:56:10 »

Don't forget that when posting code in these forums, Riven has very kindly provided an easy-peasy way to insert syntax highlighted code snippets (the button with the hash sign in the post editor).
Pages: [1]
  ignore  |  Print  

hadezbladez (377 views)
2018-11-16 13:46:03

hadezbladez (198 views)
2018-11-16 13:41:33

hadezbladez (380 views)
2018-11-16 13:35:35

hadezbladez (93 views)
2018-11-16 13:32:03

EgonOlsen (2195 views)
2018-06-10 19:43:48

EgonOlsen (2245 views)
2018-06-10 19:43:44

EgonOlsen (1387 views)
2018-06-10 19:43:20

DesertCoockie (2025 views)
2018-05-13 18:23:11

nelsongames (1691 views)
2018-04-24 18:15:36

nelsongames (2337 views)
2018-04-24 18:14:32
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46 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‑
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!