void repaint(){
    if(!display){
      cout << "Repaint: You must first call initCanvas before "<<
	"using any graphics features.\n";
      exit(1);
    }

    if(globalRepaintFlag){
      int tl_x=0,tl_y=0,width=canvas_width(),height=canvas_height();
      XCopyArea(display, screenBG, screenTmp, gc, 
		tl_x, tl_y, width, height, tl_x, tl_y);

      curr_d = screenTmp;

      for(auto iter = spriteSet.begin(); iter != spriteSet.end(); iter ++){
	(*iter)->paint();
      } 

      XCopyArea(display, screenTmp, canvas_window, gc, 
		tl_x, tl_y, width, height, tl_x, tl_y);
    
      XSync(display, false);
      curr_d = canvas_window;
    }
  }
Turtle::Turtle(Composite* owner){
  double pts_body[4][2] = {{0, 15}, {20, 0}, {0, -15}, {0,15}};
  setColor(COLOR("red"), false);
  setFill();
  penDown();
  reset(canvas_width()/2, canvas_height()/2,pts_body, 4, owner);
}
  int initCanvas(const char window_title[], int width, int height){

    display = XOpenDisplay(NULL);    // Connect X server by opening a display
    //display = XOpenDisplay("localhost:11.0");    // Connect X server by opening a display

    if(!display){
      cerr<<"Unable to connect X server\n";
      return 1;
    }

    screen_number = DefaultScreen(display);

    if(width == -1){
      screen_width = (DisplayWidth(display, screen_number) -100);
      screen_height = (DisplayHeight(display, screen_number) -100);
    }
    else{
      screen_width = width;
      screen_height= height;
    }

    canvas_window = XCreateSimpleWindow(display,
					RootWindow(display, screen_number),
					0,0, // left top corner.  doesnt work.
					screen_width, screen_height,
					1,
					BlackPixel(display, screen_number),
					WhitePixel(display, screen_number));

    XSetStandardProperties(display, canvas_window, window_title,
			   window_title, None, NULL, 0, NULL);

    XSelectInput(display, canvas_window, 
		 ExposureMask|ButtonPressMask|ButtonReleaseMask|KeyPressMask|
		 ButtonMotionMask);

    XMapWindow(display, canvas_window);    // Map canvas window to display
    XSync(display, True);  // flush all output requests and discards events

    gc_vals.function = GXcopy;
    //  gc_vals.foreground = Color(COLOR("black"));
    gc_vals.line_width = 2;
    gc_vals.line_style = LineSolid;
    gc_vals.cap_style = CapButt;
    gc_vals.join_style = JoinMiter;

    gc = XCreateGC(display, canvas_window, 0 ,0);    // Create GC

    if((long int) gc < 0){
      //if(gc < 0){
      cerr<<"Unable to create graphic context.\n";
      return 2;                            // ERROR CODE 2: gc error
    }


    xfs = XLoadQueryFont(display, "-*-helvetica-bold-r-normal-*-24-*");
    if (!xfs) {
      xfs = XLoadQueryFont(display, "fixed");
    }
    if(!xfs){
      cout << "Cannot load font.  You will not be able to use Text.\n";
    }

    //XClearWindow(display, canvas_window);
    //XMapRaised(display, canvas_window);

    cmap = DefaultColormap(display, screen_number);
  
    // Flush all events to X server and wait
    XSync(display, False);

    screenBG =XCreatePixmap(display, canvas_window, 
			    screen_width, screen_height, 
			    XDefaultDepth(display, screen_number));
    //    XCopyArea(display, canvas_window, screenBG, gc, 0,0,screen_width, screen_height, 0,0);

    //  {int p; cin >> p;}
    wait(0.1);
    XSync(display, False);

    screenTmp=XCreatePixmap(display, canvas_window, screen_width, screen_height, 
			    XDefaultDepth(display, screen_number));

    curr_d = canvas_window;

    Rectangle r(canvas_width()/2.0, canvas_height()/2.0, 
		canvas_width(), canvas_height());
    r.setColor(COLOR("white"));
    r.setFill();
    r.imprint();


    return 0;
  }
int init_nip_stop_codons_plot(Tcl_Interp *interp,
			      int seq_id,
			      int result_id,
			      char *e_win,
			      char *c_win, 
			      Tcl_Obj *results,
			      int container_id,
			      int element_id,
			      char *element_type,
			      int line_width,
			      char *colour,
			      float tick_ht,
			      int orientation)
{
    seq_result *s_result;
    Graph *graph;
    configs *configure;
    plot_data *result;
    int i;
    int height;
    int seq_id_h, seq_id_v;

    s_result = seq_id_to_result(result_id);
    
    if (NULL == (result = (plot_data *)xmalloc(sizeof(plot_data))))
	return -1;
    
    if (NULL == (result->configure = (configs **)xmalloc(sizeof(configs*))))
	return -1;
    
    if (NULL == (configure = (configs *)xmalloc(sizeof(configs))))
	return -1;

    configure->position = 0.5;
    configure->x_direction = '+';
    configure->y_direction = '+';
    configure->height = tick_ht;
    configure->zoom = 0;
    configure->scroll = 0;

    result->configure[0] = configure;
    result->n_configure = 1;
    result->sf_m = 1.0;
    result->sf_c = 0.0;
    result->result_id = result_id;
    result->hidden = 0;
    result->line_width = line_width;
    result->colour = strdup(colour);
    result->len_ruler = 1;
    result->amp_ruler = 0;
    sprintf(result->tags, "id%d", result_id);

    graph = Tcl_GetGraphFromObj(results);
   
    if (orientation == HORIZONTAL) {
	seq_id_h = seq_id;
	seq_id_v = -1;
	height = canvas_height(interp, e_win);
	result->scale = SCALE_X;
    } else {
	seq_id_h = -1;
	seq_id_v = seq_id;
	height = canvas_width(interp, e_win);
	result->scale = SCALE_Y;
    }

    graph->dim.y0 = 1;
    graph->dim.y1 = height;

    /* graph->dim.y1 = tick_ht; */

    for (i = 0; i < graph->d_arrays[0].n_dlines; i++) {
	graph->d_arrays[0].d_array[i].y0 = 1;
	graph->d_arrays[0].d_array[i].y1 = tick_ht;

    }
    
    init_seq_element(interp, s_result, seq_id_h, seq_id_v, result, 
		     container_id, 
		     element_id, e_win, c_win, orientation, orientation,
		     graph, element_type);
    
    return 0;
}
 void resetTurtle(){
   _PsystemTurtle_->Sprite::reset(canvas_width()/2,canvas_height()/2);
 }