| | |
Mouse Event help
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
Hi,
I have to add a mouse over event and mouse click event in my rectangles that I draw while taking the values from an input file. Now since I am taking rectangle height and width from the input text file as well I also want to take a text value to be shown while moving a mouse over a particular rectangle from my input file. Also when user clicks on that rectangle the height and width of that particular rectangle only should be displayed (re-adjusted).
Like suppose my input file has 3 fields:-
Now When my paint component draws these 2 rectangles and a user moves his mouse over the rectangle he should see text message attached to it:- Like for first row 10, 23 he should see "Hello". In this case when he clicks on this rectangle the picture should read just to show only 10 and 20 as height and width.
I am trying to write 2 classes. 1st one extends JPanel and the other one MouseInputAdapter.
It would be great to get help and guidance from you guys.
Thanks
I have to add a mouse over event and mouse click event in my rectangles that I draw while taking the values from an input file. Now since I am taking rectangle height and width from the input text file as well I also want to take a text value to be shown while moving a mouse over a particular rectangle from my input file. Also when user clicks on that rectangle the height and width of that particular rectangle only should be displayed (re-adjusted).
Like suppose my input file has 3 fields:-
Java Syntax (Toggle Plain Text)
Height Width Test_To_Show 10 23 Hello 20 44 Bye
I am trying to write 2 classes. 1st one extends JPanel and the other one MouseInputAdapter.
It would be great to get help and guidance from you guys.
Thanks
Post your initial code that is drawing the rectangles. As a general suggestion, keep a List containing java.awt.Rectangle objects for each entry you have to draw. Your mouseMoved and mouseClicked method implementations can easily test against those Rectangle objects by their contains() methods.
Alternately you could use a null layout and JLabels or a small JComponent class for your rectangles, which would manage some of the mouseover handling for you.
Alternately you could use a null layout and JLabels or a small JComponent class for your rectangles, which would manage some of the mouseover handling for you.
Hi,
I have written this code based on your guidance. I would be obliged to get corrections. I really want to make it work... Only addition I did is I have added 4rth field for color in the input file. That would be some values.
Here is the input file example below:-
Hope to get help soon.
Thanks
I have written this code based on your guidance. I would be obliged to get corrections. I really want to make it work... Only addition I did is I have added 4rth field for color in the input file. That would be some values.
Java Syntax (Toggle Plain Text)
import java.io.*; import java.util.ArrayList; import java.util.Arrays; import java.io.File; import java.io.FileNotFoundException; import java.io.BufferedReader; import java.util.List; import java.awt.*; import javax.swing.*; import java.awt.Color; import javax.swing.JFrame; import java.awt.geom.Rectangle2D; import java.awt.Rectangle; import java.awt.Container; import java.awt.Dimension; import java.awt.Graphics; import javax.swing.event.*; import java.awt.event.*; import java.awt.event.MouseListener; import java.awt.event.MouseEvent; public class MousingExample extends JPanel { public MousingExample(Frame f) { tips = new ArrayList<String>(); tips.add("Lets Start The Show"); toolTip = new JWindow(f); label = new JLabel(); label.setHorizontalAlignment(JLabel.CENTER); label.setOpaque(true); label.setBackground(UIManager.getColor("MousingExample.background")); label.setBorder(UIManager.getBorder("MosusingExample.border")); toolTip.add(label); setOpaque(true); } public ArrayList<Rectangle> rects = new ArrayList<Rectangle>(); public int width; public String color; public static int score; public static int value1; public static int value2; private final static int NUM_FIELDS = 4; public void InputReader() { String n = null; try{ BufferedReader fh = new BufferedReader(new FileReader("inputfile.txt")); while((n = fh.readLine()) != null && (n = n.trim()).length() > 0){ // System.out.println(n); f = n.split("\t"); int height = Integer.parseInt(f[0].trim()); int width = Integer.parseInt(f[1].trim()); String message = f[2]; Color color = new Color(Integer.parseInt(f[3])); Rectangle tempLineInfo = new Rectangle(color, value1, value2, width); rects.add(tempLineInfo);} } fh.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e2) { e2.printStackTrace(); } } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); setBackground(Color.white); for(int i=0; i<rects.size(); i++){ Rectangle c = rects.get(i); GradientPaint gpi = new GradientPaint(0, 0,c.getColor() , 0, 20, Color.white, true); g2d.setPaint(gpi); g2d.drawRect(c.value1, 20, c.width, 35); g2d.fillRect(c.value1, 20, c.width, 35); System.out.printf("Drawing at %s\n", c); } } public static void main(String[] args) { MousingExample test = new MousingExample(); test.InputReader(); Tipster tipster = new Tipster(test); test.addMouseListener(tipster); test.addMouseMotionListener(tipster); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setTitle("Trying To MouseOver"); frame.setSize(400, 400); frame.setContentPane(test); frame.setVisible(true); } } class Rectangle { private Color color; public int value1; public int value2; public int width; public Rectangle(Color c, int value1, int value2, int width){ this.color = c; this.value1 = value1; this.value2 = value2; this.width = width; } public Color getColor(){ return color; } public String toString() { return String.format("Color=%s,top=%d,bottom=%d,width=%d", color.toString(), value1, value2, width); } } class Tipster extends MouseInputAdapter { MousingExample Mousing; Point start; public Tipster(MousingExample tt) { MousingExample = tt; } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseDragged(MouseEvent e) { } public void mouseMoved(MouseEvent e) { List<Rectangle> rects = MousingExample.Rectangle; //Not Sure what I should do here } }
Here is the input file example below:-
Java Syntax (Toggle Plain Text)
Height Width Test_To_Show Colors 10 23 Hello 300 20 44 Bye 000
Hope to get help soon.
Thanks
Here is my modified code but still not getting what I want 

Java Syntax (Toggle Plain Text)
import java.io.*; import java.util.ArrayList; import java.util.Arrays; import java.io.File; import java.io.FileNotFoundException; import java.io.BufferedReader; import java.util.List; import java.awt.*; import javax.swing.*; import java.awt.Color; import javax.swing.JFrame; import java.awt.geom.Rectangle2D; import java.awt.Container; import java.awt.Dimension; import java.awt.Graphics; import javax.swing.event.*; import java.awt.event.*; import java.awt.event.MouseListener; import java.awt.event.MouseEvent; public class MousingExample extends JPanel { List<String> tips; JWindow toolTip; JLabel label; public String f[]; public MousingExample(Frame f) { tips = new ArrayList<String>(); tips.add("Lets Start The Show"); toolTip = new JWindow(f); label = new JLabel(); label.setHorizontalAlignment(JLabel.CENTER); label.setOpaque(true); label.setBackground(UIManager.getColor("MousingExample.background")); label.setBorder(UIManager.getBorder("MosusingExample.border")); toolTip.add(label); setOpaque(true); } public ArrayList<Rectangle> rects = new ArrayList<Rectangle>(); public int width; public String color; public static int score; public static int value1; public static int value2; private final static int NUM_FIELDS = 4; public void InputReader() { String n = null; try{ BufferedReader fh = new BufferedReader(new FileReader("inputfile.txt")); while((n = fh.readLine()) != null && (n = n.trim()).length() > 0){ // System.out.println(n); f = n.split("\t"); int height = Integer.parseInt(f[0].trim()); int width = Integer.parseInt(f[1].trim()); String message = f[2]; Color color = new Color(Integer.parseInt(f[3])); Rectangle tempLineInfo = new Rectangle(color, value1, value2, width); rects.add(tempLineInfo); } fh.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e2) { e2.printStackTrace(); } } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); setBackground(Color.white); for(int i=0; i<rects.size(); i++){ Rectangle c = rects.get(i); GradientPaint gpi = new GradientPaint(0, 0,c.getColor() , 0, 20, Color.white, true); g2d.setPaint(gpi); g2d.drawRect(c.value1, 20, c.width, 35); g2d.fillRect(c.value1, 20, c.width, 35); System.out.printf("Drawing at %s\n", c); } } public static void main(String[] args) { JFrame frame = new JFrame(); MousingExample test = new MousingExample(frame); test.InputReader(); Tipster tipster = new Tipster(test); test.addMouseListener(tipster); test.addMouseMotionListener(tipster); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setTitle("Trying To MouseOver"); frame.setSize(400, 400); frame.setContentPane(test); frame.setVisible(true); } } class Rectangle { private Color color; public int value1; public int value2; public int width; public Rectangle(Color c, int value1, int value2, int width){ this.color = c; this.value1 = value1; this.value2 = value2; this.width = width; } public Color getColor(){ return color; } public String toString() { return String.format("Color=%s,top=%d,bottom=%d,width=%d", color.toString(), value1, value2, width); } } class Tipster extends MouseInputAdapter { MousingExample Mousing; Point start; public Tipster(MousingExample tt) { Mousing = tt; } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseDragged(MouseEvent e) { } public void mouseMoved(MouseEvent e) { List<Rectangle> rects = MousingExample.Rectangle; //Not Sure what I should do here } }
Here is one more correction to my previous code:-
Thanks
Java Syntax (Toggle Plain Text)
import java.io.*; import java.util.ArrayList; import java.util.Arrays; import java.io.File; import java.io.FileNotFoundException; import java.io.BufferedReader; import java.util.List; import java.awt.*; import javax.swing.*; import java.awt.Color; import javax.swing.JFrame; import java.awt.geom.Rectangle2D; import java.awt.Container; import java.awt.Dimension; import java.awt.Graphics; import javax.swing.event.*; import java.awt.event.*; import java.awt.event.MouseListener; import java.awt.event.MouseEvent; public class MouseExample extends JPanel { List<String> tips; JWindow toolTip; JLabel label; public String f[]; public MouseExample(Frame f) { tips = new ArrayList<String>(); tips.add("Lets Start The Show"); toolTip = new JWindow(f); label = new JLabel(); label.setHorizontalAlignment(JLabel.CENTER); label.setOpaque(true); label.setBackground(UIManager.getColor("MouseExample.background")); label.setBorder(UIManager.getBorder("MosusingExample.border")); toolTip.add(label); setOpaque(true); } public ArrayList<Rectangle> rects = new ArrayList<Rectangle>(); public int width; public String color; public static int score; public static int value1; public static int value2; private final static int NUM_FIELDS = 4; public void InputReader() { String n = null; try{ BufferedReader fh = new BufferedReader(new FileReader("inputfile.txt")); while((n = fh.readLine()) != null && (n = n.trim()).length() > 0){ // System.out.println(n); f = n.split("\t"); int height = Integer.parseInt(f[0].trim()); int width = Integer.parseInt(f[1].trim()); String message = f[2]; Color color = new Color(Integer.parseInt(f[3])); Rectangle tempLineInfo = new Rectangle(color, value1, value2, width); rects.add(tempLineInfo); } fh.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e2) { e2.printStackTrace(); } } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); setBackground(Color.white); for(int i=0; i<rects.size(); i++){ Rectangle c = rects.get(i); GradientPaint gpi = new GradientPaint(0, 0,c.getColor() , 0, 20, Color.white, true); g2d.setPaint(gpi); g2d.drawRect(c.value1, 20, c.width, 35); g2d.fillRect(c.value1, 20, c.width, 35); System.out.printf("Drawing at %s\n", c); } } public static void main(String[] args) { JFrame frame = new JFrame(); MouseExample test = new MouseExample(frame); test.InputReader(); Tipster tipster = new Tipster(test); test.addMouseListener(tipster); test.addMouseMotionListener(tipster); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setTitle("Trying To MouseOver"); frame.setSize(400, 400); frame.setContentPane(test); frame.setVisible(true); } } class Rectangle { private Color color; public int value1; public int value2; public int width; public Rectangle(Color c, int value1, int value2, int width){ this.color = c; this.value1 = value1; this.value2 = value2; this.width = width; } public Color getColor(){ return color; } public String toString() { return String.format("Color=%s,top=%d,bottom=%d,width=%d", color.toString(), value1, value2, width); } } class Tipster extends MouseInputAdapter { MouseExample Mousing; Point start; public Tipster(MouseExample tt) { Mousing = tt; } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseDragged(MouseEvent e) { } public void mouseMoved(MouseEvent e) { } }
Thanks
Where you got this code? There are so many statements either meaningless or written falsely.
1. As stated by Ezzaral, Rectangle classes create ambiguity.
2. MousingExample class has no definition
3. What about this? Is this a way to instantiate MousingExample.Rectangle.
1. As stated by Ezzaral, Rectangle classes create ambiguity.
2. MousingExample class has no definition
Java Syntax (Toggle Plain Text)
MousingExample test = new MousingExample();
Java Syntax (Toggle Plain Text)
List<Rectangle> rects = MousingExample.Rectangle;
Failure is not fatal, but failure to change might be. - John Wooden
Hi... I did some modifications to my mouseMoved function as below:-
Thanks
Java Syntax (Toggle Plain Text)
public void mouseMoved(MouseEvent e) { Point p = e.getPoint(); boolean traversing = false; List<Rectangle> rect = mousing.rects; for(int i; i<rect.size() ; i++ ){ Rectangle r = rect.get(i); if(r.contains(p)) { // I am not sure howto assign something like setText() method to read from my input file f[2] field so that the message comes up. Need help here .!! break; } } }
Thanks
Hi this is new version of my program. I am still struggling
Need your kind guidance.
Thanks
Need your kind guidance. java Syntax (Toggle Plain Text)
import java.io.*; import java.util.ArrayList; import java.util.Arrays; import java.io.File; import java.io.FileNotFoundException; import java.io.BufferedReader; import java.util.List; import java.awt.*; import javax.swing.*; import java.awt.Color; import javax.swing.JFrame; import java.awt.geom.Rectangle2D; import java.awt.Container; import java.awt.Dimension; import java.awt.Graphics; import javax.swing.event.*; import java.awt.event.*; import java.awt.event.MouseListener; import java.awt.event.MouseEvent; public class MouseExample extends JPanel { List<String> tips; JWindow toolTip; JLabel label; public String f[]; public MouseExample(Frame f) { tips = new ArrayList<String>(); // tips.add("Lets Start The Show"); toolTip = new JWindow(f); label = new JLabel(); label.setHorizontalAlignment(JLabel.CENTER); label.setOpaque(true); label.setBackground(UIManager.getColor("MouseExample.background")); label.setBorder(UIManager.getBorder("MosusingExample.border")); toolTip.add(label); setOpaque(true); } public ArrayList<Rectangle> rects = new ArrayList<Rectangle>(); public int width; public String color; public static int score; public static int value1; public static int value2; private final static int NUM_FIELDS = 4; public void InputReader() { String n = null; try{ BufferedReader fh = new BufferedReader(new FileReader("inputfile.txt")); while((n = fh.readLine()) != null && (n = n.trim()).length() > 0){ // System.out.println(n); f = n.split("\t"); int height = Integer.parseInt(f[0].trim()); int width = Integer.parseInt(f[1].trim()); String message = f[2]; tips.add(message); Color color = new Color(Integer.parseInt(f[3])); Rectangle tempLineInfo = new Rectangle(color, value1, value2, width); rects.add(tempLineInfo); } fh.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e2) { e2.printStackTrace(); } } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); setBackground(Color.white); for(int i=0; i<rects.size(); i++){ Rectangle c = rects.get(i); GradientPaint gpi = new GradientPaint(0, 0,c.getColor() , 0, 20, Color.white, true); g2d.setPaint(gpi); g2d.drawRect(c.value1, 20, c.width, 35); g2d.fillRect(c.value1, 20, c.width, 35); System.out.printf("Drawing at %s\n", c); } } public boolean isToolTipShowing() { return toolTip.isShowing(); } public void showToolTip(int index, Point p) { for(index = 0; index<tips.size();index++){ label.setText(tips.get(index)); } toolTip.pack(); toolTip.setLocation(p); toolTip.setVisible(true); } public static void main(String[] args) { JFrame frame = new JFrame(); MouseExample test = new MouseExample(frame); test.InputReader(); Tipster tipster = new Tipster(test); test.addMouseListener(tipster); test.addMouseMotionListener(tipster); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setTitle("Trying To MouseOver"); frame.setSize(400, 400); frame.setContentPane(test); frame.setVisible(true); } } class Rectangle { private Color color; public int value1; public int value2; public int width; public Rectangle(Color c, int value1, int value2, int width){ this.color = c; this.value1 = value1; this.value2 = value2; this.width = width; } public Color getColor(){ return color; } public String toString() { return String.format("Color=%s,top=%d,bottom=%d,width=%d", color.toString(), value1, value2, width); } } class Tipster extends MouseInputAdapter { MouseExample mousing; Point start; public Tipster(MouseExample tt) { mousing = tt; } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseDragged(MouseEvent e) { } public void mouseMoved(MouseEvent e) { Point p = e.getPoint(); boolean traversing = false; List<Rectangle> rect = mousing.rects; for(int i; i<rect.size() ; i++ ){ Rectangle r = rect.get(i); if(r.contains(p)) { if(!mousing.isToolTipShowing()) { SwingUtilities.convertPointToScreen(p, mousing); mousing.showToolTip(i, p); } traversing = true; break; } } } }
Thanks
Last edited by Tekmaven; Jun 4th, 2009 at 5:30 pm. Reason: Code Tags
![]() |
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 |






lol. I was looking at many examples and just trying to make my story. I know I am doing many things wrongly. Perhaps this is the first time I am using Java 2D swing. So plz be nice to my mistakes. 
