【问题标题】:Drawing Images twitching in processing绘图图像在处理中抽搐
【发布时间】:2017-07-08 07:39:56
【问题描述】:

在这个草图中,它水平绘制了一个对象列表。每个对象的 x 位置取决于其左侧的对象。此外,当对象不在屏幕上时,它也不会绘制。但是,当我移动时,物体会抽搐。我真的不知道为什么,可能是处理的加载图像需要一些时间,所以会有一点延迟?我只用绘制矩形(rect())对此进行了测试,但问题没有发生。

以下是使用相同算法绘制从互联网加载的图像的代码:(加载草图可能需要一段时间,因为它正在从不同的网站检索图像)运行草图后,使用左侧和右箭头键左右移动。使用向上和向下箭头更改移动速度。

PImage backgroundImage;
PFont font;
int x = 0;
int xoffset = 0;
int deltax = 5;
float scale = 512/30;
float deltascale = 0.02;
ArrayList<Object> ObjectList;

class Object {
  String name;
  float diameter;
  String unit;
  String sprite;
  PImage img;
  int wid;
  int hei;
  int xpos;

  Object(String Oname, float Odiameter, String Ounit, String Oimg) {
    name = Oname;
    diameter = Odiameter;
    unit = Ounit;
    img = loadImage(Oimg);
    sprite = Oimg;
    //img = loadImage(Oimg);
    wid = img.width;
    hei = img.height;
  }
}

void setup() {
  size(1500, 800);
  frameRate(200);
  fill(255,255,255);
  textSize(32);
  textAlign(CENTER, CENTER);
  text("Images are loading (this may take a while)",700,400);
  ObjectList = new ArrayList();
  readObjects();
}

void addObject(String Aname, float Adiameter, String Aunit, String Aimg) {
  ObjectList.add(new Object(Aname, Adiameter, Aunit, Aimg));
}

void readObjects() {
  addObject("RED", 30, "m", "https://s-media-cache-ak0.pinimg.com/736x/8f/1b/cc/8f1bcc72c81d1e9370597f7239ee476a.jpg");
  //addObject("ORANGE", 60, "m", "http://6iee.com/data/uploads/36/489075.jpg");
  addObject("YELLOW", 70, "m", "https://i.stack.imgur.com/aez1V.jpg");
  addObject("GREEN", 100, "m", "http://space-facts.com/wp-content/uploads/mars.jpg");
  addObject("PURPLE", 105, "m", "http://space-facts.com/wp-content/uploads/jupiter.png");
  addObject("PINK", 110, "m", "https://vignette1.wikia.nocookie.net/starwars/images/4/4a/Alderaan.jpg");
  addObject("BROWN", 150, "m", "https://cdn.pixabay.com/photo/2013/07/12/16/33/venus-151142_960_720.png");
  addObject("GRAY", 180, "m", "http://www.lpi.usra.edu/lpi_40th/images/1989/neptune.jpg");
  //addObject("WHITE", 200, "m", "http://www.clipartkid.com/images/19/log-in-sign-up-upload-clipart-Dpr48a-clipart.png");
  addObject("INDIGO", 300, "m", "https://upload.wikimedia.org/wikipedia/commons/thumb/a/a0/Sun_in_February.jpg/155px-Sun_in_February.jpg");
  addObject("SKYBLUE", 301, "m", "https://upload.wikimedia.org/wikipedia/commons/e/ea/Uranus_(Edited).jpg");
  addObject("BLUE", 1000, "m", "https://vignette2.wikia.nocookie.net/heman/images/3/36/Earth.jpg");
}




void draw() {

  fill(0,0,0);
  rect(0, 0, 1500, 800);

  int x = 0 + xoffset;
  for ( Object drawObject : ObjectList ) {   
    float dia = drawObject.diameter;

    if (x+drawObject.img.width >= 0 && x <= 1500) {


      //checks if image is too small to be drawn 
      int drawWidth = int(scale * dia);
      int drawHeight = int(drawWidth * drawObject.hei / drawObject.wid);
      if (drawWidth > 0 && drawHeight > 0) {
        drawObject.img.resize(drawWidth, drawHeight);
        image(drawObject.img, x, 400-(drawObject.img.height/2));
        //calculates font size relative to object's current size
        int textSize;
        //checks if the name or diameter is longer, then uses the longer one for calculation
        fill(255,255,255);
        if (drawObject.name.length() < str(drawObject.diameter).length()) {
          textSize = int(drawObject.img.width/str(drawObject.diameter).length());
        } else {
          textSize = int(drawObject.img.width/drawObject.name.length());
        }
        if (textSize > 0) {
          textSize(textSize);
          float texty = 418+(drawObject.img.height/2);
          if (texty < (418+(drawObject.img.height/2))) {
            texty = 418;
          }
          text(drawObject.name, x+(drawObject.img.width/2), texty);
          text(drawObject.diameter+drawObject.unit, x+(drawObject.img.width/2), texty+textSize);
        }
      }
      drawObject.xpos = x;
      x += (drawObject.img.width*0.08 + drawObject.img.width);
    }
    //display deltax
    textSize(32);
    text(str(deltax), 1450, 10);
    text(str(deltascale), 1450, 50);

    println(str(scale));
  }
}

void mouseWheel(MouseEvent event) {
  float e = event.getCount();
  if (e < 0) {
    scale *= (1 + deltascale);
  } else if (e > 0) {
    scale *= (1 - deltascale) ;
  }
}

void keyPressed() {
  if (key == CODED) {
    if (keyCode == LEFT) {
      xoffset += deltax;
    } else if (keyCode == RIGHT) {
      xoffset -= deltax;
    } else if (keyCode == UP) {
      deltax += 5;
    } else if (keyCode == DOWN) {
      deltax -= 5;
    }
  }
  if (key == 'r') {
    //reloadObjects();
  } else if (key == 'q') {
    deltascale += 0.02;
  } else if (key == 'a') {
    deltascale -= 0.02;
  }
  if (deltax < 0) {
    deltax = 0;
  } else if (deltascale < 0) {
    deltascale = 0;
  }
}

以下原始代码将不起作用,因为它使用了只有我拥有的图像。

The original code

我希望有人可以帮助我解决这个错误,或者帮助我改进我的算法。 编辑:代码现在是可验证的,并提出了我遇到的问题

【问题讨论】:

  • 请张贴minimal reproducible example 说明问题。就像你说的,你发布的代码没有出现问题,所以帮助调试会有点困难。此外,您真的应该尝试格式化您的代码 - 处理编辑器甚至会为您执行此操作。还有一件事:你真的不应该使用名为Object 的类,因为已经有一个名为Object 的Java 类令人困惑。
  • @KevinWorkman 我已经更新了帖子,现在包含一个可验证的示例而不是显示问题。

标签: processing


【解决方案1】:

嗯,我注意到每当您的一张图片即将离开屏幕左边缘时,图片就会跳动。当您的每张图像都离开屏幕时,看起来您可能会以不同的方式计算 x 位置。尝试摆脱您的 if (x+drawObject.img.width &gt;= 0 &amp;&amp; x &lt;= 1500) 测试(暂时将其注释掉),但将代码保留在 if 中。

几点:

  • 绝对不要创建自己的 Object 类(正如 Kevin Workman 所说)
  • 当您调用img.resize() 时,您正在重新采样图像数据,随着时间的推移,这将导致质量下降。最好只使用 width, height 参数到 image() 函数(或使用 scale() 转换函数 - 但这可能更令人困惑)。然后在其余的绘图代码中使用drawWidth/Height 而不是img.width/height
  • 如果您想将图像居中,只需使用imageMode(CENTER) 而不是自己进行数学运算 - 特别是因为看起来您使用的是“幻数”400
  • 尽量不要为您的屏幕尺寸使用原始数字 - 而是使用 widthheight 全局变量 - 当您更改屏幕尺寸时,它会让您的生活更轻松。

【讨论】:

  • 有趣地注释掉 if 语句有效!我之前有这个条件,因为我不想处理绘制所有对象,而只绘制屏幕上的对象。将来如果我有很多对象,我应该保留还是删除 if 语句?我一定会牢记这些要点:)
  • if 语句中重要的部分(在保持绘图稳定方面)是改变您的 x 值的部分:x += (drawObject.img.width*0.08 + drawObject.img.width); 如果您将那一行移动到在您的if 之外,您将避免抽搐,并保持优化。通常,您希望在绘制函数内的循环中尽可能少地执行操作,以保持帧速率较高。
  • 添加 if 语句同时在其外部添加 x 语句会导致相同的抽搐,因此我尝试使用 image(img,a,b,c,d) 并且它运行顺利!谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-20
  • 2016-06-21
  • 2020-04-25
  • 2020-02-24
  • 1970-01-01
  • 2012-04-15
相关资源
最近更新 更多