Results 1 to 18 of 18
  1. #1
    pulkownik is offline Member
    Join Date
    Mar 2014
    Posts
    10
    Rep Power
    0

    Default Loading image from file and add it to Jpanel

    Hi everyone,

    I'm working on paint application and have small problem. I want to load image from file (here is no problem) and then i want to paint it on my Jpanel. I don't want to display it on new Jpanel because it's not a point. After loaded image i want to draw on it shapes etc.

    Java Code:
    public class DrawPanel extends JPanel
    {
    	private DrawPanelListener dpl;
    	private List<Shape> shapes = new ArrayList<>();
    	private Shape tmpShape;
    	private JLabel picLabel;
    
    	public DrawPanel()
    	{
    		setOpaque(true);
    		setBackground(Color.WHITE);
    
    		dpl = new DrawPanelListener(this);
    		addMouseListener(dpl);
    		addMouseMotionListener(dpl);
    	}
    
    	public void paintComponent(Graphics g)
    	{
    		super.paintComponent(g);
    
    		for (Shape s : shapes)
    		{
    			if (s != null)
    				s.render(g);
    		}
    		if (tmpShape != null)
    			tmpShape.render(g);
    	}
    
    	public void addImage(BufferedImage img)
    	{
    		Graphics2D g = (Graphics2D) img.getGraphics();
    		g.setPaint(new TexturePaint(img, new Rectangle(0, 0, img.getWidth(), img.getHeight())));
    		g.fillRect(0,0,getWidth(),getHeight());
    		g.drawImage(img, 0, 0, null);
    
    		repaint();
    	}
    
     etc rest of codes
    I suppose i should modify my addImage method but i have no idea how. Can someone help me? Thanks !

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Loading image from file and add it to Jpanel

    i want to paint it on my Jpanel.
    Add a call to the Graphics class's drawImage() method to the paintComponent() method.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    pulkownik is offline Member
    Join Date
    Mar 2014
    Posts
    10
    Rep Power
    0

    Default Re: Loading image from file and add it to Jpanel

    Thanks for your response, but im not sure that i understand you.
    to my paintComponent() method i should add g.drawImage(img, 0, 0, null); ? just like that? but then i dont have define img. Could you be more specific? As i'm beginner and not everything is clear for me :) thanks !

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Loading image from file and add it to Jpanel

    You said:
    I want to load image from file (here is no problem)
    which I thought meant that you knew how to load the file.

    If you don't know how to load an image from a file, the ImageIO class has some methods that will do that.
    Read the file into an image object before the paintComponent() method is called and then draw that image in the paintComponent() method.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    pulkownik is offline Member
    Join Date
    Mar 2014
    Posts
    10
    Rep Power
    0

    Default Re: Loading image from file and add it to Jpanel

    Java Code:
    jmOpenFile.addActionListener(new ActionListener()
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    
    				JFileChooser openFile = new JFileChooser();
    				int result = openFile.showOpenDialog(openFile);
    
    				if (result == JFileChooser.APPROVE_OPTION)
    				{
    					File file = openFile.getSelectedFile();
    					String pathName = file.getAbsolutePath();
    					File imageFile = new File(pathName);
    
    					try
    					{
    						BufferedImage image = ImageIO.read(imageFile);
    						Dimension imageSize = new Dimension(image.getWidth(),
    								image.getHeight());
    						panel.setPreferredSize(imageSize);
    						panel.addImage(image);
    						panel.repaint();
    					}
    					catch (IOException e1)
    					{
    						e1.printStackTrace();
    					}
    				}
    			}
    		});
    Here is my method to load image from file, and i displayed loading image using private JLabel picLabel; in my DrawPanel class but it was not effective as i i had two panels my DrawPanel and picLabel.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Loading image from file and add it to Jpanel

    it was not effective
    Please explain.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    pulkownik is offline Member
    Join Date
    Mar 2014
    Posts
    10
    Rep Power
    0

    Default Re: Loading image from file and add it to Jpanel

    My point is when i loaded image to new JLabel i can't for example draw on it for example circle or rectangle, and i have to close the second JLabel as it was not editable. And i want to load image and draw shapes on it like in windows paint.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Loading image from file and add it to Jpanel

    i want to load image and draw shapes on it
    Sorry, without seeing the code I can't say what the problem is.
    Can you make a small complete program that compiles, executes and shows the problem?

    Load an image
    draw it in a paintComponent() method
    add some drawLine or drawOval method calls in that method to draw shapes over the image
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    pulkownik is offline Member
    Join Date
    Mar 2014
    Posts
    10
    Rep Power
    0

    Default Re: Loading image from file and add it to Jpanel

    As you see at my first post i use paintComponent() method to draw shapes, so i cant use paintComponent() method to draw image.
    See my project here:
    Last edited by pulkownik; 05-12-2014 at 08:51 PM.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Loading image from file and add it to Jpanel

    i use paintComponent() method to draw shapes, so i cant use paintComponent() method to draw image.
    Have you tried it? You can call drawImage and after that call drawLine and drawOval etc all in the same paintComponent() method.

    Please make a small complete program that compiles, executes and shows the problem. Don't post the whole project.
    Only enough code to show the problem.

    See the tutorial: http://docs.oracle.com/javase/tutori...ing/index.html
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    pulkownik is offline Member
    Join Date
    Mar 2014
    Posts
    10
    Rep Power
    0

    Default Re: Loading image from file and add it to Jpanel

    So maybe different:

    My drawpanel where i extends Jpanel and display drawing image by user (Rectangle, circle, pencil and line)
    package be.wout.draw; import java.awt.Color; import java.awt.Graphics; impo - Pastebin.com
    DrawPanelListner here i implemented mouselistners to get x,y x2,y2 for drawings shapes
    package be.wout.listeners; import java.awt.Color; import java.awt.Point; im - Pastebin.com
    example class for rectangle, the rest of class is similar but instead of g.drawRect or g.fillRect i use oval, line etc:
    package be.wout.draw.shapes; import java.awt.Color; import java.awt.Graphics - Pastebin.com

    and here my open file listener:
    Java Code:
    		jmOpenFile.addActionListener(new ActionListener()
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    
    				JFileChooser openFile = new JFileChooser();
    				int result = openFile.showOpenDialog(openFile);
    
    				if (result == JFileChooser.APPROVE_OPTION)
    				{
    					File file = openFile.getSelectedFile();
    					String pathName = file.getAbsolutePath();
    					File imageFile = new File(pathName);
    
    					try
    					{
    						BufferedImage image = ImageIO.read(imageFile);
    						Dimension imageSize = new Dimension(image.getWidth(),
    								image.getHeight());
    						panel.setPreferredSize(imageSize);
    						panel.repaint();
    					}
    					catch (IOException e1)
    					{
    						e1.printStackTrace();
    					}
    				}
    			}
    		});
    Here when i loaded image from path file, i want to add it to my DrawPanel. To be specific the background should display image and still i can draw on it shapes etc. If it is not still clear tomorrow i refactor my code and upload mini example.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Loading image from file and add it to Jpanel

    Can you make a small complete program that compiles, executes and shows the problem?
    Hard code the image address, don't require a chooser or any other user input.

    display image and still i can draw on it shapes
    Yes, that can all be done in the one paintComponent() method.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    pulkownik is offline Member
    Join Date
    Mar 2014
    Posts
    10
    Rep Power
    0

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Loading image from file and add it to Jpanel

    Please post the code here on the forum, no links.
    Be sure to wrap the code in code tags.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default Re: Loading image from file and add it to Jpanel

    Instead of drawing the Image in the "addImage()" method, try saving it to a variable and using that in the "paintComponent()" method.

  16. #16
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,383
    Rep Power
    5

    Default Re: Loading image from file and add it to Jpanel

    I don't even see where you try to draw the image on the panel. You never reference an image in your paintComponent method. And BTW,
    clearly your Shape class is not the interface specified in the JDK since the Shape interface has no render method. This is why you need to
    show your classes and the imports. Otherwise, folks won't know what you're trying to do.

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

  17. #17
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default Re: Loading image from file and add it to Jpanel

    Java Code:
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
    
            //  first paint an image on the panel
     
            if (image != null)
                g.drawImage(...);
    
            //  now paint shapes on top of the image
    
            for (Shape s : shapes)
            {
                if (s != null)
                    s.render(g);
            }
            if (tmpShape != null)
                tmpShape.render(g);
        }

  18. #18
    pulkownik is offline Member
    Join Date
    Mar 2014
    Posts
    10
    Rep Power
    0

    Default Re: Loading image from file and add it to Jpanel

    Quote Originally Posted by StormyWaters View Post
    Instead of drawing the Image in the "addImage()" method, try saving it to a variable and using that in the "paintComponent()" method.
    Great advice it was what im looking for ! Now everything works correctly! Big thanks!

    Paint method:

    Java Code:
    public void paintComponent(Graphics g)
    	{
    		super.paintComponent(g);
    
    		if (!(imageFromFile == null))
    		{
    			g.drawImage(imageFromFile, 0, 0, this);
    		}
    		
    		for (Shape s : shapes)
    		{
    			if (s != null)
    				s.render(g);
    		}
    		if (tmpShape != null)
    			tmpShape.render(g);
    	}
    and here it is my open file method:

    Java Code:
    		jmOpenFile.addActionListener(new ActionListener()
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    
    				JFileChooser openFile = new JFileChooser();
    				int result = openFile.showOpenDialog(openFile);
    
    				if (result == JFileChooser.APPROVE_OPTION)
    				{
    					File file = openFile.getSelectedFile();
    					String pathName = file.getAbsolutePath();
    					File imageFile = new File(pathName);
    
    					try
    					{
    						BufferedImage image = ImageIO.read(imageFile);
    						panel.setImageFromFile(image);
    						panel.repaint();
    					}
    					catch (IOException e1)
    					{
    						e1.printStackTrace();
    					}
    				}
    			}
    		});
    Thanks one more time :) It was really difficult for me.
    By the way: Is any way to fit size of the loaded image to the size of the jpanel? Probably i should re-size image in openfile method, any hints which method i should use?
    Edit:
    I get it i should use:

    g.drawImage(imageFromFile, 0, 0, getWidth(), getHeight(), null);

    Thanks everyone, it was very good java and English lesson for me :)
    Last edited by pulkownik; 05-13-2014 at 07:41 PM.

Similar Threads

  1. Loading a image with Image and ImageIcon
    By cel0x in forum AWT / Swing
    Replies: 3
    Last Post: 02-15-2012, 10:38 AM
  2. Replies: 12
    Last Post: 04-14-2011, 01:58 PM
  3. Problem With JInternalFrame loading on JPanel
    By Robb in forum AWT / Swing
    Replies: 0
    Last Post: 12-30-2010, 03:10 AM
  4. Image Loading
    By hobbles in forum New To Java
    Replies: 10
    Last Post: 09-06-2010, 08:30 PM
  5. Problems while loading a JPanel to JApplet...
    By Ananth Chellathurai in forum Java Applets
    Replies: 0
    Last Post: 11-24-2007, 10:47 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
  •