Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By gimbal2

Thread: Trying to make a screen black for a buffered image

  1. #1
    daniel0 is offline Member
    Join Date
    Jan 2015
    Posts
    5
    Rep Power
    0

    Default Trying to make a screen black for a buffered image

    I tried to make a buffered screen image that constantly refreshed an image (a black screen) however it does not seem to be working.


    Java Code:
    import java.awt.Canvas;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.image.BufferStrategy;
    import java.awt.image.BufferedImage;
    
    import javax.swing.JFrame;
    
    
    public class Main extends Canvas implements Runnable{
    	
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	public static int width = 300;
    	public static int height = width / 16 * 9;
    	public static int scale = 3;
    	
    	private Thread gameThread;
    	private boolean running = false;
    	private JFrame frame;
    	private BufferedImage image = new BufferedImage (width, height, BufferedImage.TYPE_INT_RGB);
    	
    	public Main(){
    		Dimension size = new Dimension(width * scale, height * scale);
    		setPreferredSize(size);
    		
    		frame = new JFrame();
    	}
    	
    	public synchronized void start()	{
    		gameThread = new Thread(this, "Display");
    		gameThread.start();
    		
    	}
    	public synchronized void stop()	{
    		running = false;
    		try{
    			gameThread.join();
    		} catch(InterruptedException e)	{
    			e.printStackTrace();
    		}
    	}
    	
    	public void run()	{
    		while (running)	{
    			update();
    			render();
    		}
    	}
    	
    	public void update()	{
    	}
    	
    	public void render()	{
    		
    		BufferStrategy bs = getBufferStrategy();
    		if (bs == null)	{
    			createBufferStrategy(3);
    			return;
    		}
    		
    		Graphics g = bs.getDrawGraphics();
    		g.setColor(Color.BLACK);
    		g.fillRect(0, 0, getWidth(), getHeight());
    		g.dispose();
    		bs.show();
    	}
    	
    public static void main(String[]args)	{
    	Main game = new Main();
    	game.frame.setResizable(false);
    	game.frame.setTitle("Game");
    	game.frame.add(game);
    	game.frame.pack();
    	game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	game.frame.setLocationRelativeTo(null);
    	game.frame.setVisible(true);
    	
    	game.start();
    	
    }
    }
    Thank You

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    15

    Default Re: Trying to make a screen black for a buffered image

    For future reference, saying "it doesn't work" doesn't give us a lot to go off of.

    But one thing I do notice is that you never use your image variable. What is its purpose?

    Also, why are you calling getBufferStrategy() instead of just using a paint() method?

    Java Code:
    import java.awt.Canvas;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.image.BufferStrategy;
    import java.awt.image.BufferedImage;
     
    import javax.swing.JFrame;
     
     
    public class Main extends Canvas implements Runnable{
         
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        public static int width = 300;
        public static int height = width / 16 * 9;
        public static int scale = 3;
         
        private Thread gameThread;
        private boolean running = false;
        private JFrame frame;
        private BufferedImage image = new BufferedImage (width, height, BufferedImage.TYPE_INT_RGB);
         
        public Main(){
            Dimension size = new Dimension(width * scale, height * scale);
            setPreferredSize(size);
             
            frame = new JFrame();
        }
         
        public synchronized void start()    {
            gameThread = new Thread(this, "Display");
            gameThread.start();
             
        }
        public synchronized void stop() {
            running = false;
            try{
                gameThread.join();
            } catch(InterruptedException e) {
                e.printStackTrace();
            }
        }
         
        public void run()   {
            while (running) {
                update();
                render();
            }
        }
         
        public void update()    {
        }
         
        public void render()    {
        	repaint();
        }
        
        public void paint(Graphics g){
        	
            g.setColor(Color.BLACK);
            g.fillRect(0, 0, getWidth(), getHeight());
           
        }
         
    public static void main(String[]args)   {
        Main game = new Main();
        game.frame.setResizable(false);
        game.frame.setTitle("Game");
        game.frame.add(game);
        game.frame.pack();
        game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        game.frame.setLocationRelativeTo(null);
        game.frame.setVisible(true);
         
        game.start();
         
    }
    }
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Trying to make a screen black for a buffered image

    One has to make a choice indeed: either use Swing (JFrame) which has double buffering built-in and thus you don't need to worry about it, or don't use Swing and use a basic Window/Canvas and thus use a BufferStrategy to manage the double/triple buffering yourself.

    Also Swing is already threaded, so having your own thread with an active 100% CPU core consuming for-loop is very wrong.
    KevinWorkman likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    15

    Default Re: Trying to make a screen black for a buffered image

    Just for the record: I agree with everything gimbal said. The code I posted was an attempt to get OP's code to work, but I believe the answer is to use Swing.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Trying to make a screen black for a buffered image

    Or not use Swing at all ;) For simplicity I tended to prefer to just go bare-bones Canvas + BufferStrategy when I punished myself with Java2D game development.

    (assumption that it is a game, but when you have an update()/render() loop then it is a rather safe assumption)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    15

    Default Re: Trying to make a screen black for a buffered image

    Yeah, fair enough. I would perhaps argue that any benefit you get from going barebones is outweighed by losing simplicity, but to each their own.

    Any "real" game should be using OpenGL (through JOGL or LWJGL or libGDX) anyway, so debating the merits of Swing or AWT is a bit of a moot point. It's fine (and in fact, I encourage) learning the basics in Java2D, but anything other than the basics is going to need OpenGL.

    So since using AWT is more complicated and doesn't give you the power that OpenGL does, I don't see much point in using it.

    But there are a million different "right" ways in programming, so to each their own!
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

Similar Threads

  1. Robot screen capture returns black image
    By Jaeela in forum New To Java
    Replies: 1
    Last Post: 11-30-2012, 09:51 AM
  2. Replies: 1
    Last Post: 08-27-2012, 08:25 PM
  3. problem about Unknown Black Screen
    By lawlawlaws in forum Java 2D
    Replies: 1
    Last Post: 02-02-2012, 02:08 AM
  4. Problem about Black Screen
    By lawlawlaw in forum New To Java
    Replies: 2
    Last Post: 01-26-2012, 04:18 PM
  5. How can I make a buffered image solid?
    By rajkobie in forum AWT / Swing
    Replies: 2
    Last Post: 04-17-2011, 08:35 AM

Posting Permissions

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