张世豪
2025-12-09 32524195d474b74e48916867b2a6c2f022a40d98
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package zhuye;
 
import ui.UIConfig;
 
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
 
public class RemoteControlDialog extends JDialog {
    private final Color THEME_COLOR;
    
    public RemoteControlDialog(Component parent, Color themeColor) {
        super(parent != null ? (JFrame) SwingUtilities.getWindowAncestor(parent) : null,
              "遥控操作", false);
    this.THEME_COLOR = themeColor;
        setModalityType(ModalityType.MODELESS);
        setAlwaysOnTop(true);
    initializeDialog((int) (UIConfig.DIALOG_WIDTH * 0.8), (int) (UIConfig.DIALOG_HEIGHT / 3.0 * 0.7));
        initializeRemoteContent();
        if (parent == null) {
            setLocationRelativeTo(null); // 居中显示
        }
    }
    
    private void initializeDialog(int width, int height) {
        setSize(width, height);
        setLocationRelativeTo(getOwner());
        setResizable(false);
        getContentPane().setBackground(Color.WHITE);
    }
    
    private void initializeRemoteContent() {
        JPanel contentPanel = new JPanel(new BorderLayout());
        contentPanel.setBorder(BorderFactory.createEmptyBorder(16, 16, 16, 16));
        contentPanel.setBackground(Color.WHITE);
 
    ImageIcon forwardIcon = loadScaledIcon("image/go.png", 36, 36);
    ImageIcon backwardIcon = loadScaledIcon("image/speedslow.png", 36, 36);
    ImageIcon leftIcon = loadScaledIcon("image/left.png", 36, 36);
    ImageIcon rightIcon = loadScaledIcon("image/right.png", 36, 36);
    ImageIcon pressedIcon = loadScaledIcon("image/anxia.png", 36, 36);
 
    JButton forwardBtn = createControlButton("前进", THEME_COLOR, forwardIcon, pressedIcon);
    JButton backwardBtn = createControlButton("后退", new Color(255, 107, 107), backwardIcon, pressedIcon);
    JButton turnLeftBtn = createControlButton("左转", new Color(96, 125, 139), leftIcon, pressedIcon);
    JButton turnRightBtn = createControlButton("右转", new Color(96, 125, 139), rightIcon, pressedIcon);
 
        JPanel forwardBackwardPanel = new JPanel();
        forwardBackwardPanel.setOpaque(false);
        forwardBackwardPanel.setLayout(new BoxLayout(forwardBackwardPanel, BoxLayout.Y_AXIS));
        forwardBackwardPanel.add(Box.createVerticalGlue());
        forwardBackwardPanel.add(forwardBtn);
        forwardBackwardPanel.add(Box.createRigidArea(new Dimension(0, 16)));
        forwardBackwardPanel.add(backwardBtn);
        forwardBackwardPanel.add(Box.createVerticalGlue());
        forwardBackwardPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 24));
 
        JPanel turnPanel = new JPanel();
        turnPanel.setOpaque(false);
        turnPanel.setLayout(new BoxLayout(turnPanel, BoxLayout.Y_AXIS));
        turnPanel.add(Box.createVerticalGlue());
        JPanel turnButtonsRow = new JPanel(new FlowLayout(FlowLayout.RIGHT, 16, 16));
        turnButtonsRow.setOpaque(false);
        turnButtonsRow.add(turnLeftBtn);
        turnButtonsRow.add(turnRightBtn);
        turnPanel.add(turnButtonsRow);
 
        contentPanel.add(forwardBackwardPanel, BorderLayout.WEST);
        contentPanel.add(turnPanel, BorderLayout.CENTER);
 
        getContentPane().add(contentPanel);
    }
    
    private JButton createControlButton(String text, Color background, ImageIcon icon, ImageIcon pressedIcon) {
        JButton button = new JButton();
        button.setFont(new Font("微软雅黑", Font.BOLD, 18));
        button.setForeground(Color.WHITE);
        button.setFocusPainted(false);
        button.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
 
        if (icon != null) {
            button.setIcon(icon);
            button.setText(null);
            button.setContentAreaFilled(false);
            button.setOpaque(false);
            button.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
        } else {
            button.setText(text);
            button.setBackground(background);
            button.setOpaque(true);
            button.setContentAreaFilled(true);
        }
 
        if (icon != null && pressedIcon != null) {
            button.addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    button.setIcon(pressedIcon);
                }
 
                @Override
                public void mouseReleased(MouseEvent e) {
                    button.setIcon(icon);
                }
 
                @Override
                public void mouseExited(MouseEvent e) {
                    if (!button.getModel().isPressed()) {
                        button.setIcon(icon);
                    }
                }
            });
        }
 
        return button;
    }
 
    private ImageIcon loadScaledIcon(String path, int width, int height) {
        try {
            ImageIcon icon = new ImageIcon(path);
            if (icon.getIconWidth() <= 0 || icon.getIconHeight() <= 0) {
                return null;
            }
            Image scaled = icon.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH);
            return new ImageIcon(scaled);
        } catch (Exception ex) {
            System.err.println("加载图标失败: " + path + " - " + ex.getMessage());
            return null;
        }
    }
}