加工中的不同颜色

发布于 2024-07-06 18:59:08 字数 1516 浏览 9 评论 0原文

我一直致力于将一些处理代码移植到 NetBeans 中的常规 Java。 到目前为止,大多数情况都很好,除了当我使用非灰度颜色时。

我有一个绘制螺旋图案的脚本,并且应该根据模量检查改变螺旋中的颜色。 然而,脚本似乎挂起,我无法真正解释原因。

如果有人有一些处理和 Java 的经验,并且你可以告诉我我的错误在哪里,我真的很想知道。

为了进行同行评审,这是我的小程序:

package spirals;
import processing.core.*;

public class Main extends PApplet
{
    float x, y;
    int i = 1, dia = 1;

    float angle = 0.0f, orbit = 0f;
    float speed = 0.05f;

    //color palette
    int gray = 0x0444444;
    int blue = 0x07cb5f7;
    int pink = 0x0f77cb5;
    int green = 0x0b5f77c;

    public Main(){}

    public static void main( String[] args )
    {
        PApplet.main( new String[] { "spirals.Main" } );
    }

    public void setup()
    {
        background( gray );
        size( 400, 400 );
        noStroke();
        smooth();
    }

    public void draw()
    {
        if( i % 11 == 0 )
            fill( green );
        else if( i % 13 == 0 )
            fill( blue );
        else if( i % 17 == 0 )
            fill( pink );
        else
            fill( gray );

        orbit += 0.1f; //ever so slightly increase the orbit
        angle += speed % ( width * height );

        float sinval = sin( angle );
        float cosval = cos( angle );

        //calculate the (x, y) to produce an orbit
        x = ( width / 2 ) + ( cosval * orbit );
        y = ( height / 2 ) + ( sinval * orbit );

        dia %= 11; //keep the diameter within bounds.
        ellipse( x, y, dia, dia );
        dia++;
        i++;
    }
}

I've been working on porting some of my Processing code over to regular Java in NetBeans. So far so well, most everything works great, except for when I go to use non-grayscale colors.

I have a script that draws a spiral pattern, and should vary the colors in the spiral based on a modulus check. The script seems to hang, however, and I can't really explain why.

If anyone has some experience with Processing and Java, and you could tell me where my mistake is, I'd really love to know.

For the sake of peer-review, here's my little program:

package spirals;
import processing.core.*;

public class Main extends PApplet
{
    float x, y;
    int i = 1, dia = 1;

    float angle = 0.0f, orbit = 0f;
    float speed = 0.05f;

    //color palette
    int gray = 0x0444444;
    int blue = 0x07cb5f7;
    int pink = 0x0f77cb5;
    int green = 0x0b5f77c;

    public Main(){}

    public static void main( String[] args )
    {
        PApplet.main( new String[] { "spirals.Main" } );
    }

    public void setup()
    {
        background( gray );
        size( 400, 400 );
        noStroke();
        smooth();
    }

    public void draw()
    {
        if( i % 11 == 0 )
            fill( green );
        else if( i % 13 == 0 )
            fill( blue );
        else if( i % 17 == 0 )
            fill( pink );
        else
            fill( gray );

        orbit += 0.1f; //ever so slightly increase the orbit
        angle += speed % ( width * height );

        float sinval = sin( angle );
        float cosval = cos( angle );

        //calculate the (x, y) to produce an orbit
        x = ( width / 2 ) + ( cosval * orbit );
        y = ( height / 2 ) + ( sinval * orbit );

        dia %= 11; //keep the diameter within bounds.
        ellipse( x, y, dia, dia );
        dia++;
        i++;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

肩上的翅膀 2024-07-13 18:59:08

您是否考虑过添加调试语句 (System.out.println) 并查看 Java 控制台?

可能会有大量的产出和明显的放缓,但你至少可以看到当什么都没有发生时会发生什么。

我认为逻辑错误在于填充 if 语句; 每次迭代,您都决定该迭代的颜色并用该颜色填充。 只有 i == 11、13 或 17 的迭代才会填充颜色。 下一次迭代该颜色将被灰色覆盖。 我认为它会闪烁,可能是看得太快了。

难道你不想要类似的东西吗

public class Main extends PApplet
{
  ...

  int currentColor = gray;

  public Main(){}

  ...

  public void draw()
    {
        if( i % 11 == 0 )
           currentColor = green;
        else if( i % 13 == 0 )
           currentColor = blue;
        else if( i % 17 == 0 )
           currentColor = pink;
        else {
           // Use current color
        } 

        fill(currentColor);

        ...
}

?这样你从灰色开始,变成绿色,蓝色,粉色,绿色,蓝色,粉色等。如果你
还想在某个时候看到灰色,您必须添加类似的内容

  else if ( i % 19 ) {
    currentColor = gray;
  }

希望这会有所帮助。

Have you considered adding debugging statements (System.out.println) and looking at the Java Console?

There may be a massive amount of output and definitive slowdown, but you can at least see what happens when nothing seems to happen.

What I do think is a logic error is the filling if statement; every iteratation you decide the color of that iteration and fill with that color. Only iterations with i == 11, 13, or 17 get filled with a color. And the next iteration that color is overwritten by gray. I would think it tends to flicker, possibly to fast to see.

Didn't you want something like

public class Main extends PApplet
{
  ...

  int currentColor = gray;

  public Main(){}

  ...

  public void draw()
    {
        if( i % 11 == 0 )
           currentColor = green;
        else if( i % 13 == 0 )
           currentColor = blue;
        else if( i % 17 == 0 )
           currentColor = pink;
        else {
           // Use current color
        } 

        fill(currentColor);

        ...
}

In that way you start with gray, go to green, blue, pink, green, blue, pink etc. If you
also want to see gray at some point you'd have to add something like

  else if ( i % 19 ) {
    currentColor = gray;
  }

Hope this helps.

玩套路吗 2024-07-13 18:59:08

要查看此处发生的情况,请

stroke(255);

在抽奖开始时添加。 您将看到所有想要的圆圈都已绘制,但没有颜色。 正如之前的海报提到的:您仅在每 11、13 和 17 次迭代中使用非灰色。

我认为你的颜色值是这里的主要问题。 从参考页来看

,从技术角度来看,颜色是 32 位信息,排序为 AAAAAAAARRRRRRGGGGGGGGGBBBBBBBB,其中 A 包含 alpha 值,R 是红色/色调值,G 是绿色/饱和度,B 是蓝色/亮度。

如果您查看您的值,您会看到非常低的 alpha 值,这可能无法与背景区分开来。

To see whats happening here add

stroke(255);

at the beginning of the draw. You'll see all the wanted circles draw, but without color. As the previous poster mentioned: you're using a non gray color only every 11th, 13th and 17th iteration.

I think that your color values are the main issue here. As from the reference page

From a technical standpoint, colors are 32 bits of information ordered as AAAAAAAARRRRRRRRGGGGGGGGBBBBBBBB where the A's contain the alpha value, the R's are the red/hue value, G's are green/saturation, and B's are blue/brightness.

If you look at your values you'll see a very low alpha value, which is maybe impossible to distinguish from the background.

蓝眸 2024-07-13 18:59:08

不确定您是否还有问题。 你提到了悬挂。 这是一个黑暗中的尝试,但我记得 fry 重复说 size() 调用必须是 setup() 中的第一条指令。 因此,也许向下移动 background() 调用可能会有所帮助。 反正也伤不到。

Not sure if you have still an issue. You mention hanging. It is a shot in the dark, but I remember fry repeating that size() call must be the first instruction in setup(). So perhaps moving down the background() call might help. Couldn't hurt anyway.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文