Applet类,getImage()和drawImage()不显示图像

时间:2015-06-23 17:02:42

标签: java intellij-idea

我直接从我正在阅读的书中介绍了一些演示代码(Java编程简介,问题解决方法,2007年第1版,Ray Dean和John Dean,第187页)。在编写演示代码之后,图像应该显示出来,但它并没有显示出来。任何人都可以帮我找出原因吗?

守则:

import java.awt.*;  // for Graphics, Image, and Color classes
import java.applet.Applet;

public class GraphicsDemo extends Applet
{
    public URL base;

    public void paint (Graphics g)
    {
        try {
            base = getDocumentBase();
        } catch (Exception e) {
            e.printStackTrace();
        }

        Image image =
                this.getImage(base, "images/hurricanes.jpg");

        g.drawImage(image, 0, 0, 427, 284,      // destination topL, botR
                0, 0, 640, 427, this);          // source topL, botR

        // establish color of all lines to be drawn
        g.setColor(Color.BLUE);

        // draw rectangle around region to be expanded
        g.drawRect(200, 60, 120, 120);          // topL, width & height

        // draw lines between corners of rectangles
        g.drawLine(200, 60, 240, 240);          // upper left
        g.drawLine(320, 60, 600, 240);          // upper right
        g.drawLine(200, 180, 240, 600);         // lower left
        g.drawLine(320, 180, 600, 600);         // lower right

        // display expanded part of original image
        g.drawImage(image, 240, 240, 600, 600,  // destination topL, botR
                300, 90, 480, 270, this);       // source topL, botR

        // draw rectangle around expanded part of image
        g.drawRect(240, 240, 360, 360);         // topL, width & height

        // create BLUE colored oval and write name on it
        g.fillOval(520, 380, 45, 30);           // topL, width & height
        g.setColor(Color.WHITE);                // change color for text
        g.drawString("MAX", 530, 400);          // string & start position
    } // end paint
} // end GraphicsDemo class

我知道它看到了图像,因为我的IDE(Intellij)超链接代码中的图像。

以下是文件夹细分:

enter image description here

以下是应该显示的图片:

enter image description here

以下是我运行Applet时实际显示的内容(请注意无图像):

enter image description here

这是应该出现的图片(来自书中):

enter image description here

2 个答案:

答案 0 :(得分:0)

您需要附加一个ImageObserver,它将重新绘制()组件作为更多的图像加载。图像可以异步加载,因此您需要在加载时更新显示。

答案 1 :(得分:0)

我希望问题是该文件不是您的程序所期望的。

如果我将图片文件放在错误的位置,我可以复制您的结果,如果我正确设置图像的路径,我会得到图书的结果。

您会注意到它所说的getImage()电话的文档(强调我的):

  

返回一个Image对象,然后可以在屏幕上绘制。 url参数必须指定绝对URL。 name参数是一个相对于url参数的的说明符。

     

此方法始终立即返回,图像是否存在。当此applet尝试在屏幕上绘制图像时,将加载数据。绘制图像的图形基元将在屏幕上逐渐绘制。

当我将代码放在临时项目中并打印出来自base的网址时,我得到file:/C:/projects/scratch/scratch/target/classes/GraphicsDemo1435079664101.html

当我按原样运行您的代码时,使用src/images/hurricanes.jpg中的图片文件,我会获得相同的空白小程序,但如果我将getImage()调用更改为

    Image image = this.getImage(base, "../../src/images/hurricanes.jpg");

...然后图片加载就好了。

如果您在运行程序时检查base的网址,那么根据您的项目设置方式找出正确的相对路径,就不应该花费太多费力。

修改

由于您的小程序未在保存图像的同一位置运行,因此您可能需要使用另一个getImage()调用,该调用会获取文件的绝对URL。 / p>

尝试类似

的内容
    Image image = null;
    try {
        image = this.getImage(new URL("file:/D:/Java_Projects/John_Dean_Ray_Dean_Book/myJavaPgms/Chapter5/5_17_GraphicsDemo_‌​Fixed/src/images/hurricanes.jpg"));
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

这不是我建议在实际代码中做的事情,因为通常你不知道在运行你的applet的机器上文件系统是什么样的,但它应该得到你的示例程序工作

在实际应用程序中,图像文件将作为包含applet的JAR文件的一部分进行部署,因此您希望getImage(URL, String)使用基于您放置图像的位置的相对路径你的JAR文件。