Snap Box to Grid

时间:2018-04-20 04:09:39

标签: java swing user-interface

我正在创建一个小应用程序,用户可以将Box放在托盘上。我已经设法使用50x100 Box使其工作,并通过将鼠标位置四舍五入到最接近的50值来使其与网格对齐。

Nice ratio Box

问题在于,当我使用奇数尺寸的Box时,例如50x110,框会重叠或有间隙,因为我强迫它们移动到最接近的50值。如果我增加这个值,我会在盒子之间产生间隙,如果我把它做得太小,用户就很难对齐盒子,所以它们之间没有间隙

Overlapping Box

Large Gap but looks okay

我想知道是否有人无论如何都要解决这个问题,无论大小和比例如何,让盒子互相对齐/对齐?

以下是生成按钮并将按钮放在托盘上的代码的一部分,底部缺少功能的说明

int package_width = 60;
    int package_height = 100;
    int snap_size = 50; //snap everything into 5 pixel limits
    int count = 0;
    int pallet_width = 400;
    int pallet_height = 400;

    JButton package_left = new JButton("<");
    package_left.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            Selector.selected = "<";
        }
    });

    JPanel pallet = new JPanel();
    pallet.setLayout(null);
    pallet.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {

            if(Selector.selected != "" && Selector.selected != null) {
                Point position = e.getPoint();

                JButton pack = new JButton(Selector.selected + Selector.count);

                int x_pos = -1;
                int y_pos = -1;
                int pack_height = -1;
                int pack_width = -1;
                if(Selector.selected == "<" || Selector.selected == ">") {
                    pack_width = package_height;
                    pack_height = package_width;
                }else {
                    pack_width = package_width;
                    pack_height = package_height;
                }
                pack.setSize(pack_width, pack_height);

                //snap using the height and width
                x_pos = (int)((int)(position.x-pack_width/2)/(pack_width/2))*pack_width/2;
                y_pos = (int)((int)(position.y-pack_height/2)/(pack_height/2))*pack_height/2;
                pack.setLocation(x_pos, y_pos);

                if(collision(pack.getLocation(), snap_size)) {
                    x_pos = -1;
                    y_pos = -1;
                }

                //If the position failed to set, its overlapping
                if (x_pos != -1 && y_pos != -1) {

                    Selector.point_list.add(pack.getLocation());
                    if(Selector.selected == "<" || Selector.selected == ">") {
                        Selector.point_list.add(new Point(pack.getLocation().x+snap_size, pack.getLocation().y));
                    }else{
                        Selector.point_list.add(new Point(pack.getLocation().x, pack.getLocation().y+snap_size));
                    }
                    pallet.add(pack);
                    main_page.repaint();

                    Selector.selected = "";
                }
            }
        }
    }

碰撞功能

    public static boolean collision(Point pack, int snap_size) {
    Iterator<Point> it = Selector.point_list.iterator();
    Point next;
    while(it.hasNext()) {
        next = it.next();
        if(pack.x == next.x && pack.y == next.y) {
            return true;
        }
        if(Selector.selected == "<" || Selector.selected == ">") {              
            if(pack.x+snap_size == next.x && pack.y == next.y) {
                return true;
            }
        }else {
            if(pack.x == next.x && pack.y+snap_size == next.y) {
                return true;
            }
        }
    }
    return false;
}

Selector包含2个静态变量。一个是point_list,其中包含已经包含按钮的所有点的列表。另一个是selected,其用于基于按下的第一个按钮是否具有^,v,&lt;来确定按钮的方向。或者&gt;在它上面。

0 个答案:

没有答案