Hi All,
I am new to swing and graphics stuff, i want draw a line between two buttons by clicking on them. Here i am giving some code, when i enable the paint method, it is not showing buttons.
Can you help me how to proceed with the paint method.
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.awt.Graphics2D.*;
import javax.swing.*;
import layout.TableLayout;
import layout.TableLayoutConstraints;
public class ButtonConnector extends JFrame implements ActionListener{
private JButton hi;
private JButton hello;
private JButton sorry;
public ButtonConnector() {
super("ButtonConnector");
setSize(600,600);
intializeGUI();
}
private void intializeGUI() {
Container container = getContentPane();
JPanel mainPanel = new JPanel();
double border = 10;
double size [][] = {{border,90,90,90,90,90, border}, // columns
{45,45,45,45,45,45}}; // rows
mainPanel.setLayout(new TableLayout(size));
hi = new JButton("Hi");
hi.setActionCommand("Hi");
hi.addActionListener(this);
hello = new JButton("Hello");
hello.setActionCommand("Hello");
hello.addActionListener(this);
sorry = new JButton("Sorry");
hi.setActionCommand("Sorry");
hi.addActionListener(this);
mainPanel.add(hi,"1,3");
mainPanel.add(hello,"3,1");
mainPanel.add(sorry,"3,5");
container.add(mainPanel);
}
public static void main(
String[] args) {
ButtonConnector bConnector = new ButtonConnector();
bConnector.show();
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("Hi")) {
Point p = hi.getLocationOnScreen();
}
if(e.getActionCommand().equals("Hello")) {
Point P = hello.getLocationOnScreen();
}
if(e.getActionCommand().equals("Sorry")) {
Point P = sorry.getLocationOnScreen();
}
}
/* public void paint(Graphics g) {
Graphics2D line = (Graphics2D)g;
}
*/
}