Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-24-2008, 01:45 AM
Zosden's Avatar
Senior Member
 
Join Date: Apr 2008
Posts: 386
Zosden is on a distinguished road
[SOLVED] Double Buffering
Can someone please help me make this code use double buffering.

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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-24-2008, 04:28 AM
Zosden's Avatar
Senior Member
 
Join Date: Apr 2008
Posts: 386
Zosden is on a distinguished road
NVM found answer
NVM found answer

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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Demonstration of animation using double buffering Java Tip SWT 0 06-28-2008 11:23 PM
Double Buffering problem aprenz Java Applets 0 05-28-2008 06:26 AM
Calculating sin of a double value Java Tip Java Tips 0 01-13-2008 10:13 PM
transforming double to int AlejandroPenton New To Java 2 12-11-2007 03:34 AM
Getting smallest possible Double value Java Tip Java Tips 0 12-06-2007 04:15 PM


All times are GMT +3. The time now is 11:51 AM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org