Results 1 to 6 of 6
Like Tree3Likes
  • 1 Post By DarrylBurke

Thread: How to display image on the frame in java?

  1. #1
    Java Example is offline Moderator
    Join Date
    Sep 2014
    Posts
    27
    Rep Power
    0

    Default How to display image on the frame in java?

    In this tutorial, we will show how to display image on the frame. In the example below, three methods have been used to display the image on the frame in your application. The MediaTracker is the class of j¬ava.awt.*; package, which is a utility class that tracks the status of a number of media objects. And this type of object can include images and audio clips. The MediaTracker class is used after creating the instance for that and calling the addImage() of the MediaTracker. The AwtImage() is the constructor of the AwtImage class in which, the instance of the MediaTracker class has been created to add the image on the frame using the addImage() method of the MediaTracker class. This constructor set the size, visibility and the close operation on the close button of the frame.And finally the addImage() method of the MediaTracker class which is used load the image.

    Java Code: Program to display image on the frame
    import java.awt.*;
    import java.awt.event.*;
    
    public class AwtImage extends Frame {
    
        Image img;
    
        public static void main(String[] args) {
            AwtImage ai = new AwtImage();
        }
    
        public AwtImage() {
            super("Image Frame");
            MediaTracker mt = new MediaTracker(this);
            img = Toolkit.getDefaultToolkit().getImage("icon_confused.gif");
            mt.addImage(img, 0);
            setSize(400, 400);
            setVisible(true);
            addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent we) {
                    dispose();
                }
            });
        }
    
        public void update(Graphics g) {
            paint(g);
        }
    
        public void paint(Graphics g) {
            if (img != null) {
                g.drawImage(img, 100, 100, this);
            } else {
                g.clearRect(0, 0, getSize().width, getSize().height);
            }
        }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How to display image on the frame in java?

    Why are you posting AWT code? Most people are using Swing?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: How to display image on the frame in java?

    Probably some kid trying to show how 'cool' he is by copying and pasting code found all over the net.

    But the real fault lies with the nincompoop who created a section for AWT Examples.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    JavaForums's Avatar
    JavaForums is offline Administrator
    Join Date
    Dec 2006
    Posts
    877
    Rep Power
    10

    Default Re: How to display image on the frame in java?

    Hi,

    I have posted an answer to the moderators forum for this issue.
    By the way, if you have good reasons for calling me nincompoop (a new word for me), let me know those reasons. ;)

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: How to display image on the frame in java?

    As i have said in my reply to your PM, AWT lost relevance several years ago. Beginners look for simple examples like the ones posted in this section; however, no beginner would or should be interested in long outdated AWT code, least of all shoddy code copy/pasted from other sites.

    Also, your protracted absence from these forums and lack of response to PMs, coupled with a steady increase of spam attacks encouraged no doubt by the non-implementation of modern security measures like CAPTCHAs and email verifications, led to a general belief that the site had been hijacked. To quote figures: in about two months from February 7 to April 9 I banned a total of 2621 spammer accounts. Then I gave up and dissociated myself from moderator duties here. See the thread 'Spam-o-Manual' in the Moderators section for details.

    Oh, and my choice of words was intentional. The intention was to elicit a response.

    db
    jim829 likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    JavaForums's Avatar
    JavaForums is offline Administrator
    Join Date
    Dec 2006
    Posts
    877
    Rep Power
    10

    Default Re: How to display image on the frame in java?

    Oh, and my choice of words was intentional. The intention was to elicit a response.
    Ok, as i said in my reply to your PM, i understand the situation better now.

Similar Threads

  1. Display Java Image
    By jumpman8947 in forum New To Java
    Replies: 2
    Last Post: 08-09-2013, 05:10 AM
  2. Unable to Display Two Objects in Frame
    By AryanArs in forum AWT / Swing
    Replies: 4
    Last Post: 02-26-2012, 06:56 PM
  3. Unable to Display Two Objects in Frame
    By AryanArs in forum New To Java
    Replies: 2
    Last Post: 02-26-2012, 06:43 AM
  4. Display filenames in frame
    By cool in forum AWT / Swing
    Replies: 1
    Last Post: 11-19-2010, 02:51 PM
  5. Simple Java image stream display
    By hoobajue in forum New To Java
    Replies: 2
    Last Post: 08-17-2010, 01:18 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •