4 Replies - 52613 Views - Last Post: 14 March 2010 - 06:50 PM Rate Topic: -----

#1 nikoll  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 16-November 09

How to add image on JPanel (Java swing)

Posted 14 March 2010 - 08:09 AM

I would like to add image from specific location (folder) to JPanel and to specify location where it will be places on JPanel.

Does anyone have idea how to do it ? I found different posts, but all of them use BufferImage and AWT, and it is not working because I use JPanel from Java Swing.

Thank You for any assistance!
Is This A Good Question/Topic? 0
  • +

Replies To: How to add image on JPanel (Java swing)

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: How to add image on JPanel (Java swing)

Posted 14 March 2010 - 08:19 AM

you will have to override the paintComponent(Graphics g) of JPanel.
and call drawImage of the Graphics class:
public void paintComponent(Graphics g){
       super.paintComponent(g);
       //call drawImage here..
       }


here is a good example to show you how to draw on a JPanle:
http://www.particle....r06/images.html

This post has been edited by japanir: 14 March 2010 - 08:20 AM

Was This Post Helpful? 1
  • +
  • -

#3 xor-logic  Icon User is offline

  • HAL9000 was an Apple product
  • member icon

Reputation: 128
  • View blog
  • Posts: 764
  • Joined: 04-February 10

Re: How to add image on JPanel (Java swing)

Posted 14 March 2010 - 08:21 AM

Edit: Did not fully understand what OP was asking

This post has been edited by xor-logic: 14 March 2010 - 08:25 AM

Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,025
  • Joined: 15-July 08

Re: How to add image on JPanel (Java swing)

Posted 14 March 2010 - 08:23 AM

Well, to load the image into a BufferedImage, I suggest using the ImageIO.read(File object) method as it quickly reads it and successfully.

As for actually painting it on a JPanel, use the Graphics drawImage method to draw the BufferedImage to screen. Here is what I do to load it.

	public BufferedImage scanImage(String fn) throws IOException {
		BufferedImage img = null;
		URL url = new URL("http://antwrp.gsfc.nasa.gov/apod/image/1003/centaurusA_carruthers.jpg");		
		img = ImageIO.read(url);
		
		return img;
	}



Now, I have actually written a JPanel that repaints itself every 60 milliseconds:
public class PicturePanel extends JPanel {


	private static final long serialVersionUID = -8687907176608557245L;
	
	private BufferedImage img = null;
	
	public PicturePanel() {
		Timer time = new Timer(60, new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				repaint();				
			}
			
		});
		
		time.start();
	}
	
	public void setImage(BufferedImage image) {
		img = image;
	}
	
	public void paintComponent(Graphics g) {
		super.paintComponent(g);		
		Image drawImage;
		if (img != null) {
			drawImage = img.getScaledInstance(
					-1, this.getHeight() - 4, BufferedImage.SCALE_DEFAULT);
			
			int xPos = (getWidth()/2) - (drawImage.getWidth(null)/2);
			g.drawImage(drawImage, xPos, 2, null);
		}
	}
	
}	


Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: How to add image on JPanel (Java swing)

Posted 14 March 2010 - 06:50 PM

Or add it to a JLabel and add this JLabel to yoiur pane;

pane.add(new JLabel(new ImageIcon("abc.jpg")));
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1