// Medusa is a graph viewer which allows interactive editing of networks
// (edges and nodes) and also connects to databases.
//
// Copyright (C) 2006 Sean Hooper
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or (at
// your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
// Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the
// Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330,
// Boston, MA 02111-1307 USA
/*
* ExamplePanel.java
*
* Created on October 24, 2006, 6:08 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package medusa.example;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import medusa.MedusaSettings;
import medusa.display.BasicGraphPanel;
import medusa.graph.Edge;
import medusa.graph.Node;
/**
* This is an example of how to extend the BasicGraphPanel
* to draw nodes and edges any way you want. Refer to the Node and
* Edge documentation to see which parameters you can use. In
* this example, paintNode is extended and uses Node.getColor()
* to create a button-like effect.
* @author hooper
*/
public class ExamplePanel extends BasicGraphPanel{
BufferedImage red;
NodeIcon ni;
/**
* Constructor
* @param ms default settings
*/
public ExamplePanel(MedusaSettings ms) {
super(ms);
ni=new NodeIcon();
red=ni.createGlossyButton(Color.RED,10);
}
/**
* Overridden paintEdge method.
* This version just draws a line. You can try paths,
* icons or anything you like.
* @param g graphics2d context
* @param e the current edge
*/
public void paintEdge(Graphics2D g, Edge e) {
g.setPaint(basicEdgeColor);
paintPath(g,e);
}
private void paintPath(Graphics2D g2d, Edge e){
// get the values
Node from =graph.getNode(e.getFromName());
Node to = graph.getNode(e.n2);
int x1 = (int)from.getX();
int y1 = (int)from.getY();
int x2 = (int)to.getX();
int y2 = (int)to.getY();
// just draw the line
g2d.drawLine(x1,y1,x2,y2);
}
/**
* Overridden paintNode method.
* Paints two ellipses with a gradient based on the node
* color. The button effect is created by using opposite gradients.
* If you want the dragged node to be painted differently, override
* paintPick. If you want the fixed nodes to be painted
* differently, check for Node.isFixed.
* @param g Graphics2D
* @param n Node n
*/
public void paintNode(Graphics2D g, Node n) {
paintNodeImage(g,n);
}
private void paintNodeImage(Graphics2D g, Node n){
int x=(int) n.getX() - (int)(nodeSize/2);
int y=(int) n.getY() - (int)(nodeSize/2);
g.drawImage(red,null,x,y);
}
private void paintShadedNode(Graphics2D g, Node n){
Color c=n.getColor();
int nodeSize=16;
int border=2;
int x=(int) n.getX() - (int)(nodeSize/2);
int y=(int) n.getY() - (int)(nodeSize/2);
float fx=(float)x;
float fy=(float)y;
Point2D p1=new Point2D.Float(x,y);
Point2D p2=new Point2D.Float(x+nodeSize,y+nodeSize);
GradientPaint outside=
new GradientPaint(p1,c,
p2,Color.white);
GradientPaint inside=
new GradientPaint(p1,Color.white,
p2,c);
Shape outsideShape =
new Ellipse2D.Double(x, y, nodeSize, nodeSize);
Shape insideShape =
new Ellipse2D.Double(x+border, y+border,
nodeSize-2*border, nodeSize-2*border);
g.setPaint(outside);
g.fill(outsideShape);
g.setPaint(inside);
g.fill(insideShape);
g.setPaint(Color.black);
if (label)
g.drawString(n.getLabel(),x-2,y-2);
}
}
Ads help maintain this website.