user3237600
user3237600

Reputation: 3

Loading Images into Java

I've just started programming in java. Recently, however, I haven't been able to load images into my games.

I've tried loading them into a separate resource package and read/watched countless tutorials, but nothing seems to be working at all!

Can someone tell me what's wrong with the code or provide any suggestions? Any help would be much appreciated.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class Main extends JFrame{

Image dbImage;
Graphics dbg;

//Variables for screen size
int
GWIDTH = 800,
GHEIGHT = 500;

//Dimension of gwidth and gheight
Dimension screenSize = new Dimension (GWIDTH, GHEIGHT);

public Main (){
//constructor to spawn window
this.setTitle ("Pond");
this.setSize(screenSize);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setBackground(Color.cyan);
this.addKeyListener(new AL ());
ImageIcon img = new ImageIcon (getClass().getResource("/resources/ninja-png.gif"));

}

 public static void main (String[] args){

 Main m = new Main ();
 }

 //Double Buffering
  @Override

   public void paint (Graphics g){

dbImage = createImage (getWidth(), getHeight());
dbg = dbImage.getGraphics ();
draw (dbg);
g.drawImage(dbImage, 0, 0, this);  

 }

 public void draw (Graphics g){
 }
 public void paintComponent (Graphics g){
 }
 //Event Listener Class
 public class AL extends KeyAdapter {
@Override
public void keyPressed (KeyEvent e){
}
@Override
public void keyReleased (KeyEvent e){
 }
 }

  } 

Upvotes: 0

Views: 1568

Answers (2)

NonameSL
NonameSL

Reputation: 27

I belive there is a simpler way: Create a JLabel and add it to the frame:

BufferedImage bi = ImageIO.read(/*This uses a URL*/new URL(YOURURLHERE));
ImageIcon image = new ImageIcon(bi);
JLabel label = new JLabel(image);

Then just add that label to the frame.

Upvotes: 1

Paul Samsotha
Paul Samsotha

Reputation: 209102

"Can someone tell me what's wrong with the code or provide any suggestions?"

  1. Don't paint on top-level containers like JFrame.

  2. Instead use a JPanel and override its paintComponent method and call super.paintComponent

  3. @Override getPreferredSize() on JPanel when painting, so the panel will have a preffered size.

  4. Call frame.pack() instead of setting the size, and the preferred size of the JPanel will be respected.

  5. Add that custom painting panel to the JFrame.

  6. You never initialized the image you were trying to paint. Do this instead.

    dbImage = new ImageIcon(getClass().getResource("/resources/stackoverflow5.png")).getImage();
    

    of declaring a new ImageIcon that you never use.

  7. Run Swing apps from the Event Dispatch Thread like this

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                Main m = new Main();
            }
        });
    }
    
  8. Makes sure you have a resources package directly in the src

    Project
          src
             resources
    

enter image description here

Full refactored source code with all above mentioned things.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Main extends JFrame {

    Image dbImage;

    int GWIDTH = 800;
    int GHEIGHT = 500;

    public Main() {
        dbImage = new ImageIcon(getClass().getResource("/resources/stackoverflow5.png")).getImage();
        this.setTitle("Pond");
        add(new ImagePanel());
        this.pack();
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setBackground(Color.cyan);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                Main m = new Main();
            }
        });
    }

    public class ImagePanel extends JPanel {

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);

            g.drawImage(dbImage, 0, 0, GWIDTH, GHEIGHT, this);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(GWIDTH, GHEIGHT);
        }
    }
}

Upvotes: 3

Related Questions