package graphmatcher.gui;
import graphmatcher.graph.Edge;
import graphmatcher.graph.Graph;
import graphmatcher.graph.Vertex;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import javax.swing.JPanel;
public class GraphPanel extends JPanel {
private Graph g = null;
private Point centerOfMass;
private int min_x, min_y, max_x, max_y;
private Rectangle rectangle;
private Color nodeColor;
private Color edgeColor;
public GraphPanel(Color nodeColor, Color edgeColor) {
super();
this.nodeColor = nodeColor;
this.edgeColor = edgeColor;
}
public void paint(Graphics gra) {
Graphics2D gr = (Graphics2D) gra;
gr.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Dimension d = getSize();
gr.setColor(Color.white);
gr.fillRect(0, 0, d.width, d.height);
if (g == null)
return;
double ratio1 = (double) (d.height - 10) / (double) rectangle.height;
double ratio2 = (double) (d.width - 10) / (double) rectangle.width;
double ratio = Math.min(ratio1, ratio2);
gr.scale(ratio, ratio);
gr.translate(5d, 5d);
gr.translate((double) -rectangle.x, (double) rectangle.y + rectangle.height);
gr.setColor(edgeColor);
for (int i = 0; i < g.edges().length; i++) {
Edge e = g.edges()[i];
Vertex p = g.vertices()[e.vertex1];
Vertex q = g.vertices()[e.vertex2];
gr.drawLine(p.x, -p.y, q.x, -q.y);
// gr.drawLine(p.iX, d.height - p.iY, q.iX, d.height - q.iY);
}
for (int i = 0; i < g.vertices().length; i++) {
Vertex v = g.vertices()[i];
// System.out.println(v.iX+"-"+v.iY+" <-> "+d+" - "+ratio);
gr.setColor(nodeColor);
int x = v.x;
int y = v.y;
// System.out.println(x+"-"+y);
gr.fillOval(x, -y, 3, 3);
gr.setColor(Color.BLACK);
gr.drawString("" + i, x + 2, -y + 2);
// gr.fillOval(v.iX - 3, d.height - v.iY - 3, 7, 7);
// gr.setColor(Color.BLACK);
// gr.drawString(""+i, v.iX+2, d.height - v.iY+2);
}
gr.setColor(Color.BLACK);
gr.drawLine(centerOfMass.x - 3, -centerOfMass.y - 3, centerOfMass.x + 3, -centerOfMass.y + 3);
gr.drawLine(centerOfMass.x + 3, -centerOfMass.y - 3, centerOfMass.x - 3, -centerOfMass.y + 3);
}
public Graph getGraph() {
return g;
}
public void setGraph(Graph g) {
this.g = g;
centerOfMass = g.getCenter();
updateGraphMeasures();
repaint();
}
private void updateGraphMeasures() {
rectangle = g.getRectangle();
min_x = Integer.MAX_VALUE;
min_y = Integer.MAX_VALUE;
max_x = 0;
max_y = 0;
for (int i = 0; i < g.vertices().length; i++) {
if (min_x > g.vertices()[i].x)
min_x = g.vertices()[i].x;
if (min_y > g.vertices()[i].y)
min_y = g.vertices()[i].y;
if (max_x < g.vertices()[i].x)
max_x = g.vertices()[i].x;
if (max_y < g.vertices()[i].y)
max_y = g.vertices()[i].y;
}
}
public void setEdgeColor(Color edgeColor) {
this.edgeColor = edgeColor;
repaint();
}
public void setNodeColor(Color nodeColor) {
this.nodeColor = nodeColor;
repaint();
}
}