| | |
| | | private final Color THEME_COLOR; |
| | | |
| | | public RemoteControlDialog(Component parent, Color themeColor) { |
| | | super(parent != null ? (JFrame) SwingUtilities.getWindowAncestor(parent) : null, |
| | | "遥控操作", true); |
| | | super(parent != null ? (JFrame) SwingUtilities.getWindowAncestor(parent) : null, |
| | | "遥控操作", false); |
| | | this.THEME_COLOR = themeColor; |
| | | // 使用统一对话框尺寸 |
| | | initializeDialog(UIConfig.DIALOG_WIDTH, UIConfig.DIALOG_HEIGHT); |
| | | 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(getParent()); |
| | | setLocationRelativeTo(getOwner()); |
| | | setResizable(false); |
| | | getContentPane().setBackground(Color.WHITE); |
| | | } |
| | | |
| | | private void initializeRemoteContent() { |
| | | JPanel contentPanel = new JPanel(new BorderLayout()); |
| | | contentPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); |
| | | |
| | | JPanel directionPanel = new JPanel(new GridLayout(3, 3, 10, 10)); |
| | | directionPanel.setBackground(Color.WHITE); |
| | | |
| | | directionPanel.add(new JLabel()); |
| | | JButton upBtn = createDirectionButton("⬆️"); |
| | | directionPanel.add(upBtn); |
| | | directionPanel.add(new JLabel()); |
| | | |
| | | JButton leftBtn = createDirectionButton("⬅️"); |
| | | directionPanel.add(leftBtn); |
| | | JButton centerBtn = createDirectionButton("⏺️"); |
| | | directionPanel.add(centerBtn); |
| | | JButton rightBtn = createDirectionButton("➡️"); |
| | | directionPanel.add(rightBtn); |
| | | |
| | | directionPanel.add(new JLabel()); |
| | | JButton downBtn = createDirectionButton("⬇️"); |
| | | directionPanel.add(downBtn); |
| | | directionPanel.add(new JLabel()); |
| | | |
| | | JPanel speedPanel = new JPanel(new GridLayout(1, 2, 10, 0)); |
| | | speedPanel.setBackground(Color.WHITE); |
| | | |
| | | JButton backwardBtn = createSpeedButton("后退", new Color(255, 107, 107)); |
| | | JButton forwardBtn = createSpeedButton("前进", THEME_COLOR); |
| | | |
| | | speedPanel.add(backwardBtn); |
| | | speedPanel.add(forwardBtn); |
| | | |
| | | contentPanel.add(directionPanel, BorderLayout.CENTER); |
| | | contentPanel.add(speedPanel, BorderLayout.SOUTH); |
| | | |
| | | 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 createDirectionButton(String icon) { |
| | | JButton button = new JButton(icon); |
| | | button.setFont(new Font("Segoe UI Emoji", Font.PLAIN, 20)); |
| | | button.setPreferredSize(new Dimension(60, 60)); |
| | | button.setBackground(Color.WHITE); |
| | | button.setBorder(BorderFactory.createCompoundBorder( |
| | | BorderFactory.createLineBorder(new Color(200, 200, 200)), |
| | | BorderFactory.createEmptyBorder(10, 10, 10, 10) |
| | | )); |
| | | 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.addMouseListener(new MouseAdapter() { |
| | | public void mousePressed(MouseEvent e) { |
| | | button.setBackground(new Color(240, 240, 240)); |
| | | } |
| | | public void mouseReleased(MouseEvent e) { |
| | | button.setBackground(Color.WHITE); |
| | | } |
| | | }); |
| | | |
| | | 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 JButton createSpeedButton(String text, Color color) { |
| | | JButton button = new JButton(text); |
| | | button.setFont(new Font("微软雅黑", Font.BOLD, 14)); |
| | | button.setBackground(color); |
| | | button.setForeground(Color.WHITE); |
| | | button.setBorder(BorderFactory.createEmptyBorder(15, 0, 15, 0)); |
| | | button.setFocusPainted(false); |
| | | |
| | | button.addMouseListener(new MouseAdapter() { |
| | | public void mouseEntered(MouseEvent e) { |
| | | button.setBackground(color.darker()); |
| | | |
| | | private ImageIcon loadScaledIcon(String path, int width, int height) { |
| | | try { |
| | | ImageIcon icon = new ImageIcon(path); |
| | | if (icon.getIconWidth() <= 0 || icon.getIconHeight() <= 0) { |
| | | return null; |
| | | } |
| | | public void mouseExited(MouseEvent e) { |
| | | button.setBackground(color); |
| | | } |
| | | }); |
| | | |
| | | return button; |
| | | 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; |
| | | } |
| | | } |
| | | } |