⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 statuslight.java

📁 SRI international 发布的OAA框架软件
💻 JAVA
字号:
/**
 * The contents of this file are subject to the OAA  Community Research
 * License Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License
 * at http://www.ai.sri.com/~oaa/.  Software distributed under the License
 * is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * rights and limitations under the License.  Portions of the software are
 * Copyright (c) SRI International, 1999-2003.  All rights reserved.
 * "OAA" is a registered trademark, and "Open Agent Architecture" is a
 * trademark, of SRI International, a California nonprofit public benefit
 * corporation.
*/

package com.sri.oaa2.agt.startit;

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;

public class StatusLight extends JPanel {
  Color bgColor = StartitProcess.getColor(StartitProcess.NOT_RUNNING);
  Color blinkColor;
  boolean blinking = false;
  boolean blinkState = false;
  int blinkMSec = 250;
  javax.swing.Timer blinkTimer;

  public static final int DEFAULT_SIZE = 25;

  private JLabel label = new JLabel();

  public StatusLight() {
    this(DEFAULT_SIZE);
  }

  public StatusLight(int size) {
    setLayout(new GridBagLayout());
    setPreferredSize(new Dimension(size, size));
    setBackground(bgColor);
    setBorder(BorderFactory.createCompoundBorder
	      (BorderFactory.createEtchedBorder(EtchedBorder.RAISED),
	       BorderFactory.createEtchedBorder(EtchedBorder.RAISED)));

    blinkTimer = new Timer(blinkMSec, new ActionListener() {
      public void actionPerformed(ActionEvent e) {
	setBackground(blinkState ? bgColor : blinkColor);
	//label.setForeground(blinkState ? Color.gray : Color.white);
	blinkState = !blinkState;
      }
    });
    
    label.setForeground(goodForegroundColor(bgColor));
    add(label);

    //JPanel innard = new JPanel();
    //add(innard);
    //innard.setBackground(color[i++]);

  }

  public void setLabelFont(Font f) {
    label.setFont(f);
  }

  public void setStatus(int status) {
    Color bg = StartitProcess.getColor(status);
    setBackground(bg);
    label.setForeground(goodForegroundColor(bg));
    label.setText(StartitProcess.getLabel(status));
  }

  // returns white or black, depending on how bright the bg parameter is
  // (this is obviously very quick and dirty - I know nothing about this stuff...)
  public static Color goodForegroundColor(Color bg) {
    int addedComponents = bg.getRed() + bg.getGreen() + bg.getBlue();
    return (addedComponents > 500) ? Color.black : Color.white;
  }
  
  private void blink(boolean yes) {
    if (yes) {
      blinkColor = getBackground();
      blinkTimer.start();
    }
    else {
      blinkTimer.stop();
      setBackground(blinkColor);
      //label.setForeground(Color.white);
    }
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -