Results 1 to 4 of 4

Thread: Finally done :D

  1. #1
    Shadaw is offline Member
    Join Date
    Dec 2008
    Posts
    4
    Rep Power
    0

    Default Finally done :D

    I finally finished my pong program. However, as it seems to apply to every applet I make, what the heck is up with the constant flickering on the screen? It gets worse and worse as the game (or program) continues. I tried changing the refresh rate, but that only makes the flickering a little harder to see but in the long run doesn't matter because the blinking becomes so excessive you can hardly even see the graphics I've coded onto the screen. Are applets just like this or can I get an opinion on how to fix it? If you need me source code, ask and I'll be happy to post it.

    Thanks,
    Shadaw

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Ways to eliminate flicker in AWT animations:
    1 override the update method to call paint
    Java Code:
    public void update(Graphics g) {
        paint(g);
    }
    2 use limited repaints, viz, the repaint method with four parameters
    3 use an offscreen image, aka, double buffering.
    Here's a demonstration of option 3:
    Java Code:
    // <applet code="Anima" width="600" height="300"></applet>
    import java.applet.Applet;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Anima extends Applet {
        SimpleAnimation simple     = new SimpleAnimation();
        BufferedAnimation buffered = new BufferedAnimation();
        AnimaEngine engine;
    
        public void init() {
            Panel animaPanel = new Panel(new GridLayout(1,0,10,0));
            animaPanel.setBackground(Color.black);
            animaPanel.add(simple);
            animaPanel.add(buffered);
            Animable[] animables = { simple, buffered };
            engine = new AnimaEngine(animables);
            setLayout(new BorderLayout());
            add(animaPanel);
            add(getLabelPanel(), "Last");
            addListeners();
        }
    
        private Panel getLabelPanel() {
            Panel panel = new Panel(new GridLayout(1,0,10,0));
            panel.setBackground(Color.black);
            panel.add(getLabel("simple",   Color.white));
            panel.add(getLabel("buffered", Color.white));
            return panel;
        }
    
        private Label getLabel(String s, Color color) {
            Label label = new Label(s, Label.CENTER);
            label.setBackground(color);
            label.setFont(new Font("dialog", Font.PLAIN, 20));
            return label;
        }
    
        private void addListeners() {
            new Thread(new Runnable() {
                public void run() {
                    // Wait for gui to stabilize to avoid generating
                    // events before components are fully initialized.
                    try {
                        Thread.sleep(250);
                    } catch(InterruptedException e) {
                        System.out.println("addComponentListeners interrupted");
                    }
                    simple.addComponentListener(resizer);
                    buffered.addComponentListener(resizer);
                }
            }).start();
        }
    
        public void start() {
            engine.start();
        }
    
        public void stop() {
            engine.stop();
        }
    
        private ComponentListener resizer = new ComponentAdapter() {
            public void componentResized(ComponentEvent e) {
                simple.reset();
                buffered.reset();
            }
        };
    }
    
    class SimpleAnimation extends Panel implements Animable {
        Point center = new Point(100, 150);
        int R = 24;
        int dx = 3;
        int dy = 2;
    
        public void paint(Graphics g) {
            g.setColor(Color.white);
            g.fillRect(0, 0, getWidth(), getHeight());
            g.setColor(Color.red);
            g.fillOval(center.x-R, center.y-R, 2*R, 2*R);
        }
    
        public void advance() {
            if(center.x - R + dx < 0 || center.x + R + dx > getWidth()) {
                dx *= -1;
            }
            if(center.y - R + dy < 0 || center.y + R + dy > getHeight()) {
                dy *= -1;
            }
            center.x += dx;
            center.y += dy;
            repaint();
        }
    
        public void reset() {
            center.setLocation(100, 150);
        }
    }
    
    class BufferedAnimation extends Panel implements Animable {
        Point center = new Point(225, 200);
        int R = 24;
        int dx = -3;
        int dy = 2;
        Image image;
        Graphics ig;
    
        public void paint(Graphics g) {
            g.setColor(Color.blue);
            g.fillOval(center.x-R, center.y-R, 2*R, 2*R);
        }
    
        public void update(Graphics g) {
            if(image == null) {
                image = createImage(getWidth(), getHeight());
                ig = image.getGraphics();
            }
            ig.setColor(Color.white);
            ig.fillRect(0, 0, getWidth(), getHeight());
            paint(ig);
            g.drawImage(image, 0, 0, this);
        }
    
        public void advance() {
            if(center.x - R + dx < 0 || center.x + R + dx > getWidth()) {
                dx *= -1;
            }
            if(center.y - R + dy < 0 || center.y + R + dy > getHeight()) {
                dy *= -1;
            }
            center.x += dx;
            center.y += dy;
            repaint();
        }
    
        public void reset() {
            center.setLocation(225, 200);
            image = null;
            ig.dispose();
        }
    }
    
    interface Animable {
        public void advance();
    }
    
    class AnimaEngine implements Runnable {
        Animable[] animables;
        Thread thread;
        long delay = 40;
        boolean animating = false;
    
        public AnimaEngine(Animable[] animables) {
            this.animables = animables;
        }
    
        public void run() {
            while(animating) {
                try {
                    Thread.sleep(delay);
                } catch(InterruptedException e) {
                    animating = false;
                }
                for(int i = 0; i < animables.length; i++) {
                    animables[i].advance();
                }
            }
        }
    
        public void start() {
            if(!animating) {
                animating = true;
                thread = new Thread(this);
                thread.start();
            }
        }
    
        public void stop() {
            animating = false;
            if(thread != null) {
                thread.interrupt();
                thread = null;
            }
        }
    }

  3. #3
    Shadaw is offline Member
    Join Date
    Dec 2008
    Posts
    4
    Rep Power
    0

    Default

    how does public void update work?
    I see the way it's intended to work, but what change must i make to all of my other painted graphics in order to compensate for the constant lines that public void update leaves? and thanks, the fix certainly did work, i just have this little problem now.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    how does public void update work?
    This is best explained in the Method Detail section for the update method in the Component api. At the end of the description is a link to a helpful article with more discussion about AWT drawing.

    but what change must i make to all of my other painted graphics in order to compensate for the constant lines that public void update leaves?
    Not sure what you mean by "the constant lines that public void update leaves?"

Similar Threads

  1. How to use Finally
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:40 PM
  2. Try---finally
    By javarishi in forum New To Java
    Replies: 14
    Last Post: 04-09-2008, 10:34 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
  •