Exported Java program splash screen not showing - java

So I created a simple java program(not important) and in eclipse I have a gif file that is the splash screen. When I exported the program into a jar the splash screen came up blank. Any help with this? Thanks in advance. Here is the main class:
package com.ethan.main;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JWindow;
import javax.swing.SwingConstants;
public class SingAlong {
public static void main (String args[]) {
int close = 0;
JWindow window1 = new JWindow();
window1.getContentPane().add(
new JLabel("", new ImageIcon("res/Backgrounds/Final GIF.gif"), SwingConstants.LEFT));
window1.setBounds(0, 100, 300, 200);
window1.setVisible(true);
if(close == 1){
window1.dispose();
}
try {
Thread.sleep(6850);
close = 1;
}
catch (InterruptedException e) {
e.printStackTrace();
}
JFrame window = new JFrame("Science Quiz");
window.setContentPane(new SingAlongMain());
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(true);
window.pack();
window.setVisible(true);
}
}

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

Why does my audio playback freeze ActionListners?

Im using the this javaPlayer to playback my sounds.
But in my larger
project it freezes/ignores almost every input JFrame.EXIT_ON_CLOSE and ActionListners that alter the frame content. I tried to repruduce the issue in this snippet.
The btn.addActionListener(e -> System.out.println("Check1")); is not applied to my button. Only if I comment out the whole try/catch-block the "Check1" is also reached.
What could be the problem? I already tried to send the playerpart with Swing.invokeLater to another thread.
package mainMVC;
import java.io.FileInputStream;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;
public class Alone {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JButton btn = new JButton("Test");
btn.addActionListener(e -> System.out.println("Check"));
frame.add(btn);
frame.pack();
try (FileInputStream fis = new FileInputStream("src/EgyptianTavernFullofGuitarists_1.mp3"))
{
Player player = new Player(fis);
player.play();
} catch (IOException | JavaLayerException e) {
e.printStackTrace();
}
btn.addActionListener(e -> System.out.println("Check1"));
}
}
I don't know what's a problem. I've tested your code in my IDE, and it works. During start Frame, music plays. ActionListener returns Check and music doesn't stop, same EXIT_ON_CLOSE.
EDIT: I found that execution is stopped till the playback of the file is finished.
I managed it with a manual thread creation. Now both "Check" and "Check1" are printed.
File: Audio.java
package mainMVC;
import java.io.FileInputStream;
import java.io.IOException;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;
class Audio extends Thread {
public void run(){
try (FileInputStream fis = new FileInputStream("src/EgyptianTavernFullofGuitarists_1.mp3"))
{
Player player = new Player(fis);
player.play();
} catch (IOException | JavaLayerException e) {
e.printStackTrace();
}
}
}
File: Alone.java
package mainMVC;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Alone {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JButton btn = new JButton("Test");
btn.addActionListener(e -> System.out.println("Check"));
frame.add(btn);
frame.pack();
Audio myThread = new Audio();
myThread.start();
btn.addActionListener(e -> System.out.println("Check1"));
}
}

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

Java / Swing / WebcamCapture : When I disable a JLabel, an other image appears

I come to you because I have a strange issue, for which I don't find any solution...
I build an application using a webcam, in order to take some photographs.
I use WebcamCapture to do that, and I don't encounter any issues with it.
The only "weird" thing that happens is the following :
I use a JDialog in which I make photograph. In its JFrame parent, I display those photographs in JLabel.
Then, i need to disable those JLabel, and I do that by calling a method which disable all components. The weird thing is, when I disable JLabel, the JLabel display the last image capture by the webcam. Not the last photographs, but really the last captured image.
It's seems that BufferedImage (used by WebcamPanel) are linked to the issue.
Here is the SSCE :
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamPanel;
#SuppressWarnings("serial")
public class CameraFrame extends JFrame implements ActionListener{
public Webcam webcam;
Boolean enabled = true;
CameraFrame frame;
private JButton btnSaveVerso;
private JLabel lblVerso;
private JButton btnEnable;
private JButton btnQuit;
private JPanel mainPanel;
private WebcamPanel streamPanel;
public static void main(String[] args){
CameraFrame frame = new CameraFrame();
frame.setVisible(true);
}
public CameraFrame() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
setMinimumSize(new Dimension(800, 600));
setPreferredSize(new Dimension(800,600));
buildPanel();
setContentPane(mainPanel);
}
});
}
public void buildPanel() {
mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
Border blackline = BorderFactory.createLineBorder(Color.black, 1, true);
webcam = Webcam.getDefault();
webcam.open();
streamPanel = new WebcamPanel(webcam);
streamPanel.setPreferredSize(new Dimension(webcam.getViewSize()));
streamPanel.setMaximumSize(new Dimension(webcam.getViewSize()));
btnSaveVerso = new JButton("Take pic");
btnSaveVerso.setActionCommand("take");
btnSaveVerso.addActionListener(this);
lblVerso = new JLabel("Here will be the pic taken by the camera");
lblVerso.setBorder(blackline);
btnEnable = new JButton("Disable");
btnEnable.setActionCommand("disable");
btnEnable.addActionListener(this);
btnQuit = new JButton("Quit");
btnQuit.setActionCommand("quit");
btnQuit.addActionListener(this);
mainPanel.add(streamPanel);
mainPanel.add(btnSaveVerso);
mainPanel.add(lblVerso);
mainPanel.add(btnEnable);
mainPanel.add(btnQuit);
}
#Override
public void actionPerformed(final ActionEvent e) {
Thread newThread = new Thread(){
public void run(){
if(e.getActionCommand().equals("take")){
ImageIcon icon = new ImageIcon(webcam.getImage().getScaledInstance(100, 150, Image.SCALE_SMOOTH ));
lblVerso.setIcon(new ImageIcon(icon.getImage()));
lblVerso.revalidate();
lblVerso.repaint();
}
else if(e.getActionCommand().equals("disable")){
if(enabled){
lblVerso.setEnabled(false);
enabled = false;
btnEnable.setText("Enable");
}
else{
lblVerso.setEnabled(true);
enabled = true;
btnEnable.setText("Disable");
}
}
}
};
newThread.run();
if(e.getActionCommand().equals("quit")){
webcam.close();
this.setVisible(false);
}
}
}
I hope you will compile it without issues. Don't forget to link the librairies.
Thanks in advance
I finally resolved the problem : you simply need to close the webcam after each pictures, as follows :
BufferedImage picture = webcam.getImage();
webcam.close();
webcam.open();
... Do what you need with picture
(You don't even need to convert BufferedImage picture in an other type.)

Fullscreen freezes after closing JOptionPane

package sample;
import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class NewClass {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
final JFrame frame = new JFrame();
final JDesktopPane d = new JDesktopPane();
frame.setTitle("Frame");
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
device.setFullScreenWindow(frame);
device.setDisplayMode(new DisplayMode(800, 600, 32, 60));
frame.setVisible(true);
JButton btn = new JButton();
btn.setText("Button");
final JPanel panel = new JPanel();
panel.add(btn);
frame.add(panel);
final JFileChooser chooser = new JFileChooser();
chooser.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals(JFileChooser.APPROVE_SELECTION)) {
System.out.println("File selected: " + chooser.getSelectedFile());
chooser.getFocusCycleRootAncestor().setVisible(false);
} else {
chooser.getFocusCycleRootAncestor().setVisible(false);
}
}
});
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showInternalOptionDialog(frame.getContentPane(), chooser, "Browse", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, new Object[]{}, null);
}
});
}
}
This code looks weird for you, but thats the only way to preserve my full screen using GraphicsDevice. My problem is that, when I click the cancel or open button of the JFileChooser, my screen freezes using this code chooser.getFocusCycleRootAncestor().setVisible(false);. How can I close the JOPtionPane using internal dialog without freezing my screen or closing the whole screen.
you problem is not in
chooser.getFocusCycleRootAncestor().setVisible(false);
if you make these changes, your code will work flawlessly
just remove this part
JOptionPane.showInternalOptionDialog(frame.getContentPane(),chooser, "Browse",JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, new Object[]{}, null);
and add this code instead
chooser.showOpenDialog(frame);
let me know if you have further concerns
The problem is, the program still thinks that there is a modal dialog open, which is restricting focus to the modal dialog...
Try changing your chooser's actionListener to something like this...
chooser.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Container parent = chooser.getParent();
while (!(parent instanceof JOptionPane)) {
parent = parent.getParent();
}
JOptionPane op = (JOptionPane) parent;
op.setValue("done");
if (e.getActionCommand().equals(JFileChooser.APPROVE_SELECTION)) {
System.out.println("File selected: " + chooser.getSelectedFile());
} else {
}
}
});
This basically "tricks" the JOptionPane into thinking that the user has selected a value (which you've actually not provided anything for) and closes the dialog, returning control back to your application

Categories