问题
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);
}
}
回答1:
From what i can see, most likely your images are null or empty. Seeing how this is applet code and you're not using proper URLs, the Toolkit will likely return a sentinel value (an image with width and height = -1) if it can't locate the images. Unfortunately, g.drawImage will not throw an exception in this case. Please check that your images are loaded correctly.
回答2:
There are a lot of reason this might not work, the most obvious are
- You don't have permission to read the images (either of the file system or the URL)
- The URL reference is wrong (because the location of the files are not where you think they are)
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.
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");
This assumes that the images reside in the same directory on the server as the HTML file. You can use relative paths as needed.
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();
}
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.
This eventually resulted in
PS- I set the applets layout manager to BorderLayout
rather then using absolute positioning.
来源:https://stackoverflow.com/questions/11910783/images-are-not-showing-up-in-paint-component