Skip to main content

Java Graphs with Jung library

1 reply [Last post]
nedsana
Offline
Joined: 2013-02-26
Points: 0

Hi,

I need to create a graph view, whose nodes/vertexes are JPanels with some GUI items(JButtons, JTextFields, etc).

I succeeded in creating one, unfortunately the items in the JPanel vertexes are not active. And I have no idea how to make them active. I was trying to fix this for the last few weeks, but I had no success.

I will place my code below. Please I desperately need some assistance with this one.
Many thanks in advance.

import java.awt.AWTEvent;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import javax.accessibility.AccessibleComponent;
import javax.accessibility.AccessibleContext;
import javax.swing.BorderFactory;
import javax.swing.CellRendererPane;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.OverlayLayout;
import org.apache.commons.collections15.Transformer;
import edu.uci.ics.jung.algorithms.layout.CircleLayout;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.graph.DirectedOrderedSparseMultigraph;
import edu.uci.ics.jung.graph.DirectedSparseGraph;
import edu.uci.ics.jung.visualization.RenderContext;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
import edu.uci.ics.jung.visualization.control.GraphMouseListener;
import edu.uci.ics.jung.visualization.control.ModalGraphMouse;
import edu.uci.ics.jung.visualization.control.PluggableGraphMouse;
import edu.uci.ics.jung.visualization.picking.PickedState;
import edu.uci.ics.jung.visualization.renderers.Renderer;
import edu.uci.ics.jung.visualization.transform.shape.GraphicsDecorator;
class MyActionListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
System.out.println("ActionListener: Item " + e.getID() + " was " + e.getActionCommand());
}
}
class MyItemListener implements ItemListener
{
@Override
public void itemStateChanged(ItemEvent e)
{
// TODO Auto-generated method stub
Object subject = e.getItem();
if(subject instanceof JPanel)
{
JPanel panel = (JPanel) subject;
System.out.println("JPanel " + e.getItem() + " is now selected");
panel.requestFocus();
int i = 0;
Component comp = null;
for(i=0; i
{
panel.addMouseListener(new MyMouseListener());
System.out.println("Panel conatins " + panel.getComponentCount() + " item " + i + " : " + panel.getComponent(i).getClass().getName());
comp = panel.getComponent(i);
comp.setFocusable(true);
comp.requestFocus();
comp.requestFocusInWindow();
comp.addMouseListener(new MyMouseListener());
comp.dispatchEvent(e);
}
}
else
{
System.out.println("JPanel not selected!");
}
}
}
class MyMouseListener implements MouseListener
{
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("mouseEntered: Item " + e.getID());
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("mouseExited: Item " + e.getID());
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("mousePressed: Item " + e.getID());
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("mouseReleased: Item " + e.getID());
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("mouseClicked: Item " + e.getID());
}
}
public class GraphPanel_new extends Container
{
static final long serialVersionUID = 420001L;
DirectedOrderedSparseMultigraph graph = null;
VisualizationViewer vv = null;
PickedState pickedState = null;
public GraphPanel_new(ArrayList vertex_list)
{
try
{
graph = new DirectedOrderedSparseMultigraph();
int i = 0;
for(i=0; i {
graph.addVertex(vertex_list.get(i));
graph.addEdge(i, vertex_list.get(i), vertex_list.get(i+1));
}
vv = new VisualizationViewer(new CircleLayout(graph), new Dimension(400, 400));
vv.getRenderer().setVertexRenderer(new MyRenderer());
vv.setLayout(new OverlayLayout(vv));
// The vertex pick listener
pickedState = vv.getPickedVertexState();
pickedState.addItemListener(new MyItemListener());
// The following code adds capability for mouse picking of
// vertices/edges. Vertices can even be moved!
final DefaultModalGraphMouse graphMouse = new DefaultModalGraphMouse();
vv.setGraphMouse(graphMouse);
graphMouse.setMode(ModalGraphMouse.Mode.PICKING);
vv.getRenderContext().setVertexShapeTransformer(new ShapeTransformer(new Dimension(100,80)));
vv.addGraphMouseListener(new MyGraphMouseListener());
// pre/post render
vv.addPreRenderPaintable(new VisualizationViewer.Paintable()
{
@Override
public void paint(Graphics g)
{
// TODO Auto-generated method stub
System.out.println("PRE RENDER");
System.out.println("vv.getComponentCount()= " + vv.getComponentCount());
}
@Override
public boolean useTransform()
{
// TODO Auto-generated method stub
return false;
}
});
vv.addPostRenderPaintable(new VisualizationViewer.Paintable()
{
public void paint(Graphics g)
{
System.out.println("POST RENDER");
System.out.println("vv.getComponentCount()= " + vv.getComponentCount());
Component comp = vv.getComponent(0);
System.out.println(comp);
System.out.println(comp.getClass());
}
public boolean useTransform()
{
return false;
}
});
}
catch (Exception e)
{
System.err.println("Failed to construct graph!\n");
System.err.println("Caught Exception: " + e.getMessage());
}
}
/*Attach the graph panel/container to a specified frame*/
public void attach_to_frame(JFrame frame)
{
frame.setContentPane(vv);
}
static class MyGraphMouseListener implements GraphMouseListener
{
public void graphClicked(V v, MouseEvent me)
{
JPanel panel = (JPanel)v;
System.out.println("Vertex "+v+" was clicked at ("+me.getX()+","+me.getY()+")");
int i = 0;
Component comp = null;
for(i=0; i
{
panel.addMouseListener(new MyMouseListener());
System.out.println("Panel conatins " + panel.getComponentCount() + " item " + i + " : " + panel.getComponent(i).getClass().getName());
comp = panel.getComponent(i);
comp.setFocusable(true);
comp.requestFocus();
comp.requestFocusInWindow();
comp.addMouseListener(new MyMouseListener());
comp.dispatchEvent(me);
comp.setEnabled(true);
}
}
public void graphPressed(V v, MouseEvent me) {
System.out.println("Vertex "+v+" was pressed at ("+me.getX()+","+me.getY()+")");
}
public void graphReleased(V v, MouseEvent me) {
System.out.println("Vertex "+v+" was released at ("+me.getX()+","+me.getY()+")");
}
}
static class ShapeTransformer implements Transformer
{
Dimension d = null;
public ShapeTransformer(Dimension d_in)
{
d = d_in;
}
public Shape transform(JPanel panel)
{
System.out.println("Shape transform");
Rectangle form = new Rectangle(d);
panel.setEnabled(true);
return form;
}
}
static class MyRenderer extends JPanel implements Renderer.Vertex
{
static final long serialVersionUID = 420000L;
@Override
public void paintVertex(RenderContext rc,Layout layout, JPanel vertex)
{
System.out.println("MY RENDER");
try
{
GraphicsDecorator graphicsContext = rc.getGraphicsContext();
Point2D center = layout.transform(vertex);
Dimension size = vertex.getPreferredSize();
graphicsContext.draw(vertex, rc.getRendererPane(), (int)center.getX(), (int)center.getY(), size.width, size.height, true);
}
catch (Exception e)
{
System.err.println("Failed to render images!\n");
System.err.println("Caught Exception: " + e.getMessage());
}
}
}
public static void main(String[] args)
{
/*Create the window*/
int i = 0;
ArrayList vertex_list = new ArrayList();
for(i=0; i<3; i++)
{
JPanel sv = new JPanel();
sv.setName("P"+i);
sv.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
sv.setBackground(Color.GREEN);
sv.setPreferredSize(new Dimension(100, 80));
sv.setFocusable(true);
sv.requestFocusInWindow();
sv.addMouseListener(new MyMouseListener());
JButton b = new JButton("Button"+i);
sv.add(b);
b.requestFocus();
b.addActionListener(new MyActionListener());
b.addItemListener(new MyItemListener());
JTextField t = new JTextField(20);
sv.add(t);
t.addActionListener(new MyActionListener());
vertex_list.add(sv);
}
JFrame frame = new JFrame("FRAME");
GraphPanel_new g = new GraphPanel_new(vertex_list);
g.attach_to_frame(frame);
frame.getContentPane().setPreferredSize(new Dimension(640, 480));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}/*2*/

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
nedsana
Offline
Joined: 2013-02-26
Points: 0

Anybody?