Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7995111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:18:53+00:00 2026-06-04T14:18:53+00:00

I can successfully compiled and run the Hello World code. Now I want to

  • 0

I can successfully compiled and run the Hello World code.
Now I want to do something like animation.

I first create a rectangle class to implement draw() from Fl::widget

class myRect: public Fl_Widget {
private:
    Fl_Color color;
    void draw(){
        fl_color(color);
        fl_rectf(x(),y(),w(),h(),color);
    }
public:
    myRect(int X,int Y,int W,int H, Fl_Color c) : Fl_Widget(X,Y,W,H),color(c) {}
};



int main (int argc, char ** argv)
{
    Fl_Window *window = new Fl_Window (300, 180, "FLTK Test");

    vector<myRect*> allRect;
    for(int i=0; i<10; ++i){
        allRect.push_back(new myRect ((i*10)%100,100,50,50,i%256));
    }
    window->end();
    window->show();

    return Fl::run();
}

The code above can run as I expected.
But Now I want to show the rectangles one by one, with some time interval such as 1 second.
Make it just like animation.

I have read the official document but I still have no idead about that.
Please give me some information. Thanks !!


Thanks to DejanLekic, I revised my code as below:

#include <iostream>
#include <vector>
#include <FL/Fl.H>
#include <FL/Fl_Widget.H>
#include <FL/Fl_Double_Window.H>
#include <FL/fl_draw.H>

using namespace std;

class myRect: public Fl_Widget {
private:
    Fl_Color color;
    void draw(){
    fl_color(color);
    fl_rectf(x(),y(),w(),h(),color);
}

public:
    myRect(int X,int Y,int W,int H, Fl_Color c)
        :Fl_Widget(X,Y,W,H),color(c) {}
};

vector<myRect*> allRect;

void winUpdate(void *data)
{
    static unsigned i = 0;
    Fl_Double_Window *o = (Fl_Double_Window*)data;
    if(i < allRect.size()){
        o->add(allRect[i]);
        if(i>=3) o->remove(allRect[i-3]);
        o->redraw();
        Fl::add_timeout(0.5,winUpdate,data);
        ++i;
    }
}

int main (int argc, char ** argv)
{
    for(int i=0; i<8; ++i){
        allRect.push_back(new myRect(i*30,i*30,50,50,i));
    }
    Fl_Double_Window *window = new Fl_Double_Window (400, 400, "FLTK Test");
    Fl::add_timeout(2,winUpdate,window);
    window->end();
    Fl::visual(FL_DOUBLE|FL_INDEX);
    window->show();
    return Fl::run();
}

It seems to run well, but I’m not sure whether it is correct or not.
If there is any problem, please let me know. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T14:18:55+00:00Added an answer on June 4, 2026 at 2:18 pm

    Cory, you are on the right path.

    Here is a complete example how to do a simple 2D animation using FLTK’s drawing capabilities: http://seriss.com/people/erco/fltk/#AnimateDrawing

    Similar thing using OpenGL: http://seriss.com/people/erco/fltk/#OpenGlInterp

    The key, in both examples is in the Fl::add_timeout(0.25, Timer_CB, (void*)this); line, and in the Timer_CB() static (callback) method. Both examples are nice and easy and I am confident you will understand them instantly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can successfully create QR Code PNG images with ZXing but there is no
I can't seem to run a simple Hello World program using eclim. I followed
I have an application in linux, which is compiled successfully. I want to run
I have my program written in C++ and it is can be successfully compiled
I can successfully extract the data from the database however am having trouble now
I successfully compiled and installed s3fs (http://code.google.com/p/s3fs/) on my Fedora 14 machine. I included
I wrote an application in the Eclipse, which was successfully compiled and run. After
I can successfully download files from server using the macro below, but sometimes the
I can successfully send emails by using CakeEmail class of CakePHP. But how can
I can successfully do this: App.SomeCollection = Backbone.Collection.extend({ comparator: function( collection ){ return( collection.get(

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.