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 4 of 4

Thread: Drawing image on JPanel from another frame

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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:

    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:

    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:

    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:

    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
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Drawing image on JPanel from another frame

    Is the JFrame visible or validated when you call changePicture? If so you should make a call to validate
    (Container#validate())
    You may wish to do this another way by simply changing the image reference in your JPanel and calling repaint

  3. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Drawing image on JPanel from another frame

    Thanks for reply, but I have already solve the problem.

  4. #4
    Junior Member
    Join Date
    Dec 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Drawing image on JPanel from another frame

    Thanks for sharing! It's great!

Similar Threads

  1. how to modify a JList Frame?
    By JM_4ever in forum AWT / Java Swing
    Replies: 0
    Last Post: October 14th, 2009, 11:58 PM
  2. The Frame to be Center Position
    By r12ki in forum AWT / Java Swing
    Replies: 3
    Last Post: October 1st, 2009, 10:36 AM
  3. Replies: 5
    Last Post: September 19th, 2009, 06:48 AM
  4. Drawing "Hello world" on screen
    By shikh_albelad in forum Java SE APIs
    Replies: 4
    Last Post: June 11th, 2009, 03:21 AM
  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