EasyX 教程之入门篇
今天我们来分享一种绘图工具:EasyX。给大家讲解的语言为 C++,系统应为 Win10 或 Win11。
下载软件和 EasyX
首先我们要先下载支持 EasyX 的软件,我推荐 Visual Studio 2022。我们先从最基础的下载开始。
首先打开 Microsoft 的 Visual 下载界面,下载 Community 版本。注:千万别下载预览版!
等下载完成后,也就是下面的界面。
点开它,等下载好后勾选下面的选项。
然后进行配置即可。配置好后,应该就会出现下面的情况。
点击启动,然后按下面的步骤操作即可。
- 点击“创建新项目”;
- 选择 C++ 控制台应用并点击下一步;
现在 Visual Studio 2022 就配置好啦并且可以开始写代码啦!
注:以后每次写新的代码时都要按上述步骤操作。
接下来我们配置 EasyX,打开网页后就是下面的界面。
点击下载 EasyX,然后等文件下载好后点开它,并点击“下一步”,就会出现下图的弹窗,找到 Visual C++ 2022 那一栏,点击“安装”。
现在 EasyX 也安装完成啦!
配置额外头文件
习惯用万能头文件的 OIer 们可以看一看下面的步骤,因为 Visual 本身并不支持万能头文件。
首先打开和我相似或相同的路径。
接下来在 include 文件夹中新建一个名称为 bits 的文件夹,在里面再新建一个名称为 stdc++ 的 .h 文件,在这个 .h 文件中粘贴如下代码。
// C++ includes used for precompiling -*- C++ -*-
// Copyright (C) 2003-2014 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <http://www.gnu.org/licenses/>.
/** @file stdc++.h
* This is an implementation file for a precompiled header.
*/
// 17.4.1.2 Headers
// C
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <windows.h>//这一行为我自行添加的内容
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
现在,你就可以用万能头文件了。
EasyX 头文件
每次你在写 EasyX 代码时,都要添加以下两个头文件:
#include <easyx.h>
#include <graphics.h>
只有有了这两个头文件,EasyX 代码才能编译成功。
配置测试
我们建立一个 C++ 的控制台应用,在里面写入以下代码来查看 EasyX 是否配置成功。
#include <easyx.h>
#include <graphics.h>
#include <bits/stdc++.h>
using namespace std;
int main()
{
initgraph(480,600);
setlinecolor(WHITE);
setfillcolor(RED);
fillrectangle(150, 60, 200, 85);
Sleep(5000);
closegraph();
return 0;
}
如果上面的代码能编译成功,那我们的 EasyX 就配置好了。
建立窗口函数和关闭窗口函数
EasyX 是用窗口来绘图的,那么怎么建立这个窗口呢?这时候我们的 initgraph() 函数就派上用场了。
用法如下。
#include <easyx.h>
#include <graphics.h>
#include <bits/stdc++.h>
using namespace std;
int main()
{
initgraph(/*宽度*/,/*高度*/);
//例:initgraph(480, 600); 创建一个宽480像素、高600像素的窗口,EasyX 绘图窗口默认背景颜色为黑色,而窗口最左上角的位置的坐标为(0,0),最右下角的位置的坐标为(480,600)
return 0;
}
那么你要关闭绘图窗口时,就要用到 closegraph() 函数了,用法:
#include <easyx.h>
#include <graphics.h>
#include <bits/stdc++.h>
#include <windows.h>//Sleep()的头文件(不加也可以)
using namespace std;
int main()
{
initgraph(480, 600);
Sleep(5000);//等待5000毫秒(5秒)
closegraph();//关闭绘图窗口
return 0;
}
清空界面
当你想要清空界面时,就要用到 cleardevice() 这个函数了。
用法如下。
#include <bits/stdc++.h>
#include <easyx.h>
#include <graphics.h>
using namespace std;
int main()
{
initgraph(640, 480);
cleardevice();//清空界面所有内容
Sleep(5000);
closegraph();
return 0;
}
设置背景颜色
当你觉得背景颜色为黑色时不好看,想变换背景颜色时,就会用到 setbkcolor(/*在这里填入颜色*/)。
用法:
#include <bits/stdc++.h>
#include <easyx.h>
#include <graphics.h>
using namespace std;
int main()
{
initgraph(640, 480);
setbkcolor(WHITE); // 设置背景颜色为白色
cleardevice();//一定要在设置背景颜色后清空一下原来的界面才能显现出新的背景颜色
Sleep(1000);
setbkcolor(RED); // 设置背景颜色为红色
cleardevice();
Sleep(1000);
setbkcolor(BLUE); // 设置背景颜色为蓝色
cleardevice();
Sleep(1000);
closegraph();
return 0;
}
小补充:在 setbkcolor(); 的括号内填入十六进制的颜色名称也可以,但要在前面加上 0x,因为这两个符号代表着接下来的内容是以
例:
setbkcolor(0xFFFFFF/*0xffffff也可以*/); // 设置背景颜色为白色
设置线条颜色
当我们在绘制图形时、画线时(以后会提到),图形的边框颜色是肯定要设置的(其实不设置也行,默认白色),那该怎么做呢?先看代码吧!
#include <bits/stdc++.h>
#include <easyx.h>
#include <graphics.h>
using namespace std;
int main()
{
initgraph(640, 480);
setlinecolor(RED);//设置线条颜色为红色
Sleep(1000);
closegraph();
return 0;
}
不难发现,设置线条颜色时要用到 setlinecolor()。而括号内还是一样,填颜色的英文或
设置填充颜色
当我们在绘制图形时,图形内部的颜色是肯定要设置的(其实不设置也行,默认白色),这时候我们的 setfillcolor() 就闪亮登场了。下面是示例代码。
#include <bits/stdc++.h>
#include <easyx.h>
#include <graphics.h>
using namespace std;
int main()
{
initgraph(640, 480);
setfillcolor(RED);//设置填充颜色为红色
Sleep(1000);
closegraph();
return 0;
}
绘制圆形
接下来为大家说一说怎么用 EasyX 绘制一个圆形。其实想要绘制一个圆形并不是一件简单的事情,先看看代码吧!
#include <bits/stdc++.h>
#include <easyx.h>
#include <graphics.h>
using namespace std;
int main()
{
initgraph(640, 480);
setlinecolor(WHITE);
setfillcolor(RED);
cleardevice();
fillcircle(200, 300, 50); // 在x轴为200,y轴为300处做圆心画一个半径为50的圆
Sleep(3000);
closegraph();
return 0;
}
我们可以发现用 fillcircle() 函数就可以画出一个圆了(废话),这个函数的第一个参数为圆心的
绘制长方形
圆形绘制好了,我们来说说怎么绘制长方形。这是我们要用到 fillrectangle() 或 fillroundrect() 了。
用法详解:
首先我们来说说 fillrectangle(),我们先画一个长方形,设它的四条边分别为
那么此时 fillrectangle() 的第一个参数就是
代码演示:
#include <bits/stdc++.h>
#include <easyx.h>
#include <graphics.h>
using namespace std;
int main()
{
initgraph(640, 480);
setlinecolor(WHITE);
setfillcolor(RED);
cleardevice();
fillrectangle(100, 100, 200, 200);// 画一个填充色为红色,线条颜色为白色的长方形
Sleep(3000);
closegraph();
return 0;
}
接下来我们再来说说 fillroundrect(),因为难度较大,先给大家看一看这个函数的原型。
void fillroundrect(int left, int top, int right, int bottom, int ellipseWidth, int ellipseHeight);
{
//函数内容
}
老规矩,我们把这个矩形(不能叫长方形了)的四条边分别称为
那么函数中的
接下来我们看一看如何使用它。
#include <bits/stdc++.h>
#include <easyx.h>
#include <graphics.h>
using namespace std;
int main()
{
initgraph(640, 480);
setlinecolor(WHITE);
setfillcolor(RED);
cleardevice();
fillroundrect(100, 100, 200, 200, 15, 15);// 画一个填充色为红色,线条颜色为白色的圆角弧度为15的矩形
Sleep(3000);
closegraph();
return 0;
}
下期预告
好了,入门篇教程到此结束,下期的基础篇会讲解如何在绘图界面中打印文字、插入图片等。下次再见!
特别声明
fillroundrect() 函数讲解部分的大部分内容来源于网络。