| | |
Mouse Event help
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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
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. 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: As I mentioned before, using JLabel instead of a custom Glyph class would get you a lot of that functionality for free.
java Syntax (Toggle Plain Text)
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; import java.util.ArrayList; import java.util.List; import javax.swing.JFrame; import javax.swing.JPanel; public class RectangleMouseover extends JFrame { private JPanel paintPanel; public RectangleMouseover() { setDefaultCloseOperation(EXIT_ON_CLOSE); setMinimumSize(new Dimension(300, 300)); paintPanel = new PaintPanel(); getContentPane().add(paintPanel, BorderLayout.CENTER); pack(); } class PaintPanel extends JPanel implements MouseMotionListener { private List<Glyph> glyphs; public PaintPanel(){ super(); addMouseMotionListener(this); glyphs = new ArrayList<Glyph>(); glyphs.add(new Glyph(25, 15, 60, 30, Color.BLUE, "Blue node")); glyphs.add(new Glyph(75, 60, 50, 60, Color.ORANGE, "Orange node")); } public void paintComponent(Graphics g) { super.paintComponent(g); for (Glyph glyph : glyphs){ glyph.draw(g); } } public void mouseDragged(MouseEvent e) {} public void mouseMoved(MouseEvent e) { for(Glyph g : glyphs){ g.showLabel( g.contains(e.getX(), e.getY()) ); } repaint(); } } class Glyph { private Rectangle bounds; private Color color; private String label; private boolean showLabel = false; public Glyph(int x, int y, int width, int height, Color color, String label) { bounds = new Rectangle(x, y, width, height); this.color = color; this.label = label; } public void draw(Graphics g){ Graphics2D g2 = (Graphics2D)g; g2.setColor(color); g2.draw(bounds); if (showLabel){ g2.setColor(Color.BLACK); int labelWidth = g2.getFontMetrics().stringWidth(label); int fontHeight = g2.getFontMetrics().getHeight(); g2.drawString( label, (int)(bounds.getX() + (bounds.getWidth()-labelWidth) / 2), (int)(bounds.getY() + (bounds.getHeight()+fontHeight/2) / 2d)); } } public boolean contains(int x, int y){ return bounds.contains(x,y); } public void showLabel(boolean show){ showLabel = show; } } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new RectangleMouseover().setVisible(true); } }); } }
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
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
My input file is like this:-
Thanks
Java Syntax (Toggle Plain Text)
public void run() { new RectangleMouseover().setVisible(true); } });
I mean howto pass my this constructor
Java Syntax (Toggle Plain Text)
RectangleMouseover(int height, int fixvalue1, int width, int fixvalue2, Color c,String text)
Java Syntax (Toggle Plain Text)
import java.io.*; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; import java.util.ArrayList; import java.util.List; import javax.swing.JFrame; import javax.swing.JPanel; import java.io.File; import java.io.FileNotFoundException; import java.io.BufferedReader; public class RectangleMouseover extends JFrame { public Color color; public int height; public int fixvalue1; public int fixvalue2; public int width; public String text; private JPanel paintPanel; public RectangleMouseover(){ } public RectangleMouseover(int height, int fixvalue1, int width, int fixvalue2, Color c,String text) { this.color = c; this.height = height; this.fixvalue1 = fixvalue1; this.width = width; this.fixvalue2 = fixvalue2; this.text = text; setDefaultCloseOperation(EXIT_ON_CLOSE); setMinimumSize(new Dimension(800, 400)); paintPanel = new PaintPanel(); getContentPane().add(paintPanel, BorderLayout.CENTER); pack(); } class PaintPanel extends JPanel implements MouseMotionListener { private List<Glyph> glyphs; public int height; public int width; public String f[]; public PaintPanel(){ super(); addMouseMotionListener(this); glyphs = new ArrayList<Glyph>(); String n = null; try{ BufferedReader fh = new BufferedReader(new FileReader("InputFile.txt")); while((n = fh.readLine()) != null && (n = n.trim()).length() > 0){ f = n.split("\t"); int height = Integer.parseInt(f[0].trim()); int width = Integer.parseInt(f[1].trim()); String text = f[2]; Color color = new Color(Integer.parseInt(f[3])); int fixvalue1 = 60; int fixvalue2 = 27; glyphs.add(new Glyph(height, fixvalue1, width, fixvalue2, color, text)); } fh.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e2) { e2.printStackTrace(); } } public void paintComponent(Graphics g) { super.paintComponent(g); for (Glyph glyph : glyphs){ glyph.draw(g); } } public void mouseDragged(MouseEvent e) {} public void mouseMoved(MouseEvent e) { for(Glyph g : glyphs){ g.showLabel( g.contains(e.getX(), e.getY()) ); } repaint(); } } class Glyph { private Rectangle bounds; private Color color; private String label; private boolean showLabel = false; public Glyph(int x, int y, int width, int height, Color color, String label) { bounds = new Rectangle(x, y, width, height); this.color = color; this.label = label; } public void draw(Graphics g){ Graphics2D g2 = (Graphics2D)g; g2.setColor(color); g2.draw(bounds); if (showLabel){ g2.setColor(Color.BLACK); int labelWidth = g2.getFontMetrics().stringWidth(label); int fontHeight = g2.getFontMetrics().getHeight(); g2.drawString( label, (int)(bounds.getX() + (bounds.getWidth()-labelWidth) / 2), (int)(bounds.getY() + (bounds.getHeight()+fontHeight/2) / 2d)); } } public boolean contains(int x, int y){ return bounds.contains(x,y); } public void showLabel(boolean show){ showLabel = show; } } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new RectangleMouseover().setVisible(true); } }); } public Color getColor(){ return color; } public String toString() { return String.format("Color=%s,top=%d,bottom=%d,width=%d", color.toString(), height, fixvalue1, width, fixvalue2, text); } }
My input file is like this:-
Java Syntax (Toggle Plain Text)
10 40 Web_Sailor 003 51 60 Ezzaral 000 112 46 DaniWeb 933
Thanks
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.
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.
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:-
Will it work ? but I need to do it.
Thanks
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);
Thanks
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.
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.
![]() |
Similar Threads
- Mouse Over Sound Event (Visual Basic 4 / 5 / 6)
- Object and Mouse events... (Java)
- keyboard instead of mouse (Visual Basic 4 / 5 / 6)
- Mouse Cursor Image (Java)
- JSpinner mouse event (Java)
- A doubt in SWING event handling (Java)
- Getting mouse coordinates from MouseListener (Java)
- Microsoft Paint Simulated Mouse Click (C++)
- Mouse Posistion (VB.NET)
- Locking the mouse button down (C)
Other Threads in the Java Forum
- Previous Thread: String input
- Next Thread: digital spacing
| Thread Tools | Search this Thread |






How can I accomodate java.awt.Rectangle to my Rectangle class ? I am confused since never done this before. 
