How do I import a jpg image in java? - java

I wanted to import a image in java, and I have no idea how to do it. I need someone to show me how. I wanted to import a jpg image I found on google, I tried and a lot example code online but none of them works for me. The application I use for programming my code is by using eclipse. P.S I use a mac.

Given that you've tagged JPanel in your question, I'm going to assume that you're using swing. You can add an image to a JLabel like so:
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.net.URL;
// inside your class constructor/method
JPanel panel = new JPanel();
ImageIcon img = new ImageIcon(new URL("http://image...."));
JLabel jlPic = new JLabel(img);
panel.add(jlPic);
If your image is on your computer instead of from a URL, you can simply pass a String path to that image to the constructor of ImageIcon instead.

Related

imageio.read in java is not appearing

i spent alot of hours to find the solution, i was making image file that imageIO read it but imageIO.read() method is not appearing, i made the import but read method is not appearing or any method on imageIO there is the code, i made it in a button :
import java.io.File;
import java.awt.Image;
import javax.imageio.ImageIO;
private void imActionPerformed(java.awt.event.ActionEvent evt) {
File f = new File("img.jpg");
Image m = new ImageIO.read(f);
}
its making error at read because the netbeans ide cant read it i dont know why and he cant read any method of ImageIO.
I want to know why he cant read the methods in ImageIO and what is the solution.

Problems with images when compiled

I have been all over the internet trying to work out how to get an image icon displayed after compiling into a runnable jar. I discovered this problem way too late, I ran my program many times before in eclipse and every thing has worked, now 6 months later with project finished, I compiled my program with eclipse and no audio or images work. Reading on the net, it says about the location of images folder should be inside jar, but mine doesnt get put there?
I have played around with the images folder moving it inside the source folder, but it didn't work. I have a feeling that it might be something to do with the path of the resource...ebut thats only guessing.
I have built a simple program that has the same results... works when ran in eclipse, but not when compiled. Could somebody show me an example by modifying my code below. Thanks in advance.
SOURCE CODE:
package ImageIcon;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Gui {
public static JLabel c;
public Gui(){
JFrame f = new JFrame();
JPanel p = new JPanel();
p.setBounds(0, 0, 120, 200);
p.setBackground(Color.black);
p.setLayout(null);
JPanel bg = new JPanel(new BorderLayout());
bg.setBounds(50, 50, 15, 15);
bg.setBackground(Color.white);
ImageIcon a = new ImageIcon("images/success.jpg");
c = new JLabel(a);
f.setSize(100, 200);
f.setLayout(null);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.add(p);
p.add(bg);
bg.add(c);
}
public static void main(String[] args) {
new Gui();
}
}
With your current directory setup, the images dir won't even get built into the jar. Try to extract it and you will most likely see that it's not in there.
You can tell by the fact that it doesn't have the little package logo in the folder, as seen here with resources
The only default directory built into the classpath (/jar) is the src. We need to either put the resources into the src
or configure the build path to include the files that are in the resources. Once we do that, we will see the little package icon inside the folder icon. That's how we know the files are on the build path
CODE we would use:
First Image: Can't, it won't work (this your current predicament)
Second Image:
ImageIcon icon = new ImageIcon(
getClass().getResource("/resources/stackoverflow.png"));
Third Image:
ImageIcon icon = new ImageIcon(
getClass().getResource("/stackoverflow.png"));
To configure the build path to use the third option, follow the instructions in Example 2 in this answer
As from your screenshot, the image exists in a folder called "images". Put it in a folder inside your classpath: src/images/success.jpg and call:
ImageIcon a = new ImageIcon(getClass().getResource("/images/success.jpg"));

getContentPane() not recognized when java.awt is imported

ANSWERED / CLOSED
to make things simple, I've just pulled code directly from the java applet tutorial.
The code is:
private void createGUI() {
JLabel label = new JLabel(
"You are successfully running a Swing applet!");
label.setHorizontalAlignment(JLabel.CENTER);
label.setBorder(BorderFactory.createMatteBorder(1,1,1,1,Color.black));
getContentPane().add(label, BorderLayout.CENTER);
}
The only thing is, getContentPane() isn't recognized even though I have the following imports
import java.applet.*;
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.JFrame;
could someone please tell me what I have to do to get Eclipse Kepler to recognize getContentPane()?
Thanks

how to import icons from other packages in java

Ok so I use use this code to import an icon from the same file that the class is in and assign it a JLabel,
JLabel label1;
Icon iron_ore = new ImageIcon(getClass().getResource("icon_ore"));
label1 = new JLabel(iron_ore);
But what code do I have to add if I want to put the icon in a file different from witch the class is in? Thank you.
If the image is in the package foo.bar.baz, you use
getClass().getResource("/foo/bar/baz/the-icon.png")
as explained in the javadoc, of course.

How do I save firefox web page as image

Please help me in saving web page as image using java.
I am using selenium web driver for an application, I need to take screenshot for an alert box.
So I thought it will be better if we have "save as image" button so that I can take the alert screenshot.
I am using firefox web driver
Newer versions of firefox having new feature of executing commands by pressing SHIFT+F2
It helped me in taking alert screenshots, I used robot object for this but not using web driver
You can simply install Firefox plugin: https://addons.mozilla.org/en-US/firefox/addon/fireshot/ and take any screenshot of web page.
This robot function will help you to take screenshot of displaying screen. You can edit it.
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
public void captureScreen(String fileName)throws Exception {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screenRectangle = newRectangle(screenSize);
Robot robot = newRobot();
BufferedImage image = robot.createScreenCapture(screenRectangle);
ImageIO.write(image,"png",newFile(fileName));
}
The following may help you.
File screenShot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
File f = new File("Location to save your image ");

Categories