I am developing an applet class that implements MouseMotionListener. On mouse over on top of a polygon, I display some text corresponding to that polygon. Once the text is displayed, it remains displayed until the User hits a Clear button.
So, the text remains even if the mouse is not over the polygon any more. (That is, I have implemented the update method not to clear the screen)
However, when I try to print it from the browser, I get the original view without the text. How could I get the text to be printed also?
Please advise,
Thanks,
Uma.
September 27th, 1999, 12:05 PM
Hi Uma,
For the first problem, your mouse has to listen every movement. If mouse position is not on the object, you have to repaint or update, the screen. I am working on the second problem. I will soon reply
September 27th, 1999, 12:13 PM
Hi again,
There is no problem with the Mouse Events at all.
The only problem I have is with the printing - i.e after the Event. I want the text that is generated after the Event to be printed also. I only get the original view of the Applet printed.
Thanks for taking the time to reply to my question,
Uma.
September 27th, 1999, 12:23 PM
Then i will reply soon after trying some thing
Regards jagannadh
poochi
September 27th, 1999, 06:12 PM
> I am developing an applet class that implements MouseMotionListener. On mouse over on top of a
> polygon, I display some text corresponding to that polygon. Once the text is displayed, it
> remains displayed
You mean you are trying to show a tooltip like stuff for that polygon ? Which method are u
using to display the text ? Graphics.drawString()... ?
One simple solution( ? ) is create a TooltipLabel window and since you know the co-ordinates
of the Polygon , in your mouseMotionListener , check for enter & Exit. If the cursor is
inside the Poligon , set the position of the label window. If it exits then reset the label
window. Try like this..
public void mouseMoved( MouseEvent e ){
if( myPolygon.contains( e.getX() , e.getY() ) ){
toolTipWindow.setBounds( e.getX() , e.getY() , 100 , 30 ); // set the width and height properly
}else{
toolTipWindow.setBounds( 0, 0 , 0 , 0 );
}
}
If you like to set exact width and height of the label window .. Try this..
public void showTooltip( String TooltipMessage , int xOffset , int yOffset ){
FontMetrics fontMetrics = getFontMetrics( getFont());
char [] message = new char[ TooltipMessage.length() ];
TooltipMessage.getChars( 0 , TooltipMessage.length() , message , 0 );
int width = fontMetrics.charsWidth( message , 0 , TooltipMessage.length() );
int height = fontMetrics.getHeight();
toolTipWindow.setText(TooltipMessage);
toolTipWindow.setBounds( xOffset , yOffset , width , height );
}
Poochi..
Uma
September 27th, 1999, 06:24 PM
As I had mentioned earlier, I am able to get the events to work correctly.
I am using the drawString method to display the text by finding the coordinates, etc.
It's only while printing that I do not get the text to be printed also.
Will the use of ToolTip Label window allow me to print the text after the Event?
Thanks,
Uma.
poochi
September 27th, 1999, 06:37 PM
> Will the use of ToolTip Label window allow me to print the text after the Event?
I am sorry ... I could not able to get this.
What do you mean by " Printing the text after the event ? "...
As long as you are in the Polygon , you will get the text , after that the Label window will
disappear.
Poochi...
Uma
September 28th, 1999, 06:55 AM
Hi,
To give you more details, I have a chart with several polygons representing the points of Measurement. When the user places his Mouse Over a Polygon, the text corresponding to the Polygon is displayed. I have implemented the Update() method such that it calls paint() without Clearing the Screen. So, the text continues to be displayed. The user can compare the data for several points of Measurement.
On printing from the browser, I get the original Chart with the Polygons but not the Text associated with each. Why???
P.S: I have looked at some other Applets on the Web and I noticed that any changes after an Event, doesn't get printed.
For Eg: http://www.javasoft.com/applets/jdk/1.1/demo/DrawTest/index.html
(After you draw something, try to print. It won't show up. Why?)
Thanks,
Uma.
poochi
September 28th, 1999, 07:20 AM
post the code..
Uma
September 28th, 1999, 07:34 AM
Here is a snippet of my code:
public class ChartAverageGap extends Applet implements MouseMotionListener, ActionListener
{
....
public class MyApplet extends Applet{
int[] x = { 10 , 50 , 88 };
int[] y = { 100 , 130 , 30 };
Polygon p;
int X , Y;
boolean isMouseOver;
public void init(){
p = new Polygon( x,y,3);
this.addMouseMotionListener( new MouseMotionAdapter(){
public void mouseMoved( MouseEvent e ){
if( p.contains( e.getX() , e.getY() )){
isMouseOver = true;
X += 10 ;
Y +=10;
repaint();
}
}
});
}
public void paint( Graphics g ){
g.fillPolygon(p);
if( isMouseOver ){
g.drawString( "Poochi" ,X , Y );
}
}
public void update( Graphics g ){
paint( g );
}
}
it works as i expect. So , the problem might be in some other place. Did you debug the
code ? Does it go inside the if( isMouseOver ) condition in the paint method ?
poochi
September 28th, 1999, 12:03 PM
If you dont mind , can you send me your java source files to me to the following id..
sankar_s_74@yahoo.com
Uma
September 28th, 1999, 12:07 PM
Hi again,
Yes, I did compile your code after changing JApplet to Applet.
Could you get the updated applet with the text "poochi" to be printed from the browser.
Please try to PRINT using the printer and I don't know if you are mentioning - printing to the screen. The Applet is working correctly.
Thanks,
Uma.
poochi
September 28th, 1999, 03:15 PM
I am sorry ma'm. I apologize. I misunderstood your question..
Hopefully this will solve your problem...
public class MyApplet extends Applet{
int[] x = { 10 , 50 , 88 };
int[] y = { 100 , 130 , 30 };
Polygon p;
int X = 0;
int Y = 0;
boolean isMouseOver;
Image img;
public void init(){
img = createImage( this.getBounds().width , this.getBounds().height );
p = new Polygon( x,y,3);
this.addMouseMotionListener( new MouseMotionAdapter(){
public void mouseMoved( MouseEvent e ){
if( p.contains( e.getX() , e.getY() )){
isMouseOver = true;
X+= 10 ;
Y+=10;
repaint();
}else{
isMouseOver = false;
}
}
});
}
public void paint( Graphics g ){
img.getGraphics().fillPolygon(p);
if( isMouseOver ){
img.getGraphics().drawString( "Poochi" , X , Y );
}
g.drawImage( img , 0 , 0 , this );
}
public void update( Graphics g ){
paint( g );
}
}
I tested this code. I am getting the printout( from the printer ) with the text "Poochi".
Basically , if you say print , the paint method will be called and that will paint on the
printer instead of screen. In your case you draws Polygon and ,since the isMouseOver is
false , it skips drawString line. Even though if it comes inside the if condition , you will
get only one text. So u are getting the Polygons only.
And , I am wondering , If you press ALT + TAB , what happend ? All the text which you
Printed on the screen would have been erased and you would have got the polygons only in the screen.
Am i correct ? Because of the reason i mentioned above. If you use my solution , you wont
get this problem. If you still haveing any problem , post it. Tell me whether it works or not.
Best of luck...
Poochi..
Uma
September 28th, 1999, 06:10 PM
Hi,
I tried the Image object and I am able to get the text to be Printed ( printout for those that might get confused :-) ) from NETSCAPE!
But IE won't print it!
I have been using IE for all these tests and I was getting frustrated.
Another thing with IE is that the background of the Image is gray. I tried setting the img.getGraphics.setFont(Color.white), but no change. Of course without the Image, I could use the setBackground(Color.white) to change it to white.
And one thing to point out with your code -
use Graphics i = img.getGraphics();
and use object "i" each time to draw polygon/text etc. Otherwise, it gets reinstantiated.
Anyway, you get my 10 points!
Thanks a lot!
Uma.
poochi
September 28th, 1999, 07:17 PM
> Another thing with IE is that the background of the Image is gray. I tried setting the
> img.getGraphics.setFont(Color.white), but no change. Of course without the Image, I could use the
> setBackground(Color.white) to change it to white.
That is not the background color of the Image. It's the background color of the Applet.
Still you can change the background color of the Applet using setBackground( Color.white )
method. Put it in your init() function.
> But IE won't print it!
Yes , just now i noticed it. I was trying to print from appletviewer. I hate these browsers.
And , thanks for those points. This the first time i am getting points in this forum. :-)
P.S : Are u working for 24 hours ? :-)
Uma
September 29th, 1999, 07:43 AM
>That is not the background color of the Image. It's the background color of the Applet.
>Still you can change the background color of the Applet using setBackground( Color.white )
>method. Put it in your init() function.
I had the setBackground(Color.white) in the init() method of the Applet but it was after the createImage() method call and so it didn't update the background. Then I changed it to be above that line and it worked!
>P.S : Are u working for 24 hours ? :-)
Actually, I had this doubt about you!
Uma
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.