Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (524)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (592)
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  
  Problem when rendering on Graphics in a Frame and a JFrame is visible aside.  (Read 774 times)
0 Members and 1 Guest are viewing this topic.
Offline kaouete

Junior Newbie





« Posted 2006-04-11 16:34:57 »

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)

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  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
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 :]
Offline trembovetski

Senior Devvie




If only I knew what I'm talking about!


« Reply #1 - Posted 2006-04-11 17:43:00 »

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
Offline kaouete

Junior Newbie





« Reply #2 - Posted 2006-04-12 20:59:54 »

Ok, i understand now :]

thanks a lot for your help.
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.

toopeicgaming1999 (64 views)
2014-11-26 15:22:04

toopeicgaming1999 (57 views)
2014-11-26 15:20:36

toopeicgaming1999 (10 views)
2014-11-26 15:20:08

SHC (24 views)
2014-11-25 12:00:59

SHC (24 views)
2014-11-25 11:53:45

Norakomi (28 views)
2014-11-25 11:26:43

Gibbo3771 (24 views)
2014-11-24 19:59:16

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

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

digdugdiggy (52 views)
2014-11-12 21:11:50
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!