加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

[转载]旋转的按钮(JAVA SWING)

(2014-09-04 11:03:00)
标签:

转载

分类: JavaSE
原文地址:旋转的按钮(JAVA SWING)作者:晴天

空中楼阁,是特效的天地.

第一篇东西,也没有什么好炫耀的,刚刚写了个旋转按钮盘,献给大家[转载]旋转的按钮(JAVA <wbr>SWING).

 

 

package cn.edu.blog.easyhouse.fungui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Point2D;


public class RotateButton implements Runnable{
   
    private static final int NUM_OF_BUTTONS = 10;
    JButton buttons[] ;
    JPanel panel;
    //theta与按钮数目有关.offset用于控制偏移,speed控制按钮盘的速度和方向
    double theta = 0 , offset = 0 , speed = 0.1 ;
    //线程睡眠长度
    long sleep = 24;
    //目前半径
    int currentRaduis;
    //按钮盘中心
    Point2D currentCenter;
    //标记是否停止转动
    boolean isWait = false;
   
    //初始化
    public RotateButton(){
        panel = new JPanel();
        panel.setLayout(null);
        buttons = new JButton[NUM_OF_BUTTONS];
        for(int i = 0 ; i<NUM_OF_BUTTONS ; i++){
            buttons[i] = new JButton(String.valueOf(i));
            panel.add(buttons[i]);
            Dimension d = buttons[i].getPreferredSize();
            buttons[i].setBounds(i, i, d.width, d.height);
        }
        theta = Math.PI * 2 / NUM_OF_BUTTONS;
        panel.addMouseMotionListener(new MML());
        currentCenter = new Point2D.Double();
        layout();
        new Thread(this).start();
    }
   
    //自己布局按钮
    private void layout(){
        Component c = panel.getParent();
        int width , height;
        int center_x = panel.getWidth() / 2 ,
                center_y = panel.getHeight() / 2;
        currentCenter.setLocation(center_x, center_y);
        int raduis = getRaduis(center_x , center_y);
        currentRaduis = raduis;
        double incre = offset;
        for(int i = 0 ; i<NUM_OF_BUTTONS ; i++ , incre += theta){
            width = buttons[i].getWidth();
            height = buttons[i].getHeight();
            buttons[i].setLocation((int)(center_x + raduis * Math.sin(incre)) - width/2,
                    (int)(center_y + raduis * Math.cos(incre)) - height/2);
        }
    }
   
    //获得半径
    private int getRaduis(int center_x , int center_y){
        return (int)(1.0 * Math.min(center_x , center_y) * 2 / 3);
    }
   
    @Override
    public void run(){
        double diPI = Math.PI * 2;
        while(true){
            try{
                Thread.sleep(sleep);
                if(isWait)
                    continue;
                offset += speed;
                if(offset > diPI)
                    offset -= diPI;
                if(offset < 0)
                    offset += diPI;
                layout();
            }catch(InterruptedException ie){
               
            }
        }
    }
   
    //鼠标移动监听器,按钮盘的旋转速度跟鼠标的位置有关.
    private class MML implements MouseMotionListener{
        Point2D mousePoint = new Point2D.Double();
        public void mouseMoved(MouseEvent e){
            Component c = (Component)e.getSource();
            int center_x = c.getWidth() / 2;
            int center_y = c.getHeight() / 2;
            int raduis = getRaduis(center_x , center_y);
            int x = e.getX() , y = e.getY();
            mousePoint.setLocation(x, y);
            //根据位置,改变sleep的值.
            double distance = mousePoint.distance(currentCenter);
            double minus = Math.abs(distance - currentRaduis);
            if(minus < 7){
                isWait = true;
            }else{
                isWait = false;
                if(distance < currentRaduis)
                    speed = speed>0?-speed:speed;
                else speed = speed<0?-speed:speed;
                sleep = (long)(24 +500 / minus);
            }
        }
        public void mouseDragged(MouseEvent e){
           
        }
    }
   
    public static void main(String argv[]){
        JFrame frame = new JFrame("旋转的按钮");
        RotateButton rb = new RotateButton();
        frame.getContentPane().add(rb.panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 300);
        frame.setVisible(true);
    }
}

这个东西是从WOW里获得的灵感,就是那个密保的形式.按钮排成圆形,盘子的旋转与鼠标的位置有关.做得不是很好,见笑了[转载]旋转的按钮(JAVA <wbr>SWING)

 

截图:

http://s6/bmiddle/59a943d54534c168d2785SWING)" TITLE="[转载]旋转的按钮(JAVA SWING)" />

0

  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有