• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

how to draw line between two buttons by clicking

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;

}
*/

}
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You
 
Why does your bag say "bombs"? The reason I ask is that my bag says "tiny ads" and it has stuff like this:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic