2 つの質問があります。1) MySQL データベースからデータを取得するネットワークを開発しました。LayoutChoose
コンボボックスを使って試していました。しかし、プログラムの実行中に、次の出力が得られました。エッジが乗算されています。コーディングを同封しました。では、どこを修正すればよいのでしょうか。 .
2) コンボ ボックスをもう 1 つ追加する必要があります。MySQL データベースからアイテムのリストを取得し、コンボ ボックスに表示する必要があります。その値に応じて、グラフを変更する必要があります。これで私を助けてくれませんか。
package snp1;
import java.applet.Applet;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.GridLayout;
import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;
import org.apache.commons.collections15.Transformer;
import org.apache.commons.collections15.functors.ConstantTransformer;
import snp1.VertexCollapseDemoWithLayouts.LayoutChooser;
import com.jshapes.g2d.shapes.basic.Plaque;
import com.jshapes.g2d.shapes.basic.Triangle;
import com.jshapes.g2d.shapes.stars.Star;
import edu.uci.ics.jung.algorithms.cluster.WeakComponentClusterer;
import edu.uci.ics.jung.algorithms.layout.AggregateLayout;
import edu.uci.ics.jung.algorithms.layout.CircleLayout;
import edu.uci.ics.jung.algorithms.layout.FRLayout;
import edu.uci.ics.jung.algorithms.layout.ISOMLayout;
import edu.uci.ics.jung.algorithms.layout.KKLayout;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.algorithms.layout.SpringLayout;
import edu.uci.ics.jung.algorithms.layout.SpringLayout2;
import edu.uci.ics.jung.graph.DirectedSparseGraph;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.util.Pair;
import edu.uci.ics.jung.graph.util.TestGraphs;
import edu.uci.ics.jung.visualization.DefaultVisualizationModel;
import edu.uci.ics.jung.visualization.RenderContext;
import edu.uci.ics.jung.visualization.VisualizationModel;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.annotations.AnnotatingGraphMousePlugin;
import edu.uci.ics.jung.visualization.annotations.AnnotatingModalGraphMouse;
import edu.uci.ics.jung.visualization.control.AbstractModalGraphMouse;
import edu.uci.ics.jung.visualization.control.AbstractPopupGraphMousePlugin;
import edu.uci.ics.jung.visualization.control.CrossoverScalingControl;
import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
import edu.uci.ics.jung.visualization.control.GraphMousePlugin;
import edu.uci.ics.jung.visualization.control.ModalGraphMouse;
import edu.uci.ics.jung.visualization.control.PickingGraphMousePlugin;
import edu.uci.ics.jung.visualization.control.PluggableGraphMouse;
import edu.uci.ics.jung.visualization.control.ScalingControl;
import edu.uci.ics.jung.visualization.control.ScalingGraphMousePlugin;
import edu.uci.ics.jung.visualization.control.TranslatingGraphMousePlugin;
import edu.uci.ics.jung.visualization.control.ViewScalingControl;
import edu.uci.ics.jung.visualization.decorators.PickableVertexPaintTransformer;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
import edu.uci.ics.jung.visualization.layout.LayoutTransition;
import edu.uci.ics.jung.visualization.picking.PickedState;
import edu.uci.ics.jung.visualization.subLayout.GraphCollapser;
import edu.uci.ics.jung.visualization.util.Animator;
class Edge {
private final String name;
Edge(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}
public class snp_interaction extends Applet {
static Connection connection = null;
Graph g;
Class[] layoutClasses = new Class[] { CircleLayout.class,
SpringLayout.class, FRLayout.class, KKLayout.class };
static Graph collapsedGraph;
VisualizationViewer vv;
static Layout layout;
static String name1[] = new String[1000];
static String name2[] = new String[1000];
static String name3[] = new String[1000];
static String name4[] = new String[1000];
static String name5[] = new String[1000];
static String PMID[] = new String[1000];
static String na[] = new String[1000];
static String species[] = new String[1000];
static int f = 0;
static int i = 0;
static int j = 0;
static int k = 0;
static int l = 0;
static int z = 0;
public snp_interaction() {
connection = SNP_Conn.getConnection();
g =TestGraphs.getOneComponentGraph();
// collapsedGraph = g;
// collapser = new GraphCollapser(g);
layout = new FRLayout(g);
Dimension preferredSize = new Dimension(400, 400);
final VisualizationModel visualizationModel = new DefaultVisualizationModel(
layout, preferredSize);
vv = new VisualizationViewer(visualizationModel, preferredSize);
try {
String query = "SELECT * FROM interr";
Statement pest = connection.createStatement();
ResultSet rs = pest.executeQuery(query);
while (rs.next()) {
name1[i] = rs.getString("Mole1");
g.addVertex(name1[i]);
System.out.println(name1[i]);
// name2[j] = rs.getString("");
// System.out.println(name2[j]);
// name5[z] = rs.getString("MoleculeType");
// System.out.println(name5[z]);
name3[k] = rs.getString("Mole2");
// System.out.println(name3[k]);
name4[l] = rs.getString("Intercation_Type");
if (name3[k] != null) {
g.addVertex(name3[k]);
}
i++;
j++;
k++;
l++;
}
for (k = 0; k < 1000; k++) {
l = k;
i = k;
g.addEdge(new Edge(name4[l]), name1[i], name3[k]);
}
rs.close();
pest.close();
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, e1);
}
PickedState<String> picked_state = vv.getPickedVertexState();
RenderContext<String, Edge> rc = vv.getRenderContext();
AnnotatingGraphMousePlugin<String, Edge> annotatingPlugin = new AnnotatingGraphMousePlugin<String, Edge>(
rc);
final AnnotatingModalGraphMouse<String, Edge> graphMouse = new AnnotatingModalGraphMouse<String, Edge>(
rc, annotatingPlugin);
vv.setGraphMouse(graphMouse);
vv.addKeyListener(graphMouse.getModeKeyListener());
DefaultModalGraphMouse<?, ?> gm = new DefaultModalGraphMouse<Object, Object>();
gm.setMode(ModalGraphMouse.Mode.EDITING);
gm.setMode(ModalGraphMouse.Mode.TRANSFORMING);
vv.setGraphMouse(gm);
vv.getRenderContext().setVertexFillPaintTransformer(
new PickableVertexPaintTransformer<String>(vv
.getPickedVertexState(), Color.red, Color.yellow));
vv.setVertexToolTipTransformer(new ToStringLabeller<String>());
vv.setVertexToolTipTransformer(new Transformer<String, String>() {
public String transform(String v) {
try {
String bb = "SELECT * FROM interr";
Statement pest = connection.createStatement();
ResultSet v1 = pest.executeQuery(bb);
while (v1.next())
if (v.toString().equals(v1.getString("Mole1")))
na[i] = v1.getString("Pubchem_ID");
System.out.println(na[i]);
v = na[i].toString();
return "PUBMED:" + v.toString();
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, e1);
}
return null;
}
});
vv.setVertexToolTipTransformer(new Transformer<String, String>() {
public String transform(String v) {
try {
String bb = "SELECT * FROM interr";
Statement pest = connection.createStatement();
ResultSet v1 = pest.executeQuery(bb);
while (v1.next())
if (v.toString().equals(v1.getString("Mole2")))
na[i] = v1.getString("Pubchem_ID");
System.out.println(na[i]);
v = na[i].toString();
return "PUBMED:" + v.toString();
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, e1);
}
return null;
}
});
vv.setEdgeToolTipTransformer(new Transformer<Edge, String>() {
public String transform(Edge e) {
return "Interaction:" + e.toString();
}
});
vv.addKeyListener(gm.getModeKeyListener());
vv.setGraphMouse(gm);
float dash[] = { 10.0f };
Transformer<Edge, Paint> edgePaint = new Transformer<Edge, Paint>() {
public Paint transform(Edge s) {
Color customColor = new Color(10, 10, 255);
if (s.toString().equals("Bind"))
return Color.BLACK;
if (s.toString().equals("association"))
return Color.green;
if (s.toString().equals("physical association"))
return Color.blue;
if (s.toString().equals("acetylation reaction"))
return Color.darkGray;
if (s.toString().equals("cleavage reaction"))
return Color.magenta;
if (s.toString().equals("colocalization"))
return Color.pink;
if (s.toString().equals("direct interaction"))
return Color.ORANGE;
else
return null;
}
};
Transformer<String, Paint> vertexPaint = new Transformer<String, Paint>() {
public Paint transform(String i) {
try {
String bb = "SELECT * FROM interr";
Statement pest = connection.createStatement();
ResultSet rs = pest.executeQuery(bb);
while (rs.next())
if (i.toString().equals(rs.getString("Mole1")))
return Color.red;
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, e1);
}
return Color.green;
}
};
Transformer<String, Shape> shape = new Transformer<String, Shape>() {
public Shape transform(String i) {
Rectangle bounds1 = new Rectangle(-15, -15, 20, 20);
Triangle shape1 = new Triangle(bounds1, Triangle.UP);
Ellipse2D circle1 = new Ellipse2D.Double(-15, -15, 10, 10);
Rectangle2D rect = new Rectangle2D.Double(-15, -15, 20, 20);
Rectangle bounds = new Rectangle(-15, -15, 20, 20);
double tipSize = 25;
String style = Star.STAR_8_POINTS;
Star star = new Star(bounds, tipSize, style);
RoundRectangle2D round = new RoundRectangle2D.Double(-15, -15,
30, 30, 30, 20);
Rectangle bound = new Rectangle(-15, -15, 20, 20);
double cornerSize = 30;
Plaque shape = new Plaque(bound, cornerSize);
FontMetrics fm = vv.getFontMetrics(vv.getFont());
int width = fm.stringWidth(i);
return circle1;
}
};
vv.getRenderContext().setVertexFillPaintTransformer(vertexPaint);
// vv1.getRenderContext().setVertexFillPaintTransformer(vertexPaint);
vv.getRenderContext().setEdgeStrokeTransformer(edgeStrokeTransformer);
vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
vv.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);
JLabel lblGeneName = new JLabel("Disease Name");
final ScalingControl scaler = new ViewScalingControl();
JButton plus = new JButton("+");
plus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
scaler.scale(vv, 1.1f, vv.getCenter());
}
});
final ScalingControl scaler1 = new ViewScalingControl();
JButton minus = new JButton("-");
minus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
scaler1.scale(vv, 1 / 1.1f, vv.getCenter());
}
});
JButton button = new JButton("Show network");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
vv.show();
}
});
Class[] combos = getCombos();
final JComboBox jcb = new JComboBox(combos);
JComboBox pathway = new JComboBox();
// pathway.setSelectedItem("Choose the pathway");
// use a renderer to shorten the layout name presentation
jcb.setRenderer(new DefaultListCellRenderer() {
public Component getListCellRendererComponent(JList list,
Object value, int index, boolean isSelected,
boolean cellHasFocus) {
String valueString = value.toString();
valueString = valueString.substring(valueString
.lastIndexOf('.') + 1);
return super.getListCellRendererComponent(list, valueString,
index, isSelected, cellHasFocus);
}
});
jcb.addActionListener(new LayoutChooser(jcb, vv));
jcb.setSelectedItem(FRLayout.class);
JLabel label1 = new JLabel("Select");
JFrame frame = new JFrame(" Breast Neoplasms");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(vv);
frame.pack();
frame.setVisible(true);
Container content = frame.getContentPane();
JPanel panel = new JPanel(new BorderLayout());
// panel.add(vv);
content.add(panel);
JMenuBar menuBar = new JMenuBar();
JMenu modeMenu = gm.getModeMenu();
modeMenu.setText("Options");
modeMenu.setIcon(null);
modeMenu.setPreferredSize(new Dimension(60, 20));
JPanel p = new JPanel();
p.add(label1);
p.add(button);
p.add(plus);
p.add(minus);
// p.add(compressEdges);
p.add(jcb);
// p.add(pathway);
frame.getContentPane().add(p, BorderLayout.SOUTH);
frame.setSize(600, 600);
frame.show();
menuBar.add(modeMenu);
frame.setJMenuBar(menuBar);
}
public static class LayoutChooser implements ActionListener {
private final JComboBox jcb;
private final VisualizationViewer vv;
private LayoutChooser(JComboBox jcb, VisualizationViewer vv) {
super();
this.jcb = jcb;
this.vv = vv;
}
Class[] layoutClasses = new Class[] { CircleLayout.class,
SpringLayout.class, FRLayout.class, KKLayout.class };
public void actionPerformed(ActionEvent arg0) {
Object[] constructorArgs = { collapsedGraph };
Class<? extends Layout> layoutC = (Class<? extends Layout>) jcb
.getSelectedItem();
Class lay = layoutC;
try {
Constructor<? extends Layout> constructor = layoutC
.getConstructor(new Class[] { Graph.class });
Object o = constructor.newInstance(constructorArgs);
Layout l = (Layout) o;
l.setInitializer(vv.getGraphLayout());
l.setSize(vv.getSize());
layout = l;
LayoutTransition lt = new LayoutTransition(vv,
vv.getGraphLayout(), l);
Animator animator = new Animator(lt);
animator.start();
vv.getRenderContext().getMultiLayerTransformer()
.setToIdentity();
vv.repaint();
} catch (Exception e) {
e.printStackTrace();
}
}
}
@SuppressWarnings("unchecked")
private Class<? extends Layout>[] getCombos() {
List<Class<? extends Layout>> layouts = new ArrayList<Class<? extends Layout>>();
layouts.add(KKLayout.class);
layouts.add(FRLayout.class);
layouts.add(CircleLayout.class);
layouts.add(SpringLayout.class);
layouts.add(SpringLayout2.class);
layouts.add(ISOMLayout.class);
return layouts.toArray(new Class[0]);
}
public static void main(String[] args) throws IOException {
JFrame frame = new JFrame(" Breast Neoplasms");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new snp_interaction());
frame.pack();
frame.setVisible(true);
}
}