Solved

Very easy question about IMAGE!

Posted on 2001-06-11
14
205 Views
Last Modified: 2010-03-31
Hello everybody!
Look at this:
import java.applet.*;
import java.awt.*;
import java.awt.image.*;
import java.net.*;

/*<applet code = "Game15" width = 242 height = 265> </applet>*/

public class Game15 extends Applet
{
Image ImageBG;

public void init()  
{setBackground(Color.red);}

public void paint(Graphics g)
{
        URL baseURL = getCodeBase();
     ImageBG = getImage(baseURL, "images/board.jpg");
     g.drawImage(ImageBG,1,1,240,240, this);
        g.drawLine(45,245,100,245);
}
}

The idea is to create an applet 240x240 with red background, image on the top and a small LINE at the bottom.

I compiled it in Java 1.0.2 and tried this sample in the different browsers and got different results!!!!!
1. In appletviewer I always get what I want without any problems!!!!!!!!!!!!!!!!
2. In IE 5.?0 it draws everything but SOMETIMES it does not redraw an image if I use repaint().
3. In IE 4.?0 it doesn't redraw an image almost always.
4. In Netscape 4.08 this sample made only red background without LINE and IMAGE. But I was able to play some JAVA-games with graphics from WebSites.
In all cases Browsers doesn't show any errors!

Where is the problem in my code? What code should I use to display an images in both type of Browsers?

Thank you in advance!
0
Comment
Question by:Masonok
  • 5
  • 4
  • 2
  • +3
14 Comments
 

Author Comment

by:Masonok
ID: 6176460
In addition!

I tried this code on Windows95,98,NT4.0,Win2000Prof
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 6176805
I have compiled you code and run the applet is IE 5.01 and Netscape 4.6/4.7. I cam't see any problem. I don't understand why you re-define the ImageBG and baseURL variables on every paint request. Do these change ? If not, try this :

import java.applet.*;
import java.awt.*;
import java.awt.image.*;
import java.net.*;

/*<applet code="Game15" width="242" height="265"></applet>*/

public class Game15 extends Applet{

     Image imageBG;

     public void init(){
          setBackground(Color.red);
          URL baseURL = getCodeBase();
          imageBG = getImage(baseURL, "board.jpg");
     }

     public void paint(Graphics g){
         g.drawImage(imageBG,1,1,240,240, this);
         g.drawLine(45,245,100,245);
     }
}
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 6176807
You should only nee to get the image and the baseURL once, so do them in initialisation rather than on each paint().
0
 

Author Comment

by:Masonok
ID: 6176853
You absolutely right about paint() function. That what I did there is meanless for real program but in this example I tried to be sure that especially at this moment picture is loaded...

But!!!! it doesn't show a picture in Netscape 4.08...

Why! What is the problem?!

It even show a button but not an image! :(
0
 
LVL 15

Accepted Solution

by:
ozymandias earned 50 total points
ID: 6176940
How about using a MediaTracker when you load the image :

Try this :

import java.applet.*;
import java.awt.*;
import java.awt.image.*;
import java.net.*;

/*<applet code="Game15" width="242" height="265"></applet>*/

public class Game15 extends Applet{

     Image imageBG;
     MediaTracker mt;

     public void init(){
          setBackground(Color.red);
          URL baseURL = getCodeBase();
          try {
               imageBG = getImage(baseURL, "board.jpg");
               mt = new MediaTracker(this);
               mt.addImage(imageBG, 1);
               if (mt.isErrorAny()){ System.out.println("Media error occurred 1."); imageBG = null; }
               mt.waitForAll();
               if (mt.isErrorAny()){ System.out.println("Media error occurred 2."); imageBG = null; }
          }catch (Exception e) {
               mt = null; System.out.println("Could not do image!");
               imageBG = null;
          }

     }

     public void paint(Graphics g){
         g.drawImage(imageBG,1,1,240,240, this);
         g.drawLine(45,245,100,245);
     }
}
0
 

Expert Comment

by:teast
ID: 6177135
to the missing line problem in netscape:
possibly
g.setColor(Color.something);
will show the line.

regards
Daniel
0
 

Author Comment

by:Masonok
ID: 6179763
The Absolutely same thing IE is GOOD! It shows everything.
But Netscape shows only the Red square.

http://www.lostjungle.com/games/java/torpedo/
I was to this site in Netscape 4.08 (right now) and played the game!!!!

But I still cannot see my image!

Are there any restrictions for images in Java?
I downloaded Netscape from official Netscape site and especially for testing my applets (to be sure that more people will be able to see my program!)

And since every browsers sooner or later are not be able to display my images - it means that there are something wrong!

May be I need to use another way to load an image? Unfortunately I am new one in Java.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Masonok
ID: 6180728
People!

what does it mean?

THIS CODE DRAWs THE LINE!!!!!!!!1 without and image
public void paint(Graphics g)  
{
     g.setColor(Color.white);
     g.drawLine(0,245,100,245);
     g.drawImage(ImageBG,1,1, this);
} I see white hrizontal line.

THIS CODE DOESN'T DRAW THE LINE!!!!!!!! and image
public void paint(Graphics g)  
{
     g.drawImage(ImageBG,1,1, this);
     g.setColor(Color.white);
     g.drawLine(0,245,100,245);

} I see red background only!

I tried to open examples from JAVA package from SUN - Netscape doesn't show any example which should show an images. IE5 does! :)

0
 

Expert Comment

by:teast
ID: 6181752
my netscape 4.72 works fine with images.
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 6181777
Have you tried compiling the code using a later version of the JDK ?

I am using 1.3.
If you post your mail address I will mail you my version of the class file to try.
0
 

Author Comment

by:Masonok
ID: 6184485
My e-mail : masonok@yahoo.com

Here could be an another question what version of Java I should use?!
I am a begginer in Java (just a little bit above 0 - because I know that Java does exist! :)   )

I have downloaded Java 1.3.02 but I afaid to use it for applets , because I heard that by default IE understands only Java1.1 so all features of Java1.2 will not works in this browser! Netscape 6.0 has an support of Java 1.3 but Netscape may became a dead browser for Windows! Also I believe that it is meanless to download 10Mb JRE1.3 to play 1 small game especially if you can play another games without it!
Can somebody tell me Am I right or wrong?

Of course if you create a big program or a really good game - you will download it.

My main idea - to create games for the Internet for free! (HOBBY!) In this case I want that more people will be able to play them. Because of that I am going to use earlier version of Java 1.0.2 which works on most browsers!
Actually Java 1.0.2 is enough to display an images and create games! :)) Actually I have create one game Puzzle15 and it works on some browsers! :( And I cannot recognize why some browsers don't want to display my images? but These browsers are able to play games from the Internet!!!!!

Also I tried to run my applet on Netscape 4.5 (English) and I failed!
I got the sample code (how image is loading) from SUN.

I type my program in Notepad and then compile it as javac.exe name.java

should I use jar or whatever? You see whole test code - it's small simple sample without any special effects. Javac gives me a CLASS file without any errors. And it dowsn't work on Netscape!

Can you give me your e-mail - I will send you this project as it was compiled on my computer! And you will try it on your computer.
Because I am the begginer in Java it is possible that my System is not setted up correctly!

Please! HELP!
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6758780
What is the status on this question which remains open today?

Moondancer
Community Support Moderator @ Experts Exchange
0
 
LVL 27

Expert Comment

by:Asta Cu
ID: 6907137
Please update the experts here who have so willingly stepped in to help you, since much time has passed since your last comments, and Email notifications may not have been generated to the participating experts here due to some problems at that time.  If you've been helped, accept the respective question by that expert to grade and close it.

Somewhat off-topic, but important.

****************************** ALERT********************************
WindowsUpdate - Critical Update alert March 28, 2002 from Microsoft
http://www.microsoft.com/technet/treeview/default.asp?url=/technet/security/bulletin/ms02-015.asp
Synopsis:
Microsoft Security Bulletin MS02-015  
28 March 2002 Cumulative Patch for Internet Explorer
Originally posted: March 28, 2002
Summary
Who should read this bulletin: Customers using Microsoft® Internet Explorer
Impact of vulnerability: Two vulnerabilities, the most serious of which would allow script to run in the Local Computer Zone.
Maximum Severity Rating: Critical
Recommendation: Consumers using the affected version of IE should install the patch immediately.
Affected Software:
Microsoft Internet Explorer 5.01
Microsoft Internet Explorer 5.5
Microsoft Internet Explorer 6.0

Thought you'd appreciate knowing this.
":0)
Asta
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 9027469
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:


[POINTS TO OZYMANDIAS]


Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
sudhakar_koundinya
EE Cleanup Volunteer
---------------------
If you feel that your question was not properly addressed, or that none of the comments received were appropriate answers, please post your concern in THIS thread.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now