图像未显示在绘图组件中 [英] images are not showing up in paint component

查看:95
本文介绍了图像未显示在绘图组件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它给了我一个黑色屏幕的setbackground,
但它似乎没有读取图像或绘制它们,
一些帮助将不胜感激,如果你能告诉我信息
与我的代码有关,我无法理解一般的
r模糊的答案,谢谢。

it gives me a black screen for setbackground, but it doesnt seem to read the images or draw them, some help would be appreciated, also if you could tell me the information pertaining to my code, i have trouble understanding general r vague answers, thanks.

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

public class Movie extends JApplet {

    private String movName1;
    private String director1;
    private int yearMade1;
    private Image movPic1;
    private String movName2;
    private String director2;
    private int yearMade2;
    private Image movPic2;
    private String movName3;
    private String director3;
    private int yearMade3;
    private Image movPic3;
    private String movName4;
    private String director4;
    private int yearMade4;
    private Image movPic4;

    public void init() {
        MovieDis goo = new MovieDis(movPic1, movPic2, movPic3, movPic4);
        goo.setBounds(0, 0, 750, 500);
        add(goo);
    }
}

class MovieDis extends JComponent {

    private String movName1;
    private String director1;
    private int yearMade1;
    private Image movPic1;
    private String movName2;
    private String director2;
    private int yearMade2;
    private Image movPic2;
    private String movName3;
    private String director3;
    private int yearMade3;
    private Image movPic3;
    private String movName4;
    private String director4;
    private int yearMade4;
    private Image movPic4;

    public MovieDis(Image movPic1, Image movPic2, Image movPic3, Image movPic4) {
        setBackground(Color.black);
        movPic1 = Toolkit.getDefaultToolkit().createImage("Shaw.jpg");
        movPic2 = Toolkit.getDefaultToolkit().createImage("dances.jpg");
        movPic3 = Toolkit.getDefaultToolkit().getImage("Inception.jpg");
        movPic4 = Toolkit.getDefaultToolkit().getImage("Cuckoo.jpg");
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.black);
        g.fillRect(0, 0, 750, 500);
        g.drawImage(movPic1, 35, 35, 200, 200, this);
        g.drawImage(movPic2, 35, 515, 200, 200, this);
        g.drawImage(movPic3, 265, 35, 200, 200, this);
        g.drawImage(movPic4, 35, 515, 200, 200, this);
    }
}


推荐答案

那里有很多原因可能不起作用,最明显的是

There are a lot of reason this might not work, the most obvious are


  • 您无权阅读图像(文件系统或URL)

  • URL引用错误(因为文件的位置不在您认为的位置)

根据文件的存储位置,您有两种选择。

Depending on where the files are stored, you have two choices.

如果文件存储在服务器上,还有罐。您需要知道文档库引用。

If the files are stored on the server, along with the Jar. You will need to know the document base reference.

所以在 init 方法中,您需要添加

So in your init method, you'll need add

URL base = getDocumentBase();
System.out.println("base = " + base); // testing only
movPic1 = getImage(base, "issue169.jpg");
movPic2 = getImage(base, "issue194.jpg");
movPic3 = getImage(base, "issue248.jpg");
movPic4 = getImage(base, "issue78.jpg");

这假设图像与HTML文件位于服务器上的同一目录中。您可以根据需要使用相对路径。

This assumes that the images reside in the same directory on the server as the HTML file. You can use relative paths as needed.

如果文件存储/捆绑在 Jar (这可能是静态图像的首选方式),你需要使用类加载器来找到它们

If the files are stored/bundled in the Jar (this is probably the preferred way for static images), you need to use the classloader to find them

try {
    movPic1 = ImageIO.read(getClass().getResource("/testapplet/issue169.jpg"));
    movPic2 = ImageIO.read(getClass().getResource("/testapplet/issue194.jpg"));
    movPic3 = ImageIO.read(getClass().getResource("/testapplet/issue248.jpg"));
    movPic4 = ImageIO.read(getClass().getResource("/testapplet/issue78.jpg"));
} catch (IOException ex) {
    ex.printStackTrace();
}

我使用这种方法,但在测试时确实遇到了一个小问题。因为我的文件已被捆绑(但),applet查看器试图将它们作为文件引用加载,它没有权限 - applet视图允许你将其设置为不受限制。

I used this method but did run into a small problem while testing. Because my files handed been bundled (yet), the applet viewer was trying to load them as a File reference which it didn't have permission to do - the applet view allows you to set it as unrestricted.

这最终导致

PS-我将applets布局管理器设置为 BorderLayout 而不是使用绝对定位。

PS- I set the applets layout manager to BorderLayout rather then using absolute positioning.

这篇关于图像未显示在绘图组件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆