Mouse Event help

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

Join Date: May 2007
Posts: 4,481
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 515
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is online now Online
Industrious Poster

Re: Mouse Event help

 
0
  #11
Jun 4th, 2009
You've defined your own Rectangle class, so you can't expect to use the java.awt.Rectangle method contains() for a hit test on that. You could change your class a little to keep a java.awt.Rectangle internally for it's bounds and use that for the hit testing and stuff.

For the tooltip behavior, I think you'd have an easier time just using JLabels for your rectangles, which already have a tool tip that you can just set directly, instead of your custom class. You can also set their size, location, text, and background color directly on JLabels and you inherit a ton of JComponent methods for handling other UI behaviors if you need them.

If you decide to stick with a class that uses a Rectangle and rendering it yourself, I would recommend moving the rendering code into the class itself by adding a draw(Graphics g) method and passing in the graphics reference. You can then draw the label string yourself if need be and your frame class doesn't have to keep track of all of the pieces. Your paintComponent() methods would just loop your List and call draw() on each of them.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 135
Reputation: Web_Sailor is an unknown quantity at this point 
Solved Threads: 0
Web_Sailor's Avatar
Web_Sailor Web_Sailor is offline Offline
Junior Poster

Re: Mouse Event help

 
0
  #12
Jun 8th, 2009
Hi I am completely lost How can I accomodate java.awt.Rectangle to my Rectangle class ? I am confused since never done this before.
I tried many combinations but unfortunately all failed to give desired results
If there is a breakthrough I will inform you but as of now I am completely lost

Thanks
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,481
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 515
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is online now Online
Industrious Poster

Re: Mouse Event help

 
0
  #13
Jun 8th, 2009
Ok, here's a small example using a Glyph class that internally uses Rectangle to manage its bounds and shows a label when moused over:
  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.Rectangle;
  7. import java.awt.event.MouseEvent;
  8. import java.awt.event.MouseMotionListener;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. import javax.swing.JFrame;
  12. import javax.swing.JPanel;
  13.  
  14. public class RectangleMouseover extends JFrame {
  15.  
  16. private JPanel paintPanel;
  17.  
  18. public RectangleMouseover() {
  19. setDefaultCloseOperation(EXIT_ON_CLOSE);
  20. setMinimumSize(new Dimension(300, 300));
  21.  
  22. paintPanel = new PaintPanel();
  23. getContentPane().add(paintPanel, BorderLayout.CENTER);
  24. pack();
  25. }
  26.  
  27. class PaintPanel extends JPanel implements MouseMotionListener {
  28. private List<Glyph> glyphs;
  29.  
  30. public PaintPanel(){
  31. super();
  32. addMouseMotionListener(this);
  33.  
  34. glyphs = new ArrayList<Glyph>();
  35. glyphs.add(new Glyph(25, 15, 60, 30, Color.BLUE, "Blue node"));
  36. glyphs.add(new Glyph(75, 60, 50, 60, Color.ORANGE, "Orange node"));
  37. }
  38.  
  39. public void paintComponent(Graphics g) {
  40. super.paintComponent(g);
  41. for (Glyph glyph : glyphs){
  42. glyph.draw(g);
  43. }
  44. }
  45.  
  46. public void mouseDragged(MouseEvent e) {}
  47.  
  48. public void mouseMoved(MouseEvent e) {
  49. for(Glyph g : glyphs){
  50. g.showLabel( g.contains(e.getX(), e.getY()) );
  51. }
  52. repaint();
  53. }
  54. }
  55.  
  56. class Glyph {
  57. private Rectangle bounds;
  58. private Color color;
  59. private String label;
  60. private boolean showLabel = false;
  61.  
  62. public Glyph(int x, int y, int width, int height, Color color, String label) {
  63. bounds = new Rectangle(x, y, width, height);
  64. this.color = color;
  65. this.label = label;
  66. }
  67.  
  68. public void draw(Graphics g){
  69. Graphics2D g2 = (Graphics2D)g;
  70. g2.setColor(color);
  71. g2.draw(bounds);
  72. if (showLabel){
  73. g2.setColor(Color.BLACK);
  74. int labelWidth = g2.getFontMetrics().stringWidth(label);
  75. int fontHeight = g2.getFontMetrics().getHeight();
  76. g2.drawString( label,
  77. (int)(bounds.getX() + (bounds.getWidth()-labelWidth) / 2),
  78. (int)(bounds.getY() + (bounds.getHeight()+fontHeight/2) / 2d));
  79. }
  80. }
  81.  
  82. public boolean contains(int x, int y){
  83. return bounds.contains(x,y);
  84. }
  85.  
  86. public void showLabel(boolean show){
  87. showLabel = show;
  88. }
  89. }
  90.  
  91. public static void main(String args[]) {
  92. java.awt.EventQueue.invokeLater(new Runnable() {
  93. public void run() {
  94. new RectangleMouseover().setVisible(true);
  95. }
  96. });
  97. }
  98. }
As I mentioned before, using JLabel instead of a custom Glyph class would get you a lot of that functionality for free.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 135
Reputation: Web_Sailor is an unknown quantity at this point 
Solved Threads: 0
Web_Sailor's Avatar
Web_Sailor Web_Sailor is offline Offline
Junior Poster

Got stuck in constructor now

 
0
  #14
Jun 8th, 2009
Hi Thanks for your kind suggestions... I am able to come this far but still one problem is there. In this code I donno howto deal with the Constructor. Since at the end I am doing like this
  1. public void run() {
  2. new RectangleMouseover().setVisible(true);
  3. }
  4. });
I don't know howto pass my constructor with values and make it work. I know that is the problem why my code below is showing empty window. How can I deal with this problem ? I just created a dummy empty constructor to make this code work but donno howto pass my actual construtor with values ?

I mean howto pass my this constructor
  1. RectangleMouseover(int height, int fixvalue1, int width, int fixvalue2, Color c,String text)

  1. import java.io.*;
  2. import java.awt.BorderLayout;
  3. import java.awt.Color;
  4. import java.awt.Dimension;
  5. import java.awt.Graphics;
  6. import java.awt.Graphics2D;
  7. import java.awt.Rectangle;
  8. import java.awt.event.MouseEvent;
  9. import java.awt.event.MouseMotionListener;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. import javax.swing.JFrame;
  13. import javax.swing.JPanel;
  14. import java.io.File;
  15. import java.io.FileNotFoundException;
  16. import java.io.BufferedReader;
  17.  
  18. public class RectangleMouseover extends JFrame {
  19. public Color color;
  20. public int height;
  21. public int fixvalue1;
  22. public int fixvalue2;
  23. public int width;
  24. public String text;
  25.  
  26. private JPanel paintPanel;
  27. public RectangleMouseover(){
  28.  
  29. }
  30.  
  31. public RectangleMouseover(int height, int fixvalue1, int width, int fixvalue2, Color c,String text) {
  32. this.color = c;
  33. this.height = height;
  34. this.fixvalue1 = fixvalue1;
  35. this.width = width;
  36. this.fixvalue2 = fixvalue2;
  37. this.text = text;
  38.  
  39. setDefaultCloseOperation(EXIT_ON_CLOSE);
  40. setMinimumSize(new Dimension(800, 400));
  41. paintPanel = new PaintPanel();
  42. getContentPane().add(paintPanel, BorderLayout.CENTER);
  43. pack();
  44. }
  45. class PaintPanel extends JPanel implements MouseMotionListener {
  46. private List<Glyph> glyphs;
  47. public int height;
  48. public int width;
  49. public String f[];
  50.  
  51. public PaintPanel(){
  52. super();
  53. addMouseMotionListener(this);
  54. glyphs = new ArrayList<Glyph>();
  55. String n = null;
  56. try{
  57. BufferedReader fh = new BufferedReader(new FileReader("InputFile.txt"));
  58. while((n = fh.readLine()) != null && (n = n.trim()).length() > 0){
  59.  
  60. f = n.split("\t");
  61. int height = Integer.parseInt(f[0].trim());
  62. int width = Integer.parseInt(f[1].trim());
  63. String text = f[2];
  64. Color color = new Color(Integer.parseInt(f[3]));
  65. int fixvalue1 = 60;
  66. int fixvalue2 = 27;
  67.  
  68. glyphs.add(new Glyph(height, fixvalue1, width, fixvalue2, color, text));
  69.  
  70. }
  71. fh.close();
  72. } catch (FileNotFoundException e) {
  73. e.printStackTrace();
  74. } catch (IOException e2) {
  75. e2.printStackTrace();
  76. }
  77. }
  78. public void paintComponent(Graphics g) {
  79. super.paintComponent(g);
  80. for (Glyph glyph : glyphs){
  81. glyph.draw(g);
  82. }
  83. }
  84. public void mouseDragged(MouseEvent e) {}
  85. public void mouseMoved(MouseEvent e) {
  86. for(Glyph g : glyphs){
  87. g.showLabel( g.contains(e.getX(), e.getY()) );
  88. }
  89. repaint();
  90. }
  91. }
  92. class Glyph {
  93. private Rectangle bounds;
  94. private Color color;
  95. private String label;
  96. private boolean showLabel = false;
  97. public Glyph(int x, int y, int width, int height, Color color, String label) {
  98. bounds = new Rectangle(x, y, width, height);
  99. this.color = color;
  100. this.label = label;
  101. }
  102. public void draw(Graphics g){
  103. Graphics2D g2 = (Graphics2D)g;
  104. g2.setColor(color);
  105. g2.draw(bounds);
  106. if (showLabel){
  107. g2.setColor(Color.BLACK);
  108. int labelWidth = g2.getFontMetrics().stringWidth(label);
  109. int fontHeight = g2.getFontMetrics().getHeight();
  110. g2.drawString( label,
  111. (int)(bounds.getX() + (bounds.getWidth()-labelWidth) / 2),
  112. (int)(bounds.getY() + (bounds.getHeight()+fontHeight/2) / 2d));
  113. }
  114. }
  115. public boolean contains(int x, int y){
  116. return bounds.contains(x,y);
  117. }
  118. public void showLabel(boolean show){
  119. showLabel = show;
  120. }
  121. }
  122. public static void main(String args[]) {
  123. java.awt.EventQueue.invokeLater(new Runnable() {
  124. public void run() {
  125. new RectangleMouseover().setVisible(true);
  126. }
  127. });
  128. }
  129. public Color getColor(){
  130. return color;
  131. }
  132.  
  133. public String toString() {
  134. return String.format("Color=%s,top=%d,bottom=%d,width=%d", color.toString(), height, fixvalue1, width, fixvalue2, text);
  135. }
  136. }

My input file is like this:-
  1. 10 40 Web_Sailor 003
  2. 51 60 Ezzaral 000
  3. 112 46 DaniWeb 933

Thanks
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 135
Reputation: Web_Sailor is an unknown quantity at this point 
Solved Threads: 0
Web_Sailor's Avatar
Web_Sailor Web_Sailor is offline Offline
Junior Poster

I solved it myself

 
0
  #15
Jun 9th, 2009
Ezzral you are great :-) I cracked it myself :-)
I called the constructor like this: - constructor(0, 0, 0, 0, null, null) and it worked.
I know its very basic for you but for me its new ;-) lo
Thanks a lot for your kind guidance. Again I can tell you are great :-) lol.

Now I am working to add a drag function on this code. Will let you know my progress once I reach to some extent :-)

Thanks a lot.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 135
Reputation: Web_Sailor is an unknown quantity at this point 
Solved Threads: 0
Web_Sailor's Avatar
Web_Sailor Web_Sailor is offline Offline
Junior Poster

Re: Mouse Event help

 
0
  #16
Jun 9th, 2009
Hi... I am now trying to use gradient paint in my code but again getting into complications. Also my code is not taking colors from my text input field.

I am trying to do something like this inside Glyph class:-
for(int i=0; i<glyph.size(); i++){
PaintPanel c = glyph.get(i);

GradientPaint gpi = new GradientPaint(0, 0,c.getColor() , 0, 20, Color.white, true);
// g2d.setColor(c.getColor());
g2d.setPaint(gpi);
g2d.drawRect(c.height, fixedvalue1, c.width, fixedvalue2);
g2d.fillRect(c.height, fixedvalue1, c.width, fixedvalue2);
Will it work ? but I need to do it.

Thanks
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 135
Reputation: Web_Sailor is an unknown quantity at this point 
Solved Threads: 0
Web_Sailor's Avatar
Web_Sailor Web_Sailor is offline Offline
Junior Poster

Re: Mouse Event help

 
0
  #17
Jun 9th, 2009
Dear Ezzaral since the initial problem is solved so I marked it as solved to add a feather to your cap :-)
I have opened another thread "Contd from Mouse Event" in which I have posted my code for Gradient paint. Please have a look and guide me where I am wrong. That code is compiling but I am getting runtime errors.

Thanks a lot.
Reply With Quote Quick reply to this message  
Reply

Tags
graphics

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC