Question

Trying to draw an image and I have no idea why it's not appearing on the screen. Image is loaded fine.

System.out.printf("%nPlayer_1.img_player1 is: "+Player_1.img_player1); says that the image is loaded correctly (correct image w/h etc).

Have tried all manner of image observers, drawing the image from different classes (namely Player_1.drawSquare and MyPanel.painComponent)

There are regular repaint() requests coming in from the Control class, but I do not believe this is preventing the drawing.

No error's are thrown.

player_1.gif is located within the game_2 package, I have tried running the code with it in the src and root folder.

package test;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.net.URL;
public class Main{
 public static void main(String[] args) {
     GUI.main(args);
 }
}

class GUI {
static JFrame f = new JFrame("Swing Paint Demo");
    public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI(); 
        }
    });
}

private static void createAndShowGUI() {
    System.out.printf("%nCreated GUI on EDT? "+
    SwingUtilities.isEventDispatchThread());

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    f.add(new MyPanel());
    f.setSize(640,640);
    f.setVisible(true);


} 

}

class MyPanel extends JPanel {

public MyPanel() {

    //setBorder(BorderFactory.createLineBorder(Color.black));
}


public Dimension getPreferredSize() {
    return new Dimension(640,640);
}

public void paintComponent(Graphics g) {
    super.paintComponent(g);       
    System.out.printf("%nPaincomponent on GUI");

    Player_1.paintSquare(g);
    System.out.printf("%nPlayer_1.img_player1 is: "+Player_1.img_player1);

}

}

class Player_1{
public static Image img_player1;
public int int_x = 0;
public int int_y = 1;
public int int_w = 1;
public int int_h = 1;

public static void paintSquare(Graphics g){
     if (img_player1 == null)
         img_player1 = IOControl.getImage("player_1.gif");

    g.drawImage(img_player1, 32, 32, 32, 32, GUI.f);

}
}
class IOControl {

public static void main(String[] args) {

}

static BufferedImage getImage(String path)
{

    BufferedImage tempImage = null;
        try
        {

                URL imageURL = Main.class.getResource(path);
                tempImage = toBufferedImage(Toolkit.getDefaultToolkit().getImage(imageURL));
        }
        catch(Exception e)
        {
                System.out.printf("%nAn error occured -" + e.getMessage());
        }
        System.out.printf("%n"+path);
        System.out.printf("%nIOControl.path");

        System.out.printf("%n"+tempImage);
        System.out.printf("%nIOControl.tempImage");
        return tempImage;


}

public static BufferedImage toBufferedImage(Image img)
{
    if (img instanceof BufferedImage)
    {
        return (BufferedImage) img;
    }

    // Create a buffered image with transparency
    BufferedImage bimage = new BufferedImage(32, 32, BufferedImage.TYPE_INT_ARGB);

    // Draw the image on to the buffered image
    Graphics2D bGr = bimage.createGraphics();
    bGr.drawImage(img, 0, 0, null);
    bGr.dispose();

    // Return the buffered image
    return bimage;
}
}
Was it helpful?

Solution

  1. Don't call the main method. It is meant for the JVM to call as the starting point of your program. Also, you should only have one main method, in whichever class is the launching class.

  2. Don't create the image in the paintXxx method. You should create in the constructor.

  3. All the other faulty points I mentioned in my comments in the original post.

  4. It looks like your main problem is in the getImage method. I simplified it for you and it works (given I fixed the other points I mentioned above).

    public static BufferedImage getImage(String path) {
        BufferedImage img = null;
        try {
            img = ImageIO.read(GUI.class.getResource(path));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return img;
    }
    

Here's the full running code. Note: you had the image location correct, with it in the same package as the Main class.

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;

public class Main {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new GUI().createAndShowGUI();
            }
        });
    }
}

class GUI {

    JFrame f = new JFrame("Swing Paint Demo");

    public void createAndShowGUI() {
        System.out.printf("%nCreated GUI on EDT? "
                + SwingUtilities.isEventDispatchThread());

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new MyPanel());
        f.setSize(640, 640);
        f.setVisible(true);

    }

}

class MyPanel extends JPanel {

    Player_1 player1;

    public MyPanel() {
        player1 = new Player_1(MyPanel.this);
    }

    public Dimension getPreferredSize() {
        return new Dimension(640, 640);
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        System.out.printf("%nPaincomponent on GUI");

        player1.paintSquare(g);

    }

}

class Player_1 {

    public Image img_player1;
    public int int_x = 0;
    public int int_y = 1;
    public int int_w = 1;
    public int int_h = 1;
    JPanel panel;

    public Player_1(JPanel panel) {
        this.panel = panel;
        img_player1 = IOControl.getImage("player_1.png");
    }

    public void paintSquare(Graphics g) {
        g.drawImage(img_player1, 32, 32, 32, 32, panel);
    }
}

class IOControl {

    public static BufferedImage getImage(String path) {
        BufferedImage img = null;
        try {
            img = ImageIO.read(GUI.class.getResource(path));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return img;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top