Solved

Image .....from a gif file

Posted on 2004-10-04
15
171 Views
Last Modified: 2013-11-19
Hi Experts,
         
     How do I create an Image from a *.gif file ??? e.g.
   
       Image offscreen = new Image( ??? ("background0.gif"));

   thanks !
0
Comment
Question by:meow00
  • 7
  • 6
  • 2
15 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12223741
Image i = new ImageIcon("xyz.gif").getImage();
0
 
LVL 92

Expert Comment

by:objects
ID: 12223750
but if you want to create an offscreen image then you need to create a blank one:

BufferedImage offscreen = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = offscreen.getGraphics();
0
 
LVL 1

Author Comment

by:meow00
ID: 12223897
thanks very much ....

  so for the graphics g ... is it possible to load " *.gif " to it .... or it can only use functions like :
 fillOvals .... fillRect ... drawString ..etc.  ???

  thanks a lot !!!
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 92

Expert Comment

by:objects
ID: 12223900
yes that is correct
0
 
LVL 1

Author Comment

by:meow00
ID: 12223908
err ..............so .... does it mean I need to paint the offscreen pixel by pixel  ....... by myself ???
0
 
LVL 92

Assisted Solution

by:objects
objects earned 200 total points
ID: 12223913
you do the same painting you are already doing, just instead of painting to the screen you paint to the offscreen image.
Then in your paint() method you just paint the offscreen image to the screen.
0
 
LVL 1

Author Comment

by:meow00
ID: 12223985
Hi,

    Thanks very much ..... so I tried the offscreen in the following code .... but I still got "flash" .... did I make any mistake in the following code ??? please help !!! many thanks !!!

//-------------------------------------------------------------------  
   .... blah blah ....

    ImageIcon headImg = new ImageIcon(getClass().getResource("head.gif"));
    ImageIcon bgImg = new ImageIcon(getClass().getResource("background0.gif"));
   
    BufferedImage offscreen = new BufferedImage(400, 400, BufferedImage.TYPE_INT_RGB);
    Graphics offscreenG = offscreen.getGraphics();

      .... blah blah ....

  public void update(Graphics g)
    {
        paint(offscreenG);               // call the paint function with offscreen graphics
        g.drawImage(offscreen,0,0,this);    // draw image to screen
    }

   
    public void paint(Graphics offscreenG)
    {
        offscreenG.drawImage( bgImg.getImage(), 0, 0, 420, 420,  null) ;
        offscreenG.drawImage( headImg.getImage(), snake_x[0], snake_y[0], 20, 20, null) ;  
    }
   .... blah blah ......

 
0
 
LVL 7

Expert Comment

by:tomboshell
ID: 12224165
Looking at you code I would think that the actual screen painting occurs when the update method is being executed.  This will not always occur.  I do believe objects stated >> Then in your paint() method you just paint the offscreen image to the screen.

Set the image up in your constructor or initalizer, ie before you ever want to actually display it.  That keeps a ready reference to it, then paint it out upon need.    It may also help to set the container (probably JPanel)  to double buffered since sometimes that is all you need.  
0
 
LVL 1

Author Comment

by:meow00
ID: 12224174
thanks for the info ... but how do I  "Set the image up in your constructor or initalizer" exactly ???
0
 
LVL 92

Expert Comment

by:objects
ID: 12224311
problem could be that you are painting to the frame, I generally subclass JPanel instead of JPanel and have never has any flicker problems.
0
 
LVL 7

Accepted Solution

by:
tomboshell earned 200 total points
ID: 12224342
up there where you have >>.... blah blah ....

That is what I would say, so then you would have it stored.  But see what Objects says...

The paint method will be called from outside your class also, ie when the screen (panel) is getting painted it will call your paint method also.  Then a different graphics object will be sent in (like for the screen or panel), that is where you want to draw your stored image.

Maybe the following will give you some ideas....or not

public class YourBlahClass implements ImageObserver{
// this class can now 'observe' itself
    ImageIcon headImg;
    ImageIcon bgImg;
   
    private int x=0, y=0, width = 0, height = 0; // properties of the combo-image

    BufferedImage offscreen;
    Graphics offscreenG;
    private Graphics summonerGraphics;

  public YourBlahClass(){  //constructor
        headImg  = new ImageIcon(getClass().getResource("head.gif"));
        bgImg  = new ImageIcon(getClass().getResource("background0.gif"));
        offscreen  = new BufferedImage(400, 400, BufferedImage.TYPE_INT_RGB);
        offscreenG  = offscreen.getGraphics();

       offscreenG.drawImage( bgImg.getImage(), 0, 0, 420, 420,  null) ;
       offscreenG.drawImage( headImg.getImage(), snake_x[0], snake_y[0], 20, 20, null) ;
   }

  public boolean imageUpdate(Image img, int infoF, inx x1, int y1, int width1, int height1){
    // store the x,y, width, height for possible later adjustment
     // then call the paint method
     paint(summonerGraphics);  // set the first time this was called.  
  }

 public void paint(Graphics g){  // the graphics object will be coming from whatever wants to show the image...
    g.drawImage(offscreenG, x,y, width, height, this);
    summonerGraphics = g;
 }
}
0
 
LVL 1

Author Comment

by:meow00
ID: 12235908
Hi object, I tried JPanel ... but still got the same problem .....

Hi tomoshell, ... I tried the above method ... but still got flickers .......

still working on it ........
0
 
LVL 92

Expert Comment

by:objects
ID: 12235987
> Hi object, I tried JPanel ... but still got the same problem .....

can you post the code
0
 
LVL 1

Author Comment

by:meow00
ID: 12245664
problem solved ... thanks all !
0
 
LVL 92

Expert Comment

by:objects
ID: 12245676
(:
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HSSFWorkbook cannot be resolved error 10 68
couple of eclipse 5 36
Oracle SQL syntax check  without executing 6 50
CSV file parsing thru Java 13 27
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Many programs have tried to outwit PowerPoint in terms of technology and skill. These programs, however, still lack several characteristics that PowerPoint has possessed from the start. Here's why PowerPoint replacements won't entirely work for desi…
Learn how to create interesting presentations by including videos to keep your audience engaged using Prezi. Select "Insert" from the top menu in your Prezi editor: Select "YouTube Video": Paste the video URL into the prompt: "Select "Insert":…
Learn how to download your full Prezi presentation for offline presenting. Prezi doesn’t have to be viewed and shared in a web browser, even with a free account you can download your full presentation to share with others. Be sure to download any vi…

776 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