Skip to content
This repository was archived by the owner on Jun 18, 2026. It is now read-only.

Commit f01bf0a

Browse files
refactor: simplify legend panel using EdgeType enum loop
- Added legendImagePath field to EdgeType enum - Replaced 60+ lines of manual legend construction with a 10-line loop - Legend now automatically stays in sync when new EdgeType values are added - No behavioral change
1 parent e7a2803 commit f01bf0a

2 files changed

Lines changed: 22 additions & 60 deletions

File tree

Gvisual/src/gvisual/EdgeType.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,25 @@
1818
*/
1919
public enum EdgeType {
2020

21-
FRIEND ("f", "friend", Color.GREEN, 10, 2),
22-
CLASSMATE ("c", "Classmate", Color.BLUE, 30, 1),
23-
FAMILIAR ("fs", "Familiar Stranger", Color.GRAY, 2, 1),
24-
STRANGER ("s", "Stranger", Color.RED, 2, 2),
25-
STUDY_GROUP ("sg", "Study Groups", Color.ORANGE, 20, 1);
21+
FRIEND ("f", "friend", Color.GREEN, "images/green.jpg", 10, 2),
22+
CLASSMATE ("c", "Classmate", Color.BLUE, "images/blue.jpg", 30, 1),
23+
FAMILIAR ("fs", "Familiar Stranger", Color.GRAY, "images/gray.jpg", 2, 1),
24+
STRANGER ("s", "Stranger", Color.RED, "images/red.jpg", 2, 2),
25+
STUDY_GROUP ("sg", "Study Groups", Color.ORANGE,"images/yellow.jpg", 20, 1);
2626

2727
private final String code;
2828
private final String displayLabel;
2929
private final Color color;
30+
private final String legendImagePath;
3031
private final int defaultDurationThreshold;
3132
private final int defaultMeetingThreshold;
3233

33-
EdgeType(String code, String displayLabel, Color color,
34+
EdgeType(String code, String displayLabel, Color color, String legendImagePath,
3435
int defaultDurationThreshold, int defaultMeetingThreshold) {
3536
this.code = code;
3637
this.displayLabel = displayLabel;
3738
this.color = color;
39+
this.legendImagePath = legendImagePath;
3840
this.defaultDurationThreshold = defaultDurationThreshold;
3941
this.defaultMeetingThreshold = defaultMeetingThreshold;
4042
}
@@ -54,6 +56,11 @@ public Color getColor() {
5456
return color;
5557
}
5658

59+
/** Returns the path to the legend colour-swatch image. */
60+
public String getLegendImagePath() {
61+
return legendImagePath;
62+
}
63+
5764
/** Returns the default duration-of-meeting threshold (minutes). */
5865
public int getDefaultDurationThreshold() {
5966
return defaultDurationThreshold;

Gvisual/src/gvisual/Main.java

Lines changed: 9 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -637,62 +637,17 @@ public final void initializeLegendSpace(){
637637

638638
JLabel legendHeading = new JLabel("Legend for the Graph");
639639

640-
String blueImgPath = "images/blue.jpg";
641-
String redImgPath = "images/red.jpg";
642-
String greenImgPath = "images/green.jpg";
643-
String yellowImgPath = "images/yellow.jpg";
644-
String grayImgPath = "images/gray.jpg";
645-
646-
Icon blue = new ImageIcon(blueImgPath);
647-
Icon red = new ImageIcon(redImgPath);
648-
Icon green = new ImageIcon(greenImgPath);
649-
Icon yellow = new ImageIcon(yellowImgPath);
650-
Icon gray = new ImageIcon(grayImgPath);
651-
652-
JLabel fLabel = new JLabel("Friend");
653-
JLabel fColor= new JLabel(green);
654-
JLabel fsLabel= new JLabel("Familiar Stranger");
655-
JLabel fsColor= new JLabel(gray);
656-
JLabel sLabel= new JLabel("Stranger");
657-
JLabel sColor= new JLabel(red);
658-
JLabel cLabel= new JLabel("Classmate");
659-
JLabel cColor = new JLabel(blue);
660-
JLabel sgLabel= new JLabel("Study Group");
661-
JLabel sgColor= new JLabel(yellow);
662-
663-
Box HBox[] = new Box[5];
664-
Box VBox[] = new Box[1];
665-
666-
VBox[0]= Box.createVerticalBox();
667-
668-
HBox[0]= Box.createHorizontalBox();
669-
HBox[1]= Box.createHorizontalBox();
670-
HBox[2]= Box.createHorizontalBox();
671-
HBox[3]= Box.createHorizontalBox();
672-
HBox[4]= Box.createHorizontalBox();
673-
674-
675-
HBox[0].add(fColor);
676-
HBox[1].add(fsColor);
677-
HBox[2].add(sColor);
678-
HBox[3].add(cColor);
679-
HBox[4].add(sgColor);
680-
681-
HBox[0].add(fLabel);
682-
HBox[1].add(fsLabel);
683-
HBox[2].add(sLabel);
684-
HBox[3].add(cLabel);
685-
HBox[4].add(sgLabel);
686-
687-
VBox[0].add(HBox[0]);
688-
VBox[0].add(HBox[1]);
689-
VBox[0].add(HBox[2]);
690-
VBox[0].add(HBox[3]);
691-
VBox[0].add(HBox[4]);
640+
Box legendBox = Box.createVerticalBox();
641+
for (EdgeType type : EdgeType.values()) {
642+
Icon icon = new ImageIcon(type.getLegendImagePath());
643+
Box row = Box.createHorizontalBox();
644+
row.add(new JLabel(icon));
645+
row.add(new JLabel(type.getDisplayLabel()));
646+
legendBox.add(row);
647+
}
692648

693649
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
694-
legendHeading, VBox[0]);
695-
650+
legendHeading, legendBox);
696651

697652
legendPanel.add(splitPane);
698653

0 commit comments

Comments
 (0)