tobi(at)vonloesch.de
|
Java applet TutorialShowing and manipulating pics via MemoryImageSource1.01 IntroductionWelcome to my first Java tutorial. This one deals with a very simple zoomer. We load a picture into memory and zoom in and out until the user aborts the execution. For output we use a very simple and un-advanced method, only good enough for demonstration purposes. Have fun... (((you can get the complete source here)))1.1 Create Applet and ThreadFirst we need to create a new Class, inherited from java.applet.Applet (which is inherited from java.awt.panel), with the interface Runnable (not absolutely necessary for the applet, but for the thread). An applet needs (in contrast to an application) no main() method, because the init() and start() methods manage the execution of the applet. And we have to import some classes for loading and processing the pictures.import java.awt.*
public class bild extends java.applet.Applet implements Runnable
We also need some variables for zooming and some for saving the picture
in memory.
public void start(){
public void stop(){
1.2. InitializationA constructor is not necessary, because we initialize the thread in init() , which is always called when the browser loads the applet. But we have to use a MediaTracker to supervise the loading of the resources (pictures, sounds, ...), because we don't know when they are completely loaded. The method getCodeBase() returns the path to the applet, so there is no need to use absolute paths. By using getimage (URL, string) we could load and process gif and jpeg pictures. To get the color information of every single pixel we copy the data with a PixelGrabber from the Image variable to our (previously defined) array. The array has to be defined as array of int because every pixel uses 4 x 8 bit for color information, the highest byte is the alpha value (for loaded pictures, this value is always 0xFF, opaque). The other three bytes represent the color values for red, green and blue. With the help of our MemoryImageSource object we later (after processing) re-convert the array back to Image.
1.3 The CalculationThe method run() is called by the thread and contains the actual code. Not much to do here, the new picture is calculatedby ZoomIn (int) and displayed with repaint(). Then we stop the thread for 30 ms, to slow down the demo. The next Tutorial will have automatic time measurement to get better control of the speed (on different pc's the same execution speed). One problem of Java is that it manages the memory usage on it's own and some methods and objects are quite generous with memory. The GarbageCollector does his work in the breaks. But according to my experience he doesn't use this breaks very wisely, that's why the applet performance gets worse and worse after some time of running and you get the feeling that it formats your HDD. To prevent that memory gets to low, we call gc() by ourselves every time we had a complete zoom cycle. ZoomIn(int) does the actual calculation of the next image, but optimization is currently very low. The method is synchronized as paint() is to prevent the simultaneous output and calculation of the same picture (distorts the displayed picture) To display the calculated image (saved in displayAr) on the screen we copy the data from the array to the Image object by using createImage (MemoryImageSource). Here is a flow chart which shows the way of the image till output :
public void run(){
while (!quit){
public synchronized void ZoomIn (int n){
int mult216[]=new int [height];
// to save one mutliplication
for(int i2=0;i2<=height-1;i2++){
for(int i1=0;i1<=width-1;i1++){
display = createImage(mic);
1.4 Output to screenTo get our new picture on screen as quick as possible, we overwrite the paint(Graphics) and update(Graphics) methods.paint(Graphics) is obvious (because we need to have our own customized output), but the update(Graphics) method is important, because normally Java would first clear the screen and then repaint it which is very slow and will result in heavy flicker. Besides we can overwrite the whole picture thus no deleting/clearing is necessary public synchronized void paint(Graphics g)
}
1.5 The RestThe remaining methods take care of the mouse action :public boolean mouseEnter(Event e, int x, int y){
public boolean mouseExit(Event e, int x, int y){
1.6 Epilogue und PreviewThat's all ! Our funky Zoomer is done. The zooming algorithm isn't very good (quick and dirty) as the output method MemoryImageSource is. In the flowchart you can see how many steps you need to produce one picture and that many steps are very time and performance consuming which results in the low speed. But for simple effects (like this one) this method is OK.In the next tutorial I will use another method (for output) and I will show how to use the good old 8 bit pallet. As usual I'm thankful for any kind of suggestions, ideas, comments and code improvements because my Java skills are far from perfect. ©©©© 18.Juli.00 Tobias von Loesch
|