Results 1 to 3 of 3
  1. #1
    hopelessnoob is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default button displays image

    I have no idea where in my code I'm going wrong.
    When I press the button (which is otherwise healthy) nothing happens.

    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    
    public class picdisplay implements ActionListener {
    
    JButton button;
    JFrame frame;
    
    public static void main (String[] args) {
    picdisplay ducpic=new picdisplay();
    ducpic.go();
    }
    
    public void go () {
    frame=new JFrame();
    button=new JButton("click to view");
    button.addActionListener(this);
    frame.getContentPane().add(button);
    frame.setSize(300,300);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
    public void actionPerformed (ActionEvent event) {
    DuckPicJPanel pic=new DuckPicJPanel();
    frame.getContentPane().remove(button);
    frame.getContentPane().add(pic);
    pic.repaint();
    }
    }
    the panel code is:
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    class DuckPicJPanel extends JPanel {
    public void paintComponent (Graphics g) {
    Image image=new ImageIcon("rubberDuck.jpeg").getImage();
    g.drawImage(image,0,0,this);
    }
    }
    any help would be appreciated.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Do not read in images in a paint or paintComponent method if you don't want your program graphics to be very slow. Instead, read in the image in the class's constructor and save it to a class field.

    Next, after you change a container's components, you'll need to call revalidate for the components to be laid out correctly in the container.

    Java Code:
    public void actionPerformed (ActionEvent event) {
      DuckPicJPanel pic=new DuckPicJPanel();
      frame.getContentPane().remove(button);
      frame.getContentPane().add(pic);
      ((JPanel)frame.getContentPane()).revalidate();
      frame.getContentPane().repaint();
    }

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default

    Do not read in images in a paint or paintComponent method if you don't want your program graphics to be very slow.
    Actually, in this case, since the ImageIcon constructor does not block on Toolkit's image loading, in most cases the image will never be fully loaded and there will be nothing to be painted.

    @hopelessnoob : Painting method overrides must be limited to painting and painting alone. No I/O, no computations that affect the state of the class, no component addition/removal and no relayout. Keep that in mind and it'll serve you well when you get into more complicated custom painting.

    db

Similar Threads

  1. AWT button image
    By timkd127 in forum AWT / Swing
    Replies: 2
    Last Post: 03-01-2010, 05:41 AM
  2. Web site Image Displays based on day of the week?
    By foxdog175 in forum New To Java
    Replies: 6
    Last Post: 04-15-2009, 01:21 AM
  3. Set focus on image button in swt
    By diva_garg in forum New To Java
    Replies: 3
    Last Post: 08-18-2008, 01:27 PM
  4. Get Focus on Image button
    By diva_garg in forum SWT / JFace
    Replies: 0
    Last Post: 08-08-2008, 12:14 PM
  5. creating an image button when using struts
    By yuchuang in forum Web Frameworks
    Replies: 1
    Last Post: 05-03-2007, 05:48 PM

Posting Permissions

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