package common;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class ResourcesToAccess {
public static Icon sharedAbstractDownIcon;
public static Icon sharedAbstractPlayIcon;
public static Icon sharedAbstractPauseIcon;
public static Icon sharedAbstractBlackCursor;
public static Icon sharedAbstractWhiteCursor;
public ResourcesToAccess(){
InputStream is = this.getClass().getClassLoader().getResourceAsStream("/src/images/blackCursor.png");
try{
BufferedImage bi = ImageIO.read(is);
sharedAbstractBlackCursor = (Icon) new ImageIcon(bi);
new JFrame().add(new JLabel(sharedAbstractBlackCursor)).setVisible(true);
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
new ResourcesToAccess();
}
}
I am using this code to see whether the PNG images can be properly loaded to create JLabels, Icons, etc but I get the error that:
java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at common.ResourcesToAccess.<init>(ResourcesToAccess.java:21)
at common.ResourcesToAccess.main(ResourcesToAccess.java:29)
Why do I see that error message?
try with
this.getClass().getClassLoader().getResourceAsStream("images/blackCursor.png")
it is looking in your classpath so no more src directory there
Related
hope you're doing well.
Actually, i'm facing a problem when trying to rotate an image using Core.rotate Class of OpenCV.
the colors of the image pixels keep changing every time i click the rotation Button.
Can anyone please help me fixing this problem ?
Thanks in advance.
I've tried to change the colors of the image using Imgproc.cvtColor but it didn't work.
This is the code :
`
package application;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.imgcodecs.Imgcodecs;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
public class SampleController {
#FXML
private ImageView myimageview;
#FXML
private Button showimageButton, rotateButton;
#FXML
void rotateButtonClicked(ActionEvent event) throws Exception {
BufferedImage image = SwingFXUtils.fromFXImage(myimageview.getImage(), null);
Mat src = BufferedImage2Mat(image);
Core.rotate(src, src, Core.ROTATE_90_CLOCKWISE);
image = Mat2BufferedImage(src);
try {
Image converted_image = SwingFXUtils.toFXImage(image, null);
myimageview.setImage(converted_image);
}
catch (Exception e) {
System.out.println(e);
}
}
public Mat BufferedImage2Mat(BufferedImage image) throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", byteArrayOutputStream);
byteArrayOutputStream.flush();
return Imgcodecs.imdecode(new MatOfByte(byteArrayOutputStream.toByteArray()), Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);
}
static BufferedImage Mat2BufferedImage(Mat matrix)throws Exception {
MatOfByte mob=new MatOfByte();
Imgcodecs.imencode(".jpg", matrix, mob);
byte ba[]=mob.toArray();
BufferedImage bi=ImageIO.read(new ByteArrayInputStream(ba));
return bi;
}
#FXML
void showimageButtonClicked(ActionEvent event) {
Image im = new Image("file:///C:\\Users\\poste\\eclipse-workspace\\dragImage\\Images\\temp.jpg");
myimageview.setImage(im);
}
}
`
I am trying to get the view count of a youtube video in Jsoup. I started by getting the title which worked well but am having trouble getting the view count which is in a span class.
Here is my code so far:
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import java.io.IOException;
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class HigherOrLowerYoutube {
public static void main(String[] args) {
try {
Document doc =
Jsoup.connect("https://www.youtube.com/watchv=omlFsLz2WKM").get();
println(doc.title());
} catch (IOException e) {
e.printStackTrace();
}
}
private static void println(String string) {
System.out.println(string);
}
}
Try:
System.out.println(doc.select(".watch-view-count").first().text());
I am building a simple program with one button. I want to play the "zvuk.wav" file after I click on the button. It's not working though and I cant solve why. When I click the button, nothing happens. The zvuk.wav file is in the src file with the classes.
Here is my first class which imports java.applet:
package Music;
import java.net.MalformedURLException;
import java.net.URL;
import java.applet.*;
public class Music {
private URL soubor;
public Music(String cesta){
try {
soubor = new URL("file:"+cesta);
} catch (MalformedURLException vyjimka) {
System.err.println(vyjimka);
}
Applet.newAudioClip(soubor).play();
}
}
MainFram which extends JFrame and has one Button:
package Music;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class MainFrame extends JFrame{
public static final int WIDTH = 480;
public static final int HEIGHT = 600;
private String file;
public MainFrame(){
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setTitle("Přehrávač");
setResizable(false);
JPanel jPanel = new JPanel();
JButton bPlay = new JButton("PLAY");
jPanel.setLayout(null);
add(jPanel);
jPanel.add(bPlay);
bPlay.setBounds(200, 250, 100, 50);
bPlay.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
Music music = new Music("zvuk.wav");
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new MainFrame();
}
});
}
}
Please note that Applet.newAudioClip(url).play() does not throw an error if it fails for whatever reason (for example nothing will happen if the project cannot find the wav file).
Try this stand alone test app. Does it work?
import java.applet.Applet;
import java.applet.AudioClip;
import java.net.URL;
public class MainClass {
public static void main(String[] args) {
try {
URL url = new URL("file:zvuk.wav" );
AudioClip ac = Applet.newAudioClip(url);
ac.play();
System.out.println("Press any key to exit.");
System.in.read();
ac.stop();
} catch (Exception e) {
System.out.println(e);
}
}
}
If this small sample works, then it should be a small matter to modify it for your purposes.
However if it doesn't work then we almost certainly know that you project is unable to find the wav file.
Try add this to the code above:
//existing line
URL url = new URL("file:zvuk.wav" );
//new lines to debug wav file location
File myMusicFile = new File(url.getPath());
if(myMusicFile.exists() && !myMusicFile.isDirectory()) {
System.out.println("File exists and is not a directory");
}
If the file does not exist then that's your problem, and you need to point your URL to the correct location.
However if the file does exist and it still doesn't work then we have another possible issue outside of code.
It is possible that .play() is completing too quickly, see below for an example of how to keep it alive.
It is possible that your wav file is not a type that can be played, or it requires an unsupported codec. This is a far bigger topic and needs a new question, and a little bit of research on your part.
Here is the example to keep it alive from the sample code:
//load and start audio
AudioClip ac = Applet.newAudioClip(url);
ac.play();
System.out.println("Press any key to exit.");
//keep thread alive until a key is pressed
System.in.read();
ac.stop();
Sources:
http://www.java2s.com/Code/JavaAPI/java.applet/AppletnewAudioClipURLaudioFileURL.htm
http://docs.oracle.com/javase/7/docs/api/java/applet/AudioClip.html#play%28%29
I do this using NetBeans. This is the code.
Music.java file
package sound.play;
import java.applet.Applet;
import java.net.MalformedURLException;
import java.net.URL;
public class Music {
private URL soubor;
public Music(String cesta) {
try {
soubor = new URL("file:" + cesta);
} catch (MalformedURLException vyjimka) {
System.err.println(vyjimka);
}
Applet.newAudioClip(soubor).play();
}
}
MainFram which extends JFrame and has one Button
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JPanel;
public class MainFrame extends javax.swing.JFrame {
public static final int WIDTH = 200;
public static final int HEIGHT = 200;
private String file;
public MainFrame() {
initComponents();
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setTitle("Přehrávač");
setResizable(false);
JPanel jPanel = new JPanel();
jPanel.setLayout(null);
add(jPanel);
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Music music = new Music("zvuk.wav");
String filename = "zvuk.wav";
URL url = this.getClass().getResource(filename);
File myMusicFile = new File(url.getPath());
AudioClip ac = Applet.newAudioClip(url);
ac.play();
System.out.println("Press any key to exit.");
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainFrame().setVisible(true);
}
});
}
I've written a program that has to open an image I've placed on the desktop. The problem is that the program runs but nothing happens in the console nor does it open the image. Do I've to implement a GUI to fix the problem or...?
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Image {
public JavaImage() {
try {
BufferedImage image = ImageIO.read(new File("C:/Users/Username/Desktop/index.jpg"));
}
catch (IOException e) {
System.out.println("There isn't a picture in the folder");
}
}
public static void main(String[] args) {
new Image();
}
}
I want to display information about the musical composition in the browser via Java applet. I use the library beaglebuddy_mp3.jar for id3 tags. A folder with files looks like this:
applet
- index.html
- FirstApplet.class
- beaglebuddy_mp3.jar
In index.html I connect an applet:
<applet code="FirstApplet.class" archive="beaglebuddy_mp3.jar" width="500" height="500"></applet>
FirstApplet.class contains the following code:
import java.applet.Applet;
import java.awt.Graphics;
import java.io.File;
import java.io.IOException;
import com.beaglebuddy.mp3.MP3;
public class FirstApplet extends Applet{
public void paint(Graphics g){
try {
MP3 mp3 = new MP3("D:\\Music\\abc.mp3");
g.drawString(mp3.getBand() +" "+mp3.getTitle(), 20, 20);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
After starting the index.html file dialog box appears with a warning stating that I run the application at your own risk. Then I click "Run", instantly appears and disappears gray square. On that nothing is displayed.
Try the following:
import javax.swing.JApplet;
import javax.swing.SwingUtilities;
import javax.swing.JLabel;
import java.io.File;
import java.io.IOException;
import com.beaglebuddy.mp3.MP3;
public class FirstApplet extends JApplet {
public void init() {
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
MP3 mp3 = new MP3("D:\\Music\\abc.mp3");
JLabel label = new JLabel(mp3.getBand() +" "+mp3.getTitle());
add(label);
}
});
} catch (Exception e) {
System.err.println("createGUI didn't complete successfully");
}
}
}
And secondly you have to sign your applet code with an official certificate to be able to run it in your web browser.