int shape = SHAPE_RECT; // retângulo é a forma default
bem como as variáveis de classe, definindo quatro formas possíveis
para os nós:
static final int SHAPE_CIRC = 0; // ## tp & joa
static final int SHAPE_OVAL = 1; // ## tp & joa
static final int SHAPE_RECT = 2; // ## tp & joa
static final int SHAPE_ROUNDRECT = 3; // ## tp & joa
/* Associa a forma correspondente a "shapeName" ao nó,
definida no parâmetro "shapes" na chamada do applet.
Formas possíveis: circ, oval, rect e roundrect,
*/
void setShape(String shapeName) { // ## tp & joa
if (shapeName.equals("circ")) {
shape = SHAPE_CIRC;
} else if (shapeName.equals("oval")) {
shape = SHAPE_OVAL;
} else if (shapeName.equals("roundRect")) {
shape = SHAPE_ROUNDRECT;
} else {
shape = SHAPE_RECT; } // SHAPE_RECT is default
}
/* old code...
g.fillRect(x - w/2, y - h / 2, w, h);
g.setColor(Color.black);
g.drawRect(x - w/2, y - h / 2, w-1, h-1);
*/
Por:
switch (n.shape) { // ## tp & joa
case Node.SHAPE_CIRC:
g.fillOval(x - w/2, y - w / 2, w, w);
g.setColor(Color.black);
g.drawOval(x - w/2, y - w / 2, w-1, w-1);
break;
case Node.SHAPE_OVAL:
g.fillOval(x - w/2, y - h / 2, w, h);
g.setColor(Color.black);
g.drawOval(x - w/2, y - h / 2, w-1, h-1);
break;
case Node.SHAPE_ROUNDRECT:
g.fillRoundRect(x - w/2, y - h / 2, w, h, w / 3, h / 3);
g.setColor(Color.black);
g.drawRoundRect(x - w/2, y - h / 2, w-1, h-1, w / 3, h / 3);
break;
case Node.SHAPE_RECT:
default:
g.fillRect(x - w/2, y - h / 2, w, h);
g.setColor(Color.black);
g.drawRect(x - w/2, y - h / 2, w-1, h-1);
break;
}
edges = getParameter("shapes");
for (StringTokenizer t = new StringTokenizer(edges, ",") ;
t.hasMoreTokens() ; ) {
String str = t.nextToken(), forma;
int i = str.indexOf(':');
Node no = panel.nodes[panel.findNode(str.substring(0,i))];
forma = str.substring(i+1);
no.setShape(forma);
}
Chamada do applet:
<applet code="Graph.class"
width=500 height=450>
<param name="edges"
value="circulo-oval,circulo-retangulo,retangulo-normal,circulo maior-oval,
retangulo com bordas arredondadas-circulo">
<param name="colors"
value="circulo:FFA0A0,retangulo:00FF00,normal:A0A0A0,circulo maior:A0A0FF">
<param name="shapes"
value="circulo:circ,retangulo com bordas arredondadas:roundRect,oval:oval,
circulo maior:circ">
</applet>
Observe que as cores acima foram definidas por números
hexadecimais,
representando o código RGB. Utilizamos a seguinte rotina para
converter a string hexa num inteiro:
/*
Retorna um valor inteiro (int) correspondendo
ao número hexadecimal contido na string "str"
*/
static int valorHexa(String str) {
int cont = 0;
for (int i = 0; i < str.length(); i++) {
cont = cont * 16 +
Character.digit(str.charAt(i),16); }
return cont;
}
Para maiores detalhes, veja o código fonte.