-
Applet run twice
Hi,
my applet run twice. (Kam na obed v Žiline)
In init method I just set background.
public void init() {
Color c = new Color(150, 250, 150); //#EDEFF4
//#3B5998
setBackground(Color.WHITE);
}
// applet rendering method
public void start(){
}
All logic is nested in paint method.
public void paint(Graphics g){
try {
g.setColor(Color.BLACK);
g.drawRect(9, 9, 101, 21);
String menu = menuBuilder(g, true);
g.drawString(menu, 10, 15);
} catch (Exception e) {
g.setColor(Color.RED);
g.drawString(e.getMessage(), 30, 15);
}
}
menuBuilder method collects String through web to display and while collecting draw progress bar.
I think about using start() method to nest menuBuilder method and in paint method just draw collected string but I can not pass Graphics object to start() method to draw progress bar.
After some tests I found that calling this method cause duplicate drawing (running)
public static byte[] getBytesFromUrl(URL iurl) throws IOException, InterruptedException {
ByteArrayOutputStream tmpOut = new ByteArrayOutputStream();
HttpURLConnection urlConn = (HttpURLConnection) iurl.openConnection();
urlConn.setDefaultUseCaches(false);
urlConn.setDoInput(true);
urlConn.setDoOutput(true);
urlConn.setUseCaches(false);
urlConn.setRequestMethod("GET");
urlConn.setRequestProperty("Pragma", "no-cache");
urlConn.setRequestProperty("Cache-Control", "no-cache");
/* when I comment out this part applet draws only once
InputStream openStream = new BufferedInputStream(urlConn.getInputStream());
byte[] buf = new byte[16384];
int len;
while (true) {
len = openStream.read(buf);
if (len == -1) {
break;
}
tmpOut.write(buf, 0, len);
}
tmpOut.close();
ByteBuffer bb = ByteBuffer.wrap(tmpOut.toByteArray(), 0, tmpOut.size());
*/
byte[] binaryData = tmpOut.toByteArray();
/* when I comment out this part
openStream.close();
*/
return binaryData;
}
How to avoid to draw applet twice?