Appletviewer and biss.awt

Aleksander Slominski aztoruns at mat.uni.torun.pl
Wed Apr 23 11:20:48 PDT 1997


Could someone help me find out where it happens and why?
I am resting under Solaris 2.5 / SPARCstation
I was quite enthusiastic about kaffe but now I am more sceptic.
Tried my applet but it seems I have some problem...

> kaffe sun.applet.AppletViewer example1.html       
java.lang.NullPointerException
        at biss/awt/kernel/Toolkit.<init>(line unknown, pc 20e3a8)
        at biss/awt/kernel/Toolkit.<init>(line unknown, pc 1fec9c)
        at java/awt/Toolkit.getDefaultToolkit(190)
        at java/awt/Window.getToolkit(155)
        at java/awt/Frame.addNotify(82)
        at java/awt/Window.pack(89)
        at sun/applet/AppletViewer.<init>(175)
        at sun/applet/StdAppletViewerFactory.createAppletViewer(77)
        at sun/applet/AppletViewer.parse(767)
        at sun/applet/AppletViewer.parse(752)
        at sun/applet/AppletViewer.main(891)

I included ENVIRMENT.csh and example1.html but it seems that
appletviewer has a problem.

When I started standalone version from Listing1.java
 kaffe Listing1
it worked just fine!

Alek
-- 
Aleksander Slominski http://www.mat.uni.torun.pl/~aztoruns
ENTIA NON SUNT MULTIPLICANDA PRAECETER NECESSITAEM -William z Occam
-------------- next part --------------
/* $Id: ClickMe.java,v 1.4 1997/04/05 09:28:10 aztoruns Exp $ */
/* (C) AzToruniS'96 */
import java.awt.*;

// TODO: gallery with desriptions: files gallery.html gallery01.html
// TODO: moving "click me" message

/**
 * This applet shows on screen Lissajoux curves 
 * and let user choose shape using mouse.
 *
 * For further applet desctiption look on 
 * http://www.mat.uni.torun.pl/~aztoruns/ClickMe/
 *
 * Feel free to use and enjoy it!
 *
 * Written by AzTorunis'97
 */

public class ClickMe extends java.applet.Applet implements Runnable {
  public final static int INIT = 1;
  public final static int CHOOSING = 2;
  public final static int DRAWING = 3;

  //double a=0.5, b=0.5 * Math.PI;
  // Ciekawe parametry
  double a=1.50584, b=4.63385;    //a rotating fish
  //double a=5.01187, b=1.17286;  //a standing wave 
  //double a=1.05372, b=2.86322;  //circle up-down rotating
  //double  a=6.24388, b=5.24924; //designs changing very fast
  // double a=6.24388, b=3.18136; // fast shrinking and expanding
  //double a=4.33383, b=5.021;  // standing wave
  //double a=2.96422, b=3.93193; // nice standing wave
  //double a=8.3435, b=2.41053; //very nice going up and down
  //double a=9.01688, b=4.74203; //fantastic wave
  double c = 1.0, d = 0.0; 
  //  Color bkColor = Color.gray; //yellow;
  Color bkColor = Color.white; //yellow;
  //  Color drawColor = Color.blue;
  //  Color drawColor = Color.cyan;
  Color drawColor = Color.black;
  double delta;
  Font font = new Font("Helvetica", Font.PLAIN, 8);
  Font fontTitle = new Font("Helvetica", Font.BOLD, 16);
  FontMetrics fm;
  private int head;
  private int length;
  int height;
  private Thread kicker;
  private Rectangle lines[];
  private int mode = INIT;
  private int offHeight;  
  private Image offImage;  
  private int offWidth;  
  private int oldX, oldY;
  //  private Color selectColor= Color.green;
  private Color selectColor = Color.black;
  // TODO test for small sizes (2, 3...)
  private int size=150;  //30
  private boolean stopped = false;
  private int tail;
  double time;
  int width;

  private synchronized void addLine(int x1, int y1, int x2, int y2) {
    //Graphics g = getGraphics();
    //    g.setColor(drawColor); 
    //g.setXORMode(bkColor);
    ///debug("addLine:"+line);
    ///debug("addRect: distance="+distance());
    if(full()) {
      Rectangle ol = pop();
      //debug(ol.toString());
      //drawLine(g, ol);
    }
    //debug(line.toString());
    push(x1, y1, x2, y2);
    //if(empty()) 
    //push(0, 0, width, height); //HACK
    //drawLine(g, top());
    time += delta;
  }
  
  private synchronized void calcShape() {
    double x,y;
    int x1,y1,x2,y2;
    mode = DRAWING;
    //clear();
    for(int i = 0; i < size/(12/4); ++i) {
      // TODO: c * + d was compiled. Why?
      x = Math.sin(c * time + d) / 2 + 0.5;
      y = Math.sin(a * time + b) / 2 + 0.5;
      x1 = (int)(x * width);
      y1 = (int)(y * height);
      x = Math.sin(c * (time + delta) + d) / 2 + 0.5;
      y = Math.sin(a * (time + delta) + b) / 2 + 0.5;
      x2 = (int)(x * width);
      y2 = (int)(y * height);
      addLine(x1, y1, x2, y2);
    }
  }

  private synchronized void clear() {
    head=tail=0;
    length=0;
  }

  private synchronized void clearAll() {
    clear();
    time = 0;
    paint(getGraphics());
  }

  private void debug(String s) {
    System.err.println("DEBUG: "+s);
  }

  Color decodeColor(String s) {
    int val = 0;
    try {
      if (s.startsWith("0x")) {
	val = Integer.parseInt(s.substring(2), 16);
      } else if (s.startsWith("#")) {
	val = Integer.parseInt(s.substring(1), 16);
      } else if (s.startsWith("0") && s.length() > 1) {
	val = Integer.parseInt(s.substring(1), 8);
      } else {
	val = Integer.parseInt(s, 10);
      }
      return new Color(val);
    } catch (NumberFormatException e) {
      return null;
    }
  }

  public void destroy() {
    if(kicker != null && kicker.isAlive()) {
      kicker.stop();
      //debug("stop:"+kicker);
    }
    kicker = null;
  }

  private synchronized int distance() {
    return length;
  }

  private void drawLine(Graphics g, Rectangle line) {
    //debug("drawLine:"+line);
    g.drawLine(line.x + 1, line.y + 1, line.x + 1 + line.width, line.y + 1 + line.height);
    //we draw point on the end of line for XOR to connect with next line
    g.drawLine(line.x + 1 + line.width, line.y + 1 + line.height, line.x + 1 + line.width, line.y + 1 + line.height);
  }

  private synchronized boolean empty() {
    return length==0;
  }

  private synchronized boolean full() {
    return length==size;
  }

  private Color getColor(String key, Color def) {
    Color color = null;
    String param = getParameter(key);
    if (param != null) {
      color = decodeColor(param);
    }
    if(color == null)
      color = def;
    debug("getColor() key="+key+" = "+color);
    return color;
  }

  private double getDouble(String key, double def) {
    double val = def;
    String param = getParameter(key);
    try {
      if(param != null)
	val = new Double(param).doubleValue();
    } catch (NumberFormatException e) {
      debug("getDouble() not a double number parameter key "+key+"="+param);
    }
    debug("getDouble() parameter key "+key+"="+val);
    return val;
  }

  /**
   * Parameter info.
   */
  public String[][] getParameterInfo() {
    String[][] info = {
      {"a", "double", "(double real number) curve parameter"},
      {"b", "double", "(double real number) curve parameter"},
      {"bkcolor", "int", "(24-bit number) displayed as background"},
      {"drawcolor", "int", "(24-bit number) used to draw"},
      {"selectcolor", "int", "(24-bit number) used to show text"},
    };
    return info;
  }

  public void init() {
    //debug("init");
    Dimension d = size();
    width = d.width - 2;
    height = d.height - 2;
    debug(""+getBackground());
    a = getDouble("A", a);
    b = getDouble("B", b);
    bkColor = getColor("BKCOLOR", bkColor);
    drawColor = getColor("DRAWCOLOR", drawColor);
    selectColor = getColor("SELECTCOLOR", selectColor);
    
    setBackground(bkColor);    
    time=0.0;
    delta = 2 * 2 * Math.PI / Math.min(50, Math.max(width, height));
    head=tail=0;
    lines = new Rectangle[size];
    // HACK: to avoid memory gc problems we preinitalize
    for(int i = 0; i < size; ++i)
      lines[i] = new Rectangle();
    if(kicker == null) {
      kicker = new Thread(this);
      kicker.setPriority(Thread.MIN_PRIORITY);
      //      kicker.setDaemon(true);
      kicker.start();
      //debug("start:"+kicker);
    }
  }

  public boolean mouseDown(Event evt, int x, int y) {
    mode = CHOOSING;
    stop();
    clearAll();
    mouseCalc(x, y);
    oldX = x;
    oldY = y;
    return true;
  }

  public boolean mouseDrag(Event evt, int x, int y) {
    mouseCalc(oldX, oldY);
    oldX = x;
    oldY = y;
    mouseCalc(x, y);
    return true;
  }

  private void mouseCalc(int x, int y) {
    b = ((double)x / width) * 2 * Math.PI;
    a = Math.pow(10, 2 * ((double)y / height) - 1);

    Graphics g = getGraphics();
    String message = "sin("+a+"*x + "+b+")";

    g.setFont(font);
    FontMetrics fm = this.getFontMetrics(font);
    int stringwidth = fm.stringWidth(message);
    int stringheight = fm.getHeight();
    int stringascent = fm.getAscent();
    g.setColor(bkColor);
    int w = 2  * stringwidth;
    if(w > width)
      w = width -2;
    g.fillRect(1, 1, w, stringheight+10);
    g.setColor(selectColor);
    g.drawString(message, 5, stringascent);

    g.setXORMode(bkColor);
    g.drawLine(0, y, width, y);
    g.drawLine(x, 0, x, height);
  }

  public boolean mouseUp(Event evt, int x, int y) {
    mode = DRAWING;
    mouseCalc(x, y);
    paint(getGraphics());
    start();
    System.err.println("ClickMe: (sin(c*t+d),sin(a*t+b)), a="+a+", b="+b+"; c="+c+" d="+d+"; t=t+delta delta="+delta+";"); //x="+x+" y="+y);
    return true;
  }

  private synchronized int next(int pos) {
    return (pos + 1) % size;
  }

  private synchronized int prev(int pos) {
    return pos > 0 ? pos - 1 : size -1;
  }

  private synchronized void push(int x1, int y1, int x2, int y2) {
    if(full()) 
      throw new RuntimeException("Overful in queue in applet ClickMe."); 
    lines[head].reshape(x1, y1, x2 - x1, y2 - y1);
    ///debug("push() "+lines[head]);
    head=next(head);
    ++length;
  }

  private synchronized Rectangle pop() {
    if(empty())
      throw(new RuntimeException("No elements on queue in applet ClickMe."));
    --length;
    tail=next(tail);
    return lines[prev(tail)];
  }

  public synchronized void paint(Graphics g) {
    Dimension d = this.size();
    if ((offImage == null) || ((offWidth != d.width) || (offHeight != d.height))) {
      offImage = this.createImage(d.width, d.height);
      width = (offWidth = d.width) - 2;
      height = (offHeight = d.height) - 2;
    }
    Graphics offG = offImage.getGraphics();
    offG.setColor(bkColor);
    // debug("xor:"+offG.getXORMode());
    //offG.setXORMode(Color.black);
    //offG.setColor(Color.white);
    // TODO: how is drawed 3D (what colors used)?
    //offG.fill3DRect(0, 0, offWidth, offHeight, mode == CHOOSING);
    offG.fillRect(0, 0, offWidth, offHeight);

    offG.setColor(selectColor);
    FontMetrics fm = this.getFontMetrics(fontTitle);
    String message = (mode != INIT) ? "Click me! V2" : "Initialising...";
    int stringwidth = fm.stringWidth(message);
    int stringheight = fm.getHeight();
    int stringascent = fm.getAscent();
    offG.drawString(message, (int)((width - stringwidth)/2), (int)(height/2 + stringascent - stringheight/2));
    offG.setColor(drawColor);
    //offG.setXORMode(bkColor);
    // TODO: not such low level walking on queue --> Enumeration (in future...)
    //debug("paint:"+head+" "+tail+ lines[0]);
    for(int i = tail; !empty() && i != prev(head); i=next(i)) {
      ///debug("====t="+tail+" h="+head+" l="+length+" "+lines[i].toString());
      drawLine(offG, lines[i]);
    }
    //debug("paint: "+offG+" "+g);
    //transfer drawings on window
    g.drawImage(offImage, 0, 0, this);
    ///debug("<-paint");
  }

  public void start() {
    ///debug("start() stopped=false");
    stopped=false;
  }

  public void stop() {
    ///debug("stop() stoped=true");
    stopped = true;
    // we set flag and wait for other thread to catch up
    try {
      Thread.sleep(250);
    }
    catch(InterruptedException e) {;}
  }

  public void repaint() {
    paint(getGraphics());
  }

  /**
   * We implement *simple* protocol to start/stop working thread
   * Just using a global variable stopped to synchronize threads.
   * Other choice was to use Thread.start/stop but under Netscape
   * it got sometimes mixed: there were two or more threads...
   */

  public void run() {
    ///debug("<-run");
    int tick = 0;
    long mili = 0;
    /*
    while(true) {
      if(++tick > 100) {
	debug("run: mem="+Runtime.getRuntime().freeMemory()+"/"+Runtime.getRuntime().totalMemory());
	tick = 0;
      }      
      try {
	//      	Thread.sleep(1);
      }     
      catch(InterruptedException e) {;}
    }
    */
    while(true) {
      // let main thread have time to initalize
      try {
	Thread.sleep(300);
      }
      catch(InterruptedException e) {;}

      while(!stopped) {
	calcShape();
	paint(getGraphics());
	Thread.yield();
	try {
	  Thread.sleep(1000/(12));  // no frames on second
	}
	catch(InterruptedException e) {;}
	// HACK! HACK! KACK! If we don't sleep gc won't work 
	// and java process will suck up all memory (2MB/min)
	//	if(++tick > 1000) {	
	if(System.currentTimeMillis() - mili > 1000) {
	  mili = System.currentTimeMillis();
	  /*
	  try {
	    Thread.sleep(250);
	  }
	  catch(InterruptedException e) {;}
	  */
	  //Runtime.getRuntime().gc();
	  //Runtime.getRuntime().runFinalization();
	  
	  // We want to SEE gc is working	  
	  ///debug("run: mem free/total="+Runtime.getRuntime().freeMemory()+"/"+Runtime.getRuntime().totalMemory());
	  tick = 0;
	} 
	   
	// HACK: but I've got java.lang.OutOfMemoryError
	//if(++tick > 1000) {
	//  debug("run: mem="+Runtime.getRuntime().freeMemory()+"/"+Runtime.getRuntime().totalMemory());
	  //	  Runtime.getRuntime().gc();
	  //Runtime.getRuntime().runFinalization();

	//  tick = 0;
	//}
	///if(stopped) 
	  ///debug("run: stopped");	
      }
    }
  }
  
  private synchronized Rectangle top() {
    if(empty())
      throw(new RuntimeException("No elements on queue in applet ClickMe."));
    return lines[prev(head)];    
  }

  public void update(Graphics g) {
    paint(g);
  }
}
-------------- next part --------------
import java.awt.*;
        
public class Listing1
{
  static public void main (String args[])
    {
      Frame a_frame = new Frame("Listing 1");
      a_frame.add("Center", new Label("This is a test"));
      a_frame.pack();
      a_frame.show();
    }
}
 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ENVIRONMENT.csh
Type: application/x-csh
Size: 214 bytes
Desc: not available
Url : http://kaffe.org/pipermail/kaffe/attachments/19970423/cf40873c/attachment-0003.csh 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://kaffe.org/pipermail/kaffe/attachments/19970423/cf40873c/attachment-0003.html 


More information about the kaffe mailing list