trouble importing bitmap

Alright, I’m trying to import a bitmap to display using JFrame. Here’s my paint method:

public void paint(Graphics g) {
BufferedImage oil = loadImage(“oil.bmp”);

        g.drawImage(oil, 800, 600, this);
  }

My compiler is telling me that the draw Image method can’t take in a BufferedImage…am I doing something wrong?

Oh, yeah, and here’s what I’m importing:

import javax.swing.JFrame;
import javax.imageio.ImageIO;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.net.URL;

Could you post the entire class file source code?

sure:

package central;

import javax.swing.JFrame;
import javax.imageio.ImageIO;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.net.URL;

public class Master
{
public static final int WIDTH = 1024;
public static final int HEIGHT = 768;

  public Master()
  {
        JFrame DC = new JFrame ("Destiny Crystal");
        DC.setBounds(0,0, WIDTH, HEIGHT);
        DC.setVisible(true);
  }
  
  
public BufferedImage loadImage(String name) 
{

    URL url = null;
    
    try 
        {
          url = getClass().getClassLoader().getResource(name);
          return ImageIO.read(url);
    } 
    
    catch (Exception e) 
        {
          System.out.println("Cannot load image " + name +" from "+url);
          System.out.println("The error was : "+e.getClass().getName()+" "+e.getMessage());
          System.exit(0);
          return null;
      }
    }

  public void paint(Graphics g)
  {
       BufferedImage oil = loadImage("oil.bmp");
        
        g.drawImage(oil, 800, 600, this);
  }
  
  public static void main(String[] args) 
  {
        Master DC = new Master();
  }

}

I think the problem is probably you passing in “this” into your drawImage function. Change that to “null”. Master is NOT an ImageObserver. Luckily though, you don’t need one, and null will do just fine.

I second that. After changing the reference of ‘this’ to ‘null’ on line #47, the code will compile. This is exactly why I wanted the full class code.

Now, I don’t have an image called “oil.bmp”, but making this change will eliminate the compiler error, and assuming you’ve initialized the url to the file “oil.bmp” correctly, things should go well.

By the way, the compiler error I got when compiling what you gave was this:


Master.java:47: cannot resolve symbol
symbol  : method drawImage (java.awt.image.BufferedImage,int,int,Master)
location: class java.awt.Graphics
            g.drawImage(oil, 800, 600, this);
             ^
1 error

This error doesn’t mean necessarily that the ‘drawImage’ method doesn’t take BufferedImages as a parameter, what this message means is that there was no ‘drawImage’ method with a method signature matching the one you provided (in this case, no ‘drawImage’ method which accepts the type ‘Master’ as a parameter).

Make sense?

Yes, that makes sense. Thank you; I’ll give that a try.

UPDATE: Another quick question: what type of object do I need to call my paint method using the dot operator?

I’m not even sure I understand the question…

There’s about a million classes with a paint() method in AWT/Swing. Could you be a bit more specific?

and further, what is the “dot operator”?

Presumably this “.” is the dot operator, which in Java is the only “operator” that allows you to call methods (but a C++ background would cause someone to call it that because of the exist of the “->” operator)

In your case, your paint() method can only be called on an instance of Master.

Kev