📄 basicsliderui.java
字号:
} /** * This method paints a major tick for a vertical slider at the given y * value. y represents the y coordinate to paint at. * * @param g The {@link Graphics} object to draw with. * @param tickBounds The tickRect rectangle. * @param y The y coordinate to draw the tick at. */ protected void paintMajorTickForVertSlider(Graphics g, Rectangle tickBounds, int y) { int x = tickRect.width / 4; Color saved = g.getColor(); g.setColor(Color.BLACK); g.drawLine(x, y, x + tickRect.width / 2, y); g.setColor(saved); } /** * This method paints all the labels from the slider's label table. This * method must make sure that the label table is not null before painting * the labels. Each entry in the label table is a (integer, component) * pair. Every label is painted at the value of the integer. * * @param g The {@link Graphics} object to draw with. */ public void paintLabels(Graphics g) { if (slider.getLabelTable() != null) { Dictionary table = slider.getLabelTable(); Integer tmpKey; Object key; Object element; Component label; if (slider.getOrientation() == JSlider.HORIZONTAL) { for (Enumeration list = table.keys(); list.hasMoreElements();) { key = list.nextElement(); if (! (key instanceof Integer)) continue; tmpKey = (Integer) key; element = table.get(tmpKey); // We won't paint them if they're not // JLabels so continue anyway if (! (element instanceof JLabel)) continue; label = (Component) element; paintHorizontalLabel(g, tmpKey.intValue(), label); } } else { for (Enumeration list = table.keys(); list.hasMoreElements();) { key = list.nextElement(); if (! (key instanceof Integer)) continue; tmpKey = (Integer) key; element = table.get(tmpKey); // We won't paint them if they're not // JLabels so continue anyway if (! (element instanceof JLabel)) continue; label = (Component) element; paintVerticalLabel(g, tmpKey.intValue(), label); } } } } /** * This method paints the label on the horizontal slider at the value * specified. The value is not a coordinate. It is a value within the range * of the slider. If the value is not within the range of the slider, this * method will do nothing. This method should not paint outside the * boundaries of the labelRect. * * @param g The {@link Graphics} object to draw with. * @param value The value to paint at. * @param label The label to paint. */ protected void paintHorizontalLabel(Graphics g, int value, Component label) { // This relies on clipping working properly or we'll end up // painting all over the place. If our preferred size is ignored, then // the labels may not fit inside the slider's bounds. Rather than mucking // with font sizes and possible icon sizes, we'll set the bounds for // the label and let it get clipped. Dimension dim = label.getPreferredSize(); int w = (int) dim.getWidth(); int h = (int) dim.getHeight(); int max = slider.getMaximum(); int min = slider.getMinimum(); if (value > max || value < min) return; // value // | // ------------ // | | // | | // | | // The label must move w/2 to the right to fit directly under the value. int xpos = xPositionForValue(value) - w / 2; int ypos = labelRect.y; // We want to center the label around the xPositionForValue // So we use xpos - w / 2. However, if value is min and the label // is large, we run the risk of going out of bounds. So we bring it back // to 0 if it becomes negative. if (xpos < 0) xpos = 0; // If the label + starting x position is greater than // the x space in the label rectangle, we reset it to the largest // amount possible in the rectangle. This means ugliness. if (xpos + w > labelRect.x + labelRect.width) w = labelRect.x + labelRect.width - xpos; // If the label is too tall. We reset it to the height of the label // rectangle. if (h > labelRect.height) h = labelRect.height; label.setBounds(xpos, ypos, w, h); javax.swing.SwingUtilities.paintComponent(g, label, null, label.getBounds()); } /** * This method paints the label on the vertical slider at the value * specified. The value is not a coordinate. It is a value within the range * of the slider. If the value is not within the range of the slider, this * method will do nothing. This method should not paint outside the * boundaries of the labelRect. * * @param g The {@link Graphics} object to draw with. * @param value The value to paint at. * @param label The label to paint. */ protected void paintVerticalLabel(Graphics g, int value, Component label) { Dimension dim = label.getPreferredSize(); int w = (int) dim.getWidth(); int h = (int) dim.getHeight(); int max = slider.getMaximum(); int min = slider.getMinimum(); if (value > max || value < min) return; int xpos = labelRect.x; int ypos = yPositionForValue(value) - h / 2; if (ypos < 0) ypos = 0; if (ypos + h > labelRect.y + labelRect.height) h = labelRect.y + labelRect.height - ypos; if (w > labelRect.width) w = labelRect.width; label.setBounds(xpos, ypos, w, h); javax.swing.SwingUtilities.paintComponent(g, label, null, label.getBounds()); } /** * <p> * This method paints a thumb. There are two types of thumb: * </p> * <pre> * Vertical Horizontal * a---b a-----b * | | | \ * e c | c * \ / | / * d e-----d * </pre> * * <p> * In the case of vertical thumbs, we highlight the path b-a-e-d and shadow * the path b-c-d. In the case of horizontal thumbs, we highlight the path * c-b-a-e and shadow the path c-d-e. In both cases we fill the path * a-b-c-d-e before shadows and highlights are drawn. * </p> * * @param g The graphics object to paint with */ public void paintThumb(Graphics g) { Color saved_color = g.getColor(); Point a = new Point(thumbRect.x, thumbRect.y); Point b = new Point(a); Point c = new Point(a); Point d = new Point(a); Point e = new Point(a); Polygon bright; Polygon light; // light shadow Polygon dark; // dark shadow Polygon all; // This will be in X-dimension if the slider is inverted and y if it isn't. int turnPoint; if (slider.getOrientation() == JSlider.HORIZONTAL) { turnPoint = thumbRect.height * 3 / 4; b.translate(thumbRect.width - 1, 0); c.translate(thumbRect.width - 1, turnPoint); d.translate(thumbRect.width / 2 - 1, thumbRect.height - 1); e.translate(0, turnPoint); bright = new Polygon(new int[] { b.x - 1, a.x, e.x, d.x }, new int[] { b.y, a.y, e.y, d.y }, 4); dark = new Polygon(new int[] { b.x, c.x, d.x + 1 }, new int[] { b.y, c.y - 1, d.y }, 3); light = new Polygon(new int[] { b.x - 1, c.x - 1, d.x + 1 }, new int[] { b.y + 1, c.y - 1, d.y - 1 }, 3); all = new Polygon(new int[] { a.x + 1, b.x - 2, c.x - 2, d.x, e.x + 1 }, new int[] { a.y + 1, b.y + 1, c.y - 1, d.y - 1, e.y }, 5); } else { turnPoint = thumbRect.width * 3 / 4 - 1; b.translate(turnPoint, 0); c.translate(thumbRect.width - 1, thumbRect.height / 2); d.translate(turnPoint, thumbRect.height - 1); e.translate(0, thumbRect.height - 1); bright = new Polygon(new int[] { c.x - 1, b.x, a.x, e.x }, new int[] { c.y - 1, b.y, a.y, e.y - 1 }, 4); dark = new Polygon(new int[] { c.x, d.x, e.x }, new int[] { c.y, d.y, e.y }, 3); light = new Polygon(new int[] { c.x - 1, d.x, e.x + 1}, new int[] { c.y, d.y - 1, e.y - 1}, 3); all = new Polygon(new int[] { a.x + 1, b.x, c.x - 2, c.x - 2, d.x, e.x + 1 }, new int[] { a.y + 1, b.y + 1, c.y - 1, c.y, d.y - 2, e.y - 2 }, 6); } g.setColor(Color.WHITE); g.drawPolyline(bright.xpoints, bright.ypoints, bright.npoints); g.setColor(Color.BLACK); g.drawPolyline(dark.xpoints, dark.ypoints, dark.npoints); g.setColor(Color.GRAY); g.drawPolyline(light.xpoints, light.ypoints, light.npoints); g.setColor(Color.LIGHT_GRAY); g.drawPolyline(all.xpoints, all.ypoints, all.npoints); g.fillPolygon(all); g.setColor(saved_color); } /** * This method sets the position of the thumbRect. * * @param x The new x position. * @param y The new y position. */ public void setThumbLocation(int x, int y) { thumbRect.x = x; thumbRect.y = y; } /** * This method is used to move the thumb one block in the direction * specified. If the slider snaps to ticks, this method is responsible for * snapping it to a tick after the thumb has been moved. * * @param direction The direction to move in. */ public void scrollByBlock(int direction) { // The direction is -1 for backwards and 1 for forwards. int unit = direction * (slider.getMaximum() - slider.getMinimum()) / 10; int moveTo = slider.getValue() + unit; if (slider.getSnapToTicks()) moveTo = findClosestTick(moveTo); slider.setValue(moveTo); } /** * This method is used to move the thumb one unit in the direction * specified. If the slider snaps to ticks, this method is responsible for * snapping it to a tick after the thumb has been moved. * * @param direction The direction to move in. */ public void scrollByUnit(int direction) { // The direction is -1 for backwards and 1 for forwards. int moveTo = slider.getValue() + direction; if (slider.getSnapToTicks()) moveTo = findClosestTick(moveTo); slider.setValue(moveTo); } /** * This method is called when there has been a click in the track and the * thumb needs to be scrolled on regular intervals. This method is only * responsible for starting the timer and not for stopping it. * * @param dir The direction to move in. */ protected void scrollDueToClickInTrack(int dir) { scrollTimer.stop(); scrollListener.setDirection(dir); scrollListener.setScrollByBlock(true); scrollTimer.start(); } /** * This method returns the X coordinate for the value passed in. * * @param value The value to calculate an x coordinate for. * * @return The x coordinate for the value. */ protected int xPositionForValue(int value) { int min = slider.getMinimum(); int max = slider.getMaximum(); int len = trackRect.width - 1; int xPos = (max == min) ? 0 : (value - min) * len / (max - min); if (! drawInverted()) xPos += trackRect.x; else { xPos = len - xPos; xPos += trackRect.x; } return xPos; } /** * This method returns the y coordinate for the value passed in. * * @param value The value to calculate a y coordinate for. * * @return The y coordinate for the value. */ protected int yPositionForValue(int value) { int min = slider.getMinimum(); int max = slider.getMaximum(); int len = trackRect.height - 1; int yPos = (max == min) ? 0 : (value - min) * len / (max - min); if (! drawInverted()) { yPos = len - yPos; yPos += trackRect.y; } else yPos += trackRect.y; return yPos; } /** * This method returns the value in the slider's range given the y * coordinate. If the value is out of range, it will return the closest * legal value. * * @param yPos The y coordinate to calculate a value for. * * @return The value for the y coordinate. */ public int valueForYPosition(int yPos) { int min = slider.getMinimum(); int max = slider.getMaximum(); int len = trackRect.height; int value; // If the length is 0, you shouldn't be able to even see where the slider // is. This really shouldn't ever happen, but just in case, we'll return // the middle. if (len == 0) return ((max - min) / 2); if (! drawInverted()) value = ((len - (yPos - trackRect.y)) * (max - min) / len + min); else value = ((yPos - trackRect.y) * (max - min) / len + min); // If this isn't a legal value, then we'll have to move to one now. if (value > max) value = max; else if (value < min) value = min; return value; } /** * This method returns the value in the slider's range given the x * coordinate. If the value is out of range, it will return the closest * legal value. * * @param xPos The x coordinate to calculate a value for. * * @return The value for the x coordinate. */ public int valueForXPosition(int xPos) { int min = slider.getMinimum(); int max = slider.getMaximum(); int len = trackRect.width; int value; // If the length is 0, you shouldn't be able to even see where the slider // is. This really shouldn't ever happen, but just in case, we'll return // the middle
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -