Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / December 2006

Tip: Looking for answers? Try searching our database.

drawImage() Question

Thread view: 
danny - 08 Dec 2006 22:33 GMT
Hello

I am traying to draw an image in a JPanel and I really don't know how,
I already searched in google but none of the answers worked ok,

where I have the problem is getting the image if I do this:

g.drawImage(img, x, y,(I don't understand what goes here)

How can I have the image loaded

Thank you
Danny
Andrew Thompson - 09 Dec 2006 01:22 GMT
> g.drawImage(img, x, y,(I don't understand what goes here)

If you name any one of the 5 overloaded methods
for Graphics.drawImage that start with 'Image,x,y',
I am sure that someone here will explain any parts
of it that you are unclear on.  In that event, hopefully
you will be able to figure the rest.

So.. do you want to name one?

Andrew T.
danny - 09 Dec 2006 02:48 GMT
> So.. do you want to name one?

Yes, the one that I am trying to use is the simplest one, the one that
needs the image the x and y coordinates ,the
width, height and the image observer . The image I have it saved in the
same file as the workspace for this application, I want to know how I
create this image argument.

Thank you
Danny
Andrew Thompson - 09 Dec 2006 04:54 GMT
> > So.. do you want to name one?
>
> Yes, the one that I am trying to use is the simplest one, the one that
> needs the image the x and y coordinates ,the
> width, height and the image observer .

OK.. but, Knute has already posted source (an
SSCCE, no less) that does exactly that. ...

>...The image I have it saved in the
> same file as the workspace for this application, I want to know how I
> create this image argument.

..The only difference is that he did not specifically
use an image in the local directory, but one on the
interent (so anyone can try it).

Note that the command he used (at the bottom -
to run it against an internet image), can easily be
changed to refer to a local file.

Try it, 1st exactly as it is with the image on the
internet, see how you go, and if you have any
problems, it would probably be best to add
further questions or comments to *that* strand
of the thread.

Andrew T.
Andrew Thompson - 09 Dec 2006 05:05 GMT
..
> ..The only difference is that he did not specifically
> use an image in the local directory, but one on the
> interent (so anyone can try it).

D'Oh!  Both local file and internet forms were shown.
I noticed the URL form, because a certain 'web
interface to usenet' formats it as a link* - it stands
out, the 'local file' form was immediately above it,
but (silly grin) I only glanced at the end of the post..

*
<http://groups.google.com/group/comp.lang.java.help/msg/2f44707c471b8b73>

A.
Knute Johnson - 09 Dec 2006 01:41 GMT
> Hello
>
[quoted text clipped - 9 lines]
> Thank you
> Danny

Danny:

It would be nice if you told us where the image actually is.  But let's
assume (you know what that makes us) that it is on disk or accessible
through the internet.  Let's also assume that you are using a modern
compiler, 1.4 or later.

g.drawImage(Image image,int x,int y,ImageObserver observer)

What goes there is the ImageObserver if you are using the
Producer/Observer pattern.  If you acquire the whole image before you
attempt to draw it you can put 'null' there.  If you are going to use
the Producer/Observer pattern you will probably put a reference to the
component that you are drawing on.  In most cases that will be 'this'.

Here is an example of how to read an image file and draw it on a JPanel.

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.net.*;
import javax.imageio.*;
import javax.swing.*;

public class ImageIOExample extends JPanel {
    BufferedImage image;

    public ImageIOExample(String urlString) {
        try {
            URL url = new URL(urlString);
            image = ImageIO.read(url);
            setPreferredSize(new Dimension(
             image.getWidth(),image.getHeight()));
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }

    public void paint(Graphics g) {
        // no observer required but may be included
        g.drawImage(image,0,0,null);
    }

    public static void main(final String[] args) {
        // GUI must be created on the EDT
        Runnable r = new Runnable() {
            public void run() {
                JFrame f = new JFrame("ImageIOExample");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                ImageIOExample iioe = new ImageIOExample(args[0]);
                f.add(iioe,BorderLayout.CENTER);
                f.pack();
                f.setVisible(true);
            }
        };
        // runs the Runnable above on the EDT
        EventQueue.invokeLater(r);
    }
}

To see an image on the local disk:

java ImageIOExample file:localimage.jpg

To see an image from the net:

java ImageIOExample http://www.thealpacastore.com/alpacacam/latest640.jpg

Signature

Knute Johnson
email s/nospam/knute/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.