How do you display a JPeg file in java?

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 3139 Views - Last Post: 25 June 2012 - 03:01 PM Rate Topic: -----

#1 shadowhunterO  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 12
  • Joined: 24-June 12

How do you display a JPeg file in java?

Posted 24 June 2012 - 09:58 AM

I've tried lots of things to get java to display a png or JPeg image but nothing so far has worked, so someone please help!
Is This A Good Question/Topic? 1
  • +

Replies To: How do you display a JPeg file in java?

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1399
  • View blog
  • Posts: 3,101
  • Joined: 05-April 11

Re: How do you display a JPeg file in java?

Posted 24 June 2012 - 09:59 AM

Display a JLabel with the icon set to the image
Was This Post Helpful? 0
  • +
  • -

#3 shadowhunterO  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 12
  • Joined: 24-June 12

Re: How do you display a JPeg file in java?

Posted 24 June 2012 - 10:02 AM

How do you do that, can you show me the code please, thanks!
Was This Post Helpful? 1
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: How do you display a JPeg file in java?

Posted 24 June 2012 - 10:06 AM

JLabel label = new JLabel(new ImageIcon("myfile.jpg"));
then add the label to a JComponent

http://docs.oracle.c...ents/label.html
Was This Post Helpful? 1
  • +
  • -

#5 shadowhunterO  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 12
  • Joined: 24-June 12

Re: How do you display a JPeg file in java?

Posted 24 June 2012 - 10:18 AM

I did that and it said

Writer.java:24: cannot find symbol
symbol : class Jlabel
location: class Writer
JLabel label = new Jlabel(new ImageIcon("LOADING.jpg"));
^
1 error

so I don't know what to do, do u think u can help?
Was This Post Helpful? 1
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10572
  • View blog
  • Posts: 39,143
  • Joined: 27-December 08

Re: How do you display a JPeg file in java?

Posted 24 June 2012 - 10:22 AM

You have to import the JLabel class from the javax.swing package. Also, remember that Java is case sensitive. So it is JLabel, not Jlabel.
Was This Post Helpful? 1
  • +
  • -

#7 shadowhunterO  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 12
  • Joined: 24-June 12

Re: How do you display a JPeg file in java?

Posted 24 June 2012 - 11:26 AM

THANKS SO MUCH MACOSNERD101!!! It works!
Was This Post Helpful? 1
  • +
  • -

#8 shadowhunterO  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 12
  • Joined: 24-June 12

Re: How do you display a JPeg file in java?

Posted 24 June 2012 - 11:32 AM

Just one more thing, I want my program to wait because the image I'm showing is a animated loading gif, so if you could help me with that to it would be great! =)
Was This Post Helpful? 1
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10572
  • View blog
  • Posts: 39,143
  • Joined: 27-December 08

Re: How do you display a JPeg file in java?

Posted 24 June 2012 - 02:58 PM

Java doesn't support animated GIFs. If you want to create an animation, you will have to extend a JComponent like JPanel, override the paintComponent() method, and handle drawing the individual still images yourself. You can use Swing Timer to invoke a repaint() every 20-30 milliseconds.
Was This Post Helpful? 1
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: How do you display a JPeg file in java?

Posted 24 June 2012 - 06:55 PM

JLabel label = new Jlabel(new ImageIcon("LOADING.jpg"));

new JLabel with a L not a l it is JLabel not Jlabel
Was This Post Helpful? 0
  • +
  • -

#11 shadowhunterO  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 12
  • Joined: 24-June 12

Re: How do you display a JPeg file in java?

Posted 25 June 2012 - 07:40 AM

I Fixed that, sorry I din't tell you, but he's my whole program

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;
import java.net.*;

public class Writer extends Panel {
  public Writer() {

  }

  static public void main(String args[]) throws
Exception {

  JFrame frame = new JFrame("Writer");
  JTextArea jt= new JTextArea("",60, 60);
  Panel panel = new Writer();
  frame.getContentPane().add(panel);
  panel.add(jt);
  JLabel label = new JLabel(new ImageIcon("LOADING.gif"));
  Button button = new Button("Save"); 
  frame.add(button);
  frame.setLayout(new FlowLayout());
  frame.setSize(900, 750);
  frame.setVisible(true);
  frame.setLocation(290, 90);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

  }
}


all it does is launch a frame and shows the textarea, it never shows the image

It compiles and runs normally, just never shows the image.
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10572
  • View blog
  • Posts: 39,143
  • Joined: 27-December 08

Re: How do you display a JPeg file in java?

Posted 25 June 2012 - 07:46 AM

You don't add the JLabel anywhere.
Was This Post Helpful? 1
  • +
  • -

#13 shadowhunterO  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 12
  • Joined: 24-June 12

Re: How do you display a JPeg file in java?

Posted 25 June 2012 - 09:11 AM

Oh... I am really sorry, he's my code

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;
import java.net.*;

public class Writer extends Panel {
  public Writer() {

  }

  static public void main(String args[]) throws
Exception {

  JFrame frame = new JFrame("Writer");
  JTextArea jt= new JTextArea("",60, 60);
  Panel panel = new Writer();
  frame.getContentPane().add(panel);
  panel.add(jt);
  JLabel label = new JLabel(new ImageIcon("LOADING.gif"));
  Button button = new Button("Save"); 
  frame.add(button);
  frame.setLayout(new FlowLayout());
  frame.setSize(900, 750);
  frame.setVisible(true);
  frame.setLocation(290, 90);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

  }
}

Was This Post Helpful? 0
  • +
  • -

#14 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2727
  • View blog
  • Posts: 11,469
  • Joined: 20-September 08

Re: How do you display a JPeg file in java?

Posted 25 June 2012 - 09:25 AM

You don't add the JLabel (as you've already been told)

Also, don't mix awt components (such as Panel) and Swing. That should be JPanel. Making Writer a subclass of any kind of panel is not serving any useful purpose at the moment.
Was This Post Helpful? 0
  • +
  • -

#15 shadowhunterO  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 12
  • Joined: 24-June 12

Re: How do you display a JPeg file in java?

Posted 25 June 2012 - 09:28 AM

Like I said be for it compiles and runs perfectly, just never shows the image

Why am I not spost to do that? It seem to work that way.

Sorry about the bad English =(
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2