Problem when rendering on Graphics in a Frame and a JFrame is visible aside.

Hi,

i am completely new to the game dev world and i want to make a little game that have 2 frame :
a Frame that render the game (it is a map containing things)
a JFrame to play or pause the game.
(so there is no interaction between the user and the Frame, the Frame is just here to show the user how the map)

I tried to begin a little thing with GAGE2D and this is what i have done.
I have one problem,
while the map is rendered on the Frame i dont have access to the JFrame who is completely grey and doesnt respond.

what i want is to be able to click on a button in the JFrame while the Frame is rendered.

(you can note that the gu was made with netbeans)


package up5.mi.etu.gl.matribux;

import com.dnsalias.java.gage.map.*;
import javax.imageio.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;

public class MaTribuXUI extends javax.swing.JFrame {
    
    /** Creates new form MaTribuXUI */
    public MaTribuXUI() {
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">
    private void initComponents() {
        jPanel1 = new javax.swing.JPanel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jTabbedPane2 = new javax.swing.JTabbedPane();
        jTabbedPane3 = new javax.swing.JTabbedPane();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jTabbedPane1.addTab("tab1", jTabbedPane2);

        jTabbedPane1.addTab("tab2", jTabbedPane3);

        jButton1.setText("jButton1");

        jButton2.setText("jButton2");

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jButton1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jButton2)
                .addContainerGap(339, Short.MAX_VALUE))
            .add(jTabbedPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 533, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jTabbedPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 252, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton2)
                    .add(jButton1)))
        );

        jMenu1.setText("Menu");
        jMenuItem1.setText("Item");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });

        jMenu1.add(jMenuItem1);

        jMenuItem2.setText("Item");
        jMenu1.add(jMenuItem2);

        jMenuItem3.setText("Item");
        jMenu1.add(jMenuItem3);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pack();
    }// </editor-fold>

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
        nouvellePartie();
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MaTribuXUI().setVisible(true);
            }
        });
    }
    
    private static Partie partie;
    private static MaTribuXFrame frame;
    
    private boolean running = true;
    private static Image background;
    //private static String[] tileNames = {"tile1.png", "tile2.png", "tile3.png", "tile4.png", "tile5.png", "tile6.png"};
    private static String[] tileNames = {};
    private static int[] ids = new int[tileNames.length];
    
    public void nouvellePartie() {
        TileManager manager = new TileManager(64, 64);
        Parallax parallax = new Parallax();
        Map map = new Map(manager, 64, 64, 640, 480);
        Map map2 = new Map(manager, 64, 64, 640, 480);
        
        try
        {
            background = ImageIO.read(MaTribuXUI.class.getResource("/images/background.png"));
            Image[] images = new Image[tileNames.length];
        
            for(int i=0; i<images.length; i++)
            {
                images[i] = Toolkit.getDefaultToolkit().getImage(MaTribuXUI.class.getResource("/images/"+tileNames[i]));
            }
            for(int i=0; i<images.length; i++) ids[i] = manager.addTile(images[i]);
            
        }catch(Exception e)
        {
            e.printStackTrace();
            
            System.out.println("Some or all of the images are missing!");
            System.exit(0);
        }
        
        map2.setBackground(background);
        
        parallax.addMap(map2);
        parallax.addMap(map);    
        
        partie = new Partie(64,64);
        
        frame = new MaTribuXFrame();
        frame.init();

        // main loop
        while(running) {
            frame.run(parallax);
            try
            {
                Thread.sleep(1000);
            }catch(Exception e) {
                
            }
        }
    }

    
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTabbedPane jTabbedPane2;
    private javax.swing.JTabbedPane jTabbedPane3;
    // End of variables declaration
    
    private class MaTribuXFrame {
        private Frame frame = new Frame();
        private DisplayMode oldDisplayMode;
        private BufferStrategy strategy;
        
        public void init() {
            frame.setBounds(0,0,640,480);
            frame.setVisible(true);
            frame.createBufferStrategy(2);
            strategy = frame.getBufferStrategy();
            frame.addKeyListener(this);
        }
        
        public void run(Parallax map) {
             Graphics g = strategy.getDrawGraphics();
             g.setColor(Color.white);
             map.render(g);
             g.dispose();
             strategy.show(); 
        }
    }
}

thanks you for your help :]

Your nouvellePartie() method which does the rendering, is called on the event dispatch thread,
which is a big no-no. Since your method is blocking the EDT, the JFrame can’t get repainted.

Dmitri

Ok, i understand now :]

thanks a lot for your help.