diff --git a/Gvisual/src/gvisual/LegendPanel.java b/Gvisual/src/gvisual/LegendPanel.java new file mode 100644 index 0000000..372553f --- /dev/null +++ b/Gvisual/src/gvisual/LegendPanel.java @@ -0,0 +1,47 @@ +package gvisual; + +import javax.swing.*; +import java.awt.*; +import java.util.LinkedHashMap; +import java.util.Map; + +/** + * Reusable legend panel that displays a color-coded key for edge types. + * + *

Extracted from {@link Main#initializeLegendSpace()} to separate UI + * component construction from the main frame, making it independently + * testable and reusable in other views (e.g., headless report generation + * or secondary analysis windows).

+ * + * @author zalenix + */ +public class LegendPanel extends JPanel { + + private static final Map LEGEND_ENTRIES = new LinkedHashMap<>(); + static { + LEGEND_ENTRIES.put("Friend", "images/green.jpg"); + LEGEND_ENTRIES.put("Familiar Stranger", "images/gray.jpg"); + LEGEND_ENTRIES.put("Stranger", "images/red.jpg"); + LEGEND_ENTRIES.put("Classmate", "images/blue.jpg"); + LEGEND_ENTRIES.put("Study Group", "images/yellow.jpg"); + } + + /** + * Creates a legend panel with the default edge-type colour mapping. + */ + public LegendPanel() { + JLabel heading = new JLabel("Legend for the Graph"); + Box vbox = Box.createVerticalBox(); + + for (Map.Entry entry : LEGEND_ENTRIES.entrySet()) { + Box row = Box.createHorizontalBox(); + row.add(new JLabel(new ImageIcon(entry.getValue()))); + row.add(new JLabel(entry.getKey())); + vbox.add(row); + } + + JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, + heading, vbox); + add(splitPane); + } +} diff --git a/Gvisual/src/gvisual/Main.java b/Gvisual/src/gvisual/Main.java index 8d2159f..630df1d 100644 --- a/Gvisual/src/gvisual/Main.java +++ b/Gvisual/src/gvisual/Main.java @@ -674,70 +674,7 @@ public void addGraph() throws ParserConfigurationException, IOException, SAXExce * initialize the Legend Space */ public final void initializeLegendSpace(){ - legendPanel = new JPanel(); - - JLabel legendHeading = new JLabel("Legend for the Graph"); - - String blueImgPath = "images/blue.jpg"; - String redImgPath = "images/red.jpg"; - String greenImgPath = "images/green.jpg"; - String yellowImgPath = "images/yellow.jpg"; - String grayImgPath = "images/gray.jpg"; - - Icon blue = new ImageIcon(blueImgPath); - Icon red = new ImageIcon(redImgPath); - Icon green = new ImageIcon(greenImgPath); - Icon yellow = new ImageIcon(yellowImgPath); - Icon gray = new ImageIcon(grayImgPath); - - JLabel fLabel = new JLabel("Friend"); - JLabel fColor= new JLabel(green); - JLabel fsLabel= new JLabel("Familiar Stranger"); - JLabel fsColor= new JLabel(gray); - JLabel sLabel= new JLabel("Stranger"); - JLabel sColor= new JLabel(red); - JLabel cLabel= new JLabel("Classmate"); - JLabel cColor = new JLabel(blue); - JLabel sgLabel= new JLabel("Study Group"); - JLabel sgColor= new JLabel(yellow); - - Box HBox[] = new Box[5]; - Box VBox[] = new Box[1]; - - VBox[0]= Box.createVerticalBox(); - - HBox[0]= Box.createHorizontalBox(); - HBox[1]= Box.createHorizontalBox(); - HBox[2]= Box.createHorizontalBox(); - HBox[3]= Box.createHorizontalBox(); - HBox[4]= Box.createHorizontalBox(); - - - HBox[0].add(fColor); - HBox[1].add(fsColor); - HBox[2].add(sColor); - HBox[3].add(cColor); - HBox[4].add(sgColor); - - HBox[0].add(fLabel); - HBox[1].add(fsLabel); - HBox[2].add(sLabel); - HBox[3].add(cLabel); - HBox[4].add(sgLabel); - - VBox[0].add(HBox[0]); - VBox[0].add(HBox[1]); - VBox[0].add(HBox[2]); - VBox[0].add(HBox[3]); - VBox[0].add(HBox[4]); - - JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, - legendHeading, VBox[0]); - - - legendPanel.add(splitPane); - - } + legendPanel = new LegendPanel(); /** * Initializes the shortest path finder panel with source/target selection, @@ -2683,4 +2620,4 @@ public static void main(String[] args) { } }); } -} \ No newline at end of file +}