Moving a button and an image with each other

The question says:

Place an Image on top of a Button. Move both when the Button is pushed.

It's here (http://books.google.com/books?id=We21AwAAQBAJ&lpg=PP1&dq=programming%20principle%20and%20practice&pg=PA579#v=onepage&q&f=true) exercise no.3

Please have a look at this code:

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
#include <GUI.h>
using namespace Graph_lib;

//------------------------------------------------

class rotation : public Window {
public:
	rotation(Point xy, int w, int h, const string& title):
		Window(xy,w,h,title),
	b(Point(200,200), 100, 60, "Button", cb_b) {
		attach(b);
		attach(i);
	}

	static void cb_b(Address, Address pw) { reference_to<rotation>(pw).rotate(); }
	void rotate() { b.move(100,100); i.move(100,100);}
private:
	Button b;
	Image i(Point(200,200), "image.jpg");
};

//-------------------------------------------

int main(){

		rotation win(Point(100,100), 600, 400, "Rotation");
		return gui_main();
}


The problem is that, it is not possible to define an Image in public/private part of the class rotation. Image needs all arguments to be supplied even when declaring one. (Image is here http://www.stroustrup.com/Programming/Graphics/ in Graph.h) And if I define that Image in the body of main() function, so how to provide access for void rotate() to change its position with the position of button simultaneously.

I have tried some ways so far but no success.
Any idea?
Last edited on
If you don't have the necessary information for the image when rotation constructed you need to resort to a pointer to an image and new image when it's ready.

if you define the image in main() (before you define rotation) you may pass the pointer to the image as another paramater of the constructor of rotation
Thanks.

Pointers may help, especially here for make access but I haven't been taught pointers until now. The pointers are in next chapter(s).
Except from using pointers, do you have any other idea?
If line 19 is the problem, try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class rotation : public Window {
public:
	rotation(Point xy, int w, int h, const string& title):
		Window(xy,w,h,title),
	i(Point(200,200), "image.jpg"),
	b(Point(200,200), 100, 60, "Button", cb_b) {
		attach(b);
		attach(i);
	}

	static void cb_b(Address, Address pw) { reference_to<rotation>(pw).rotate(); }
	void rotate() { b.move(100,100); i.move(100,100);}
private:
	Button b;
	Image i(Point(200,200), "image.jpg");
};
WOW!!!
It's really so strange for me. How it can be possible please?

The constructor of Image (as is shown in one of the above links) is like below and shows that supplying all of the arguments are needed when making one Image, for example.

1
2
3
4
5
6
7
8
9
10
11
struct Image : Shape {
	Image(Point xy, string s, Suffix::Encoding e = Suffix::none);
	~Image() { delete p; }
	void draw_lines() const;
	void set_mask(Point xy, int ww, int hh) { w=ww; h=hh; cx=xy.x; cy=xy.y; }
	void move(int dx,int dy) { Shape::move(dx,dy); p->draw(point(0).x,point(0).y); }
private:
	int w,h,cx,cy; // define "masking box" within image relative to position (cx,cy)
	Fl_Image* p;
	Text fn;
};

So how it is possible to make an Image inside the class without any of its arguments but at the same time if we want to make another Image this time inside the body the main() function, it's not possible there!?
Can you please explain it?

To be honest, it was the first idea that I was thinking of it but since I thought that all the arguments are needed when making an image so I changed my mind for thinking about another method :)
Last edited on
It's really so strange for me. How it can be possible please?
What do you mean? The member constructor (of b and i) are called in the initialize list of the emcompassing class (rotation). This are only different notations.

I'm not sure whether I understand what you ask?
The image is shown when you call attach(i);

So if you wrote:
1
2
3
4
5
6
7
8
9
int main(){
		Image i(Point(200,200), "image.jpg"); // Note

		rotation win(Point(100,100), 600, 400, "Rotation");

		win.attach(i); // Note

		return gui_main();
}
It should work too.
I say, why it's not possible to define an Image this way Image i; in main function but it's possible to define that in private section of class (as has been defined, line 15)?
Last edited on
The difference between Image i; in main() and rotation is that in rotation it is actually only a declaration while in main() it's also a definition.

It's actually only a semantically difference. You have to provide the constructor anyway, just at different positions. Another way to provide the constructor (if your compiler supports it / only for constant values):
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
#include <GUI.h>
using namespace Graph_lib;

//------------------------------------------------

class rotation : public Window {
public:
	rotation(Point xy, int w, int h, const string& title):
		Window(xy,w,h,title),
	b(Point(200,200), 100, 60, "Button", cb_b) {
		attach(b);
		attach(i);
	}

	static void cb_b(Address, Address pw) { reference_to<rotation>(pw).rotate(); }
	void rotate() { b.move(100,100); i.move(100,100);}
private:
	Button b;
	Image i = Image(Point(200,200), "image.jpg");
};

//-------------------------------------------

int main(){
		Image i = Image(Point(200,200), "image.jpg");

		rotation win(Point(100,100), 600, 400, "Rotation");
		return gui_main();
}
OK. So how it is possible to declare (not define) and Image (e.g., Image i;) in private section of the class but not in the main() function. My question is this.
Topic archived. No new replies allowed.