C++基础图形编程
C++编程语言是一种多功能的编程语言。使用C++,你也可以创建低端图形,例如,可以使用C++创建基本的形状和文字(带时尚字体),并为它们添加颜色。
可以使用终端或命令提示符在C++中进行图形编程,或者可以下载DevC++编译器来创建图形程序。
对于终端,你需要将graphics.h库添加到GCC编译器中。为此,你需要输入以下命令。
>sudo apt-get install build-essential >sudo apt-get install libsdl-image1.2 libsdl-image1.2-dev guile-2.0 \ guile-2.0-dev libsdl1.2debian libart-2.0-dev libaudiofile-dev \ libesd0-dev libdirectfb-dev libdirectfb-extra libfreetype6-dev \ libxext-dev x11proto-xext-dev libfreetype6 libaa1 libaa1-dev \ libslang2-dev libasound2 libasound2-dev >sudo make install sudo cp /usr/local/lib/libgraph.* /usr/lib
依次输入以上所有命令,即可成功在终端的GCC编译器中安装graphics.h库。
另一种方法是安装DevC++编译器。
graphics.h库 − graphics.h库用于向C++程序添加图形。对于图形编程,这是一个必须包含的库,因为它包含所有必需的方法。
在C++程序中包含图形的语法:
语法
#include<graphics.h>
示例
展示C++图形编程实现的程序:
#include<graphics.h> #include<isotream.h> using namespace std; int main() { int gd = DETECT, gm; initgraph(−gd, −gm, "C:\TC\BGI"); rectangle(100,100,200,200); getch(); closegraph(); return 0; }
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
输出
C++图形编程的其他常用函数:
arc() − 创建给定角度和给定半径的弧。
bar() − 创建具有给定坐标的条形。
circle() − 创建给定半径的圆。
closegraph() − 关闭图形模式并释放内存块。
ellipse() − 创建具有给定长轴和短轴的椭圆。
floodfill() − floodfill用于将特定颜色填充到给定坐标的特定点。
line() − 创建具有给定起点和终点的线。
rectangle() − 创建具有给定坐标的矩形。
广告