Loading...
Logo
Processing Forum
Hello all,

new day, new problem !!!
I try to make a layer system and sort the layer from the top to the bottom.
But I've big problem to sort the objects from my ArrayList.
The idea it's to show the level of the layer and give the position, if it's the first, second,... or the last.
I try to extract the level of my layer list to copy in list of rank to sort this one, because I don't manage to sort directly my list of object... I try to make an example short, but don't manage too. So I give the big sketch.
I see few solution in the forum, but very complex for me...I just find the solutions with Collections.sort(arg).

I show the problem is in the part 3 between ///////////////////// and ///////////////
Copy code
  1. color BG = color(100,95,95) ;

    ControlLayer cLayer ;
    PVector posLayer = new PVector (0,0) ;
    PVector sizeLayer = new PVector (0,0) ;

    float rowHeight = 30.0 ;
    float margin = 6.0 ;

    void setup ()
    {
      size (500, 500 ) ; background( BG) ;
      cLayer = new ControlLayer(rowHeight) ;
    }

    void draw ()
    {
      background( BG) ;
      cLayer.display() ;
      float mrg = margin /2.0 ;
      for (int i = 0 ; i < height ; i += rowHeight ) {
        stroke(0) ;
        strokeWeight(1) ;
       
        line (0, i-mrg , width , i-mrg ) ;
        line (0, i+mrg , width , i+mrg ) ;
      }
    }

    void mousePressed()
    {
      float posY = (floor(mouseY / rowHeight) * rowHeight) + rowHeight / 2.0 ;
      // println (posY) ;
      posLayer.x = mouseX ;
      posLayer.y = posY ;
      sizeLayer.x = width ;
      sizeLayer.y = rowHeight -margin ;
      int identity = round(random(1,6)) ;
      cLayer.addLayer(posLayer, sizeLayer, identity) ;
    }
  2. class ControlLayer
    {
      ArrayList<Layer> listLayer ;
      float rowHeight ;
      int maxLayer = 5 ;
     
      ControlLayer(float rowHeight)
      {
        this.rowHeight = rowHeight ;
        listLayer = new ArrayList<Layer>() ;
      }
      //////////////////
      void addLayer (PVector p, PVector s, int ID)
      {
        for ( Layer lyr : listLayer ) {
         
          if (lyr.detection() || listLayer.size() > maxLayer-1) {;
            return ;
          }
         
        }
        //Layer lyr = new Layer(p, s, rowHeight, Layer.ID1) ; // example to call the ID directly from the class Layer
       Layer lyr = new Layer(p, s, rowHeight, ID) ;
        listLayer.add(lyr) ;
    }
     
      void display()
      {   
        for ( Layer lyr : listLayer ) {
          lyr.drag(listLayer) ;
          lyr.update() ;
          lyr.collision(listLayer) ;
         
          println(lyr.getRanking() ) ;
         
          lyr.display() ;
          lyr.info(listLayer) ;     
        }
      }
    }
  3. class Layer
    {
      private color c ;
      private color noir = color (0) ;
      private color blanc = color (255) ;
      private color rouge = color (215,30,30 ) ;
      private color orange = color (255,150,0 ) ;
      private color jaune = color (255,230,0 ) ;
      private color violet = color (150,90,200 ) ;
      private color vert = color (160,180,70 ) ;
      private color bleu = color (90,170,200 ) ;
      private color IDcolor1 = orange ;
      private color IDcolor2 = jaune ;
      private color IDcolor3 = rouge ;
      private color IDcolor4 = violet ;
      private color IDcolor5 = bleu ;
      private color IDcolor6 = vert ;
     
      PVector p, s ;
      float newPosX, newPosY ; // we cannot use PVector for this relay, if we use PVector relay the object don't come back
      float rowHeight ;
     
      // identity of layer
      int ID = 0 ;
      final static int ID1 = 1 ;
      final static int ID2 = 2 ;
      final static int ID3 = 3 ;
      final static int ID4 = 4 ;
      final static int ID5 = 5 ;
      final static int ID6 = 6 ;
     
      // ranking of layer
      int rankLayer = 1 ;
      ArrayList listRanking ;
      // boolean to move the layer
      boolean inside, locked, contact ;
      boolean dropped = true ;
     
      Layer(PVector p, PVector s, float rowHeight, int ID)
      {
        rectMode (CENTER) ;
        this.p = p ;
        newPosX = p.x ;
        newPosY = p.y ;
     
        this.s = s ;
        this.rowHeight = rowHeight ;
        this.ID = ID ;
       
        listRanking = new ArrayList() ;
      }
     
      void update()
      {
          float posY = (floor(mouseY / rowHeight) * rowHeight) + rowHeight / 2.0 ; // move in the row grid
          if(locked) { // if the layer is locked he follow the mouse
          newPosX = mouseX ;
          newPosY = posY ;
        } else if (contact && !locked && !dropped) {
          c= blanc ;
          newPosX = p.x ;
          newPosY = p.y ;
        } else if (contact) {
          c= blanc ;
        } else {
          c = noir ;
          newPosX = newPosX ;
          newPosY = newPosY ;
        }
      }
     
      void display()
      {
        if      ( ID == 1 ) c = IDcolor1 ;
        else if ( ID == 2 ) c = IDcolor2 ;
        else if ( ID == 3 ) c = IDcolor3 ;
        else if ( ID == 4 ) c = IDcolor4 ;
        else if ( ID == 5 ) c = IDcolor5 ;
        else if ( ID == 6 ) c = IDcolor6 ;
        else c = noir ;
        fill(c) ;
       
        noStroke() ;
        // rect (newPosX, newPosY, s.x, s.y ) ; // basic
        rect (width/2, newPosY, s.x, s.y ) ;
      }
      // DRAG
      void drag(ArrayList<Layer> listLayer)
      {
        // check if boolean inside is true and the mouse is pressed
        if (detection() ) inside = true;  else inside = false;
       
        if ( inside && mousePressed && !otherLocked(listLayer)  )  { locked = true ; dropped = false ; }
        if ( !mousePressed ) { locked = false ;  }
        if ( !mousePressed && !inside  ) { dropped = true ;  }
      }
      //COLLISION.CONTACT.NoCONTACT
      //::Collision
      void collision(ArrayList<Layer> listLayer )
      {
        for (Layer target : listLayer) {
          if (target != this) {
            //check if two layers are in contact
            if (collide(target)  ) {
                contact = true ;
                break ; // we must stop the loop at the first collide
            } else {
                contact = false ;
            }
          }
        }
      }
      //INFO
      //show info
      void info (ArrayList<Layer> listLayer)
      {
        fill (noir) ;
        rankingLayers(listLayer) ;
        text (rank() + " / " + rankLayer, 10, newPosY +5 ) ; // give the rank of layer from the top of board
      }
      ///////////////THE PROBLEM IS HERE////////////////////////
      /////////////////////////////////////////////////////////
      // ranking the layer from the top to the bottom
      void rankingLayers(ArrayList<Layer> listLayer )
      {
        // clear the rank list to update the ranking
        listRanking.clear() ;
       
        for (Layer target : listLayer) {
          if ( listRanking.size() != listLayer.size()  ) {
            listRanking.add(target.rank()) ;
          }
        }
        Collections.sort(listRanking) ;
      //  String r = (listRanking) ;
      //  println(listRanking) ;
    /*   
        listLayer.clear() ;
       
        for( Layer lyr : listRanking ) {
          if ( listRanking.size() != listLayer.size() ) {
            listLayer.add(lyr) ;
          }
        }
      */   
       // println(listRanking) ;
       
      }
      //RETURN
      // return rank from the top of board
      ArrayList getRanking() {
        return listRanking ;
      }
      ////////////////////////////////////////////////////////
      ////////////////////////////////////////////////////////
      int rank() {
        int r = floor(newPosY / rowHeight) +1 ;
        return r ;
      }
      // check if the cursor is on the layer or not
      boolean detection() {
        if( mouseX >  newPosX - s.x /2.0 &&
            mouseX <  newPosX + s.x /2.0 &&
            mouseY >  newPosY - s.y /2.0 &&
            mouseY <  newPosY + s.y /2.0) {
          return true;
        } else {
          return false;
        }
      }
      //
      // Make sure no other buttons are active in the list of objects wished
      boolean otherLocked(ArrayList<Layer> listLayer)
      {
        for (Layer lyr : listLayer ) { // don't need to call the the ArrayList because we call this one in the Collision Void
          if (lyr.locked) {
            return true;
          }
        }
        return false;
      }
      //
      // see if there is collide between two things
      boolean collide(Layer target)
      {
        float distanceX = newPosX - target.newPosX ;
        float distanceY = newPosY - target.newPosY ;
        float treshX = s.x /2.0 + target.s.x /2.0 ;
        float treshY = s.y /2.0 + target.s.y /2.0 ;
       // println(abs(distanceY) + " / " + treshY) ;
        if (abs(distanceX) < treshX && abs(distanceY) < treshY) {  // if the distance is less than the threshold, we are colliding!
          return true;
        } else {
          return false;
        }
      }
      ///////////////
    }
Thanks

Stan

Replies(14)

class Layer implements Comparable<Layer>

and add:
  public int compareTo(Layer layer)
  {
      return rank() - layer.rank(); // Supposes rank() isn't big
  }
to the class. Then you can use Collections.sort() on an ArrayList<Layer> directly.
(untested)
Just awesome !!!! work perfectly.
I'm looking for solution since two days. And you give me a solution than I'de write in ten secondes.

Thx a lot.

Stan


Hello,

There is a new problem with sort in the P2.0b8
he cannot find something named "Collections" it's removed in the new version ?

thx
Add this statement as the first line of your code
Copy code
  1. import java.util.*;
Hello Quarks,

Perfect, I think now every time i have a problem with something in P2.0b8 I have import java.util.*; !!!!! why he don't had directly.... :)


Thx a lot for your quick answer
" why he don't had directly"
It was the default until 2.0b7, then, for some reason, they removed the default imports.
One of the most confusing, thus controversial, changes (even more as it broke some example programs coming with Processing, too!).

... for some reason, they removed the default imports.
Reason is standardization (canonization is more appropriate)!  
So the various Processing incarnations would have a formal language base, especially Processing.JS.

I'd prefer much more if Processing would grab more of the unique strengths of each language, especially Java,
instead of going on a path of devolution to a poor common base!

I wonder how feasible it is to light-fork version 1.5.1,
just enough to keep up on some of the new functions from series 2. 

Like PShape & PVector and others!
I don't know why they don't tailor the default imports based on the editor mode JAVA/JAVASCRIPT/ etc
if I understand they remove fews import function to translate easily to processing.js and other language brother ?

There is a list of the remove import ?
You can find it @ %AppData%\Processing\preferences.txt

preproc.imports.list =
java.applet.*,java.awt.Dimension,java.awt.Frame,java.awt.event.MouseEvent,java.awt.event.KeyEvent,
java.awt.event.FocusEvent,java.awt.Image,java.io.*,java.net.*,java.text.*,java.util.*,java.util.zip.*,java.util.regex.*
thx..
I think you code on PC I don't find your adress AppData/Processing/preferences.txt :)
Go to the Preferences dialog, it shows where the preferences.txt file is stored on your system.

"canonization"
I know, but it replaces questions like "why my program, using Dimension and Collections and all doesn't work in JavaScript?" with questions like "why my program working in previous versions doesn't work now?".
Not much a progress...
It makes usage of advanced Java classes more explicit, but it is a two-edged sword.

but it replaces questions like... but it is a two-edged sword.
Yea, but I get a feeling that questions were reversed in bigger numbers about:
  • Why don't examples from a Processing's book work?
  • Why don't examples from reference/wiki work?
  • What do I have to import to make my program work?
  • etc.
I think people already had an idea that not everything in Processing's Java
could be imported to Processing's JavaScript and others.

But their programs were already working before they attempted to transition it to JS.
However, when a program fails outright in Java mode from an official source example,
people got in despair and ask a lot!  

For me it's not a problem the simplification, less is more. And I used the advanced-classes because I'd need in my project.

But may be the debug window can give back information when there is problem with old version and the new :

he cannot say just "not found" but "this function is removed, and cannot work without specific import" or something like that.

what do you think about that ?