How to draw a line between to buttons
hello,
in my application i have to draw a line between to buttons if and only if the buttons are active.
my output depends on the values given by an sensor board which connect through serial port...everything i working fine but i'm unable to show a line between two buttion which are in JPanel ..
there is one condition to show the line ..i want to show the line only if buttons are added to JPanel..
buttons are added at runtime depending on the output of sensor board.
at the max i have three sensor node so at tun time i may get 2 buttons or 3 buttons(for ex: if i get 2 buttons j1 and j2 i have to draw line betwwn j1 and j2 and if i get three buttons j1,j2 and j3 then i have to draw lines between j1 and j2& j1 and j3).
please help me how to draw the line..i tried to draw line using paint(graphics g) but the lines were drawn even if i dont have a single button..
thank you very much..
# 1
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Line2D;
import javax.swing.*;
public class RuntimeLines extends JPanel implements ActionListener {
public RuntimeLines() {
super(new GridBagLayout());
GridBagConstraints gbc = getInitialConstraints();
add(new JButton("Button 1"), gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(new JButton("Button 2"), gbc);
}
private GridBagConstraints getInitialConstraints() {
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1.0;
gbc.weighty = 1.0;
return gbc;
}
public void actionPerformed(ActionEvent e) {
String ac = e.getActionCommand();
int n = getComponentCount();
if(ac.equals("ADD")) {
GridBagLayout gridbag = (GridBagLayout)getLayout();
GridBagConstraints gbc = (n==0) ? getInitialConstraints()
: gridbag.getConstraints(getComponent(n-1));
gbc.gridwidth = ((n+1)%2 == 0) ? GridBagConstraints.REMAINDER
: GridBagConstraints.RELATIVE;
add(new JButton("Button " + (n+1)), gbc);
}
if(ac.equals("REMOVE") && n > 0)
remove(n-1);
revalidate();
repaint();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(Color.blue);
Component[] c = getComponents();
for(int j = 0; j < c.length; j++) {
Rectangle r = c[j].getBounds();
double x1 = r.getCenterX();
double y1 = r.getCenterY();
for(int k = j+1; k < c.length; k++) {
r = c[k].getBounds();
double x2 = r.getCenterX();
double y2 = r.getCenterY();
g2.draw(new Line2D.Double(x1,y1,x2,y2));
}
}
}
private JPanel getUIPanel() {
String[] ids = { "add", "remove" };
JPanel panel = new JPanel();
for(int j = 0; j < ids.length; j++) {
JButton button = new JButton(ids[j]);
button.setActionCommand(ids[j].toUpperCase());
button.addActionListener(this);
panel.add(button);
}
return panel;
}
public static void main(String[] args) {
RuntimeLines test = new RuntimeLines();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(test);
f.getContentPane().add(test.getUIPanel(), "Last");
f.setSize(300,200);
f.setLocation(200,200);
f.setVisible(true);
}
}