Java 如何检查两个按钮之间的线路?

Java 如何检查两个按钮之间的线路?,java,swing,jbutton,paintcomponent,jradiobutton,Java,Swing,Jbutton,Paintcomponent,Jradiobutton,如何检查借助于两个单选按钮之间的drawline功能绘制的线这些按钮之间是否存在帮助我检查线的功能 import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.*; import javax.swing.*; /** * @see http://stackoverflow.com/a/12389479/909085 */ public

如何检查借助于两个单选按钮之间的drawline功能绘制的线这些按钮之间是否存在帮助我检查线的功能

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;

/**
 * @see http://stackoverflow.com/a/12389479/909085
 */
public class ComponentLinkerTest extends JComponent {
    // private Map<JComponent, JComponent> linked;
    Map<JComponent, java.util.List<JComponent>> linked;// = new HashMap<>();
    int n = 1;

    public ComponentLinkerTest() {
        super();
        linked = new HashMap();
    }
    static JRadioButton[] button = new JRadioButton[25];

    public void gui() {
        setupLookAndFeel();
        JFrame frame = new JFrame();
        linker = new ComponentLinkerTest();
        frame.setGlassPane(linker);
        linker.setVisible(true);
        JPanel content = new JPanel();
        content.setLayout(new GridLayout(5, 5, 5, 5));
        content.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        frame.add(content);
        int i;
        for (i = 0; i < 25; i++) {
            // final JButton button = new JButton ( "Button" + i );
            button[i] = new JRadioButton();
            //  panel.add(fontButtons[i]);
            button[i].addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    link((JRadioButton) e.getSource());
                }
            });
            content.add(button[i]);
        }
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    /*public void link ( JComponent c1, JComponent c2 )
     {
     linked.put ( c1, c2 );
     repaint ();
     }*/
    public void link(JComponent c1, JComponent c2) {
        if (linked.containsKey(c1)) {
            linked.get(c1).add(c2);
        } else {
            java.util.List<JComponent> list = new LinkedList<>();
            list.add(c2);
            linked.put(c1, list);
        }
        repaint();
    }

    /*  protected void paintComponent ( Graphics g )
     {
     Graphics2D g2d = ( Graphics2D ) g;
     g2d.setRenderingHint ( RenderingHints.KEY_ANTIALIASING, 
        RenderingHints.VALUE_ANTIALIAS_ON );
     g2d.setPaint ( Color.BLACK );
     for ( JComponent c1 : linked.keySet () )
     {
     Point p1 = getRectCenter ( getBoundsInWindow ( c1 ) );
     Point p2 = getRectCenter ( getBoundsInWindow ( linked.get ( c1 ) ) );
     /* Stroke stroke = new BasicStroke(8//,
     /*BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0,
     new float[] { 12, 12 }, 0);
     g2d.setStroke(stroke);
     g2d.setColor(Color.RED);
     g2d.drawLine ( p1.x, p1.y, p2.x, p2.y );
     }
     }*/
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
                RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setPaint(Color.BLACK);
        for (JComponent c1 : linked.keySet()) {
            for (JComponent c2 : linked.get(c1)) {
                Point p1 = getRectCenter(getBoundsInWindow(c1));
                Point p2 = getRectCenter(getBoundsInWindow(c2));
                /* Stroke stroke = new BasicStroke(8//,
                 /*BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0,
                 new float[] { 12, 12 }, 0);
                 g2d.setStroke(stroke);*/
                if (n == 1) {
                    g2d.setColor(Color.RED);
                    n = 2;
                } else {
                    g2d.setColor(Color.BLUE);
                    n = 1;
                }
                g2d.drawLine(p1.x, p1.y, p2.x, p2.y);
            }
        }
    }

    private Point getRectCenter(Rectangle rect) {
        return new Point(rect.x + rect.width / 2, rect.y + rect.height / 2);
    }

    private Rectangle getBoundsInWindow(Component component) {
        return getRelativeBounds(component, getRootPaneAncestor(component));
    }

    private Rectangle getRelativeBounds(Component component, Component relativeTo) {
        return new Rectangle(getRelativeLocation(component, relativeTo),
                component.getSize());
    }

    private Point getRelativeLocation(Component component, Component relativeTo) {
        Point los = component.getLocationOnScreen();
        Point rt = relativeTo.getLocationOnScreen();
        return new Point(los.x - rt.x, los.y - rt.y);
    }

    private JRootPane getRootPaneAncestor(Component c) {
        for (Container p = c.getParent(); p != null; p = p.getParent()) {
            if (p instanceof JRootPane) {
                return (JRootPane) p;
            }
        }
        return null;
    }

    public boolean contains(int x, int y) {
        return false;
    }
    private static ComponentLinkerTest linker;

    public static void main(String[] args) {
        ComponentLinkerTest ct = new ComponentLinkerTest();
        ct.gui();
    }
    private static JRadioButton last = null;

    private static void link(JRadioButton buton) {
        int a = 0;
        int i;
        if (last == null) {
            last = buton;
            System.out.println(last.getX());
        } else {
            for (i = 0; i < 25; i++) {
                if (buton == button[i]) {
                    /*if(button[i-1] == last || button[i+1]==last 
                     * || button[i-5] == last || button[i+5]==last)*/
                    if ((i > 0 && button[i - 1] == last)
                            || (i < (button.length - 1) && button[i + 1] == last)
                            || (i > 5 && button[i - 5] == last)
                            || (i < (button.length - 1) && button[i - 5] == last)) {
                        System.out.println("in cond");
                        linker.link(last, buton);
                        buton.setSelected(false);
                        last.setSelected(false);
                        last = null;
                    } else {
                        System.out.println("out cond");
                        buton.setSelected(false);
                        last.setSelected(false);
                        last = null;
                        JOptionPane.showMessageDialog(null, "Wrong position clicked ");
                    }
                    break;
                } else {
                    System.out.println("button not found");
                }
            }

        }
    }

    private static void setupLookAndFeel() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }
    }
}
import java.awt.*;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.util.*;
导入javax.swing.*;
/**
*@见http://stackoverflow.com/a/12389479/909085
*/
公共类组件LinkerTest扩展了JComponent{
//私人地图连接;
映射链接;/=new HashMap();
int n=1;
公共组件LinkerTest(){
超级();
linked=新HashMap();
}
静态JRadioButton[]按钮=新JRadioButton[25];
公共用户界面(){
setupLookAndFeel();
JFrame=新JFrame();
链接器=新组件LinkerTest();
frame.setGlassPane(链接器);
linker.setVisible(true);
JPanel content=新的JPanel();
setLayout(新的GridLayout(5,5,5,5));
content.setboorder(BorderFactory.createEmptyBorder(5,5,5,5));
框架。添加(内容);
int i;
对于(i=0;i<25;i++){
//最终JButton按钮=新JButton(“按钮”+i);
按钮[i]=新的JRadioButton();
//面板。添加(按钮[i]);
按钮[i].addActionListener(新建ActionListener()){
已执行的公共无效操作(操作事件e){
链接((JRadioButton)e.getSource());
}
});
content.add(按钮[i]);
}
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
/*公共无效链接(JC组件c1、JC组件c2)
{
连接。放置(c1,c2);
重新油漆();
}*/
公共无效链接(JC组件c1、JC组件c2){
if(链接的容器(c1)){
链接。获取(c1)。添加(c2);
}否则{
java.util.List List=新建LinkedList();
增加(c2);
链接。放置(c1,列表);
}
重新油漆();
}
/*受保护组件(图形g)
{
Graphics2D g2d=(Graphics2D)g;
g2d.setRenderingHint(RenderingHits.KEY_抗锯齿,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setPaint(颜色:黑色);
对于(JComponent c1:linked.keySet())
{
点p1=getRectCenter(getBoundsInWindow(c1));
点p2=getRectCenter(getBoundsInWindow(linked.get(c1));
/*冲程=新的基本冲程(8//,
/*基本行程.端盖对接,基本行程.连接坡口,0,
新的浮点[]{12,12},0);
g2d.设定行程(行程);
g2d.setColor(Color.RED);
g2d.绘制线(p1.x、p1.y、p2.x、p2.y);
}
}*/
@凌驾
受保护组件(图形g){
超级组件(g);
Graphics2D g2d=(Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_抗锯齿,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setPaint(颜色:黑色);
对于(JComponentC1:linked.keySet()){
for(JComponentC2:linked.get(c1)){
点p1=getRectCenter(getBoundsInWindow(c1));
点p2=getRectCenter(getBoundsInWindow(c2));
/*冲程=新的基本冲程(8//,
/*基本行程.端盖对接,基本行程.连接坡口,0,
新的浮点[]{12,12},0);
g2d.设定行程(行程)*/
如果(n==1){
g2d.setColor(Color.RED);
n=2;
}否则{
g2d.setColor(Color.BLUE);
n=1;
}
g2d.绘制线(p1.x、p1.y、p2.x、p2.y);
}
}
}
专用点getRectCenter(矩形rect){
返回新点(矩形x+矩形宽度/2,矩形y+矩形高度/2);
}
私有矩形getBoundsInWindow(组件){
返回getRelativeBounds(组件,getRootPaneAncestor(组件));
}
私有矩形getRelativeBounds(组件组件,组件相对){
返回新矩形(getRelativeLocation(component,relativeTo),
getSize());
}
私有点getRelativeLocation(组件组件,组件相对){
点服务水平=组件。getLocationOnScreen();
点rt=相对于屏幕上的GetLocationOn();
返回新点(los.x-rt.x,los.y-rt.y);
}
专用JRootPane getRootPaneAncestor(组件c){
对于(容器p=c.getParent();p!=null;p=p.getParent()){
if(JRootPane的p实例){
返回(JRootPane)p;
}
}
返回null;
}
公共布尔包含(int x,int y){
返回false;
}
私有静态组件链接器;
公共静态void main(字符串[]args){
ComponentLinkerTest ct=新的ComponentLinkerTest();
ct.gui();
}
私有静态JRadioButton last=null;
专用静态无效链接(JRadioButton buton){
int a=0;
int i;
if(last==null){
最后=布顿;
System.out.println(last.getX());
}否则{
对于(i=0;i<25;i++){
如果(buton==按钮[i]){
/*如果(按钮[i-1]==last | |按钮[i+1]==last
*| |按钮[i-5]==最后一个| |按钮[i+5]==最后一个)*/
if((i>0&&button[i-1]==last)
||(i<(按钮长度-1)和按钮[i+1]==最后一个)
||(i>5&&按钮[i-5]==最后一个)
||(i<(按钮长度-1)和按钮[i-5]==最后一个)){
System.out.println(“in cond”);
linker.link(最后一个,buton);
buton.setSelected(假);