Swing自定義組件: SwitchButton

          最近跟JList走的比較近.聽說她最近離婚了..唉...可惜啊.可惜.
          聽JList說 是因為她脾氣比較暴躁..不知道什么時候 該開 什么時候該關.
          說實話..我很喜歡替美女解決煩惱,特別像JList這么漂亮又乖巧得..
          那么.咱們來用一個開關..也許能起到些作用....

          當然.不會讓滑塊瞬間轉移,而是優美得滑過去..這樣才配的上美麗的JList.

          |----------------------------------------------------------------------------------------|
                               版權聲明  版權所有 @chensiyu
                      引用請注明來源 
          www.aygfsteel.com/chensiyu04

                      本文由  陳思羽 于 2011年9月2號 出品..
          |----------------------------------------------------------------------------------------|





          目前還不太完美..有時間在整下.

          package swing;

          import java.awt.BorderLayout;
          import java.awt.Color;
          import java.awt.Dimension;
          import java.awt.GradientPaint;
          import java.awt.Graphics;
          import java.awt.Graphics2D;
          import java.awt.Paint;
          import java.awt.RenderingHints;
          import java.awt.Shape;
          import java.awt.event.ActionEvent;
          import java.awt.event.ActionListener;
          import java.awt.geom.RoundRectangle2D;
          import javax.swing.JFrame;
          import javax.swing.JLabel;
          import javax.swing.JPanel;
          import javax.swing.JToggleButton;
          import javax.swing.SwingUtilities;
          import javax.swing.Timer;

          /**
           *
           * 
          @author chensiyu
           * @createDate 2011/9/2
           * @address www.aygfsteel.com/chensiyu04
           * 
           
          */
          public class SwitchButton extends JToggleButton implements ActionListener {

              
          private static final int ARCNUMBER = 35;
              
          private int currentResolution = 50;
              
          private long cycleStart;
              
          private Timer timer = null;
              
          private final int MOVE_TIME = 2000;
              
          private int moveMinX;
              
          private int moveMaxX;
              
          private int buttonX;
              
          private ActionListener moveActionListener = new ActionListener() {

                  @Override
                  
          public void actionPerformed(ActionEvent e) {
                      startTimer(currentResolution);
                  }
              };

              
          public SwitchButton() {
                  setBorder(
          null);
                  addActionListener(moveActionListener);
              }

              @Override
              
          protected void paintComponent(Graphics g) {
                  
          super.paintComponent(g);
                  Graphics2D g2 
          = (Graphics2D) g;
                  Color oldColor 
          = g2.getColor();
                  g2.setColor(Color.WHITE);
                  g2.fillRect(
          00, getWidth(), getHeight());
                  g2.setColor(oldColor);

                  g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                          RenderingHints.VALUE_ANTIALIAS_ON);
                  Shape backgroundShape 
          = new RoundRectangle2D.Float(11, getWidth() - 2, getHeight() - 2, ARCNUMBER, ARCNUMBER);

                  
          boolean selected = getModel().isSelected();
                  
          //fill shape
                  Paint paint = null;
                  
          if (selected) {
                      paint 
          = new GradientPaint(00new Color(45106169), 0, getHeight(), new Color(135183245));
                  } 
          else {
                      paint 
          = new GradientPaint(00new Color(144144144), 0, getHeight(), new Color(244244244));
                  }
                  g2.setPaint(paint);
                  g2.fill(backgroundShape);
                  
          //draw shape bounds
                  g2.setColor(Color.DARK_GRAY);
                  g2.draw(backgroundShape);

                  JLabel onLabel 
          = new JLabel("ON");
                  JLabel offLabel 
          = new JLabel("OFF");
                  Dimension labelSize;
                  
          //draw text.  off/on
                  if (selected) {
                      oldColor 
          = g2.getColor();
                      g2.setColor(Color.WHITE);
                      labelSize 
          = onLabel.getPreferredSize();
                      g2.drawString(onLabel.getText(), getWidth() 
          / 2 / 2 - labelSize.width / 2, getHeight() / 2 + labelSize.height / 2);
                      g2.setColor(oldColor);
                  } 
          else {
                      oldColor 
          = g2.getColor();
                      g2.setColor(Color.BLACK);
                      labelSize 
          = offLabel.getPreferredSize();
                      g2.drawString(offLabel.getText(), getWidth() 
          / 2 + labelSize.width / 2, getHeight() / 2 + labelSize.height / 2);
                      g2.setColor(oldColor);
                  }
                  
          //draw button
                  int buttonY = 0;
                  
          int buttonWidth = getWidth() / 2;
                  
          int buttonHeight = getHeight();
                  Shape buttonShape 
          = new RoundRectangle2D.Float(buttonX, buttonY, buttonWidth, buttonHeight, ARCNUMBER, ARCNUMBER);
                  paint 
          = new GradientPaint(00new Color(244244244), 0, getHeight(), new Color(144144144));
                  g2.setPaint(paint);
                  g2.fill(buttonShape);
                  g2.setColor(
          new Color(140140140));
                  g2.draw(buttonShape);
              }

              
          public static void main(String[] args) {
                  SwitchButton button 
          = new SwitchButton();
                  button.setBounds(
          601010030);

                  JPanel mainPane 
          = new JPanel();
                  mainPane.setOpaque(
          true);
                  mainPane.setBackground(Color.WHITE);
                  mainPane.setLayout(
          null);
                  mainPane.add(button);

                  
          final JFrame frame = new JFrame();
                  frame.setTitle(
          "SwitchButton Demo");
                  frame.setLayout(
          new BorderLayout());
                  frame.getContentPane().add(mainPane, BorderLayout.CENTER);
                  frame.setSize(
          23380);
                  frame.setLocationRelativeTo(
          null);
                  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  SwingUtilities.invokeLater(
          new Runnable() {

                      @Override
                      
          public void run() {
                          frame.setVisible(
          true);
                      }
                  });
              }

              @Override
              
          public void actionPerformed(ActionEvent e) {
                  
          long currentTime = System.nanoTime() / 1000000;
                  
          long totalTime = currentTime - cycleStart;

                  
          if (totalTime > MOVE_TIME) {
                      cycleStart 
          = currentTime;
                  }
                  
          float fraction = (float) totalTime / MOVE_TIME;
                  fraction 
          = Math.min(1.0f, fraction);
                  fraction 
          = 1 - Math.abs(1 - (2 * fraction));
                  animate(fraction);
              }

              
          public void animate(float fraction) {
                  
          float animationFactor = (float) Math.sin(fraction * (float) Math.PI / 2);
                  animationFactor 
          = Math.min(animationFactor, 1.0f);
                  animationFactor 
          = Math.max(animationFactor, 0.0f);
                  buttonX 
          = moveMinX + (int) (.5f + animationFactor * (float) (moveMaxX - moveMinX));
                  
          if (isSelected()) {
                      
          if (buttonX >= moveMaxX) {
                          timer.stop();
                          timer.setInitialDelay(
          100);
                          buttonX 
          = getWidth() / 2 - 1;
                      }
                  } 
          else {
                      
          if (buttonX - 1 <= moveMaxX) {
                          timer.stop();
                          timer.setInitialDelay(
          100);
                          buttonX 
          = 0;
                      }
                  }
                  repaint();
              }

              
          private void startTimer(int resolution) {
                  
          if (timer == null) {
                      timer 
          = new Timer(resolution, this);
                  }
                  
          if (!timer.isRunning()) {
                      
          if (isSelected()) {
                          moveMinX 
          = 0;
                          moveMaxX 
          = getWidth() / 2 - 1;
                      } 
          else {
                          moveMinX 
          = getWidth() / 2 - 1;
                          moveMaxX 
          = 0;
                      }
                      timer.start();
                  }

              }
          }

          posted on 2011-09-02 15:33 相信 閱讀(2622) 評論(0)  編輯  收藏 所屬分類: Swing文章

          <2025年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          導航

          統計

          公告

          不顯示applet

          常用鏈接

          留言簿(16)

          我參與的團隊

          隨筆檔案

          文章分類

          文章檔案

          新聞檔案

          相冊

          swingchina 專業搞Swing的網站

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 阜平县| 甘南县| 交口县| 浮梁县| 康乐县| 龙泉市| 三门县| 广丰县| 蕲春县| 无锡市| 巴东县| 临朐县| 怀来县| 彭阳县| 阿坝县| 陵川县| 黎平县| 正阳县| 禄劝| 上饶县| 大关县| 巴彦淖尔市| 从化市| 通化市| 晴隆县| 西贡区| 崇州市| 景洪市| 木里| 克山县| 乡宁县| 保德县| 大丰市| 开封县| 中江县| 竹山县| 江源县| 石泉县| 元谋县| 惠州市| 开封市|