简体   繁体   中英

Applet Class, getImage() and drawImage() not showing image

I'm trying out some demo code straight out of a book I'm going through (Introduction to Programming with Java A Problem Solving Approach, 1st Edition 2007 by Ray Dean and John Dean, page 187). After writing the demo code, the image is supposed to show up, but it doesn't. Can anyone help me figure out why?

The Code:

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

I know it see's the image because my IDE (Intellij) hyperlinks the image in the code.

Here is the folder breakdown:

在此处输入图片说明

Here is the image that is supposed to show:

在此处输入图片说明

Here is what actually shows when I run the Applet (notice no image):

在此处输入图片说明

Here is the image that is supposed to show up (from the book):

在此处输入图片说明

You will need to attach an ImageObserver that will repaint() the component as more of the image loads. Images can load in asynchronously, so you'll need to update the display as it loads.

I expect the problem is that the file isn't where your program expects it to be.

I can duplicate your results if I put the image file in the wrong place, and get the book's result if I set the path to the image correctly.

You'll notice that in the documentation for the getImage() call that it says (emphasis mine):

Returns an Image object that can then be painted on the screen. The url argument must specify an absolute URL. The name argument is a specifier that is relative to the url argument .

This method always returns immediately, whether or not the image exists . When this applet attempts to draw the image on the screen, the data will be loaded. The graphics primitives that draw the image will incrementally paint on the screen.

When I put your code in a scratch project and print out the url from base , I get file:/C:/projects/scratch/scratch/target/classes/GraphicsDemo1435079664101.html .

When I run your code as-is, with the image file in src/images/hurricanes.jpg , I get the same blank applet, but if I change the getImage() call to

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

...then the picture loads just fine.

If you check the URL of base when you run your program, it shouldn't take too much fiddling for you to figure out the correct relative path based on how your project is set up.

Edit

Since your applet isn't running in the same place that you've got your image saved, you might need to use the other getImage() call which takes an absolute URL to the file.

Try something like

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

This isn't something I would recommend doing in real code, since normally you'd have no idea what the file system looks like on the machine that is running your applet, but it should get your example program working.

In a real application the image file would be deployed as part of the JAR file which contains the applet, so you'd want to use getImage(URL, String) with a relative path based on where you put the image in your JAR file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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