imageio.read in java is not appearing - java

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.

Related

java JFileChooser how to temporarily disable file selection window

Is there a way how to temporarily disable file chooser in the JFileChooser window?
I created custom preview JPanel and I need the next file selection to be made only after the file preview is done/created (it created image from Wavefront OBJ files when such obj file is selected in the file chooser window).
Maybe some sort of disabling the whole file selection window part?
EDIT:
I was searching around here a bit more and found post Start a JFileChooser with files ordered by date. Now if I understood it right, then according to it, this piece of code should in fact enabling access to FilePane inside the JFileCHooser (of course, I downloaded the SwingUtils.java class first):
JTable table = SwingUtils.getDescendantsOfType(JTable.class, fileChooser).get(0);
But when I do that, I got error in NetBeansIDE saying: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
Does anyone know why?
I also found post How to disable file input field in JFileChooser?, according to which this piece of code accessing the JFileChooser's textfield showing the selected file name:
import java.awt.Frame;
import java.lang.reflect.Field;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.plaf.metal.MetalFileChooserUI;
public class FileChooser {
public static void main(String[] args) throws Exception{
Frame f = new JFrame();
JFileChooser jFileChooser = new JFileChooser();
MetalFileChooserUI ui = (MetalFileChooserUI)jFileChooser.getUI();
Field field = MetalFileChooserUI.class.getDeclaredField("fileNameTextField");
field.setAccessible(true);
JTextField tf = (JTextField) field.get(ui);
tf.setEditable(false);
tf.setEnabled(false);
jFileChooser.showDialog(f, "Select");
f.dispose();
}
}
So, by inspecting all available fields I found out there is one called "filePane". So I took a risk and try to imitate the above code just with a few changes so that the FilePane would be targeted instead like this:
Field fieldB = MetalFileChooserUI.class.getDeclaredField("filePane");
fieldB.setAccessible(true);
FilePane filePane = (FilePane) fieldB.get(ui);
filePane.setEnabled(false);
I thought the code above would disable the file selection part of the JFileChooser window, but it did absolutely nothing, unfortunately.
So after a while I've solved it myself by playing further with the above code ending up with this functioning piece below:
FilePane filePane = SwingUtils.getDescendantsOfType(FilePane.class, fileChooser).get(0);
filePane.setEnabled(false);//<- this line doesn't work, but I'll leave it here so others know
filePane.setVisible(false);

How do I import a jpg image in 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.

How am I supposed to draw an image from my Java Applet?

I am not sure how to fully express this but, I have probably gone through 10 pages of Google links on this topic and not one of them has helped me solve my issue.
I thought it would be simple enough, I was just trying to add an image to the paint function of my java applet, like any other shape, but this has turned out to be a nightmare for me.
The problem is that every time I try to run the drawImage function it keeps saying Access Denied ("java.io.FilePermission" "Image.jpg" "read"). Yet, none of the tutorials mention this at all, all they ever say is that I should do the following:
import java.applet.*;
import java.awt.*;
Image img;
//These would go in the paint function
img=getImage(getDocumentBase(),"/Image.jpg"); //I have tried without the slash too
g.drawImage(img,20,20,this);
This is all they do and it works for them, but it just won't work for me. Other methods are far too complex for the sake of just adding an image, and even when I go through the toil of doing those it keeps giving me the "Access Denied" message. There's also the method of "signing" it, but I really don't think that's going to help given all that I have tried, so I am afraid it might just be another wasted endeavor. None of the tutorials even tell you to have your applet signed.
I have the image in the "build" (also called bin) folder together with the classes.
The program seemed to run when I included the entire file path, but even then the image did not display. That is not to mention I can't really include the complete path from my own computer because then it wouldn't work when I actually send it to another person.
Please, I just want to know why it doesn't work for me yet seems to work perfectly for others. That, and if there's a way around this.
This is an example of what I am doing:
import java.applet.*;
import java.awt.*;
public class JavaProject extends JApplet
{
Image img;
public void init()
{
img=getImage(getDocumentBase(),"/Image.jpg");
}
public void paint(Graphics g)
{
super.paint(g);
g.drawImage(img,20,20,this);
}
}
This is my HTML file:
<html>
<head>
<title> My First Web Page </title>
</head>
<body>
<applet code="JavaProject.class" width="400" height="500">
</applet>
</body>
</html>
According JApplet java docs method getImage(URL url, String name) should have two parameter: URL-link to picture and String name.
Is method getDocumentBase() returnig an URL-link?
You must start by understanding that an Applet, unless signed, may not read from the file system. It must use either classpath resources or things fetched from the same place it was fetched from. You have to decide which of these applies to you. If the image is a fixed image, you can put it in your classpath as a resource, and use Class.getResourceAsStream. If it's a different image every time, you'll have to use HTTP.
Try this one if the image in the "build" (also called bin) folder together with the classes.
import java.awt.Graphics;
import java.awt.Image;
import java.net.URL;
import javax.swing.JApplet;
public class JavaProject extends JApplet {
Image img;
public void init() {
img = getImage(getDocumentBase(), "images/222.png");
// Please ensure that 222.png is placed under bin/images folder directly
}
#Override
public void paint(Graphics g) {
update(g);
}
#Override
public void update(Graphics g) {
g.drawImage(img, 20, 20, this);
}
}
Try with HTTP URL first
URL myURL = new URL("https://www.gravatar.com/avatar/a94613cea642e6e9e2105867bc2e103e?s=32&d=identicon&r=PG&f=1");
img = getImage(myURL);
If you are using Eclipse under Windows then have a look at below screenshot:
Please have a look at below post to get some understanding about it.
java.io.FilePermission exception - Read from jar file?

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 ");

How can i play mp4 video file in a JFrame?

I want to play a video (mp4) inside a fixed size JFrame. How should I do this? Is there any tutorial or documentation for this?
I guess you could convert your video into a .gif file and then get the audio from that video and play it in the background, it may lower the quality but for simple things it should work.
`import java.io.File;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Main {
public static void audio() {
try {
File file = new File("audio.wav");
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(file));
clip.loop(Clip.LOOP_CONTINUOUSLY);
clip.start();
} catch (Exception e) {
System.err.println("Put the music.wav file in the sound folder if you want to play background music, only optional!");
}
}
private static String arg;
public static void main(String[] args){
arg = "background.gif";
JFrame f = new JFrame();
JPanel p = new JPanel();
JLabel l = new JLabel();
ImageIcon icon = new ImageIcon(arg);
f.setSize(480, 360);
f.setVisible(true);
l.setIcon(icon);
p.add(l);
f.getContentPane().add(p);
f.setLocationRelativeTo(null);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
audio();
}
}
https://www.mediafire.com/?18n4iluk9hwitlb Audio.wav
http://www.mediafire.com/view/jx7vogxzsbor0j4/background.gif Background.gif
I think you have use a special framework to do that. You could for example use the Java Media Framework You can download it at the oracle homepage.
The "fixed size(1) JFrame" is easy, or at least it should be. Are you having a specific problem with it? (I ask because it seems odd you should mention it.)
As to support for MP4, the JMF will not handle it. Your best bet is to see what Google throws up for java+mp4. After looking through a few of the top ranked hits, it seems the offerings are not great.
Is MP4 an unchangeable requirement? The JMF can handle many other simpler (older) formats well.
If opening the MP4 in the standard media player is not out of the question - that is as easy as:
Desktop.getDesktop().open(new File("the.mp4"));
(1) And as an end user, have to comment that I detest someone delivering video content to my desktop that cannot be resized. It's my (dang) desktop - I should be able to choose how much of it the video covers!
The JavaFX widget MediaView is able to render an MP4 file. You can embed JavaFX in Swing like this http://docs.oracle.com/javafx/8/embed_swing/jfxpub-embed_swing.htm and then you can use the MediaView like this http://docs.oracle.com/javafx/2/media/simpleplayer.htm
All you really need is a library called vlcj to play any kind of videos on JFrame, since JavaFX's MediaPlayer doesn't support .avi files.
Link to vlcj: https://github.com/caprica/vlcj
(There is also implementation for JavaFX: https://github.com/caprica/vlcj-javafx)

Categories