无法使用 JComponent 绘制简单图像

发布于 2024-11-27 06:33:12 字数 738 浏览 2 评论 0原文

我正在使用 netbeans IDE 练习一些 Java 基本代码。但我没有成功绘制PNG图像。下面是我的代码,

package JavaApplication1;

import javax.swing.*;
import java.awt.*;

class MyCanvas extends JComponent
{
    Image img;
    public MyCanvas(){
        img = Toolkit.getDefaultToolkit().createImage("pengbrew.png");
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.drawImage(img, 0, 0, null);
    }
}

public class JavaGame
{
    public static void main(String args[]){
        JFrame window = new JFrame("Image demo");
        window.getContentPane().add( new MyCanvas() );
        window.setSize(400,400);
        window.setVisible(true);
    }
}

我在 netbeans 项目中添加了图像。

请指教。

非常感谢。

I'm using netbeans IDE to practice some Java basic code. But I'm unsuccessful to draw a PNG image. Below is my code,

package JavaApplication1;

import javax.swing.*;
import java.awt.*;

class MyCanvas extends JComponent
{
    Image img;
    public MyCanvas(){
        img = Toolkit.getDefaultToolkit().createImage("pengbrew.png");
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.drawImage(img, 0, 0, null);
    }
}

public class JavaGame
{
    public static void main(String args[]){
        JFrame window = new JFrame("Image demo");
        window.getContentPane().add( new MyCanvas() );
        window.setSize(400,400);
        window.setVisible(true);
    }
}

I added the images in the netbeans project.

Please advise.

Many thanks.

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

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

发布评论

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

评论(2

怂人 2024-12-04 06:33:12

最简单的方法是寻找 JLabel 这是最好的 JComponentsIcon/ImageIcon 示例 此处

easiest ways is look for JLabel that's best of JComponents for Icon/ImageIcon examples here

鹿! 2024-12-04 06:33:12

要加载图像,请使用ImageIO.read(File file)。它是一个更新的 API,更易于使用并且得到更好的支持。有关加载图像的教程位于 http://docs.oracle.com/ javase/tutorial/2d/images/loadimage.html 如果您需要更多指针。

您的代码看起来像这样,

Image img;
public MyCanvas(){
    try {
        img = ImageIO.read(new File("pengbrew.png"));
    } catch (IOException e) {
        // Handle exception if there is one.
    }
}

对于为什么您使用的方法不起作用的长答案可能是因为以下原因...

您的图像可能位于正确的位置,而 Toolkit.createImage() 没有在框架开始绘制之前完成图像加载。 Toolkit.createImage() 在底层图像实际完成加载之前将控制权返回给您的应用程序。您通常可以通过调整应用程序大小以强制其重新绘制来验证这是否是问题。如果尝试调整应用程序大小几秒钟后,图像显示出来,这是由于在第一次绘制调用期间未加载图像。

为了确保在继续之前加载图像,您需要使用 MediaTracker。下面是一些示例代码,用于确保在使用图像之前加载图像已完成。

Component component = new Component() {};
MediaTracker TRACKER = new MediaTracker(component);

...

Image image = Toolkit.getDefaultToolkit().createImage("imageToLoad.png");
synchronized(TRACKER) {
   int id = ++mediaTrackerID;
   TRACKER.addImage(image, id);
   try {
       TRACKER.waitForID(id, 0);
   } catch (InterruptedException ex) {
       image = null;
   }
   TRACKER.removeImage(image, id);
}
// Now you can use your image.

您将在 ImageIcon 类的方法 loadImage(Image image) 中看到类似的代码。

To load an image use ImageIO.read(File file). It's a newer API, easier to use and better supported. A tutorial on loading images is here http://docs.oracle.com/javase/tutorial/2d/images/loadimage.html if you need some more pointers.

Your code would look like this instead,

Image img;
public MyCanvas(){
    try {
        img = ImageIO.read(new File("pengbrew.png"));
    } catch (IOException e) {
        // Handle exception if there is one.
    }
}

For the long answer about why the method you were using wasn't working is probably because of the following...

Your image may be in the proper location and instead Toolkit.createImage() hasn't finished loading the image before the frame comes up to paint. Toolkit.createImage() returns control back to your application before the underlying image has actually finished being loaded. You can usually verify if this is the issue by resizing your application to force it to repaint. If after a few seconds of trying to resize the application the image shows up it was due to the image not being loaded during the first paint calls.

To ensure that the image is loaded before you continue you need to use a MediaTracker. Here is some example code to ensure loading of Image is complete before using it.

Component component = new Component() {};
MediaTracker TRACKER = new MediaTracker(component);

...

Image image = Toolkit.getDefaultToolkit().createImage("imageToLoad.png");
synchronized(TRACKER) {
   int id = ++mediaTrackerID;
   TRACKER.addImage(image, id);
   try {
       TRACKER.waitForID(id, 0);
   } catch (InterruptedException ex) {
       image = null;
   }
   TRACKER.removeImage(image, id);
}
// Now you can use your image.

You'll see code just like this in the ImageIcon class in the method loadImage(Image image).

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