一、 步骤:1.先确定Image的位置,大小(可以不写)
2.设置初始面板,绘制初始的x,y坐标轴
3.画x,y向的刻度线,标刻x,y轴刻度
4.获取数据 (可以不写)
5.将数据标刻在Image上 (可以不写)
二、所使用的函数解析
1. FillRect:填充矩形(FillRect 方法的作用是在画布上填充已绘制的矩形)
2. 使用语法:"FillRect (x,y,width,height);", 其中x、y分别表示矩形左上角的x坐标、y坐标
3. rect:矩形
4. Moveto:移动到某个坐标。 moveto(x,y)
5. Lineto:从当前坐标, 移动的某个坐标连接到当前坐标。 lineto(x,y)
这两个函数加起来就是画一条直线: Moveto(x,y)和Lineto(x,y)
6. TextOut:函数使用当前选定的字体、背景色和文本颜色在指定位置写入一个字符串。
TextOutA(x,y,text);
三、具体操作流程
1.打开C++ Builder 2010软件,File -> New -> VCL Forms Application - C++Builder 。创建界面。
2.修改name中的 Form1 成 fm1 方便后续操作。
3. 添加 Image 组件。在组件栏(Tool Palette)搜 Image 并拖到主界面中。
4. 切换到代码界面。按 F12 或 双击主界面 或 点击主页面下方的 Unit1.cpp(这里我采用双击主界面)
5.编写代码。
(1)确定Image的位置,大小(可以不写)
fm1->Image1 ->Top =30;fm1->Image1 ->Left =30; //Image定位
fm1->Image1 ->Width =1000;fm1->Image1 ->Height =400; //Image大小限定
(2) 设置初始面板,绘制初始的x,y坐标轴
fm1->Image1->Canvas->Pen->Color = clWhite; //Canvas:画布 Pen:刷子 设置颜色
fm1->Image1->Canvas->Brush->Color = clBlack; //Brush:刷子 设置颜色
fm1->Image1->Canvas->FillRect(Rect(0,0, fm1->Image1->Width,fm1->Image1->Height)); //初始面板
fm1->Image1->Canvas->MoveTo(50,350); fm1->Image1->Canvas->LineTo(fm1->Image1->Width,350); //初始x轴绘制
fm1->Image1->Canvas->MoveTo(80,380); fm1->Image1->Canvas->LineTo(80,20); //初始y轴绘制
fm1->Image1->Canvas->MoveTo(fm1->Image1->Width*0.98,345); fm1->Image1->Canvas->LineTo(fm1->Image1->Width,350); //x轴箭头绘制
fm1->Image1->Canvas->LineTo(fm1->Image1->Width,350); fm1->Image1->Canvas->LineTo(fm1->Image1->Width*0.98,355);
fm1->Image1->Canvas->MoveTo(75,40); fm1->Image1->Canvas->LineTo(80,20); //y轴箭头绘制
fm1->Image1->Canvas->MoveTo(80,20); fm1->Image1->Canvas->LineTo(85,40);
(3)画x,y向的刻度线,标刻x,y轴刻度
// 画刻度线与刻度
// 纵向线:
fm1->Image1->Canvas->Pen->Color = clTeal;
pos=920;
for(i=0;i<20;i++)
{
pos -= 40;
fm1->Image1->Canvas->MoveTo(pos,350); fm1->Image1->Canvas->LineTo(pos,50);
}
// 横向线:
fm1->Image1->Canvas->Pen->Color = clTeal;
pos = 35;
for(i=0;i<20;i++)
{
pos += 15;
fm1->Image1->Canvas->MoveTo(80,pos); fm1->Image1->Canvas->LineTo(fm1->Image1->Width*0.98,pos);
}
// x轴刻度
fm1->Image1->Canvas->Font->Color = clWhite;
fm1->Image1->Canvas->Font->Name = "Arial";
fm1->Image1->Canvas->Font->Size = 9;
fm1->Image1->Canvas->Pen->Color = clWhite;
pos = 80;
for(i=0;i<20;i++)
{
AnsiString number_x;
number_x=String((i+1)*10.0)+".0";
pos += 40;
fm1->Image1->Canvas->MoveTo(pos,350); fm1->Image1->Canvas->LineTo(pos,355);
fm1->Image1->Canvas->TextOutA(pos-14,360,number_x);
}
// y轴刻度
fm1->Image1->Canvas->Font->Color = clWhite;
fm1->Image1->Canvas->Font->Name = "Arial";
fm1->Image1->Canvas->Font->Size = 9;
fm1->Image1->Canvas->Pen->Color = clWhite;
pos = 35;
for(i=0;i<20;i++)
{
AnsiString number_x,number_y;
number_x=String((i+1)*10.0)+".0";
number_y=String((i+1)*10.0)+".0";
pos += 15;
fm1->Image1->Canvas->MoveTo(75,pos); fm1->Image1->Canvas->LineTo(80,pos);
fm1->Image1->Canvas->TextOutA(40,343-(i+1)*15,number_y);
}
fm1->Image1->Canvas->TextOutA(20,343-0*15,"0.0 ");
6.完整代码。上述第5点是代码解析,复制第6点可以直接生成代码。
int pos,i;
void __fastcall Tfm1::FormCreate(TObject *Sender)
{
/*
FillRect:填充矩形
fillRect 方法的作用是在画布上绘制已填充的矩形
使用语法:"fillRect (x,y,width,height):", 其中x、y分别表示矩形左上角的x坐标、y坐标
rect:矩形
moveto:移动到某个坐标 moveto(x,y)
lineto:从当前坐标, 移动的某个坐标连接到当前坐标。 lineto(x,y)
这两个函数加起来就是画一条直线:moveto(x,y)和lineto(x,y)
TextOut:函数使用当前选定的字体、背景色和文本颜色在指定位置写入一个字符串。
TextOutA(x,y,text);
*/
/*
步骤:1.先确定Image的位置,大小(可以不写)
2.设置初始面板,绘制初始的x,y坐标轴
3.画x,y向的刻度线,标刻x,y轴刻度
4.获取数据 (可以不写)
5.将数据标刻在Image上 (可以不写)
*/
// 定位、大小(可以不写)
fm1->Image1 ->Top =30;fm1->Image1 ->Left =30;
fm1->Image1 ->Width =1000;fm1->Image1 ->Height =400;
// 设置
fm1->Image1->Canvas->Pen->Color = clWhite; //Canvas:画布 brush:刷子
fm1->Image1->Canvas->Brush->Color = clBlack;
fm1->Image1->Canvas->FillRect(Rect(0,0, fm1->Image1->Width,fm1->Image1->Height)); //初始面板
fm1->Image1->Canvas->MoveTo(50,350); fm1->Image1->Canvas->LineTo(fm1->Image1->Width,350); //初始x轴绘制
fm1->Image1->Canvas->MoveTo(80,380); fm1->Image1->Canvas->LineTo(80,20); //初始y轴绘制
fm1->Image1->Canvas->MoveTo(fm1->Image1->Width*0.98,345); fm1->Image1->Canvas->LineTo(fm1->Image1->Width,350); //x轴箭头绘制
fm1->Image1->Canvas->LineTo(fm1->Image1->Width,350); fm1->Image1->Canvas->LineTo(fm1->Image1->Width*0.98,355);
fm1->Image1->Canvas->MoveTo(75,40); fm1->Image1->Canvas->LineTo(80,20); //y轴箭头绘制
fm1->Image1->Canvas->MoveTo(80,20); fm1->Image1->Canvas->LineTo(85,40);
// 画刻度线与刻度
// 纵向线:
fm1->Image1->Canvas->Pen->Color = clTeal;
pos=920;
for(i=0;i<20;i++)
{
pos -= 40;
fm1->Image1->Canvas->MoveTo(pos,350); fm1->Image1->Canvas->LineTo(pos,50);
}
// 横向线:
fm1->Image1->Canvas->Pen->Color = clTeal;
pos = 35;
for(i=0;i<20;i++)
{
pos += 15;
fm1->Image1->Canvas->MoveTo(80,pos); fm1->Image1->Canvas->LineTo(fm1->Image1->Width*0.98,pos);
}
// x轴刻度
fm1->Image1->Canvas->Font->Color = clWhite;
fm1->Image1->Canvas->Font->Name = "Arial";
fm1->Image1->Canvas->Font->Size = 9;
fm1->Image1->Canvas->Pen->Color = clWhite;
pos = 80;
for(i=0;i<20;i++)
{
AnsiString number_x;
number_x=String((i+1)*10.0)+".0";
pos += 40;
fm1->Image1->Canvas->MoveTo(pos,350); fm1->Image1->Canvas->LineTo(pos,355);
fm1->Image1->Canvas->TextOutA(pos-14,360,number_x);
}
// y轴刻度
fm1->Image1->Canvas->Font->Color = clWhite;
fm1->Image1->Canvas->Font->Name = "Arial";
fm1->Image1->Canvas->Font->Size = 9;
fm1->Image1->Canvas->Pen->Color = clWhite;
pos = 35;
for(i=0;i<20;i++)
{
AnsiString number_x,number_y;
number_x=String((i+1)*10.0)+".0";
number_y=String((i+1)*10.0)+".0";
pos += 15;
fm1->Image1->Canvas->MoveTo(75,pos); fm1->Image1->Canvas->LineTo(80,pos);
fm1->Image1->Canvas->TextOutA(40,343-(i+1)*15,number_y);
}
fm1->Image1->Canvas->TextOutA(20,343-0*15,"0.0 ");
}
7.编译代码并运行。按 F9键 或 Run
8.得到结果。
9.源码链接。可直接在桌面运行
C++Builder2010绘制坐标资源-CSDN文库https://download.csdn.net/download/God_me_1/89397348
标签:Canvas,LineTo,Builder,pos,C++,fm1,MoveTo,Image1,2010 From: https://blog.csdn.net/God_me_1/article/details/139437220