Loading...
Logo
Processing Forum
New to Processing any help is massively appreciated. I have 2 classes created in Java but I'd like to convert them to processing anyone up for helping me convert them? 
Code below:
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.*;



public class MatrixDisplay extends Frame
implements ImageObserver, WindowListener, Runnable {
    public int vRows[]; //unused
    public int vCols[]; //unused
    public int nactive[]; //number of active cells in the column ('stream length')
    public int offsets[]; //offsets of the columns from 0
    public int velos[]; //velocities of the columns (right now, 0-4)
    public int probs[]; //velocities of the columns (right now, 0-4)
    public int upd[];
    public int nRows = 0; //number of rows (estimated)
    public int nCols = 0; //number of columns (estimated)
    
    public int vI;
    
    public char[] carr;
    public char[][] matrix;

    
    private Thread repaintThread;
    private RCGthread RCG;
    private RNGthread RNG;

    transient private Image backImage = null; //background image for debuffering
    transient private Graphics bg     = null; //gfx context for debuffering
    
    public Color flash;
    public Color tail;
    public Color ngreen;
    
    //constructor
    public MatrixDisplay() {
super();      
if((this.getSize().width > 0) && (this.getSize().height > 0)){
nCols = (this.getSize().width/15);
nRows = (this.getSize().height/15);
   }
else
   {
nCols = 400/15;
nRows = 250/15;
   }
try{
   matrix = new char[nCols][nRows];
   nactive = new int[nCols];
   offsets = new int[nCols];
   velos = new int[nCols];
   probs = new int[nCols];
   upd = new int[nCols];
   
   //setup initial r/c/v s
   for(int i = 0; i < nCols; i++)
{
   nactive[i] = (int)(java.lang.Math.random()*(nRows/1.5));
   offsets[i] = 15-(nactive[i]*15);//(int)(java.lang.Math.random()*nRows);
   velos[i] = (int)((java.lang.Math.random()*12)+2);
   probs[i] = (int)((java.lang.Math.random()*10));
   upd[i] = 0;
}
}catch(Exception ex){
   System.err.println("Initialization error:"+ ex);
   System.err.println("Try resizing your window to fix it");
}
//window resizing 
resizer sizer = new resizer();
addComponentListener(sizer);
addWindowListener(this);
flash = new Color(230,255,230);
tail = Color.green.darker().darker();
ngreen = Color.green.darker();
repaintThread = new Thread(this);
repaintThread.start();
RCG = new RCGthread(10240);
RCG.start();
RNG = new RNGthread(10240);
RNG.start();

carr = new char[1];
    }
    
    public void run () {
while (true) {
   try { Thread.sleep(110); } catch (InterruptedException e) {}
   repaint();
}
    }
    
    
    public void update (Graphics g) {
if( backImage == null || backImage.getWidth(this) != this.getSize().width ||
   backImage.getHeight(this) != this.getSize().height ) {
   MediaTracker mt = new MediaTracker( this );
   backImage = createImage( this.getSize().width, this.getSize().height );
   mt.addImage( backImage, 0 );
   try{ mt.waitForAll(); }catch(Exception e){}
   bg = backImage.getGraphics();
}
if( bg != null && this != null ) {
   bg.setColor( Color.black );
   bg.setFont(new Font("serif", Font.BOLD, 15));
   bg.fillRect( 0, 0, this.getSize().width, this.getSize().height );
}
paint(bg);
if (backImage != null) g.drawImage(backImage,0,0,this);
    }
    
    //double buffered painting... 
    public void paint(Graphics g)
    {
int colctr;
try{
   for(int il = 0; il < nCols; il++)
{
   double tempR = RNG.get_RNG();

   if(offsets[il] < nactive[il]*15)
    {
   offsets[il] += 15;
       upd[il] = 3;
}
   else if ((int)(tempR*96) <= ( probs[il] * (velos[il]-5))) 
{
   offsets[il] += 15 ;
       upd[il] = 2;
}

   else if((int)(RNG.get_RNG()*100) <= ( probs[il] * (velos[il]-5)))
{
   offsets[il] = offsets[il] + velos[il] ;
       upd[il] = 1;
}

   else
{
   upd[il] = 0;
}
}
}catch(Exception ex){/*System.err.println("paint caught:"+ex);*/}
int h = this.getSize().height;
int jptemp;
int iptemp;
try{
   //loop, updating each column's position
   for (int i = 1; i < nCols; i++ )
{
   
   iptemp = i * 15;
   colctr = nactive[i];
   
   for (int j = 0; j <= colctr; j++ )
{
   jptemp = j * 15;
   
   if ((j == 0 ) /*&& (upd[i] == 1)*/)
{
   bg.setColor(tail);
}
   else if (j == 2)
{
   bg.setColor(ngreen);
}

   if (upd[i] > 1)    
{
   if (j == colctr)
   {
if (upd[i] == 2)
bg.setColor(flash);
    matrix[i][j] = RCG.get_RCG();
   }
   else
    matrix[i][j] = matrix[i][j+1];

   carr[0] = matrix[i][j];
   bg.drawChars( carr,
 0,1, (iptemp), ( (offsets[i]) + (jptemp) ) % h );
}

   else if (upd[i] == 1 )
{
   
   if (j == colctr)
bg.setColor(flash);
   
   carr[0] = RCG.get_RCG();
   matrix[i][j] = carr[0];
   bg.drawChars( carr,
 0,1, (iptemp), ( (offsets[i]) + (jptemp) ) % h );
}
   else
{
   if(matrix[i][j] == '\0')
   {
   carr[0] = RCG.get_RCG();
       matrix[i][j] = carr[0];
   }
   else
   carr[0] = matrix[i][j];
   bg.drawChars( carr,
 0,1, (iptemp), 
 ( (offsets[i] ) + (jptemp) ) % h );
}    
}
}
   
}catch(Exception ex){/*System.err.println("paint error:"+ex);*/}
    }
   
    
    //resizing 
    class resizer extends java.awt.event.ComponentAdapter
    {
public void componentResized(java.awt.event.ComponentEvent event)
{
   Component c = event.getComponent();
   
   nCols = c.getSize().width/15;
   
   nRows = c.getSize().height/15;

   matrix = new char[nCols][nRows];    
   nactive = new int[nCols];
   offsets = new int[nCols];
   velos = new int[nCols];
   probs = new int[nCols];
   upd = new int[nCols];
   
   for(int il = 0; il < nCols; il++)
{
//System.err.println(il);
   nactive[il] = (int)(RNG.get_RNG()* (nRows/1.5));
   offsets[il] = 15-(nactive[il]*15);/*(int)(RNG.get_RNG()* nRows);*/
   velos[il] = (int)((RNG.get_RNG()*12)+2);
   probs[il] = (int)((RNG.get_RNG()*10));
   upd[il] = 0;
}
   try{
c.repaint();
c.setVisible(true);
   }catch(Exception ex){;}
}
    }
    
    //mouse 
    class mad extends java.awt.event.MouseAdapter
    {
public void mouseClicked(java.awt.event.MouseEvent event)
{
   repaint();
}
    }
    
    // WindowListener
    public void windowClosing (WindowEvent event) {
dispose();
    }
    
    public void windowClosed (WindowEvent event) {
        close();
System.exit(0);
    }

    public void close() {
        repaintThread.stop();
  repaintThread = null;
        RNG.stop();
  RNG = null;
        RCG.stop();
RCG = null;
  System.exit(0);
  }

    
    public void windowActivated (WindowEvent event) { }
    public void windowDeactivated (WindowEvent event) { }
    public void windowDeiconified (WindowEvent event) { }
    public void windowIconified (WindowEvent event) { }
    public void windowOpened (WindowEvent event) { }
    
}





import java.applet.*;

public class Matrix extends Applet
{

   MatrixDisplay md;

   public static void main (String args[]) {
Matrix blah = new Matrix();
       blah.init();

   }

   public void init()
   {
md = new MatrixDisplay();
md.setSize(400,250);
md.show();
   }

   public void destroy()
   {
if (md != null) {
try {
     md.close();
} catch (Exception ex) { /*we were shutting down anyway*/ }
   }
 }
}

Replies(5)

Voilà!

Fate, it seems, is not without a sense of irony.
Copy code
  1. int i,h,w;
  2.  
  3. void setup() {
  4.   size(w=1000, h=w/2);
  5.   textFont(createFont("Arial Black", 10));
  6. }
  7.  
  8. void draw() {
  9.   fill(0, 10);
  10.   rect(0, 0, w, h);
  11.   fill(0, h/2, i=0);
  12.   while (i<w) text((char)random(h/2), i+=7, (frameCount*7+noise(i)*h*i)%h);
  13. }
Thanks for the reply i've commented below
Thanks for your answer amnon.owed. I tried for hours but still can't seem to get my program running correctly.
I've made a few minor changes to my original idea but basically I'm trying to create the 10101010101 scrolling screen from the movie The Matrix.
Changes I've made to the original idea is that my program should read the values from a text file or maybe the 0's from one text file and the 1's from another.
I'm using eclipse and I'm trying to get it running as a PApplet.
I'd also like to use a keyboard input to begin the scrolling process when the applets starts up.
I'd be hugely in your debt if you could help me with this project as it is 20% of my college grade and processing is just not clicking with me. Thank You.
A starting point, as I have no time to finish this, even less to test this.
Most of the work is to remove Java fluff...
You will have to manage the paint() so it doesn't get a Graphics, but instead paints on the sketch itself.
You better load the image using Processing idioms directly.
You have to guess what the two Thread classes, whose code is missing here, are doing. Perhaps they don't need to be Threads, anyway.
I hope to give you a general idea of the work to do.

MatrixDisplay matrix;

void setup()
{
  size(400, 250);

  matrix = new MatrixDisplay();
}

void draw()
{
  matrix.paint();
}

public class MatrixDisplay {
  public int vRows[]; //unused
  public int vCols[]; //unused
  public int nactive[]; //number of active cells in the column ('stream length')
  public int offsets[]; //offsets of the columns from 0
  public int velos[]; //velocities of the columns (right now, 0-4)
  public int probs[]; //velocities of the columns (right now, 0-4)
  public int upd[];
  public int nRows = 0; //number of rows (estimated)
  public int nCols = 0; //number of columns (estimated)

  public int vI;

  public char[] carr;
  public char[][] matrix;

  // Classes are missing, you have to guess what they do and skip the Thread part, perhaps
  private RCGthread RCG;
  private RNGthread RNG;

  public Color flash;
  public Color tail;
  public Color ngreen;

  //constructor
  public MatrixDisplay() {
    nCols = width/15;
    nRows = height/15;
    try {
      matrix = new char[nCols][nRows];
      nactive = new int[nCols];
      offsets = new int[nCols];
      velos = new int[nCols];
      probs = new int[nCols];
      upd = new int[nCols];

      //setup initial r/c/v s
      for (int i = 0; i < nCols; i++)
      {
        nactive[i] = (int) random(nRows/1.5);
        offsets[i] = 15-(nactive[i]*15);
        velos[i] = (int) random(12)+2;
        probs[i] = (int) random(10);
        upd[i] = 0;
      }
    }
    catch(Exception ex) {
      println("Initialization error:"+ ex);
      println("Try resizing your window to fix it");
    }
    flash = new Color(230, 255, 230);
    tail = Color.green.darker().darker();
    ngreen = Color.green.darker();

    RCG = new RCGthread(10240);
    RCG.start();
    RNG = new RNGthread(10240);
    RNG.start();

    carr = new char[1];
  }

  public void update (Graphics g) {
    if ( backImage == null || backImage.getWidth(this) != this.getSize().width ||
      backImage.getHeight(this) != this.getSize().height ) {
      MediaTracker mt = new MediaTracker( this );
      backImage = createImage( this.getSize().width, this.getSize().height );
      mt.addImage( backImage, 0 );
      try {
        mt.waitForAll();
      }
      catch(Exception e) {
      }
      bg = backImage.getGraphics();
    }
    if ( bg != null && this != null ) {
      bg.setColor( Color.black );
      bg.setFont(new Font("serif", Font.BOLD, 15));
      bg.fillRect( 0, 0, this.getSize().width, this.getSize().height );
    }
    paint(bg);
    if (backImage != null) g.drawImage(backImage, 0, 0, this);
  }

  //double buffered painting...
  public void paint(Graphics g)
  {
    int colctr;
    try {
      for (int il = 0; il < nCols; il++)
      {
        double tempR = RNG.get_RNG();

        if (offsets[il] < nactive[il]*15)
        {
          offsets[il] += 15;
          upd[il] = 3;
        }
        else if ((int)(tempR*96) <= ( probs[il] * (velos[il]-5)))
        {
          offsets[il] += 15 ;
          upd[il] = 2;
        }

        else if ((int)(RNG.get_RNG()*100) <= ( probs[il] * (velos[il]-5)))
        {
          offsets[il] = offsets[il] + velos[il] ;
          upd[il] = 1;
        }

        else
        {
          upd[il] = 0;
        }
      }
    }
    catch(Exception ex) {/*System.err.println("paint caught:"+ex);*/
    }
    int h = height;
    int jptemp;
    int iptemp;
    try {
      //loop, updating each column's position
      for (int i = 1; i < nCols; i++ )
      {
        iptemp = i * 15;
        colctr = nactive[i];

        for (int j = 0; j <= colctr; j++ )
        {
          jptemp = j * 15;

          if ((j == 0 ) /*&& (upd[i] == 1)*/)
          {
            bg.setColor(tail);
          }
          else if (j == 2)
          {
            bg.setColor(ngreen);
          }

          if (upd[i] > 1)   
          {
            if (j == colctr)
            {
              if (upd[i] == 2)
                bg.setColor(flash);
              matrix[i][j] = RCG.get_RCG();
            }
            else
              matrix[i][j] = matrix[i][j+1];

            carr[0] = matrix[i][j];
            bg.drawChars( carr,
            0, 1, (iptemp), ( (offsets[i]) + (jptemp) ) % h );
          }

          else if (upd[i] == 1 )
          {

            if (j == colctr)
              bg.setColor(flash);

            carr[0] = RCG.get_RCG();
            matrix[i][j] = carr[0];
            bg.drawChars( carr,
            0, 1, (iptemp), ( (offsets[i]) + (jptemp) ) % h );
          }
          else
          {
            if (matrix[i][j] == '\0')
            {
              carr[0] = RCG.get_RCG();
              matrix[i][j] = carr[0];
            }
            else
              carr[0] = matrix[i][j];
            bg.drawChars( carr,
            0, 1, (iptemp),
            ( (offsets[i] ) + (jptemp) ) % h );
          }
        }
      }
    }
    catch(Exception ex) {/*System.err.println("paint error:"+ex);*/
    }
  }
}

Thank You this was very helpful. :)