Link to home
Start Free TrialLog in
Avatar of Philippe Charles
Philippe CharlesFlag for France

asked on

Pb trying to update graphics in a panel from an applet ?

I'm using a javascript code in an html page to call an applet function which should update graphics of the associated panel.

When I call ma javascript function, it seems that my applet is still using a old set of data, even I put my mouse pointer inside applet's frame (for any of these mouse events : enter event, exit event, move event). BUT when I click my mouse button, data seem to be reloaded and updated with the last modified version of data set (the 'emps' vector).

My analysis is that my panel is working with an old set of data (a "frozen" data set), even my applet has changed these data. At any mouse event, but the mouse click, nothing change. And at mouse click, data are updated !!!

My question is : may I force my panel data to be updated (it seem's that the synchronized 'update' function lock the data for a while) ? It seems that a call to 'update' in my applet,or 'repaint'  doesn't work at all.

Source code following...
Avatar of Philippe Charles
Philippe Charles
Flag of France image

ASKER

Source code example :

import java.util.*;
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.Font;


class Emp {
    String name;
    double x;
    double y;
    int x_actual;
    int y_actual;
}


class GraphPanel extends Panel
    implements MouseListener, MouseMotionListener {
    Graph graph;
    int nemp;
    Vector emps;
    Dimension d = null;


    Image finalscreen;
    Graphics finalgraphics;


    GraphPanel(Graph graph) {
     this.graph = graph;
     addMouseListener(this);
        addMouseMotionListener(this);

     emps = new Vector ();
    }

    void addPoint (String name, int rx, int ry) {
     emps.addElement (new Emp(name,rx,ry));
     nemp = emps.size();
    }

    public void selectPoint (int i) {
     ((Emp)emps.elementAt(i)).picked = true;
    }
   
    public void paintAllPoints(Vector emps) {
         [...]
    }
    public void update(Graphics g) {
     d = getSize();

     if ((bgscreen == null) || (d.width != bgscreensize.width) || (d.height != bgscreensize.height)) {
          //first time
          [...]
          finalscreen = createImage(d.width,d.height);
          if (finalgraphics != null) {
               finalgraphics.dispose();
          }
          finalgraphics = (Graphics)finalscreen.getGraphics();
          finalgraphics.setFont (ttipFont);
     }
     [...]
     finalgraphics.drawImage(paintAllPoints(emps),0,0,this);
     g.drawImage(finalscreen, 0, 0, this);
    }

    public void paint(Graphics g) {
        update(g);
    }

    // traitements des évènements souris
    void traitementEvents (MouseEvent e) {
     [...]
     repaint();
     e.consume();
    }
         
    //1.1 event handling
    public void mouseClicked(MouseEvent e) {
     if (e.getClickCount() == 2) {
          for (int i = nemp-1; i >= 0; i --)
               ((Emp)emps.elementAt(i)).picked = false;
     }
     else
          ((Emp)emps.elementAt(current_picked)).picked = ((Emp)emps.elementAt(current_picked)).picked?false:true;

     traitementEvents (e);
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
     pick.paint_it = true;  
     traitementEvents (e);
     e.consume();
     repaint ();
    }

    public void mouseExited(MouseEvent e) {
     pick.paint_it = false;  
     traitementEvents (e);
     e.consume();
     repaint ();
    }

    public void mouseDragged(MouseEvent e) {
    }

    public void mouseMoved(MouseEvent e) {
     traitementEvents (e);
    }
}


public class Graph extends Applet {

    GraphPanel panel;
    Panel controlPanel;

    public void init() {
     setLayout(new BorderLayout());

     panel = new GraphPanel(this);
     add("Center", panel);

     String edge = "";
     int i = 3;
     while ((edge = getParameter("p"+i)) != null) {
          [...]
          panel.addPoint(pn, pa, ps);
          i ++;
     }

     panel.transformCoord (getSize());
    }

    public void selectLine (int k) {
         panel.selectPoint (k);
     panel.repaint ();
    }

[...]
Avatar of Mick Barry
can u post the applet code that does the update.
which method is the javascript calling?
You have the applet code at the end.
Javascript is calling "selectLine(int k)".
Actually : 'update' method is 'synchronized' and is using data of 'emps' variable.
looks ok.

Add some debug to ensure Graph.selectLine() and GraphPanel.update() are actually getting called.

Can you post the code for paintAllPoints().
debug : OK, I found it :
It comes from my HTML page :
#1)
If I call 'selectLine' using javascript like this :
<input type="checkbox" onChange="document.all.test.selectLine(0)">Line 0
It causes the problem I described.'update' is differed until mouse click.
#2)
If I call 'selectLine' like this :
<a href="javascript:document.all.test.selectLine(0)">select line #0</a>
It works fine. 'update' is done in realtime, not differed like in case #1.

Any explanation ????

What are you refering to when you keep mentioning 'update'?

Did u check what was getting called as I suggested above?
'update' is the 'update' method of my panel.

Yes, I did.
case #1) (using checkbox) I change the value of the checkbox, and... nothing! selectLine,nor selectPoint are called (or it seems that they are not called). But calls are "stored" somewhere, because after mouse click (but not move events, nor enter or exit events), my traces show me that these function are finally called (events have been differed, or something like this).

case #2) (using <a ...> tag) I click the link, and... first selectLine, then selectPoint are called in realtime, and graphic update is done!!!!????

What do U think ?
Avatar of CleanupPing
CleanupPing

thenaz:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Question Deleted and Points refunded

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

TimYates
EE Cleanup Volunteer
ASKER CERTIFIED SOLUTION
Avatar of Philippe Charles
Philippe Charles
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial