Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  trouble importing bitmap  (Read 1382 times)
0 Members and 1 Guest are viewing this topic.
Offline Alakazam

Senior Newbie




Java games rock!


« Posted 2004-11-02 19:08:56 »

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;
kul_th_las
Guest
« Reply #1 - Posted 2004-11-02 21:21:02 »

Could you post the entire class file source code?
Offline Alakazam

Senior Newbie




Java games rock!


« Reply #2 - Posted 2004-11-02 21:33:24 »

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();
     }
}
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Malohkan

Senior Member




while (true) System.out.println("WOO!!!!");


« Reply #3 - Posted 2004-11-02 23:35:01 »

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.

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
kul_th_las
Guest
« Reply #4 - Posted 2004-11-03 02:59:10 »

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:

1  
2  
3  
4  
5  
6  
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?
Offline Alakazam

Senior Newbie




Java games rock!


« Reply #5 - Posted 2004-11-03 19:58:00 »

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?
kul_th_las
Guest
« Reply #6 - Posted 2004-11-05 21:21:40 »

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?
Offline Malohkan

Senior Member




while (true) System.out.println("WOO!!!!");


« Reply #7 - Posted 2004-11-06 01:18:58 »

and further, what is the "dot operator"?

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Offline kevglass

JGO Kernel


Medals: 163
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #8 - Posted 2004-11-06 06:44:57 »

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

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (20 views)
2014-09-12 09:08:26

Norakomi (55 views)
2014-09-10 13:57:51

TehJavaDev (64 views)
2014-09-10 06:39:09

Tekkerue (32 views)
2014-09-09 02:24:56

mitcheeb (54 views)
2014-09-08 06:06:29

BurntPizza (38 views)
2014-09-07 01:13:42

Longarmx (24 views)
2014-09-07 01:12:14

Longarmx (28 views)
2014-09-07 01:11:22

Longarmx (28 views)
2014-09-07 01:10:19

mitcheeb (36 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!