Solved

Very easy question about IMAGE!

Posted on 2001-06-11
14
210 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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
 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Apps blocked by Java 9 88
reverse digits of a number using for loop 5 52
java jdbc batch example 8 41
Java Restore security prompts not working 10 56
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

856 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