-------- Original Message --------
Subject: Re: Calling Same Native method more than once.
Date: Wed, 04 Aug 1999 10:46:06 -0400
From: Jacob Nikom <[EMAIL PROTECTED]>
Reply-To: [EMAIL PROTECTED]
Organization: MERL
To: [EMAIL PROTECTED]
References: <[EMAIL PROTECTED]> <[EMAIL PROTECTED]>
<[EMAIL PROTECTED]>

Hi Nagaraj,

I think it is better to see one time the working code than discuss how
to
do it. Here are the two files: one in Java and one in C which create
animated color. The program runs on Linux and NT. You have to run
it as application, not applet.

Jacob Nikom


=========Start of the JCMotion.java file ===========
/*
 *<applet code=JCMotion width=200 height=200>
 *</applet>
 */

import javax.swing.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;

// This applet creates a series of moving
// lines by creating a memory image and cycling
// its color palette.

public class JCMotion extends JApplet implements Runnable,
ActionListener
{
  public native void imageGen(int[] intArr, int imageWidth, int
ImageHeight, int
squareSize);

  static
  {
    System.loadLibrary("Image_native");
  }

  // variables come after system library
  protected Image cycledImage;   // image after cycling

  protected Thread cycleThread = null;
  protected int delay = 10;   // milliseconds between cycles

  protected static int squareSize  = 10;
  protected static int imageWidth  = 100;
  protected static int imageHeight = 100;

  // holding pixels in memory
  int intArr[];

  public void init()
  {

// Create space for the memory image
    intArr = new int[imageWidth * imageHeight];

// Now create the image
    imageGen(intArr, imageWidth, imageHeight, squareSize);

// Create the cycled image
    cycledImage =
      createImage(
        new MemoryImageSource(imageWidth, imageHeight, intArr, 0,
imageWidth));
  }

// Paint simply draws the cycled image
  public synchronized void paint(Graphics g)
  {
//  g.drawImage(cycledImage, 0, 0, this);
    g.drawImage(cycledImage, 0, 0, getWidth(), getHeight(), this);
  }

// Flicker-free update
  public void update(Graphics g)
  {
    paint(g);
  }

// Cycles the colors and creates a new cycled image. Uses media
// tracker to ensure that the new image has been created before
// trying to display. Otherwise, we can get bad flicker.

  public synchronized void doCycle()
  {
// Flush clears out a loaded image without having to create a
// while new one.
    cycledImage.flush();

// Generate new image
    imageGen(intArr, imageWidth, imageHeight, squareSize);
// Create the cycled image
    cycledImage =
      createImage(
        new MemoryImageSource(imageWidth, imageHeight, intArr, 0,
imageWidth));

// When we use waitForID on this image now, it will be regenerated.

    MediaTracker myTracker = new MediaTracker(this);
    myTracker.addImage(cycledImage, 0);
    try
    {
// Cause the cycledImage to be regenerated
      if (!myTracker.waitForID(0, 1000))
      {
        return;
      }
    } catch (Exception ignore) { }

// Now that we have reloaded the cycled image, ask that it
// be redrawn.
    repaint();
  }

// Typical threaded applet start and stop
   public void start()
  {
    if (cycleThread == null)
    {
      cycleThread = new Thread(this, "Cycle");
      cycleThread.start();
    }
  }

  public void stop()
  {
    cycleThread = null;
  }

// Continually run
  public void run()
  {
    Thread myCurrentThread = Thread.currentThread();

    while (cycleThread == myCurrentThread)
    {
      doCycle();
      try
      {
        Thread.sleep(delay);
      } catch (Exception hell) { }
    }
  }

  public void actionPerformed(ActionEvent evt)
  {
    System.out.println("actionPerformed");
    this.stop();
  }

/**
  * application entry point. create window and new set of
  * command-line arguments
 **/
  public static void main (String args[])
  {
    JFrame f = new JFrame ("Cycler");
    JCMotion motion = new JCMotion();

    motion.init ();
    f.setSize (imageWidth, imageHeight);
    f.getContentPane().add ("Center", motion);
    f.setVisible(true);
    f.addWindowListener(new WindowCloser());

    motion.start ();
  }
}

class WindowCloser extends WindowAdapter
{
  public void windowClosing(WindowEvent e)
  {
    Window win = e.getWindow();
    win.setVisible(false);
    win.dispose();
    System.exit(0);
  }
}

==============End of the JCMotion.java file =============

=============Start of the Image_native.c file=============

/* JCMotion.h generated by applying javah
 * on the Java class, and contains the
 * function declaration for the native method
 */
#include "JCMotion.h"

/* used for printf */
#include "stdio.h"

/* Signatures of several of these are in JCMotion.h */
static ii = 0;

JNIEXPORT void JNICALL Java_JCMotion_imageGen(
                                              JNIEnv *env, jobject obj,
                                              jintArray intArr,
                                              jint imageWidth,
                                              jint imageHeight,
                                              jint squareSize
                                             )
{
   int i, j;

   int alpha, red, green, blue, index;

   jint* intArrElems = (*env)->GetIntArrayElements(env, intArr, 0);

   alpha = 255;
   index = 0;

   for (i = 0; i < imageHeight; i++)
   {
      for (j = 0; j < imageWidth; j++)
      {
         red = 255 - ii*2;
         green = j*2 + ii/2;
         blue = ii + j;
         intArrElems[index++] = (alpha << 24) | (red << 16) | (green <<
8) | (blue
<< 0);
      }
   }

   if ( ii >= imageWidth - squareSize) ii = 0;

   printf("imageGen: ii = %3d\n",ii);
   ii++;

   (*env)->ReleaseIntArrayElements(env, intArr, intArrElems, 0);
}
================End of the Image_native.c file==============


"Nagaraj S.B" wrote:

> Jacob Nikom wrote:
> >
> > I don't think there is any limitations on the number of calls of C routine.
> > I call my C routine from Java thousand times without any problem. Look
> > for something else - memory problem of threads. Do you call your routine
> > from the main tread or you created separate one?
> I am calling it from the main.
> Nagaraj
> >
> > Jacob
> >
> > "Nagaraj S.B" wrote:
> >
> > > Hi all,
> > > Thanks for helping me in interfacing C with Java.I am facing a problem
> > > in executing my program(java) which calls a 'C' function.
> > > I am calling 'C' native method for every 10 secs in my 'java' program ,
> > > but after first loop, my program(java) exits by giving
> > > SIGSEV 11* Segmentation violation .................
> > > How to call same native method for more than one time ?.
> > > Thanks in advance,
> > > --
> > > Nagaraj S.B.
> > > Bells Softech Ltd,Bells House,1036,
> > > 26th Main,4th 'T' Block,
> > > Jayanagar, Banglore - 560 041.Ph.No.:6650084/33.
> > >
> > > ----------------------------------------------------------------------
> > > To UNSUBSCRIBE, email to [EMAIL PROTECTED]
> > > with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]
> >
> > ----------------------------------------------------------------------
> > To UNSUBSCRIBE, email to [EMAIL PROTECTED]
> > with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]
>
> --
> Nagaraj S.B.
> Bells Softech Ltd,Bells House,1036,
> 26th Main,4th 'T' Block,
> Jayanagar, Banglore - 560 041.Ph.No.:6650084/33.
>
> ----------------------------------------------------------------------
> To UNSUBSCRIBE, email to [EMAIL PROTECTED]
> with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]


----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

Reply via email to