Solved

JDK 1.02 - drawImage(x, y, w, h, observer) behaviour

Posted on 1997-10-21
4
207 Views
Last Modified: 2008-03-06
I'm writing a game in Java, for which I create two threads,
one for event and game handling, and one for redrawing and
updating the screen (in the usual way, using an off-screen
GC, etc.). Most of the time I use the drawImage(x, y, obsr)
function for drawing the sprites, because their width and
height is constant. But I wanted to create a dynamic scaling
effect for some of them, so I decided to create a loop where
the width and height (not of the actual image, but that
which I wish to draw on screen) change from 0 to some fixed
numbers and use drawImage(x, y, width, height, obsr) to
draw the scaled image on screen.
The problem is that, while with the 1st drawImage() I get
no flashing and tearing (I already mentioned I'm using an
off-screen GC and a media tracker to eliminate these), with
the scaling version I get significant flashing and tearing
all over the window of the applet and I never get to see the
drawn image!
So, what's going on? Any workarounds?(besides using JDK 1.1)

Thanks in advance
0
Comment
Question by:DGeomel
4 Comments
 
LVL 6

Expert Comment

by:jpk041897
ID: 1229327
Have you looked into double buffering?
0
 
LVL 16

Accepted Solution

by:
imladris earned 100 total points
ID: 1229328
I have found that, contrary to intuition, the image appears to get "reloaded" or rerendered or something internally, if you draw it scaled. This is presumably why MediaTracker provides some scaled variations. Using them cures the problem for me:

      public void adjScale(String f)
      {      int t=pi.getWidth(null);
            width=Util.adjScale(t);
            height=Util.adjScale(pi.getHeight(null));
            scaled=(t!=width);
            if(scaled)
            {      trker.addImage(pi,1,width,height);
                  imgSynch("image "+f+" wouldn't scale");
            }
            return;
      }

      private void imgSynch(String es)
      {      try
            {      trker.waitForID(1);
            } catch(InterruptedException e){};
            if(trker.isErrorID(1))Util.sysError(es,true);
            return;
      }

The adjscale method potentially scales images based on their own size. If it is found that scaling is needed then a scaled variation of MediaTracker.addImage is called and waited on before the image is used.

0
 
LVL 4

Expert Comment

by:russgold
ID: 1229329
You say you are using "an offscreen GC..." - can you explain what you mean?  I generally take "offscreen" to imply a double buffering approach, but GC usually means "garbage collection."

The difference in your approaches is that the scaling takes noticeably more time, so double buffering is essential.
0
 

Author Comment

by:DGeomel
ID: 1229330
To russgold: By "GC" I mean "Graphics Context". I think the term
is not used in Java, but if you've programmed in other platforms
(e.g. Windows) you must've stumbled upon it before.

To jpk: By using an off-screen GC I AM using double-buffering.

To imladris: Thanks, it solved my problem too, but I need some
more clarification:
I want to scale one image for which I use getImage();
If I have to create 10 scaled versions of the same image, I
will call getImage() 10 times with the same filename, and
call the scaled version mediatracker:addImage() for each of them.
But does the file get downloaded 10 times, or only once (and then
processed locally to create the scaled versions)?


0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sites similar to codingbat to improve coding hanson skills 3 39
Error trying to install RTMT Win7 5 38
Apps blocked by Java 9 55
difference of if loops 23 37
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

947 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

22 Experts available now in Live!

Get 1:1 Help Now