Java › Swing

Figma 디자인 토큰으로 Swing 라이트·다크 테마 만들기

Figma의 색상 토큰을 Swing 팔레트로 옮겨 기본 위젯과 직접 그리는 버튼에 적용합니다. 두 테마의 색상은 스냅샷 테스트와 미리보기로 확인합니다.

Figma 디자인 토큰으로 Swing 라이트·다크 테마 만들기

RSIS에서 Figma의 라이트·다크 색을 팔레트에 옮겼습니다. 기본 위젯은 UIManager에서 색을 읽고, 직접 그리는 컴포넌트는 같은 팔레트에서 색을 가져옵니다.

Java 17과 RSIS refactor-v2의 Metal Look and Feel을 기준으로 설명합니다.

색상에 화면에서 맡는 역할 붙이기

RSIS의 라이트 테마에서 포커스 테두리와 기본 버튼은 둘 다 #176346을 씁니다. 다크 테마에서는 테두리가 #4ac991, 버튼이 #21825b입니다. 한 토큰에 묶으면 두 요소가 같은 색을 쓰게 되므로, 포커스와 버튼에 각각 이름을 붙였습니다. Figma 색상 스냅샷

Figma 변수Java에서 읽는 역할라이트다크
color/surface/canvassurface().canvas()#f4f6f2#101915
color/text/primarytext().primary()#162820#eef4f0
color/border/focusborder().focus()#176346#4ac991
color/action/primaryaction().primary()#176346#21825b

AppPalette는 관련 색을 Surface, Text, Border 같은 레코드로 묶습니다. AppTheme.of()는 선택한 팔레트에 글꼴, 그림자, 포커스 링을 더합니다. 포커스 링에는 테두리의 RGB에 알파 0x3d를 더한 반투명 색을 씁니다. AppPalette · AppTheme.of

기본 위젯에 UIManager 기본값 적용하기

AppThemeDefaults.install()은 테마를 받아 Panel.background, TextField.foreground, List.selectionBackground 등의 키를 설정합니다. RSIS는 플랫폼마다 같은 UI 델리게이트가 색을 읽도록 Metal Look and Feel을 사용합니다. Look and Feel을 바꿀 때는 새 델리게이트가 어떤 키를 읽고 각 상태를 어떻게 그리는지 확인합니다. AppThemeDefaults

색과 글꼴을 ColorUIResource, FontUIResource로 감싸면 UI 델리게이트가 기본값을 설치할 때 교체할 수 있습니다. UIManager.put(...)으로 값을 등록하고, 이미 띄운 창에는 SwingUtilities.updateComponentTreeUI(...)로 새 기본값을 적용합니다. 컴포넌트가 자기 필드에 팔레트를 보관한다면 새 값을 따로 넘깁니다. JDK 17 UIManager · JDK 17 UIResource

커스텀 버튼에 같은 팔레트 전달하기

직접 그리는 RsisButton은 버튼의 톤과 ButtonModel 상태를 보고 채움, 글자, 테두리 색을 계산합니다. 기본 버튼이 눌렸거나 포인터가 위에 있으면 action().hover(), 비활성이면 surface().disabled()와 text().muted()를 사용합니다. 키보드 포커스는 border().focus()와 포커스 링으로 드러냅니다. RsisButton 스타일 계산

적용 대상방법
JTextField 등 기본 UI 델리게이트가 그리는 부분UIManager 기본값과 UIResource
모서리·채움·포커스를 직접 그리는 버튼테마 전달 후 repaint()
운영체제 기본 스타일과 동작에 맞는 도구시스템 Look and Feel과 필요한 화면 설정

두 방식의 결과를 한 창에서 비교할 수 있습니다. 예제를 PaletteDemo.java로 저장하고 java PaletteDemo.java를 실행합니다. Switch theme을 누르면 기본 텍스트 필드와 커스텀 버튼의 배경·텍스트를 비교할 수 있습니다. Tab으로 커스텀 버튼에 포커스를 옮겨 테두리도 확인합니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.metal.MetalLookAndFeel;

public class PaletteDemo {
    record Palette(Color canvas, Color panel, Color text, Color action, Color focus) {}

    static final Palette LIGHT = palette(0xf4f6f2, 0xffffff, 0x162820, 0x176346, 0x176346);
    static final Palette DARK = palette(0x101915, 0x17241e, 0xeef4f0, 0x21825b, 0x4ac991);

    public static void main(String[] args) {
        SwingUtilities.invokeLater(PaletteDemo::open);
    }

    private static void open() {
        try {
            UIManager.setLookAndFeel(new MetalLookAndFeel());
        } catch (UnsupportedLookAndFeelException e) {
            throw new IllegalStateException(e);
        }
        install(LIGHT);

        JButton toggle = new JButton("Switch theme");
        JTextField field = new JTextField("Restaurant name", 16);
        PaintedButton button = new PaintedButton();
        JFrame frame = new JFrame("Swing palette demo");
        JPanel root = new JPanel(new FlowLayout(FlowLayout.LEFT, 12, 12));
        root.add(new JLabel("Search"));
        root.add(field);
        root.add(button);
        root.add(toggle);

        frame.setContentPane(root);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        toggle.addActionListener(e -> {
            Palette palette = button.palette == LIGHT ? DARK : LIGHT;
            install(palette);
            SwingUtilities.updateComponentTreeUI(frame);
            button.apply(palette);
        });

        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    static void install(Palette palette) {
        UIManager.put("Panel.background", new ColorUIResource(palette.canvas()));
        UIManager.put("Label.foreground", new ColorUIResource(palette.text()));
        UIManager.put("TextField.background", new ColorUIResource(palette.panel()));
        UIManager.put("TextField.foreground", new ColorUIResource(palette.text()));
        UIManager.put("TextField.caretForeground", new ColorUIResource(palette.text()));
    }

    private static Palette palette(int canvas, int panel, int text, int action, int focus) {
        return new Palette(new Color(canvas), new Color(panel), new Color(text),
                new Color(action), new Color(focus));
    }

    static final class PaintedButton extends JButton {
        private Palette palette = LIGHT;

        PaintedButton() {
            super("Apply");
            setBorder(BorderFactory.createEmptyBorder(8, 14, 8, 14));
            setContentAreaFilled(false);
            setOpaque(false);
            setFocusPainted(false);
            addActionListener(e -> setText("Applied"));
            apply(LIGHT);
        }

        void apply(Palette next) {
            palette = next;
            setForeground(Color.WHITE);
            repaint();
        }

        @Override
        protected void paintComponent(Graphics g) {
            Graphics2D copy = (Graphics2D) g.create();
            try {
                copy.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);
                copy.setColor(palette.action());
                copy.fillRoundRect(1, 1, getWidth() - 2, getHeight() - 2, 10, 10);
                if (isFocusOwner()) {
                    copy.setColor(palette.focus());
                    copy.setStroke(new BasicStroke(2));
                    copy.drawRoundRect(2, 2, getWidth() - 4, getHeight() - 4, 10, 10);
                }
            } finally {
                copy.dispose();
            }
            super.paintComponent(g);
        }
    }
}

예제에는 동작 비교를 위해 전환 버튼을 넣었습니다. RSIS는 시작할 때 -Drsis.theme=LIGHT|DARK|SYSTEM 옵션을 읽고, 여기서 고른 테마로 화면을 만듭니다. OS 테마 결정 흐름 · RSIS 시작 코드

37개 색상과 렌더링 화면 확인하기

스냅샷 TSV에는 토큰 이름과 두 테마의 값을 한 줄에 적었습니다. AppPaletteFigmaSyncTest는 37개 토큰 이름을 Java 접근자와 연결해 색을 비교합니다. 반투명 오버레이는 알파가 포함된 8자리 값까지 확인합니다. 선택 행이 의도한 토큰을 쓰는지, 선택된 상태와 포커스를 구분할 수 있는지는 화면을 보고 확인합니다. 스냅샷 비교 테스트

1
2
./gradlew test --tests org.example.rsis.ui.theme.AppPaletteFigmaSyncTest
./gradlew exploreGallery

탐색 갤러리는 같은 데이터로 1440×920 크기의 explore-light.png와 explore-dark.png를 만듭니다. 같은 위치의 목록 배경, 검색창 글자와 버튼을 비교할 수 있습니다. 이 과정에서 만든 PNG를 문서에 사용하는 방법은 GitHub 라이트·다크 캡처 글에 정리했습니다. 탐색 갤러리

라이트 테마의 음식점 탐색 화면 그림 1. 라이트 테마에서는 검색 결과 카드 배경이 흰색이고, 선택한 행에 연한 녹색이 들어갑니다.

같은 음식점 데이터와 선택 상태를 유지한 다크 화면 그림 2. 같은 탐색 화면의 다크 테마입니다. 검색창과 목록 배경은 어두워지고 선택 행에는 더 밝은 테두리가 나타납니다.

테마 변경 전 확인할 점

  • 컴포넌트에서 화면의 역할에 맞는 토큰을 읽는지 확인합니다.
  • 기본 위젯의 UI 갱신과 커스텀 위젯의 테마 전달을 Swing EDT에서 처리합니다.
  • Figma 색을 변경했으면 스냅샷과 팔레트를 갱신하고 비교 테스트를 실행합니다.
  • 기본·선택·포커스·비활성 상태를 두 배경에서 비교합니다.
이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.