0

您好,我正在尝试将桌面上的图像添加到我创建的 JFrame 中我的图像,但它在我的 drawImage 组件中导致错误,它要求一个图像观察者,我不知道它是什么,如果我自动填充一些我的图像没有出现在我的 JFrame 上。如果你们中的一个人可以查看我的代码或回答图像观察者的工作,我将不胜感激

public class Window2 extends JPanel {

    // Image Import 
    ImageIcon i = new ImageIcon("C: / Class Pokemon Game/ src / GameTitle (1).psd"); 
    Image title = i.getImage();


public void paintComponent(Graphics g)
{
    super.paintComponent(g);

    this.setBackground(Color.BLACK);

    g.setColor(Color.RED);
    g.fillRect(0, 40, 5000, 20);

    g.**drawImage**(title, 500, 500);

}

}

错误是添加参数以匹配'drawImage(Image,int,int,ImageObserver)'

4

2 回答 2

0

You can skip using an ImageObserver by putting it as null for example (Using Graphics2D)

g2.drawImage(Image texture, x, y, width, height, null);
于 2018-12-18T20:32:25.183 回答
0

ImageObserver是一个接口,它具有处理图像加载状态通知的方法。它可以根据需要使用它来重新显示。 JFrame并且Applet都实现了ImageObserver接口。

让用户了解图像的加载

  • ImageObserverinterface – 启用加载过程的监控,以便通知用户并在加载后尽快使用图像。

  • 异步加载图像——如何知道图像何时准备就绪。

    • 图像已准备就绪 -getImage()方法返回,早在知道图像的任何内容之前。

      imageUpdate(Image img, int infoflags, int x, int y, int width, int height)
      
  • 注意:java.awt.Componentimplements ImageObserver,所有子类也一样!

  • g.drawImage(imge, 0,0, this)-- this 指的是ImageObserver实例。

  • imageUpdate()ImageObserver–在必要时由 the 调用。您没有明确调用它!

    • 如果图像完整,则返回false
    • 如果图像不完整且需要更新,则返回true.
  • ImageObserver.ALLBITS = 32

  • 各种常量组合起来形成infoflags参数,它指示所有信息是否可用。

    信息标志表

于 2018-12-12T17:58:00.890 回答