1 | package org.nzdl.gsdl.GsdlCollageApplet;
|
---|
2 |
|
---|
3 | import java.awt.*;
|
---|
4 | import java.io.*;
|
---|
5 | import java.net.*;
|
---|
6 | import java.util.*;
|
---|
7 | import java.awt.image.*;
|
---|
8 | import java.awt.geom.*;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * @author Katrina Edgar
|
---|
12 | * @author David Bainbridge
|
---|
13 | *
|
---|
14 | * Stores images that are downloaded but have not yet been displayed
|
---|
15 | * in the applet. Provides methods to add to and remove from this list
|
---|
16 | * and check it's size. It uses an inner class to store the data for these
|
---|
17 | * images, comprising of three parts: the image itself, it's source url
|
---|
18 | * and the name of the image */
|
---|
19 | public class DownloadImages {
|
---|
20 |
|
---|
21 | /** Data structure to store an image that has been downloaded and is
|
---|
22 | * waiting to be used. */
|
---|
23 | public class ImageUrlTriple {
|
---|
24 |
|
---|
25 | Image image_;
|
---|
26 | String urlString_;
|
---|
27 | String name_;
|
---|
28 | URL url_;
|
---|
29 |
|
---|
30 | /** Constructs a downloaded image structure from the given parameters
|
---|
31 | * @param image the graphical image
|
---|
32 | * @param url the source url for this image
|
---|
33 | * @param name the name of the image file */
|
---|
34 | public ImageUrlTriple (Image image, URL url, String urlString, String name)
|
---|
35 | {
|
---|
36 | image_ = image;
|
---|
37 | url_ = url;
|
---|
38 | urlString_ = urlString;
|
---|
39 | name_ = name;
|
---|
40 | }
|
---|
41 |
|
---|
42 | Image image() {
|
---|
43 | return image_;
|
---|
44 | }
|
---|
45 |
|
---|
46 | String urlString() {
|
---|
47 | return urlString_;
|
---|
48 | }
|
---|
49 |
|
---|
50 | String name() {
|
---|
51 | return name_;
|
---|
52 | }
|
---|
53 |
|
---|
54 | URL url(){
|
---|
55 | return url_;
|
---|
56 | }
|
---|
57 |
|
---|
58 | }
|
---|
59 |
|
---|
60 |
|
---|
61 | ImageUrlTriple downloaded_image;
|
---|
62 | CollageImage collage_image;
|
---|
63 |
|
---|
64 | /** Refers to applet */
|
---|
65 | GsdlCollageApplet app_ = null;
|
---|
66 | boolean isJava2_;
|
---|
67 | int num_of_downloads = 1;
|
---|
68 | boolean stopDownload;
|
---|
69 |
|
---|
70 | MediaTracker tracker_;
|
---|
71 |
|
---|
72 | /** Initialises an empty vector to store downloaded images in and sets
|
---|
73 | * the verbosity for this class */
|
---|
74 | public DownloadImages(GsdlCollageApplet app, int verbosity, boolean isJava2)
|
---|
75 | {
|
---|
76 |
|
---|
77 | app_=app;
|
---|
78 | isJava2_= isJava2;
|
---|
79 | }
|
---|
80 |
|
---|
81 | /** Push a new image into the vector of downloaded images
|
---|
82 | * @param image the graphical image to add
|
---|
83 | * @param url the source url of the image
|
---|
84 | * @param name the name of the image to add */
|
---|
85 | public synchronized int downloadImage(MediaTracker tracker, URL url, String urlString, String name)
|
---|
86 | {
|
---|
87 |
|
---|
88 | tracker_= tracker;
|
---|
89 |
|
---|
90 | while (downloaded_image != null ){
|
---|
91 | System.out.println(Thread.currentThread().getName() + " wait to download...");
|
---|
92 | try{
|
---|
93 | wait();
|
---|
94 | }
|
---|
95 | catch(InterruptedException e){
|
---|
96 | return 0;
|
---|
97 | }
|
---|
98 |
|
---|
99 | }
|
---|
100 |
|
---|
101 | Image image = Toolkit.getDefaultToolkit().getImage(url);
|
---|
102 | tracker_.addImage(image,num_of_downloads);
|
---|
103 |
|
---|
104 | try{
|
---|
105 | tracker_.waitForID(num_of_downloads);
|
---|
106 | }
|
---|
107 | catch (InterruptedException e){
|
---|
108 | notify();
|
---|
109 | return num_of_downloads;
|
---|
110 | }
|
---|
111 |
|
---|
112 |
|
---|
113 | downloaded_image = new ImageUrlTriple(image,url,urlString,name);
|
---|
114 |
|
---|
115 | notify();
|
---|
116 |
|
---|
117 | return num_of_downloads ;
|
---|
118 | }
|
---|
119 |
|
---|
120 |
|
---|
121 | /** Remove the image from the specified position in the array of downloaded images
|
---|
122 | * @return an ImageUrlTriple containing the image, url and image name */
|
---|
123 | public synchronized CollageImage getCollageImage()
|
---|
124 | {
|
---|
125 | if (stopDownload) return null;
|
---|
126 |
|
---|
127 | while (downloaded_image == null){
|
---|
128 | System.out.println(Thread.currentThread().getName() + " wait to get Collage image...");
|
---|
129 | try{
|
---|
130 | wait();
|
---|
131 | }
|
---|
132 | catch(InterruptedException e){
|
---|
133 | //e.printStackTrace();
|
---|
134 | downloaded_image = null;
|
---|
135 | notify();
|
---|
136 | return null;
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | if (tracker_.statusID(num_of_downloads, false) == MediaTracker.COMPLETE){
|
---|
141 | collage_image = new CollageImage(app_,isJava2_,downloaded_image);
|
---|
142 | downloaded_image = null;
|
---|
143 | num_of_downloads++;
|
---|
144 | notify();
|
---|
145 | return collage_image;
|
---|
146 | }
|
---|
147 | else{
|
---|
148 | if ((tracker_.statusAll(false) & MediaTracker.ERRORED) != 0){
|
---|
149 | //System.out.println(downloaded_image.name_);
|
---|
150 | downloaded_image = null;
|
---|
151 | num_of_downloads++;
|
---|
152 | notify();
|
---|
153 | return null;
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | return null;
|
---|
158 |
|
---|
159 | }
|
---|
160 |
|
---|
161 | public void stopDownload(){
|
---|
162 | stopDownload = true;
|
---|
163 | }
|
---|
164 |
|
---|
165 |
|
---|
166 | }
|
---|