Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-30-2008, 01:27 PM
Member
 
Join Date: Jan 2008
Posts: 7
sandhyau is on a distinguished road
how to draw an arrow mark using java swing
how to draw an arrow mark in a straight line using java swing?.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-31-2008, 02:42 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,104
hardwired is on a distinguished road
Code:
import java.awt.*; import java.awt.geom.*; import javax.swing.*; import javax.swing.event.*; public class Arrow extends JPanel implements ChangeListener { Path2D.Double arrow = createArrow(); double theta = 0; public void stateChanged(ChangeEvent e) { int value = ((JSlider)e.getSource()).getValue(); theta = Math.toRadians(value); repaint(); } protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); int cx = getWidth()/2; int cy = getHeight()/2; AffineTransform at = AffineTransform.getTranslateInstance(cx, cy); at.rotate(theta); at.scale(2.0, 2.0); Shape shape = at.createTransformedShape(arrow); g2.setPaint(Color.blue); g2.draw(shape); } private Path2D.Double createArrow() { int length = 80; int barb = 15; double angle = Math.toRadians(20); Path2D.Double path = new Path2D.Double(); path.moveTo(-length/2, 0); path.lineTo(length/2, 0); double x = length/2 - barb*Math.cos(angle); double y = barb*Math.sin(angle); path.lineTo(x, y); x = length/2 - barb*Math.cos(-angle); y = barb*Math.sin(-angle); path.moveTo(length/2, 0); path.lineTo(x, y); return path; } private JSlider getSlider() { JSlider slider = new JSlider(-180, 180, 0); slider.addChangeListener(this); return slider; } public static void main(String[] args) { Arrow test = new Arrow(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(test); f.add(test.getSlider(), "Last"); f.setSize(400,400); f.setLocationRelativeTo(null); f.setVisible(true); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-31-2008, 08:10 AM
Member
 
Join Date: Jan 2008
Posts: 7
sandhyau is on a distinguished road
thanks for u r help. i have dought in the code. what is path2D. i am getting error in that .is it path2D is the seperate class?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-31-2008, 05:05 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,104
hardwired is on a distinguished road
Path2D is new in j2se 1.6. For earlier versions use GeneralPath instead.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 02-01-2008, 06:51 AM
Member
 
Join Date: Jan 2008
Posts: 7
sandhyau is on a distinguished road
thank you.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 02-07-2008, 12:52 PM
Member
 
Join Date: Jan 2008
Posts: 7
sandhyau is on a distinguished road
i can draw an arrow mark in a line but the arrow is not rotating when the line move.
i can draw an arrow mark in a line but the arrow is not rotating when the line moves. here i am placing the code . can any one help me that line should move as well as arrow mark should also move in the same direction.please send as soon as possible.thanks.

AffineTransform at = AffineTransform.getTranslateInstance((point2.x + point4.x)/2, (point2.y + point4.y)/2);
at.rotate(angle);
int length = 6;
int b = 10;
double angle = Math.toRadians(45);
GeneralPath path = new GeneralPath();
path.moveTo(5, 6);
path.moveTo(0,0);
// Double d = new Double(angle);
// int a = d.intValue();
int x = (int) (-b* Math.cos(angle));
int y = (int) (b * Math.sin(angle));
path.lineTo(x, y);
x = (int) (-b * Math.cos(-angle));
y = (int) (b * Math.sin(-angle));
path.moveTo(5, 6);
path.moveTo(0,0);
path.lineTo(x, y);
Shape shape = at.createTransformedShape(path);
// graphics2D.setPaint(Color.black);
graphics2D.draw(shape);
drawLine(graphics2D, point2.x, point2.y, point4.x, point4.y);
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Mark thread RESOLVED. Eranga Suggestions & Feedback 45 04-02-2008 11:34 AM
how u rotate the arrow mark as the line moves accordingly sandhyau AWT / Swing 1 02-17-2008 12:22 AM
Draw an arrow Albert SWT / JFace 3 02-01-2008 09:11 AM
BufferedReader .mark(int readAheadLimit) ShoeNinja New To Java 1 11-16-2007 11:58 PM
how to draw in Java Heather AWT / Swing 2 07-12-2007 12:01 PM


All times are GMT +3. The time now is 12:26 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org