Loading...
Logo
Processing Forum
Hi there,

Can anyone suggest what the best way would be to go about creating a periodic table of the elements using processing 2? I want to use it instead of HTML5 and I would like to create some effects with it once the basic structure is built. Can anyone offer me any help?

Thanks, Aoife

Replies(8)

is there any full code for the above? I'd love to have a play around with it but my compiled version of the fragmented code is giving me errors upon errors! Seems like the right idea though so thanks!
aoife38, I have moved all of your posts to General Discussion since they were just randomly placed in different sections of the forum. To know where to post a thread, please read: https://forum.processing.org/topic/where-to-place-threads-and-other-forum-practices
I found a data file that might help if you want to build the table from scratch:


you ***can create it from scratch but it's a horrible mess, because the table is so irregular.

I didn't even apply the correct colors...

It's using your 1st csv-file (optional)




Copy code
  1. //
  2. final boolean DEBUG = true;
  3. final color black = color(#000000);
  4. final int gridx = 18; // number of cells in x direction
  5. final int gridy = 9; // number of cells in x direction
  6. final int sizeX=42; // size
  7. final int sizeY=42;
  8. Cell [][]coor = new Cell[gridy][gridx];  // grid
  9. //
  10. void setup () {
  11.   size (900, 600);
  12.   background (black);
  13.   // frameRate(5);
  14.   //
  15.   String[] a  = loadStrings ("periodic_table.csv");
  16.   //println (a[3]);
  17.   //
  18.   // make grid
  19.   int countInternal = 1;
  20.   int atomic_number = 1;
  21.   int addY=0;

  22.   for (int j = 0; j<gridy; j++) {
  23.     for (int i = 0; i<gridx; i++) {

  24.       boolean exists=true;
  25.       if (countInternal>1&&countInternal<18) exists=false;
  26.       if (countInternal>20&&countInternal<31) exists=false;
  27.       if (countInternal>38&&countInternal<49) exists=false;
  28.       if (countInternal>=127&&countInternal<130) exists=false;
  29.       if (countInternal>=145&&countInternal<148) exists=false;
  30.       //
  31.       //  this is for the two little empty fields Lanthanides & Actinides
  32.       boolean special = false; // normal field
  33.       if (countInternal==93||countInternal==111) {
  34.         special=true; // empty field
  35.         atomic_number+=14;
  36.       }
  37.       //
  38.       // the lower two lines for Lanthanides & Actinides
  39.       if (j>=7) addY=22;
  40.       //
  41.       // if it is there read file "periodic_table.csv"
  42.       String name="";
  43.       if (a!=null) {
  44.         name="";
  45.         if (exists&&!special) {
  46.           if (atomic_number<118) {
  47.             String temp=a[atomic_number-1];
  48.             String[] parts= split(temp, ',');
  49.             println (temp);
  50.             if (!temp.equals(""))
  51.               name =parts[3];
  52.           }
  53.         }
  54.       }
  55.       //
  56.       coor[j] [i] = new Cell ( i *  (sizeX) + 72, j  *  (sizeY) + 42+addY,
  57.       color (random (255), random (255), 0),
  58.       atomic_number,
  59.       exists,
  60.       special,
  61.       countInternal,
  62.       name );
  63.       //
  64.       // if exists, increase atomic_number
  65.       if (exists)
  66.         atomic_number++;
  67.       // this gets always increased
  68.       countInternal++;
  69.     }
  70.   }
  71. }
  72. //
  73. void draw () {
  74.   background (black);
  75.   //
  76.   // show grid
  77.   for (int j = 0; j < gridy; j++) {
  78.     for (int i = 0; i < gridx; i++) {
  79.       coor[j] [i].display();
  80.     }
  81.   }
  82.   textSize(12);
  83.   text ("Lanthanides", 50+50, height-243+20);
  84.   text ("Actinides", 50+50, height-200+20);
  85.   // we assign one new color
  86.   //  int randoX = int (random(coor.length)); // rando = random number from array coor
  87.   //  int randoY = int (random(coor[1].length)); // rando = random number from array coor
  88.   //  coor [randoX][randoY].colCell= color (random (255), random (255), random (255));
  89. }
  90. //
  91. void keyPressed() {
  92.   //
  93. }
  94. void mousePressed() {
  95.   //
  96. }
  97. // =======================================================================
  98. class Cell {
  99.   // one cell is one element
  100.   int atomic_number;
  101.   float mass_number;
  102.   String name;
  103.   String symbol;
  104.   boolean exists=false;
  105.   boolean special=false;
  106.   float x;
  107.   float y;
  108.   float w=sizeX; //width/gridx-10;
  109.   float h=sizeY; //height/gridy-10;
  110.   color colCell;
  111.   int  countInternal;
  112.   //
  113.   Cell (  float x_, float y_,
  114.   color c_, int countExternal_,
  115.   boolean exist_, boolean special_,
  116.   int countInternal_,
  117.   String name_ ) {
  118.     x=x_;
  119.     y=y_;
  120.     colCell=c_;
  121.     atomic_number=countExternal_;
  122.     exists=exist_ ;
  123.     special=special_;
  124.     countInternal=countInternal_;
  125.     name=name_;
  126.   }
  127.   //
  128.   void display () {
  129.     if (exists) {
  130.       fill(colCell);
  131.       stroke(255);
  132.       rect(x, y, w, h);
  133.       fill(255);
  134.       textSize(12);
  135.       if (!special) {
  136.         text(atomic_number, x+16, y+18);
  137.         text(name, x+16, y+28);
  138.       }
  139.       if (DEBUG) {
  140.         textSize(8);
  141.         text(""+countInternal+"", x+12, y+34);
  142.       }
  143.     }
  144.   }
  145.   //
  146. } // class
  147. // =======================================================================


Impressive work,  Chrisir.  I think it would be easier if you could find (or build) a file that contained the period & group number for each element.  That would make it almost as easy as placing each element at the correct x,y coordinates.