Results 1 to 10 of 10
  1. #1
    Join Date
    Jun 2012
    Posts
    6
    Rep Power
    0

    Default dynamic size of applet

    I am trying to make some applets that dynamically scale to the size of the window, but I'm getting some weird behavior I don't understand. This behavior only arises when there are multiple applets whose sizes are both set dynamically in the same page, using the following code:

    Java Code:
    <script type="text/javascript">
    var size=window.screen.availWidth;
    document.write('<APPLET CODE="micro.class" height="'+Math.round(.3*size)+'" width="'+Math.round(.3*size)+'"></APPLET>');
    </script>

    I have seen similar code recommended on many web sites for dynamically setting the size of an applet. Is there any reason this code should not work for two applets?

    The problem that arises is that when one of the applets scrolls off screen, the one that stays on screen stops repainting. I know that the animation is still running, because the time variable continues to update: only the screen fails to repaint until forced to (e.g. by scrolling).

    I know that the applets work. Everything works fine as long as there is only one applet on the web page, or if even one of the two applets is given a predetermined size, using the following code:
    Java Code:
    <APPLET CODE="micro.class" height="200" width="200"></APPLET>
    It only fails to work if two applets both are sized dynamically, and one of them is scrolled off screen.

    I don't want to use the percent format to set the size, by the way, because I would like the applet to have a fixed aspect ratio, rather than depending on the aspect ratio of the screen.

    Thanks for your advice.

  2. #2
    Join Date
    Jun 2012
    Posts
    6
    Rep Power
    0

    Default Re: dynamic size of applet

    Can anybody help me verify that my problem occurs on other machines than my own? Is there some subtle flaw in my use of java or javascript, or is there a bug in the browser? I am using Firefox 13.0.1 and Java build build 1.7.0_05-b05 under Windows 7, screen resolution 1366x768. I attach herewith a small java applet ("micro.java"), and two html documents with two copies of the applet embedded. When one applet has a predetermined size and the other is dynamically set ("ThisWorks.html"), everything works fine. When both applets have their size set dynamically ("ThisDoesNotWork.html"), it does not work unless both applets are at least partially onscreen. (Also, close any other browser tabs that have applets running in them; when applets are running in another tab, that seems to prevent the problem from occurring.)

    micro.java
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class micro extends JApplet implements ActionListener {
    	
      	Timer timer;
    	int time;
      	int xSize,ySize;
      	Image bufferImg;
      	Graphics bufferG;
    
      	public void init() {
        		xSize=this.getWidth();
        		ySize=this.getHeight();
        		bufferImg = createImage(xSize,ySize);
        		bufferG = bufferImg.getGraphics();
    		setContentPane(new microPanel());
    		timer=new Timer(25,this);
    		time=0;
    		timer.start();
      	}
    
    	public void actionPerformed(ActionEvent e) {
    		time++;
    		repaint();
     	}
    
      	public void start() {
     		timer.restart();
      	}
    
      	public void stop() {
    		timer.stop();
      	}
    
     	class microPanel extends JPanel {
      		public void paintComponent(Graphics g) {
    			super.paintComponent(g);
    			bufferG.setColor(Color.white);
    	    		bufferG.fillRect(0,0,xSize,ySize);
    			bufferG.setColor(Color.red);
    			bufferG.drawLine(time%xSize,0,xSize-time%xSize,ySize-1);
       			g.drawImage(bufferImg,0,0,this);
      		}
    	}
    }
    ThisWorks.html
    Java Code:
    <!DOCTYPE html>
    <HTML>
    <HEAD>
    </HEAD>
    <BODY>
    
    <script type="text/javascript">
    var size=window.screen.availWidth;
    </script>
    <p>
    <script  type="text/javascript">
    document.write('<APPLET CODE="micro.class" height="'+Math.round(.3*size)+'" width="'+Math.round(.3*size)+'"></APPLET>');
    </script>
    
    <p>These lines provide enough space that one applet can be scrolled offscreen while the other is still onscreen.
    <p>In this html document, one applet is dynamically sized and the other is not.  This works.
    <p>more space<p>more space<p>more space<p>more space<p>more space<p>more space<p>more space<p>
    <APPLET CODE="micro.class" height="400" width="400"></APPLET>
    <p>These lines provide enough space that one applet can be scrolled offscreen while the other is still onscreen.
    <p>more space<p>more space<p>more space<p>more space<p>more space<p>more space<p>more space<p>
    
    </BODY>
    </HTML>
    ThisDoesNotWork.html
    Java Code:
    <!DOCTYPE html>
    <HTML>
    <HEAD>
    </HEAD>
    <BODY>
    
    <script type="text/javascript">
    var size=window.screen.availWidth;
    </script>
    <p>
    <script  type="text/javascript">
    document.write('<APPLET CODE="micro.class" height="'+Math.round(.3*size)+'" width="'+Math.round(.3*size)+'"></APPLET>');
    </script>
    
    <p>These lines provide enough space that one applet can be scrolled offscreen while the other is still onscreen.
    <p>In this html document, both applets are dynamically sized.
    <p>In my experience, if one applet is offscreen, the one that remains onscreen stops working.
    <p>Close any other tabs that have applets in them.
    <p>more space<p>more space<p>more space<p>more space<p>more space<p>
    <script  type="text/javascript">
    document.write('<APPLET CODE="micro.class" height="'+Math.round(.3*size)+'" width="'+Math.round(.3*size)+'"></APPLET>');
    </script>
    <p>These lines provide enough space that one applet can be scrolled offscreen while the other is still onscreen.
    <p>more space<p>more space<p>more space<p>more space<p>more space<p>more space<p>more space<p>
    
    </BODY>
    </HTML>
    Last edited by Mike Petrovich; 06-25-2012 at 02:29 PM.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: dynamic size of applet

    What does "does not work" mean? Can you be more specific? What can be seen to determine if the applets are working or not?
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    Join Date
    Jun 2012
    Posts
    6
    Rep Power
    0

    Default Re: dynamic size of applet

    The symptom is that under the very specific conditions I described, the screen fails to repaint, so the animation appears to stop moving.

    Nevertheless, I know that the timer continues to function in the background, because when I force the screen to repaint, for instance by scrolling, the screen appearance of the animation jumps to where it ought to be. Since this proves that the time variable is updating, I know the actionPerformed() method is being called by the timer interrupt, and therefore I know the repaint() method is getting called. But the screen is not getting repainted.

    Once again, I emphasize that the applet works fine under many conditions; the problem depends on how the HTML document it is embedded in calls the applet, which I do not understand. Specifically, the problem arises only if all three of the following conditions are met: (1) there are two applets in the page, (2) both of them are dynamically sized using Javascript, and (3) one of them is scrolled totally off screen. The applet works fine if any one of these three conditions does not obtain.

    It's as though when one applet is off screen, the JRE decides not to bother to parse the javascript determining its size. But if it doesn't know the size of the first applet, it can't figure out how to lay out the page, and so it can't paint the second applet either; thus, although the timer continues to function, paint() stops getting called. That's the only way I can imagine the behavior of something on screen being affected by something else scrolling off screen. Is something like that possible?

    Thanks again for any insight you might be able to provide.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default Re: dynamic size of applet

    What happens if you document.write() the second APPLET in the first example?
    No size calculations, just using the 400px size you're using currently.

    I suspect it is more to do with that than calculating the size...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Join Date
    Jun 2012
    Posts
    6
    Rep Power
    0

    Default Re: dynamic size of applet

    Interesting idea, I hadn't thought of that. However, upon doing the experiment, it makes no difference.

    If both applets are called by document.write(), but if even one of the document.write()'s gives a definite size in pixels -- and it doesn't matter which -- everything works fine. So it's not using document.write, it's only when both document.write's give dynamic sizes to the applets.

    That's even stranger, since it knocks out the one remaining theory I had.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default Re: dynamic size of applet

    What if the given size is the same as the calculated size?
    Put simply I find it hard to believe that the Math.round() call is the thing causing the problem.

    And can you show us that code...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    Join Date
    Jun 2012
    Posts
    6
    Rep Power
    0

    Default Re: dynamic size of applet

    Well, that is interesting... good idea, Tolls!

    I tried changing the sizes of both of the applets. It turns out it doesn't matter whether the applet is called in a document.write() or in plain text, or whether it's constant or the result of evaluating an expression. It's whenever the sizes are equal that I have a problem. If the sizes are not equal, no problem.

    Is that bizarre, or what!

    (This made me wonder if the problem is having two applets with the same name and same size... however, I tried swapping a different applet for the second one -- as will be the case in my final application -- and it made no difference. Two different applets, same size=problem. Two different applets, different size=no problem.)

    When you ask for the code, I am guessing you are asking for the HTML (if it's the java you want, it's the same as above). Here is the simplest case where the applets don't work. Change one of the dimensions, and it works!

    So ... I feel that this is progress, thanks, Tolls! I can get my application to work now, by giving the applets dimensions that differ from each other by one pixel. However, it makes me a little uncomfortable not knowing why (since I can then make the same mistake again without realizing it). Is it possible that when they're the same size, the two applets share the same Graphics or Image object, in order to save space, or something like that? Even so, I don't understand why the JRE would stop repainting the applet that's on screen, just because the other applet is off screen and doesn't need to be refreshed.....


    Java Code:
    <!DOCTYPE html>
    <HTML>
    <HEAD>
    </HEAD>
    <BODY>
    
    <p>
    <APPLET CODE="micro.class" height="409" width="409"></APPLET>
    
    
    <p>These lines provide enough space that one applet can be scrolled offscreen while the other is still onscreen.
    <p>In this html document, the two applets have the same size.  This does not work.
    <p>more space<p>more space<p>more space<p>more space<p>more space<p>more space<p>more space<p>
    <APPLET CODE="micro.class" height="409" width="409"></APPLET>
    
    <p>These lines provide enough space that one applet can be scrolled offscreen while the other is still onscreen.
    <p>more space<p>more space<p>more space<p>more space<p>more space<p>more space<p>more space<p>
    
    </BODY>
    </HTML>
    Last edited by Mike Petrovich; 06-25-2012 at 07:24 PM.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default Re: dynamic size of applet

    That is bizarre.
    Looks like it might be a very strange bug in the plugin?
    I'd be curious if the same thing happened in other browsers, but at least now you have a "fix".

    I was just asking for the HTML code to make sure we were both thinking the same thoughts as to what to put into the APPLET tags. It happens a lot here where we might make a suggestion and, just from the nature of the forum, that suggestion is misread and the change implemented is not quite what we were thinking. It just helps to ensure we're both on the same page, so to speak. Turns out that wasn't a problem here...:)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    Join Date
    Jun 2012
    Posts
    6
    Rep Power
    0

    Default Re: dynamic size of applet

    Thanks again for your suggestions!

Similar Threads

  1. Dynamic tree in java applet
    By deemu in forum Java Applets
    Replies: 1
    Last Post: 03-20-2012, 04:55 PM
  2. Swing -> Applet size problem
    By y2k in forum Java Applets
    Replies: 5
    Last Post: 03-07-2012, 08:33 PM
  3. Setting an applet size not reisizable
    By mike_ledis in forum Java Applets
    Replies: 0
    Last Post: 03-12-2011, 11:23 AM
  4. Java applet size problem
    By madnaelo in forum Java Applets
    Replies: 1
    Last Post: 12-06-2010, 01:39 AM
  5. setting applet's size
    By nikita in forum Java Applets
    Replies: 9
    Last Post: 10-01-2008, 04:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •