码迷,mamicode.com
首页 > 其他好文 > 详细

[Processing]在画布上写文本

时间:2018-07-05 23:40:57      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:tar   ott   这一   写文本   10个   code   font   tran   fill   

  • 准备工作
    • 这一步只是我强迫症犯了哈,这个随意,画几根线而已。每一小格10个像素,中格50,大格100像素
    •  1 void setup()
       2 {
       3   size(1280,720);
       4 }
       5 
       6 void draw()
       7 {
       8   background(0,0,0);
       9   //translate(width/2,height/2);
      10   Aix(1,50,width,height,40);
      11   Aix(2,100,width,height,60);
      12   Aix(1,10,5,5,100);
      13   Aix(2,50,7,7,100);
      14   stroke(150);
      15   strokeWeight(3);
      16   line(-width,0, width,0);
      17   line(0,-height, 0,height);
      18   
      19   DrawText();
      20 }
      21 
      22 float weight = 3;
      23 float del = 100;
      24 float lonW = 10;
      25 float lonH = 10;
      26 void Aix(float w,float d,float lw,float lh,float st)
      27 {
      28   weight = w;
      29   del = d;
      30   lonW = lw;
      31   lonH = lh;
      32   stroke(st);
      33   strokeWeight(weight);
      34   for(int i = 0;i <= width;i+=del)
      35   {
      36     line(i,-lonH, i,lonH);
      37   }
      38   for(int i = 0;i >= -width;i-=del)
      39   {
      40     line(i,-lonH, i,lonH);
      41   }
      42   for(int i = 0;i <= height;i+=del)
      43   {
      44     line(-lonW,i, lonW,i);
      45   }
      46   for(int i = 0;i >= -height;i-=del)
      47   {
      48     line(-lonW,i, lonW,i);
      49   }
      50 }
      51 
      52 void DrawText()
      53 {
      54   
      55 }

      技术分享图片

  • 打印文字
    • 1 text(String str,float x,float y[,float z]);//在某位置显示文本,默认为白色的文本,可用 fill() 方法填充颜色
      2 text(char[] chars,int start,int end,float x,float y[,float z]);
      3 text(String str,float x1,float y1,float x2,float y2);//在两个点决定的矩形内显示字符串
      4 text(float num,float x,float y[,float z]);
      1 void DrawText()
      2 {
      3   String str = "Hello World";
      4   text(str,50,50);
      5 }

      技术分享图片

    • 默认文字是白色的,所以如果一开始背景是浅色的话回看不清楚,这一点要注意。可用用 fill(); 方法改变颜色
  • 字体大小
  • 1 void DrawText()
    2 {
    3   String str = "Hello World";
    4   
    5   textSize(50);//修改字体大小
    6   text(str,50,50);
    7 }

    技术分享图片

  • 对其方式
    • textAlign(h[,o]); 其中,h表示水平对齐方式,o表示垂直对齐方式,可填入宏
      • h
        • RIGHT 右对齐
        • CENTER
        • LEFT
      • o
        • TOP
        • CENTER
        • BOTTOM
        • BASELINE 基线对齐
    •  1 void DrawText()
       2 {
       3   String str = "Hello World";
       4   
       5   noFill();
       6   stroke(#E0A000);
       7   rect(0,0, 500,300);//画矩形
       8   
       9   textSize(50);
      10   textAlign(RIGHT,BOTTOM);//右下对齐
      11   text(str,0,0, 500,300);//在一个矩形内显示
      12 }

      技术分享图片

       

  • 设置行高
    •  1 void DrawText()
       2 {
       3   String str = "Hello World\nWorld Hello";
       4   
       5   noFill();
       6   stroke(#E0A000);
       7   rect(0,0, 500,300);
       8   
       9   textSize(20);
      10   textLeading(20);
      11   text(str,0,0, 500,300);
      12   textLeading(40);
      13   text(str,150,0, 500,300);
      14   textLeading(80);
      15   text(str,300,0, 500,300);
      16 }

      技术分享图片

  • 文本宽度

    •  1 void DrawText()
       2 {
       3   String str = "Hello World\nWorld Hello";
       4   
       5   noFill();
       6   stroke(#E0A000);
       7   rect(0,0, 500,300);
       8   
       9   textSize(20);
      10   textLeading(20);
      11   text(str,0,0, 500,300);
      12   
      13    //画一个框把一行文字框起来
      14   stroke(#FFFFCC);
      15   line(0,0, textWidth(str),0);//用到了获取字符串宽度的方法 textWidth()
      16   line(0,20, textWidth(str),20);
      17   
      18   stroke(#EECCEE);
      19   line(0,0, 0,20);
      20   line(textWidth(str),0, textWidth(str),20);
      21 }

      技术分享图片

[Processing]在画布上写文本

标签:tar   ott   这一   写文本   10个   code   font   tran   fill   

原文地址:https://www.cnblogs.com/Yukisora/p/9270326.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!