Solved

how to repaint the image on the canvas

Posted on 2001-09-12
24
264 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
 

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
scoresSpecial  challenge 13 42
nestparen challenge 4 57
recursion example 16 70
mockito example issue 8 38
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

747 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now