Этот код должен отображать фоновое изображение и плеер. Но у него просто розовый рожок. Я не могу понять, откуда эта проблема, вот мой код.ImageIcon Не отображается в java
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");
}
}
}
Заранее большое спасибо
Привет, я положил background.jpg в папку res, но он попросил меня изменить bg на ImageIcon, а не на изображение. Это означает, что я не могу выполнить g.drawImage (bg, 0,0, null) ;. Я попытался изменить его на drawLine, но это тоже не помогло. – Pilkin
вам нужно использовать .getImage(), как и раньше, в конце экземпляра 'ImageIcon'. Не забудьте сохранить 'bg' как« изображение », которое вы не можете рисовать, и« ImageIcon » –
Я сделал это, когда я запустил программу, она не отображает изображение. Почему это происходит. Он отображает розовый на весь экран. – Pilkin