I'm trying to show a GIF image inside a JLabel using the following code
Image waitImage = null;
JLabel l1;
try {
waitImage = ImageIO.read(IndexesPanel.class.getResource("/images/error.gif"));
l1 = new JLabel(new ImageIcon(waitImage));
l1.setBounds(20, 20, 100, 100);
waitPanel.add(l1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The image is shown, but it's not animated. I'm using the following Gif:
Any idea?
ImageIO returns BufferedImage
Use new ImageIcon(new URL("path to resource"));
Guess you can use new ImageIcon(IndexesPanel.class.getResource("/images/error.gif"));
Related
I am trying to set my program (java using netbeans) to do one thing if it finds the image its looking for, and another thing if it doesn't...
this is what I have got so far however it never completes the else statement. I believe this is because "image" is not technically null, as it still has corresponds to the filename entered, however I am not sure how to set java to do something based on if the filename is not found within the directory.
public void displayImage(String strfilename, JLabel JLlabel) {
try {
JLabel label = JLlabel;
String FileName = strfilename;
BufferedImage image = ImageIO.read(new File(FileName + ".jpg"));
if(image!=null){
ImageIcon icon = new ImageIcon(image);
label.setIcon(icon);}
else{
BufferedImage image2 = ImageIO.read(new File("NOIMAGE.jpg"));
ImageIcon icon2 = new ImageIcon(image2);
label.setIcon(icon2);
}
} catch (IOException ioe) {
}
}
If anyone could help me with this I would be very grateful
You're swallowing the exception. You don't need an if{}else{} loop since you already have a try{}catch{}.
String FileName = file;
try {
BufferedImage image = ImageIO.read(new File(FileName + ".jpg"));
// Code for when the image is found
} catch (IOException ex) {
// Code for when the image is not found
}
EDIT:
As #haraldK pointed out, you can have a file that exists but is unreadable, in which case a NullPointerException will be thrown.
You can handle them both in the catch clause.
public void displayImage(String strfilename, JLabel label) {
try {
BufferedImage image = ImageIO.read(new File(strfilename + ".jpg"));
ImageIcon icon = new ImageIcon(image); // Can throw NullPointerException if the file is found but is unreadable
label.setIcon(icon);
} catch (IOException | NullPointerException ex) {
ImageIcon icon = new ImageIcon("NOIMAGE.jpg");
label.setIcon(icon);
}
// You're probably going to have to pack or validate your container here
}
One thing worth noting is that this does not check exceptions for the NOIMAGE, you might want to add that.
This is better than just calling File.exists() as it also handles files that exist but are unreadable (text file and whatnot).
you can check the file if exist before you process this method also i removed some unnecessary code.
public void displayImage(String strfilename, JLabel JLlabel) {
// declare only one reference you don't need two references
BufferedImage image=null;
if(!isImageExist(strfilename)){
// assign the NOIMAGE if image not found
image = ImageIO.read(new File("NOIMAGE.jpg"));
} else {
try {
// assign the image if found
image = ImageIO.read(new File(strfilename + ".jpg"));
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
ImageIcon icon = new ImageIcon(image);
//setting the image once instead of repeating the code in if and else blocks also you don't need to add another reference to JLlabel because you are already got it from a parameter
JLlabel.setIcon(icon);
}
private boolean isImageExist(String imageName) {
return new File(imageName.jpg).exist();
}
Hello I have this code which creates a font from a ttf file in my res folder.
try {
font1 = Font.createFont(Font.TRUETYPE_FONT, new File("res/1942.ttf"));
font1.deriveFont(12f);
} catch (FontFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I thought .deriveFont(); increased the custom font size but for me it dosnt. What is wrong?
Here is where is use the font.
g.setColor(Color.blue);
font1.deriveFont(52);
g.setFont(font1);
g.drawString("hello",480, 250);
This font1.deriveFont(12f); doesn't change font1. Rather it returns a new Font of differing size. You need to something with this returned object, perhaps something like:
setFont(font1.deriveFont(12f));
or
font1 = font1.deriveFont(12f);
Use this
g.setFont(new Font("Serif", Font.PLAIN, 14));
you can use another approach also
JButton btn = new JButton();
btn.setFont(btn.getFont().deriveFont(14.0f));
I have a JEditorPane that is inside a rectangle in a window so that it doesnt fill the whole window. However when the HTML page gets too big, it cuts it off instead of putting in a scrollbar, I know I need a JScrollpane or something, but I dont want the scrollbar on the whole window, only inside the rectangle.
Here is a snippet of my code (rweb is already defined as a Rectangle):
JEditorPane web = new JEditorPane();
web.setEditable(false);
try {
web.setPage("http://www.google.com");
}catch (IOException e) {
web.setContentType("text/html");
web.setText("<html>Could not load webpage</html>");
}
web.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e) {
if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
if(Desktop.isDesktopSupported()) {
try {
Desktop.getDesktop().browse(e.getURL().toURI());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (URISyntaxException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
});
rweb = new Rectangle(0, 0, width, 600);
web.setBorder(null);
web.setBounds(rweb);
window.add(web);
You never add the JEditorPane to a JScrollPane. Text components don't have scroll support by themselves.
Try using window.add(new JScrollPane(web)); instead
Take a look at How to use scroll panes for more details
ps- web.setBounds(rweb); will only work if you are using a null layout and if you are, you'd be better of using a EmptyBorder or a layout manager that provided the ability to specify insets, like GridBagLayout
I am trying to set font of a JLabel to a custom font. No exceptions are thrown reading the file, but nothing appears when I call label.setText("string"). Text appears when I comment out the line label.setFont(f). Anyone know what I'm doing wrong? This code is inside a JPanel class.
_mineLabel = new JLabel();
_timeLabel = new JLabel();
try {
Font f = Font.createFont(Font.TRUETYPE_FONT,new File("/Users/simon/Documents/workspace/Minesweeper/bin/minesweeper/DS-DIGI.TTF"));
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(f);
f.deriveFont(12f);
_mineLabel.setFont(f);
_timeLabel.setFont(f);
} catch(IOException e) {
e.printStackTrace();
} catch(FontFormatException e) {
e.printStackTrace();
}
this.add(_mineLabel);
this.add(_timeLabel);
_timeLabel.setText("test");
Change this line
f.deriveFont(12f);
to
f=f.deriveFont(12f);
Hi i have a jpanel to draw on. from this jpanel i make bufferedimages and safe them into a linkedlist. on a button press i want to animate this bufferedimages(play one after one)
the problem is that the jpanel dont show the buffered images but when i use ImageIO.write to safe the bufferedimages on disk i get all the pictures i want to animate. pls help me.
here is my code:
public void run(){
for(int i=0;i`<`cm.animationListe.size();i++){
b= cm.animationListe.get(i);
try {
ImageIO.write( b, "png", new File( "c:/java/circle"+i+".png" ) );
} catch (IOException e1) {
e1.printStackTrace();
}
try {
Thread.sleep(1000);
repaint();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}
animation = false;
}
public void paintComponent(Graphics g){
super.paintComponent(g);
if(animation){
g.drawImage(b,0, 0,null);
}
}
The Thread.sleep() causes the GUI to freeze so it can't repaint itself.
To do animation you need to use a Swing Timer.