-
Jul 3rd, 2015, 02:08 PM
#1
Thread Starter
Don't Panic!
Java Applet -> from book ran out of memory
Hi
Very slowly trying to learn java, reading from a book and it gave me the following example for a simple scrolling banner.
Code:
import java.awt.*;
import java.applet.*;
/*
<applet code="AppletBanner.class" width=300 height=50>
</applet>
*/
public class AppletBanner extends Applet implements Runnable {
String msg = " A Simple Moving Banner.";
Thread t = null;
int state;
volatile boolean stopFlag;
//called first
public void init() {
// init
setBackground(Color.cyan);
setForeground(Color.red);
}
public void start() {
t = new Thread(this);
stopFlag = false;
t.start();
}
public void stop() {
stopFlag = true;
t = null;
}
public void run() {
for( ; ; ) {
try {
repaint();
Thread.sleep(250);
if(stopFlag)
break;
} catch (InterruptedException e) {}
}
}
public void paint(Graphics g) {
char ch;
ch= msg.charAt(0);
msg = msg.substring(1, msg.length());
msg += msg + ch;
g.drawString(msg, 50, 30);
}
}
Used in :
Code:
<html>
<p> This file launches the 'A' applet: A.class! </p>
<!-- <applet code="AppletSkel.class" height=200 width=320> -->
<applet code="AppletBanner.class" height=400 width=400>
No Java?!
</applet>
</html>
When I run through appletviewer, it initially opens, although sizes don't correspond... But the applet viewer allows me to stretch the window. Which I did. At this point it all went wrong. The text was repainted more than once and well over the limits given.
So my question:
If I ran this on a web server, would it work fine as the applet is sized on html?
---> or like the appletviewer would it run for two seconds then crash with a long list of errors including no memory...?
Feeling like a fly on the inside of a closed window (Thunk!)
If I post a lot, it is because I am bored at work! ;D Or stuck...
* Anything I post can be only my opinion. Advice etc is up to you to persue...
-
Sep 10th, 2015, 04:25 PM
#2
Fanatic Member
Re: Java Applet -> from book ran out of memory
Honestly, I haven't tried running it, but I believe if you ran this inside a browser you may exhibit the same issues - I'm not entirely sure, I wrote an applet many years ago as a university thing and I can barely remember it.
However, you have an ugly bug in your code that is going to cause you problems. You said you saw it when you resized the applet, which means Java would call the paint(Graphics g) method. So lets have a look at it - see if you can find the problem (think about running this code once, then running it a million times, and then see if you can spot the issue):
Code:
public void paint(Graphics g) {
char ch;
ch= msg.charAt(0);
msg = msg.substring(1, msg.length());
msg += msg + ch;
g.drawString(msg, 50, 30);
}
So lets go through this code line by line, again thinking about the perspective of this method being called multiple times:
You first declare a char variable called "ch". Nothing really insane here - every time it is called this variable is initialised.
You then do "ch= msg.charAt(0)" - so you are essentially grabbing the very first character. In your case, this character would be " " (there is a space at the start of the String). Again, nothing here monstrous, and this would grab the first letter from that same String every time you run it. Harmless
You reassign msg so that it is the same string as before, but with the first character chopped off all the way to the end. Again, nothing bad - although as a tip, you could save yourself a bit of code and rewrite this as msg = msg.substring(1) - there is a substring method that allows you to set the position of the starting point of the string you want to chop, and it will start there and follow it to the end of the string. Just a tip though, nothing alarming.
Now we get onto the line that is the issue.
This takes that last character, slaps it onto the end of your chopped msg String, and then adds it to the msg variable. So lets do multiple runs of this line:
Original String:
" A Simple Moving Banner."
1st run:
"A Simple Moving Banner.A Simple Moving Banner. "
2nd run:
" Simple Moving Banner.A Simple Moving Banner. Simple Moving Banner.A Simple Moving Banner. A"
3rd run:
"Simple Moving Banner.A Simple Moving Banner. Simple Moving Banner.A Simple Moving Banner. A"Simple Moving Banner.A Simple Moving Banner. Simple Moving Banner.A Simple Moving Banner. A "
Can you see the issue? This thing grows every time the paint() method is called. Your bug is that you appended your new string - the "msg + ch".
How would you fix this problem? How would you get from the above to the results below?
Original String:
" A Simple Moving Banner."
1st run:
"A Simple Moving Banner. "
2nd run:
" Simple Moving Banner. A"
3rd run:
"Simple Moving Banner. A "
-
Oct 9th, 2015, 09:32 AM
#3
Thread Starter
Don't Panic!
Re: Java Applet -> from book ran out of memory
So its the
which should be
Ah got it... sigh.... Might explain the lack of memory...
Thank you for pointing me in the right direction...
Feeling like a fly on the inside of a closed window (Thunk!)
If I post a lot, it is because I am bored at work! ;D Or stuck...
* Anything I post can be only my opinion. Advice etc is up to you to persue...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|