Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    92
    Rep Power
    0

    Default Flashing screen,but no graphics.

    So,i'm making ANOTHER game now,and i'm having render troubles.

    The screen randomly blinks in and out. the only thing it actually renders is the sky.

    Java Code:
    public void render() {
        	Graphics g = screen.getGraphics();
        	
        	
        	//Drawing things.    	
        	g = getGraphics();
        	
        	g.drawImage(screen, 0 ,0 ,size.width ,size.height ,0 ,0 ,pixel.width ,pixel.height ,null);
    
        	sky.render(g);
        	
        	level.render(g,(int) sX,(int) sY, (pixel.width/ IDs.terrainSize) + 2,(pixel.height/ IDs.terrainSize) + 2);
    
        	g.dispose();
        }
    That's the render code....the rendering for the level is the same as another,working game i have,and sky render is this:

    Java Code:
    public class Sky {
    	
    	public int night = 1;
    	public static int day = 0;
    	public static int time = day;
    	
    	public int r1 = 70,g1 = 120,b1 = 230; //day
    	public int r2 = 15,g2 = 15,b2 =80; //night
    	public int r = r1, g = g1, b = b1;
    	public int dayFrame = 0,dayTime = 7000;
    	public int changeFrame = 0, changeTime = 5;
    	
    	public Sky(){
    		if (time == day){
    			r = r1;
    			g = g1;
    			b = b1;
    		} else if (time == night){
    			r = r2;
    			g = g2;
    			b = b2;
    		}
    	}
    	
    	
    	
    	public void tick(){
    		if(dayFrame >= dayTime){
    			if(time == day){
    				time = night;
    			} else if (time == night){
    				time = day;
    			}
    			
    			dayFrame = 0;
    		}else {
    			dayFrame += 1;
    		}
    		
    		if(changeFrame >= changeTime){
    			if(time == day){
    				if(r < r1){
    					r += 1;
    				}
    				if(g < g1){
    					g += 1;
    				}
    				if(b < b1){
    					b += 1;
    				}
    			} else if (time == night){
    				if(r > r2){
    					r -= 1;
    				}
    				if(g > g2){
    					g -= 1;
    				}
    				if(b > b2){
    					b -= 1;
    				}
    			}
    			changeFrame =0;
    			} else {
    				changeFrame +=1;
    			}
    		}
    	public void render(Graphics gr){
    		gr.setColor(new Color(r, g, b));
    		gr.fillRect(0, 0, Component.pixel.width, Component.pixel.height);
    	}
    
    }
    I honestly don't know why it's doing this,since i'm pretty much copying off of another project,and that one works fine.

    Here's the level rendering:

    Java Code:
    public void render(Graphics g, int camX, int camY, int renW, int renH) {
    		for(int x=(camX / IDs.terrainSize);x<(camX / IDs.terrainSize) + renW;x++) {
    			for(int y=(camY / IDs.terrainSize);y<(camY / IDs.terrainSize)+ renH;y++) {
    				if(x >= 0 && y >= 0 && x <worldW && y <worldH) {
    					Terrain[x][y].render(g);
    					
    					if(Terrain[x][y].id != IDs.air){
    						if(Terrain[x][y].contains(new Point((Component.mse.x ) / Component.pixelSize + (int) Component.sX, (Component.mse.y) / Component.pixelSize + (int) Component.sY))){
    							g.setColor(new Color(255, 255, 255,100));
    							g.fillRect(Terrain[x][y].x - camX, Terrain[x][y].y - camY, Terrain[x][y].width, Terrain[x][y].height);
    						}
    					}
    				}
    			}
    		}
    	}

  2. #2
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    263
    Rep Power
    6

    Default Re: Flashing screen,but no graphics.

    The flickering may be due to performance issues (does this game require more computation that the last?). A common approach is to use "double buffering." The idea is to paint everything to an offscreen image, then paint that image directly to the canvas.

    As a side note, if you really want to get into 2d game development with java, I strongly recommended looking into the slick2d and lwjgl libraries - all of this sort of stuff is already handled for you. You just need to worry about what to draw and the game logic.
    Last edited by AndrewM16921; 06-20-2013 at 02:20 PM.

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default Re: Flashing screen,but no graphics.

    Never call getGraphics() on a Component like that. That will definitely cause flickering.

    Recommended reading: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    As for libraries, there are a ton out there worth checking out- LWJGL, libGDX, JMonkeyEngine, etc. But you should learn the basics (such as not to call getGraphics() like that) before diving into them, imho.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    92
    Rep Power
    0

    Default Re: Flashing screen,but no graphics.

    I always learned to use getGraphics() just like that.I'll look into other ways to do it.

  5. #5
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    92
    Rep Power
    0

    Default Re: Flashing screen,but no graphics.

    I'm Still having the same problem.It's a lot less severe,but it's better now.

    New Render code:

    Java Code:
        public void render(Graphics g) {
        	
        	super.paintComponent(g);
        	
        	//Drawing things.
        	    	    	
        	sky.render(g);
        	
        	level.render(g,(int) sX,(int) sY, (pixel.width/ IDs.terrainSize) + 2,(pixel.height/ IDs.terrainSize) + 2);
        	g.dispose();
        	
        	g.drawImage(screen, 0 ,0 ,size.width ,size.height ,0 ,0 ,pixel.width ,pixel.height ,null);

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default Re: Flashing screen,but no graphics.

    The only reason you'd call getGraphics() is if you're doing active rendering. I wouldn't recommend doing that until you have a better idea of how everything fits together. The problem is you were mixing active rendering and passive rendering, which was causing the flickering.

    Why are you calling g.dispose()?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    92
    Rep Power
    0

    Default Re: Flashing screen,but no graphics.

    So...i should just continue,and this should go away? because on my other game,it has the older code,but it doesn't flicker. maybe because it's "slower"? It has more to render,and check and things.

  8. #8
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,324
    Rep Power
    5

    Default Re: Flashing screen,but no graphics.

    What I recommend you do is figure out why you are doing the things you are doing and then convert it to a proper paint technique. Make certain you use Swing and not AWT. If you are drawing your images in a BufferedImage then go ahead and do that. But then you should do something like the following:

    Java Code:
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(buf,.......);
    }
    The image would be drawn by imvoking repaint() somewhere else in your code. How often and the locations of where you invoke repaint() need to be investigated. And the order in which you draw items in the BufferedImage is also important. You really need to look at the custom painting tutorials that Kevin recommended. Creating graphics based, interactive games is not necessarily trivial. Quite often threads are involved (in addition to the EDT). It would be beneficial to just practice doing some simple things using graphics until you get the hang of it.

    Regards,
    Jim
    Last edited by jim829; 06-24-2013 at 11:36 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    892
    Rep Power
    2

    Default Re: Flashing screen,but no graphics.

    I want to add my two cents, albeit very old school but it might help a bit. It's manual double buffering, but it still works. I cooked up an example which has lots of flaws, but it demonstrates the use of an offscreen image to paint on and then slap it all flicker free on screen at once. I used this 10 years ago with a scrolling text applet on my homepage :)

    Pay special attention to the off-screen Image created and the painting on it. When it's done, it's all drawn on screen with drawImage.

    I am overriding update(Graphics g) because the original update(Graphics g) clears the background which causes the flickering.

    Like I said, it's not pretty but here it goes:
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class PaintTest extends JFrame {
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    PaintTest p = new PaintTest();
                    p.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                    Anim comp = new Anim("This is a fluid scrolling text!");
                    p.add(comp);
                    p.pack();
                    p.setLocationRelativeTo(null);
                    p.setVisible(true);
                    comp.start();
                }
            });
        }
    }
    
    class Anim extends JPanel {
        private final String text;
        private Image offScreen;
        private int x;
        private Thread animThread;
    
        public Anim(String text) {
            setPreferredSize(new Dimension(800, 600));
            this.text = text;
            animThread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        while (x < getWidth()) {
                            Thread.sleep(20);
                            repaint();
                            System.out.println("x = " + x);
                        }
                    }
                    catch (InterruptedException e) {
                        e.printStackTrace();
                    }
    
                }
            });
        }
    
        @Override
        public void update(Graphics g) {
            paint(g);
        }
    
        @Override
        public void paint(Graphics g) {
            if (offScreen == null) {
                offScreen = createImage(getWidth(), getHeight());
            }
    
            Graphics2D offG2D = (Graphics2D) offScreen.getGraphics();
            offG2D.setColor(Color.blue);
            offG2D.fillRect(0, 0, getWidth(), getHeight());
    
            offG2D.setColor(Color.yellow);
            x += 1;
            offG2D.drawString(text, getWidth() - x, getHeight() / 2);
    
            Graphics2D g2 = (Graphics2D) g.create();
            g2.drawImage(offScreen, 0, 0, getWidth(), getHeight(), null);
    
            offG2D.dispose();
            g2.dispose();
    
        }
    
        public void start() {
            animThread.start();
        }
    }

Similar Threads

  1. 2D Graphics "flashing"
    By varesa in forum Java 2D
    Replies: 3
    Last Post: 08-15-2011, 10:35 PM
  2. Replies: 0
    Last Post: 04-28-2011, 08:15 AM
  3. repaint without flashing
    By natdizzle in forum AWT / Swing
    Replies: 5
    Last Post: 01-15-2011, 05:06 PM
  4. Flashing JFrame
    By ilyvatar in forum AWT / Swing
    Replies: 3
    Last Post: 12-01-2010, 09:52 AM
  5. Flashing
    By Supamagier in forum Java 2D
    Replies: 6
    Last Post: 04-29-2009, 03:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •