Drawing tree with JFrame

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2007
Posts: 4,511
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 522
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Drawing tree with JFrame

 
0
  #11
Oct 15th, 2008
Originally Posted by gangsta1903 View Post
now,I found the problem,its not about the overriden method!

now there is something new,
You suggested me to use Jlabels ,but I used drawRectangle to draw nodes.Can these nodes(shapes) be clickable? Or do I have to use Jlabels?
Yes, you can simply draw the rectangles yourself, but then you will need to manage any interactions with them yourself as well. That would include things like keeping track of their bounds within the screen, determining mouse-over, rendering the text within them, etc. Using a JLabel saves you all of that work.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 54
Reputation: gangsta1903 is an unknown quantity at this point 
Solved Threads: 1
gangsta1903 gangsta1903 is offline Offline
Junior Poster in Training

Re: Drawing tree with JFrame

 
0
  #12
Oct 15th, 2008
Originally Posted by Ezzaral View Post
Yes, you can simply draw the rectangles yourself, but then you will need to manage any interactions with them yourself as well. That would include things like keeping track of their bounds within the screen, determining mouse-over, rendering the text within them, etc. Using a JLabel saves you all of that work.
thank you very much,I will try Jlabels...
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,511
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 522
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Drawing tree with JFrame

 
0
  #13
Oct 15th, 2008
Here's a small example that should give you plenty to play around with:
  1. import java.awt.BorderLayout;
  2. import java.awt.Color;
  3. import java.awt.Dimension;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.Point;
  7. import java.awt.event.ComponentEvent;
  8. import java.util.HashMap;
  9. import java.util.Map;
  10. import javax.swing.BorderFactory;
  11. import javax.swing.JFrame;
  12. import javax.swing.JLabel;
  13. import javax.swing.JPanel;
  14. import javax.swing.SwingConstants;
  15.  
  16. public class PaintNodes extends JFrame {
  17.  
  18. private NodeLabel node1;
  19. private NodeLabel node2;
  20. private JPanel paintPanel;
  21.  
  22. public PaintNodes() {
  23. initComponents();
  24. }
  25.  
  26. private void paintNodeConnections(Graphics g) {
  27. Graphics2D g2d = (Graphics2D)g;
  28. // draw line between right connection point of node1
  29. // and the left connection point of node2
  30. Point p1 = node1.getConnectionPoint("RIGHT");
  31. Point p2 = node2.getConnectionPoint("LEFT");
  32. g2d.drawLine(p1.x, p1.y, p2.x, p2.y);
  33. }
  34.  
  35. private void initComponents() {
  36. setDefaultCloseOperation(EXIT_ON_CLOSE);
  37. setMinimumSize(new Dimension(300, 300));
  38.  
  39. // this just overrides the paintComponent() method
  40. // of the created panel.
  41. paintPanel = new JPanel() {
  42. public void paintComponent(Graphics g){
  43. super.paintComponent(g);
  44. paintNodeConnections(g);
  45. }
  46. };
  47.  
  48. paintPanel.setLayout(null);
  49.  
  50. node1 = new NodeLabel("Node 1");
  51. node1.setHorizontalAlignment(SwingConstants.CENTER);
  52. node1.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0)));
  53. paintPanel.add(node1);
  54. node1.setBounds(54, 52, 56, 28);
  55.  
  56. node2 = new NodeLabel("Node 2");
  57. node2.setHorizontalAlignment(SwingConstants.CENTER);
  58. node2.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0)));
  59. paintPanel.add(node2);
  60. node2.setBounds(186, 52, 56, 28);
  61.  
  62. getContentPane().add(paintPanel, BorderLayout.CENTER);
  63.  
  64. pack();
  65. }
  66.  
  67. /** Small class to add some additionial behavior for nodes */
  68. class NodeLabel extends JLabel {
  69.  
  70. Map<String, Point> connectionPoints = new HashMap<String, Point>();
  71.  
  72. public NodeLabel(String text) {
  73. super(text);
  74. addComponentListener(new java.awt.event.ComponentAdapter(){
  75. public void componentResized(java.awt.event.ComponentEvent evt) {
  76. mapConnectionPoints();
  77. }
  78. public void componentMoved(ComponentEvent e) {
  79. mapConnectionPoints();
  80. }
  81.  
  82. });
  83. }
  84.  
  85. // updates the mapped positions of the connection points
  86. // called whenever the component get's resized or moved
  87. private void mapConnectionPoints(){
  88. connectionPoints.clear();
  89. Point point = new Point(getX(),getY()+getHeight()/2);
  90. connectionPoints.put("LEFT", point);
  91.  
  92. point = new Point(getX() + getWidth(), getY() + getHeight()/2);
  93. connectionPoints.put("RIGHT", point);
  94.  
  95. point = new Point(getX() + getWidth()/2, getY());
  96. connectionPoints.put("TOP", point);
  97.  
  98. point = new Point(getX() + getWidth()/2, getY() + getHeight());
  99. connectionPoints.put("BOTTOM", point);
  100. }
  101.  
  102. public Point getConnectionPoint(String key) {
  103. return connectionPoints.get(key);
  104. }
  105. }
  106.  
  107. public static void main(String args[]) {
  108. java.awt.EventQueue.invokeLater(new Runnable() {
  109. public void run() {
  110. new PaintNodes().setVisible(true);
  111. }
  112. });
  113. }
  114. }
(Sorry for the wraparound on a few lines, but I don't have time here to fix all that. Also, that connection point map really should use an enum for the key instead of a string, but I didn't want to obfuscate the example too much.)
Reply With Quote Quick reply to this message  
Reply

Tags
graphics

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum


Views: 3133 | Replies: 12
Thread Tools Search this Thread



Tag cloud for graphics
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC