943,641 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 8154
  • Java RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Oct 15th, 2008
0

Re: Drawing tree with JFrame

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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
Oct 15th, 2008
0

Re: Drawing tree with JFrame

Click to Expand / Collapse  Quote originally posted by Ezzaral ...
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...
Reputation Points: 12
Solved Threads: 8
Junior Poster
gangsta1903 is offline Offline
111 posts
since May 2008
Oct 15th, 2008
0

Re: Drawing tree with JFrame

Here's a small example that should give you plenty to play around with:
java Syntax (Toggle Plain Text)
  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.)
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: How to extract Java applet class file?
Next Thread in Java Forum Timeline: receiving error "illegal start of expression"





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC