summaryrefslogtreecommitdiffstats
path: root/Master/Reference Architectures and Patterns/hjp5/examples/ImageResource.inc
blob: 56cfefdf49123c1c5592d0fed9b2aa6b6d339400 (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
/* ImageResource.inc */

import java.io.*;
import java.awt.*;

//...

public Image loadImageResource(String pkgname, String fname)
throws IOException
{
  Image ret = null;
  InputStream is = getResourceStream(pkgname, fname);
  if (is != null) {
    byte[] buffer = new byte[0];
    byte[] tmpbuf = new byte[1024];
    while (true) {
      int len = is.read(tmpbuf);
      if (len <= 0) {
        break;
      }
      byte[] newbuf = new byte[buffer.length + len];
      System.arraycopy(buffer, 0, newbuf, 0, buffer.length);
      System.arraycopy(tmpbuf, 0, newbuf, buffer.length, len);
      buffer = newbuf;
    }
    //create image
    ret = Toolkit.getDefaultToolkit().createImage(buffer);
    is.close();
  }
  return ret;
}

//...