Page 2 of 6 FirstFirst 1234 ... LastLast
Results 21 to 40 of 104
Like Tree2Likes

Thread: What is the absolute easiest way to display an image in Swing?

  1. #21
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,543
    Rep Power
    5

    Default Re: What is the absolute easiest way to display an image in Swing?

    Quote Originally Posted by gimbal2 View Post
    Java Code:
    JPanel p = new JPanel();
    f.add(p);
    You add things to the content pane of the frame, not the frame itself
    Actually, JFrame.add() does add it to the content pane. The method has been overridden to do this.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  2. #22
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: What is the absolute easiest way to display an image in Swing?

    Never mind, fixed that path error. Now it is correctly reading the image, but the image will not show up. I read all your replies. But could someone just clearly explain (from here) exactly what I have to do to have it displayed? Thanks

  3. #23
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,543
    Rep Power
    5

    Default Re: What is the absolute easiest way to display an image in Swing?

    You are doing f.add(p). As was said earlier you don't want to add p but an instance of the class that extends JPanel.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #24
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: What is the absolute easiest way to display an image in Swing?

    Quote Originally Posted by jim829 View Post
    You are doing f.add(p). As was said earlier you don't want to add p but an instance of the class that extends JPanel.

    Regards,
    Jim
    Hey Jim,
    Ok, I did that.
    It is still the same error though

    Here is the changed line
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    
    
    public class DisplayImage extends JPanel {
    	private BufferedImage image;
    	public DisplayImage(){
    		try{
    			image = ImageIO.read(new File("/Users/MWaldman/Downloads/petergriffin.png"));
    		} catch(Exception e){System.out.println("Error");}
    	}
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);
    		g.drawImage(image,0,0,null);
    	}
    	public void setUp(){
    		 JFrame f = new JFrame("Peter Griffin");
    		    f.setSize(500,500);
    		    f.setVisible(true);
    		    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		    JPanel p = new JPanel();
    		    f.add(new DisplayImage());
                        p.add(new DisplayImage());
    		    p.setLayout(new BorderLayout());
    }
    	
    	public static void main(String[] args) {
       DisplayImage l = new DisplayImage();
       l.setUp();
        
    	}
    
    }
    Last edited by MW130; 01-16-2014 at 06:39 PM.

  5. #25
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,543
    Rep Power
    5

    Default Re: What is the absolute easiest way to display an image in Swing?

    Nope. Should be f.add(this);

    And get rid of the JPanel stuff in your constructor. You don't need it. That's the purpose of extending JPanel. I suggest you read the tutorials for more info.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #26
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: What is the absolute easiest way to display an image in Swing?

    Got it. Fixed that, and well, not really a surprise, the code still does not fix it :(
    Window opens, image does not display.
    Last edited by MW130; 01-16-2014 at 06:46 PM.

  7. #27
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,543
    Rep Power
    5

    Default Re: What is the absolute easiest way to display an image in Swing?

    Please show your new code.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #28
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: What is the absolute easiest way to display an image in Swing?

    yessir:

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    
    
    public class DisplayImage extends JPanel {
    	private BufferedImage image;
    	public DisplayImage(){
    		try{
    			image = ImageIO.read(new File("/Users/MWaldman/Downloads/petergriffin.png"));
    		} catch(Exception e){System.out.println("Error");}
    	}
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);
    		g.drawImage(image,0,0,null);
    	}
    	public void setUp(){
    		 JFrame f = new JFrame("Peter Griffin");
    		    f.setSize(500,500);
    		    f.setVisible(true);
    		    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		    f.add(this);
    		    setLayout(new BorderLayout());
    }
    	
    	public static void main(String[] args) {
       DisplayImage l = new DisplayImage();
       l.setUp();
        
    	}
    
    }

  9. #29
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,543
    Rep Power
    5

    Default Re: What is the absolute easiest way to display an image in Swing?

    I just ran your program with my own image and it worked fine. The only difference was that I used a JPEG image. Are you certain you are not getting an error on the console. If you are using Windows do you need to put a drive letter in front of the file name path?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  10. #30
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: What is the absolute easiest way to display an image in Swing?

    I got it working. It was just a matter of resizing. I wish it would show up without having to resize the window though. Thank you all for the help.

  11. #31
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,543
    Rep Power
    5

    Default Re: What is the absolute easiest way to display an image in Swing?

    Use the image getWidth and getHeight methods. Then use setPreferredSize on your class. Then do a f.pack(). And after you do a f.pack() you can have it centered on the screen with f.setLocationRelativeTo(null). After you do all that, then make the frame visible (otherwise, it will jump) from upper left to center.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  12. #32
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    164
    Rep Power
    2

    Default Re: What is the absolute easiest way to display an image in Swing?

    Also, if you want to export the jar file to use somewhere else you may want to put the images in your build path and then load them like this

    Java Code:
    try {
        image = ImageIO.read(getClass().getResource("path");
    }
    catch(IOExcpetion e) {
        e.printStackTrace();
    }
    This will work better if you export the jar later.

  13. #33
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,914
    Rep Power
    5

    Default Re: What is the absolute easiest way to display an image in Swing?

    Quote Originally Posted by jim829 View Post
    Actually, JFrame.add() does add it to the content pane. The method has been overridden to do this.

    Regards,
    Jim
    I should have guessed that.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  14. #34
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,914
    Rep Power
    5

    Default Re: What is the absolute easiest way to display an image in Swing?

    Quote Originally Posted by MW130 View Post
    I got it working. It was just a matter of resizing. I wish it would show up without having to resize the window though. Thank you all for the help.
    Well then keep looking for the bug in your code that causes you to need to resize first.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  15. #35
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    176
    Rep Power
    2

    Default Re: What is the absolute easiest way to display an image in Swing?

    I know you are using Awt graphics and the paint function but if you want Tommorow I can post code that displays A image using Swing Components
    With the right know how, anything is possible

  16. #36
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: What is the absolute easiest way to display an image in Swing?

    Yes please

  17. #37
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    176
    Rep Power
    2

    Default Re: What is the absolute easiest way to display an image in Swing?

    Here you go hope it helps
    Java Code:
    import javax.swing.*;
     
     
     
    public class myGame extends JFrame { //creates JFrame
    	 
        
        public static void main(String[] args) {
        JFrame f = new JFrame("Peter Griffin");
        f.setSize(500,500);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ImageDisplayer tester = new ImageDisplayer(); //creates object Image Displayer
        tester.addImage();//call method addImage inside Image Displayer class
        f.add(tester);//add Image Displayer
        f.setVisible(true); //let us see screen
        }
    }
    class ImageDisplayer extends JPanel{ //creates a JPanel to display Image I refrence it as its own object in a new class out of prefrence i just find it easier 
    	ImageIcon petergriffin = new ImageIcon("Users/MW/Downloads/petergriffin.png");//Image Icon is a Image that is meant to be added to a object
        JLabel petergriffinImg = new JLabel(petergriffin);//JLabels work great for holding Image Icons so we create one that holds the Image Icon
        public void addImage(){
        	petergriffinImg.setBounds(0,0,150,150); //Set were we want it with the parameteres (x,y,width,height)
        	add(petergriffinImg);//adds it to the jpanel called Image displayer
            } 
    	
    }
    With the right know how, anything is possible

  18. #38
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: What is the absolute easiest way to display an image in Swing?

    Quote Originally Posted by Daryn View Post
    Here you go hope it helps
    Java Code:
    import javax.swing.*;
     
     
     
    public class myGame extends JFrame { //creates JFrame
    	 
        
        public static void main(String[] args) {
        JFrame f = new JFrame("Peter Griffin");
        f.setSize(500,500);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ImageDisplayer tester = new ImageDisplayer(); //creates object Image Displayer
        tester.addImage();//call method addImage inside Image Displayer class
        f.add(tester);//add Image Displayer
        f.setVisible(true); //let us see screen
        }
    }
    class ImageDisplayer extends JPanel{ //creates a JPanel to display Image I refrence it as its own object in a new class out of prefrence i just find it easier 
    	ImageIcon petergriffin = new ImageIcon("Users/MW/Downloads/petergriffin.png");//Image Icon is a Image that is meant to be added to a object
        JLabel petergriffinImg = new JLabel(petergriffin);//JLabels work great for holding Image Icons so we create one that holds the Image Icon
        public void addImage(){
        	petergriffinImg.setBounds(0,0,150,150); //Set were we want it with the parameteres (x,y,width,height)
        	add(petergriffinImg);//adds it to the jpanel called Image displayer
            } 
    	
    }
    This opens the window, but does not show the image. Also, since this is a game, how would I set x and y coords for the jlabel?

  19. #39
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    176
    Rep Power
    2

    Default Re: What is the absolute easiest way to display an image in Swing?

    It might not show because of you path I have mine starting with the name of the drive C:/ and then to make it move you would use absolute positioning first
    Java Code:
    this.setLayout(null)//it doesnt use layout manager
           setBounds(x,y,imageWidth,imageHeight);
    With the right know how, anything is possible

  20. #40
    natdizzle's Avatar
    natdizzle is offline Nathan Nelson
    Join Date
    Jan 2009
    Posts
    100
    Rep Power
    0

    Default Re: What is the absolute easiest way to display an image in Swing?

    are you still having problems?

Page 2 of 6 FirstFirst 1234 ... LastLast

Similar Threads

  1. Retina Display and Swing
    By MW130 in forum New To Java
    Replies: 0
    Last Post: 01-30-2013, 01:00 AM
  2. Swing Bar Graph will not display in JPanel
    By Redefine12 in forum AWT / Swing
    Replies: 5
    Last Post: 05-01-2012, 01:07 AM
  3. Swing UI display
    By bprakash in forum AWT / Swing
    Replies: 15
    Last Post: 11-20-2011, 02:19 PM
  4. Replies: 4
    Last Post: 04-08-2009, 05:51 AM
  5. Swing program to display JVM information
    By satya007 in forum AWT / Swing
    Replies: 3
    Last Post: 11-13-2007, 09:59 AM

Posting Permissions

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