jnscrollbar.java
来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 548 行 · 第 1/2 页
JAVA
548 行
package org.jnode.wt.components;
import org.jnode.wt.events.JNAdjustmentEvent;
import org.jnode.wt.events.JNodeMouseEvent;
import org.jnode.wt.events.JNAdjustmentListener;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.peer.ScrollbarPeer;
/**
* @author kishore
*/
public class JNScrollBar extends org.jnode.wt.components.JNComponent implements ScrollbarPeer{
// private boolean pressed = false;
private int orientation = 1;
public static final int HORIZONTAL = 0;
public static final int VERTICAL = 1;
/*
Box is the button on top and bottom of the Scrollbar.
(Box contains the Arrow mark)
*/
private static final int BOXONE = 1;
private static final int BOXTWO = 2;
private static final int SCROLLER = 3;
private static final int box_side = 20;
protected Rectangle box_One = new Rectangle(box_side, box_side);
protected Rectangle box_Two = new Rectangle(box_side, box_side);
private Rectangle scroller = new Rectangle();
// if clicked on BoxOne or BoxTwo,
private int currentPressed = -1;
// the following will be assigned on the mouse Drag,
// and reset to -1 on mouseReleased.
private int mouseDragXdiff = -1;
private int mouseDragYdiff = -1;
public static final boolean DECREASE = false;
public static final boolean INCREASE = true;
/* Arrows. */
protected Insets box_Insets = new Insets(4, 4, 4, 4);
// fillpolygone details. of BOX ONE
protected int[] boxone_arrow_x_coordinates = new int[3];
protected int[] boxone_arrow_y_coordinates = new int[3];
// fillpolygone details. of BOX ONE
protected int[] boxtwo_arrow_x_coordinates = new int[3];
protected int[] boxtwo_arrow_y_coordinates = new int[3];
protected int minimum = 0;
protected int maximum = 0;
protected int value = 0;
private double ratio = 1;
private int unitIncriment = 0;
public JNScrollBar(int ori) {
this.orientation = ori;
init();
}
public JNScrollBar(int ori, int min, int max) {
this.orientation = ori;
setMinMax(min, max);
init();
}
private void _calculateArrowsDetails() {
if (orientation == VERTICAL) {
//int x1 = (int)box_One.getX() + ((int)box_One.getWidth() - (box_Insets.left + box_Insets.right))/2;
int x1 = box_Insets.left + ((int) box_One.getWidth() - (box_Insets.left + box_Insets.right)) / 2;
int x2 = box_Insets.left;
int x3 = this.getWidth() - box_Insets.right;
/* box one */
// Top point
boxone_arrow_x_coordinates[0] = x1;
boxone_arrow_y_coordinates[0] = box_Insets.top;
// Right point
boxone_arrow_x_coordinates[1] = x2;
boxone_arrow_y_coordinates[1] = (int) box_One.getHeight() - box_Insets.bottom;
// Left point
boxone_arrow_x_coordinates[2] = x3;
boxone_arrow_y_coordinates[2] = boxone_arrow_y_coordinates[1];
/* box two */
// Bottom point
boxtwo_arrow_x_coordinates[0] = x1;
boxtwo_arrow_y_coordinates[0] = this.getHeight() - box_Insets.top; // reverse because this arrow is in reverse direction.
// Right point
boxtwo_arrow_x_coordinates[1] = x2;
boxtwo_arrow_y_coordinates[1] = this.getHeight() - (int) box_Two.getHeight() + box_Insets.bottom; // this is reverse to box,because this arrow will be in reverse direction.
// Left point
boxtwo_arrow_x_coordinates[2] = x3;
boxtwo_arrow_y_coordinates[2] = boxtwo_arrow_y_coordinates[1];
} else if (orientation == HORIZONTAL) {
int y1 = box_Insets.right + ((int) box_One.getHeight() - (box_Insets.left + box_Insets.right)) / 2;
int y2 = box_Insets.right;
int y3 = this.getHeight() - box_Insets.left;
// LEFT point
boxone_arrow_x_coordinates[0] = box_Insets.top;
boxone_arrow_y_coordinates[0] = y1;
// Top point
boxone_arrow_x_coordinates[1] = (int) box_One.getWidth() - (box_Insets.top + box_Insets.bottom);
boxone_arrow_y_coordinates[1] = y2;
// Bottom point
boxone_arrow_x_coordinates[2] = boxone_arrow_x_coordinates[1];
boxone_arrow_y_coordinates[2] = y3;
/* box two */
// Bottom point
boxtwo_arrow_x_coordinates[0] = this.getWidth() - box_Insets.top;
boxtwo_arrow_y_coordinates[0] = y1;
// Right point
boxtwo_arrow_x_coordinates[1] = this.getWidth() - (int) box_Two.getWidth() + box_Insets.bottom;
boxtwo_arrow_y_coordinates[1] = y2;
// Left point
boxtwo_arrow_x_coordinates[2] = boxtwo_arrow_x_coordinates[1];
boxtwo_arrow_y_coordinates[2] = y3;
}
}
private void _calculateBoxDetails() {
/* Dimensions */
// for horizontal and vertical box_one x,y is 0,0 only.
box_One.setLocation(0, 0);
if (orientation == HORIZONTAL) {
box_One.setSize(box_side, this.getHeight());
box_Two.setLocation(this.getWidth() - box_side, 0);
box_Two.setSize(box_One.getSize());// size is same for both one and two.
// scroller.setLocation(box_side+1,0);
scroller.setLocation(box_side + 1, 2);
scroller.setSize(box_side, this.getHeight() - 4);
} else if (orientation == VERTICAL) {
box_One.setSize(this.getWidth(), box_side);
box_Two.setLocation(0, this.getHeight() - box_side);
box_Two.setSize(box_One.getSize());// size is same for both one and two.
// scroller.setLocation(0, box_side+1);
scroller.setLocation(2, box_side + 1);
scroller.setSize(this.getWidth() - 4, box_side);
}
_calculateArrowsDetails();
}
private void _calculateRatio() {
// 's' is visual scrolle length;
int pmin = 0;
int pmax = getLength(this.getSize()) -
(getLength(box_One) + getLength(box_Two) + getLength(scroller));
ratio = (double) (maximum - minimum) / (double) (pmax - pmin);
}
protected void _dragScroller(int newx, int newy) {
int pixels = 0;
if (orientation == HORIZONTAL) // only deals with X
{
if (mouseDragXdiff == -1) // For the FirstTime
{
mouseDragXdiff = newx - (int) scroller.getX();
return;
}
int newsbarX = newx - mouseDragXdiff;
pixels = setScrollerLocation(newsbarX, (int) scroller.getY());
} else if (orientation == VERTICAL) // only deals with Y
{
if (mouseDragYdiff == -1) // For the FirstTime
{
mouseDragYdiff = newy - (int) scroller.getY();
return;
}
int newsbarY = newy - mouseDragYdiff;
pixels = setScrollerLocation((int) scroller.getX(), newsbarY);
}
/* The following cal is for value not for calculating pixel location */
// _setValue_inPixels( t );
int loc = pixels - getLength(box_One);
// set the value.
value = minimum + (int) (ratio * loc);
// int val = minimum + (int)(ratio * loc);
// setValue( val );
// notify listeners.
notifyListeners();
}
protected void _drawArrows(Graphics g, Rectangle r) {
// g.setColor(this.getForeground());
g.setColor(Color.black);
if (r == box_One) {
g.fillPolygon(boxone_arrow_x_coordinates, boxone_arrow_y_coordinates,
boxone_arrow_x_coordinates.length);
} else if (r == box_Two) {
g.fillPolygon(boxtwo_arrow_x_coordinates, boxtwo_arrow_y_coordinates,
boxtwo_arrow_x_coordinates.length);
}
}
protected void _drawBox(Graphics g, Rectangle r, int isPressed) {
/* ------------ */
if (currentPressed == isPressed) {
g.setColor(Color.gray);
g.fillRect((int) r.getX(), (int) r.getY(), (int) r.getWidth(), (int) r.getHeight());
} else {
g.setColor(new Color(207, 207, 207));
g.fillRect((int) r.getX(), (int) r.getY(), (int) r.getWidth(), (int) r.getHeight());
}
g.setColor(Color.black);
g.drawRect((int) r.getX(), (int) r.getY(), (int) r.getWidth(), (int) r.getHeight());
//
g.setColor(Color.white);
g.drawRect((int) r.getX() + 1, (int) r.getY() + 1, (int) r.getWidth() - 2, (int) r.getHeight() - 2);
// draw Arrows
_drawArrows(g, r);
}
public void addAdjustmentListener(JNAdjustmentListener al) {
listenerList.add(al);
}
private void drawScrollbar(Graphics g) {
// draw border.
g.setColor(this.getBackground());
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(this.getForeground());
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?