FLTK, creating a rounded box

Hi guys,

The problem says: Draw a box with rounded corners. Define a class Box, consisting of four lines and four arcs.
So I wrote the below code for that exercise:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <Simple_window.h>
Simple_window win(Point(100,100), 600,400, "semi-ellipse");

struct Box: Shape{

	Box(Point p, int ww, int hh): w(ww), h(hh) 
    	{ add(Point(p.x-ww,p.y-hh));  }

	void d_l() const        //creating 4 lines
     {
	    Line hrz1 (Point(150,100), Point(400,100));
	    Line hrz2 (Point(150,300), Point(400,300));
	    Line ver1 (Point(507,150), Point(507,250));
	    Line ver2 (Point(41,150), Point(41,250));

		win.attach(hrz1);
		win.attach(hrz2);
		win.attach(ver1);
		win.attach(ver2);
     }

	void draw_lines() const      //creating 4 arcs
	{
		fl_arc(point(0).x,point(0).y,w,h,30,90);
		fl_arc(point(0).x,point(0).y,w,h,270,330);
		fl_arc(point(0).x,point(0).y,w,h,90,150);
		fl_arc(point(0).x,point(0).y,w,h,210,270);
	}
	
private:
	int w;
	int h;
};

int main()
{
    using namespace Graph_lib; 
	
	Box b(Point(100,100),100,50);
	win.attach(b);
        win.wait_for_button();
}

When I ran it I faced this exception:

Unhandled exception at 0x757FE9D7 (ole32.dll) in test.exe: 0xC0000005: Access violation reading location 0x00000004.


I know this refers to declaring "Simple_window win(Point(100,100), 600,400, "semi-ellipse");" in global state. But I did that because I had to do it. The problem is that how to attach lines and also object (here b) to Simple_window win in either parts (main() function and also Box struct).




the problem with the global variable might be that there're some initialization not done.

You don't need the gobal variable. Just pass the win object as a pointer to the box
Thanks for your reply. But I haven't been taught about the pointers until this part of the book (PPP). There should be certainly one another way.
No solution!?
The problem is attach() in d_l(). Why is this necessary? I mean why can't it be drawn in a common draw function?
>The problem is attach() in d_l(). Why is this necessary?

If there isn't attach() in d_l(), so how to attach those lines which are inside the d_l() to the Simple_window?

>why can't it be drawn in a common draw function?

In the outside of the Box class? If you mean that, no, because the exercise has wanted within the Box. Note to the context of the exercise, " ...Define a class Box, consisting of four lines and..."
Last edited on
Topic archived. No new replies allowed.