Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (803)
Games in Android Showcase (237)
games submitted by our members
Games in WIP (867)
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  
  mt object wouldnt load image  (Read 1820 times)
0 Members and 1 Guest are viewing this topic.
Offline numerical

Senior Newbie





« Posted 2017-02-21 07:34:03 »

Hello,

Here is the code for MediaTracker object it doesnt loads image, the image is located in package DrawImage, also what is the difference between getDocumentBase() and getCodeBase()

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
   package DrawImage;
     
    import java.net.*;    
    import java.awt.*;
   
    public class LoadImage extends java.applet.Applet
    {
        Image uc  ;
        URL base;
        MediaTracker mt;      
   
       public void init()
       {
             mt = new MediaTracker(this);
           try{
                  base = getDocumentBase();
                  }catch(Exception e){}
       
           uc = getImage(base, "src//DrawImage//java.gif");
           mt.addImage(uc,1);
       
           try{
           mt.waitForAll();
           }catch(InterruptedException e){}    
   }
   
       public void paint(Graphics g)
   {  
           g.drawImage(uc, 45  , 45, Color.yellow, this);
       }        
   }  


 

Offline theagentd
« Reply #1 - Posted 2017-02-21 10:54:26 »

MediaTracker is a very outdated way of loading images. I strongly recommend using ImageIO.read() instead!

Myomyomyo.
Offline numerical

Senior Newbie





« Reply #2 - Posted 2017-02-21 13:28:04 »

thanks, here is imageio but this also doesnt loads the image

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
package DrawImage;

import java.applet.Applet;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.net.URL;
import javax.imageio.*;
 
public class LoadImage extends Applet {
 
     private BufferedImage img;
 
     public void init() {
         try {
             URL url = new URL(getCodeBase(), "src//DrawImage//java.GIF");
             img = ImageIO.read(url);
         } catch (IOException e) {
         }
     }
 
     public void paint(Graphics g) {
       g.drawImage(img, 50, 50, null);
     }
}
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline theagentd
« Reply #3 - Posted 2017-02-21 14:06:53 »

You should never ever silently swallow an exception like that. You're probably getting an error telling you what the problem is.

Myomyomyo.
Offline numerical

Senior Newbie





« Reply #4 - Posted 2017-02-22 07:20:46 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
package DrawImage;

import java.applet.Applet;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.net.URL;
import javax.imageio.*;
 
public class LoadWindows extends Applet {
 
     private BufferedImage img;
 
     public void init() {
         try {
             URL url = new URL(getCodeBase(), "src//DrawImage//java.GIF");
             img = ImageIO.read(url);
         } catch (IOException e) {
             System.out.println(e);
         }
     }
 
     public void paint(Graphics g) {
       g.drawImage(img, 50, 50, null);
     }
}


here is the code with exception and it says:"javax.imageio.IIOException: Can't get input stream from URL!"
Offline Riven
Administrator

« JGO Overlord »


Medals: 1371
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2017-02-22 07:38:06 »

Not sure why you think you have to use "//" -- did you see "\\" and attempt to swap the slashes to make it cross-platform? Simply use "/"

That will be the first step to solve your problem. This thread will help you with the next steps:
http://www.java-gaming.org/topics/faq-cannot-find-the-resource-file/33226/view.html

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline numerical

Senior Newbie





« Reply #6 - Posted 2017-02-22 08:49:06 »

thanks works now with the toolkit

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
package DrawImage;   

import java.applet.Applet;
import java.awt.*;

public class Load extends Applet
{
   private Image img;  

   public void init()
   {
      img = null;
     
   }
   public void loadImage()
   {
      try
      {
         img = Toolkit.getDefaultToolkit().getImage("src//DrawImage//java.gif");;
      }
      catch(Exception e) { }
   }
   public void paint(Graphics g)
   {
      if (img == null)
         loadImage();
      g.drawImage(img, 0, 0, this);
   }
}
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (397 views)
2019-09-04 15:33:17

hadezbladez (5280 views)
2018-11-16 13:46:03

hadezbladez (2204 views)
2018-11-16 13:41:33

hadezbladez (5544 views)
2018-11-16 13:35:35

hadezbladez (1150 views)
2018-11-16 13:32:03

EgonOlsen (4584 views)
2018-06-10 19:43:48

EgonOlsen (5462 views)
2018-06-10 19:43:44

EgonOlsen (3119 views)
2018-06-10 19:43:20

DesertCoockie (4015 views)
2018-05-13 18:23:11

nelsongames (4708 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04: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!