Image Update
in
Programming Questions
•
5 months ago
I want to be able to show a graphical representation of cards moving and sorting. But I found out draw() only updates after it's done. I thought that if I call my sort from setup, then have the sort call draw() every time it wants to update, it would get rid of that problem. But it still only displays when it's done.
How can I fix this problem, and how should I organize code like this in the future? I don't want the code to be sloppy and inefficient, what is the best way to approach this? I've not come across this problem before.
PFont f;
Card[] a;
int numcards = 20;
int numcards = 20;
void setup(){
size(650, 400);
rectMode(CENTER);
f = createFont("Arial",16,true);
textFont(f);
a = new Card[numcards];
for(int i = 0; i < numcards; i++){
a[i] = new Card(i*30 + 15, 60, i+1);
}
switchcards(0, 1);
}
size(650, 400);
rectMode(CENTER);
f = createFont("Arial",16,true);
textFont(f);
a = new Card[numcards];
for(int i = 0; i < numcards; i++){
a[i] = new Card(i*30 + 15, 60, i+1);
}
switchcards(0, 1);
}
void draw(){
background(255);
for(int i = 0; i < numcards; i++){
a[i].carddraw();
}
}
background(255);
for(int i = 0; i < numcards; i++){
a[i].carddraw();
}
}
void waittime(int i){
int time = millis();
while(millis() - time < i){}
}
int time = millis();
while(millis() - time < i){}
}
void selectionSort(){
for(int i = 0; i < numcards; i++){
int m = i;
int j;
for(j = i+1; j < numcards; j++){
if(a[j].num < a[m].num){
m = j;
}
}
switchcards(i, m);
}
}
for(int i = 0; i < numcards; i++){
int m = i;
int j;
for(j = i+1; j < numcards; j++){
if(a[j].num < a[m].num){
m = j;
}
}
switchcards(i, m);
}
}
void switchcards(int i, int j){
int w = a[i].y;
int v = a[j].y;
for(int k = 0; k <= 200; k++){
a[i].y = w + k;
a[j].y = v + k;
draw();
waittime(5);
}
w = a[i].x;
v = a[j].x;
for(int k = 0; k <= v-w; k++){
a[i].x = w + k;
a[j].x = v - k;
draw();
waittime(5);
}
w = a[i].y;
v = a[j].y;
for(int k = 0; k <= 200; k++){
a[i].y = w - k;
a[j].y = v - k;
draw();
waittime(5);
}
}
int w = a[i].y;
int v = a[j].y;
for(int k = 0; k <= 200; k++){
a[i].y = w + k;
a[j].y = v + k;
draw();
waittime(5);
}
w = a[i].x;
v = a[j].x;
for(int k = 0; k <= v-w; k++){
a[i].x = w + k;
a[j].x = v - k;
draw();
waittime(5);
}
w = a[i].y;
v = a[j].y;
for(int k = 0; k <= 200; k++){
a[i].y = w - k;
a[j].y = v - k;
draw();
waittime(5);
}
}
class Card{
int x;
int y;
int num;
Card(int num_){
num = num_;
}
Card(int x_, int y_, int num_){
x = x_;
y = y_;
num = num_;
}
Card(Card c){
this.x = c.x;
this.y = c.y;
this.num = c.num;
}
void carddraw(){
fill(255, 180, 60);
stroke(0);
rect(x, y, 28, 50);
fill(0);
text("" + num, x-10, y);
}
}
int x;
int y;
int num;
Card(int num_){
num = num_;
}
Card(int x_, int y_, int num_){
x = x_;
y = y_;
num = num_;
}
Card(Card c){
this.x = c.x;
this.y = c.y;
this.num = c.num;
}
void carddraw(){
fill(255, 180, 60);
stroke(0);
rect(x, y, 28, 50);
fill(0);
text("" + num, x-10, y);
}
}
1