【问题标题】:How to get screen coordinates in Processing如何在处理中获取屏幕坐标
【发布时间】:2011-04-24 21:01:57
【问题描述】:

我正在使用 Processing 来做一个项目。
我使用draw 函数在草图板上绘制了一个草图(实际上是文本)。
我想获取文本每个单词的屏幕坐标来做一些其他有趣的事情。

不知道用什么函数可以检索,取回屏幕坐标。

任何人都可以帮助解决这个问题。非常感谢您的帮助。

谢谢

【问题讨论】:

  • 请提供更多细节,例如您如何在草图上绘制文本、使用text 方法、使用类等。

标签: text coordinates positioning processing


【解决方案1】:

希望我正确地解决了您的问题,下面的草图说明了您如何获得解决方案。它的前进速度非常快,基本上依赖于手动单词定位。首先,我将整个文本分成单个单词;存储在myWords 数组中。
draw 循环中,我使用两个变量——xPosyPos——来表示一个在屏幕上移动的假想光标。 if 子句检查当前单词是否会跳出草图区域(包括填充):

        float xPosEnd = xPos + textWidth (myWords[i]);

如果是这样,光标将跳转到下一行的开头。

        if (xPosEnd > width - PADDING_X) {
            ...

现在间距靠鼠标,行高是固定的;但也很容易是动态的。您可以使用xPosyPos 变量来调整单词的位置。此外,xPosEnd 是否指示单词的结束位置。正如我所说,这种方法非常快,也可以应用于角色级别。

手动文本定位脚本

public static final int FONT_SIZE = 20;
public static final float LINE_HEIGHT = FONT_SIZE * 1.3f;
public static final float PADDING_X = 25;
public static final float PADDING_Y = 15;

PFont font;
String myText;
String[] myWords;

float spacing = 5;

void setup () {
    size (480, 320);
    smooth ();

    font = createFont ("Arial", FONT_SIZE);
    textFont (font);

    myText = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, ";
    myText += "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ";
    myText += "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris ";
    myText += "nisi ut aliquip ex ea commodo consequat.";

    myWords = myText.split (" ");
}

void draw () {

    background (0);

    float xPos = PADDING_X;
    float yPos = PADDING_Y + FONT_SIZE;

    // For every word in the text
    for (int i=0; i < myWords.length; i++) {

        // Calculate the expected end position of 
        // the current word in this line
        float xPosEnd = xPos + textWidth (myWords[i]);

        // Check if word is not to long 
        // for current screen bounds. If...
        if (xPosEnd > width - PADDING_X) {
            // Set the cursor to the beginning 
            // of the next line
            xPos = PADDING_X;
            yPos += LINE_HEIGHT;
        }

        // Display word at xPos-yPos
        text (myWords[i], xPos, yPos);

        // Move the cursor to the right for the 
        // next word in list
        xPos += textWidth (myWords[i]) + spacing;
    }
}

void mouseMoved () {
    spacing = map (mouseX, 0, width, 0, 40);
}

【讨论】:

    【解决方案2】:

    我可以使用什么函数来检索, 取回屏幕坐标

    您可能正在寻找screen 系统变量

    说明 系统变量 存储计算机的尺寸 屏幕。例如,如果当前 屏幕分辨率为 1024x768, screen.width 是 1024 和 screen.height 是 768。这些尺寸很有用 导出全屏时 应用程序。


    示例程序

    println ("Width:" + screen.width);
    println ("Height:" + screen.height);
    

    如需更多参考,请始终参考Language API


    编辑

    但我正在寻找方法 任何物体的坐标 画在素描板上。比如说如果 我有一些文字,我想知道 每个单词的开始和结束坐标 在文中

    你用什么方法来绘制文字?您将使用什么方法为文本设置动画?这取决于您用于查找坐标的方法。

    如果您使用了text 方法,那么您将坐标作为参数提供
    例如:text(data, x, y)

    如果您将文本设置为向右移动 5 个坐标,那么如果初始 x 坐标为 10,那么现在 x 坐标将为 15。

    【讨论】:

    • 感谢您的回复。但我正在寻找方法来查找在草图板上绘制的任何对象的坐标。比如说如果我有一些文本,我想知道文本中每个单词的开始和结束坐标,但我做不到。你能帮忙吗?..谢谢!
    • 你是如何在草图上绘制文字的?使用text 函数?
    猜你喜欢
    • 1970-01-01
    • 2021-10-22
    • 2015-08-01
    • 1970-01-01
    • 2019-08-22
    • 2016-02-29
    • 1970-01-01
    • 1970-01-01
    • 2012-02-02
    相关资源
    最近更新 更多