Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Fubarable

Thread: BufferedImage's raster

  1. #1
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default BufferedImage's raster

    Hi, I have created this simple application:
    Java Code:
    import java.awt.Canvas;
    import java.awt.Graphics;
    import java.awt.image.BufferStrategy;
    import java.awt.image.BufferedImage;
    import java.awt.image.DataBufferInt;
    import java.util.Random;
    
    import javax.swing.JFrame;
    
    
    public class Display extends Canvas implements Runnable{
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	public static final int WIDTH = 1280;
    	public static final int HEIGHT = 1024;
    	public static final String TITLE = "Title";
    	
    	private Thread thread;
    	private boolean running = false;
    	private BufferedImage img;
    	private Random rand;
    	
    	public Display(){
    		img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
    	}
    	
    	private void start() {
    		if(running)
    			return;
    		running = true;
    		rand = new Random();
    		thread = new Thread(this);
    		thread.start();
    		
    	}
    	
    	@Override
    	public void run() {
    		while (running){
    			render();
    		}
    	}
    	
    	
    	public void render(){
    		BufferStrategy bs = this.getBufferStrategy();
    		if(bs == null){
    			createBufferStrategy(2);
    			return;
    		}
    		
    		Graphics g = bs.getDrawGraphics();
    		
    		g.drawImage(img, 0, 0, null);
    		
    		g.drawImage(createImg(WIDTH, HEIGHT), 0, 0, null);
    			
    		g.dispose();
    		bs.show();
    		
    	}
    	
    	
    	
    	public static void main(String[] args) {
    		Display rc = new Display();
    		JFrame frame = new JFrame();
    		frame.add(rc);
    		frame.pack();
    		frame.setTitle(TITLE);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(WIDTH, HEIGHT);
    		frame.setLocationRelativeTo(null);
    		frame.setResizable(false);
    		frame.setVisible(true);
    		
    		rc.start();
    	}
    	
    	public BufferedImage createImg(int width, int height){
    		
    		BufferedImage imgg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    		int[] pixels = ((DataBufferInt)imgg.getRaster().getDataBuffer()).getData();
    		
    		for(int i = 0; i < pixels.length;i++){
    			pixels[i] = rand.nextInt();
    		}
    		
    		return imgg;
    	}
    	
    }
    because I wanted to play around with pixels.

    I was wondering how does the imgg in createImage() method change without me changing anything in the imgg?
    I just fill the pixels array with random numbers and the imgg changes without me actauly doing anything to the imgg.
    It would make sense if I would have to do imgg.getRaster().getDataBuffer().setData(pixels);
    I have a feeling that it has something to do with the raster.
    Last edited by PhQ; 09-30-2012 at 07:16 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: BufferedImage's raster

    Quote Originally Posted by PhQ View Post
    I was wondering how does the imgg in createImage() method change without me changing anything in the imgg?
    I just fill the pixels array with random numbers and the imgg changes without me actauly doing anything to the imgg.
    It would make sense if I would have to do imgg.getRaster().getDataBuffer().setData(pixels);
    I have a feeling that it has something to do with the raster.
    Let's look at what you're doing:

    Java Code:
            // create an empty BufferedImage and have imgg refer to it.
            BufferedImage imgg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    
            // extract the data model of the image. This is "live" data -- you change the values held by this array,
            // you will change the image itself.
            int[] pixels = ((DataBufferInt)imgg.getRaster().getDataBuffer()).getData();
    
            // and that's exactly what you do -- change the values held by the array. 
            for(int i = 0; i < pixels.length;i++){
                pixels[i] = rand.nextInt();
            }
    As per the comments, you're changing the raw ints that define the image, not a copy of the data model, but the actual model itself. It should come as no surprise that you'll see your "noise" image when you make these changes.
    PhQ likes this.

  3. #3
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: BufferedImage's raster

    Quote Originally Posted by Fubarable View Post
    Let's look at what you're doing:

    Java Code:
            // create an empty BufferedImage and have imgg refer to it.
            BufferedImage imgg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    
            // extract the data model of the image. This is "live" data -- you change the values held by this array,
            // you will change the image itself.
            int[] pixels = ((DataBufferInt)imgg.getRaster().getDataBuffer()).getData();
    
            // and that's exactly what you do -- change the values held by the array. 
            for(int i = 0; i < pixels.length;i++){
                pixels[i] = rand.nextInt();
            }
    As per the comments, you're changing the raw ints that define the image, not a copy of the data model, but the actual model itself. It should come as no surprise that you'll see your "noise" image when you make these changes.
    Makes sense. Thank you :)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Replies: 1
    Last Post: 07-18-2012, 10:50 PM
  2. Replies: 0
    Last Post: 04-02-2012, 01:48 PM
  3. Replies: 1
    Last Post: 05-26-2011, 12:37 PM
  4. Replies: 6
    Last Post: 01-15-2011, 04:03 PM
  5. Replies: 0
    Last Post: 10-21-2010, 12:09 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
  •