Solved

connecting two rectangles with a line in java

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

706 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now