Java swing: image not loading - java

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!

Related

javax media player-wont play

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:

JFrame doesn't show

I have the next problem: I'm calling a class from my Main, that have to show a JFrame. I can't even continue with my program because when I try to run it, the JFrame doesn't show.
I'm using Eclipse.
Main:
package System;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.awt.Menu;
import java.awt.*;
import javax.*;
import javax.swing.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Main {
public static void main(String[] args) {
new Menu();
}
}
Second class:
package System;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.awt.*;
import javax.*;
import javax.swing.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Menu {
private JFrame ventana = new JFrame("Sistema de Productos QuĂ­micos");
private JButton sup = new JButton("Supervisor");
private JButton oper = new JButton("Operario");
Menu()
{
ventana.setSize(500,500);
ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ventana.add(sup);
ventana.add(oper);
ventana.setVisible(true);
}
}
Thanks!
because your jframe class name is Menu and you have import
import java.awt.Menu;
this create a new awt menu instead of your Menu class which create a jframe
new Menu();
to fix this change the name of the Menu class to something different.
for instance
public class MyMenu { //
If you play around with System.out.println("Say something random"), which is something to do when your program is not working and it is not syntax error, you find out that the Main class is not even calling the Menu class, so that "cuts down" your area of search on what's srong, I have not found the problem, and will update the answer when I do.

Trying to Place Order- nothing shows up on screen

Trying to Place Order nothing shows up on screen but I want to show a textarea saying:
Here's Sandwhich with the topping u provided
Can you help me?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
public void setCheese(String s) {
cheese = s;
}
public String getCheese() {
return cheese;
}}
You should be getting a NullPointerException makeASandwih as sandwich is null, then you try and call an instance method
protected void makeASandwich() {
Sandwich sandwich = null;
sandwich.setCheese(jcbcheese.getItemAt(jcbcheese.getSelectedIndex()));
Try creating a new instance of Sandwich
protected void makeASandwich() {
Sandwich sandwich = new Sandwich();
sandwich.setCheese(jcbcheese.getItemAt(jcbcheese.getSelectedIndex()));

Java Swing + Timer? Moving a line across JPanel

What I'm trying to do is simple. I have a JLayeredPane with two panels inside of it. Panel 2 (higher Z index) has a transparent background and only displays a Line2D element that goes from Y = 0 to Y = max. I need the X value to increment every so many milliseconds, and then redraw the line. I have everything set up to do so, except I can't figure out how to do the bar movement via timing.
I've done some research and every time I saw mentions of the timer class (Which I feel would be able to accomplish what I'm trying to do) people recommend not using it. I can't figure out an alternative to using the timer class in order to slide my bar across the screen.
Hope this code helps you. It does exactly what you want.
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
import java.util.Arrays;
import java.awt.EventQueue;
import javax.swing.JFrame;
public class FloorPlaner extends JFrame {
public int x=0;
public FloorPlaner(){
super("FloorPlaner");
requestFocus();
setContentPane(new DrawingPane());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
setResizable(true);
setVisible(true);
while (true) {
x++;
repaint();
try {
Thread.sleep(40); //25 FPS
} catch(InterruptedException bug) {
Thread.currentThread().interrupt();
System.out.println(bug);
}
}
}
class DrawingPane extends JPanel { //Where you actually draw on
public void paintComponent(Graphics g) { //Drawing method
g.drawLine(x,0,x,400);
}
}
public static void main(String args[]) {
new FloorPlaner(); //Start it
}
}

Add image from URL in jPanel

I have a small problem, I have tried different solutions to add a image from URL in my jPanel, but I don't succeed. This is how my jPanel looks like:
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
public class Hello extends JPanel{
JPanel panel = new BackgroundPanel();
JPanel HomeWindowPanel;
JLabel secondlbl = new JLabel("Hello");
public Hello () {
//I want to add a picture here from URL
}
}
I would really appreciate if somebody could show me how to do it.
Check out this sample code:
http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/components/LabelDemoProject/src/components/LabelDemo.java
Specifically the createImageIcon method.

Categories