810 * of the filled portion of the progress bar. 811 * @param amountFull size of the fill region, either width or height 812 * depending upon orientation. 813 * @param b Insets of the progress bar. 814 */ 815 private void paintString(Graphics g, int x, int y, int width, int height, 816 int fillStart, int amountFull, Insets b) { 817 if (!(g instanceof Graphics2D)) { 818 return; 819 } 820 821 Graphics2D g2 = (Graphics2D)g; 822 String progressString = progressBar.getString(); 823 g2.setFont(progressBar.getFont()); 824 Point renderLocation = getStringPlacement(g2, progressString, 825 x, y, width, height); 826 Rectangle oldClip = g2.getClipBounds(); 827 828 if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) { 829 g2.setColor(getSelectionBackground()); 830 SwingUtilities2.drawString(progressBar, g2, progressString, 831 renderLocation.x, renderLocation.y); 832 g2.setColor(getSelectionForeground()); 833 g2.clipRect(fillStart, y, amountFull, height); 834 SwingUtilities2.drawString(progressBar, g2, progressString, 835 renderLocation.x, renderLocation.y); 836 } else { // VERTICAL 837 g2.setColor(getSelectionBackground()); 838 AffineTransform rotate = 839 AffineTransform.getRotateInstance(Math.PI/2); 840 g2.setFont(progressBar.getFont().deriveFont(rotate)); 841 renderLocation = getStringPlacement(g2, progressString, 842 x, y, width, height); 843 SwingUtilities2.drawString(progressBar, g2, progressString, 844 renderLocation.x, renderLocation.y); 845 g2.setColor(getSelectionForeground()); 846 g2.clipRect(x, fillStart, width, amountFull); 847 SwingUtilities2.drawString(progressBar, g2, progressString, 848 renderLocation.x, renderLocation.y); 849 } 850 g2.setClip(oldClip); 851 } 852 853 854 /** 855 * Designate the place where the progress string will be painted. 856 * This implementation places it at the center of the progress 857 * bar (in both x and y). Override this if you want to right, 858 * left, top, or bottom align the progress string or if you need 859 * to nudge it around for any reason. 860 * 861 * @param g an instance of {@code Graphics} 862 * @param progressString a text 863 * @param x an X coordinate 864 * @param y an Y coordinate 865 * @param width a width 866 * @param height a height 867 * @return the place where the progress string will be painted 868 */ 869 protected Point getStringPlacement(Graphics g, String progressString, 870 int x,int y,int width,int height) { 871 FontMetrics fontSizer = SwingUtilities2.getFontMetrics(progressBar, g, 872 progressBar.getFont()); 873 int stringWidth = SwingUtilities2.stringWidth(progressBar, fontSizer, 874 progressString); 875 876 if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) { 877 return new Point(x + Math.round(width/2 - stringWidth/2), 878 y + ((height + 879 fontSizer.getAscent() - 880 fontSizer.getLeading() - 881 fontSizer.getDescent()) / 2)); 882 } else { // VERTICAL 883 return new Point(x + ((width - fontSizer.getAscent() + 884 fontSizer.getLeading() + fontSizer.getDescent()) / 2), 885 y + Math.round(height/2 - stringWidth/2)); 886 } 887 } 888 889 890 public Dimension getPreferredSize(JComponent c) { 891 Dimension size; 892 Insets border = progressBar.getInsets(); 893 FontMetrics fontSizer = progressBar.getFontMetrics( 894 progressBar.getFont()); 895 896 if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) { 897 size = new Dimension(getPreferredInnerHorizontal()); 898 // Ensure that the progress string will fit 899 if (progressBar.isStringPainted()) { 900 // I'm doing this for completeness. 901 String progString = progressBar.getString(); 902 int stringWidth = SwingUtilities2.stringWidth( 903 progressBar, fontSizer, progString); 904 if (stringWidth > size.width) { 905 size.width = stringWidth; 906 } 907 // This uses both Height and Descent to be sure that 908 // there is more than enough room in the progress bar 909 // for everything. 910 // This does have a strange dependency on 911 // getStringPlacememnt() in a funny way. 912 int stringHeight = fontSizer.getHeight() + 913 fontSizer.getDescent(); 914 if (stringHeight > size.height) { 915 size.height = stringHeight; 916 } 917 } 918 } else { 919 size = new Dimension(getPreferredInnerVertical()); 920 // Ensure that the progress string will fit. 921 if (progressBar.isStringPainted()) { 922 String progString = progressBar.getString(); 923 int stringHeight = fontSizer.getHeight() + 924 fontSizer.getDescent(); 925 if (stringHeight > size.width) { 926 size.width = stringHeight; 927 } 928 // This is also for completeness. 929 int stringWidth = SwingUtilities2.stringWidth( 930 progressBar, fontSizer, progString); 931 if (stringWidth > size.height) { 932 size.height = stringWidth; 933 } 934 } 935 } 936 937 size.width += border.left + border.right; 938 size.height += border.top + border.bottom; 939 return size; 940 } 941 942 /** 943 * The Minimum size for this component is 10. The rationale here 944 * is that there should be at least one pixel per 10 percent. 945 */ 946 public Dimension getMinimumSize(JComponent c) { 947 Dimension pref = getPreferredSize(progressBar); 948 if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) { 949 pref.width = 10; | 810 * of the filled portion of the progress bar. 811 * @param amountFull size of the fill region, either width or height 812 * depending upon orientation. 813 * @param b Insets of the progress bar. 814 */ 815 private void paintString(Graphics g, int x, int y, int width, int height, 816 int fillStart, int amountFull, Insets b) { 817 if (!(g instanceof Graphics2D)) { 818 return; 819 } 820 821 Graphics2D g2 = (Graphics2D)g; 822 String progressString = progressBar.getString(); 823 g2.setFont(progressBar.getFont()); 824 Point renderLocation = getStringPlacement(g2, progressString, 825 x, y, width, height); 826 Rectangle oldClip = g2.getClipBounds(); 827 828 if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) { 829 g2.setColor(getSelectionBackground()); 830 getTextUIDrawing().drawString(progressBar, g2, progressString, 831 renderLocation.x, renderLocation.y); 832 g2.setColor(getSelectionForeground()); 833 g2.clipRect(fillStart, y, amountFull, height); 834 getTextUIDrawing().drawString(progressBar, g2, progressString, 835 renderLocation.x, renderLocation.y); 836 } else { // VERTICAL 837 g2.setColor(getSelectionBackground()); 838 AffineTransform rotate = 839 AffineTransform.getRotateInstance(Math.PI/2); 840 g2.setFont(progressBar.getFont().deriveFont(rotate)); 841 renderLocation = getStringPlacement(g2, progressString, 842 x, y, width, height); 843 getTextUIDrawing().drawString(progressBar, g2, progressString, 844 renderLocation.x, renderLocation.y); 845 g2.setColor(getSelectionForeground()); 846 g2.clipRect(x, fillStart, width, amountFull); 847 getTextUIDrawing().drawString(progressBar, g2, progressString, 848 renderLocation.x, renderLocation.y); 849 } 850 g2.setClip(oldClip); 851 } 852 853 854 /** 855 * Designate the place where the progress string will be painted. 856 * This implementation places it at the center of the progress 857 * bar (in both x and y). Override this if you want to right, 858 * left, top, or bottom align the progress string or if you need 859 * to nudge it around for any reason. 860 * 861 * @param g an instance of {@code Graphics} 862 * @param progressString a text 863 * @param x an X coordinate 864 * @param y an Y coordinate 865 * @param width a width 866 * @param height a height 867 * @return the place where the progress string will be painted 868 */ 869 protected Point getStringPlacement(Graphics g, String progressString, 870 int x,int y,int width,int height) { 871 FontMetrics fontSizer = SwingUtilities2.getFontMetrics(progressBar, g, 872 progressBar.getFont()); 873 int stringWidth = getTextUIDrawing().getStringWidth(progressBar, fontSizer, 874 progressString); 875 876 if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) { 877 return new Point(x + Math.round(width/2 - stringWidth/2), 878 y + ((height + 879 fontSizer.getAscent() - 880 fontSizer.getLeading() - 881 fontSizer.getDescent()) / 2)); 882 } else { // VERTICAL 883 return new Point(x + ((width - fontSizer.getAscent() + 884 fontSizer.getLeading() + fontSizer.getDescent()) / 2), 885 y + Math.round(height/2 - stringWidth/2)); 886 } 887 } 888 889 890 public Dimension getPreferredSize(JComponent c) { 891 Dimension size; 892 Insets border = progressBar.getInsets(); 893 FontMetrics fontSizer = progressBar.getFontMetrics( 894 progressBar.getFont()); 895 896 if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) { 897 size = new Dimension(getPreferredInnerHorizontal()); 898 // Ensure that the progress string will fit 899 if (progressBar.isStringPainted()) { 900 // I'm doing this for completeness. 901 String progString = progressBar.getString(); 902 int stringWidth = getTextUIDrawing().getStringWidth( 903 progressBar, fontSizer, progString); 904 if (stringWidth > size.width) { 905 size.width = stringWidth; 906 } 907 // This uses both Height and Descent to be sure that 908 // there is more than enough room in the progress bar 909 // for everything. 910 // This does have a strange dependency on 911 // getStringPlacememnt() in a funny way. 912 int stringHeight = fontSizer.getHeight() + 913 fontSizer.getDescent(); 914 if (stringHeight > size.height) { 915 size.height = stringHeight; 916 } 917 } 918 } else { 919 size = new Dimension(getPreferredInnerVertical()); 920 // Ensure that the progress string will fit. 921 if (progressBar.isStringPainted()) { 922 String progString = progressBar.getString(); 923 int stringHeight = fontSizer.getHeight() + 924 fontSizer.getDescent(); 925 if (stringHeight > size.width) { 926 size.width = stringHeight; 927 } 928 // This is also for completeness. 929 int stringWidth = getTextUIDrawing().getStringWidth( 930 progressBar, fontSizer, progString); 931 if (stringWidth > size.height) { 932 size.height = stringWidth; 933 } 934 } 935 } 936 937 size.width += border.left + border.right; 938 size.height += border.top + border.bottom; 939 return size; 940 } 941 942 /** 943 * The Minimum size for this component is 10. The rationale here 944 * is that there should be at least one pixel per 10 percent. 945 */ 946 public Dimension getMinimumSize(JComponent c) { 947 Dimension pref = getPreferredSize(progressBar); 948 if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) { 949 pref.width = 10; |