Results 1 to 2 of 2
  1. #1
    Somelauw is offline Member
    Join Date
    Nov 2009
    Posts
    10
    Rep Power
    0

    Default My animation flickers.

    I'm trying to draw an animation but for some reason, it flickers.

    Here is my code:
    Java Code:
    package pong;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JFrame;
    import javax.swing.RepaintManager;
    import javax.swing.Timer;
    
    public class Field extends JFrame{
        Timer timer;
        Ball ball = new Ball(200, 200);
    
        public Field() {
            setTitle("Pong");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(400, 400);
            setVisible(true);
            getContentPane().setBackground(Color.WHITE);
    
            new Timer(100, new ActionListener() {
    
                public void actionPerformed(ActionEvent e) {
                    ball.update();
                    repaint();
                }
            }).start();
        }
    
        @Override
        public void paint(Graphics g) {
            super.paint(g);
            ball.draw(g);
        }
    
    }

  2. #2
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Don't draw directly on the JFrame for by doing so you lose Swing's automatic double buffering (and can also cause other problems). Instead draw in a paintComponent override (not paint) of a JPanel and place the JPanel in the JFrame's content pane.
    Last edited by curmudgeon; 05-17-2010 at 10:23 PM.

Similar Threads

  1. Animation with Animated GIF
    By JavaBean in forum Java 2D
    Replies: 3
    Last Post: 06-04-2011, 05:26 PM
  2. need help about animation ?
    By h9h in forum Java 2D
    Replies: 1
    Last Post: 10-30-2009, 12:41 PM
  3. JTextArea Animation
    By hoglu in forum New To Java
    Replies: 1
    Last Post: 12-18-2008, 12:44 PM
  4. Animation Applet
    By Unome in forum Java Applets
    Replies: 7
    Last Post: 10-17-2008, 08:34 AM
  5. GUI Animation
    By serfster in forum New To Java
    Replies: 2
    Last Post: 06-11-2008, 04:37 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
  •