Results 1 to 2 of 2
  1. #1
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    6

    Default [SOLVED] Double Buffering

    Can someone please help me make this code use double buffering.

    Java Code:
    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    import java.awt.image.RescaleOp;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    
    
    public class Window extends Applet implements Runnable 
    {
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = -8255319694373975038L;
    	
    	private PictureGenerator myImageGenerator;
    	private Image myImage;
    	private Thread myThread;
    	private BufferedImage bi;
    	
    	float[] scales = { 1f, 1f, 1f, 0.5f };
        float[] offsets = new float[4];
        RescaleOp rop;
        
        private float trans;
    
    	
    	public void init()
    	{
    		this.setSize(400, 300);
    		setBackground(Color.WHITE);
    		trans = 1;
    		this.setupImage();
    		this.start();
    	}
    	
    	public void start(){
    		myThread = new Thread(this);
    		myThread.start();
    	}
    
    	public void stop()
    	{
    		myThread = null;
    	}
    
    	public void run()
    	{
    		while(true)
    		{
    			try {
    				myThread.sleep(100);
    				if(trans <= -1)
    				{
    					trans = 1;
    				}
    				trans -= .01;
    				setOpacity(trans);
    				this.update();
    			} catch (InterruptedException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    	}
    
    	private void setupImage()
    	{
    		try {
                BufferedImage img = ImageIO.read(new File("images.jpeg"));
                int w = img.getWidth(null);
                int h = img.getHeight(null);
                bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
                Graphics g = bi.getGraphics();
                g.drawImage(img, 0, 0, null);
    
            } catch (IOException e) {
                System.out.println("Image could not be read");
                System.exit(1);
            }
            setOpacity(trans);
    
    	}
    	
    	private void setOpacity(float opacity) {
            scales[3] = opacity;
            rop = new RescaleOp(scales, offsets, null);
        }
    
    	public void paint(Graphics g)
    	{
    		Graphics2D g2d = (Graphics2D)g;
    		g2d.drawImage(bi, rop, 0, 0);
    	}
    	
    	public void update()
    	{
    		this.repaint();
    	}
    }
    My IP address is 127.0.0.1

  2. #2
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    6

    Default NVM found answer

    NVM found answer

    Java Code:
    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    import java.awt.image.RescaleOp;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    
    
    public class Window extends Applet implements Runnable 
    {
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = -8255319694373975038L;
    
    	private static final float MAX_TRANS = 3;
    
    	private static final float MIN_TRANS = -.5f;
    	
    	private PictureGenerator myImageGenerator;
    	private Image myImage;
    	private Thread myThread;
    	private BufferedImage bi;
    	
    	float[] scales = { 1f, 1f, 1f, 0.5f };
        float[] offsets = new float[4];
        RescaleOp rop;
        
        private float trans;
        
        private Image dbImage;
        private Graphics dbg;
    
    	
    	public void init()
    	{
    		this.setSize(400, 300);
    		setBackground(Color.WHITE);
    		trans = MAX_TRANS;
    		this.setupImage();
    		this.start();
    	}
    	
    	public void start(){
    		myThread = new Thread(this);
    		myThread.start();
    	}
    
    	public void stop()
    	{
    		myThread = null;
    	}
    
    	public void run()
    	{
    		while(true)
    		{
    			try {
    				myThread.sleep(10);
    				if(trans <= MIN_TRANS)
    				{
    					trans = MAX_TRANS;
    				}
    				trans -= .005;
    				setOpacity(trans);
    				this.repaint();
    			} catch (InterruptedException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    	}
    
    	private void setupImage()
    	{
    		try {
                BufferedImage img = ImageIO.read(new File("images.jpeg"));
                int w = img.getWidth(null);
                int h = img.getHeight(null);
                bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
                Graphics g = bi.getGraphics();
                g.drawImage(img, 0, 0, null);
    
            } catch (IOException e) {
                System.out.println("Image could not be read");
                System.exit(1);
            }
            setOpacity(trans);
    
    	}
    	
    	private void setOpacity(float opacity) {
            scales[3] = opacity;
            rop = new RescaleOp(scales, offsets, null);
        }
    
    	public void paint(Graphics g)
    	{
    		Graphics2D g2d = (Graphics2D)g;
    		g2d.drawImage(bi, rop, 0, 0);
    	}
    	
    	public void update(Graphics g)
    	{
    		if (dbImage == null)
    		{
    
    		      dbImage = createImage (this.getSize().width, this.getSize().height);
    		      dbg = dbImage.getGraphics ();
    
    		}
    
    		// clear screen in background
    		dbg.setColor (getBackground ());
    		dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
    
    		// draw elements in background
    		dbg.setColor (getForeground());
    		paint (dbg);
    
    		// draw image on the screen
    		g.drawImage (dbImage, 0, 0, this);
    	}
    }
    My IP address is 127.0.0.1

Similar Threads

  1. Replies: 0
    Last Post: 06-28-2008, 09:23 PM
  2. Double Buffering problem
    By aprenz in forum Java Applets
    Replies: 0
    Last Post: 05-28-2008, 04:26 AM
  3. Calculating sin of a double value
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 08:13 PM
  4. transforming double to int
    By AlejandroPenton in forum New To Java
    Replies: 2
    Last Post: 12-11-2007, 01:34 AM
  5. Getting smallest possible Double value
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-06-2007, 02:15 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
  •