Solved

Java applet disappear on screen?

Posted on 1997-04-06
4
791 Views
Last Modified: 2013-11-23
I have a Java applet that takes <params> from an HTML document (which represent announcements with date, title, short description) and print them in a window that simple scrolls through them every ten seconds.  My problem is that if you go to another window that covers the applet, when you switch back, it's blank until it rotates to the next announcement.  And sometimes, it doesn't display anything (for seemingly no reason) and I have to shut down Communicator and restart it to see the applet in working mode.  I have a feeling it's based on my paint() function... I'm drawing the text, then drawing over it in the background color before drawing the new text.

Following is the code -- any ideas on how I could fix this?  Thanks...

import java.awt.*;
import java.applet.Applet;

public class stuff4 extends Applet
   {
      String Message[],DateTime[],Details[];
      int totalimages;
      boolean DrawOrg;
      Font drawit=new Font("TimesRoman", Font.ITALIC, 20);
      Font smallit=new Font("TimesRoman", Font.BOLD, 15);
      Font smallerit=new Font("TimesRoman", Font.PLAIN, 13);

      public void init()
      {
         setBackground (Color.white);
         processHTMLParms();
      }

      public void processHTMLParms()
      {
         String parm1;
         parm1=getParameter("totalimages");
         totalimages=(parm1==null?50: Integer.parseInt(parm1));
         Message = new String[totalimages];
         DateTime = new String[totalimages];
         Details = new String[totalimages];
         for (int i=0; i<totalimages; i++)
         {
            Message[i]=getParameter("m"+i);
            DateTime[i]=getParameter("dt"+i);
            Details[i]=getParameter("de"+i);
         }
      }

      public void paint (Graphics g)
      {
         for (int i=0; i<totalimages; i++)
         {
            g.setColor(Color.black);
            g.setFont(drawit);
            g.drawString(DateTime[i],1,20);
            g.setColor(new Color(50,75,75));
            g.setFont(smallit);
            g.drawString(Message[i],1,35);
            g.setColor(new Color(75,100,100));
            g.setFont(smallerit);
            g.drawString(Details[i],1,50);
            try
               {
                  Thread.sleep(10000);
               }
            catch (InterruptedException e)
               {
                  showStatus(e.toString());
               }
            g.setColor(Color.white);
            g.setFont(drawit);
            g.drawString(DateTime[i],1,20);
            g.setFont(smallit);
            g.drawString(Message[i],1,35);
            g.setFont(smallerit);
            g.drawString(Details[i],1,50);

         if (i==(totalimages-1)) {i=-1;};
         }
      }
   }
0
Comment
Question by:laze
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 

Author Comment

by:laze
ID: 1219436
Edited text of question
0
 
LVL 2

Accepted Solution

by:
majkl earned 50 total points
ID: 1219437
If you take a look in documentation there is method start()
which can you override. It is called every time you visit page containing applet and it is also called when user for example press back button .
Try to put code thah draws in this start() method and read more about in :
http://java.sun.com/nav/read/Tutorial/index.html
0
 

Author Comment

by:laze
ID: 1219438
How would I move the paint code to the start code?  I tried just replacing the paint (Graphics g) with start (Graphics g)... not quite sure (in case you couldn't tell, I'm starting with doing Java and have a little trouble with the GUI methods)...

0
 

Author Comment

by:laze
ID: 1219439
Haven't heard back from you -- and putting it in the start() method didn't work... here's a slightly altered version of the code:

import java.awt.*;
import java.applet.Applet;

public class stuff6 extends Applet
   {
      String Message[],DateTime[],Details[];
      int totalimages;
      int i;
      boolean DrawOrg;
      Font drawit=new Font("TimesRoman", Font.ITALIC, 20);
      Font smallit=new Font("TimesRoman", Font.BOLD, 15);
      Font smallerit=new Font("TimesRoman", Font.PLAIN, 13);

      public void init()
      {
         setBackground (Color.white);
         processHTMLParms();
         i=-1;
      }

      public void processHTMLParms()
      {
         String parm1;
         parm1=getParameter("totalimages");
         totalimages=(parm1==null?50: Integer.parseInt(parm1));
         Message = new String[totalimages];
         DateTime = new String[totalimages];
         Details = new String[totalimages];
         for (int i=0; i<totalimages; i++)
         {
            Message[i]=getParameter("m"+i);
            DateTime[i]=getParameter("dt"+i);
            Details[i]=getParameter("de"+i);
         }
      }

      public void paint (Graphics g)
      {
            i++;
            g.setColor(Color.black);
            g.setFont(drawit);
            g.drawString(DateTime[i],1,20);
            g.setColor(new Color(50,75,75));
            g.setFont(smallit);
            g.drawString(Message[i],1,35);
            g.setColor(new Color(75,100,100));
            g.setFont(smallerit);
            g.drawString(Details[i],1,50);
            try
               {
                  Thread.sleep(5000);
               }
            catch (InterruptedException e)
               {
                  showStatus(e.toString());
               }
         if (i==(totalimages-1)) {i=-1;};
         repaint();
      }
   }


This works a little better, but when the user scrolls down or comes back to the page or switches from antoher window, it takes times for the thread.sleep to finish before it redraws the next section...

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question