Currently I am trying to build an application that will visualize an xml tree in Processing. I am new to Processing (and Java for that matter) and I am having some problems getting my ideas into objects. Currently I have a Node class:
class Node {
//DATA
color textColor;
color boxColor;
float xpos = width/2;
float ypos = 100;
float nodeWidth;
float nodeHeight;
boolean overBox = false;
boolean active = false;
PFont font;
//CONSTRUCTOR
Node(){
textColor = color(0);//sets text color
boxColor = color(244);//sets box color
font = createFont("Gil Sans", 16, true);
textFont(font,50);
nodeWidth = textWidth("Modernism");//INPUT TEXT
nodeHeight = textAscent();//?textDescent()?
rectMode(RADIUS);
}
void displayText(){
fill(textColor);
text("Modernism",xpos-nodeWidth/2,ypos+nodeHeight/2.3);
}
void displayBox(){
//stroke(boxColor);
noStroke();
noFill();
rect(xpos, ypos, nodeWidth/2, nodeHeight/2);
//FOR DEBUGGING OVERBOX
//stroke(135);
//point(300,200);
}
void overBox(){
if(mouseX > xpos-nodeWidth/2 && mouseX < xpos+nodeWidth/2 &&
mouseY > ypos-nodeHeight/2 && mouseY < ypos+nodeHeight/2) {
overBox = true;
}else{
overBox = false;
}
}
void clicked(){
if(active) {
//If box was already clicked, trigger response
textColor = color(0);
overBox = false;
active = false;
}
if(overBox) {
//checks to see if click happened within bounds of box, makes active
textColor = color(100);
active = true;
}
}
boolean activeCheck(){
if(active == true){
return true;
}else{
return false;
}
}
}
And then I want a connection cable drawn between the parent and children of the XML document:
class Connectors{
//DATA
color lineColor;
int lineWeight;
int lineX1 = 12;
int lineY1 = 155;
int lineX2 = 12;
int lineY2 = 475;
//CONSTRUCTOR
Connectors(){
lineColor = color(0);
lineWeight = 2;
}
//FUNCTIONALITY
void displayConnection(){
stroke(lineColor);
strokeWeight(lineWeight);
line(lineX1,lineY1,lineX2,lineY2);
}
}
This is all very rough, but I was wondering how the connections should relate to the nodes. To build the connections, it needs to know where it's parent node is located. Should the connections be a subclass of it's respective node?
You should favor aggregation over inheritance in this case. That is, have the Connector
keep a reference to two Node
s.
public class Connector {
// other members
private Node firstNode;
private Node secondNode;
public Connector(Node firstNode, Node secondNode) {
this.firstNode = firstNode;
this.secondNode = secondNode;
}
}
Now the Connector
knows about the two Node
s it connects.
The reason why you want to do this is the fact that Node
s and Connector
s don't share a lot in common. We have conventions for how we can translate OOP relationships into English.
Which makes more sense? " Connector
is a Node
," or " Connector
has a Node
"?
Now, you might want to make an abstract parent class, or even better, an interface, for both of Connector
and Node
if they do share some things in common, such as the ability to draw them.
public interface MyDrawable {
/**
* Draw this object on the "processing" canvas
* note: 2rs2ts does not know anything about the "processing" library
*/
public void draw();
}
// another file...
public class Connector implements MyDrawable {
// all of the other stuff
public void draw() {
stroke(lineColor);
// etc.
}
}
Inheritance means that you pull everything the parent class does into the child class. (That's the reason for the keyword extends
.) If there's stuff about Node
s that is totally separate from stuff about Connector
s, don't have one inherit from the other. If you do want to gain OOP abstraction, separate the commonalities into an interface or a common parent class.
I'm assuming you want build your XML parser or tree as a exercise, if not use some java library to walk the xml tree. There are a lot of libraries to do that.
Don't represent connection as a subclass of Node, in no way. A connection is not a node. You can represent your Tree as a special graph, the node is the vertex and the connection is the edges.
You have the option to make the node has other nodes, and that is a connection.
Like this
public class Node {
Node parent;
Node left;
Node right;
// Or if there is N Connections, or as we say in tree leafs
List<Node> connections;
}
Then you use an algorithm to walk the nodes and display the connections starting form the initial node (header), you could use list or hashtables to represent your tree as a collection of nodes. If you study a little of Graph theory or Algorith and Structural Data in computer cience you can do that.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.