summaryrefslogtreecommitdiff
path: root/java/gjt/image/ImageDissolver.java
blob: 5d0e6a2daf6ae885e4b405d8f456366cb38de17f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package gjt.image;

import java.awt.*;
import java.awt.image.*;
import gjt.Util;

/**
 * Given an image, an ImageDissolver produces an array of
 * images of varying opacity that are used in the fadeIn()
 * and fadeOut() methods for fading the image in and out
 * respectively.<p>
 *
 * As a convenience, ImageDissolver has a static method:
 * Image[] createImages() that creates the array of images
 * mentioned above, in case clients would like to create their
 * own array of images instead of using an ImageDissolver
 * directly.<p>
 *
 * @version 1.0, Apr 1 1996
 * @author  David Geary
 * @see     ThreeDBorder
 * @see     ImageButtonController
 * @see     SpringyImageButtonController
 * @see     StickyImageButtonController
 * @see     BleachImageFilter
 * @see     gjt.test.ImageButtonTest
 */
public class ImageDissolver {
    private static int _defaultNumImages = 10, 
                       _defaultPause = 50;
    Component comp;
    int       numImages, pauseInterval;
    Image     image, offscreen;
    Image[]   dissolvedImages;

    static public Image[] createImages(Image image, 
                                       int numImages,
                                       Component component) {
        Image        images[] = new Image[numImages];
        MediaTracker tracker  = new MediaTracker(component);

        DissolveFilter      filter;
        FilteredImageSource fis;

        for(int i=0; i < numImages; ++i) {
            filter = new DissolveFilter((255/(numImages-1))*i);
            fis    = new FilteredImageSource(image.getSource(), 
                                             filter);

            images[i] = component.createImage(fis);
            tracker.addImage(images[i], i);
        }
        try { tracker.waitForAll(); }
        catch(InterruptedException e) { }

        return images;
    }

    public ImageDissolver(Component comp, Image image) {
        this(comp, image, _defaultNumImages, _defaultPause);
    }
    public ImageDissolver(Component comp, Image im, 
                          int numImages, int pause) {
        this.image      = im;
        this.comp       = comp;
        this.numImages  = numImages;
        dissolvedImages = new Image[numImages];
        pauseInterval   = pause;

        Util.waitForImage(comp, im);
        dissolvedImages = createImages(image, numImages, comp);
    }
    public void fadeIn(int x, int y) {
        if(offscreen == null) 
            offscreen = comp.createImage(image.getWidth(comp), 
                                         image.getHeight(comp));

        Graphics offg  = offscreen.getGraphics();
        Graphics compg = comp.getGraphics();

        if(offg != null && compg != null) {
            clearComponent(compg, x, y);
            for(int i=0; i < numImages; ++i) {
                blitImage(compg, offg, x, y, i);
                pause    ();
            }
            blitOpaqueImage(compg, offg, x, y);
        }
    }
    public void fadeOut(int x, int y) {
        if(offscreen == null) 
            offscreen = comp.createImage(image.getWidth(comp), 
                                         image.getHeight(comp));

        Graphics offg  = offscreen.getGraphics();
        Graphics compg = comp.getGraphics();

        if(offg != null && compg != null) {
            blitOpaqueImage(compg, offg, x, y);
            for(int i=numImages-1; i >= 0; --i) {
                clearOffscreen();
                blitImage     (compg, offg, x, y, i);
                pause         ();
            }
        }
    }
    private void blitImage(Graphics compg, Graphics offg, 
                           int x, int y, int index) {
        offg.drawImage (dissolvedImages[index], 0, 0, comp);
        compg.drawImage(offscreen,              x, y, comp);
    }
    private void blitOpaqueImage(Graphics compg, Graphics offg, 
                                 int x, int y) {
        offg.drawImage(image, 0, 0, comp);
        compg.drawImage(offscreen, x, y, comp);
    }
    private void clearComponent(Graphics compg, int x, int y) {
        clearOffscreen();
        compg.drawImage(offscreen, x, y, comp);
    }
    private void clearOffscreen() {
        Graphics offg = offscreen.getGraphics();

        offg.setColor(comp.getBackground());
        offg.fillRect(0, 0, 
                image.getWidth(comp), image.getHeight(comp));
    }
    private void pause() {
        try { Thread.currentThread().sleep(pauseInterval); }
        catch(InterruptedException e) { }       
    }
}