左右滚动的滑块。

package Custom;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Polygon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Line2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JToolTip;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.ToolTipManager;
import javax.swing.plaf.basic.BasicComboBoxRenderer;
import javax.swing.plaf.basic.BasicTabbedPaneUI;

public class IndicatorToolbar extends JPanel {

    private void drawLabelIcon(JLabel jLabel, Color color, boolean isBrighter, boolean leftOrRight) {
        Dimension dimension = jLabel.getPreferredSize();
        int height = dimension.height;
        int width = dimension.width;

        BufferedImage brighterImage = new BufferedImage(width, dimension.height, BufferedImage.TYPE_INT_RGB);

        Graphics2D graphics = brighterImage.createGraphics();
        if (isBrighter) {
            graphics.setColor(color.brighter());
        } else {
            graphics.setColor(color.darker());
        }

        if (leftOrRight) {
            graphics.fillPolygon(new int[] { width, 0, width }, new int[] { 0, 0, height }, 3);
        } else {
            graphics.fillPolygon(new int[] { width, 0, 0 }, new int[] { 0, 0, height }, 3);
        }

        graphics.setColor(color.brighter());

//        if (!isBrighter) {
//            float dash1[] = { 5.0f };
//            BasicStroke basicStroke1 = new BasicStroke(0.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash1, 0.0f);
//            graphics.setStroke(basicStroke1);
//        }

        Line2D line = null;
        if (leftOrRight) {
            line = new Line2D.Float(0, 0, width, height);
        } else {
            line = new Line2D.Float(0, height, width, 0);
        }
        graphics.draw(line);

        jLabel.setIcon(new ImageIcon(brighterImage));
    }

    private void drawLabelBottomLine(JLabel jLabel) {

        Dimension dimension = jLabel.getPreferredSize();
        BufferedImage brighterImage = new BufferedImage(dimension.width, dimension.height, BufferedImage.TYPE_INT_RGB);

        Graphics2D graphics = brighterImage.createGraphics();
        graphics.setColor(jLabel.getBackground());
        graphics.fillRect(0, 0, dimension.width, dimension.height);

        graphics.setColor(jLabel.getBackground().brighter());

        int height = dimension.height;

        float dash1[] = { 3.0f, 2.0f };
        BasicStroke basicStroke1 = new BasicStroke(0.1f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1.0f, dash1, 0.0f);
        graphics.setStroke(basicStroke1);

        Line2D line = new Line2D.Float(0, height - 1, dimension.width, height - 1);
        graphics.draw(line);

        jLabel.setIcon(new ImageIcon(brighterImage));

        FontMetrics fontMetrics = jLabel.getFontMetrics(jLabel.getFont());
        int fontWidth = fontMetrics.stringWidth(jLabel.getText());
        fontWidth = (dimension.width-fontWidth)/2+fontWidth;
        jLabel.setIconTextGap(-fontWidth);
    }

    private String[][] indicatores;
    private String[]   menuItems;
    private String[]   indicator;

    public IndicatorToolbar(String[] menuItems, String[][] indicatores, String[] indicator) {
        this.menuItems = menuItems;
        this.indicatores = indicatores;
        this.indicator = indicator;

        this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
        this.setBackground(Color.BLACK);
        this.setOpaque(true);
    }

    private Box box;
    public void renderTabbe(int menuIndex, int startIndex) {
        startIndex = startIndex<0?0:startIndex;
        
        int height = this.getPreferredSize().height;
        int halfHeight = height / 2;
        halfHeight = height%2==0?halfHeight:halfHeight+1;
        
        Font font = getFont();
        int fontSize = (int) Math.round((double) height / 96 * 72);//?/72*96=30/96*72
        font = new Font(font.getName(), font.getStyle(), fontSize);

        this.removeAll();
        this.revalidate();
        this.repaint();
        
        box = Box.createHorizontalBox();

        final Color color = Color.RED;

        int comboBoxWidth = 100;

        ControlableComboBox comboBox = new ControlableComboBox(new String[] { "+", "-" });
        comboBox.setPreferredSize(new Dimension(comboBoxWidth, height));
        comboBox.setMinimumSize(new Dimension(comboBoxWidth, height));
        comboBox.setMaximumSize(new Dimension(comboBoxWidth, height));
        comboBox.setOpaque(true);
        comboBox.setFont(font);

        comboBox.setButtonBackground(color);
        comboBox.setBackground(color);

        for (int k = 0; k < menuItems.length; k++) {
            comboBox.addItem(menuItems[k]);
        }

        BasicComboBoxRenderer defaultListCellRenderer = new BasicComboBoxRenderer() {
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                list.setBackground(color.darker());
                list.setSelectionBackground(list.getBackground().brighter());
                return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            }
        };
        //        defaultListCellRenderer.setPreferredSize(new Dimension(width, height));
        comboBox.setRenderer(defaultListCellRenderer);

        box.add(comboBox);

        JButton jButtonLeft = new JButton();
        jButtonLeft.setPreferredSize(new Dimension(height, height));

        BufferedImage brighterImage1 = new BufferedImage(height, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = brighterImage1.createGraphics();
        if (startIndex == 0) {
            graphics.setColor(color.darker());
        } else {
            graphics.setColor(color);
        }

        int padding = 6;
        int size = height - padding;
        size = size % 2 == 0 ? size - 1 : size;
        int half = size / 2;
        int high = (int) java.lang.Math.sqrt(half * half + half * half);
        int tripHeight = height/3;
        
        Polygon polygonLeft = new Polygon(new int[] { tripHeight + high, tripHeight, tripHeight + high }, new int[] { padding / 2 - 1, half + padding / 2, size + padding / 2 }, 3);
        graphics.fillRect(0, 0, height, height);
        graphics.setColor(Color.BLACK);
        graphics.drawLine(0, 0, 0, height);
        graphics.fillPolygon(polygonLeft);
        graphics.drawImage(brighterImage1, 0, 0, null);

        jButtonLeft.setIcon(new ImageIcon(brighterImage1));

        BufferedImage brighterImage2 = new BufferedImage(height, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics1 = brighterImage2.createGraphics();
        graphics1.setColor(color.darker());
        graphics1.fillRect(0, 0, height, height);
        graphics1.setColor(Color.BLACK);
        graphics1.drawLine(0, 0, 0, height);
        graphics1.fillPolygon(polygonLeft);
        graphics1.drawImage(brighterImage2, 0, 0, null);

        jButtonLeft.setPressedIcon(new ImageIcon(brighterImage2));

        jButtonLeft.setBorderPainted(false);
        jButtonLeft.setMargin(new Insets(0, 0, 0, 0));
        jButtonLeft.setBorder(null);
        jButtonLeft.setOpaque(true);

        if (startIndex>0) {
            class PreviousButton extends MouseAdapter{
                private int sIndex;
                private int mIndex;
                public PreviousButton(int m, int s){
                    mIndex=m;
                    sIndex=s;
                }
                public void mouseClicked(MouseEvent e) {
                    sIndex-=1;
                    SwingUtilities.invokeLater(new Runnable(){
                        public void run() {
                            renderTabbe(mIndex, sIndex);
                        }
                    });
                }
            };
            
            jButtonLeft.addMouseListener(new PreviousButton(menuIndex, startIndex));
        }
        
        box.add(jButtonLeft);

        int width = 50;
        int count = (this.getPreferredSize().width - comboBoxWidth - height * 2) / (halfHeight * 2 + width);
        count = count > indicatores[menuIndex].length ? indicatores[menuIndex].length : count;
        int step = (this.getPreferredSize().width - comboBoxWidth - height * 2) % (halfHeight * 2 + width);

        List<JLabel> list = new ArrayList();

        startIndex = indicatores[menuIndex].length<count+startIndex?indicatores[menuIndex].length-count:startIndex;
        
        for (int i = 0; i < count; i++) {
            String str = indicatores[menuIndex][i + startIndex];
            
            boolean isOn = false;
            for (int k = 0; k < indicator.length; k++) {
                isOn = str.equalsIgnoreCase(indicator[k]);
                if (isOn) {
                    break;
                }
            }

            JLabel label1 = new JLabel();
            label1.setPreferredSize(new Dimension(halfHeight, height));
            label1.setOpaque(true);
            label1.setFont(font);
            drawLabelIcon(label1, color, isOn, true);

            final ToolTipManager toolTipManager = ToolTipManager.sharedInstance();
            toolTipManager.setReshowDelay(0);
            toolTipManager.setInitialDelay(0);

            JLabel labelText = new JLabel(str, SwingConstants.CENTER) {
                private JToolTip jToolTip;
                public JToolTip createToolTip() {
                    if (jToolTip!=null){return jToolTip;}

                    jToolTip = super.createToolTip();
                    jToolTip.setBackground(new Color(251, 223, 157));
                    jToolTip.setBorder(null);
                    return jToolTip;
                }
            };
            
            class TooltipMouseAdapter extends MouseAdapter{
                public void mouseMoved(MouseEvent e) {
                    toolTipManager.mouseExited(e);
                    toolTipManager.mouseEntered(e);
                }
            };

            labelText.addMouseListener(new TooltipMouseAdapter());
            labelText.addMouseMotionListener(new TooltipMouseAdapter());

            int tmpwidth = width + ((step > i + 1) ? 0 : 1);
            labelText.setOpaque(true);
            labelText.setPreferredSize(new Dimension(tmpwidth, height));
            labelText.setMinimumSize(new Dimension(tmpwidth, height));
            labelText.setMaximumSize(new Dimension(tmpwidth, height));
            labelText.setFont(font);
            labelText.setToolTipText(str);

            FontMetrics fontMetrics = labelText.getFontMetrics(font);
            String littlString = str;
            int fontWidth = fontMetrics.stringWidth(littlString);
            if (fontWidth>tmpwidth){
                while(true){
                    labelText.setText(littlString);
                    fontWidth = fontMetrics.stringWidth(littlString+".");
                    if (fontWidth<=tmpwidth){
                        labelText.setText(littlString + ".");
                        break;
                    }
                    littlString = littlString.substring(0, littlString.length()-1);
                }
            }
            
            if (isOn) {
                labelText.setBackground(color.brighter());
            } else {
                labelText.setBackground(color.darker());
                drawLabelBottomLine(labelText);
            }
            list.add(labelText);

            JLabel label2 = new JLabel();
            label2.setPreferredSize(new Dimension(halfHeight, height));
            label2.setOpaque(true);
            label2.setFont(font);
            drawLabelIcon(label2, color, isOn, false);

            box.add(label1);
            box.add(labelText);
            box.add(label2);

        }

        JButton jButtonRight = new JButton();
        jButtonRight.setPreferredSize(new Dimension(height, height));

        Polygon polygonRight = new Polygon(new int[] { tripHeight, tripHeight + high, tripHeight}, new int[] { padding / 2 - 1, half + padding / 2, size + padding / 2 }, 3);
        brighterImage1 = new BufferedImage(height, height, BufferedImage.TYPE_INT_RGB);
        graphics = brighterImage1.createGraphics();
        if (indicatores[menuIndex].length <= (count + startIndex)) {
            graphics.setColor(color.darker());
        } else {
            graphics.setColor(color);
        }
        graphics.fillRect(0, 0, height, height);
        graphics.setColor(Color.BLACK);
//        graphics.drawLine(0, 0, 0, height);
        graphics.fillPolygon(polygonRight);
        graphics.drawImage(brighterImage1, 0, 0, null);
        jButtonRight.setIcon(new ImageIcon(brighterImage1));

        brighterImage2 = new BufferedImage(height, height, BufferedImage.TYPE_INT_RGB);
        graphics1 = brighterImage2.createGraphics();
        graphics1.setColor(color.darker());
        graphics1.fillRect(0, 0, height, height);
        graphics1.setColor(Color.BLACK);
//        graphics1.drawLine(0, 0, 0, height);
        graphics1.fillPolygon(polygonRight);
        graphics1.drawImage(brighterImage2, 0, 0, null);

        jButtonRight.setPressedIcon(new ImageIcon(brighterImage2));

        jButtonRight.setBorderPainted(false);
        jButtonRight.setMargin(new Insets(0, 0, 0, 0));
        jButtonRight.setBorder(null);
        jButtonRight.setOpaque(true);

        if (indicatores[menuIndex].length > (count+ startIndex)) {
            class NextButton extends MouseAdapter{
                private int sIndex;
                private int mIndex;
                public NextButton(int m, int s){
                    mIndex=m;
                    sIndex=s;
                }
                public void mouseClicked(MouseEvent e) {
                    sIndex+=1;
                    SwingUtilities.invokeLater(new Runnable(){
                        public void run() {
                            renderTabbe(mIndex, sIndex);
                        }
                    });
                }
            };
            
            jButtonRight.addMouseListener(new NextButton(menuIndex, startIndex));
        }
        box.add(jButtonRight);
        

        JButton jButtonAdd = new JButton();
        jButtonAdd.setPreferredSize(new Dimension(height, height));

        brighterImage1 = new BufferedImage(height, height, BufferedImage.TYPE_INT_RGB);
        graphics = brighterImage1.createGraphics();
        graphics.setColor(color);
        graphics.fillRect(0, 0, height, height);
        graphics.setColor(Color.BLACK);
        graphics.drawLine(0, 0, 0, height);
        graphics.drawLine(height/4, height/2, height-height/4, height/2);
        graphics.drawLine(height/2, height-height/4, height/2, height/4);
        
        graphics.drawImage(brighterImage1, 0, 0, null);
        jButtonAdd.setIcon(new ImageIcon(brighterImage1));

        brighterImage2 = new BufferedImage(height, height, BufferedImage.TYPE_INT_RGB);
        graphics1 = brighterImage2.createGraphics();
        graphics1.setColor(color.darker());
        graphics1.fillRect(0, 0, height, height);
        graphics1.setColor(Color.BLACK);
        graphics1.drawLine(0, 0, 0, height);
        graphics1.drawLine(height/4, height/2, height-height/4, height/2);
        graphics1.drawLine(height/2, height-height/4, height/2, height/4);
        
        graphics1.drawImage(brighterImage2, 0, 0, null);
        jButtonAdd.setPressedIcon(new ImageIcon(brighterImage2));

        jButtonAdd.setBorderPainted(false);
        jButtonAdd.setMargin(new Insets(0, 0, 0, 0));
        jButtonAdd.setBorder(null);
        jButtonAdd.setOpaque(true);

        jButtonAdd.addMouseListener(new MouseAdapter(){
            public void mouseClicked(MouseEvent e) {
                SwingUtilities.invokeLater(new Runnable(){
                    public void run() {
                        
                    }
                });
            }
        });
        
        box.add(jButtonAdd);
        

        JButton jButtonRemove = new JButton();
        jButtonRemove.setPreferredSize(new Dimension(height, height));

        brighterImage1 = new BufferedImage(height, height, BufferedImage.TYPE_INT_RGB);
        graphics = brighterImage1.createGraphics();
        graphics.setColor(color);
        graphics.fillRect(0, 0, height, height);
        graphics.setColor(Color.BLACK);
        graphics.drawLine(0, 0, 0, height);
        graphics.drawLine(height/4, height/2, height-height/4, height/2);
        
        graphics.drawImage(brighterImage1, 0, 0, null);
        jButtonRemove.setIcon(new ImageIcon(brighterImage1));

        brighterImage2 = new BufferedImage(height, height, BufferedImage.TYPE_INT_RGB);
        graphics1 = brighterImage2.createGraphics();
        graphics1.setColor(color.darker());
        graphics1.fillRect(0, 0, height, height);
        graphics1.setColor(Color.BLACK);
        graphics1.drawLine(0, 0, 0, height);
        graphics1.drawLine(height/4, height/2, height-height/4, height/2);
        
        graphics1.drawImage(brighterImage2, 0, 0, null);

        jButtonRemove.setPressedIcon(new ImageIcon(brighterImage2));

        jButtonRemove.setBorderPainted(false);
        jButtonRemove.setMargin(new Insets(0, 0, 0, 0));
        jButtonRemove.setBorder(null);
        jButtonRemove.setOpaque(true);

        jButtonRemove.addMouseListener(new MouseAdapter(){
            public void mouseClicked(MouseEvent e) {
                SwingUtilities.invokeLater(new Runnable(){
                    public void run() {
                    }
                });
            }
        });
        
        box.add(jButtonRemove);

        this.add(box);
    }

    private static class ABasicTabbedPaneUI extends BasicTabbedPaneUI {

    };

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(800, 100));

        String[] menuItems = new String[] { "ABC", "份额发额服务", "非3 发", "f43f43f4" };

        List list = new ArrayList();

        String[][] indicatores = new String[menuItems.length][];// {new String[] { "LINE.MA", "1" }, new String[] { "BRAND.VOL", null } }
        for (int i = 0; i < 4; i++) {
            Random random = new Random();
            int length1 = random.nextInt(20);
            while (length1 < 2) {
                length1 = random.nextInt(20);
            }
            indicatores[i] = new String[length1];

            for (int k = 0; k < indicatores[i].length; k++) {
                String data = "";
                int length = random.nextInt(20);
                while (length < 2) {
                    length = random.nextInt(20);
                }
                for (int s = 0; s < length; s++) {
                    data += "" + String.valueOf((char) (0x41 + random.nextInt(26)));
                }

                if (random.nextInt(10) > 4) {
                    list.add(data);
                }
                indicatores[i][k] = data;
            }
        }

        String[] array = new String[list.size()];
        list.toArray(array);

        IndicatorToolbar indicatorToolbar = new IndicatorToolbar(menuItems, indicatores, array);
        indicatorToolbar.setPreferredSize(new Dimension(700, 15));

        JPanel jPanel = new JPanel();
        jPanel.setPreferredSize(new Dimension(800, 25));
        jPanel.setBackground(Color.BLACK);
        frame.add(jPanel);

        jPanel.add(indicatorToolbar);

        indicatorToolbar.renderTabbe(0, 0);

        frame.pack();
        frame.setVisible(true);
    }

}

 

posted on 2013-06-08 18:26  過眼云煙  阅读(168)  评论(0)    收藏  举报

导航