Urgent help need on swing problem
Dear friends,
I met a problem and need urgent help from guru here, I am Swing newbie,
I have following code and hope to draw lines between any two components at RUN-TIME, not at design time
Please throw some skeleton code, Thanks so much!!
code:
package com.swing.test;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.event.*;
publicclass LongguConnectLineCommponent
{
publicstaticvoid main(String[] args)
{
JFrame f =new JFrame("Connecting Lines");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new ConnectionPanel());
f.setSize(400,300);
f.setLocation(200,200);
f.setVisible(true);
}
}
class ConnectionPanelextends JPanel
{
JLabel label1, label2, label3, label4;
JLabel[] labels;
JLabel selectedLabel;
int cx, cy;
public ConnectionPanel()
{
setLayout(null);
addLabels();
label1.setBounds( 25, 50, 125, 25);
label2.setBounds(225, 50, 125, 25);
label3.setBounds( 25, 175, 125, 25);
label4.setBounds(225, 175, 125, 25);
determineCenterOfComponents();
ComponentMover mover =new ComponentMover();
addMouseListener(mover);
addMouseMotionListener(mover);
}
publicvoid paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
Point[] p;
for(int i = 0; i < labels.length; i++)
for(int j = i + 1; j < labels.length; j++)
{
p = getEndPoints(labels[i], labels[j]);
//g2.draw(new Line2D.Double(p[0], p[1]));
}
}
private Point[] getEndPoints(Component c1, Component c2)
{
Point
p1 =new Point(),
p2 =new Point();
Rectangle
r1 = c1.getBounds(),
r2 = c2.getBounds();
int direction = r1.outcode(r2.x, r2.y);
switch(direction)// r2 located < direction > of r1
{
case (Rectangle.OUT_LEFT):// West
p1.x = r1.x;
p1.y = r1.y;
p2.x = r2.x + r2.width;
p2.y = r2.y;
if(r1.y > cy)
{
p1.y = r1.y + r1.height;
p2.y = r2.y + r2.height;
}
break;
case (Rectangle.OUT_TOP):// North
p1.x = r1.x;
p1.y = r1.y;
p2.x = r2.x;
p2.y = r2.y + r2.height;
if(r1.x > cx && r2.x > cx)
{
p1.x = r1.x + r1.width;
p2.x = r2.x + r2.width;
}
break;
case (Rectangle.OUT_LEFT + Rectangle.OUT_TOP):// NW
p1.x = r1.x;
p1.y = r1.y;
p2.x = r2.x + r2.width;
p2.y = r2.y;
if(r1.y > r2.y + r2.height)
p2.y = r2.y + r2.height;
break;
case (Rectangle.OUT_RIGHT):// East
p1.x = r1.x + r1.width;
p1.y = r1.y;
p2.x = r2.x;
p2.y = r2.y;
if(r1.y > cy)
{
p1.y = r1.y + r1.height;
p2.y = r2.y + r2.height;
}
break;
case (Rectangle.OUT_TOP + Rectangle.OUT_RIGHT):// NE
p1.x = r1.x + r1.width;
p1.y = r1.y;
p2.x = r2.x;
p2.y = r2.y;
if(r1.y > cy)
{
p1.y = r1.y + r1.height;
p2.y = r2.y + r2.height;
if(r1.y > r2.y + r2.height)
p1.y = r1.y;
}
else
{
if(r1.y > r2.y + r2.height)
p2.y = r2.y + r2.height;
}
break;
case (Rectangle.OUT_BOTTOM):// South
p1.x = r1.x;
p1.y = r1.y + r1.height;
p2.x = r2.x;
p2.y = r2.y;
if(r1.x > cx && r2.x > cx)
{
p1.x = r1.x + r1.width;
p2.x = r2.x + r2.width;
}
break;
case (Rectangle.OUT_RIGHT + Rectangle.OUT_BOTTOM):// SE
p1.x = r1.x + r1.width;
p1.y = r1.y + r1.height;
p2.x = r2.x;
p2.y = r2.y;
break;
case (Rectangle.OUT_BOTTOM + Rectangle.OUT_LEFT):// SW
p1.x = r1.x;
p1.y = r1.y + r1.height;
p2.x = r2.x;
p2.y = r2.y;
if(r1.x > r2.x + r2.width)
p2.x = r2.x + r2.width;
if(r1.x > cx && r2.x > cx)
{
p1.x = r1.x + r1.width;
p2.x = r2.x + r2.width;
}
}
returnnew Point[]{p1, p2};
}
privatevoid determineCenterOfComponents()
{
int
xMin = Integer.MAX_VALUE,
yMin = Integer.MAX_VALUE,
xMax = 0,
yMax = 0;
for(int i = 0; i < labels.length; i++)
{
Rectangle r = labels[i].getBounds();
if(r.x < xMin)
xMin = r.x;
if(r.y < yMin)
yMin = r.y;
if(r.x + r.width > xMax)
xMax = r.x + r.width;
if(r.y + r.height > yMax)
yMax = r.y + r.height;
}
cx = xMin + (xMax - xMin)/2;
cy = yMin + (yMax - yMin)/2;
}
privateclass ComponentMoverextends MouseInputAdapter
{
Point offsetP =new Point();
boolean dragging;
publicvoid mousePressed(MouseEvent e)
{
Point p = e.getPoint();
for(int i = 0; i < labels.length; i++)
{
Rectangle r = labels[i].getBounds();
if(r.contains(p))
{
selectedLabel = labels[i];
offsetP.x = p.x - r.x;
offsetP.y = p.y - r.y;
dragging =true;
break;
}
}
}
publicvoid mouseReleased(MouseEvent e)
{
dragging =false;
}
publicvoid mouseDragged(MouseEvent e)
{
if(dragging)
{
Rectangle r = selectedLabel.getBounds();
r.x = e.getX() - offsetP.x;
r.y = e.getY() - offsetP.y;
selectedLabel.setBounds(r.x, r.y, r.width, r.height);
determineCenterOfComponents();
repaint();
}
}
}
privatevoid addLabels()
{
label1 =new JLabel("Label 1");
label2 =new JLabel("Label 2");
label3 =new JLabel("Label 3");
label4 =new JLabel("Label 4");
labels =new JLabel[]{
label1, label2, label3, label4
};
for(int i = 0; i < labels.length; i++)
{
labels[i].setHorizontalAlignment(SwingConstants.CENTER);
labels[i].setBorder(BorderFactory.createEtchedBorder());
add(labels[i]);
}
}
}

