【问题标题】:Loading an image line by line Java逐行加载图像 Java
【发布时间】:2016-10-06 12:59:34
【问题描述】:

所以这听起来很荒谬,但我正在做一个项目,我故意想减慢图像的加载速度,以便逐行加载。无论如何我可以做到这一点吗?我目前在 ImagePane 中有图像,它只是 JPanel 的扩展:

public ImagePane() {
        initComponents();
        image = null;
        //this.setAutoscrolls(true);
    }
    public void setImage(String path) throws IOException {
        image = ImageIO.read(getClass().getResource(path));
    }

    @Override
    public void paintComponent(Graphics g)
    {
        //Graphics2D g2 = (Grahpics2D)g;
        g.drawImage(image, 0,0, this);
    }

在我试图将其显示为的窗口中:

ImagePane image = new ImagePane();
try {
    image.setImage("netscapelogo2.png");
}
catch (IOException e) {
    System.out.print("Failed to Set");
    e.printStackTrace();
}
//jScrollPane1.add(image);
jScrollPane1.setViewportView(image);

我想我需要有人更改我的 paintComponent 方法才能做到这一点,但我不确定具体该怎么做。

【问题讨论】:

  • 如果你想要 90 年代的缓慢加载图像体验,你必须获取原始像素数据并逐行绘制。请参阅 Image/BufferedImagegetRGB()/getRaster() 等)的 javadocs。
  • 我会通过逐渐揭开来模拟图像正在逐行加载。

标签: java arrays image loading line-by-line


【解决方案1】:

此解决方案使用前提I would simulate that the image is loading line by line by uncovering it gradually. – rodrigoap,因此图像会立即加载并且仅显示,因为它将逐行读取!

一个解决方案是创建一个线程并让线程工作......

Runnable r = new Runnable(){

    @Override
    run(){
        for(int i = 0; i < image.getHeight(); i++){
            // wait 100ms to 'slow down'
            Thread.sleep(100)// surround with try/catch, it may throw an exception
            line = line + 1; //increase amount of visible lines
            repaint(); //update the panel
        }
    }
}

//i don't know when you want to start the animation
new Thead(r).start(); //so trigger at free will

当您绘制图像时,您只是绘制线条的数量,而不是整个图像......

@Override
public void paintComponent(Graphics g)
{
    super(g);
    int w = image.getWidth();
    int h = image.getHeight();
    g.drawImage(image, 0,0, w, line, 0,0,w,h,this);
}

drawImage 方法有点奇怪,请参阅docu 以获得更多帮助

当然你需要在某个地方定义private int line = 0;

【讨论】:

  • hmm - 我想你也可以使用摇摆计时器或其他东西......我忘了,但这个解决方案解释了如何解决问题,并且可以很容易地调整到其他时间处理程序跨度>
  • 计时器将是一个非常更好的解决方案 - 不要永远那样模拟你自己的时间 - 它不精确、容易出错并且只是一个初学者的错误。使用实时计时器ExecutorServicesLambda 以提高可读性。另外:使用static final AtomicInteger 而不是int,否则您遇到赛车状况,甚至可能陷入僵局
  • @specializt 我知道我知道 - 请修改答案,我在那个话题上不是很聪明!我知道它们存在并且是更好的解决方案,但从未使用过!真丢脸!
  • 谢谢,帮了大忙
猜你喜欢
  • 2011-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-18
  • 2017-06-11
  • 2012-05-22
  • 1970-01-01
相关资源
最近更新 更多