New to Applets, how do I do this?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: New to Applets, how do I do this?

  1. #1
    Join Date
    Sep 2004
    Posts
    37

    New to Applets, how do I do this?

    O.K. I have this Applet task. I am fairly new to using Applets and need a bit of a kickstart.

    I have to create an applet that will display a message in large font and a black color, then slowly fade the color away. The program needs a delay loop to slow down the action.

    I have started out with this but are stuck for the main body:

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

    public class Fade extends Applet
    {
    Font header= new Font("SanSerif",Font.BOLD,36);
    String text;
    String temp;
    int delayLimit;
    };

    Is delayLimit needed. Any help with how to do this would be great, am finding this a bit confusing. Thanks everyone in advance.
    Steve

  2. #2
    Join Date
    Sep 2004
    Posts
    37
    O.K heres what I have so far:

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

    public class Fade extends Applet
    {
    Font header= new Font("SanSerif",Font.BOLD,36);
    String text;
    String temp;
    int delayLimit;

    public void init()
    {
    text =getParameter("message");
    if (text==null)
    text="nothing to say";
    temp=getParameter("Limit");
    if(temp==null)
    delayLimit = 10000;
    else
    delayLimit = Integer.parseInt(temp);
    }

    public void paint(Graphics g)
    {
    g.setFont(header);

    for(int delay=0;delay<delayLimit;delay++);
    g.setColor(new Color(0,0,0));
    g.drawString(text,100,200);
    g.setColor(new Color(255,255,255));
    g.fillRect(50,80,300,30);
    }
    }

    Now this compiles fine, just that my "message" is delayed to appear rather than appearing and then slowly fading away! I know it is problay simple but I have swapped around all my setColors etc and can't seem to get it to work.
    Also could someone please tell me what the 4 ints in the g.fillRect are for ( I know they are probaly dimensions but I am unsure of what fillRect does)?
    Thanks in advance.
    Steve

  3. #3
    Join Date
    Nov 2004
    Posts
    13
    Well, I have an answer to the second question at least.
    fillRect fills a rectangle (surprise!) with the set Color (in that specific case, white). The parameters go like this:
    g.fillRect(x, y, width, height)

    fillRect starts at the upper left corner with coordinates (x, y), draws a box that is width wide and height tall, and fills it in with the color.

    Ex:
    g.fillRect(50, 50, 25, 25);

    This will fill a rectangle that starts at (50, 50), with corner points (75, 50), (75, 75), and (50, 75).

    Have you thought about using Timers rather than your delayLimit?

  4. #4
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    First of all, the paint() method is a flash.... and whatever that method does to the grapics object, it won't display properly until the paint method has finished. Remember that if you paint() while the screen is being refreshed it is the same as exhibiting a painting wile the artist is still at work:

    Gallery Owner: "Gimme that canvas, I'm gonna put it on display !!!"

    Artist: "But I haven't finished yet, nooo !!!".

    Gallery Owner "Shut up and gimme that canvas !!"

    (SPLATTER !! SPLASH!!)

    So what you really want to do is paint on an offscreen image and paint() that whole image every time you are finished covering it. To do this you must implement the Runnable interface and create an offscreen image.

    And u do it like this:
    Code:
    import java.awt.*;
    import java.applet.*;
    import java.awt.image.*;
    import java.util.*;
    
    public class Fade extends Applet implements Runnable {
      public final static int MEMIMG_W=300;
      public final static int MEMIMG_H=200;
      Font header= new Font("SanSerif",Font.BOLD,36);
      String text;
      String temp;
      int delayLimit;
      Thread t=null;
      BufferedImage memImage=null;
      Graphics memG=null;
      int r=255, g=255, b=255;
      int fadeStep=-1;
      private Hashtable colorHt=new Hashtable();
      public void init() {
        text =getParameter("message");
        if (text==null)
        text="nothing to say";
        temp=getParameter("Limit");
        if(temp==null) {
          delayLimit = 10000;
        } else {
          delayLimit = Integer.parseInt(temp);
        }
        // make a memory image and get the graphics context for it
        memImage = new BufferedImage(MEMIMG_W,MEMIMG_H, BufferedImage.TYPE_INT_RGB);
        memG = memImage.createGraphics();
      }
      public void start() {
        t=new Thread(this);
        t.start();
      }
      public void stop() { // invoked when client leaves the applet page
        t.interrupt();
      }
      public void run () {
        while (true) {
          try {
            t.sleep(delayLimit);
            this.repaint();
          } catch (InterruptedException ex) {
            return; // finish the thread
          }
    
        }
      }
      /**
       * The hashtable is used to ensure that u don't create a Color
       * u already have created.
       * @return
       */
      private Color getAFadedColor() {
        r+=fadeStep;
        if (r == 0 || r == 255) {
          fadeStep *= -1;
          r+=fadeStep;
        }
        g+=fadeStep;
        b+=fadeStep;
        Color c=(Color)colorHt.get(r+","+g+","+b);
        if (c==null) {
          c=new Color(r,g,b);
          colorHt.put(r+","+g+","+b, c);
        }
        return c;
      }
      public void update(Graphics g) {
        memG.setFont(header); // draw on the offscreen image
        Color c=getAFadedColor();
        memG.setColor(c);
        memG.drawString(text,100,200);
      }
      public void paint(Graphics g) {
        update(g);
        g.drawImage(memImage,0,0,MEMIMG_W, MEMIMG_H,this);
      }
    }
    I haven't run it yet though...
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center