• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to draw line between two buttons by clicking  RSS feed

 
vasu LK
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;

}
*/

}
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The Java Tutorial has an in-depth chapter on custom painting. It would be worth your time to give it a read.
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've selected an intricate project. Here are some general comments about it.

Overriding paint in a JFrame is tricky because it will paint over all your components. To verify this (easy enough to do with a small test app as well) look up the paint method in the JFrame api. You find it in the section Methods inherited from class java.awt.Container. Follow the link and read what it does.

A better approach is to use a JPanel, override paintComponent, render your graphics/images and then add your components to it. This will keep the components on top of the graphics.

Also, all graphics are limited to this JPanel and you don't need to be concerned with the JFrame insets or locating other (JFrame child) components in your drawing code.
 
vasu LK
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!