Solved

connecting two rectangles with a line in java

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

Technology Partners: 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

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.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

690 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