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));
}
}
Related
I am trying to get an image to appear inside of my JFrame. However, this image only appears when I maximize the GUI window. My image is located inside my src folder. I was wondering if I am implementing JFrame incorrectly.
UPDATED CODE
public static void main(String args[]) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new JLabel(new ImageIcon("logo.png")));
frame.pack();
frame.setVisible(true);
Simple example.
The image is placed within stof package. The example makes use of ImageIO to load the image. The reason for this is two fold.
If something goes wrong, an exception is raised, so you can diagnose it
ImageIO.read won't return until the image is fully loaded. This means that, unlike ImageIcon, you know the image is available for rendering once the call returns. Just remember, if the image is large and you do this from within the event dispatching thread, you will cause the UI to "stall"
Example
import java.awt.EventQueue;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
BufferedImage image = ImageIO.read(Test.class.getResource("/stof/Background.png"));
JFrame frame = new JFrame();
frame.add(new JLabel(new ImageIcon(image)));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
}
}
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"));
}
}
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
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);
}
}
I am still pretty new to Java and tried to find information about how to include a picture in a frame.
Some pictures have the right size and fit in my screen, however other definitely have to be scaled to make them fit in.
So far I got this, is there a way to improve it and scale the picture down? For example half of the size it has now? What do I have to change in the code?
import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
class DisplayImage {
public static void main(String avg[]) throws IOException
{
DisplayImage img =new DisplayImage();
}
public DisplayImage() throws IOException
{
BufferedImage img=ImageIO.read(new File("C://Address.jpg"));
BufferedImage ret = new BufferedImage(32,32,BufferedImage.SCALE_FAST);
ret.getGraphics().drawImage(img,0,0,32,32,null);
ImageIcon icon=new ImageIcon(img);
JFrame frame=new JFrame();
frame.setLayout(new FlowLayout());
frame.setSize(200,300);
JLabel lbl=new JLabel();
lbl.setIcon(icon);
frame.add(lbl);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
You can scale an image with the following code:
image.getScaledInstance(-1, 25,
java.awt.Image.SCALE_SMOOTH);