• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

tooltip and mouse event

 
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This program takes, (double[] data = { 20.2, 12.7, 19.0, 35.75, 16.2, 48.9, 31.0, 10.2 } this as an input and calcutatin x & y co-ordintes..Can anyone modify this so,that it takes both X&Y as input


import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.text.NumberFormat;
import javax.swing.*;
public class GraphicOption extends JPanel
{
double[] data = { 20.2, 12.7, 19.0, 35.75, 16.2, 48.9, 31.0, 10.2 };
final int PAD = 20;
final int PROX_DIST = 10;
Point[] dataLocs;
NumberFormat nf;
GraphicOption()
{
nf = NumberFormat.getInstance();
nf.setMinimumFractionDigits(1);
nf.setMaximumFractionDigits(2);
dataLocs = new Point[data.length];
for(int j = 0; j < dataLocs.length; j++)
dataLocs[j] = new Point();
}
/** * The JComponent method summary section for this method* explains how to do this. See api for the details. */
public String getToolTipText(MouseEvent e)
{
Point p = e.getPoint();
for(int j = 0; j < dataLocs.length; j++)
{
if(p.distance(dataLocs[j]) < PROX_DIST)
{
return nf.format(data[j]);
}
}
return null;
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
int w = getWidth();
int h = getHeight();
// Draw axes.
g2.drawLine(PAD, PAD, PAD, h-PAD);
g2.drawLine(PAD, h-PAD, w-PAD, h-PAD);
double xInc = (w - 2*PAD)/(data.length-1);
double scale = getScale(h);
// Origin location is (PAD, h-PAD).
// So y == zero at:
double y0 = h-PAD;
g2.setPaint(Color.red);
// Plot data.
for(int j = 0; j < data.length; j++)
{
double x = PAD + j*xInc;
double y = y0 - scale*data[j];
// Set location for each point.
dataLocs[j].setLocation(x, y);
g2.fill(new Ellipse2D.Double(x-1.5, y-1.5, 4, 4));
}
}
private double getScale(int h)
{
double max = -Double.MAX_VALUE;
for(int j = 0; j < data.length; j++)
{
if(data[j] > max)
max = data[j];
}
return (h - 2*PAD)/max;
}
public static void main(String[] args)
{
JFrame f = new JFrame();
GraphicOption graphicOption = new GraphicOption();
DataSurveyor surveyor = new DataSurveyor(graphicOption, f);
graphicOption.addMouseMotionListener(surveyor);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(graphicOption);
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
}
class DataSurveyor extends MouseMotionAdapter
{
GraphicOption component;
JWindow toolTip; JLabel label;
Point loc;
NumberFormat nf;
DataSurveyor(GraphicOption go, JFrame f)
{
component = go;
label = new JLabel();
label.setBorder(UIManager.getBorder("ToolTip.border"));
toolTip = new JWindow(f);
toolTip.getContentPane().setBackground(UIManager.getColor("ToolTip.background"));
toolTip.getContentPane().add(label);
}
public void mouseMoved(MouseEvent e)
{
String text = component.getToolTipText(e);
boolean hover = false;
if(text != null)
{
if(loc == null)
{
Point p = e.getPoint();
SwingUtilities.convertPointToScreen(p, component);
label.setText(text);
int height = label.getPreferredSize().height;
loc = new Point(p.x, p.y-height);
toolTip.setLocation(loc);
toolTip.pack();
toolTip.setVisible(true);
}
hover = true;
}
if(!hover && toolTip.isVisible())
{
toolTip.dispose();
loc = null;
}
}
}
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why don't you ask the author of the code:
https://coderanch.com/t/345294/GUI/java/mouse-event

multiple posts of the same problem (generally) confuses/annoys those with a solution
 
reply
    Bookmark Topic Watch Topic
  • New Topic