Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: drawing a BufferedImage onto a JPanel

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    14
    Thanks
    2
    Thanked 2 Times in 1 Post

    Default drawing a BufferedImage onto a JPanel

    hey guys, i'm relatively new to java programming and recently tried to create a program that displays a BufferedImage onto a JPanel. i don't get any compiler errors when i compile, but the image doesn't show up.
    import javax.imageio.ImageIO;
    import java.io.File;
    import java.awt.image.BufferedImage;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.Graphics;
     
     
    public class ImageEditor{
     
    	public static void main(String[] args) throws java.io.IOException {
    		BufferedImage cat = ImageIO.read( new File("/* File */"));
    		JFrame canvas = new JFrame();
    		canvas.setSize(cat.getWidth()+100,cat.getHeight()+100);
    		canvas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		canvas.setTitle("It's a cat.");
    		Container pane = canvas.getContentPane();
    		ColorPanel panel = new ColorPanel(cat);
    		canvas.setVisible(true);
    	}
    }
     
    class ColorPanel extends JPanel{
    	BufferedImage theCat;
    	public ColorPanel(BufferedImage image){
    	theCat = image;
    	}
     
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);
    		Graphics2D g2d = (Graphics2D) g;
    		g2d.drawImage(theCat, null, 50,50);
    	}
    }

    i know i could use ImageIcon for this, but i'm trying to get used to BufferedImage. thanks!


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,235
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: drawing a BufferedImage onto a JPanel

    The panel to which you draw the image is never added to the content pane of the JFrame. Using the dialect of the code you posted, one line should do it
    pane.add(panel);

  3. The Following User Says Thank You to copeg For This Useful Post:

    nemo (October 13th, 2010)

  4. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    14
    Thanks
    2
    Thanked 2 Times in 1 Post

    Default Re: drawing a BufferedImage onto a JPanel

    Quote Originally Posted by copeg View Post
    The panel to which you draw the image is never added to the content pane of the JFrame. Using the dialect of the code you posted, one line should do it
    pane.add(panel);

    wow.. i feel really dumb right now. i can't believe i forgot that. thank you!

Similar Threads

  1. problem with drawing images on JPanel
    By Asido in forum What's Wrong With My Code?
    Replies: 13
    Last Post: July 19th, 2010, 03:07 PM
  2. Creating a bufferedimage!
    By Vexst in forum Java ME (Mobile Edition)
    Replies: 0
    Last Post: June 16th, 2010, 08:05 AM
  3. Drawing image on JPanel from another frame
    By jeryslo in forum AWT / Java Swing
    Replies: 3
    Last Post: December 8th, 2009, 03:01 PM
  4. Replies: 2
    Last Post: June 29th, 2009, 03:06 PM
  5. Creating and displaying a JPanel inside another JPanel
    By JayDuck in forum AWT / Java Swing
    Replies: 1
    Last Post: April 7th, 2009, 08:02 AM