Results 1 to 6 of 6
  1. #1
    timkd127 is offline Member
    Join Date
    Dec 2008
    Posts
    41
    Rep Power
    0

    Default Using BufferedImage

    Im doing a graphics project that invloves changing the x,y, and z coordinates of a 3d object both simultaneously and independently.The object is rendered from a 3dm file. Im getting a significant amount of flicker from rerendering the object. I was wondering if someone could tell me how to store the retransformed object as a BufferedImage.

  2. #2
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    8

    Default

    Hi Tim!

    What are the odds? :D Here's a bouncing ball example implemented with a double buffer. This is the important class:
    Java Code:
    package task1;
    
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import java.util.*;
    
    public class JCanvas extends javax.swing.JPanel implements java.beans.Customizer {
    
        private Object bean;
        protected Set<Ball> balls = new HashSet<Ball>();
        protected Random random = new Random(System.currentTimeMillis());
        protected BufferedImage buffer = null;
    
        public JCanvas() {
            initComponents();
            setDoubleBuffered(true);
            addBall();
            addComponentListener(new ComponentAdapter() {
                public void componentResized(ComponentEvent evt) {
                    if (getWidth() > 0 && getHeight() > 0) {
                        buffer = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
                    }
                }
            });
            (new Thread(new Runnable() {
                public void run() {
                    while (true) {
                        for (Ball ball : balls) {
                            ball.move(getSize());
                        }
                        repaint();
                        try {
                            Thread.sleep(20);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            })).start();
        }
    
        public void setObject(Object bean) {
            this.bean = bean;
        }
        
        public void update(Graphics g) {
            paint(g);
        }
        
        public void paint(Graphics g) {
            if (buffer != null) {
                Graphics graphics = buffer.getGraphics();
                graphics.setColor(getBackground());
                graphics.fillRect(0, 0, getWidth(), getHeight());
                for (Ball ball : balls)
                    ball.paint(graphics);
                g.drawImage(buffer, 0, 0, null);
            }
        }
        
        public void addBall() {
            int axialSpeed = 300;
            int radius = random.nextInt(21) + 10;
            Point p = getSize().width > 0 && getSize().height > 0 ? new Point(radius + random.nextInt(getSize().width - radius * 2), radius + random.nextInt(getSize().height - radius * 2)) : new Point(radius, radius);
            Point v = new Point();
            while (Math.abs(v.x) <= 100 || Math.abs(v.y) <= 100) {
                v = new Point(random.nextInt(axialSpeed + 1) - axialSpeed / 2, random.nextInt(axialSpeed + 1) - axialSpeed / 2);
            }
            balls.add(new Ball(
                    p,
                    v,
                    radius,
                    new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255), random.nextInt(55) + 200)
                    ));
        }
    }
    Do all your drawing on the buffered image and draw the image on the component. This will get rid of the flickering.

    I'm buzy learning OpenGL in C++. Java is so easy. No wonder my teacher always said: "Wait 'till you get into real programming with C++". I looked for a nice book in the shops and only found one for sale. Why code in C++ if you can use C#, Java, VB or whatever right? Personally, I still think Java is the best programming language ever. Really, C++ is crazy. Even Matlab looks friendlier! :D Just thought I should comment. ;) Since there is another Tim on this planet interested in the awesome world of graphics!

    Hope this helps
    Tim
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Here's a bouncing ball example implemented with a double buffer.
    The code you posted is taken from old code used to do custom painting on AWT components. This is NOT the way it should be done in Swing.

    1) Swing is double buffered by default. There is no need to use setDoubleBuffered(true).

    2) There is not need to override the update() or paint() methods. Get rid of them.

    3) Custom paint is done by overriding the paintComponent() method.

    4) Animation should be done by using a Swing Timer, not a Thread with sleep.

  4. #4
    timkd127 is offline Member
    Join Date
    Dec 2008
    Posts
    41
    Rep Power
    0

    Default

    Thanks alot Tim! Couldnt have answered my question any better. Right now all the work i do is either in java or assembly language. I start learning c++ next semester:D, i cant wait. From what ive heard/read its more much powerful when it comes to graphical applications.

  5. #5
    timkd127 is offline Member
    Join Date
    Dec 2008
    Posts
    41
    Rep Power
    0

    Default

    P.S. out of curiosity is there any particular reason your using a HashSet to store the new ball instead of an array or LinkedList?

  6. #6
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    8

    Default

    Quote Originally Posted by camickr View Post
    The code you posted is taken from old code used to do custom painting on AWT components. This is NOT the way it should be done in Swing.

    1) Swing is double buffered by default. There is no need to use setDoubleBuffered(true).

    2) There is not need to override the update() or paint() methods. Get rid of them.

    3) Custom paint is done by overriding the paintComponent() method.

    4) Animation should be done by using a Swing Timer, not a Thread with sleep.
    Thank you for pointing this out camickr. :) Just wanted to demonstrate the technique. All of this is overkill anyway. ;)

    Quote Originally Posted by timkd127 View Post
    P.S. out of curiosity is there any particular reason your using a HashSet to store the new ball instead of an array or LinkedList?
    Nope, no reason really. This is actually a practical solution I modified. Normally I just use Vector<T>, but I felt like doing something special.

    C++ is not to be taken lightly though. I've spent about a month on it and it's like chewing on dry bread when you're used to chocolates. :D

    Good luck Tim!
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

Similar Threads

  1. BufferedImage imageScaling
    By MINGxDOG in forum New To Java
    Replies: 2
    Last Post: 11-17-2009, 04:04 PM
  2. BufferedImage through FTP
    By dudejonne in forum New To Java
    Replies: 7
    Last Post: 11-05-2009, 06:36 PM
  3. Replies: 0
    Last Post: 07-02-2008, 09:06 PM
  4. Convert Byte [] to BufferedImage
    By Smily in forum Advanced Java
    Replies: 3
    Last Post: 04-28-2008, 06:54 PM
  5. BufferedImage to Byte
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-22-2008, 09:17 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
  •