这段代码本应该显示一个背景图片和玩家,但实际上只显示了一个粉色的屏幕。我无法确定问题出在哪里,请看我的代码。
package main;
import java.awt.*;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class Images extends JFrame {
public static void main(String Args[]) {
DisplayMode dm = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN); // This is going to take 4 parameter, first 2 is x and y for resolotion. Bit depth, the number of bits in a cloour
// 16 is your bit depth. the last one is the monitor refresh, it means it will refres how much it wants
Images i = new Images(); // making an object for this class
i.run(dm); // making a run method and is taking the dm as a parameter, in this method we are putting stuff on the screen.
}
private Screen s; // Creating the Screen, from the Screen.java
private Image bg; // Background
private Image pic; // Face icon
private boolean loaded; // Making the loaded
//Run method
private void run(DisplayMode dm) { // this is where we do things for the screen
setBackground(Color.PINK); // Setting the Background
setForeground(Color.WHITE); // Setting the ForeGround
setFont(new Font("Arial", Font.PLAIN, 24)); // setting the font
s = new Screen(); // now we can call ALL methods from the Screen object
try {
s.setFullScreen(dm, this); // This is setting the full screen, it takes in 2 parameters, dm is the display mode, so its setting the display settings, the next part is the this, what is just s, the screen object.
loadpics(); // calling the loadpics method
try { // so if that try block works, then it will put it to sleep for 5 seconds
Thread.sleep(5000); // its doing this because, at the bottom (s.restorescreen) this makes it into a window again. so it needs to show it for 5 seconds.
} catch (Exception ex) {
}
} finally {
s.restoreScreen();
}
}
// Loads Pictures
private void loadpics() {
System.out.println("Loadpics == true");
bg = new ImageIcon("Users/georgebastow/Picture/background.jpg").getImage(); // Gets the background
pic = new ImageIcon("Users/georgebastow/Picture/Player.png").getImage(); // Gets the Player
System.out.println("Loaded == true in da future!");
loaded = true; // If the pics are loaded then...
}
public void paint(Graphics g) {
if (g instanceof Graphics2D) { // This has to happen, its saying if g is in the class Graphics2D, so if we have the latest version of java, then this will run
Graphics2D g2 = (Graphics2D) g; // Were making the Text smooth but we can only do it on a graphcis2D object.
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); // we are making the text aniti alias and turning it on!(it means making the text smooths! :) )
}
if(loaded == true){
System.out.println("Loaded == true3");
g.drawImage(bg,0,0,null);
g.drawImage(pic, 170, 180, null);
System.out.println("Loaded == true4");
}
}
}
在此提前非常感谢你。
ImageIcon
实例化的末尾,您需要像之前一样使用.getImage()
。确保将bg
保留为无法绘制的Image
和ImageIcon
。 - Paul Samsothabg = ImageIO.read(Images.class.getResource("/res/background.jpg");
并将其包装在 try/catch 块中。如果图像未加载,则说明路径错误。如果没有抛出异常,则问题可能出在其他地方。 - Paul Samsotha