Dear Members,
I am sorry to used that Thread , and i attached my code as a zip file with the reply , anyway I am so sorry for all that , well here is my code I am facing problem in mouse events.
Code:class GraphicComponent extends JPanel {
// make points of view data
JLabel xViewLabel;
JLabel yViewLabel;
JLabel xModelLabel;
JLabel yModelLabel;
double [] data = {1.0 , 5.8, 7.4 , 3.2};
AffineTransform xform;
AffineTransform inverted;
final int PAD = 20;
public GraphicComponent ()
{
}
public void setPoint(Point p, int x, int y) {
p.setLocation(x,y);
repaint();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
if(xform == null) setTransform();
double w = getWidth();
double h = getHeight();
// draw axes
g2.draw(new Line2D.Double(PAD, PAD, PAD, h-PAD)); // top - to bottom
g2.draw(new Line2D.Double(PAD, h-PAD, w-PAD, h-PAD)); // left to write
// draw data
g2.setPaint(Color.red);
for(int i = 0; i < data.length ; i++) {
Point2D.Double p = modelToView(i, data[i]);
g2.fill(new Ellipse2D.Double(p.x-1.5, p.y-1.5, 4, 4));
}
}
private Point2D.Double modelToView(double x, double y) {
Point2D.Double dst = new Point2D.Double();
xform.transform(new Point2D.Double(x, y), dst);
return dst;
}
private Point2D.Double viewToModel(Point p) {
Point2D.Double dst = new Point2D.Double();
inverted.transform(p, dst);
return dst;
}
private void setTransform() {
xform = new AffineTransform();
double w = getWidth();
double h = getHeight();
double dataMax = getDataMax();
double max = Math.ceil(dataMax/10)*10;
System.out.printf("dataMax = %f max = %f%n", dataMax, max);
double yScale = (h - 2*PAD)/max;
double xScale = (w - 2*PAD)/(data.length-1);
xform.translate(PAD, h-PAD); // PAD = x-axis , h-PAD = y-axis
xform.scale(xScale, -yScale); // xScale = x-axis , -yscale = y-axis
setInverted();
}
private void setInverted() {
inverted = (AffineTransform)xform.clone();
if(inverted.getDeterminant() != 0) {
try {
inverted.invert();
} catch(NoninvertibleTransformException e) {
System.out.println("invert error: " + e.getMessage());
}
} else {
inverted.setToIdentity();
}
}
private double getDataMax() {
double [] copy = Arrays.copyOf(data, data.length);
Arrays.sort(copy);
return copy[data.length-1];
}
private JPanel getOutputPanels() {
Dimension d = new Dimension(65, 25);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = GridBagConstraints.REMAINDER;
panel.add(getViewPanel(d), gbc);
panel.add(getModelPanel(d), gbc);
return panel;
}
private JPanel getViewPanel(Dimension d) {
xViewLabel = getLabel(d);
yViewLabel = getLabel(d);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(2,2,2,2);
JLabel label = getLabel(d);
label.setText("View");
panel.add(label, gbc);
gbc.weightx = 1.0;
addComponents("x", xViewLabel, panel, gbc, true);
addComponents("y", yViewLabel, panel, gbc, false);
return panel;
}
private JPanel getModelPanel(Dimension d) {
xModelLabel = getLabel(d);
yModelLabel = getLabel(d);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(2,2,2,2);
JLabel label = getLabel(d);
label.setText("Model");
panel.add(label, gbc);
gbc.weightx = 1.0;
addComponents("x", xModelLabel, panel, gbc, true);
addComponents("y", yModelLabel, panel, gbc, false);
return panel;
}
private JLabel getLabel(Dimension d) {
JLabel label = new JLabel();
label.setPreferredSize(d);
label.setBorder(BorderFactory.createEtchedBorder());
return label;
}
private void addComponents(String s, JLabel label, Container c,
GridBagConstraints gbc, boolean b) {
gbc.weightx = b ? 1.0 : 0;
gbc.anchor = GridBagConstraints.EAST;
c.add(new JLabel(s), gbc);
gbc.weightx = b ? 0 : 1.0;
gbc.anchor = GridBagConstraints.WEST;
c.add(label, gbc);
}
public static void main(String[] args)
{
GraphicComponent gc = new GraphicComponent();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(gc);
f.add(gc.getOutputPanels(), "Last");
f.setSize(400,400);
f.setLocation(100,100);
f.setVisible(true);
//make and show gui
DragClass dc = new DragClass(gc);
gc.addMouseListener(dc);
gc.addMouseMotionListener(dc);
}
}
Thanks youCode:class DragClass extends MouseAdapter {
GraphicComponent component;
Point selPoint;
double [] data ;
boolean okayToDrag = false;
Point offset = new Point();
int closeEnough;
private int dragIndex = NOT_DRAGGING;
private final static int NOT_DRAGGING = -1;
private int x;
private int y;
private int NEIGHBORHOOD = 4;
public DragClass(GraphicComponent gc) {
component = gc;
this.data = component.data;
}
@Override
public void mousePressed(MouseEvent e) {
dragIndex = NOT_DRAGGING;
int minDistance = Integer.MAX_VALUE;
Point p = e.getPoint();
closeEnough = -1;
for ( int i = 0; i < data.length ; i++)
{
selPoint.x = i;
selPoint.y = (int) data[i];
offset.x = p.x - selPoint.x;
offset.y = p.y - selPoint.y;
int distance = (int) (Math.sqrt( offset.x* offset.x + offset.y + offset.y));
if (distance < minDistance) {
minDistance = distance;
closeEnough = i;
}
break;
} // end of for loop
if (minDistance > NEIGHBORHOOD)
return;
dragIndex = closeEnough;
} // end of pressed event
@Override
public void mouseReleased(MouseEvent e) {
if (dragIndex == NOT_DRAGGING)
return;
dragIndex = NOT_DRAGGING;
}
@Override
public void mouseDragged(MouseEvent e) {
if (dragIndex == NOT_DRAGGING)
return;
x = e.getX() - offset.x;
y = e.getY() - offset.y;
component.setPoint(selPoint, x, y);
}
}

