Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-29-2010, 09:08 PM   PM User | #1
earni23
New Coder

 
Join Date: Feb 2010
Posts: 95
Thanks: 14
Thanked 0 Times in 0 Posts
earni23 has a little shameless behaviour in the past
Move rectangle from position += 1 without flicker in applet

Hi
I´m trying to move a filled rectangle by increasing the x-position by one in a applet, but it flickers. I´m 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?
earni23 is offline   Reply With Quote
Old 10-05-2010, 11:37 PM   PM User | #2
iPromote4You
New to the CF scene

 
Join Date: Oct 2010
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
iPromote4You is an unknown quantity at this point
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.
iPromote4You is offline   Reply With Quote
Old 11-03-2012, 08:47 AM   PM User | #3
SouvikRg
New to the CF scene

 
Join Date: Nov 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
SouvikRg is an unknown quantity at this point
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
SouvikRg is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:25 PM.


Advertisement
Log in to turn off these ads.