?
Solved

"get image " in a frame!!!!

Posted on 2000-02-09
2
Medium Priority
?
161 Views
Last Modified: 2010-04-01
Hello expert;
   What i can do to use function "get image " in a frame.I have try it before but after i compiled it, the compiler show me an error. The error message is "no getImage function in class frame". So what i can do if i want to call a ".gif" file into a frame.
 Thanks to answer my question .
0
Comment
Question by:tseau
2 Comments
 
LVL 16

Expert Comment

by:heyhey_
ID: 2506806
Image Toolkit.getDefaultToolkit().getImage(String filename)

Image Toolkit.getDefaultToolkit().getImage(URL url)
0
 
LVL 9

Accepted Solution

by:
Ovi earned 200 total points
ID: 2506832
import java.awt.*;
import java.io.*;
public class cFrame extends Frame {
Image img = null;

    public cFrame() {
        super();
        this.setSize(400, 400);
        this.setLocation(100, 100);
        this.setBackground(Color.green);
        this.setLayout(null);
        img = readImg("YourGifFile.gif");
        repaint();
    }

    public Image readImg(String file) {
        Image img = null;
        byte[] stream=new byte[100000];
        try {
            FileInputStream fis = new FileInputStream (file);
            int x = fis.read(stream);                
        } catch (EOFException e) {
            System.out.println("All is ok,  ...Done");
        } catch (Exception e) {
            System.out.println("All is wrong,  ...Failed "+e.toString());
        }
        Toolkit atk;
        atk = java.awt.Toolkit.getDefaultToolkit();
        img = atk.createImage(stream);
        return img;
    }

    public void update(Graphics g) {
        paint(g);
    }

    public void paint(Graphics g) {
        if(img!=null)
            g.drawImage(img, 0, 0, size().width, size().height, this);
    }    
    public static void main(String arg[]) {
        cFrame x = new cFrame();
        x.setVisible(true);
    }
}


Second method is to use java.awt.Toolkit  to read the image :


import java.awt.*;
import java.io.*;
public class cFrame extends Frame {
Image img = null;

    public cFrame() {
        super();
        this.setSize(400, 400);
        this.setLocation(100, 100);
        this.setBackground(Color.green);
        this.setLayout(null);
        Toolkit t;
        t = java.awt.Toolkit.getDefaultToolkit();
        img = t.getImage("YourGifFile.gif");//you can use an URL too
        repaint();
    }

    public void update(Graphics g) {
        paint(g);
    }

    public void paint(Graphics g) {
        if(img!=null)
            g.drawImage(img, 0, 0, size().width, size().height, this);
    }    
    public static void main(String arg[]) {
        cFrame x = new cFrame();
        x.setVisible(true);
    }
}

0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

601 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