OrRotativeButton.java :  » Music » orDrumbox-0.9.0 » com » ordrumbox » gui » widgets » Java Open Source

Java Open Source » Music » orDrumbox 0.9.0 
orDrumbox 0.9.0 » com » ordrumbox » gui » widgets » OrRotativeButton.java
package com.ordrumbox.gui.widgets;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

public class OrRotativeButton extends OrLevel implements MouseListener, MouseMotionListener {
  private static final long  serialVersionUID  = 1L;
  int                         w               = _WIDTH;
  int                         h               = _HEIGHT;
  static final int         _WIDTH_MINI       = OrWidget.W_RACK_ELEMENT / 2;
  static final int         _HEIGHT_MINI       = OrWidget.W_RACK_ELEMENT / 2;
  static final int         _WIDTH           = OrWidget.W_RACK_ELEMENT;
  static final int         _HEIGHT           = OrWidget.H_RACK_ELEMENT;
  static final int         _WIDTH_EXTENDED   = OrWidget.W_RACK_ELEMENT * 2;
  static final int         _HEIGHT_EXTENDED   = OrWidget.W_RACK_ELEMENT * 2;
  public static final int     _DISPLAY_MINI   = 10;
  public static final int     _DISPLAY_EXTENDED  = 20;
  public static final int     _DISPLAY_NORMAL   = 30;
  protected static final int  LBL_Y           = 2;

  int                         maxLevel;
  int                         minLevel;
  String                     unit               = "%";
  int                         displayType       = _DISPLAY_NORMAL;
  private String             labelToolTip;
  private String             label           = "noLabel";
  private int                 amplitude;
  private int                 lastY;

  /**
   * 
   * @param label
   * @param unit
   * @param minLevel
   * @param maxLevel
   * @param level
   */
  public OrRotativeButton(int displayType, String labelTooltip, String label, String unit, int minLevel, int maxLevel, int level) {
    super();
    initComponents(displayType, labelTooltip, label, unit, minLevel, maxLevel, level);
  }

  private void initComponents(int displayType, String labelToolTip, String label, String unit, int minLevel, int maxLevel, int level) {
    this.labelToolTip = labelToolTip;
    this.label = label;
    this.unit = unit;
    this.maxLevel = maxLevel;
    this.minLevel = minLevel;
    amplitude = computeLevelAmplitude(minLevel, maxLevel);
    setLevel(level);
    setToolTipText(labelToolTip);

    switch (displayType) {
    case _DISPLAY_MINI:
      w = _WIDTH_MINI;
      h = _HEIGHT_MINI;
      break;
    case _DISPLAY_EXTENDED:
      w = _WIDTH_EXTENDED;
      h = _HEIGHT_EXTENDED;
      break;
    case _DISPLAY_NORMAL:
      w = _WIDTH;
      h = _HEIGHT;
      break;
    }

    addMouseListener(this);
    addMouseMotionListener(this);
    setPreferredSize(new Dimension(w, h));
    setBackground(Color.darkGray);
    setCursor(new Cursor(Cursor.HAND_CURSOR));
    setLayout(null);
  }

  public void setLevel(int l) {
    // toolTipTxt = labelToolTip + " " + level + " " + unit;
    level = l;
    // System.out.println("setlevel="+jLabel.getText()+"="+ l);
    if (level < minLevel) {
      level = minLevel;
    }
    if (level > maxLevel) {
      level = maxLevel;
    }
    // System.out.println(toolTipTxt + " setlevel="+ l);
    repaint();
  }

  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    g2d.setStroke(OrWidget.PenLarge);
    g.setColor(Color.white);
    g.setFont(OrWidget.NORMAL_FONT);
    g.drawString(label, 2, h - LBL_Y);
    drawValue(g, level, maxLevel, minLevel, unit);
  }

  protected void drawValue(Graphics g, int level, int max, int min, String unit) {
    g.setColor(Color.white);
    g.setFont(OrWidget.NORMAL_FONT);
    g.drawString("" + level + unit, 8, h / 2);
    double alpha = levelToAlpha(level, min, amplitude);
    int x = (int) (Math.cos(alpha) * w / 3) + w / 2;
    int y = (int) (Math.sin(alpha) * h / 3) + h / 2;
    g.setColor(Color.red);
    g.drawLine(w / 2, w / 2, x, y);
    g.drawOval(6, 8, w - 12, h - 22);
  }

  protected double levelToAlpha(int level, int minLevel, int levelAmplitude) {
    double ret = Math.PI * 2 * (level - minLevel) / levelAmplitude;
    ret += Math.PI / 2;
    return ret;
  }

  public void mouseDragged(MouseEvent mouseEvent) {
    setLevel(yToLevel(mouseEvent.getY(), minLevel, maxLevel));
    repaint();
  }

  protected int yToLevel(int y, int minLevel, int maxLevel) {
    int ret = level;
    int incr = 0;
    incr = y - lastY;
    ret += incr;
    lastY = y;
    return ret;
  }

  public void mouseReleased(MouseEvent e) {
    if (getActionListener() != null) {
      ActionEvent actionEvent = new ActionEvent(this, 0, "" + getLevel());
      getActionListener().actionPerformed(actionEvent);
    }
  }

  public void mousePressed(MouseEvent mouseEvent) {
    lastY = mouseEvent.getY();
  }

  public void mouseClicked(MouseEvent e) {}

  public void mouseEntered(MouseEvent e) {}

  public void mouseExited(MouseEvent e) {}

  public void mouseMoved(MouseEvent e) {}

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.