Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
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  
  My game is flickering like hell ??  (Read 2200 times)
0 Members and 1 Guest are viewing this topic.
Offline aslan

Senior Newbie




Java games rock!


« Posted 2002-12-03 17:50:16 »

Am creating a 2D spaceship shooter but am having this problem with my spaceship flickering like hell. How can I solve this?


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  
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


class Raptor extends JFrame implements KeyListener
{
      Graphics2D g;
      JLabel raptorL;
      Thread loop;
     
      int x=400;
      int y=400;
     
      int skudXStart1=0;
      int skudYStart1=0;
      int skudXSlut1=0;
      int skudYSlut1=0;
     
      int skudXStart2=0;
      int skudYStart2=0;
      int skudXSlut2=0;
      int skudYSlut2=0;      
     
      boolean right,left,space;
           
     
      int enemy1x=600;
      int enemy1y=-100;
     
      Raptor()
      {
            addKeyListener(this);            
     
        setBounds(100,50,800,600);
        setVisible(true);            
      }
      public void paint(Graphics g1)
      {
            super.paint(g1);
            g = (Graphics2D) g1;
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.drawImage((new ImageIcon("raptorSmall.gif")).getImage(),x,y,this);
        g.setColor(Color.red);
        g.drawLine(skudXStart1,skudYStart1,skudXSlut1,skudYSlut1);
        g.drawLine(skudXStart2,skudYStart2,skudXSlut2,skudYSlut2);  
       
        g.drawImage((new ImageIcon("Enemy1.gif")).getImage(),enemy1x,enemy1y,this);            

          loop = new Thread(new Runnable()            
          {
             public void run()
             {
     
                            if(right)
                            {
                               x+=1;
                            }
                            if(left)
                            {
                               x-=1;
                            }
                              if(space)
                              {                                                      
                             skudYStart1-=5;
                               skudYSlut1-=5;
                               
                             skudYStart2-=5;
                               skudYSlut2-=5;                        
                            }
                           
                            try{      
                            Thread.sleep(5000);          
                            }catch(InterruptedException a){System.out.println(a);}      

                               enemy1x-=1;
                               enemy1y+=1;        
                       
                                                         
             }
          });
          loop.start();  

        repaint();    
      }
      public void keyPressed(KeyEvent e)
      {

            if(e.getKeyCode() == KeyEvent.VK_LEFT)
            {
             left=true;                  
            }
            if(e.getKeyCode() == KeyEvent.VK_RIGHT)
            {              
               right=true;                        
            }
        if(e.getKeyCode() == KeyEvent.VK_SPACE && skudYStart1 <= 0)
        {
              //System.out.println(skudYStart1);
             
                skudXStart1=x+20;
            skudYStart1=y+30;
            skudXSlut1=x+20;
              skudYSlut1=450;              
             
                skudXStart2=x+78;
            skudYStart2=y+30;
            skudXSlut2=x+78;
              skudYSlut2=450;                      
             
              space=true;
         
        }                              
      }
      public void keyReleased(KeyEvent e)
      {            

           
            if(e.getKeyCode() == KeyEvent.VK_LEFT)
            {
             left=false;      
            }
            if(e.getKeyCode() == KeyEvent.VK_RIGHT)
            {              
               right=false;      
            }
        if(e.getKeyCode() == KeyEvent.VK_SPACE)
        {      
           //space=false;      
           //loop=null;
        }      
                 
      }
      public void keyTyped(KeyEvent e){}
     
      public static void main(String[] aslan)
      {
            new Raptor();
      }
}
Offline cknoll

Junior Duke




Flame On!


« Reply #1 - Posted 2002-12-03 18:24:02 »

Ahh! My brain exploded!  heh, just kidding, lemme see here...


Looks like every time you paint, a new thread is started, is that the right design?  Also, every time you paint, you are creating a whole new ImageIcon.  Maybe that could be pre-loaded?

Anyways, I think the main problem you are having is that the default implementation of update(Graphics g) is to do a clearRect of the display area before paint is called (hence, you flicker)  Just rename your paint() method to update() and see how that goes.

Also, repaint() isn't going to do what you expect, repaint() just 'asks' the toolkit to please redraw it when it's convienent, but this will make any animation or time-related drawing (such as game programming) run like ass.  So, take that repaint out of there and find another way (hint, have a main game loop thread that will call the paint routines at defined intervals.)

-Chris
Offline rgeimer

Senior Newbie





« Reply #2 - Posted 2002-12-03 19:52:38 »

Here is a version that should eliminate most of the flickering and run much smoother. It should also be easy to convert to full screen exclusive mode. Note: I based this on the original code you posted to forums.java.sun.com.

Rick

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  
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.*;


class Raptor extends JFrame implements KeyListener{
      Graphics2D g;
      JLabel raptorL;
      Timer loop;
      int x=400;
      int y=400;
      int skudXStart1=0;
      int skudYStart1=0;
      int skudXSlut1=0;
      int skudYSlut1=0;
      int skudXStart2=0;
      int skudYStart2=0;
      int skudXSlut2=0;
      int skudYSlut2=0;
      boolean right,left,space;
      Image raptorImage = null;
      Image buffer = null;
      BufferStrategy bs = null;
      Rectangle bounds = null;

      Raptor() {
            addKeyListener(this);
            setBounds(100,50,800,600);
            bounds = getBounds();
        setVisible(true);
      }

      private void render(Graphics g1){
            g = (Graphics2D) g1;
            Color c = g.getColor();
            g.clearRect(0,0,(int)bounds.getWidth(),(int)bounds.getHeight());
      //      g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        if ( raptorImage == null ){
                  System.out.println("Loading raptor image");
                  Image tempImage = new ImageIcon("raptorSmall.gif").getImage();
                  MediaTracker mt = new MediaTracker(this);
                  mt.addImage(tempImage,0);
                  try {
                        mt.waitForID(0);
                        mt.removeImage(tempImage,0);
                  } catch ( InterruptedException e ){
                        e.printStackTrace();
                  }
                  raptorImage = createImage(tempImage.getWidth(null), tempImage.getHeight(null));
                  Graphics g = raptorImage.getGraphics();
                  g.drawImage(tempImage,0,0,null);
                  g.dispose();
            }
        g.drawImage(raptorImage,x,y,this);
        g.setColor(Color.red);
        g.drawLine(skudXStart1,skudYStart1,skudXSlut1,skudYSlut1);
        g.drawLine(skudXStart2,skudYStart2,skudXSlut2,skudYSlut2);
        if ( space ){
                  skudYStart1-=5;
                  skudYSlut1-=5;
                  skudYStart2-=5;
                  skudYSlut2-=5;
                  if ( skudYStart1 <= 0 ){
                        space = false;
                  }
            }
            if ( left ){
                  x -= 1;
            }
            if ( right ){
                  x += 1;
            }
      }

      public void run(){
            while(true){
                  if ( bs == null ){
                        createBufferStrategy(2);
                        bs = getBufferStrategy();
                  }
                  Graphics g = bs.getDrawGraphics();
                  render(g);
                  bs.show();
                  g.dispose();
            }
      }

      public void paint(Graphics g1)      {
            super.paint(g1);
            render(g1);

      }

      public void keyPressed(KeyEvent e)       {
            if(e.getKeyCode() == KeyEvent.VK_ESCAPE ) {
                  System.exit(0);
            }
            if(e.getKeyCode() == KeyEvent.VK_LEFT) {
                  left=true;
            }
            if(e.getKeyCode() == KeyEvent.VK_RIGHT)      {
                  right=true;
                  x+=1;
            }
            if(e.getKeyCode() == KeyEvent.VK_SPACE) {
                  if (skudYStart1 <= 0){
                        skudXStart1=x+20;
                        skudYStart1=y+30;
                        skudXSlut1=x+20;
                        skudYSlut1=450;
                        skudXStart2=x+78;
                        skudYStart2=y+30;
                        skudXSlut2=x+78;
                        skudYSlut2=450;
                  }
                  space=true;

            }
    }

      public void keyReleased(KeyEvent e)      {
            if(e.getKeyCode() == KeyEvent.VK_LEFT){
                  left=false;
            }
            if(e.getKeyCode() == KeyEvent.VK_RIGHT){
                  right=false;
            }
            if(e.getKeyCode() == KeyEvent.VK_SPACE){
                  //space=false;
            }
      }

      public void keyTyped(KeyEvent e){}

      public static void main(String[] aslan)      {
            new Raptor().run();
      }
}
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline cknoll

Junior Duke




Flame On!


« Reply #3 - Posted 2002-12-04 10:36:28 »

Question: is it possible to just get the Graphics2D via getGraphics of the bufferStrategy once and not need to dispose() every iteration (I thought that getting and destroying could lead to a lot of garbage) or does the buffer strategy basically require that every render frame you get graphics->draw->dispose?

-Chris
Offline aslan

Senior Newbie




Java games rock!


« Reply #4 - Posted 2002-12-04 11:54:53 »

Thx alot Chris and Rick for your answers..... Specially Rick for your very nice improvedment of my code. I will go through your changes and try to understand them Smiley
Offline aslan

Senior Newbie




Java games rock!


« Reply #5 - Posted 2002-12-04 14:37:46 »

Now I have this question...
I want a enemy spaceship to fly against the spaceship efter some delay, how do I do this? The way the code is now the enemy spaceship does this without any delay...



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  
import java.awt.*; 
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.*;
 
 
class Raptor extends JFrame implements KeyListener
{

       Graphics2D g;
       Timer loop;
       
       int x=400;
       int y=400;
       int skudXStart1=0;
       int skudYStart1=0;
       int skudXSlut1=0;
       int skudYSlut1=0;
       int skudXStart2=0;
       int skudYStart2=0;
       int skudXSlut2=0;
       int skudYSlut2=0;        
       boolean right,left,space;
       Image raptorImage = null;
       
       Image enemy1 = null;
       int enemy1x = 500;
       int enemy1y = -50;
       
       BufferStrategy bs = null;
       Rectangle bounds = null;
 
     Raptor()
     {
        addKeyListener(this);
        setBounds(100,50,800,600);
        bounds = getBounds();
        setVisible(true);
     }
 
     private void render(Graphics g1)
     {
            g = (Graphics2D) g1;
            g.clearRect(0,0,(int)bounds.getWidth(),(int)bounds.getHeight());
           
            if(raptorImage == null)
            {  
                       
                   Image tempImage = new ImageIcon("raptorSmall.gif").getImage();            
                   raptorImage = createImage(tempImage.getWidth(null), tempImage.getHeight(null));
                   Graphics g = raptorImage.getGraphics();
                   g.drawImage(tempImage,0,0,null);
                   g.dispose();
                   
            }            
            g.drawImage(raptorImage,x,y,this);
            g.setColor(Color.red);
            g.drawLine(skudXStart1,skudYStart1,skudXSlut1,skudYSlut1);
            g.drawLine(skudXStart2,skudYStart2,skudXSlut2,skudYSlut2);            
            if (space)
            {
                  skudYStart1-=5;
                  skudYSlut1-=5;
                  skudYStart2-=5;
                  skudYSlut2-=5;
           
            if (skudYStart1 <= 0)
            {
                space = false;
            }
        }
        if (left)
        {
            x -= 1;
        }
        if (right)
        {
            x += 1;
        }
       
        if(enemy1 == null)
        {
              Image tempImage2 = new ImageIcon("Enemy1.gif").getImage();
              enemy1 = createImage(tempImage2.getWidth(null), tempImage2.getHeight(null));
              Graphics g = enemy1.getGraphics();
                  g.drawImage(tempImage2,0,0,null);
                  g.dispose();              
        }
        g.drawImage(enemy1,enemy1x,enemy1y,this);
        enemy1x-=1;
        enemy1y+=1;
     }
     public void run()
     {
        while(true)
        {
           if (bs == null)
           {
              createBufferStrategy(2);
              bs = getBufferStrategy();
           }
           Graphics g = bs.getDrawGraphics();
           render(g);
           bs.show();
           g.dispose();
        }
     }
 
     public void paint(Graphics g1)
     {
         super.paint(g1);
         render(g1);
     }
 
     public void keyPressed(KeyEvent e)
     {
           if(e.getKeyCode() == KeyEvent.VK_ESCAPE )
           {
              System.exit(0);
           }
           if(e.getKeyCode() == KeyEvent.VK_LEFT)
           {
              left=true;
           }
           if(e.getKeyCode() == KeyEvent.VK_RIGHT)
           {
              right=true;
           }
           if(e.getKeyCode() == KeyEvent.VK_SPACE)
           {
                if (skudYStart1 <= 0)
                {
                      skudXStart1=x+20;
                      skudYStart1=y+30;
                      skudXSlut1=x+20;
                      skudYSlut1=450;
                      skudXStart2=x+78;
                      skudYStart2=y+30;
                      skudXSlut2=x+78;
                      skudYSlut2=450;
              }
               
              space=true;
 
         }
     }
     public void keyReleased(KeyEvent e)
     {
        if(e.getKeyCode() == KeyEvent.VK_LEFT)
        {
           left=false;
        }
        if(e.getKeyCode() == KeyEvent.VK_RIGHT)
        {
           right=false;
        }
        if(e.getKeyCode() == KeyEvent.VK_SPACE)
        {
        }
     }
     public void keyTyped(KeyEvent e){}
 
     public static void main(String[] aslan)
     {
          new Raptor().run();
     }
}
Offline rgeimer

Senior Newbie





« Reply #6 - Posted 2002-12-04 19:01:00 »

Quote
Question: is it possible to just get the Graphics2D via getGraphics of the bufferStrategy once and not need to dispose() every iteration (I thought that getting and destroying could lead to a lot of garbage) or does the buffer strategy basically require that every render frame you get graphics->draw->dispose?

-Chris


All the examples I have seen get the graphics object each time you want to draw. Maybe someone like Jeff could shed some light on the garbage collection impact of doing this.
Offline rgeimer

Senior Newbie





« Reply #7 - Posted 2002-12-04 19:04:27 »

Quote
Now I have this question...
I want a enemy spaceship to fly against the spaceship efter some delay, how do I do this? The way the code is now the enemy spaceship does this without any delay...


You could call System.currentTimeMillis() at the start of the game, then call it in the loop and launch the enemy after it has changed by an amount that you specify. However, remember that System.currentTimeMillis() has a limited resolution on Windows, so it may not be as accurate as you would like, but I think it would be good enough for a delay timer like this.

Rick
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.

Longarmx (37 views)
2014-10-17 03:59:02

Norakomi (28 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (28 views)
2014-10-15 16:18:58

TehJavaDev (52 views)
2014-10-14 00:39:48

TehJavaDev (54 views)
2014-10-14 00:35:47

TehJavaDev (42 views)
2014-10-14 00:32:37

BurntPizza (64 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (77 views)
2014-10-11 22:30:10
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!