简体   繁体   English

尝试将图像添加到JFrame

[英]Trying to add Image onto JFrame

Hello I'm trying to add an Image that I have on my desktop to my JFrame that I have created i have imported all the necessary functions and the correct variables the only trouble i have is with Image observer I set my x and y values for my image but it causes an error in my drawImage component and it asks for an Image observer which i don't know what it is and if i auto fill something my Image doesn't appear on my JFrame. 您好,我正在尝试将桌面上的图像添加到我创建的JFrame中,我已经导入了所有必要的函数和正确的变量,唯一的麻烦是使用图像观察器,我为x和y设置了值我的图像,但它在我的drawImage组件中导致错误,它要求提供一个我不知道它是什么的图像观察器,并且如果我自动填充某些图像,我的图像也不会出现在我的JFrame中。 If one of you can look at my code or answer what an Image observer does i would be greatly appreciated 如果你们中的一个可以看我的代码或回答图像观察者的工作,我将不胜感激

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);

}

} }

the error is add argument to match 'drawImage(Image, int, int, ImageObserver)' 错误是添加参数以匹配'drawImage(Image,int,int,ImageObserver)'

ImageObserver is an interface that has methods for handling notification of state of image loading. ImageObserver是一个接口,具有用于处理图像加载状态通知的方法。 It can use this for redisplay as needed. 可以根据需要使用它进行重新显示。 JFrame and Applet both implement ImageObserver interface. JFrameApplet都实现了ImageObserver接口。

To keep users informed regarding the loading of an image 为了告诉用户关于图像的加载

  • ImageObserver interface – Enables the monitoring of the loading process so that users can be informed and the image can be used asap once it is loaded. ImageObserver界面–启用监视加载过程,以便可以通知用户,并且可以在加载图像后尽快使用它。

  • Loading an image asynchronously – how to know when the image is ready. 异步加载图像–如何知道何时准备图像。

    • An image is ready – getImage() method returns, long before anything is known about the image. 图像已准备就绪–很早就知道该图像, getImage()方法返回。

       imageUpdate(Image img, int infoflags, int x, int y, int width, int height) 
  • Note: java.awt.Component implements ImageObserver , all the subclasses do as well! 注意: java.awt.Component实现ImageObserver ,所有子类也都执行!

  • g.drawImage(imge, 0,0, this) -- this refers to the ImageObserver instance. g.drawImage(imge, 0,0, this) -这是指ImageObserver实例。

  • imageUpdate() – Called by the ImageObserver whenever necessary. imageUpdate() –必要时由ImageObserver调用。 You do not call it explicitly! 您没有明确调用它!

    • If the image is complete, returns false . 如果图像完整,则返回false
    • If the image is not complete and needs to be updated, returns true . 如果图像不完整并且需要更新,则返回true
  • ImageObserver.ALLBITS = 32

  • Various constants are combined to form the infoflags argument, which indicates whether all information is available or not. 各种常量组合在一起形成infoflags参数,该参数指示所有信息是否可用。

    信息标志表

您可以通过将ImageObserver设置为null来跳过使用ImageObserver的操作(使用Graphics2D)

g2.drawImage(Image texture, x, y, width, height, null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM