Results 1 to 10 of 10
  1. #1
    jeryslo is offline Member
    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

    Default Drawing image on JPanel from another frame

    Hi there!

    I have a problem with programming a task, where user clicks on a button in one frame. And that event triggers that an image is drawn on JPanel, which is located in other frame.

    Here is the code for class where I extend JPanel. This class is used to create panel in main JFrame:

    Java Code:
    class RSlika extends JPanel
    {
    	private Image img;
    	private int x, y;
    	public RSlika(Image img, int x, int y)
    	{
    		this.img = img;
    		this.x = x;
    		this.y = y;
    	}
    	public void paintComponent(Graphics g) 
    	{
    	    super.paintComponent(g);
    	    Dimension d = getSize();
    	    Insets i = getInsets();
    	    g.drawImage(img, i.left + 2, i.top + 2, d.width - i.left - i.right - 4, d.height - i.top - i.bottom - 4, this);
    	}
    }
    In my main class StormAlarm that extends JFrame I create an object for upper class:

    Java Code:
    RSlika leva = new RSlika(img);
    leva.setPreferredSize(new Dimension(700, 0));
    leva.setBorder(zamik1);
    leva.setLayout(new BoxLayout(leva, BoxLayout.X_AXIS));
    		
    addBorder1(barvaDebelina, leva);
    getContentPane().add(leva, BorderLayout.WEST);
    Now I have another class that extends JFrame, this class is named selectingPicture and is used to create another frame, where user than has an option to select button which triggers an event, where image is changed in my first frame. I will post actionPreformed function where the main problem arises:

    Java Code:
    public void actionPerformed(ActionEvent e) 
    	{
    	    Object source = e.getSource();
    	    if(source == izhod)
    	    {
    		this.dispose();
    	    }
    	    else if(source == radarCRO)
    	    {
    		StormAlarm objekt2 = new StormAlarm();
    		objekt2.getImageCRO();
    		objekt2.changePicture();
    	    }
    	}
    }
    Here I create an object from class StormAlarm, then I call function getImage, where I get an image from URL adress. That is all working perfectly, but when I am calling function changePicture, which is defined in StormAlarm:

    Java Code:
    public void changePicture()
    	{
    	    RSlika leva = new RSlika(img);
    	    leva.setPreferredSize(new Dimension(700, 0));
    	    leva.setBorder(zamik1);
    	    leva.setLayout(new BoxLayout(leva, BoxLayout.X_AXIS));
    		
    	    addBorder1(barvaDebelina, leva);
    	    getContentPane().add(leva, BorderLayout.WEST); 
    	}
    Where as you can see I create again an object RSlika, but this time parameter is other picture. So now the new picture should be painted over old one, but guess what, when I click the button nothing happens. Now I have doubts in myself using object StormAlarm objekt2 properly?

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

    Default

    When adding the component to your contentPane are you revalidating the container?

    Another way to do this is to give RSlika a public setImage(Image img) method, and rather than creating a new RSlika object, call the method, swap pictures and then calling repaint on the RSlika object.

  3. #3
    jeryslo is offline Member
    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

    Default

    If I create function setImage, that is not called through na object, than it has to be static, right? I would also like to know what is the diffrence if I create object RSlika in class selectingPicture and than call repaint like that:

    Java Code:
    RSlika object = new RSlika();
    object.reapint();
    Or if I simply call repaint()? Does it go look for paintComponent method in StormAlarm class or you have to create object of that class type, that has paintComponent defined?

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

    Default

    Looks like an update of an old posting from the Sun forums. I guess the OP thinks the people here are smarter and don't need a SSCCE to work from.

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

    Default

    Quote Originally Posted by camickr View Post
    Looks like an update of an old posting from the Sun forums. I guess the OP thinks the people here are smarter and don't need a SSCCE to work from.
    well, maybe not smarter but definitely better looking. Agree that an SSCCE is worth its weight in gold when trying to figure out what is wrong.

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

    Default

    Quote Originally Posted by jeryslo View Post
    If I create function setImage, that is not called through na object, than it has to be static, right?
    Emphatically no. You would make an instance method and call this on the instance.
    I would also like to know what is the diffrence if I create object RSlika in class selectingPicture and than call repaint like that:
    Java Code:
    RSlika object = new RSlika();
    object.reapint();
    Not sure what you're doing, but it looks like you'd be calling repaint on the wrong RSlika object. Instead, you would need to call repaint on the RSlika object that is displayed.

    Or if I simply call repaint()? Does it go look for paintComponent method in StormAlarm class or you have to create object of that class type, that has paintComponent defined?
    huh? Not sure what you mean by this but it sounds as if you'd get a quicker answer by trying it yourself.

    For all questions, as camickr suggests and as I agree, an SSCCE is worth a thousand words.

  7. #7
    jeryslo is offline Member
    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

    Default

    Thanks for answearing my questions, as you could see I am very confused how to solve this problem.

    Quote Originally Posted by camickr View Post
    Looks like an update of an old posting from the Sun forums. I guess the OP thinks the people here are smarter and don't need a SSCCE to work from.
    Yes it is a repost because only one member on that forum helped me, other post were not so helpful. That is why I decided to try other forums. And yes I did read about SSCCE, especially this:

    I am not proposing that every single problem needs a SSCCE in order to be solved. I am also not suggesting an example is, or should be, compulsory.
    Fubarable, If I past the whole code here, would you please help me on this? I think that SSCCE would not be any shorter and also the code isn't to long.

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

    Default

    Quote Originally Posted by jeryslo View Post
    Fubarable, If I past the whole code here, would you please help me on this?
    If you posted the whole code, I'd be forced to create an SSCCE myself to isolate the problem, and that would be putting the brunt of the work on me. This is not a good situation, since the one who is asking the question should be responsible for doing the brunt of the work, should be the one making it as easy as possible for others to answer them.

    I think that SSCCE would not be any shorter and also the code isn't to long.
    A good SSCCE would likely be shorter.

  9. #9
    jeryslo is offline Member
    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

    Default

    Finally solved this issue. When I created new frame changePicture I "supplied" it with the panel that I was painting the picture on. But the stupidest mistake I made was, that I completly forgot, that when you are creating new instance of a class, you are storing for example img in your new object, so the old object that I created still had old image stored. For supplying RSlika with img I did as you said Fubarable. Created function setImage(Image img) and then called that function through the object type RSlika, that was created in main class. Then just called repaint() and now it works.

    Thanks again Fubarable for helping me out

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

Similar Threads

  1. Drawing Graph and Add to JPanel
    By halo3 in forum NetBeans
    Replies: 17
    Last Post: 09-22-2009, 01:31 PM
  2. drawing in JPanel ,beyond the co-ordinates
    By anandnarayan891 in forum AWT / Swing
    Replies: 4
    Last Post: 04-05-2009, 03:42 AM
  3. drawing to a JPanel
    By diggitydoggz in forum New To Java
    Replies: 11
    Last Post: 03-09-2009, 07:42 AM
  4. Drawing points on a JPanel
    By josephdcoleman in forum New To Java
    Replies: 6
    Last Post: 02-25-2009, 03:47 PM
  5. X&Y Coordinate Drawing on jPanel
    By BHCluster in forum Java 2D
    Replies: 2
    Last Post: 03-27-2008, 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
  •