Course list http://www.c-jump.com/bcc/
FLTK classes Fl_Box and Fl_Image provide facilities to load and draw images in FLTK. In this lab we will display Joint Photographic Experts Group (JPEG) file. The Fl_JPEG_Image class supports loading, caching, and drawing of JPEG and File Interchange Format (JFIF) images.
When user clicks the button, the callback function will load, resize, and draw the image inside the Fl_Box widget:
The prototype for this project is Lab 4:
c255labs\labs\c255_lab04
Make a copy of Lab 4 subfolder, or download and unzip c255_lab04.zip
Rename the new folder as c255_lab06_jpeg.
Open solution file in Visual Studio and finish the usual steps to rename the project.
Start FLUID program and open project
c255labs\labs\c255_lab06_jpeg\fluid_project\CFluidWindow.fl
Use FLUID menu or Widget Bin to add the widgets to your window. Select Window win_app object in FLUID tree view. Use menu
New/Other/Box
to add Fl_Box widget to the window. It appears in the design view as a square box.
In the properties window, name the widget as box_jpeg_image.
Add a push button,
New/Buttons/Button
Name the button in the Properties/C++ as btn_show_text and btn_show_jpeg.
Specify button label in Properties/GUI as "Show JPEG"
Save FLUID project and generate C++ code by
File/Write Code...
Open solution
c255labs\labs\c255_lab06_jpeg\c255_lab06.sln
in Visual Studio.
Open CMainWindow.h in text editor. Add NEW callback code as shown:
// CMainWindow.h #ifndef _CMAINWINDOW_H_INCLUDED_ #define _CMAINWINDOW_H_INCLUDED_ #include "../fluid_project/CFluidWindow.h" // NEW #include <FL/Fl_Shared_Image.H> #include <FL/Fl_JPEG_Image.H> class CMainWindow : public CFluidWindow { Fl_Image* jpeg_image; // NEW public: CMainWindow(); // NEW void show(); void click_btn_show_jpeg(); // NEW // callback functions static void callback_window_closing(Fl_Widget* widg, void* userdata_); // NEW static void callback_btn_show_jpeg(Fl_Widget* widg, void* userdata_); };//class CMainWindow #endif // _CMAINWINDOW_H_INCLUDED_
Notice that the CMainWindow class has the data member
Fl_Image* jpeg_image;
Open CMainWindow.cpp in the text editor.
Download the new version of CMainWindow.cpp , or add NEW code manually:
// Lab 6: Fl_Box, Fl_Image, Fl_JPEG_Image // CMainWindow.cpp #include "CMainWindow.h" // NEW CMainWindow::CMainWindow() : jpeg_image( nullptr ) { } void CMainWindow::show() { // Window callbacks: win_app->callback( (Fl_Callback*)CMainWindow::callback_window_closing, (void*)(this) ); // NEW btn_show_jpeg->callback( (Fl_Callback*)CMainWindow::callback_btn_show_jpeg, (void*)(this) ); // Make the window visible: win_app->show(); } // callback functions void CMainWindow::callback_window_closing(Fl_Widget* widg, void* userdata_) { std::cout << "X button clicked -- exiting the program\n"; while( Fl::first_window() ) { Fl::first_window()->hide(); } } // NEW void CMainWindow::callback_btn_show_jpeg(Fl_Widget* widg, void* userdata_) { CMainWindow* win = static_cast< CMainWindow* > ( userdata_ ); win->click_btn_show_jpeg(); } // NEW void CMainWindow::click_btn_show_jpeg() { std::cout << "btn_show_jpeg click\n"; Fl_JPEG_Image* jpeg_image_orig = new Fl_JPEG_Image( "../../JPEG/bird.JPG" ); // copy allow us to resize the image: jpeg_image = jpeg_image_orig->copy( box_jpeg_image->w(), box_jpeg_image->h() ); delete jpeg_image_orig; box_jpeg_image->image( jpeg_image ); win_app->redraw(); }
Compile and test the program.
This is a self-learning lab. There are no files to submit. Make sure to complete all required steps to build and test the project on your own home computer.