how do you put images on a screen?

  • Follow


can someone give me some simple code only showing me how to put images
on a screen (from an image file)

0
Reply blahbah7 (8) 10/22/2007 8:18:17 PM

blah bah7 wrote:
> can someone give me some simple code only showing me how to put images
> on a screen (from an image file)
> 

.... The acronyms `GIYF' (Google is your friend) and `RTFM' (Read the 
fine manual) are relevant here.

<http://java.sun.com/javase/6/docs/> is a great resource, including even 
links to a plethora of manuals.

<http://java.sun.com/javase/6/docs/api/java/awt/Image.html> is another 
good starting point.

-- 
Beware of bugs in the above code; I have only proved it correct, not 
tried it. -- Donald E. Knuth
0
Reply Joshua 10/22/2007 8:43:03 PM


On Oct 22, 4:43 pm, Joshua Cranmer <Pidgeo...@verizon.invalid> wrote:
> blah bah7 wrote:
> > can someone give me some simple code only showing me how to put images
> > on a screen (from an image file)
>
> ... The acronyms `GIYF' (Google is your friend) and `RTFM' (Read the
> fine manual) are relevant here.
>
> <http://java.sun.com/javase/6/docs/> is a great resource, including even
> links to a plethora of manuals.
>
> <http://java.sun.com/javase/6/docs/api/java/awt/Image.html> is another
> good starting point.
>
> --
> Beware of bugs in the above code; I have only proved it correct, not
> tried it. -- Donald E. Knuth

i was hoping for an example so i could study the example...

0
Reply blah 10/22/2007 8:59:39 PM

Joshua Cranmer wrote:
>> ... The acronyms `GIYF' (Google is your friend) and `RTFM' (Read the
>> fine manual) are relevant here.
>>
>> <http://java.sun.com/javase/6/docs/> is a great resource, including even
>> links to a plethora of manuals.
>>
>> <http://java.sun.com/javase/6/docs/api/java/awt/Image.html> is another
>> good starting point.

blah bah7 wrote:
> i [sic] was hoping for an example so i could study the example...

And your hope will be realized when you follow Joshua's advice.

-- 
Lew
0
Reply Lew 10/22/2007 9:07:46 PM

blah bah7 wrote:
> i was hoping for an example so i could study the example...
> 

If you want an example, you are going to need to tell us some of the 
context of what you want to do. I can think of two (three?) 
fundamentally different ways to display a picture off of the top of my 
head, depending on what the overarching goal is. "Code to display an 
image" is only a notch or two less vague than "Code to display output."

-- 
Beware of bugs in the above code; I have only proved it correct, not 
tried it. -- Donald E. Knuth
0
Reply Joshua 10/22/2007 9:10:52 PM

On Oct 22, 5:10 pm, Joshua Cranmer <Pidgeo...@verizon.invalid> wrote:
> blah bah7 wrote:
> > i was hoping for an example so i could study the example...
>
> If you want an example, you are going to need to tell us some of the
> context of what you want to do. I can think of two (three?)
> fundamentally different ways to display a picture off of the top of my
> head, depending on what the overarching goal is. "Code to display an
> image" is only a notch or two less vague than "Code to display output."
>
> --
> Beware of bugs in the above code; I have only proved it correct, not
> tried it. -- Donald E. Knuth

say the image was pac_man.gif

i want to know how to say something like "display pac_man.gif at x,y",
only in a working example

0
Reply blah 10/22/2007 11:53:58 PM

blah bah7 wrote:
> say the image was pac_man.gif
> 
> i [sic] want to know how to say something like "display pac_man.gif at x,y",
> only in a working example

<http://java.sun.com/docs/books/tutorial/2d/index.html>

-- 
Lew
0
Reply Lew 10/23/2007 12:58:06 AM

blah bah7 wrote:
>..."display pac_man.gif at x,y",

<sscce>
import javax.swing.*;
import java.net.URL;

class ImageAnywhereOnScreen {

  public static void main(final String[] args)
    throws Exception {

    final URL imageLocation =
      new URL("http://java.sun.com/images/lgsun.gif");
    Runnable r = new Runnable() {
      public void run() {
        int x = 300;
        int y = 200;
        if (args.length>=2) {
          try {
            x = Integer.parseInt(args[0]);
            y = Integer.parseInt(args[1]);
          } catch(Exception e) {
            //use defaults
            System.err.println(
              "Number(s) unparsable as "+
              "integer, using defaults" );
          }
        }

        JLabel label = new JLabel(
          new ImageIcon(
            imageLocation));
        JWindow w = new JWindow();
        w.setLocation(x,y);
        w.add(label);
        w.pack();
        w.setVisible(true);
      }
    };
    SwingUtilities.invokeLater(r);
  }
}
</sscce>

-- 
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via http://www.javakb.com

0
Reply Andrew 10/23/2007 1:03:42 AM

On Oct 23, 11:03 am, "Andrew Thompson" <u32984@uwe> wrote:
> blah bah7 wrote:
> >..."display pac_man.gif at x,y",
....
> class ImageAnywhereOnScreen {

As an aside, that example was intended to help you
appreciate the advice already offered by Joshua and
Lew.

The Layouts tutorial might also help with that 'x,y'
bit, although Lew's pointer to the 2D part of the
tutorial is another way to do it (whereas the way I
place the image in the example, is 'crude and simple').

Andrew T.

0
Reply Andrew 10/23/2007 1:26:05 AM

blah bah7 wrote:
> On Oct 22, 5:10 pm, Joshua Cranmer <Pidgeo...@verizon.invalid> wrote:
>> blah bah7 wrote:
>>> i was hoping for an example so i could study the example...
>> If you want an example, you are going to need to tell us some of the
>> context of what you want to do. I can think of two (three?)
>> fundamentally different ways to display a picture off of the top of my
>> head, depending on what the overarching goal is. "Code to display an
>> image" is only a notch or two less vague than "Code to display output."
>>
>> --
>> Beware of bugs in the above code; I have only proved it correct, not
>> tried it. -- Donald E. Knuth
> 
> say the image was pac_man.gif
> 
> i want to know how to say something like "display pac_man.gif at x,y",
> only in a working example
> 

Given this context, it seems to me that your ultimate end result would 
fall into this sort of pattern: [ much code removed for conciseness]

public class GameDisplay extends JComponent {
     private static Image pacMan;
     private int x, y;

     public GameDisplay() {
         if (pacMan == null) {
             pacMan = this.getToolkit().createImage("pac_man.gif");
         }
     }

     public void paintComponent(Graphics g) {
         // Do other stuff
         g.drawImage(pacMan, x, y, this);
         // Do more stuff
     }
}

(note: code untested)
-- 
Beware of bugs in the above code; I have only proved it correct, not 
tried it. -- Donald E. Knuth
0
Reply Joshua 10/23/2007 2:05:22 AM

Andrew Thompson wrote:
> ...  Lew's pointer to the 2D part of the
> tutorial is another way to do it (whereas the way I
> place the image in the example, is 'crude and simple').

I came into Swing programming ass backwards, and I don't have anywhere near 
Andrew's expertise, particularly in the GUI aspects of Swing.  A couple of 
years back I did a project to print /n/-up images, much as a printer runs off 
a batch of business cards.  (/n/-up printing is a grid layout of the same 
item, /n/ to a row, as many rows as fit on a page.)  This required extensive 
use of Java 2D and affine transforms.  Yummy.

People gripe about Swing as a GUI framework, but really it's quite potent.  I 
used to program GUIs in Motif and Open Look; there's a distinct similarity of 
outlook among these frameworks.

Anyway, that experience has me more attuned to the 2D library than some folks 
might be.

FWIW, I implemented the project as a grid of JPanels with a BufferedImage 
drawn into each of them, scaled to fit.  Details escape me after this time, 
but I think I also played some fun games with laying out text over top of the 
image within each JPanel.

Then the print library came into play.  More fun.  I was surprised to find 
that Java lets a body do some magic with a printer.

The result was a WYSIWYG /n/-up print-layout tool.

After some learning curve, including even buying a book, it really wasn't so 
hard to do it.  Familiarity with the API was the key, as with everything Java.

It did turn out pretty hard to put together what I wanted to know about the 
Java printing libraries.  For one thing, there are at least two.  Not entirely 
incompatible with each other.  For another no one source really said much 
about it.  There would be a little bit here, another take on it there, all 
over the place.  It took me some effort to synthesize information from the 
disparate sources in that area.

-- 
Lew
0
Reply Lew 10/23/2007 3:46:31 AM

On Mon, 22 Oct 2007 13:18:17 -0700, blah bah7 <blahbah7@gmail.com>
wrote, quoted or indirectly quoted someone who said :

>can someone give me some simple code only showing me how to put images
>on a screen (from an image file)

see http://mindprod.com/jgloss/image.html
-- 
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
0
Reply Roedy 10/23/2007 7:07:38 AM

On Mon, 22 Oct 2007 16:53:58 -0700, blah bah7 <blahbah7@gmail.com>
wrote, quoted or indirectly quoted someone who said :

>i want to know how to say something like "display pac_man.gif at x,y",
>only in a working example

see http://mindprod.com/jgloss/image.html

the ImageViewer class lets you plop images much the way you position
Buttons with a layout.

ImageViewer is bundled part of
http://mindprod.com/products1.html#COMMON11
-- 
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
0
Reply Roedy 11/5/2007 9:48:13 PM

12 Replies
339 Views

(page loaded in 0.144 seconds)

Similiar Articles:


















7/23/2012 11:51:32 AM


Reply: