Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
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  
  game starts nothing moves, lots of errors!!!  (Read 1254 times)
0 Members and 1 Guest are viewing this topic.
Offline WolfAlvein

Senior Newbie





« Posted 2012-01-04 22:36:58 »

Hi everyone, first of all I'm going to present myself, I'm Andres Scarpone and i live in Venezuela currently trying to learn Video Game programing, and with lots of questions and problems in the tutorial I'm doing...

Well for all of you the tutorial I'm doing and where I have learned almost everything is from the book: Beginning JAVA SE 6 Game programming, I'm almost at the end of the book with the last project but instead of making it in a applet i tried to build it in the JFrame... and got lots of problems, I'll post the code, if someone can help me out I would really appreciate it thanks.

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  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235  
236  
237  
238  
239  
240  
241  
242  
243  
244  
245  
246  
247  
248  
249  
250  
251  
252  
253  
254  
255  
256  
257  
258  
259  
260  
261  
262  
263  
264  
265  
266  
267  
268  
269  
270  
271  
272  
273  
274  
275  
276  
277  
278  
279  
280  
281  
282  
283  
284  
285  
286  
287  
288  
289  
290  
291  
292  
293  
294  
295  
296  
297  
298  
299  
300  
301  
302  
303  
304  
305  
306  
307  
308  
309  
310  
311  
312  
313  
314  
315  
316  
317  
318  
319  
320  
321  
322  
323  
324  
325  
326  
327  
328  
329  
330  
331  
332  
333  
334  
335  
336  
337  
338  
339  
340  
341  
342  
343  
344  
345  
346  
347  
348  
349  
350  
351  
352  
353  
354  
355  
356  
357  
358  
359  
360  
361  
362  
363  
364  
365  
366  
367  
368  
369  
370  
371  
372  
373  
374  
375  
376  
377  
378  
379  
380  
381  
382  
383  
384  
385  
386  
387  
388  
389  
390  
391  
392  
393  
394  
395  
396  
397  
398  
399  
400  
401  
402  
403  
404  
405  
406  
407  
408  
409  
410  
411  
412  
413  
414  
415  
416  
417  
418  
419  
420  
421  
422  
423  
424  
425  
426  
427  
428  
429  
430  
431  
432  
package guerragalactica;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;

public class GuerraGalactica extends JComponent implements Runnable, KeyListener
{
   
    //Variables
   static int ancho = 800;
    static int alto = 600;
    static int centroX = ancho / 2;
    static int centroY = alto / 2;
    static int asteroidesMax = 10;
    static int balasMax = 10;
    static int velBalas = 4;
    static double aceleracion = 0.05;
    static int spriteNorm = 0;
    static int spriteChoca = 1;
    Thread loopJuego;
    BufferedImage buffer;
    Graphics2D g2d;
    boolean muestraLimites = true;
    boolean pruebaColicion = true;
    ImagenesEntidad fondo;
    Sprite nave;
    Sprite[] ast = new Sprite[asteroidesMax];
    Sprite[] balas = new Sprite[balasMax];
    int balasAct = 0;
    Random azar = new Random();
    SonidoClass shoot;
    SonidoClass explocion;
    SonidoClass acelera;
    boolean teclaAbajo, teclaArriba, teclaDer, teclaIzq, teclaDisparo;
    int cuentaMarco = 0, velMarco = 0;
    long tiempoInicio = System.currentTimeMillis();
    //Fin

    public static void main(String[] args)
    {
        new GuerraGalactica();
    }
   
    public GuerraGalactica()
    {
        super("Guerra Galactica");
       
        buffer = new BufferedImage(ancho, alto, BufferedImage.TYPE_INT_RGB);
        g2d = buffer.createGraphics();
       
        fondo = new ImagenesEntidad(this);
        fondo.carga("\\media\\bluespace.png");
               
        nave = new Sprite(this, g2d);
        nave.cargarImg("\\media\\ship.png");
        nave.setPosicion(new Point2D(centroX, centroY));
        nave.setVivo(true);
        explocion = new SonidoClass("\\media\\explode.wav");
        shoot = new SonidoClass("\\media\\shoot.wav");
        acelera = new SonidoClass("\\media\\thrust.wav");
       
        for (int i = 0; i < balasMax; i++)
        {
            balas[i] = new Sprite(this, g2d);
            balas[i].cargarImg("\\media\\shoot.png");
        }
       
        for (int i = 0; i < asteroidesMax; i++)
        {
            ast[i] = new Sprite(this, g2d);
            ast[i].setVivo(true);
            ast[i].cargarImg("\\media\\asteroid"+(azar.nextInt(5)+1)+".png");
            ast[i].setPosicion(new Point2D(azar.nextInt(ancho), azar.nextInt(alto)));
            ast[i].setAnguloMov(azar.nextInt(360));
            ast[i].setAnguloVista(azar.nextInt(360));
            ast[i].setFacRot(azar.nextDouble());
            double ang = ast[i].anguloMov() - 90;
            ast[i].setVelocidad(new Point2D(calcMovAnguloX(ang), calcMovAnguloY(ang)));  
        }
        addKeyListener(this);
       
        loopJuego = new Thread(this);
        loopJuego.start();
       
        setSize(ancho, alto);
        setVisible(true);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
    }
   
    public void paint(Graphics g)
    {
        g2d.drawImage(fondo.getImage(), 0, 0, ancho - 1, alto - 1, this);
       
        dibujaNave();
        dibujaAst();
        dibujaBalas();
       
        System.out.println("FPS: " + velMarco);
        System.out.println("Nave: " + Math.round(nave.pos.getX()) + ", " + Math.round(nave.pos.getY()));
        System.out.println("Angulo: " + Math.round(nave.anguloMov() + 90));
        System.out.println("Vista: " + Math.round(nave.anguloVista()));
       
        if (muestraLimites)
        {
            g2d.setColor(Color.GREEN);
            g2d.drawString("Cajas Limitantes", ancho - 150, 10);
        }
       
        if (pruebaColicion)
        {
            g2d.setColor(Color.GREEN);
            g2d.drawString("Prueba de Coliciones", ancho - 150, 10);
        }
               
        g.drawImage(buffer, 0, 0, this);
    }
   
    public void dibujaNave()
    {
        nave.transform();
        nave.draw();
        if (muestraLimites)
        {
            if (nave.estado() == spriteChoca)
                nave.drawLimite(Color.RED);
            else
                nave.drawLimite(Color.BLUE);
        }
    }
   
    public void dibujaAst()
    {
        for (int i = 0; i < asteroidesMax; i++)
        {
           if(ast[i].vivo())
           {
                ast[i].transform();
                ast[i].draw();
                if (muestraLimites)
                {
                    if (ast[i].estado() == spriteChoca)
                        ast[i].drawLimite(Color.RED);
                    else
                        ast[i].drawLimite(Color.BLUE);
                }
           }
        }
    }
   
    public void dibujaBalas()
    {
        for (int i = 0; i < balasMax; i++)
        {
           if(balas[i].vivo())
           {
                balas[i].transform();
                balas[i].draw();
                if (muestraLimites)
                {
                    if (balas[i].estado() == spriteChoca)
                        balas[i].drawLimite(Color.RED);
                    else
                        balas[i].drawLimite(Color.BLUE);
                }
           }
        }
    }
   
    public void run()
    {
        Thread t = Thread.currentThread();
       
        while(t == loopJuego)
        {
            try
            {
                gameUpdate();
                Thread.sleep(20);
            }
            catch (InterruptedException e) { e.printStackTrace(); }
           
            cuentaMarco++;
            if (System.currentTimeMillis() > tiempoInicio + 1000)
            {
                tiempoInicio = System.currentTimeMillis();
                velMarco = cuentaMarco;
                cuentaMarco = 0;
            }
            repaint();
        }
    }
   
    public void gameUpdate()
    {
        checkEntrada();
        actNave();
        actAst();
        actBalas();
        if (pruebaColicion) checkColicion();
    }
   
    public void actNave()
    {
        nave.posicion();
        double newX = nave.posicion().getX();
        double newY = nave.posicion().getY();
       
        if (nave.posicion().getX() < -10)
            newX = ancho + 10;
        else if (nave.posicion().getX() > ancho + 10)
            newX = -10;
       
        if (nave.posicion().getY() < -10)
            newY = alto + 10;
        else if (nave.posicion().getY() > alto + 10)
            newY = -10;
       
        nave.setPosicion(new Point2D(newX, newY));
        nave.setEstado(spriteNorm);
    }
           
    public void actAst()
    {
        for (int i = 0; i < asteroidesMax; i++)
        {
            if (ast[i].vivo())
            {
                ast[i].posicion();
                ast[i].actRot();
                int w = ast[i].imgAncho() - 1;
                int h = ast[i].imgAlto() - 1;
                double newX = ast[i].posicion().getX();
                double newY = ast[i].posicion().getY();
               
                if (ast[i].posicion().getX() < -w)
                    newX = ancho + w;
                else if (ast[i].posicion().getX() > ancho + w)
                    newX = -w;
               
                if (ast[i].posicion().getY() < -h)
                    newY = alto + h;
                else if (ast[i].posicion().getX() > alto + h)
                    newY = -h;
               
                ast[i].setPosicion(new Point2D(newX, newY));
                ast[i].setEstado(spriteNorm);
            }
        }
    }
   
    public void actBalas()
    {
        for (int i = 0; i < balasMax; i++)
        {
            if (balas[i].vivo())
            {
                balas[i].posicion();

                if (balas[i].posicion().getX() < 0 || balas[i].posicion().getX() > ancho)
                    balas[i].setVivo(false);

                if (balas[i].posicion().getY() < 0 || balas[i].posicion().getX() > alto)
                    balas[i].setVivo(false);

                balas[i].setEstado(spriteNorm);
            }
        }
    }
   
    public void checkColicion()
    {
        for (int i = 0; i < asteroidesMax; i++)
        {
            if (ast[i].vivo())
            {
                for (int n = 0; n < balasMax; n++)
                {
                    if (balas[n].vivo())
                    {
                        if (ast[i].chocaCon(balas[n]))
                        {
                            balas[n].setEstado(spriteChoca);
                            ast[i].setEstado(spriteChoca);
                            explocion.start();
                        }
                    }
                }
            }
        }
       
        for (int i = 0; i < asteroidesMax; i++)
        {
            if (ast[i].vivo())
            {
                if (nave.chocaCon(ast[i]))
                {
                    ast[i].setEstado(spriteChoca);
                    nave.setEstado(spriteChoca);
                    explocion.start();
                }
            }
        }
       
    }
   
    public void checkEntrada()
    {
        if (teclaIzq)
        {
            nave.setAnguloVista(nave.anguloVista() - 5);
            if (nave.anguloVista() < 0) nave.setAnguloVista(360-5);
        }
        else if (teclaDer)
        {
            nave.setAnguloVista(nave.anguloVista() + 5);
            if (nave.anguloVista() > 360) nave.setAnguloVista(5);
        }
        else if (teclaArriba)
        {
            aplicaImpulso();
        }
    }
   
    public void keyTyped(KeyEvent k) {}
    public void keyPressed(KeyEvent k)
    {
        switch (k.getKeyCode())
        {
            case KeyEvent.VK_LEFT:
                teclaIzq = true;
                break;
            case KeyEvent.VK_RIGHT:
                teclaDer = true;
                break;
            case KeyEvent.VK_UP:
                teclaArriba = true;
                break;
            case KeyEvent.VK_CONTROL:
            case KeyEvent.VK_ENTER:
            case KeyEvent.VK_SPACE:
                teclaDisparo = true;
                break;
            case KeyEvent.VK_B:
                muestraLimites = !muestraLimites;
                break;
            case KeyEvent.VK_C:
                pruebaColicion = ! pruebaColicion;
                break;
        }
    }
   
    public void keyReleased(KeyEvent k)
    {
        switch (k.getKeyCode())
        {
            case KeyEvent.VK_LEFT:
                teclaIzq = false;
                break;
            case KeyEvent.VK_RIGHT:
                teclaDer = false;
                break;
            case KeyEvent.VK_UP:
                teclaArriba = false;
                acelera.stop();
                break;
            case KeyEvent.VK_CONTROL:
            case KeyEvent.VK_ENTER:
            case KeyEvent.VK_SPACE:
                teclaDisparo = false;
                dispara();
                break;
        }
    }
   
    public void aplicaImpulso()
    {
        nave.setAnguloMov(nave.anguloVista() - 90);
       
        double velX = nave.velocidad().getX();
        velX += calcMovAnguloX(nave.anguloMov()*aceleracion);
        double velY = nave.velocidad().getY();
        velY += calcMovAnguloY(nave.anguloMov()*aceleracion);
       
        nave.setVelocidad(new Point2D(velX, velY));
       
        acelera.setLoop(true);
        acelera.start();
    }
   
    public void dispara()
    {
        balasAct++;
        if (balasAct > balasMax - 1) balasAct = 0;
        if (!balas[balasAct].vivo())
        {
            balas[balasAct].setVivo(true);
       
            int w = balas[balasAct].imgAncho();
            int h = balas[balasAct].imgAlto();

            double x = nave.centro().getX() - w / 2;
            double y = nave.centro().getY() - h / 2;
            balas[balasAct].setPosicion(new Point2D(x, y));

            balas[balasAct].setAnguloVista(nave.anguloVista());
            balas[balasAct].setAnguloMov(nave.anguloVista() - 90);

            double ang = balas[balasAct].anguloMov();
            double svX = calcMovAnguloX(ang) * velBalas;
            double svY = calcMovAnguloY(ang) * velBalas;
            balas[balasAct].setVelocidad(new Point2D(svX, svY));
            shoot.start();            
        }        
    }
       
    public double calcMovAnguloX(double angulo)
    {
        return Math.cos(angulo * Math.PI / 180);
    }
   
    public double calcMovAnguloY(double angulo)
    {
        return Math.sin(angulo * Math.PI / 180);
    }
   
}
   
   


Update: I changed the code to reflect the new changes and took out the image since that issue has been resolved.
Offline ra4king

JGO Kernel


Medals: 337
Projects: 2
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2012-01-04 23:32:44 »

According to the (very tiny) image, it looks like lines 96 and 124 are throwing NullPointerExceptions. This means both variables "fondo" and "nave" are null. The top exception means "fondo" was null at first, but then you made assigned it to an object which caused then caused the bottom two exceptions to say "nave" is null.

EDIT: Aha! You call setVisible(true) before you initialize anything. This means "fondo" and "nave" are both null while paint(Graphics) gets called for the first time. Try moving that line to the end of the constructor, right before you start the new Thread.

Offline WolfAlvein

Senior Newbie





« Reply #2 - Posted 2012-01-04 23:41:42 »

Thanks for the quick reply the errors are all gone with that just did what you told me... but the game is still frozen and nothing moves and the console is writing like if it where on fire i mean it's like it writes 100 lines/sec telling me the data i wanted but the game is still frozen. but thanks now i Know how to avoid that error.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 337
Projects: 2
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2012-01-05 00:15:20 »

I didn't look at all your code but your setup and listeners look fine.

Couple things to add:
- Extend and draw on a JComponent instead of JFrame because you will have automatic double buffering by overriding paintComponent(Graphics) and 0,0 won't be hidden behind the decoration (the top bar with minimize, maximize, and close buttons).
- Don't import java.lang.System. java.lang.* is automatically imported in every single class file......forever Tongue

Offline WolfAlvein

Senior Newbie





« Reply #4 - Posted 2012-01-05 01:29:25 »

Ok I'll try the changes, but how exactly do I draw on the JComponent I have never used it before. Could you please show me an example or tell me where can I find one?
Offline Thimbletack

Junior Newbie





« Reply #5 - Posted 2012-01-05 01:35:47 »

This is a pretty good example using a JPanel:

http://zetcode.com/tutorials/javagamestutorial/basics/

Basically, you just do most of the coding in the JPanel, then all the JFrame does is put it on the screen.
Offline ra4king

JGO Kernel


Medals: 337
Projects: 2
Exp: 5 years


I'm the King!


« Reply #6 - Posted 2012-01-05 06:01:07 »

It is best (and makes more sense) to extend a javax.swing.JComponent. All you do is create a class that extends it, override paintComponent(Graphics) and do the rendering there (don't do your own double buffering, Swing is automatically double buffered). Also, please don't extend JFrame, it is not meant to be extended, but to be created. You create a new instance of JFrame, set the approriate properties, add(new MyComponent()), set it to visible, and create the thread.

Offline theagentd
« Reply #7 - Posted 2012-01-05 06:07:29 »

Don't take a too big leap when making something new. Do small changes and check so everything is working between them.

Myomyomyo.
Offline WolfAlvein

Senior Newbie





« Reply #8 - Posted 2012-01-05 14:35:36 »

Ok so instead of JFrame I extended to JComponent and now the lines containing this codes gave me an error, I'm guessing this part of the code only is to be used with JFrame so how exactly do I go changing this about, the override to paint still works perfectly...

1  
2  
3  
4  
5  
6  
7  
super("Guerra Galactica");
fondo = new ImagenesEntidad(this);
nave = new Sprite(this, g2d);
balas[i] = new Sprite(this, g2d);
ast[i] = new Sprite(this, g2d);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


And since I moved to the JComponent class should I Still use the buffer, or should I completely eliminate it???

Well it seems I can't really make the change to JComponent without making the other classes blowup, since they are dependent on the JFrame, I'll try testing the JComponent in the next tutorial I take, I have some questions that maybe you guys can help me out and help me understand.

1- What is better Jpanle or Jcomponent??
2- How does one go around and implement a framework like openGL?? or more precisely what are the benefits of using any type of framework?Huh
3- Is openGL a good framework for 2D games?

Ohh and by the way with the problem that nothing moves, the sound works fine and the bullets get shoot and they sound when they do it's just that the sprites aren't being moved around I think it might be on the update method the problem...
Offline theagentd
« Reply #9 - Posted 2012-01-05 16:33:09 »

OpenGL is not a framework. OpenGL is about as low level as DirectX in that it grants access to all graphics cards supporting OpenGL with drivers. It does not SIMPLIFY making games, it simply allows you to utilize the graphics card. A framework is more like a game engine (I'd say framework = game engine), in that it handles lots of things that are related to games, like handling game objects/entities, drawing backgrounds and loading resources.

Vastly simplified: To a game engine, you can say "Draw this object for me", "Move this object to (x ,y)" or "Fire a bullet here" and it will do it. OpenGL however doesn't know what an object is. It doesn't even know what a 3D model is. The only thing it knows is drawing triangles, but it is INSANELY fast at drawing those. You'd therefore build a framework OVER OpenGL, so when you tell your game engine "Draw this object for me", your game engine in turn tells OpenGL which triangles to draw.

Concerning the benefits of OpenGL vs Java2D (what you're using now), I recommend that you stay as far away from OpenGL as possible for now. You'll get by fine with Java2D. OpenGL trades performance for simplicity, and you should definitely get a hang of Java2D before moving on to OpenGL when Java2D isn't fast enough anymore.

I'll leave the JPanel vs JComponent question to the Java2D experts. Cheesy

Myomyomyo.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Grunnt

JGO Wizard


Medals: 66
Projects: 8
Exp: 5 years


Complex != complicated


« Reply #10 - Posted 2012-01-05 16:47:47 »

3- Is openGL a good framework for 2D games?

A good framework for starting game development is Slick2D (http://slick.cokeandcode.com/). It uses OpenGL hardware accelleration, but it has a framework built around it which makes it a lot easier to use. However, as a starting point just using normal Java2D is probably the easiest.

Offline BoBear2681

JGO Coder


Medals: 18



« Reply #11 - Posted 2012-01-05 17:40:38 »

JPanel vs. JComponent, as far as games are concerned, is practically a non-issue.  The differences between the two are default layout, opaqueness (depending on the LAF), things like that.*  If you're painting the entire component each frame, there will be no functional difference between them.  All that said, folks generally recommend JComponent since it's "lower-level" (JPanel being a subclass of JComponent).

* As an aside, JComponent actually isn't double buffered by default but JPanel is; however, this is also a non-issue since the root pane of a JFrame double-buffers all of its children.
Offline WolfAlvein

Senior Newbie





« Reply #12 - Posted 2012-01-05 17:50:07 »


Ok so OpenGL is not a framework but basically the directives and drivers to communicate with the system hardware or more properly the video card.

I'll keep using Java2D for now I'm not interested in doing a 3D game yet, I want to learn a lot about 2D gaming and after that advance too 3D, ill check out the framework you told me about, and what I understood about framework's is that they are game engines made to simplify the development of some game styles, basically 2D and 3D no matter what type of game (RPG, RTS, etc).

So can anyone point me to a good tutorial about game engines??? just so I learn to make my one before using some framework jejejeje.
Offline ra4king

JGO Kernel


Medals: 337
Projects: 2
Exp: 5 years


I'm the King!


« Reply #13 - Posted 2012-01-05 18:18:57 »

There aren't really "tutorials" about game engines. A game engine is in fact the popular term for a framework or a game library. These frameworks handles things for you, they make your life easy. Since you're a beginner I absolutely recommend you to not use a Java2D framework. In fact, I think you should read this post to understand the situation between Java2D and OpenGL.

Regarding your code, I believe your error is "setDefaultCloseOperation(...)", since that's not a method in JComponent, only in JFrame. Here is a bit of pseudocode to understand what to do you with JFrame and JComponent:
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  
public class Main {
    public static void main(String[] args) {
        JFrame frame = new JFrame("My Game");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(width,height);
        frame.setResizable(false);
        frame.add(new MyComponent());
        frame.setVisible(true);
    }
}

public class MyComponent extends JComponent implements Runnable, KeyListener {
    // Move everything from your original class to this class
   
    Sprite ....
    ....
    boolean isRunning;

    public MyComponent() {
        //initialize everything
       
        addKeyListener(this);
    }
   
    public void addNotify() {
        super.addNotify();
       
        isRunning = true;
        new Thread(this).start();
    }
   
    public void run() {
        //game loop
       
        while(isRunning) {
            update();
           
            repaint();
           
            //sleep
       }
    }
   
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
       
        //draw everything
   }

    public void keyPressed(KeyEvent key) {}
    public void keyReleased(KeyEvent key) {}
    public void keyTyped(KeyEvent key) {}
}


Again, move all your variables and methods into this class. Looking at your code, you are not using any of JFrame's methods (except the ones to initialize) so you shouldn't be having any problems.

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.

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

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

pw (39 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (26 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21

Zero Volt (50 views)
2014-07-17 23:47:54

danieldean (42 views)
2014-07-17 23:41:23

MustardPeter (44 views)
2014-07-16 23:30:00
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

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24: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!