📄 slider.java
字号:
if (getGraphics() != null) paint(getGraphics()); } public void setValue(long value) { setValue((double)value); } protected void movePointerTo(int x, int y) { int gx = getGoodX(x); if (gx != -1) { pointer.setLocation(gx,pposition.y); if (getGraphics() != null) paint(getGraphics()); } } protected void movePointerTo(Point p) { int gx = getGoodX(p.x); if (gx != -1) { pointer.setLocation(gx,pposition.y); if (getGraphics() != null) paint(getGraphics()); } } /** * paint the slider. */ public void paint (Graphics g) { Dimension d = getSize(); updateSize(d); Shape s = g.getClip(); Image dbi = null; Graphics dbg = null; dbi = ImageCache.getImage(this, d.width, d.height); dbg = dbi.getGraphics(); dbg.setClip(s); dbg.setColor(color); dbg.clearRect(0,0, d.width, d.height); dbg.fillRect(marginx, marginy, width-(2*marginx), height-(2*marginy)); if (border) { dbg.setColor(color.darker()); dbg.drawRect(marginx, marginy, width-(2*marginx), height-(2*marginy)); } dbg.setColor(Color.white); dbg.fill3DRect(marginx + rect_margin_x, marginy + rect_margin_y - guideHeight/2, getGraduationLength(), guideHeight, false); paintGraduation(dbg); dbg.setColor(color.darker()); pointer.paint(dbg); g.drawImage(dbi, 0, 0, this); } protected int getGraduationLength() { // use to recover arround error. int length = width - 2 * (marginx + rect_margin_x); int nbpart = (int)((max - min) / step); int pixelstep = length / nbpart; return pixelstep * nbpart; } protected void updateGraduationPosition() { int length = width - 2 * (marginx + rect_margin_x); int nbpart = (int) ((max - min) / step); int pixelstep = length / nbpart; int y1 = marginy + 5 * rect_margin_y / 3; int y2 = y1+ graduationHeight; int minpixel = marginx + rect_margin_x; int maxpixel = length + minpixel; int gradx = minpixel; graduations = new Graduation[nbpart+1]; int i=0; while (i < graduations.length){ if (manageLong) { graduations[i] = new Graduation(gradx,y1,gradx,y2,(long)(min+i*step)); } else { graduations[i] = new Graduation(gradx,y1,gradx,y2,min+i*step); } gradx += pixelstep; i++; } } protected void paintGraduation(Graphics g) { g.setColor(Color.black); Font font = new Font(g.getFont().getName(), g.getFont().getStyle(), g.getFont().getSize() - 1); g.setFont(font); for (int i=0 ; i < graduations.length; i++) graduations[i].draw(g, (i == 0) || (i == graduations.length-1)); g.setColor(Color.white); currentGraduation.showValue(g); } /** * update the slider. */ public void update (Graphics g){ paint(g); } protected void setDefaultSize(double min, double max, double step) { int length = minwidth - 2 * (marginx + rect_margin_x); int nbpart = (int)((max - min) / step); int pixelstep = length / nbpart; if (pixelstep < minpixelstep) { minwidth = (minpixelstep * nbpart) + 2 * (marginx + rect_margin_x); defaultWidth = minwidth; } updateSize(defaultWidth, defaultHeight); pposition = new Point(pposition.x, marginy + rect_margin_y); pointer.setLocation(pposition); currentGraduation = graduations[0]; } /** * Resizes this component so that it has width "width" and height "height". * @param d - The dimension specifying the new size of this slider. */ public void updateSize(Dimension d) { updateSize(d.width, d.height); } private Dimension oldsize = new Dimension(0,0); /** * Resizes this component so that it has width "width" * and height "height". * @param width - The new width of this slider * @param height - The new height of this slider */ public void updateSize (int width, int height) { if ((oldsize.width != width) || (oldsize.height != height)) { double value = getValue(); if (height < minheight) height = minheight; if (width < minwidth) width = minwidth; super.setSize(width, height); oldsize = getSize(); this.width = width; this.height = height; rect_margin_y = (height - (2* marginy)) / 3; updateGraduationPosition(); pposition.y = marginy + rect_margin_y; updatePointerPosition(value); updateCurrentGraduation(); } } /** * Gets the mininimum size of this component. * @return A dimension object indicating this slider's minimum size. */ public Dimension getMinimumSize() { return new Dimension(minwidth,minheight); } /** * Set the slider's color. * @param color - the slider's color. */ public void setColor(Color color) { this.color = color; } /** * Set the minimum bound of the slider. * Use initialize or SetBounds if you want to set more than one value. * @param min - the min bound * @see #setMax * @see #setBounds * @see #setStep * @see #initialize */ public void setMin(double min) { this.min = min; update(); } /** * Set the maximum bound of the slider. * Use initialize or SetBounds if you want to set more than one value. * @param max - the max bound * @see #setMin * @see #setBounds * @see #setStep * @see #initialize */ public void setMax(double max) { this.max = max; update(); } /** * Set the bounds of the slider. * @param min - the min bound * @param max - the max bound * @see #setMin * @see #setMax * @see #setStep * @see #initialize */ public void setBounds(double min, double max) { this.min = min; this.max = max; update(); } /** * Set the step of the slider. * Use initialize or SetBounds if you want to set more than one value. * @param step - the step between two position * @see #setMin * @see #setMax * @see #setBounds * @see #initialize */ public void setStep(double step) { this.step = step; update(); } /** * Initialize the slider's bounds and Step * @param min - the min bound * @param max - the max bound * @param step - the step between two position * @see #setMin * @see #setMax * @see #setBounds * @see #initialize */ public void initialize(double min, double max, double step) { this.manageLong = false; this.min = min; this.max = max; this.step = step; update(); } public void initialize(double min, double max, double step, boolean border) { initialize(min,max,step); this.border = border; } public void initialize(long min, long max, long step) { initialize((double)min, (double) max, (double) step); this.manageLong = true; update(); } public void initialize(long min, long max, long step, boolean border) { initialize(min,max,step); this.border = border; } protected void update() { setDefaultSize(min,max,step); updateGraduationPosition(); if (graduations != null) currentGraduation = graduations[0]; setVisible(true); Component parent = getParent(); if (parent != null) parent.validate(); } /** * Constructs an invisible Slider. * use initialize to define it. * @param minPixelStep - the min step (in pixels) between two positions. * @param border - if true draw a border arround the slider. */ public Slider(int minPixelStep, boolean border) { this(); this.minpixelstep = minPixelStep; this.border = border; } /** * Constructs an invisible Slider. * use initialize to define it. */ public Slider() { pposition = new Point(marginx + rect_margin_x, marginy + rect_margin_y); pointer = new Pointer(pointerWidth,pointerHeight, pposition); PointerClickListener clickListener = new PointerClickListener(this); PointerMotionListener motionListener = new PointerMotionListener(this); addMouseListener( clickListener ); addMouseMotionListener( motionListener ); setVisible(false); } // TEST public static void main(String argv[]) { Frame f = new Frame ("Slider"); f.setLayout( new GridLayout(6,1)); Slider slider = null; slider = new Slider (); slider.initialize(0.0, 1.0, 0.1); slider.setColor(Color.lightGray); f.add (slider); slider = new Slider (); slider.initialize(0.0, 0.1, 0.01); slider.setColor(Color.lightGray); f.add (slider); slider = new Slider (); slider.initialize(0.0, 0.01, 0.0005); slider.setColor(Color.lightGray); f.add (slider); slider = new Slider (); slider.initialize(0, 100, 10); slider.setColor(Color.lightGray); f.add (slider); slider = new Slider (); slider.initialize(0, 30000, 5000); slider.setColor(Color.lightGray); f.add (slider); slider = new Slider (); slider.initialize(0, 30000, 1000); slider.setColor(Color.lightGray); f.add (slider); if (argv.length > 1) { int width = Integer.parseInt(argv[0]); int height = Integer.parseInt(argv[1]); slider.setSize(width, height); } f.pack(); f.show (); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -