Real coordinates on the remote pc.
Sorry about the things last time i had done. Now i really need some
help from
Andrew or Roedy Green or any other person ( He or she is welcome).
Please i didn't force anyone to see or solve the problem but i really
need help this time! All the very good helping nature people in the
group.I try to solve it but i didn't get the things right.I am just
like your friend need some help?I like to know your
views its precious for me to solve the problem.
Platform:Windows 98se
Java version:v1.4.1
Problem:I had implemented one JScrollPane on which i had displayed one
image
and i add one MouseMotionListener to JScrollPane. This
MouseMotionListener
give me coordinates w.r.t the current JScrollPane.But when i scroll the
picture
through horizontal and vertical scrollbars the image changes.When i
click
somewhere on the image it gives me the coordinate w.r.t the
JScrollPane.
Is their someway i can know the real coordinates w.r.t the original
image.
Why i am asking this question is
Because i want to implement one remote desktop thing
Server end:- Client end:-
Send request -----> Take screenshot
Display Screenshot on
JScrollPane. <----- Send it to Server end
On JScrollPane if user -------> NOW WHAT CAN I DO
clicks an image.Send click HERE TO MAP THE
to Client end. VIRTUAL COORDINATE WITH
with coordinates get by REAL COORDINATE.
e.getX();, e.getY();
APPROACH 1:
----------
What i think until now if i add scrollbar listeners to the scrollbars
of JScrollPane. It will help me to get Unit and Block Increment.
So that when the user scrolls horizontally i will add
e.getX()+X where X=displacement from the top of real image.
e.getY()+Y where Y=displacement from the left of real image.(or
Vertical)
What i try to implement in the below code?
->I take vertical scrollbar from JScrollPane
->I take horizontal scrollbar from JScrollPane
->add AdjustmentListener to the scrollbar
->if the button,knob or Track gets displaced know total block and value
(unit increment).
->for X displacement i calculate Math.abs(block - value) for horizontal
displacement.
->The same for Y displacement.
I already read java doc properly and implement one code myself to know
the displacement
But i don't know How i can solve it?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ScrollTest extends JFrame
{
private int horizontal=0;
private int vertical=0;
private String output=null;
private JTextArea textArea ;
private int value = 0,block=0;
ScrollTest()
{
Container c = getContentPane();
c.setLayout(new BorderLayout());
textArea = new JTextArea(17,20);
JScrollPane pane = new JScrollPane(textArea);
pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
// Listen for value changes in the scroll pane's scrollbars
AdjustmentListener listener = new MyAdjustmentListener();
pane.getHorizontalScrollBar().addAdjustmentListener(listener);
pane.getVerticalScrollBar().addAdjustmentListener(listener);
//textArea.setText(output);
//c.add(textArea,BorderLayout.CENTER);
c.add(pane,BorderLayout.CENTER);
setSize(400,400);
validate();
setVisible(true);
}
public static void main(String args[])
{
ScrollTest app = new ScrollTest();
app.addWindowListener(
new WindowAdapter(){
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
}
class MyAdjustmentListener implements AdjustmentListener {
// This method is called whenever the value of a scrollbar is
changed,
// either by the user or programmatically.
public void adjustmentValueChanged(AdjustmentEvent evt) {
Adjustable source = evt.getAdjustable();
//output = null;
// getValueIsAdjusting() returns true if the user is
currently
// dragging the scrollbar's knob and has not picked a final
value
if (evt.getValueIsAdjusting()) {
// The user is dragging the knob
return;
}
// Determine which scrollbar fired the event
int orient = source.getOrientation();
if (orient == Adjustable.HORIZONTAL) {
output+="\nhorizontal:";
} else {
output+="\nvertical:";
}
// Determine the type of event
int type = evt.getAdjustmentType();
switch (type) {
case AdjustmentEvent.UNIT_INCREMENT:
System.out.println("unit increment");
value -= 1;//(up)
break;
case AdjustmentEvent.UNIT_DECREMENT:
System.out.println("unit decrement");
value += 1;//(down) Scrollbar was decreased by one unit
break;
case AdjustmentEvent.BLOCK_INCREMENT:
System.out.println("Block increment");
value -= 3;// (up)Scrollbar was increased by one block
break;
case AdjustmentEvent.BLOCK_DECREMENT:
System.out.println("Block decrement");
block += 3;// (down)Scrollbar was decreased by one block
break;
case AdjustmentEvent.TRACK:
System.out.println("Under Track");
// block += 3; (i didn't know the direction)// The knob on the
scrollbar was dragged
break;
}
// Get current value
int scrollbarvalue = evt.getValue();
if (orient == Adjustable.HORIZONTAL) {
horizontal += Math.abs( block - value );
output+="\nhorizontal:";
} else {
vertical += Math.abs( block - value );
output+="\nvertical:";
}
System.out.println(output + "block:" + Integer.toString(block) +
"value:" + Integer.toString(value) + "horizontal(from top):" +
Integer.toString(horizontal) + "vertical(from left):" +
Integer.toString(vertical) + "scrollbarvalue:" +
Integer.toString(scrollbarvalue));
textArea.setVisible(true);
}
}
}
By seeing the code Please help me to solve the problem. If you had any
code to
solve such problem please send it to me.
I am thinking whether this is the right approach or i need to do
scalling.
I don't know I get frustated while trying to solve such problem help
me!! please
I want to know your views?
APPROACH 2:
----------
I need to scale it w.r.t the remote pc coordinates.
x/ScrollPanewidth * ServerResolution = X/RemoteScreenwidth *
clientResolutionwidth;
y/ScrollPaneheight * ServerResolution = Y/RemoteScreenheight *
clientResolutionheight;
But this is not working as the mouse pointer goes to top left corner.
May be something is wrong?
Roedy Green - 08 Oct 2005 23:01 GMT
>Is their someway i can know the real coordinates w.r.t the original
>image.
If want a fast answer, you can took up the word in the Java glossary.
It will point you to other resources. I have been doing this for
about ten years, so most questions I tackle I have answered before and
the answers are sitting there waiting for you.
http://mindprod.com/jgloss/coordinates.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
ajay - 09 Oct 2005 15:42 GMT
Yes sir,
You are among my ideals.I read a lot of Java docs.
I solved the problem myself last night.
I take your word work hard for the whole night and i get it right.
To get the real coordinate.
->Take vertical scrollbar from JScrollPane.
->Take horizontal scrollbar from JScrollPane.
->add AdjustmentListener to vertical and horizontal Scrollbar.
->get
variables from event.getValue(); to get the value of scrollbar.
->add it to horizontal and vertical coordinates from x and y
coordinate.
Below is the source code:-
AdjustmentListener listener = new MyAdjustmentListener();
scroller = new JScrollPane(new ImageComponent(image));
scroller.getHorizontalScrollBar().addAdjustmentListener(listener);
scroller.getVerticalScrollBar().addAdjustmentListener(listener);
class MyAdjustmentListener implements AdjustmentListener {
public void adjustmentValueChanged(AdjustmentEvent evt) {
Adjustable source = evt.getAdjustable();
if (evt.getValueIsAdjusting()) {
return;
}
// Determine which scrollbar fired the event
int orient = source.getOrientation();
int scrollbarvalue = evt.getValue();
if (orient == Adjustable.HORIZONTAL) {
horizontal = scrollbarvalue;
} else {
vertical = scrollbarvalue;
}
}
}
int x = e.getX()+horizontal;
int y = e.getY()+vertical;
It gives you the real coordinate.
Thanks! Mr Roedy.
Roedy Green - 12 Oct 2005 12:07 GMT
>int x = e.getX()+horizontal;
>int y = e.getY()+vertical;
>It gives you the real coordinate.
>Thanks! Mr Roedy.
It is a lot easier than that. Read the entry again noting the part
about the SwingUtilities methods and relative origins.
It was good for your soul to do it the hard way in terms of
understanding.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.