Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (537)
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 1816 times)
0 Members and 1 Guest are viewing this topic.
Offline ZyuR

Junior Newbie





« Posted 2012-05-23 08: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 09: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.

Offline ZyuR

Junior Newbie





« Reply #2 - Posted 2012-05-23 14: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 : 

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


JLayeredPane layeredPane;

--------

RenderingThreadHardBuffer :



 while (true) {
    Graphics g = strategy.getDrawGraphics();
    layeredPane.getGraphics();
    strategy.show();
 }

JLayeredPane
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 15:43:45 »

Double buffering is to avoid flickering, not lagging.

while (true) {
    Graphics g = strategy.getDrawGraphics();
    layeredPane.getGraphics();
    strategy.show();
 }

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 ?



Offline ZyuR

Junior Newbie





« Reply #4 - Posted 2012-06-04 20: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.io.File;
   import java.io.IOException;
   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.setLocationRelativeTo(null);
         this.setResizable(true);
         this.setTitle("Alphamega");
         
         addComponentListener(this);

         this.addWindowListener(new WindowAdapter() {
            
              public void windowClosing(WindowEvent e) {
              System.exit(0);
              }
          });
         this.setVisible(true);
         graphicalRender();
         
      }

      public void setVisible(boolean b) {
           super.setVisible(b);
           if (b & !initiated)
               init();
       }

       /**
        * initialise l'affichage
        */
       private void init() {
          
           setIgnoreRepaint(true);
           createBufferStrategy(2);
           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 );
         backBuffer.dispose();
         strategy.show();
      }
      
      
      

      

}
Offline 65K
« Reply #5 - Posted 2012-06-04 21: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.

Offline BoBear2681

JGO Coder


Medals: 18



« Reply #6 - Posted 2012-06-06 02: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  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

CogWheelz (16 views)
2014-08-01 22:53:16

CogWheelz (15 views)
2014-08-01 22:51:43

CopyableCougar4 (18 views)
2014-08-01 19:37:19

CogWheelz (19 views)
2014-07-30 21:08:39

Riven (27 views)
2014-07-29 18:09:19

Riven (16 views)
2014-07-29 18:08:52

Dwinin (14 views)
2014-07-29 10:59:34

E.R. Fleming (42 views)
2014-07-29 03:07:13

E.R. Fleming (13 views)
2014-07-29 03:06:25

pw (44 views)
2014-07-24 01:59:36
Resources for WIP games
by CogWheelz
2014-08-01 18:20:17

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

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

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22
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!