Image doesn't display properly

Discussion in 'Java' started by rob.anteau@chartermi.net, Apr 27, 2006.

  1. Guest

    Hello everyone,

    I am very new to Java programming, or any programming for that matter.
    I have been playing with getting images to display in an application
    when an action is performed, but I have been experiencing problems.
    When I click the button the image doesn't display. If I click the
    button a second time the image displays. Could anyone tell me how I
    could resolve this?

    Here is a very simple version of my code.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;


    public class SimpleImage extends JFrame implements ActionListener{


    Image exampleImage;
    JButton btnExample;

    public SimpleImage( ) {

    super("Image Example");
    // Get the image from the file
    btnExample = new JButton("Click Me");

    Container pane = getContentPane( );

    FlowLayout flow = new FlowLayout();

    pane.setLayout(flow);
    pane.add(btnExample);

    // Set up the frame features
    setSize(400, 300);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);

    //event handler
    btnExample.addActionListener(this);
    }
    public void actionPerformed(ActionEvent e)
    {
    if(e.getSource()== btnExample){
    exampleImage =
    Toolkit.getDefaultToolkit().getImage("happyface.jpg");
    Graphics draw = getGraphics();
    draw.drawImage(exampleImage, 100, 175, this);
    }
    }

    public static void main(String[ ] args) {
    SimpleImage frame = new SimpleImage( );
    }
    }
     
    , Apr 27, 2006
    #1
    1. Advertising

  2. wrote:
    > Hello everyone,
    >
    > I am very new to Java programming, or any programming for that matter.
    > I have been playing with getting images to display in an application
    > when an action is performed, but I have been experiencing problems.
    > When I click the button the image doesn't display. If I click the
    > button a second time the image displays. Could anyone tell me how I
    > could resolve this?
    >
    > Here is a very simple version of my code.
    >
    > import java.awt.*;
    > import java.awt.event.*;
    > import javax.swing.*;
    >
    >
    > public class SimpleImage extends JFrame implements ActionListener{
    >
    >
    > Image exampleImage;
    > JButton btnExample;
    >
    > public SimpleImage( ) {
    >
    > super("Image Example");
    > // Get the image from the file
    > btnExample = new JButton("Click Me");
    >
    > Container pane = getContentPane( );
    >
    > FlowLayout flow = new FlowLayout();
    >
    > pane.setLayout(flow);
    > pane.add(btnExample);
    >
    > // Set up the frame features
    > setSize(400, 300);
    > setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    > setVisible(true);
    >
    > //event handler
    > btnExample.addActionListener(this);
    > }
    > public void actionPerformed(ActionEvent e)
    > {
    > if(e.getSource()== btnExample){
    > exampleImage =
    > Toolkit.getDefaultToolkit().getImage("happyface.jpg");
    > Graphics draw = getGraphics();
    > draw.drawImage(exampleImage, 100, 175, this);
    > }
    > }
    >
    > public static void main(String[ ] args) {
    > SimpleImage frame = new SimpleImage( );
    > }
    > }
    >


    Rob:

    You've got some general problems with your program design. First, all
    drawing should take place in the paint() of AWT components or the
    paintComponent() of Swing components. I know that this is
    counter-intuitive but it has to be or your program will not be able to
    repaint itself when another window is placed over it and removed or it
    is minimized and then subsequently maximized. So draw your image in the
    paintComponent(). To change images start a thread in your
    actionListener that downloads a new image and then call repaint. The
    reason to start a new thread is that you want your GUI to be functional
    while the image is being retrieved. If you wait in your actionListener
    you are blocking the EDT and none of your GUI will redraw or respond to
    input. Also unless you need to use an out of date JDK, forget the
    Toolkit.getImage(). Use the ImageIO.read() instead. It is synchronous
    and is much simpler than getImage().

    Here is a simple example of what you said you wanted to do. Replace the
    image file name with something you want to see.

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

    public class ImageTest extends JPanel {
    BufferedImage image;

    public ImageTest(BufferedImage image) {
    this.image = image;
    }

    public void paintComponent(Graphics g) {
    // if there is no image yet draw the message instead
    if (image != null)
    g.drawImage(image,0,0,getWidth(),getHeight(),null);
    else
    g.drawString("No Image Loaded!",20,getHeight()/2);
    }

    // this method is used to change images
    public void setImage(BufferedImage image) {
    this.image = image;
    }

    public static void main(String[] args) {
    // all Swing apps should be created on the EDT
    // do your GUI creation in a static method (in this case main)
    started
    // on the EDT with EventQueue.invokeLater()
    Runnable r = new Runnable() {
    public void run() {
    JFrame f = new JFrame("ImageTest");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // itest is final so that it can be seen in the
    actionlistener
    // of the button below
    final ImageTest itest = new ImageTest(null);
    // set the preferred size so the layout manager knows
    how big
    // to make your component
    itest.setPreferredSize(new Dimension(400,300));
    f.add(itest,BorderLayout.CENTER);

    JButton b = new JButton("Load Image");
    f.add(b,BorderLayout.NORTH);
    b.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
    // start a new thread to get the image so that this
    // method will return immediately
    Runnable r = new Runnable() {
    public void run() {
    try {
    // you can use a File, URL or
    InputStream
    // as the source of an image with
    ImageIO
    BufferedImage bi =
    ImageIO.read(new File("saturn.jpg"));
    // set the image you just read as
    the image
    // to draw on your component
    itest.setImage(bi);
    // call repaint to tell the manager to
    // redraw the component
    itest.repaint();
    } catch (IOException ioe) {
    ioe.printStackTrace();
    }
    }
    };
    new Thread(r).start();
    }
    });
    f.pack();
    f.setVisible(true);
    }
    };
    EventQueue.invokeLater(r);
    }
    }

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, Apr 28, 2006
    #2
    1. Advertising

  3. Thomas Weidenfeller, Apr 28, 2006
    #3
  4. Roedy Green Guest

    On Fri, 28 Apr 2006 09:58:33 +0200, Thomas Weidenfeller
    <> wrote, quoted or indirectly quoted someone
    who said :

    >> Graphics draw = getGraphics();

    >
    >
    >Bzzzzt, game over. Your whole application architecture is wrong. Have a
    >look at the GUI FAQ for a start, and work through Sun's GUI and Java 2D
    >tutorial. Pointers are in the FAQ.


    Another approach is start at http://mindprod.com/jgloss/paint.html
    and follow links.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Apr 28, 2006
    #4
  5. Oliver Wong Guest

    <> wrote in message
    news:...
    > Hello everyone,
    >
    > I am very new to Java programming, or any programming for that matter.
    > I have been playing with getting images to display in an application
    > when an action is performed, but I have been experiencing problems.
    > When I click the button the image doesn't display. If I click the
    > button a second time the image displays. Could anyone tell me how I
    > could resolve this?
    >
    > Here is a very simple version of my code.
    >

    [...]
    > exampleImage =
    > Toolkit.getDefaultToolkit().getImage("happyface.jpg");


    Have you considered using the ImageIO.read() method?
    http://java.sun.com/j2se/1.5.0/docs/api/javax/imageio/ImageIO.html#read(java.io.File)

    - Oliver
     
    Oliver Wong, Apr 28, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SWu
    Replies:
    1
    Views:
    340
  2. Joe
    Replies:
    1
    Views:
    492
  3. Henry S. Thompson

    SPAN tag doesn't display properly in JTextPane

    Henry S. Thompson, May 1, 2004, in forum: Java
    Replies:
    0
    Views:
    515
    Henry S. Thompson
    May 1, 2004
  4. Keeper
    Replies:
    0
    Views:
    576
    Keeper
    Apr 27, 2004
  5. Rob Blimploid
    Replies:
    3
    Views:
    125
    Mohit Sindhwani
    Apr 15, 2009
Loading...

Share This Page