11 Replies - 1686 Views - Last Post: 18 June 2009 - 03:15 PM Rate Topic: -----

#1 bpatzin2  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 80
  • Joined: 09-March 08

Setting Image in Jframe

Posted 16 June 2009 - 12:00 PM

I want the image to appear in the message window when the button is pressed, but right now the applet loads funny and the image appears throughout the entire applet.

Any help is very appreciated.

package briansxnlapplet;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.*;
import java.awt.*;
import java.net.*;

public class image_test extends javax.swing.JApplet {

URL myurl;
Image myimg;

	/** Initializes the applet image_test */
	@Override
	public void init() {
		try {
			java.awt.EventQueue.invokeAndWait(new Runnable() {
				public void run() {
					initComponents();
				}
			});
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jButton1 = new javax.swing.JButton();

		jButton1.setText("Button1");
		jButton1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton1ActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(
			layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
			.addGroup(layout.createSequentialGroup()
				.addGap(160, 160, 160)
				.addComponent(jButton1)
				.addContainerGap(165, Short.MAX_VALUE))
		);
		layout.setVerticalGroup(
			layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
			.addGroup(layout.createSequentialGroup()
				.addGap(96, 96, 96)
				.addComponent(jButton1)
				.addContainerGap(181, Short.MAX_VALUE))
		);
	}// </editor-fold>

	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
			 JFrame frame = new JFrame("Help");
			JOptionPane.showMessageDialog(frame,
			 "The image should appear under this.\n\n" );
			ShowImage();
	}


	// Variables declaration - do not modify
	private javax.swing.JButton jButton1;
	// End of variables declaration

	public void ShowImage() {
myurl=getClass().getResource("SmileyFace.jpg");
myimg=getToolkit().createImage(myurl);
setSize(800,600);
show();
}
	@Override
public void paint(Graphics g) {
g.drawImage(myimg,5,20,this);
}
}




Is This A Good Question/Topic? 0
  • +

Replies To: Setting Image in Jframe

#2 virgul  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 269
  • Joined: 18-March 09

Re: Setting Image in Jframe

Posted 16 June 2009 - 02:01 PM

I believe that it is because you are adding it to the jframe itself
public void paint(Graphics g) {
g.drawImage(myimg,5,20,this);
}



when you say "this" you are adding the image to the jframe, not to the component that you want to add it to.

I cant tell where you want it added though because its hard to read someone else's code, if you could show me what component you want it added to, then i could be of more help

This post has been edited by virgul: 16 June 2009 - 02:02 PM

Was This Post Helpful? 0
  • +
  • -

#3 bpatzin2  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 80
  • Joined: 09-March 08

Re: Setting Image in Jframe

Posted 16 June 2009 - 02:13 PM

I think you should be able to run the code I posted. When you click the button in the middle of the applet a message appears. I want to get the picture on that message.
Was This Post Helpful? 0
  • +
  • -

#4 virgul  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 269
  • Joined: 18-March 09

Re: Setting Image in Jframe

Posted 16 June 2009 - 02:21 PM

I cant quite tell where you want the image to be added, do you want it in the main frame, or do you want it to be in the popup dialog box?
Was This Post Helpful? 0
  • +
  • -

#5 bpatzin2  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 80
  • Joined: 09-March 08

Re: Setting Image in Jframe

Posted 16 June 2009 - 02:26 PM

View Postvirgul, on 16 Jun, 2009 - 01:21 PM, said:

I cant quite tell where you want the image to be added, do you want it in the main frame, or do you want it to be in the popup dialog box?


popup dialog box
Was This Post Helpful? 0
  • +
  • -

#6 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Setting Image in Jframe

Posted 16 June 2009 - 05:25 PM

When you create the image. set the visiblity to False. when they click the button have the image visiblity become true. If you have more then 1 image I suggest an arraylist of images, and everytime a new button is pressed, make a method to cycle through the arraylist to see if they are visible and if they are set them to not visible. unless you need a specific one to show then only show that one.

Hope this helps!

Edit - Learn how to hard code GUI before using a Drag and Drop to make GUI's. Will help in the long run. And most people who hard code it and read NetBeans Generated Code, are quite confused sometimes.

This post has been edited by Fuzzyness: 16 June 2009 - 05:27 PM

Was This Post Helpful? 0
  • +
  • -

#7 bpatzin2  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 80
  • Joined: 09-March 08

Re: Setting Image in Jframe

Posted 16 June 2009 - 08:11 PM

I am able to set the image as the background and then set it visible or not, but I am trying to have the image within the pop-up. Is this possible?

I am trying to make a help button which produces directions on how to use the applet and a screenshot of the applet being used. Is there a different way to do this? I think I have seen Windows Explorer type windows appear when the help button is hit on other applications. Would this be possible?

This post has been edited by bpatzin2: 16 June 2009 - 09:33 PM

Was This Post Helpful? 0
  • +
  • -

#8 bpatzin2  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 80
  • Joined: 09-March 08

Re: Setting Image in Jframe

Posted 17 June 2009 - 12:09 PM

I am able to create a new frame, but I am not sure why the image will not appear in it.

		JFrame newframe = new JFrame("Help");

		ImageIcon icon = new ImageIcon("SmileyFace.jpg");
		JLabel label = new JLabel();
		label.setIcon(icon);
		newframe.getContentPane().add(label);


		newframe.pack();
		newframe.setVisible(true);



Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

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

Reputation: 8065
  • View blog
  • Posts: 31,308
  • Joined: 06-March 08

Re: Setting Image in Jframe

Posted 17 June 2009 - 04:31 PM

View Postbpatzin2, on 17 Jun, 2009 - 11:09 AM, said:

I am able to create a new frame, but I am not sure why the image will not appear in it.

		JFrame newframe = new JFrame("Help");

		ImageIcon icon = new ImageIcon("SmileyFace.jpg");
		JLabel label = new JLabel();
		label.setIcon(icon);
		newframe.getContentPane().add(label);


		newframe.pack();
		newframe.setVisible(true);



it depends of your layout for the JFrame
right now you have a JLabel with the Icon on it ... but how is this label displayed in your JFrame ?
and you realize that your JFrame will not be able to contain another component than this JLabel ... kind of useless
better tou use a JPanel and to hack its repaint method so that it will draw your image in the background

class ImagePanel extends JPanel {
	Image image;
	ImageObserver imageObserver; 
	
	ImagePanel(String filename) {
		ImageIcon icon = new ImageIcon(filename);
		image = icon.getImage();
		imageObserver = icon.getImageObserver();
	}

	public void paint( Graphics g ) {
		g.drawImage(image,  0 , 0 , getWidth() , getHeight() , imageObserver);
		super.paint( g );
	}
}


Was This Post Helpful? 1
  • +
  • -

#11 bpatzin2  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 80
  • Joined: 09-March 08

Re: Setting Image in Jframe

Posted 18 June 2009 - 08:01 AM

I still cannot get the image to appear.
When i check to see if the file exists, it prints yes.

		  JFrame newframe = new JFrame("Help");  
		 ImagePanel newpanel = new ImagePanel();
	   newpanel.setSize(500, 500);
	   newpanel.setVisible(true);
		newframe.getContentPane().add(newpanel);
	   newpanel.setVisible(true);
		newframe.pack();
		newframe.setVisible(true);



public class ImagePanel extends JPanel {
	Image image;
	ImageObserver imageObserver;

	public ImagePanel() {
		   boolean exists = (new File("xnlAppletImage.JPG")).exists();
	if (exists) {
	 System.out.println("yes");
	} else {
	 System.out.println("no");
	}
		ImageIcon icon = new ImageIcon("xnlAppletImage.JPG");
		image = icon.getImage();
		imageObserver = icon.getImageObserver();
	}

	@Override
	public void paint( Graphics g ) {
		g.drawImage(image,  0 , 0 , getWidth() , getHeight() , imageObserver);
		super.paint( g );
	}
}


This post has been edited by bpatzin2: 18 June 2009 - 08:01 AM

Was This Post Helpful? 0
  • +
  • -

#12 bpatzin2  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 80
  • Joined: 09-March 08

Re: Setting Image in Jframe

Posted 18 June 2009 - 08:20 AM

		
		JFrame _frame = new JFrame("Help");
		 JLabel _label = new JLabel();
		 ImageIcon _icon = new ImageIcon("xnlAppletImage.JPG");
		 _label.setIcon(_icon);
		 _frame.getContentPane().add(_label);
		 _frame.setSize(400, 400);
		 _frame.pack();
		 _frame.setVisible(true);



This works. Thanks for your help.
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

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

Reputation: 8065
  • View blog
  • Posts: 31,308
  • Joined: 06-March 08

Re: Setting Image in Jframe

Posted 18 June 2009 - 03:15 PM

View Postbpatzin2, on 18 Jun, 2009 - 07:01 AM, said:

I still cannot get the image to appear.
When i check to see if the file exists, it prints yes.

		  JFrame newframe = new JFrame("Help");  
		 ImagePanel newpanel = new ImagePanel();
	   newpanel.setSize(500, 500);
	   newpanel.setVisible(true);
		newframe.getContentPane().add(newpanel);
	   newpanel.setVisible(true);
		newframe.pack();
		newframe.setVisible(true);



public class ImagePanel extends JPanel {
	Image image;
	ImageObserver imageObserver;

	public ImagePanel() {
		   boolean exists = (new File("xnlAppletImage.JPG")).exists();
	if (exists) {
	 System.out.println("yes");
	} else {
	 System.out.println("no");
	}
		ImageIcon icon = new ImageIcon("xnlAppletImage.JPG");
		image = icon.getImage();
		imageObserver = icon.getImageObserver();
	}

	@Override
	public void paint( Graphics g ) {
		g.drawImage(image,  0 , 0 , getWidth() , getHeight() , imageObserver);
		super.paint( g );
	}
}


oups... in the paint() method invert 2 calls

super.paint( g );
g.drawImage(image, 0 , 0 , getWidth() , getHeight() , imageObserver);



View Postbpatzin2, on 18 Jun, 2009 - 07:20 AM, said:

		
		JFrame _frame = new JFrame("Help");
		 JLabel _label = new JLabel();
		 ImageIcon _icon = new ImageIcon("xnlAppletImage.JPG");
		 _label.setIcon(_icon);
		 _frame.getContentPane().add(_label);
		 _frame.setSize(400, 400);
		 _frame.pack();
		 _frame.setVisible(true);



This works. Thanks for your help.

Yes, but you won't be able to put anything else in your JFrame :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1