Solved

connecting two rectangles with a line in java

Posted on 2004-04-14
4
530 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
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

830 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