00001
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 import javax.swing.*;
00036 import java.awt.*;
00037 import javax.swing.border.*;
00038
00039
00040 public class LegendPanel extends JPanel
00041 {
00042 private SenseGui gui;
00043 private TopologyPanel tp;
00044 private JPanel app1_p;
00045 private JPanel app2_p;
00046 private JPanel app3_p;
00047 private JPanel app4_p;
00048 private JPanel app_p;
00049 private int width, height;
00050 private Font label_font;
00051 public int font_size;
00052
00053
00054 public LegendPanel(SenseGui g, int w, int h) {
00055 super();
00056 gui = g;
00057 tp = gui.view_panel;
00058 width = w;
00059 height = h;
00060
00061 setOpaque(false);
00062 setPreferredSize(new Dimension(width, height));
00063 setLayout(new BorderLayout());
00064 setBorder(BorderFactory.createTitledBorder("Legend"));
00065
00066 JPanel p = new JPanel();
00067 p.setOpaque(false);
00068 p.setLayout(new BorderLayout(4, 0));
00069
00070 JPanel app_p = new JPanel();
00071 app_p.setOpaque(false);
00072 app_p.setLayout(new BorderLayout(6, 0));
00073
00074 app1_p = new JPanel();
00075 app1_p.setOpaque(false);
00076 app1_p.setLayout(new BoxLayout(app1_p, BoxLayout.Y_AXIS));
00077
00078 app2_p = new JPanel();
00079 app2_p.setOpaque(false);
00080 app2_p.setLayout(new BoxLayout(app2_p, BoxLayout.Y_AXIS));
00081 app_p.add(app2_p, BorderLayout.WEST);
00082
00083 app3_p = new JPanel();
00084 app3_p.setOpaque(false);
00085 app3_p.setLayout(new BoxLayout(app3_p, BoxLayout.Y_AXIS));
00086 app_p.add(app3_p, BorderLayout.CENTER);
00087
00088 app4_p = new JPanel();
00089 app4_p.setOpaque(false);
00090 app4_p.setLayout(new BoxLayout(app4_p, BoxLayout.Y_AXIS));
00091 app_p.add(app4_p, BorderLayout.EAST);
00092 p.add(app_p, BorderLayout.CENTER);
00093
00094 JPanel top_p = new JPanel();
00095 top_p.setLayout(new BorderLayout());
00096 top_p.setOpaque(false);
00097 top_p.setBorder(BorderFactory.createLineBorder(Color.gray));
00098 JLabel sample = new JLabel("Sample");
00099 Font as_font = new Font(sample.getFont().getName(), Font.PLAIN,
00100 sample.getFont().getSize()-1);
00101 label_font = new Font(sample.getFont().getName(), Font.PLAIN,
00102 sample.getFont().getSize()-3);
00103 font_size = label_font.getSize();
00104 int as_font_size = as_font.getSize();
00105
00106 JLabel dummy = new JLabel(" ");
00107 dummy.setFont(new Font(label_font.getName(), Font.PLAIN,
00108 label_font.getSize()/2));
00109 JLabel arc = new JLabel("connections",
00110 (Icon)(new LegendIcon(gui.view_panel.arc_color,
00111 font_size*2, font_size*4)), JLabel.RIGHT);
00112 arc.setFont(as_font);
00113 arc.setIconTextGap(0);
00114 arc.setVerticalTextPosition(JLabel.TOP);
00115 JLabel txsphere = new JLabel("transmit radii",
00116 (Icon)(new LegendIcon(gui.view_panel.sphere_color,
00117 font_size*2, font_size*4)), JLabel.RIGHT);
00118 txsphere.setFont(as_font);
00119 txsphere.setIconTextGap(0);
00120 txsphere.setVerticalTextPosition(JLabel.TOP);
00121 top_p.add(dummy, BorderLayout.NORTH);
00122 top_p.add(arc, BorderLayout.CENTER);
00123 top_p.add(txsphere, BorderLayout.SOUTH);
00124
00125 JPanel west_p = new JPanel();
00126 west_p.setLayout(new BorderLayout(0, 0));
00127 west_p.setOpaque(false);
00128 west_p.add(top_p, BorderLayout.NORTH);
00129 west_p.add(app1_p, BorderLayout.SOUTH);
00130 p.add(west_p, BorderLayout.WEST);
00131
00132 add(p, BorderLayout.CENTER);
00133 setSize(getPreferredSize());
00134 }
00135
00136
00137 public void start() {
00138 app1_p.removeAll();
00139 app2_p.removeAll();
00140 app3_p.removeAll();
00141 app4_p.removeAll();
00142 int how_many = gui.topology.getGroups().size();
00143 for (int i=0; i<how_many; i++) {
00144
00145
00146
00147
00148 JLabel icon = new JLabel((Icon)(new LegendIcon(
00149 gui.view_panel.group_colors[i], 3*font_size/2, font_size*3)));
00150
00151
00152 JPanel p = new JPanel();
00153 p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
00154
00155 p.add(icon);
00156
00157
00158 if (how_many > 12) {
00159 if (i < 2*how_many/11)
00160 app1_p.add(p);
00161 else if (i < 5*how_many/11)
00162 app2_p.add(p);
00163 else if (i < 8*how_many/11)
00164 app3_p.add(p);
00165 else
00166 app4_p.add(p);
00167 } else
00168 app3_p.add(p);
00169 }
00170 setVisible(true);
00171 }
00172
00173 public Dimension getMinimumSize() {
00174 return getPreferredSize();
00175 }
00176 public Dimension getMaximumSize() {
00177 return getPreferredSize();
00178 }
00179 public Dimension getPreferredSize() {
00180 return new Dimension(this.width, this.height);
00181 }
00182
00183 protected class LegendIcon implements Icon {
00184 private Color color;
00185 private int height;
00186 private int width;
00187
00188 public LegendIcon(Color c, int h, int w) {
00189 color = c;
00190 height = h;
00191 width = w;
00192 }
00193
00194 public void paintIcon(Component c, Graphics g, int x, int y) {
00195 g.setColor(color);
00196 g.fillRect(x - (width/2) - 1, y - (height/2) - 1, width+1, height+1);
00197 }
00198
00199 public int getIconWidth() {
00200 return width;
00201 }
00202 public int getIconHeight() {
00203 return height;
00204 }
00205 }
00206 }