swing - Can't Load Image in Java -
I want to load an image and want to use it as a back ground for my zenith, No error or result
I also tried to use the buffard image and tried to set the file space on the path of the image, but I got an error "The input file can not be read! "After some research, I found this method and an easy choice.
Import javax.swing.ImageIcon; Import javax.swing. *; Import java.awt. *; Import java.awt.image. *; Import java.io.file; Import java.io.IOException; Public Sector Draerea extends JPNail {Public Dyeria () {init (); } Private zero init () {setPreferredSize (new dimension (570, 570)); SetVisible (true); } Private Zero initializeGrid (Graphics G) {Graphics2D g2d = (Graphics2D) G; Image img = new imagen ("/ linear aquisition / src / exit.png"). GetImage (); G2d.drawImage (IMG, 0, 0, this); } Public Zero Paint Component (Graphics G) {Superpact Component (G); initializeGrid (g); }} thanks in advance
never read in one Image in Paint or Paint Component Method. Understand that this method determines the extent of your program's perceived response, and if you slow down without reading repeatedly in your images, your users will not be happy.
Your problem is probably one of the wrong ways to use the comparative path. I suggest that you try to read your image in the init () method and store it as a variable, read it as a file, as you are doing Rather than receiving the input from the class resources as the spread. increases the public square DrawArea JPanel {// We do not know that this is the correct path yet. Private static final string IMG_PATH = "/exit.png"; Public Dryness () {// better Throwed it the proper exception! SetPreferredSize (new dimension (570, 570)); // It is not certain that // set Vivek (true); // There is no need for this IMG = imageIO.read (getClass (). GetResourceAsStream (IMG_PATH)); } Public Zero Paint Component (Graphics G) {Superpact Component (G); If (IMG! = Null) {g.drawImage (IMG, 0, 0, this); }}
Comments
Post a Comment