Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Feb 2010
    Posts
    95
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Move rectangle from position += 1 without flicker in applet

    Hi
    Im trying to move a filled rectangle by increasing the x-position by one in a applet, but it flickers. Im not sure if the code below is the right way to go but this is how I have programmed?

    Code:
    public class Game extends Applet implements Runnable
    {
        private BufferedImage bi = null;
        private TexturePaint paint = null;
        private Graphics2D g2d = null;
        private Rectangle rect = new Rectangle(0,0,50,50);
        private Boolean isRunningFirstTime = true;
        private Thread thread = null;
        private int x = 0;
    
        public void init()
        {
        }
    
        public void paint(Graphics g)
        {
            update(g);
        }
    
        public void update(Graphics g)
        {
            if (isRunningFirstTime)
            {
                bi = new BufferedImage(50, 50, BufferedImage.TYPE_INT_RGB);
                Graphics2D g2 = bi.createGraphics();
                g2.setColor(Color.red);
                g2.fillRect(0, 0, 50, 50);
                paint = new TexturePaint(bi, new Rectangle(50, 50));
                isRunningFirstTime = false;
            }
    
            g2d = (Graphics2D) g;
            g2d.setColor(Color.white);
            g2d.clearRect(0, 0, getWidth(), getHeight());
            g2d.setPaint(paint);
            g2d.fill(rect);
        }
    
        public void run()
        {
            while (thread != null)
            {
                    rect.setLocation(x+=1, 0);
                    repaint();
                    try
                    {
                            Thread.sleep(10);
                    }
                    catch (InterruptedException error)
                    {
                    }
            }
    
        }
    
        public void start() {
                if (thread == null) {
                        thread = new Thread(this);
                        thread.start();
                }
        }
    
        public void stop() {
                thread = null;
        }
    }

    As I said, it flickers! How do I solve this?

  • #2
    New to the CF scene
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have a question for you before I answer, are you using RTP, because I have heard rumours that the ready to program IDE gives the flickering problem. Personally I do not make java applets so I cannot help you out there.

    What I suggest is to do a quick search on google if there is an external class or a JDK class that can be imported and offers to load the java animations in the computer's RAM so the flickering disappears.

  • #3
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    just increase the time limit for the thread.
    here you have set it to 100..add a few more zeros and you would do fine


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •