Link to home
Start Free TrialLog in
Avatar of ws1999
ws1999

asked on

how to repaint the image on the canvas

i have an image show applet, but when the new image showed,the old one cannot be totally dispeared, i don't know why the techinc i used is MemoryIamgeSource.

what might be the problem?

ws1999
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Can u post your code that's painting the image?
Avatar of ws1999
ws1999

ASKER

hi objects

here is the code:
public void pictureShow(String ImageID) {

   Animator animator = new Animator(5);
   int[] emptyBuffer = new int[imw*imh];

    getResource(ImageID);
   
       try {
               im = createImage((ImageProducer)url.getContent());
               mt.addImage(im, 0);
               mt.waitForID(0);
              }
          catch(Exception e) { e.printStackTrace(); }

    flag = false;  //whether the picture has been fully showed;

          imw = im.getWidth(this);
          imh = im.getHeight(this);
    //emptyBuffer = new int[imw*imh];
    thisPicture = new Picture(imh,imw);
          allPixels      = new int[imw*imh];
          offscreen = createImage(imw, imh);

          mis = new MemoryImageSource(imw,imh,thisPicture.pixels,0,imw);

          mis.setAnimated(true);
          animatedImage = createImage(mis);

    GrabPixel();


          animator.start();
}

bad code, but hope you can help me
Thanks for that. I'm more interested in the code that displays (drawImage) the image.

Also whats animator doing?

Avatar of ws1999

ASKER

hi,there

class Animator extends Thread {
          private int numBoxes, curx, cury, curw, curh;
          private boolean showing = false;
          private Graphics g;
    private int[] emptyBuffer = new int[imw*imh];

          public Animator(int numBoxes) {
               this.numBoxes = numBoxes;
          }
          public void run() {
               while(true) {
                    if(isShowing()) {
          //if (showing) boxIn();
           //else
           boxOut();
                         //showing = showing ? false : true;
         // pause(1000);
                     }
               }
          }
     
          private void boxOut() {
               mis.setFullBufferUpdates(false);


               while(curw < imw && curh < imh) {
                    curx = imw/2 - curw/2;
                    cury = imh/2 - curh/2;

                    for(int i=0; i < curh; ++i) {
                         System.arraycopy(allPixels,
                                             (cury+i)*imw + curx,thisPicture.pixels,
                                             (cury+i)*imw + curx, curw);
                    }
                    mis.newPixels(curx,cury,curw,curh);

                    curw += imw/numBoxes;
                    curh += imh/numBoxes;

                    pause(50);
               }
               System.arraycopy(allPixels, 0, thisPicture.pixels, 0, imw*imh);
               mis.newPixels();
      flag = true;      //the picture has fully showed;
          }

hi,object the animater show the image by boxout();

ws1999

Still haven't seen the code that actually draws the image on the component.
Avatar of ws1999

ASKER

hello object

infact with these code the picture can be showed. also there is other code like, but i think it's not the problem exists:

  public void GrabPixel()
  {
           try {
                pg = new PixelGrabber(im, 0, 0, imw, imh,
                                              allPixels, 0, imw);
               pg.grabPixels();
          }
          catch(InterruptedException e) {
               e.printStackTrace();
          }
   }

     public void update(Graphics g) {
        paint(g);
     }
     public void paint(Graphics g) {
          Graphics      offg = null;
          Dimension      size = getSize();
          try {
             offg = offscreen.getGraphics();
       offg.clearRect(0,0,size.width,size.height);
                 offg.drawImage(animatedImage,0,0,this);
                g.drawImage(offscreen,0,0,this);

          }
          finally {
               offg.dispose();
          }
     }

hope can help you some.

ws1999
I noticed that you create your offscreen image the same size as the loaded image.

> offscreen = createImage(imw, imh);

But then later you clear it using the size of the component.

> offg.clearRect(0,0,size.width,size.height);
               
Not sure if this is a problem or not.

Can you explain in more detail what your problem is to help me understand what is happening.
Avatar of ws1999

ASKER

hello object,

i have a button on the applet, whenever i pressed it,it will invoke the procedure showPicture(), that is a new image will change the old one. but i notice if the old one is more large then there is still part of the image left on the canvas,only when i minimize or maximize the applet can it dispeared.

as the offscreen , speak the truth, i'm not quite clear about its function, i copy it from a reference book which can animate show picture.but it work well in a single picture.

and object, i notice whenever a showpicture run there will be a new MemoryImageSource created, is it will eat many resource of the computer? i find when more picture showed the computer will slow down or even dead.

worried ws1999
> will slow down or even dead.

That could be a thread problem. A new Animator thread is created for each picture and that thread does not appear to ever complete.

Your paint problem is related to the image/component size inconsistency I mentioned above.

Try changing:
offg.clearRect(0,0,size.width,size.height);
to just:
clearRect(0,0,size.width,size.height);
               

Avatar of ws1999

ASKER

how can i refresh what's on the applet in program?

ws1999
Yes, jest call the applets repaint() method.
Avatar of ws1999

ASKER

hi objiect,

repaint()has no effect in my program

ws1999
Where and why are you using it?
Avatar of ws1999

ASKER

i try it in many places after loading a new image,

i just want to disapear the old image.

ws1999
The old image won't be gotten rid of until animateImage is updated. So you need to call repaint() after you assign a new value to animateImage.
Avatar of ws1999

ASKER

hi object,

why i minimize or maximize the applet window, then the old image disappeared.

ws1999
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia 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
Avatar of ws1999

ASKER

also no effect, i really not too sure the principle of java image showing.

ws1999
Avatar of ws1999

ASKER

hello object,

one thing you are right , i create so many thread without stop it, it keeps running and swollow the computer resource.

now the program is better, these points will give to you.

thanks

ws1999
Avatar of ws1999

ASKER

too questions
Hi ws1999,
You've requested to delete this question, but its status remains as 'Pending Delete' because one or more comments have been added.  Normally, the only way to fully delete such a Question is to post a message to Community Support and ask for assistance.

EE is making a one-time database sweep to purge the Pending Delete Questions automatically.  During this sweep:

    ws1999 -- To allow the deletion to proceed:  Do nothing.
    EXPERTS -- Please DON'T POST a comment except to contest this deletion.

In the future, please refer to https://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp#8 for instruction on deleting questions.

DanRollins -- EE database cleanup volunteer
I would like to contest the deletion based on the following comment made by ws1999 above:

> now the program is better, these points will give to you.
Thanks for the good input, objects.  That helps a lot.
Recommended disposition:

    Accept objects@idg's comment(s) as an answer.

DanRollins -- EE database cleanup volunteer
Per recommendation, force-accepted.

Netminder
CS Moderator