Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Alternative to copyArea?

Posted on 1999-07-13
12
Medium Priority
?
423 Views
Last Modified: 2008-03-10
I'm doing an animation which involves moving an image across the screen. I use an offscreen buffer & double-buffering, there's a background image and a 'sprite' image. For a start I was copying the entire background image to the buffer, then the sprite, updating, redrawing the bg image to 'clear' then screen and redrawing the sprite at the new position.

I then used copyArea to copy part of a background to an offscreen area so that I can then draw an image over that part of the background, update the screen, copy the background section back, move the image and repeat.

This works fine but I'd rather create a small graphics buffer to hold the image. So is there an alternative function that will copy an area of one buffer into another?

Cheers
0
Comment
Question by:BOINGED
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
12 Comments
 
LVL 7

Expert Comment

by:vladi21
ID: 1245900
fastest way when you working with arrays - use System.arraycopy()
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 1245901
copyArea works inside the same graphics object.
you can always use another (offscreen) graphics object and then use drawImage.
or you can use MemoryImageSource (with byte[])

but which one is faster ...
0
 

Author Comment

by:BOINGED
ID: 1245902
vladi21
How could I use System.arraycopy() with an image - I know how to put the image into an array via an image filter but don't really want to do this.

heyhey
Using copyArea at the moment within the same graphics object which is my offscreen buffer and using drawImage in the paint method/update method.

Don't know anything about MemoryImageSource - will that still require the image to be in an array?
0
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
LVL 16

Expert Comment

by:heyhey_
ID: 1245903
copyArea() is one of the possible solutions.
why don't you like it ?

otherwise you can always have two separate offscreen buffers and use drawImage().
or you can have MemoryImage source (think of it as one big Color map) - you can chagne some Color values inside the array and call the newPixels() method to inform all the renderers that you have changed your underline Color information.
I think that a lot of online applet games (1.1 compatible) use MemoryImageSource - in such case you work with the raw color data and can use System.arraycopy() etc ...
0
 

Author Comment

by:BOINGED
ID: 1245904
I'm against copyArea as it involves having wasted buffer space - e.g if my sprite is 32x32 then I have to make the offscreen buffer either w+32 wide, or h+32 high (w,h=dimensions of applet size) wasting either h-32 or w-32 of space respectively.

I used the 2 offscreen buffer method originally but was unhappy with the performance. Each redraw would involve 2 x drawImage of size w,h + a drawImage for the sprite, rather than 1 x drawImage of size w,h + 3 x drawImage for the sprite (once to copy background area away, once to draw sprite and once to copy background area back again later).

I'll take a look at MemoryImageSource though.
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 1245905
I think that I've some (pretty small) example...
I'll try to find it and post it.
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 1245906
I'm reposting this example.
hope that it helps

/*
  this is a sample how to grab "image", modify some of the pixels and create new image
  this example is application (but you can easy turn it into applet)
 
  to run , just set appropriate image name
// image = Toolkit.getDefaultToolkit().getImage("d:/duke.gif");  
  compile it with javac Test.java
  run it with java Test
 there are some comments, but you can get enough information from the docs ...
 
 that's all
   heyhey
   
P.S. just a note - maybe I deserve some more points ... :)
 
*/
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
public class Test extends Panel
{
Image image, image2, image3;
PixelGrabber grabber;
int h,w;
Dimension size;
    int[] pixels;
    int[] pixels2;
    int[] pixels3;
public Test()
{
   
image = Toolkit.getDefaultToolkit().getImage("d:/duke.gif");
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(image, 0);
try {
tracker.waitForID(0);
} catch (InterruptedException e )
{
        System.out.println("Interrupted !!!");    
}
// Image "duke.gif" loaded
h = image.getHeight(null);
w = image.getWidth(null);
System.out.println("h = " + h + ", w = " + w);
     size = new Dimension((w+10) * 3,h + 10);
// adjust array for pixel info    
        pixels = new int[w * h];    
     grabber = new PixelGrabber(image, 0, 0, w, h, pixels, 0, w);
// grab the pixels
     try {
         boolean res = grabber.grabPixels();
} catch (InterruptedException e )
{
        System.out.println("Interrupted !!!");    
}
// dump all information
/*
        System.out.println("ColorModel " + grabber.getColorModel());
for (int i = 0; i< w*h; i++)
        {
            int pixel = pixels[i];
            int alpha = (pixel >> 24) & 0xff;
         int red   = (pixel >> 16) & 0xff;
         int green = (pixel >>  8) & 0xff;
         int blue  = (pixel      ) & 0xff;
         System.out.println("[" + red + ", " + green + ", " + blue + ", " + alpha + "]");
        }
*/        
// create new pixel array with 'random' colors (example form documentation)
    pixels2 = new int[w * h];
     int index = 0;
     for (int y = 0; y < h; y++) {
         int red = (y * 255) / (h - 1);
         for (int x = 0; x < w; x++) {
     int blue = (x * 255) / (w - 1);
     pixels2[index++] = (255 << 24) | (red << 16) | blue;
         }
     }
// create image from pixels2 array    
        image2 = createImage(new MemoryImageSource(w, h, pixels2, 0, w));
// get upper left pixel for empty value        
int empty = pixels[0];
// create new pixel array from modified pixels
    pixels3 = new int[w * h];
    System.arraycopy(pixels, 0, pixels3, 0, w * h);
    for (int i = 0; i< w * h; i++)
    {
//        System.out.print(".");
        if (pixels3[i] == empty) pixels3[i] = pixels2[i] ;
    }
// create image from pixels3 array    
        image3 = createImage(new MemoryImageSource(w, h, pixels3, 0, w));
}
    public Dimension getPreferredSize()
    {
        return size;
    }
    public void paint(Graphics g)
    {
// paint the three pictures
        g.drawImage(image, 5, 5 , null, null);
        g.drawImage(image2, w + 10 + 5, 5 , null, null);
        g.drawImage(image3, (w + 10) * 2 + 5, 5 , null, null);
    }
public static void main (String[] args)
{
Frame f = new Frame ();
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Test t = new Test();
f.add(t);
f.pack();
f.show();
}
}
0
 

Author Comment

by:BOINGED
ID: 1245907
Cool! 'Answer' the question, I'll try the code later. [note point increase :) ]
0
 
LVL 16

Accepted Solution

by:
heyhey_ earned 400 total points
ID: 1245908
Thanks.

if you have more problems, post a comment :)
0
 

Author Comment

by:BOINGED
ID: 1245909
Cool! 'Answer' the question, I'll try the code later. [note point increase :) ]
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 1245910
>>P.S. just a note - maybe I deserve some more points ... :)

oh I just noticed this line ... I have posted this code at some question - and now just Copy&Paste it.
I didn't even try it ... but I think it will work fine.
0
 

Author Comment

by:BOINGED
ID: 1245911
Cool! 'Answer' the question, I'll try the code later. [note point increase :) ]
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
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.

670 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