如何将图像上传到eclipse所以我可以加载它

时间:2014-11-24 02:13:40

标签: java eclipse image

好的,我正在使用一本关于Java的书中的代码,它在一些文本下显示4个PNG和1个JPG。他们为我提供了代码,图片可以从我下载的网站下载。为了将图像上传到eclipse,我在项目中创建了一个源文件夹来保存图像。然后我通过New> File> 2DGraphicsTest> LinkToFileSystem>将我的图像上传到“images”源文件夹。然后我选择了我的形象。但由于某种原因,当我运行以下代码时,我得到的文字超出了我的照片....

我的问题是,我如何正确上传图像,以便loadImage(图像)的东西得到它? 代码:

import java.awt.*;

import javax.swing.ImageIcon; import javax.swing.JFrame;

@SuppressWarnings( “串行”) 公共类ImageTest扩展了JFrame {

public static void main(String[] args) {

    DisplayMode displayMode;

    if (args.length == 3) {
        displayMode = new DisplayMode(
            Integer.parseInt(args[0]),
            Integer.parseInt(args[1]),
            Integer.parseInt(args[2]),
            DisplayMode.REFRESH_RATE_UNKNOWN);
    }
    else {
        displayMode = new DisplayMode(800, 600, 16,
            DisplayMode.REFRESH_RATE_UNKNOWN);
    }

    ImageTest test = new ImageTest();
    test.run(displayMode);
}

private static final int FONT_SIZE = 24;
private static final long DEMO_TIME = 10000;

private SimpleScreenManager screen;
private Image bgImage;
private Image opaqueImage;
private Image transparentImage;
private Image translucentImage;
private Image antiAliasedImage;
private boolean imagesLoaded;

public void run(DisplayMode displayMode) {
    setBackground(Color.blue);
    setForeground(Color.black);
    setFont(new Font("Consolas", Font.PLAIN, FONT_SIZE));
    imagesLoaded = false;

    screen = new SimpleScreenManager();
    try {
        screen.setFullScreen(displayMode, this);
        loadImages();
        try {
            Thread.sleep(DEMO_TIME);
        }
        catch (InterruptedException ex) { }
    }
    finally {
        screen.restoreScreen();
    }
}


public void loadImages() {
    bgImage = loadImage("images/background.jpg");
    opaqueImage = loadImage("images/opaque.png");
    transparentImage = loadImage("images/transparent.png");
    translucentImage = loadImage("images/translucent.png");
    antiAliasedImage = loadImage("images/antialiased.png");
    imagesLoaded = true;
    // signal to AWT to repaint this window
    repaint();
}


private Image loadImage(String fileName) {
    return new ImageIcon(fileName).getImage();
}


public void paint(Graphics g) {
    // set text anti-aliasing
    if (g instanceof Graphics2D) {
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(
            RenderingHints.KEY_TEXT_ANTIALIASING,
            RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    }

    // draw images
    if (imagesLoaded) {
        g.drawImage(bgImage, 0, 0, null);
        drawImage(g, opaqueImage, 0, 0, "Opaque");
        drawImage(g, transparentImage, 320, 0, "Transparent");
        drawImage(g, translucentImage, 0, 300, "Translucent");
        drawImage(g, antiAliasedImage, 320, 300,
            "Translucent (Anti-Aliased)");
    }
    else {
        g.drawString("Loading Images...", 5, FONT_SIZE);
    }
}


public void drawImage(Graphics g, Image image, int x, int y,
    String caption)
{
    g.drawImage(image, x, y, null);
    g.drawString(caption, x + 5, y + FONT_SIZE +
        image.getHeight(null));
}

}

0 个答案:

没有答案