Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 318
  • Last Modified:

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
0
ws1999
Asked:
ws1999
  • 11
  • 10
  • 2
  • +1
1 Solution
 
objectsCommented:
Can u post your code that's painting the image?
0
 
ws1999Author Commented:
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
0
 
objectsCommented:
Thanks for that. I'm more interested in the code that displays (drawImage) the image.

Also whats animator doing?

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
ws1999Author Commented:
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

0
 
objectsCommented:
Still haven't seen the code that actually draws the image on the component.
0
 
ws1999Author Commented:
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
0
 
objectsCommented:
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.
0
 
ws1999Author Commented:
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
0
 
objectsCommented:
> 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);
               

0
 
ws1999Author Commented:
how can i refresh what's on the applet in program?

ws1999
0
 
objectsCommented:
Yes, jest call the applets repaint() method.
0
 
ws1999Author Commented:
hi objiect,

repaint()has no effect in my program

ws1999
0
 
objectsCommented:
Where and why are you using it?
0
 
ws1999Author Commented:
i try it in many places after loading a new image,

i just want to disapear the old image.

ws1999
0
 
objectsCommented:
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.
0
 
ws1999Author Commented:
hi object,

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

ws1999
0
 
objectsCommented:
Because it does a repaint.

Did u change the clearRect() call I mentioned?
0
 
ws1999Author Commented:
also no effect, i really not too sure the principle of java image showing.

ws1999
0
 
ws1999Author Commented:
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
0
 
ws1999Author Commented:
too questions
0
 
DanRollinsCommented:
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 http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp#8 for instruction on deleting questions.

DanRollins -- EE database cleanup volunteer
0
 
objectsCommented:
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.
0
 
DanRollinsCommented:
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
0
 
NetminderCommented:
Per recommendation, force-accepted.

Netminder
CS Moderator
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 11
  • 10
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now