Solved

how to repaint the image on the canvas

Posted on 2001-09-12
24
296 Views
Last Modified: 2008-02-01
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
Comment
Question by:ws1999
  • 11
  • 10
  • 2
  • +1
24 Comments
 
LVL 92

Expert Comment

by:objects
ID: 6478295
Can u post your code that's painting the image?
0
 

Author Comment

by:ws1999
ID: 6478312
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
 
LVL 92

Expert Comment

by:objects
ID: 6478318
Thanks for that. I'm more interested in the code that displays (drawImage) the image.

Also whats animator doing?

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ws1999
ID: 6478343
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
 
LVL 92

Expert Comment

by:objects
ID: 6478363
Still haven't seen the code that actually draws the image on the component.
0
 

Author Comment

by:ws1999
ID: 6478379
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
 
LVL 92

Expert Comment

by:objects
ID: 6478387
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
 

Author Comment

by:ws1999
ID: 6478489
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
 
LVL 92

Expert Comment

by:objects
ID: 6478509
> 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
 

Author Comment

by:ws1999
ID: 6478839
how can i refresh what's on the applet in program?

ws1999
0
 
LVL 92

Expert Comment

by:objects
ID: 6478874
Yes, jest call the applets repaint() method.
0
 

Author Comment

by:ws1999
ID: 6478897
hi objiect,

repaint()has no effect in my program

ws1999
0
 
LVL 92

Expert Comment

by:objects
ID: 6478910
Where and why are you using it?
0
 

Author Comment

by:ws1999
ID: 6478934
i try it in many places after loading a new image,

i just want to disapear the old image.

ws1999
0
 
LVL 92

Expert Comment

by:objects
ID: 6478942
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
 

Author Comment

by:ws1999
ID: 6478977
hi object,

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

ws1999
0
 
LVL 92

Accepted Solution

by:
objects earned 30 total points
ID: 6478985
Because it does a repaint.

Did u change the clearRect() call I mentioned?
0
 

Author Comment

by:ws1999
ID: 6478990
also no effect, i really not too sure the principle of java image showing.

ws1999
0
 

Author Comment

by:ws1999
ID: 6481304
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
 

Author Comment

by:ws1999
ID: 6512910
too questions
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7085970
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
 
LVL 92

Expert Comment

by:objects
ID: 7086513
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
 
LVL 49

Expert Comment

by:DanRollins
ID: 7101276
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
 
LVL 5

Expert Comment

by:Netminder
ID: 7102364
Per recommendation, force-accepted.

Netminder
CS Moderator
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
hibernate insert example 13 39
jsp login check 12 53
Java: anonymous class 4 38
Netbeans and org.apache.commons.lang3 issue 3 26
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question