We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Move shape with mouse
Page Index Toggle Pages: 1
Move shape with mouse? (Read 799 times)
Move shape with mouse?
Dec 17th, 2008, 3:32am
 
Hi,

   I am new to processing but I am very excited to start working with it.  I was wondering how to make a shape move when someone clicks on the shape and drags the mouse.  I created a class and tried to use mouseDragged(); however, it didn't turn out how I would expect.  The reason why I am using a class is because eventually, I'll have multiple instances that I want to be able to move.  Since mouseDragged doesn't seem to work and most examples that I have found online don't use classes, should I not use classes?

Thanks for the help,
Hans

Code:

Query myQuery1;


void setup()
{
 size(800, 200);
 smooth();
 myQuery1 = new Query(color(0,0,255),width/2.0,height/5.0,100,2);
}

void draw()
{
 background(0);
   myQuery1.display();
}



class Query {
 color qc;
 float qx;
 float qy;
 int dQx;
 int dQy;

 Query(color tempQc, float tempQx, float tempQy,int tempdQx, int tempdQy) {
   qc = tempQc;
  qx = tempQx;
  qy = tempQy;
  dQx = tempdQx;
  dQy = tempdQy;
 }

 void display() {
   stroke(0);
   fill(qc);
   rectMode(RADIUS);
   rect(qx,qy,dQx,dQy);
 }
}

Re: Move shape with mouse?
Reply #1 - Dec 17th, 2008, 4:14am
 
No, you should definitely take advantage of classes.  The reason for this is because you can use inheritance and/or interface to drag different types of objects without re-implementing the algorithm for everything.

Try this.  A bit buggy, but I'm sure you can work out the rest:

Query myQuery1;

Boolean isDragging;
int dragX;
int dragY;

void setup()
{
 size(800, 500);
 smooth();
 myQuery1 = new Query(color(0,0,255),width/2.0,height/5.0,100,20);
}

void draw()
{
 background(0);
   myQuery1.display();
}



class Query {
 color qc;
 float qx;
 float qy;
 int dQx;
 int dQy;

 Query(color tempQc, float tempQx, float tempQy,int tempdQx, int tempdQy) {
   qc = tempQc;
  qx = tempQx;
  qy = tempQy;
  dQx = tempdQx;
  dQy = tempdQy;
 }

 void display() {
   background(0);
   stroke(0);
   fill(qc);
   rectMode(RADIUS);
   rect(qx,qy,dQx,dQy);
 }
 
 boolean inQuery(int x, int y){
   //btw, my code here is horrible.  Use Shape instead.  I was too lazy (sw01)
 
   if((x > qx) & x < (qx+dQx)){
     if((y > qy)  & y < (qy+dQy)){
       
       return true;
     }
   }
   return false;
 }
 
 
 
}

void mousePressed(){
 println("pressed");
 if (myQuery1.inQuery(mouseX, mouseY)){
   isDragging = true;
   dragX = (int)myQuery1.qx - mouseX;
   dragY = (int)myQuery1.qy - mouseY;
   
 }else{
   print("not in query");
   isDragging = false;
 }
}



void mouseReleased(){
 isDragging = false;
}


void mouseDragged(){
 println("dragging");
 if(isDragging){
   myQuery1.qx = mouseX + dragX;
   myQuery1.qy = mouseY + dragY;
 }
}
Re: Move shape with mouse?
Reply #2 - Dec 17th, 2008, 6:07am
 
Thanks for the help.  I fixed the detection code so that it can tell it is in the Query rectangle everytime.  However, I am still confused as to why when I try to make myQuery2 display, myQuery1 disappears.  Also, isn't the void mouseDragged() and mousePressed() code too specific to myQuery1 so that any of the other instances are not draggable?

Code:

Query myQuery1;
//Query myQuery2;

Boolean isDragging;
int dragX;
int dragY;

void setup()
{
 size(800, 500);
 smooth();
 myQuery1 = new Query(color(0,0,255),width/2.0,height/5.0,100,20);
//  myQuery2 = new Query(color(255,0,0),width/2.0,4.0*height/5.0,100,20);
}

void draw()
{
 background(0);
   myQuery1.display();
//    myQuery2.display();
}



class Query {
 color qc;
 float qx;
 float qy;
 int dQx;
 int dQy;

 Query(color tempQc, float tempQx, float tempQy,int tempdQx, int tempdQy) {
   qc = tempQc;
  qx = tempQx;
  qy = tempQy;
  dQx = tempdQx;
  dQy = tempdQy;
 }

 void display() {
   background(0);
   stroke(0);
   fill(qc);
   rectMode(RADIUS);
   rect(qx,qy,dQx,dQy);
 }
 
 boolean inQuery(int x, int y){
   //changed to x > qx-dQx and y > qy-dQy
   
   if((x > qx-dQx) & x < (qx+dQx)){
     if((y > qy-dQy)  & y < (qy+dQy)){
       
       return true;
     }
   }
   return false;
 }
 
 
 
}  

void mousePressed(){
 println("pressed");
 if (myQuery1.inQuery(mouseX, mouseY)){
   isDragging = true;
   dragX = (int)myQuery1.qx - mouseX;
   dragY = (int)myQuery1.qy - mouseY;
   
 }else{
   print("not in query");
   isDragging = false;
 }
}



void mouseReleased(){
 isDragging = false;
}


void mouseDragged(){
 println("dragging");
 if(isDragging){
   myQuery1.qx = mouseX + dragX;
   myQuery1.qy = mouseY + dragY;
 }
}
Re: Move shape with mouse?
Reply #3 - Dec 17th, 2008, 6:52am
 
Yes, you are absolutely right that the dragging implementation is too specific.  However, with a little change, you can formalize and abstract the process.

Here's how using a class helps to organize your code.  One thing to note is how you can use the concept of "one hand" by using an object variable "queryBeingDragged" instead of using a Boolean variable.  A "null" object (a special singleton object pointing to nothing) in the variable represents an "empty hand."  Furthermore, the use of an array allows you to handle any number of Queries in your code.

By the way, how do you put that box around your code in your post?

code:


//////////////////////////////////////////////

import java.util.ArrayList;

ArrayList myQueries;

Query queryBeingDragged;


int dragX;
int dragY;

void setup()
{
 queryBeingDragged = null;
 myQueries = new ArrayList();
 
 size(800, 500);
 smooth();
 
 
 myQueries.add( new Query("myQuery1",color(0,0,255),width/2.0+100,height/5.0+100,100,20));
 myQueries.add( new Query("myQuery2", color(255,0,0),width/2.0,4.0*height/5.0,100,20));
 
 
}

void draw()
{
 background(0);
 
 for(int i = 0; i < myQueries.size(); i++){
   Query myQuery1 = (Query)myQueries.get(i);
   myQuery1.display();
 }
}



class Query {
 String name;
 
 color qc;
 float qx;
 float qy;
 int dQx;
 int dQy;

 Query(String name, color tempQc, float tempQx, float tempQy,int tempdQx, int tempdQy) {
  this.name = name;
   qc = tempQc;
  qx = tempQx;
  qy = tempQy;
  dQx = tempdQx;
  dQy = tempdQy;
 }

 void display() {
   //println("displaying" + name);
   //background(0);<=== ***this is why you only saw one box
   stroke(0);
   fill(qc);
   rectMode(RADIUS);
   rect(qx,qy,dQx,dQy);
 }
 
 boolean inQuery(int x, int y){
   //changed to x > qx-dQx and y > qy-dQy
   
   if((x > qx-dQx) & x < (qx+dQx)){
     if((y > qy-dQy)  & y < (qy+dQy)){
       
       return true;
     }
   }
   return false;
 }
 
 
 
}  

void mousePressed(){
 for(int i = 0; i < myQueries.size(); i++){
    // note how I made it generic
   
   Query myQuery1 = (Query)myQueries.get(i);
   evaluateQuerySelection(myQuery1);
 
 }
 println("pressed");
 
}

void evaluateQuerySelection(Query myQuery1){
 // note how absolutely lazy I was in just pasting the old code
if (myQuery1.inQuery(mouseX, mouseY) & queryBeingDragged==null){
   //note how it will not evaluate if something is already being dragged
 
   
   dragX = (int)myQuery1.qx - mouseX;
   dragY = (int)myQuery1.qy - mouseY;
   queryBeingDragged = myQuery1;
 
}

}



void mouseReleased(){
 queryBeingDragged = null;
}


void mouseDragged(){
 if( queryBeingDragged != null){
    println("dragging" + queryBeingDragged.name);
   moveQueryByMouse(queryBeingDragged);
  }
}  



void moveQueryByMouse(Query myQuery1){
 myQuery1.qx = mouseX + dragX;
 myQuery1.qy = mouseY + dragY;


}
Re: Move shape with mouse?
Reply #4 - Dec 17th, 2008, 6:11pm
 
Thanks sw01.  I completely understood how you were able to have multiple instances of the query line and have them move.  This'll help a lot when I add more query lines later.  What about, though, if there are multiple class objects that I want to be able to move?  Basically what I want to be able to do is have two lines, a query rectangle and a hit rectangle that is connected by a connection line that is bound to specific points on the query and hit rectangle.  Here is an example of what it looks like, but there are no class objects so I can easily modulate it.  I've been rewriting it so that each line is a class object, however,  I find that it is hard to rewrite the code to include class objects and have the same visual outcome.  The reason is because there can be only 1 mousePressed();, mouseDragged, etc in the code but the class object values are local and won't be able to be placed there?

Do you have any advice on converting this over to class objects to produce the same visual outcome?  I have all the classes created on another version of the program, but I only have the query line moving (thanks to your help) but the connection line doesn't move in respect to the query line movement.  

btw, to put your code in the special code block, type [ c o d e ] at the beginning of the block and [ / c o d e ] at the end of the block, without the spaces.

thanks again for all your help.
Hans

Code:


// query coords
float qx;
float qy;
// hit coords
float hx;
float hy;
// "line" length / height for both
int dx = 100;
int dy = 2;
// offsets to define "alignment mapping" line
int connector_hit_offset = -57;
int connector_qry_offset = +32;
// booleans for clicking and dragging
boolean qover = false;
boolean hover = false;
boolean qlocked = false;
boolean hlocked = false;
// values for dragging
float qdifx = 0.0;
float qdify = 0.0;
float hdifx = 0.0;
float hdify = 0.0;


void setup()
{
 size(800, 200);
 qx = width/2.0;
 qy = height/5.0;
 hx = width/2.0;
 hy = 4.0*height/5.0;
 rectMode(RADIUS);
}

void draw()
{
 background(0);

 // Test if the cursor is over the query line
 if (mouseX > qx-dx && mouseX < qx+dx &&
     mouseY > qy-dy && mouseY < qy+dy) {
   qover = true;
   if(!qlocked) {
     stroke(255);
     fill(153);
   }
 } else {
   stroke(153);
   fill(153);
   qover = false;
 }

 // Draw the query line
 rect(qx, qy, dx, dy);
 // Draw the hit line
 rect(hx, hy, dx, dy);
 // Draw the connector line
 line(qx + connector_qry_offset,qy,hx + connector_hit_offset,hy);

// Test if the cursor is over the hit line
 if (mouseX > hx-dx && mouseX < hx+dx &&
     mouseY > hy-dy && mouseY < hy+dy) {
   hover = true;
   if(!hlocked) {
     stroke(255);
     fill(153);
   }
 } else {
   stroke(153);
   fill(153);
   hover = false;
 }

}

void mousePressed() {
 if(qover) {
   qlocked = true;
   fill(255, 255, 255);
 } else {
   qlocked = false;
 }
 qdifx = mouseX-qx;
 qdify = mouseY-qy;

 if(hover) {
   hlocked = true;
   fill(255, 255, 255);
 } else {
   hlocked = false;
 }
 hdifx = mouseX-hx;
 hdify = mouseY-hy;
}

void mouseDragged() {
 if(qlocked) {
   qx = mouseX-qdifx;
   qy = mouseY-qdify;
 }
 if(hlocked) {
   hx = mouseX-hdifx;
   hy = mouseY-hdify;
 }
}

void mouseReleased() {
 qlocked = false;
 hlocked = false;
}

 
Re: Move shape with mouse?
Reply #5 - Dec 17th, 2008, 7:05pm
 
Ah, I was hoping it'll come to that.  

This situation calls for inheritance: Both Query and Hit share very similar characteristics, so you can make a parent class that has those characteristics.  By using inheritance, the draw routine can handle both Hit and Query as a same category of objects.

Let's call it "abstractSearchObject".  Abstract because there is no real object that is both hit and a query, and can never be instantiated (you could, but it may not make sense).  Also, A query may or may not have a hit.  A line should be drawn from a Query to a Hit, but that is an extra feature that can be added to Query, but not for hit.

These statements are embodied in the code below.  It is not the most flexible code - but you can modify it to make it so (e.g., you can modify this code to have multiple hits per query using ArrayList).

Here's the code (thanks for the tip!):

Code:

import java.util.ArrayList;

ArrayList myQueries;

// Note how the "hand" no longer holds just Queries
abstractSearchObject queryBeingDragged;


int dragX;
int dragY;

void setup()
{
 queryBeingDragged = null;
 myQueries = new ArrayList();
 
 size(800, 500);
 smooth();
 
 Hit h1 = new Hit("myHit1",color(0,255, 0),width/2.0+100,height/5.0+50,100,20);
 Query q1 =  new Query("myQuery1",color(0,0,255),width/2.0+100,height/5.0+100,100,20);
 Query q2 =  new Query("myQuery2", color(255,0,0),width/2.0,4.0*height/5.0,100,20);
 

// here is where the q1 "connects" to h1
 q1.hit = h1;
 
// note how I am stuffing everything into one array
 myQueries.add(h1 );
 myQueries.add(q1 );
 myQueries.add(q2 );
 
}

void draw()
{
 background(0);
 
 for(int i = 0; i < myQueries.size(); i++){

// note how I no longer assume it is only query that is being drawn.
   abstractSearchObject myQuery1 = (abstractSearchObject)myQueries.get(i);
   myQuery1.display();
 }
}



void mousePressed(){
 for(int i = 0; i < myQueries.size(); i++){
    // note how I made it generic
   abstractSearchObject myQuery1 = (abstractSearchObject)myQueries.get(i);
   evaluateQuerySelection(myQuery1);
 }
 println("pressed");
 
}

void mouseReleased(){
 queryBeingDragged = null;
}

void mouseDragged(){
 if( queryBeingDragged != null){
    println("dragging" + queryBeingDragged.name);
   //moveQueryByMouse(queryBeingDragged);
   // note how I encapsulated the movement from directly affecting qx and qy
   queryBeingDragged.moveByMouseCoord(mouseX, mouseY);
  }
}  




void evaluateQuerySelection(abstractSearchObject myQuery1){
 if (myQuery1.inQuery(mouseX, mouseY) & queryBeingDragged==null){
   dragX = (int)myQuery1.qx - mouseX;
   dragY = (int)myQuery1.qy - mouseY;
   queryBeingDragged = myQuery1;
 }
}



// This is how to use inheritance.  Use interface instead if you want even more flexibility.
abstract class abstractSearchObject{
 String name;
 
 color qc;
 float qx;
 float qy;
 int dQx;
 int dQy;

 abstractSearchObject(String name, color tempQc, float tempQx, float tempQy,int tempdQx, int tempdQy) {
   this.name = name;
    qc = tempQc;
    qx = tempQx;
    qy = tempQy;
    dQx = tempdQx;
    dQy = tempdQy;
 }

 void display() {
   stroke(0);
   fill(qc);
   rectMode(RADIUS);
   rect(qx,qy,dQx,dQy);
 }
 
 boolean inQuery(int x, int y){
   if((x > qx-dQx) & x < (qx+dQx)){
     if((y > qy-dQy)  & y < (qy+dQy)){
       
       return true;
     }
   }
   return false;
 }
 
 void moveByMouseCoord(int mausX, int mausY){
   this.qx = mausX + dragX;
   this.qy = mausY + dragY;
 }
}


// "extends" is what makes inheritance happen.
class Query extends abstractSearchObject{
 
 Hit hit = null;
 
 Query(String name, color tempQc, float tempQx, float tempQy,int tempdQx, int tempdQy) {
// by convention, you cannot inherit the constructor.
   super( name,  tempQc,  tempQx,  tempQy, tempdQx,  tempdQy);
 }
 
 void display(){
//super is the keyword to refer to the parent class.
       super.display();  // see how I'm calling the parent class's draw routine
   if(hit != null){
   // Draw the connector line

stroke(255);
   line(this.qx,this.qy, hit.qx, hit.qy);
   
   }
 
 }
 
 
}


class Hit extends abstractSearchObject{
 
 Hit(String name, color tempQc, float tempQx, float tempQy,int tempdQx, int tempdQy) {
   super( name,  tempQc,  tempQx,  tempQy, tempdQx,  tempdQy);
 }
}










Re: Move shape with mouse?
Reply #6 - Dec 17th, 2008, 8:17pm
 
Thanks sw01.  I didn't know about abstract class creation and how to extend to other classes.  This is my first time really diving into OOP.  Thanks again for all your help.

I notice to connect the Query1 and Hit1 rectangle, you just equate them and the line is drawn in the middle connecting the two rectangles where the origin of the shape is located.  However, I may have different offsets to different regions of connection.  Here is the example of what I did to create the classes before your last post to have a static visual representation of what I am going for.  

Code:



Query myQuery1;
Hit myHit1;
Connect myConnect1;
Connect myConnect2;
Connect myConnect3;

boolean isDragging;
int dragX;
int dragY;

void setup()
{
size(800, 300);
smooth();
myQuery1 = new Query(color(0,0,255),width/2.0,height/5.0,100,4);
myHit1 = new Hit(color(255,0,0),width/2.0,4.0*height/5.0,100,4);
myConnect1 = new Connect(color(255,0,0),width/2.0,height/5.0,width/2.0,4.0*height/5.0,+32,-57);
myConnect2 = new Connect(color(255,255,50),width/2.0,height/5.0,width/2.0,4.0*height/5.0,+30,+50);
myConnect3 = new Connect(color(0,255,50),width/2.0,height/5.0,width/2.0,4.0*height/5.0,-64,+59);
}

void draw()
{
background(0);
myQuery1.display();
myHit1.display();
myConnect1.display();
myConnect2.display();
myConnect3.display();
}


//Class Creations following to allow for multiple instances of query lines, hit lines, and connector lines.
//Query Class Creation
class Query {
color qc;
float qx;
float qy;
int dQx;
int dQy;

Query(color tempQc, float tempQx, float tempQy,int tempdQx, int tempdQy) {
qc = tempQc;
qx = tempQx;
qy = tempQy;
dQx = tempdQx;
dQy = tempdQy;
}

void display() {
stroke(0);
fill(qc);
rectMode(RADIUS);
rect(qx,qy,dQx,dQy);
}

boolean inQuery(int x, int y){

if((x > qx-dQx) && x < (qx+dQx)){
if((y > qy-dQy) && y < (qy+dQy)){

return true;
}
}
return false;
}
}


//Hit Class Creation
class Hit {
color hc;
float hx;
float hy;
int dHx;
int dHy;

Hit(color tempHc, float tempHx, float tempHy,int tempdHx, int tempdHy) {
hc = tempHc;
hx = tempHx;
hy = tempHy;
dHx = tempdHx;
dHy = tempdHy;
}

void display() {
stroke(0);
fill(hc);
rectMode(RADIUS);
rect(hx,hy,dHx,dHy);
}
}

//Connector Class Creation
class Connect {
color cc;
float qx;
float qy;
float hx;
float hy;
// offsets to define "alignment mapping" line
int connector_qry_offset;
int connector_hit_offset;


Connect(color tempCc, float tempQx, float tempQy, float tempHx, float tempHy,
int tempconnector_qry_offset, int tempconnector_hit_offset) {
cc = tempCc;
qx = tempQx;
qy = tempQy;
hx = tempHx;
hy = tempHy;
connector_qry_offset = tempconnector_qry_offset;
connector_hit_offset = tempconnector_hit_offset;
}

void display() {
stroke(cc);
line(qx + connector_qry_offset,qy,hx + connector_hit_offset,hy);

}
}

void mousePressed(){
println("pressed");
if (myQuery1.inQuery(mouseX, mouseY)){
isDragging = true;
dragX = (int)myQuery1.qx - mouseX;
dragY = (int)myQuery1.qy - mouseY;

}else{
print("not in query");
isDragging = false;
}
}



void mouseReleased(){
isDragging = false;
}


void mouseDragged(){
println("dragging Query");
if(isDragging){
myQuery1.qx = mouseX + dragX;
myQuery1.qy = mouseY + dragY;
}
}

Re: Move shape with mouse?
Reply #7 - Dec 17th, 2008, 8:59pm
 
Yes, this is where oop shines.  

You've already taken the right step in creating the "Connect" object.  Now all that needs to be done is for the Query to handle that connect objects instead of directly dealing with hit.


Code:

import java.util.ArrayList;

ArrayList myQueries;

abstractSearchObject queryBeingDragged;


int dragX;
int dragY;

void setup()
{
queryBeingDragged = null;
myQueries = new ArrayList();

size(800, 500);
smooth();

Hit h1 = new Hit("myQuery1",color(0,255, 0),width/2.0+100,height/5.0+50,100,20);
Query q1 = new Query("myQuery1",color(0,0,255),width/2.0+100,height/5.0+100,100,20);
Query q2 = new Query("myQuery2", color(255,0,0),width/2.0,4.0*height/5.0,100,20);



//q1.hit = h1;
//myConnect1 = new Connect(color(255,0,0),width/2.0,height/5.0,width/2.0,4.0*height/5.0,+32 ,-57);
q1.addHitLink("myConnect1", color(255,0,0), h1,+32 ,-57);
//myConnect2 = new Connect(color(255,255,50),width/2.0,height/5.0,width/2.0,4.0*height/5.0, +30,+50);
q1.addHitLink("myConnect2", color(255,255,50), h1, +30,+50);
//myConnect3 = new Connect(color(0,255,50),width/2.0,height/5.0,width/2.0,4.0*height/5.0,-64,+59);
q1.addHitLink("myConnect1", color(0, 255,50), h1,-64,+59);


myQueries.add(h1 );
myQueries.add(q1 );
myQueries.add(q2 );

}

void draw()
{
background(0);

for(int i = 0; i < myQueries.size(); i++){
abstractSearchObject myQuery1 = (abstractSearchObject)myQueries.get(i);
myQuery1.display();
}
}



void mousePressed(){
for(int i = 0; i < myQueries.size(); i++){
// note how I made it generic
abstractSearchObject myQuery1 = (abstractSearchObject)myQueries.get(i);
evaluateQuerySelection(myQuery1);
}
println("pressed");

}

void mouseReleased(){
queryBeingDragged = null;
}

void mouseDragged(){
if( queryBeingDragged != null){
println("dragging" + queryBeingDragged.name);
queryBeingDragged.moveByMouseCoord(mouseX, mouseY);
}
}




void evaluateQuerySelection(abstractSearchObject myQuery1){
if (myQuery1.inQuery(mouseX, mouseY) & queryBeingDragged==null){
dragX = (int)myQuery1.qx - mouseX;
dragY = (int)myQuery1.qy - mouseY;
queryBeingDragged = myQuery1;
}
}



// This is how to use inheritance. Use interface instead if you want even more flexibility.
abstract class abstractSearchObject{
String name;

color qc;
float qx;
float qy;
int dQx;
int dQy;

abstractSearchObject(String name, color tempQc, float tempQx, float tempQy,int tempdQx, int tempdQy) {
this.name = name;
qc = tempQc;
qx = tempQx;
qy = tempQy;
dQx = tempdQx;
dQy = tempdQy;
}

void display() {
stroke(0);
fill(qc);
rectMode(RADIUS);
rect(qx,qy,dQx,dQy);
}

boolean inQuery(int x, int y){
if((x > qx-dQx) & x < (qx+dQx)){
if((y > qy-dQy) & y < (qy+dQy)){

return true;
}
}
return false;
}

void moveByMouseCoord(int mausX, int mausY){
this.qx = mausX + dragX;
this.qy = mausY + dragY;
}
}



class Query extends abstractSearchObject{

//Hit hit = null;
ArrayList connects = new ArrayList();


Query(String name, color tempQc, float tempQx, float tempQy,int tempdQx, int tempdQy) {
super( name, tempQc, tempQx, tempQy, tempdQx, tempdQy);
}

void display(){
super.display(); // see how I'm calling the parent class's draw routine

for(int i=0; i < connects.size(); i++){
Connect connect = (Connect)connects.get(i);
connect.display();
}
}


void addHitLink(String connectName, color cc, Hit hit, int connector_qry_offset, int connector_hit_offset ){
Connect connect = new Connect(connectName, cc, this, hit, connector_qry_offset, connector_hit_offset);
connects.add(connect);
}

}


class Hit extends abstractSearchObject{

Hit(String name, color tempQc, float tempQx, float tempQy,int tempdQx, int tempdQy) {
super( name, tempQc, tempQx, tempQy, tempdQx, tempdQy);
}

}


//Connector Class Creation
class Connect {
color cc;

String name;

Query query = null;
Hit hit = null;
//float qx;
//...
// offsets to define "alignment mapping" line
int connector_qry_offset;
int connector_hit_offset;


Connect(String name, color tempCc, Query query, Hit hit, int tempconnector_qry_offset, int tempconnector_hit_offset) {
this.name = name;
cc = tempCc;
this.query = query;
this.hit = hit;

//Note how hit and query replaces the lines below - the objects already contain the data
//qx = tempQx;
//...
connector_qry_offset = tempconnector_qry_offset;
connector_hit_offset = tempconnector_hit_offset;
}

void display() {
stroke(cc);
// this is where the data from query and hit objects are used.
line(query.qx + connector_qry_offset, query.qy, hit.qx + connector_hit_offset, hit.qy);
}
}
Page Index Toggle Pages: 1