Solved

connecting two rectangles with a line in java

Posted on 2004-04-14
4
532 Views
Last Modified: 2010-07-27
hi,

i need information on how to connect two shapes drawn witha line. what should happen is that the shapes should be drawn before the line adn then after them being drawn then, there should be a line going from one shape to the other. the line shouldnt pass on top of those shapes., instead it should go from one end of the shape to the other.
any assistance in the form of sample codes or sites would be greatly appreciated.
0
Comment
Question by:Kalliba
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 3

Accepted Solution

by:
itbeme earned 20 total points
ID: 10823115
It is not difficult to get the corner of a Polygon or Rectangle in Java.  The format for drawing a line is
Graphics g;
g.drawLine(Startxcoord, Startycoord, endx, endy);
You should be able to find out the coordinates from the appropriate corners of the rectangle.  If you don't want the line to match the corner, but somewhere on the side, you should be able to subtract or add the appropriate value.
0
 

Assisted Solution

by:Steve_Pugh
Steve_Pugh earned 20 total points
ID: 10823443

A way to do it would be to make your shapes objects. the objects may just contain the co-odinats of each corner, the width and the height of each shape. This will make it easyer when it comes to get the location from where you want the line to be drawn and where to.

the workings bellow can be put into a overloaded paint method
as bellow

public void paint(Graphics g){
      super.paint(g);//making the initial call to the super methoud in this case its passing the input back the aim
      TaskNode tNode;//of this is to get aditional behavior not different behavior.

The next thing to do is to setup condition statments to check which one will be classed as the origin and which one as the destination.

e.g

if((int)tTemp.getX() + TaskNodeGui.x < ((int)fTemp.getX() )){

if shapeOne.xCo-ordinate + shapeOne.width < shapeTwo.xCo-ordinate

if this is true you know that shape two is to the right of shape one. (you will need four of these or more depending on how meny sides your shapes have)

inside those if's you will then need another if like

if(fTemp.getY() > tTemp.getY())

if shapeOne.yCo-ordinate < shapeTwo.yCo-ordinate

to find out if the second shape is above or bellow.

then create two new points

fromPoint =  new Point((int)fTemp.getX() + (TaskNodeGui.x / 2) , (int)fTemp.getY());
toPoint = new Point((int)tTemp.getX() + (TaskNodeGui.x / 2), (int)tTemp.getY() + (TaskNodeGui.y));

these are egsamples the key is to think if that condition is true where on shape one do you want the line to go from and where shape two do you want it to arive then create the points

then use the

g.drawLine((int)fromPoint.getX(),(int)fromPoint.getY(),(int)toPoint.getX(),(int)toPoint.getY());//draws the 2D line

if your condition statments are correct your lines should snap on to the shapes in the places you define in your conditions

this is definatly not the only way to do it but it a simple way

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about a problem I had getting the GPS LocationListener working.
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question