3.3.2 Pixelmap and EventHandler

 The following MainView class is an example to create a pixelmap and draw colored points on it, and copy the pixelmap onto a window by an eventhandler for ExposureMask.
Note:
1. Define your own top level application view class(MainView) to be a subclass of OZ::ApplicationView class.
2. Define a member varialbe for Pixelmap class to store colored points.
3. Define a member variable for DC class which can be used as a device(graphics) context for the pixelmap.
4. Define a constructor to intialize the above member variables.
5. Define a draw eventhandler method to copy the pixelmap to your window.
6. In main funcition, create create an instance of OZ::Application class, and an instance of your own application view class(MainView).
7. Call realize method of the instance of the application view.
8. Call run method of the instance of Application to enter an event loop.





//>
//ColorModel.cpp
//Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED.

#include <oz++/motif/ApplicationView.h>
#include <oz++/motif/DC.h>
#include <oz++/motif/Color.h>
#include <oz++/motif/Pixelmap.h>

namespace OZ {

class MainView :public ApplicationView {
private:
  SmartPtr<Pixelmap> pixelmap;
  SmartPtr<DC>       back;

  void    draw(Event& event)
  {
    DC  dc(this);
    dc.copyArea(pixelmap->get(), 0, 0, 150,150, 20, 20);
  }

public:
  MainView(Application& applet, const char* name, Args& args)
  :ApplicationView(applet, name, args)
  {
    Display* display = getDisplay();
    const int num = 64;//256;
    const int US = 65535;
    pixelmap = new Pixelmap(display, num*2, num*2);

    back = new DC(display, pixelmap->get());
    Color   color(display);
    back -> setForeground(color.allocNamedColor("white"));
    back -> fillRectangle(0, 0, num*2, num*2);
    for (int r = 0; r<num; r+=2 ) {
      for(int g = 0; g<num; g+=2) {
        for(int b = 0; b<num; b+=2) {
          XColor c;
          c.red   = US-(num-r)*US/num;;
          c.green = US-(num-g)*US/num;  
          c.blue  = US-(num-b)*US/num; 

          back->setForeground(color.allocColor(&c));
               back->fillRectangle(b + r, g + r, 2, 2); 
        }
      }
    }

    addEventHandler(ExposureMask, this, 
                  (Handler)&MainView::draw, NULL);
  }

  ~MainView()
  {
  }
};

}

//
int main(int argc, char** argv)
{
  try {
    const char*  appclass = argv[0];
    Application applet(appclass, argc, argv);

    Args args;
    args.set(XmNgeometry, "300x300");
    MainView view(applet, argv[0], args);
    view.realize();
    applet.run();
  } catch (Exception& ex) {
    ex.display();
  }
  return 0;
}



Last modified: 1 Jan 2017

 Last modified: 1 Jan 2017

Copyright (c) 2000-2017 Antillia.com ALL RIGHTS RESERVED.