javax media player-wont play - java

Trying to embed a .mp4 video in a JPanel. JPanel will load and execute but video does not. The try{} throws an error but not sure why the try{} is throwing the error. Copied this code from another source but it will not compile.
import java.awt.BorderLayout;
import java.awt.Component;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Player;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JPanel
{
public static void main(String[] args)
{
URL mediaUrl=null;
File file = new File("Mortal Combat Video.mp4");
System.out.println(file);
try
{
mediaUrl = file.toURL();
}
catch (MalformedURLException ex)
{
System.out.println(ex);
}
JFrame mediaTest = new JFrame( "Movie Player" );
mediaTest.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
MediaPlayer mediaPanel = new MediaPlayer( mediaUrl );
System.out.println(mediaPanel);
mediaTest.add( mediaPanel );
mediaTest.setSize( 800, 700 ); // set the size of the player
mediaTest.setLocationRelativeTo(null);
mediaTest.setVisible( true );
}
}
import java.awt.BorderLayout;
import java.awt.Component;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Player;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MediaPlayer extends JPanel {
public MediaPlayer(URL mediauUrl) {
setLayout(new BorderLayout());
Player mediaPlayer = null;
Component video = null;
Component control = null;
try
{
mediaPlayer=Manager.createRealizedPlayer(new MediaLocator(mediauUrl));
video=mediaPlayer.getVisualComponent();
control=mediaPlayer.getControlPanelComponent();
System.out.println("here3");
if (video == null)
System.out.println("video");
else
{
System.out.println("video2");
add(video, BorderLayout.CENTER); // place the video component in the panel
}
add(control, BorderLayout.SOUTH); // place the control in panel
mediaPlayer.start();
}
catch (Exception e)
{
System.out.println("error");
}
}
}
The catch{} prints "error" but not sure why the try{} code will not compile. Any advice is appreciated.
Its running now, video plays, controls work but the video itself is not visible (audio plays).
Here's screenshot:

Related

no icon shows on java swing in linux ubunutu

I copied the path of the image but no icon appears click on the image to see
no icon
I created simple frame and needed to include icon
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.awt.Color;
public class App {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("JFrame title goes here");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //exit out of app
frame.setResizable(false); // prevent frame from being resized
frame.setSize(700,700);
frame.setVisible(true);
try {
URL resource = frame.getClass().getResource("/games.png");
BufferedImage image = ImageIO.read(resource);
frame.setIconImage(image);
} catch (IOException e) {
e.printStackTrace();
}
frame.getContentPane().setBackground(new Color(123,50,250));
}
}

Load Image into Java with BufferedImage - Oracle tutorial

I'm a beginner in Java and I would like to load an image with this script:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
/**
* This class demonstrates how to load an Image from an external file
*/
public class LoadImageApp extends Component {
BufferedImage img;
public void paint(Graphics g) {
g.drawImage(img, 0, 0, null);
}
public LoadImageApp() {
try {
img = ImageIO.read(getClass().getResource("/resources/java.png"));//cannot found image
} catch (IOException e) {
}
}
public Dimension getPreferredSize() {
if (img == null) {
return new Dimension(100,100);
} else {
return new Dimension(img.getWidth(null), img.getHeight(null));
}
}
public static void main(String[] args) {
JFrame f = new JFrame("Load Image Sample");
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.add(new LoadImageApp());
f.pack();
f.setVisible(true);
}
}
Then I put a picture on a folder resource "resources", change the name of the location of the picture like "/resources/java.png" and when I compile, there is an empty window without image.
You can see error here : https://ibb.co/ysjNyQw
The first thing you're going to want to do is do some research into "embedded resources", for example
I don't use Eclipse, I use Netbeans, but the process should be the same
As you can see, I've placed my image in the resources package within the projects "sources". This will ensure that it's available at runtime via the class path search mechanism (and embedded within the resulting Jar file when I export it).
I then used a JLabel to display it...
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
BufferedImage image = ImageIO.read(getClass().getResource("/resources/java.png"));
JLabel label = new JLabel(new ImageIcon(image));
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(label);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (IOException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
}
Now, if you want to continue using a custom painting route, I suggest having a read of:
Performing Custom Painting
Painting in AWT and Swing
to get a better understanding of how painting works in Swing and how you should work with it

Url loading error for image in runnable jar

So, when I try the following code:
//Main Class
package com.mgflow58.Main;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class Game {
public static void main(String[] args) {
BufferedImage mainicon = null;
JFrame window = new JFrame("Guppy's Adventure");
window.add(new GamePanel());
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(false);
window.pack();
window.setLocationRelativeTo(null);
window.setVisible(true);
try {
String mainurlstring = "icon.gif";
URL mainurl = new URL(mainurlstring);
try {
mainicon = ImageIO.read(mainurl);
window.setIconImage(new ImageIcon(mainicon).getImage());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I get the following error:
java.net.MalformedURLException: no protocol: icon.gif
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at com.mgflow58.Main.Game.main(Game.java:28)
The file icon.gif is in the root folder, and I even tried copying it into src/com/mgflow58/Main where the class executing the code is in. What am I doing wrong? I simply can not set an image icon no matter which methods I try and I have been trying to find an answer for a while already.
I have simplified the answer to something from a previous question. I have done a stupid mistake somewhere, but now I wrote a line anew and the problem was fixed. This is how I accessed icon.gif:
//Main Class
package com.mgflow58.Main;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class Game {
public static void main(String[] args) {
BufferedImage mainicon = null;
JFrame window = new JFrame("Guppy's Adventure");
window.add(new GamePanel());
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(false);
window.pack();
window.setLocationRelativeTo(null);
window.setVisible(true);
window.setIconImage(new ImageIcon(Game.class.getResource("/icon.gif")).getImage());
}
}

Java swing: image not loading

I'm trying to load an image but my code continuously enters the catch statement. I followed the example provided on: http://docs.oracle.com/javase/tutorial/2d/images/examples/LoadImageApp.java.
I really don't understand what the programming error is here.
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.accessibility.Accessible;
import javax.imageio.ImageIO;
import javax.swing.*;
public class RackBuilder extends javax.swing.JFrame {
BufferedImage img;
/**
* Creates new form RackBuilder
*/
public RackBuilder() {
initComponents();
JPanel rackGrid = new JPanel(new GridLayout(42,0));
try {
img = ImageIO.read(new File ("alexi.jpg"));
} catch (IOException e){
System.out.println("Image could not be read.");
System.exit(1);
}
add(rackGrid);
}
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new RackBuilder().setVisible(true);
}
});
}
Thanks!

"Selection does not contain a main Type" - Eclipse Run Error

I made a sample code to start one project just refactoring another one.
This the refactored one:
package com.sh.st;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingConstants;
public class Main extends JFrame implements ActionListener {
/**
*
*/
JMenuBar bar;
JMenu file, register;
JMenuItem close, search;
ImageIcon figure1= new ImageIcon("C:/Users/Victor/Downloads/Untitled.jpg");
//ImageIcon figure2= new ImageIcon("C:/Victor Rocha/carroicon.jpg");
JLabel Ibimagem1,Ibimagem2;
/**
*
*/
public Main()
{
bar= new JMenuBar();
file= new JMenu("file");
register= new JMenu("register");
register.setMnemonic(KeyEvent.VK_R);
file.setMnemonic(KeyEvent.VK_F);
close= new JMenuItem("Close");
close.addActionListener(this);
search= new JMenuItem("Search");
search.addActionListener(this);
Ibimagem1= new JLabel(figure1, JLabel.CENTER);
Ibimagem1.setVerticalTextPosition(SwingConstants.CENTER);
bar.add(file);
bar.add(register);
file.add(close);
register.add(search);
//register.add(carro);
//register.add(cliente);
//register.add(funcionario);
getContentPane().add(Ibimagem1);
setSize(800,600);
setTitle("SHST");
setJMenuBar(bar);
setDefaultCloseOperation(0);
//setIconImage(figure2.getImage());
WindowListener J=new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
};
addWindowListener(J);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==close){
System.exit(0);
}
if(e.getSource()==search){
Search s= new Search();
s.setVisible(true);
}
}
}
This is the original one:
package com.professordelphi.locadora;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingConstants;
public class Principal extends JFrame implements ActionListener {
JMenuBar barra;
JMenu arquivo, cadastro;
JMenuItem fechar, cliente, funcionario, carro;
ImageIcon figura1= new ImageIcon("C:/Victor Rocha/carro.jpg");
ImageIcon figura2= new ImageIcon("C:/Victor Rocha/carroicon.jpg");
JLabel Ibimagem1,Ibimagem2;
public Principal()
{
barra= new JMenuBar();
arquivo= new JMenu("Arquivo");
cadastro= new JMenu("Cadastro");
cadastro.setMnemonic(KeyEvent.VK_C);
arquivo.setMnemonic(KeyEvent.VK_A);
fechar= new JMenuItem("Fechar");
fechar.addActionListener(this);
carro= new JMenuItem("Carro");
carro.addActionListener(this);
cliente= new JMenuItem("Cliente");
cliente.addActionListener(this);
funcionario= new JMenuItem("Funcionario");
funcionario.addActionListener(this);
Ibimagem1= new JLabel(figura1, JLabel.CENTER);
Ibimagem1.setVerticalTextPosition(SwingConstants.CENTER);
barra.add(arquivo);
barra.add(cadastro);
arquivo.add(fechar);
cadastro.add(carro);
cadastro.add(cliente);
cadastro.add(funcionario);
getContentPane().add(Ibimagem1);
setSize(800,600);
setTitle("Sistema de Cadastro");
setJMenuBar(barra);
setDefaultCloseOperation(0);
setIconImage(figura2.getImage());
WindowListener J=new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
};
addWindowListener(J);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==fechar){
System.exit(0);
}
if(e.getSource()==carro){
Carro k = new Carro();
k.setVisible(true);
}
if(e.getSource()==cliente){
Cliente c = new Cliente();
c.setVisible(true);
}
if(e.getSource()==funcionario){
Funcionario f= new Funcionario();
f.setVisible(true);
}
}
}
The thing is, the original e building and the refactored is not. The error I receive from the refactored is that "Selection does not contain a main type". I saw a lot of posts regarding this theme but none of them solve my problem. Here is one little list of the things I've tried;
Source: Editor does not contain a main type
clean your workspace and rebuild your Project.
make sure you add your source folder in the project properties -> java build path -> source.
close your project and reopen it.
Trying to run as a Java Application with Eclipse, anyone has suggestions of what should I do?
You dont have a main function defined in the class. The main function is the function that will be called when you run the file.
Try adding
public static void main(String [] args)
{
}
and create and show an object of your JFrame in the main method.
Right click on your project ->Properties -> Java Build Path -> Source -> Add Folder
Now Select the src folder and click on OK
You should define a main method in your class (either one) with the following signature:
public static void main(String args[])
This method is the start point of program.
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingConstants;
public class Main extends JFrame implements ActionListener {
public static void main(String args[]){
// from here the program execution starts
}
....
your other code
.....
}
The entry point for Java programs is the main method.
Does your class contain main method like below ?
public static void main(String[] args) {
//Code
}
If you do not have this, your program will not run.

Categories