Advertisement
redandwhite

BarDrawer

Aug 27th, 2012
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.95 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package ui;
  6.  
  7. import java.awt.*;
  8. import java.awt.geom.RoundRectangle2D;
  9. import java.io.IOException;
  10. import java.util.LinkedHashMap;
  11. import java.util.Map;
  12. import javax.swing.JFrame;
  13. import javax.swing.JPanel;
  14.  
  15. /**
  16.  *
  17.  * @author Administrator
  18.  */
  19. public class BarDrawer extends JPanel {
  20.  
  21.     private int colorIndex;
  22.     private Color[] colors = {
  23.     Color.decode("#DE683A"),
  24.     Color.decode("#DEB53A"),
  25.     Color.decode("#B2DE3A"),
  26.     Color.decode("#3ADEAD"),
  27.     Color.decode("#3A65DE"),
  28.     Color.decode("#973ADE"),
  29.     Color.decode("#DE3A3A")
  30.     };
  31.     public LinkedHashMap<FileCollectionPanel, Double> collectionMap;
  32.  
  33.     public BarDrawer(LinkedHashMap<FileCollectionPanel, Double> collectionMap) throws IOException {
  34.     this.collectionMap = collectionMap;
  35.  
  36.     this.collectionMap = new LinkedHashMap<FileCollectionPanel, Double>();
  37. //  collectionMap.put("Pictures", 13d);
  38. //  collectionMap.put("Music", 12d);
  39. //  collectionMap.put("Videos", 13d);
  40. //  collectionMap.put("TXT Files", 12d);
  41. //  collectionMap.put("EXE Files", 16d);
  42. //  collectionMap.put("Compressed Files", 10d);
  43. //  collectionMap.put("Downloads Folder", 1d);
  44. //  collectionMap.put("Another collection", 22d);
  45.  
  46.  
  47. //  collectionMap.put("Pictures", 66d);
  48. //  collectionMap.put("Music", 1d);
  49. //  collectionMap.put("Videos", 33d);
  50.  
  51.  
  52.     FileCollectionPanel fcp1 = new FileCollectionPanel();
  53.     fcp1.setTitle("Pictures");
  54.     FileCollectionPanel fcp2 = new FileCollectionPanel();
  55.     fcp2.setTitle("Music");
  56.     FileCollectionPanel fcp3 = new FileCollectionPanel();
  57.     fcp3.setTitle("Videos");
  58. //
  59.     this.collectionMap.put(fcp1, 66d);
  60.     this.collectionMap.put(fcp2, 11d);
  61.     this.collectionMap.put(fcp3, 23d);
  62.     }
  63.  
  64.     @Override
  65.     protected void paintComponent(Graphics gr) {
  66.     super.paintComponent(gr);
  67.     Graphics2D g = (Graphics2D) gr;
  68.     setPreferredSize(new Dimension(getWidth(), 150));
  69.  
  70.     Map desktopHints = (Map) (Toolkit.getDefaultToolkit().getDesktopProperty("awt.font.desktophints"));
  71.     if (desktopHints != null) {
  72.         g.addRenderingHints(desktopHints);
  73.     }
  74.  
  75.     g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  76.         RenderingHints.VALUE_ANTIALIAS_ON);
  77.  
  78.     colorIndex = 0;
  79.     int realWidth = getWidth() - 1;
  80.     int heightOfBar = (int) (getHeight() - 50);
  81.  
  82. //  g.setColor(Color.decode("#cccccc"));
  83.     double radius = getHeight() / 3;
  84.     RoundRectangle2D r = new RoundRectangle2D.Double(0, 0, realWidth, heightOfBar, radius, radius * 2);
  85.  
  86.     g.setClip(getBounds());
  87.     g.setColor(Color.black);
  88.     double barSegment = realWidth / collectionMap.size();
  89.     if (barSegment > 120) {
  90.         barSegment = 120;
  91.     }
  92.     int collectionNameX = 0;
  93.  
  94.     int x = 0;
  95.     for (FileCollectionPanel fcp : collectionMap.keySet()) {
  96.  
  97.         g.setColor(getNextColor());
  98.  
  99.         double percentage = collectionMap.get(fcp) + adjustmentNeeded(collectionMap, fcp);
  100.         double width = (((double) percentage / 100) * realWidth);
  101.  
  102.         g.setClip(r);
  103.         g.fillRect(x, 0, (int) width, heightOfBar);
  104.  
  105.         g.setFont(g.getFont().deriveFont(14f));
  106.         String titleShortened = fcp.getTitle();
  107.         int lettersToRemove = 1;
  108.         while (g.getFontMetrics().stringWidth(titleShortened) > barSegment - 15) {
  109.         titleShortened = shortenString(fcp.getTitle(), lettersToRemove);
  110.         lettersToRemove++;
  111.         titleShortened += "...";
  112.         }
  113.  
  114.         g.setClip(getBounds());
  115.         g.setClip(0, 0, getWidth(), getHeight());
  116.         g.fillRect(collectionNameX + 15, heightOfBar + 10, 10, 10);
  117.         g.setColor(Color.black);
  118.         g.drawString(titleShortened, collectionNameX + 30, heightOfBar + 20);
  119.         System.out.println("heightOfBar = " + heightOfBar);
  120.         System.out.println("collectionNameX = " + collectionNameX);
  121.         g.setFont(g.getFont().deriveFont(11f));
  122.         g.drawString(Double.toString(collectionMap.get(fcp)) + "%", collectionNameX + 30, heightOfBar + 20 + g.getFontMetrics().getHeight());
  123.  
  124.         collectionNameX += barSegment;
  125.         x = x + (int) width;
  126.     }
  127.  
  128.     // draw lines
  129.     g.setClip(r);
  130.     for (int lineX = 15; lineX < realWidth - 1; lineX += 25) {
  131.         g.setColor(new Color(255, 255, 255, 75));
  132.         g.drawLine(lineX, 0, lineX - 15, heightOfBar - 1);
  133.  
  134.         int gray = 128;
  135.         Color grayTransparent = new Color(gray, gray, gray, 75);
  136.         g.setColor(grayTransparent);
  137.         g.drawLine(lineX - 1, 0, lineX - 16, heightOfBar - 1);
  138.     }
  139.  
  140.     // adding gradient
  141.     Paint p = new GradientPaint(0.0f, 0.0f, new Color(255, 255, 255, 255), 0.0f, getHeight(), new Color(255, 255, 255, 0));
  142.  
  143.     Paint p2 = new LinearGradientPaint(0.0f, 0.0f, getWidth(), getHeight(), new float[]{0.5f, 1.0f}, new Color[]{new Color(255, 255, 255, 255), new Color(255, 255, 255, 0)});
  144.     g.setPaint(p);
  145.     }
  146.  
  147.     public static String shortenString(String s, int lettersToRemove) {
  148.     if (lettersToRemove > s.length()) {
  149.         return Character.toString(s.charAt(0));
  150.     }
  151.     char[] cArr = new char[s.length() - lettersToRemove];
  152.     for (int i = 0; i < cArr.length; i++) {
  153.         cArr[i] = s.charAt(i);
  154.     }
  155.  
  156.     return new String(cArr);
  157.     }
  158.  
  159.     /**
  160.      * Checks for two things. First, it checks that the entry being dealt with
  161.      * is the last entry in a {@link LinkedHashMap LinkedHashMap}. Second, if it
  162.      * is, then the total of the all the values is counted. If the total is less
  163.      * than 100, then the result of this method will be adjustment needed to get
  164.      * to 100. E.g. if the total of all the doubles in the map is 98, and the
  165.      * entry passed is the last one, then this method will return 2. Else, it
  166.      * will return 0.
  167.      *
  168.      * @param map
  169.      * @param value
  170.      * @return
  171.      */
  172.     public double adjustmentNeeded(LinkedHashMap<FileCollectionPanel, Double> map, FileCollectionPanel fcp) {
  173.     double minVal = Double.MAX_VALUE;
  174.     FileCollectionPanel smallestFcp = null;
  175.     for (FileCollectionPanel currentCollection : map.keySet()) {
  176.         double val = map.get(currentCollection);
  177.         if (minVal > val) {
  178.         minVal = val;
  179.         smallestFcp = currentCollection;
  180.         }
  181.     }
  182.  
  183.     double total = 0;
  184.     for (Double val : map.values()) {
  185.         total += val;
  186.     }
  187.  
  188.     if ((total < 100d) && (smallestFcp.equals(fcp))) {
  189.         return 100d - total;
  190.     }
  191.  
  192.     return 0;
  193.     }
  194.  
  195.     public Color getSelectedColor() {
  196.     return colors[colorIndex];
  197.     }
  198.  
  199.     public Color getNextColor() {
  200.     if (colorIndex < 0 || colorIndex >= colors.length - 1) {
  201.         colorIndex = 0;
  202.     } else {
  203.         colorIndex++;
  204.     }
  205.  
  206.     return colors[colorIndex];
  207.     }
  208.  
  209.     public static void main(String[] args) throws IOException {
  210.     JFrame frame = new JFrame();
  211.  
  212.     frame.add(new BarDrawer(null));
  213.     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  214.     frame.setSize(600, 150);
  215.     frame.setVisible(true);
  216.     }
  217. }
  218.  
  219. class FileCollectionPanel extends JPanel {
  220.  
  221.     private String title;
  222.  
  223.     public FileCollectionPanel() {
  224.     }
  225.  
  226.     /**
  227.      * @return the title
  228.      */
  229.     public String getTitle() {
  230.     return title;
  231.     }
  232.  
  233.     /**
  234.      * @param title the title to set
  235.      */
  236.     public void setTitle(String title) {
  237.     this.title = title;
  238.     }
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement