jnscrollbar.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 548 行 · 第 1/2 页

JAVA
548
字号

        // draw BorderLine
        g.drawRect(0, 0, this.getWidth(), this.getHeight());

        g.setColor(Color.white);
        g.drawRect(1, 1, this.getWidth() - 2, this.getHeight() - 2);

        g.setColor(this.getForeground());

        /* ------------ */
        _drawBox(g, box_One, BOXONE);
        /* ------------ */
        _drawBox(g, box_Two, BOXTWO);
        /* ------------ */		// draw Scroller.
        _drawBox(g, scroller, SCROLLER);

//	System.out.println(scroller.getX()+" ---  "+ scroller.getY());

    }

    private int getLength(Dimension d) {
        if (orientation == HORIZONTAL) {
            return (int) d.getWidth();
        } else if (orientation == VERTICAL) {
            return (int) d.getHeight();
        }

        return -1;
    }

    private int getLength(Rectangle rect) {
/*	if(orientation == HORIZONTAL)
	{
		return (int)rect.getWidth();
	}
	else if(orientation == VERTICAL)
	{
		return (int)rect.getHeight();
	}

	return -1;
*/
        return getLength(rect.getSize());
    }

    private void init() {
//	this.setBackground(Color.gray);
//	this.setBackground(new Color(207, 207, 207));
        this.setForeground(Color.black);
        this.setBackground(JNDefaultLookAndFeel.ScrollBar_BackgroundColor);
//	this.setPreferredSize(box_side*2, box_side*2);
    }

    protected void internallyPaint(Graphics g) {
        g.setColor(this.getBackground());
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
        g.setColor(this.getForeground());

        drawScrollbar(g);
    }

    protected void notifyListeners() {
        int id = JNAdjustmentEvent.ADJUSTMENT_VALUE_CHANGED;
        int type = -1;

//	System.out.println(value+" &&&&");



        for (int i = 0; i < listenerList.size(); i++) {
            JNAdjustmentEvent ae = new JNAdjustmentEvent(this, id, type, value);
            ((JNAdjustmentListener) listenerList.get(i)).adjustmentValueChanged(ae);
/*		JNComponent comp = (JNComponent)listenerList.get( i );

		JNAdjustmentEvent ae = new JNAdjustmentEvent(comp, id, type, value );
		JNDesktopManager.getDesktopManager().postEvent(ae);
*/
        }
    }

    protected void processMouseEvent(JNodeMouseEvent event) {
        int lx = event.getX();// - this.getX();
        int ly = event.getY();// - this.getY();

        if (event.getID() == JNodeMouseEvent.MOUSE_PRESSED) {
            // if in box one area,
            if (box_One.contains(lx, ly)) {
                currentPressed = BOXONE;
                setValue(value - unitIncriment);
            }

            // if in box two area,
            else if (box_Two.contains(lx, ly)) {
                currentPressed = BOXTWO;
                setValue(value + unitIncriment);
            } else if (scroller.contains(lx, ly))  // if in scroller area,
                currentPressed = SCROLLER;
            else {
                // TODO.
            }
        } else if (event.getID() == JNodeMouseEvent.MOUSE_RELEASED) {
            currentPressed = -1;
            mouseDragXdiff = -1;
            mouseDragYdiff = -1;
        }

        /*
        if (event.getID() == MouseEvent.MOUSE_DRAGGED )
        {
            if( currentPressed == SCROLLER )  // if current pressed is scroller.
            {
                _dragScroller(lx, ly);
            }
        }*/

        Graphics gfx = getGraphics().create();
        gfx.translate(getAbsLocation().x, getAbsLocation().y);
        update(gfx);

        super.processMouseEvent(event);
    }

    protected void processMouseMotionEvent(JNodeMouseEvent event) {
        int lx = event.getX();// - this.getX();
        int ly = event.getY();// - this.getY();

        if (event.getID() == JNodeMouseEvent.MOUSE_DRAGGED) {
            if (currentPressed == SCROLLER)  // if current pressed is scroller.
            {
                _dragScroller(lx, ly);
            }
        }

        Graphics gfx = getGraphics().create();
        gfx.translate(getAbsLocation().x, getAbsLocation().y);
        update(gfx);

        super.processMouseEvent(event);
    }

    /**
     * recalculate method comment.
     */
    public void recalculate() {
    }

    /**
     * resize method comment.
     */
    public void resize() {
    }

    public void setMinMax(int mn, int mx) {
        minimum = mn;
        maximum = mx;

        _calculateRatio();
    }

    public void setOrientation(int ori) {
        orientation = ori;

        // for every change of orientation, recalculate box.
        _calculateBoxDetails();
    }

    protected int setScrollerLocation(int sx, int sy) {
        int pixels = 0;

        if (orientation == HORIZONTAL) {
            // if dimensions is less than minimum move it to minimum.
            if (sx < box_One.getWidth()) {
                sx = (int) box_One.getWidth();
            }
            // if dimensions is less than maximum move it to maximum.
            int maxX = (int) (this.getWidth() - (scroller.getWidth() + box_Two.getWidth()));
            if (sx > maxX) {
                sx = maxX;
            }
            pixels = sx;
        } else if (orientation == VERTICAL) {

            if (sy < box_One.getHeight()) {
                sy = (int) box_One.getHeight();
            }
            int maxY = (int) (this.getHeight() - (scroller.getHeight() + box_Two.getHeight()));
            if (sy > maxY) {
                sy = maxY;
            }
            pixels = sy;
        }

        /* set the new value  */
        scroller.setLocation(sx, sy);

        return pixels;
    }

    public void setSize(Dimension d) {

/*	if(orientation == HORIZONTAL)
	{
		w = w; // no change.
		h = box_side;
	}
	else if(orientation == VERTICAL)
	{
		w = box_side;
		h = h;// no change.
	}
*/
//	super.setSize(w, h);
        super.setSize(d);

        _calculateBoxDetails();

// <temp.
/*	int smin = 0;
	int smax = getLength( this.getSize()) -
				( getLength( box_One ) + getLength( box_Two ) + getLength( scroller ) );

	setMinMax(smin, smax);*/
// temp>

        _calculateRatio();
    }

    public void setUnitIncrement(int u) {
        this.unitIncriment = u;
    }

    public void setValue(int val) {

        /* check min max boundries. */

        if (val < minimum)
            val = minimum;
        else if (val > maximum)
            val = maximum;

        this.value = val;

        /* calculate pixels from value */
        int pixels = (int) ((val - minimum) / ratio) + getLength(box_One);


        if (orientation == HORIZONTAL)  // only deals with X
        {
            setScrollerLocation(pixels, (int) scroller.getY());
        } else if (orientation == VERTICAL) // only deals with Y
        {
            setScrollerLocation((int) scroller.getX(), pixels);
        }



        /*  notify listeners. */
        notifyListeners();

    }

    public void setLineIncrement(int inc) {
        //TODO: implement it
    }

    public void setPageIncrement(int inc) {
        //TODO: implement it
    }

    public void setValues(int value, int visible, int min, int max) {
        //TODO: implement it
    }
}

⌨️ 快捷键说明

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