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

📄 lwslider.java

📁 Zaval Light-Weight Visual Components Library (LwVCL) is a pure Java alternative to humble AWT-based
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
 /**  * Gets the minimal possible value.  * @return a minimal possible value.  */  public int getMin() {    return min;  } /**  * Gets the number of the slider intervals.  * @return a number of the slider intervals.  */  public int getIntervals () {    return intervals.length;  } /**  * Gets the specified interval size.  * @param <code>i</code> the specified interval index.  * @return an interval size.  */  public int getIntervalSize (int i) {    return intervals[i];  } /**  * Sets the slider properties.  * @param <code>min</code> the specified minimal possible value.  * @param <code>max</code> the specified maximal possible value.  * @param <code>intervals</code> the specified intervals sizes array.  * @param <code>roughStep</code> the specified rough step. The step is used to change  * the slider value whenever the mouse button has been pressed.  * @param <code>exactStep</code> the specified exact step. The step is used to change  * the slider value whenever the mouse has been dragged or appropriate key has been pressed.  */  public void setValues (int min, int max, int[] intervals, int roughStep, int exactStep)  {    if (roughStep <= 0 || exactStep < 0 || min >= max ||        min + roughStep > max || min + exactStep > max   ) throw new IllegalArgumentException ();    for (int i=0, start = min; i<intervals.length; i++)    {      start += intervals[i];      if (start > max || intervals[i] < 0) throw new IllegalArgumentException ();    }    this.min  = min;    this.max  = max;    this.roughStep = roughStep;    this.exactStep = exactStep;    this.intervals = new int[intervals.length];    System.arraycopy (intervals, 0, this.intervals, 0, intervals.length);    if (value < min || value > max) setValue(isIntervalModel()?min + intervals[0]:min);    vrp();  } /**  * Gets the start value for the specified interval.  * @param <code>i</code> the specified interval index.  * @return a start value for the specified interval.  */  public int getPointValue(int i)  {    int v = min + getIntervalSize(0);    for (int j=0; j < i; j++, v+=getIntervalSize(j));    return v;  } /**  * Adds the specified action listener to receive action events from this slider.  * The event is performed when the slider value has been changed.  * @param <code>l</code> the specified action listener.  */  public void addActionListener(LwActionListener l) {    if (support == null) support = new LwActionSupport();    support.addListener(l);  } /**  * Removes the specified action listener so it no longer receives action events  * from this component.  * @param <code>l</code> the specified action listener.  */  public void removeActionListener(LwActionListener l) {    if (support != null) support.removeListener(l);  }  public void keyPressed (LwKeyEvent e)  {    boolean b = isIntervalModel();    switch (e.getKeyCode())    {      case KeyEvent.VK_UP:      case KeyEvent.VK_LEFT:      {        int v = nextValue(value, exactStep, -1);        if (v >= min) setValue(v);      } break;      case KeyEvent.VK_DOWN:      case KeyEvent.VK_RIGHT:      {        int v = nextValue(value, exactStep, 1);        if (v <= max) setValue(v);      } break;      case KeyEvent.VK_HOME: setValue(b?getPointValue(0):min); break;      case KeyEvent.VK_END : setValue(b?getPointValue(getIntervals()-1):max); break;    }  }  public void mousePressed (LwMouseEvent e)  {    if (LwToolkit.isActionMask(e.getMask()))    {      int x = e.getX(), y = e.getY();      if (!getBundleBounds(value).contains(x, y))      {        int l = ((orient == LwToolkit.HORIZONTAL)?x:y);        int v = loc2value(l);        if (value != v) setValue(isJumpOnPress() ? v : nextValue(value, roughStep, v<value?-1:1));      }    }  }  private int correctDt;  public void startDragged(LwMouseMotionEvent e)  {    Rectangle r = getBundleBounds(getValue());    if (r.contains(e.getX(), e.getY()))    {      bits = MathBox.getBits(bits, DRAGGED_BIT, true);      correctDt = orient == LwToolkit.HORIZONTAL?r.x + r.width/2 - e.getX():r.y + r.height/2 - e.getY();    }  }  public void endDragged  (LwMouseMotionEvent e) {    bits = MathBox.getBits(bits, DRAGGED_BIT, false);  }  public void mouseDragged(LwMouseMotionEvent e)  {    if (MathBox.checkBit(bits, DRAGGED_BIT))      setValue (findNearest(e.getX() + (orient == LwToolkit.HORIZONTAL?correctDt:0),                            e.getY() + (orient == LwToolkit.HORIZONTAL?0:correctDt)));  }  public void keyReleased  (LwKeyEvent e)    {}  public void keyTyped     (LwKeyEvent e)    {}  public void mouseClicked (LwMouseEvent e)  {}  public void mouseEntered (LwMouseEvent e)  {}  public void mouseExited  (LwMouseEvent e)  {}  public void mouseReleased(LwMouseEvent e)  {}  public void mouseMoved   (LwMouseMotionEvent e) {}  public /*C#override*/ void paint(Graphics g)  {    if (pl == null)    {      pl = new int[getIntervals()];      for (int i=0, l=min; i<pl.length; i++)      {        l += getIntervalSize(i);        pl[i] = value2loc(l);      }    }    Insets    ins = getInsets();    Dimension bs = views[BUNDLE_VIEW].getPreferredSize();    Dimension gs = views[GAUGE_VIEW].getPreferredSize();    int       w = width - ins.left - ins.right - 2, h = height - ins.top - ins.bottom - 2;    if (orient == LwToolkit.HORIZONTAL)    {      int topY = ins.top + (h - psH)/2 + 1;      if (isEnabled())        views[GAUGE_VIEW].paint(g, ins.left + 1, topY + (bs.height - gs.height)/2, w, gs.height, this);      else      {        g.setColor(Color.gray);        g.drawRect(ins.left + 1, topY + (bs.height - gs.height)/2, w, gs.height);      }      int by = topY;      topY += bs.height;      if (MathBox.checkBit(bits, SHOW_SCALE))      {        topY += gap;        g.setColor(isEnabled ()?scaleColor:Color.gray);        for (int i=min; i<=max; i+=scaleStep)        {          int xx = value2loc(i);          g.drawLine(xx, topY, xx, topY + netSize);        }        for (int i=0; i<pl.length; i++)          g.drawLine(pl[i], topY, pl[i], topY + 2*netSize);        topY += (2*netSize);      }      paintNums(g, topY);      views[BUNDLE_VIEW].paint(g, getBundleLoc(value), by, this);    }    else    {      int leftX = ins.left + (w - psW)/2 + 1;      if (isEnabled())        views[GAUGE_VIEW].paint(g, leftX + (bs.width - gs.width)/2, ins.top + 1, gs.width, h, this);      else      {        g.setColor(Color.gray);        g.drawRect(leftX + (bs.width - gs.width)/2, ins.top + 1, gs.width, h);      }      int bx = leftX;      leftX += bs.width;      if (MathBox.checkBit(bits, SHOW_SCALE))      {        leftX += gap;        g.setColor(scaleColor);        for (int i=min; i<=max; i+=scaleStep)        {          int yy = value2loc(i);          g.drawLine(leftX, yy, leftX + netSize, yy);        }        for (int i=0; i<pl.length; i++)          g.drawLine(leftX, pl[i], leftX + 2*netSize, pl[i]);        leftX += (2*netSize);      }      paintNums(g, leftX);      views[BUNDLE_VIEW].paint (g, bx, getBundleLoc (value), this);    }    if (hasFocus())      LwManager.getView("br.dot").paint(g, ins.left, ins.top, w + 2, h + 2, this);  } /**  * Finds nearest slider value where a bundle can be moved by the specified location.  * @param <code>x</code> the specified x coordinate.  * @param <code>y</code> the specified y coordinate.  * @return a nearest value.  */  public int findNearest(int x, int y)  {    int v = loc2value(orient == LwToolkit.HORIZONTAL?x:y);    if (isIntervalModel())    {      int nearest = Integer.MAX_VALUE, res = 0;      for (int i=0; i<intervals.length; i++)      {        int pv = getPointValue(i);        int dt = Math.abs(pv - v);        if (dt < nearest)        {          nearest = dt;          res = pv;        }      }      return res;    }    else    {      v = exactStep*((v + v%exactStep)/exactStep);      if (v > max) v = max;      else      if (v < min) v = min;      return v;    }  } /**  * Converts the specified slider value to a location. The method returns  * "x" coordinate for horizontal aligned slider or "y" coordinate for vertical  * aligned slider.  * @param <code>v</code> the specified slider value.  * @return a "x" or "y" location depending on the slider alignment.  */  public int value2loc (int v) {    return (getScaleSize() * (v - min))/(max - min) + getScaleLocation();  } /**  * Converts the specified location to a slider value. The specified location  * is "x" coordinate for horizontal aligned slider and "y" coordinate  * otherwise.  * @param <code>xy</code> the specified location.  * @return a slider value.  */  public int loc2value (int xy) {    return min + ((max - min)*(xy - getScaleLocation()))/getScaleSize();  }  public /*C#override*/ void invalidate() {    pl = null;    super.invalidate();  }  protected /*C#override*/ Dimension calcPreferredSize() {    return new Dimension (psW + 2, psH + 2);  }  protected /*C#override*/ void recalc ()  {    Dimension ps = views[BUNDLE_VIEW].getPreferredSize();    int       ns = MathBox.checkBit(bits, SHOW_SCALE)?(gap + 2*netSize):0;    int       dt = getMax() - getMin();    int hMax = 0, wMax = 0;    if (MathBox.checkBit(bits, SHOW_TITLE) && getIntervals()>0)    {      for (int i=0; i<getIntervals(); i++)      {        LwView    v = provider.getView(this, new Integer(getPointValue(i)));        Dimension d = v.getPreferredSize();        if (d.height > hMax) hMax = d.height;        if (d.width  > wMax) wMax = d.width;      }    }    if (orient == LwToolkit.HORIZONTAL)    {      psW = dt*2 + ps.width;      psH = ps.height + ns + hMax;    }    else    {      psW = ps.width + ns + wMax;      psH = dt*2 + ps.height;    }  }  private void paintNums (Graphics g, int loc)  {    if (MathBox.checkBit(bits, SHOW_TITLE))      for (int i=0; i<pl.length; i++)      {        LwView render = provider.getView(this, new Integer(getPointValue(i)));        Dimension d = render.getPreferredSize();        if (orient == LwToolkit.HORIZONTAL)          render.paint (g, pl[i] - d.width/2, loc, this);        else          render.paint (g, loc, pl[i] - d.height/2, this);      }  }  private int getScaleSize()  {    Insets    ins = getInsets();    Dimension bs = views[BUNDLE_VIEW].getPreferredSize();    return (orient == LwToolkit.HORIZONTAL?width - ins.left - ins.right - bs.width :height - ins.top - ins.bottom - bs.height)-2;  }  private int getScaleLocation()  {    Dimension bs = views[BUNDLE_VIEW].getPreferredSize();    return (orient == LwToolkit.HORIZONTAL?(int)((insets >> 16) & 0xFFFF) + bs.width/2                                          :(int)(insets & 0xFFFF) + bs.height/2) + 1;  }  private int nextValue (int value, int s, int d)  {    if (isIntervalModel()) return getNeighborPoint(value, d);    else    {      int v = value + (d * s);      if (v > max) v = max;      else      if (v < min) v = min;      return v;    }  }  private int getBundleLoc (int v) {    Dimension bs  = views[BUNDLE_VIEW].getPreferredSize();    return value2loc(v) - (orient == LwToolkit.HORIZONTAL?bs.width/2:bs.height/2);  }  private Rectangle getBundleBounds(int v)  {    Insets    ins = getInsets();    Dimension bs  = views[BUNDLE_VIEW].getPreferredSize();    if (orient == LwToolkit.HORIZONTAL)      return new Rectangle (getBundleLoc(v),                            ins.top + (height - ins.top - ins.bottom - psH)/2 + 1,                            bs.width, bs.height);    else      return new Rectangle (ins.left + (width - ins.left - ins.right - psW)/2 + 1,                            getBundleLoc(v),                            bs.width, bs.height);  }  private int getNeighborPoint(int v, int d)  {    int left = min + getIntervalSize(0), right = getPointValue(getIntervals()-1);    if (v < left) return left;    else    if (v > right) return right;    if (d > 0)    {      int start = min;      for (int i=0; i<getIntervals(); i++)      {        start += getIntervalSize(i);        if (start > v) return start;      }      return right;    }    else    {      int start = right;      for (int i=getIntervals()-1; i>=0; i--)      {        if (start < v) return start;        start -= getIntervalSize(i);      }      return left;    }  }  private static final short INTERVAL_MODEL_BIT = 128;  private static final short DRAGGED_BIT        = 256;  private static final short SHOW_SCALE         = 512;  private static final short SHOW_TITLE         = 1024;  private static final short JUMP_ONPRESS       = 2048;}

⌨️ 快捷键说明

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