comp.lang.ada
 help / color / mirror / Atom feed
* Getting started with AppletMagic
@ 1996-05-01  0:00 Simon Johnston
  0 siblings, 0 replies; only message in thread
From: Simon Johnston @ 1996-05-01  0:00 UTC (permalink / raw)



I am interested in using the AppletMagic compiler and have had a look through
the demos etc. but now need to get going on something real. I have taken a
simple but complete applet, a ticker text, and removed some of its more obscure
features to get a small simple example. I now need to start on the Ada version.

I started by running java2ada ticker.class which provided me a useful package
spec, but how do I return the array of from getParameterInfo? and how come
java2ada creates subprogram specs for init_ticker and new_ticker? I know there
are example applets in the distribution but I still don't see answers to all
the questions.

Can we have an Ada Applet page somewhere, either Intermetrics, HBAP or the
SigAda web-ada homepage where people can either add or link to applets written
in Ada (preferably in source).

import java.awt.*;
import java.lang.*;
import java.net.*;
import java.util.*;

public class ticker extends java.applet.Applet implements Runnable {
  private String message;
  private Font messageF;
  private int messageX;
  private int messageY;
  private int messageW = 0;
  private int speed;
  private Thread t = null;
  private boolean active = false;
  private Color txtCo;
  private Color bgCo;
  private Dimension lastS = new Dimension(1,1);
  private Image im = null;
  private Graphics gr = null;

  public void init () {
    message = getStringParameter("msg", "No Message?");

    speed = getIntegerParameter("speed", 10);

    txtCo = getColorParameter("txtco", Color.black);

    bgCo = getColorParameter("bgco", getBackground());
  }

  /** Parameter Info. */
  public String[][] getParameterInfo() {
    String[][] info = {
      {"msg",    "String",  "Message to display"},
      {"speed",  "int",     "animation speed in pixels (10)"},
      {"txtco",  "int[3]",  "RGB-Color of Message (black)"},
      {"bgco",   "int[3]",  "RGB-Color of background (getBackground)"},
    };
    return info;
  }

  /** Applet Info. */
  public String getAppletInfo() {
    return "SimpleTicker.java - skj@acm.org";
  }

  /** Show the stuff, call update */
  public void paint(Graphics g) {
    update(g);
  }

  /** Show the stuff */
  public synchronized void update(Graphics g) {
    // Recalc params, if something has changed
    if ((size().height != lastS.height) || (size().width != lastS.width)) {
      createParams();
    }

    // fill area with bgcolor
    gr.setColor(bgCo);
    gr.fillRect(0,0,lastS.width,lastS.height);

    // draw the text
    gr.setColor(txtCo);
    gr.setFont(messageF);
    gr.drawString(message, messageX, messageY);

    // finally show all together on the screen
    g.drawImage(im,0,0,this);
  }

  /** calc next position of message and call repaint */
  public synchronized void nextPos() {
      // decrement position
      messageX -= speed;
      // and stay in the bounds
      if ((messageX + messageW) < 0) {
            messageX = lastS.width;
      }
      // indirectly call update
      repaint();
  }

  /** Run the loop. This method is called by class Thread. */
  public void run() {
    // others might be more important
    Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
    while (active) {
      // do the job
      nextPos();
      // pause
      try {Thread.sleep(100);}
      catch (InterruptedException e){ }
    }
  }

  /** Start the applet by forking an animation thread. */
  public void start() {
    if (!active) {
      t = new Thread(this);
      active = true;
      t.start();
    }
  }

  /** Stop the applet. The thread will exit because run() exits. */
  public void stop() {
    active = false;
    t = null; // give GC a chance.
  }

  /**
   * retrieve a parameter from the .html file, with
   * a default value if the parameter does not exist.
   */
  private String getStringParameter(String ParamName, String aDefault) {
    String ParamString = getParameter(ParamName);

    if (ParamString == null) {
      return aDefault;
    }

    return ParamString;
  }

  /**
   * retrieve a parameter from the .html file, with
   * a default value if the parameter does not exist.
   */
  private int getIntegerParameter(String ParamName, int aDefault) {
    String ParamString = getParameter(ParamName);
    int anInt;

    if (ParamString == null) {
      return aDefault;
    }

    try {
      anInt = Integer.valueOf(ParamString).intValue();
    }
    catch (Exception e) {
      return aDefault;
    }

    return anInt;
  }

  /**
   * Convert a ","-delimited String with RGB-Values to Color
   * Uses aDefault, if no or not enough RGB-Values
   */
  private Color getColorParameter(String ParamName, Color aDefault) {
    String ParamString = getParameter(ParamName);

    if (ParamString == null) {
      return aDefault;
    }

    int r, g, b;
    StringTokenizer st = new StringTokenizer(ParamString, ",");

    try {
      r = Integer.valueOf(st.nextToken()).intValue();
      g = Integer.valueOf(st.nextToken()).intValue();
      b = Integer.valueOf(st.nextToken()).intValue();
    }
    catch (Exception e) {
      return aDefault;
    }

    return new Color(r,g,b);
  }

  /**
   * Create the image Parameters.
   * Called, if just created or size has changed
   */
  private void createParams() {
    // Init some constants
    int w = size().width;
    int h = size().height;
    lastS.width = w;
    lastS.height = h;

    // dispose old buffer etc.
    if (gr != null) { gr.finalize(); }
    if (im != null) { im = null; }
    // Calc the font and positions. Message must fit applets area.
    int refH = 14;
    Font tf = new Font("TimesRoman", Font.BOLD, refH);
    setFont(tf);
    FontMetrics tfm = getFontMetrics(tf);
    int fh = tfm.getHeight();
    fh = refH*(h-10)/fh;
    messageF = new Font("TimesRoman", Font.BOLD, fh);
    FontMetrics fm = getFontMetrics(messageF);
    fh = fm.getHeight();
    messageX = w;
    messageY = ((h-fh) >> 1)+fm.getAscent();
    messageW = fm.stringWidth(message);
    im = createImage(w, h);
    gr = im.getGraphics();
  }
}


with StandardDisclaimer; use StandardDisclaimer;
package Sig is
--,-------------------------------------------------------------------------.
--|Simon K. Johnston - Development Engineer (C++/Ada95) |ICL Retail Systems |
--|-----------------------------------------------------|3/4 Willoughby Road|
--|Unix Mail: skj@acm.org                               |Bracknell          |
--|Telephone: +44 (0)1344 476320 Fax: +44 (0)1344 476302|Berkshire          |
--|Internal : 7261 6320   OP Mail: S.K.Johnston@BRA0801 |RG12 8TJ           |
--|WWW URL  : http://www.acm.org/~skj/                  |United Kingdom     |
--`-------------------------------------------------------------------------'
end Sig;




^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~1996-05-01  0:00 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-05-01  0:00 Getting started with AppletMagic Simon Johnston

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox