View Single Post
  #2 (permalink)  
Old 02-17-2008, 12:22 AM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
Code:
Point point2 = new Point(100,200); protected void paintComponent(Graphics g) { Graphics2D graphics2D = (Graphics2D)g; AffineTransform at = AffineTransform.getTranslateInstance(point2.x, point2.y); int b = 8; double theta = Math.toRadians(45); // The idea of using a GeneralPath is so we can // create the (three lines that make up the) arrow // (only) one time and then use AffineTransform to // place it anywhere we want. GeneralPath path = new GeneralPath(); // Add the arrow shaft. path.moveTo(0,0); path.lineTo(-100,0); // distance between line and the arrow mark <** not ** // Start a new line segment from the position of (0,0). path.moveTo(0,0); // Create one of the two arrow head lines. int x =(int)(-b * Math.cos(theta)); int y =(int) (b * Math.sin(theta)); path.lineTo(x,y); // distance between line and the arrow mark <** not ** // Make the other arrow head line. x = (int) (-b * Math.cos(-theta)); y = (int) (b * Math.sin(-theta)); path.moveTo(0,0); path.lineTo(x,y); // theta was used for the angle between the arrow // shaft and each of its arrow head lines. // If you wnat to also rotate the arrow (GeneralPath) // by 45 degrees, okay. // Set up the transform the way you want it, ie, do // all the work before drawing the final result. at.rotate(theta,x,y); Shape shape = at.createTransformedShape(path); graphics2D.setPaint(Color.black); graphics2D.draw(shape); // This is unnecessary since you are using the // GeneralPath to store all three lines segments // that make up the arrow, ie, the GeneralPath is // the arrow. So you apply the AffineTransform to // the GeneralPath and it does all the work. // line drawing 2 points. // graphics2D.drawLine(point2.x,point2.y,point4.x,point4.y); }
Reply With Quote