Modify a text file: $/practice-array3.pde
$/practice-array3.pde
//// practice-array3 // Program to demonstrate arrays and algorithms. Version3 int[] a= new int[] { 55, 22, 33, 77, 11, 99, 88, 66, 44 }; int many= a.length; int x=10, y=100; boolean sorted=false; Button sortButton, fillButton; void setup() { size(500,300); sortButton= new Button("SORT", 300,50); fillButton= new Button("FILL", 300,150, color(127,127,255), 0); } void draw() { background(191,255,191); sortButton.show(); fillButton.show(); // Now, show the array (sorted or not). if ( ! sorted) { y=30; showArray(a,many); showBig(); } else { fill(0); text("SORTED ARRAY:", 10,10); y= 50; //// Display array, showArray(a, many); showBig(); } } void mousePressed() { //// Check which button was clicked. if (sortButton.isOver( mouseX, mouseY )) { sortArray(a, many); sorted= true; sortButton.c= color(0,255,0); // Button turns green. } if (fillButton.isOver( mouseX, mouseY )) { fillArray(a, many); sorted= false; sortButton.c= color(255,0,0); // Button turns green. } } void keyPressed() { //// Sort the array. sortArray(a, many); sorted= true; } //// ARRAY METHODS //// void fillArray( int a[], int many) { //// Fill the array for (int j=0; j
1 ) { //// Find biggest and move it to the end. int k= whereBig( a, n ); swap( a, k, n-1 ); // Move biggest to end. n--; // Now, shrink the array. } } int whereBig( int a[], int n ) { //// Return index of biggest. int w= 0; //// Start with first element. for (int j=1; j
a[w]) w= j; } return w; } void swap( int a[], int i, int j ) { //// Swap 2 elements of an array. int tmp; tmp= a[i]; a[i]= a[j]; a[j]= tmp; } void showArray( int a[], int many) { //// Show the array // Display the array. for (int j=1; j
big) big= a[j]; } // Output the result. println("Biggest: "+big); fill(0,0,0); text("Biggest value in array: ", 200,20); text(big, 350,20); } class Button { //// A button, displayed on screen, and able to detect when mouse is OVER. int x= 10; // Position and size of this button. int y= 10; int w= 100; int h= 50; color c; // Color of this button. int type=1; // Button type (1=rectangle) String s; // Text on this button. //// CONSTRUCTOR(S) //// Button() { //// Default constructor. c=color(255,127,127); s="BUTTON"; } Button( String name) { //Button(); //// Accept x & y coordinates; use default for the others. c=color(255,127,127); s=name; } Button( String name, int xset, int yset) { //Button(); //// Accept x & y coordinates; use default for the others. x= xset; y= yset; c=color(255,127,127); s=name; } Button( String name, int xset, int yset, color cset, int tset) { //Button(); //// Accept x & y coordinates; use default for the others. x= xset; y= yset; c= cset; type= tset; s=name; } //// METHODS() //// void show() { //// Show the button. fill(c); if (type<1) { ellipseMode(CORNER); ellipse(x,y,w,h); }else{ rectMode(CORNER); rect(x,y,w,h); } // Name on button. fill(0); text( s, x+10, y+h/2 ); } boolean isOver( int xx, int yy ) { //// Return true if xx,yy is over this button! if (xx < x) return false; else if (xx > x+w) return false; else if (yy < y) return false; else if (yy > y+h) return false; background(0); return true; //OR: return ( xx>x && xx
y && yy