?
Solved

connecting two rectangles with a line in java

Posted on 2004-04-14
4
Medium Priority
?
534 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 80 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 80 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
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…

800 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