目前我正在嘗試構建一個應用程序,它將在Processing中可視化xml樹。我對Processing很陌生(對於Java來說也是如此),並且在將我的想法變爲對象時遇到了一些問題。目前,我有一個Node類:處理中的OOP,我應該如何構建我的類?
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;
}
}
}
然後,我要的XML文檔的父母和孩子之間繪製的連接電纜:
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);
}
}
這是非常粗糙的,但我想知道連接應該如何與節點相關聯。爲了建立連接,它需要知道父節點所在的位置。連接應該是它各自節點的子類嗎?
子類=>「是一個」,就像「Car」類可能是「Vehicule」類的一個子類。 – 2014-02-21 19:56:12