How to add images into a java application (not applet)

Hello,
I am new in java programming. I would like to know how to add images into a java application (not an applet). If i could get an standard example about how to add a image to a java application, I would apreciated it. Any help will be greatly apreciated.
Thank you,
Oscar

Your' better off looking in the java 2d forum.
package images;
import java.awt.*;
import java.awt.image.*;
import java.io.FileInputStream;
import javax.imageio.ImageIO;
import javax.swing.*;
/** * LogoImage is a class that is used to load images into the program */
public class LogoImage extends JPanel {
     private BufferedImage image;
     private int factor = 1; /** Creates a new instance of ImagePanel */
     public LogoImage() {
          this(new Dimension(600, 50));
     public LogoImage(Dimension sz) {
          //setBackground(Color.green);      
          setPreferredSize(sz);
     public void setImage(BufferedImage im) {
          image = im;
          if (im != null) {
               setPreferredSize(
                    new Dimension(image.getWidth(), image.getHeight()));
          } else {
               setPreferredSize(new Dimension(200, 200));
     public void setImageSizeFactor(int factor) {
          this.factor = factor;
     public void paintComponent(Graphics g) {
          super.paintComponent(g);
          //paint background 
          Graphics2D g2D = (Graphics2D) g;
          //Draw image at its natural size first. 
          if (image != null) {
               g2D.drawImage(image, null, 0, 0);
     public static LogoImage createImage(String filename) { /* Stream the logo gif file into an image object */
          LogoImage logoImage = new LogoImage();
          BufferedImage image;
          try {
               FileInputStream fileInput =
                    new FileInputStream("images/" + filename);
               image = ImageIO.read(fileInput);
               logoImage =
                    new LogoImage(
                         new Dimension(image.getWidth(), image.getHeight()));
               fileInput.close();
               logoImage.setImage(image);
          } catch (Exception e) {
               System.err.println(e);
          return logoImage;
     public static void main(String[] args) {
          JFrame jf = new JFrame("testImage");
          Container cp = jf.getContentPane();
          cp.add(LogoImage.createImage("logo.gif"), BorderLayout.CENTER);
          jf.setVisible(true);
          jf.pack();
}Now you can use this class anywhere in your pgram to add a JPanel

Similar Messages

  • Help on moving of image across screen(java application,not applet)

    I've searched the entire internet and everything I've found is on java applets and I'm an alien to the differences between an applet and an application.
    This is actually for a java game I'm creating using Eclipse's visual editor and I'm failing miserably.
    What I'm trying to do is to find some java source code that enables me to start an image automatically moving across the screen that only stops when I click on it. I did find some applet codes but when I tried converting it to application code a load of errors popped out.
    Thanks for the help if there's any! I'm getting desperate here because the game's due this friday and I'm stuck at this stage for who knows how long.

    Here is one of the codes I found ,it's not mine but I'm trying to edit it into a java application instead of an applet...Sort of like: ' public class MovingLabels extends JFrame ' or some code that I can copy and paste into another java program.
    * Swing version.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class MovingLabels extends JApplet
    implements ActionListener {
    int frameNumber = -1;
    Timer timer;
    boolean frozen = false;
    JLayeredPane layeredPane;
    JLabel bgLabel, fgLabel;
    int fgHeight, fgWidth;
    int bgHeight, bgWidth;
    static String fgFile = "wow.gif";
    static String bgFile = "Spring.jpg";
    //Invoked only when run as an applet.
    public void init() {
    Image bgImage = getImage(getCodeBase(), bgFile);
    Image fgImage = getImage(getCodeBase(), fgFile);
    buildUI(getContentPane(), bgImage, fgImage);
    void buildUI(Container container, Image bgImage, Image fgImage) {
    final ImageIcon bgIcon = new ImageIcon(bgImage);
    final ImageIcon fgIcon = new ImageIcon(fgImage);
    bgWidth = bgIcon.getIconWidth();
    bgHeight = bgIcon.getIconHeight();
    fgWidth = fgIcon.getIconWidth();
    fgHeight = fgIcon.getIconHeight();
    //Set up a timer that calls this object's action handler
    timer = new Timer(100, this); //delay = 100 ms
    timer.setInitialDelay(0);
    timer.setCoalesce(true);
    //Create a label to display the background image.
    bgLabel = new JLabel(bgIcon);
    bgLabel.setOpaque(true);
    bgLabel.setBounds(0, 0, bgWidth, bgHeight);
    //Create a label to display the foreground image.
    fgLabel = new JLabel(fgIcon);
    fgLabel.setBounds(-fgWidth, -fgHeight, fgWidth, fgHeight);
    //Create the layered pane to hold the labels.
    layeredPane = new JLayeredPane();
    layeredPane.setPreferredSize(
    new Dimension(bgWidth, bgHeight));
    layeredPane.addMouseListener(new MouseAdapter() {
    public void mousePressed(MouseEvent e) {
    if (frozen) {
    frozen = false;
    startAnimation();
    } else {
    frozen = true;
    stopAnimation();
    layeredPane.add(bgLabel, new Integer(0)); //low layer
    layeredPane.add(fgLabel, new Integer(1)); //high layer
    container.add(layeredPane, BorderLayout.CENTER);
    //Invoked by the applet browser only.
    public void start() {
    startAnimation();
    //Invoked by the applet browser only.
    public void stop() {
    stopAnimation();
    public synchronized void startAnimation() {
    if (frozen) {
    //Do nothing. The user has requested that we
    //stop changing the image.
    } else {
    //Start animating!
    if (!timer.isRunning()) {
    timer.start();
    public synchronized void stopAnimation() {
    //Stop the animating thread.
    if (timer.isRunning()) {
    timer.stop();
    public void actionPerformed(ActionEvent e) {
    //Advance animation frame.
    frameNumber++;
    //Display it.
    fgLabel.setLocation(
    ((frameNumber*5)
    % (fgWidth + bgWidth))
    - fgWidth,
    (bgHeight - fgHeight)/2);
    //Invoked only when run as an application.
    public static void main(String[] args) {
    Image bgImage = Toolkit.getDefaultToolkit().getImage(
    MovingLabels.bgFile);
    Image fgImage = Toolkit.getDefaultToolkit().getImage(
    MovingLabels.fgFile);
    final MovingLabels movingLabels = new MovingLabels();
    JFrame f = new JFrame("MovingLabels");
    f.addWindowListener(new WindowAdapter() {
    public void windowIconified(WindowEvent e) {
    movingLabels.stopAnimation();
    public void windowDeiconified(WindowEvent e) {
    movingLabels.startAnimation();
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    movingLabels.buildUI(f.getContentPane(), bgImage, fgImage);
    f.setSize(500, 125);
    f.setVisible(true);
    movingLabels.startAnimation();
    }

  • How to: add images into an Interactive PDF

    I was wondering if it was possible to add images into an interactive PDF once it has been sent to someone. The PDFs are forms that need to be filled by a client via their iPads using Adobe Expert, and they may need to occasionally attach images to the document and be able to edit these images (i.e. circle parts of the image to highlight them) then send these PDF's back to myself.
    Is there a way this can be done?
    Thanks

    There is no program on the iPad called Adobe Expert. I think you mean Adobe Reader.
    Adobe Reader lets you fill in forms but not to attach images. If you wish to make a request of the Adobe Reader product manager, you can post on the Adobe Reader for iOS forum here:
    http://forums.adobe.com/community/adobe_reader_forums/ios

  • How [Insert|Add] Image into Opening PDF file with Acrobat SDK.

    Hi Guys,
    I'm trying to insert|add image file into opening PDF file with specified location (X|Y) and scale (Width|Hight) but got many of troubles
    If use third party like iTextSharp or something else thing become simple but i want to use Acrobat SDK to do this
    Any suggestion or idea?
    Any help appreciated.

    Thank for your interested.
    I use VB.NET and Acrobat
    Here is some my code:
    Try
       If File.Exists(T(0)) Then
       Dim AcroAVDoc As AcroAVDoc = Ap.GetActiveDoc
       Dim AcroPDDoc As AcroPDDoc = AcroAVDoc.GetPDDoc
       Dim AcroPDPage As Acrobat.AcroPDPage = AcroPDDoc.AcquirePage(Integer.Parse(T(3)))
       Dim data() As String = T(1).Split("^")
       Dim imgX = data(0)
       Dim imgY = data(1)
       Dim imgWidth = data(3)
       Dim imgHight = data(4)
       'TODO: insert into opening PDF file
       Return 1
       End If
       Catch ex As Exception
       End Try
    I don not know what to do next to insert an image (JPEG, PNG, ..) into PDF file.
    Can you show me or suggest some solution, idea?
    Thank in advance.

  • How to Add Image to Web Dynpor Application

    Hi,
    I want to develop new view with two elements
       1. Label
       2. image
    lable properties are
         text property: Welcome to My Page
         design property: header1
    image properties are
            source:  img1.jpeg
    i created view and in the view i creatge label with out any error.
    comming to image  i inserted child to view root element and to select SOURCE for the image element its showing context window.
    i am not able to see here any image fine
    one thing to tel u that i already placed IMAGE.JPEG under /MIME/MYCOMPONENT/IMAGE.JPEG
    So please let me know how to get the image  in the form..
    regards
    mmukesh

    1. Add an another UI element like Caption along with a label
    2. Create a Value Attribute for the image and make its Calculated property to true. This automatically creates the setter and getter methods to retrieve the image.
    Bind the image source property to this created value attribute.
    3.  In the get<value attribute> method, return the image.(image file name and extension are case sensitive - for ex: Image.jpeg)
    Thanks and Regards,
    Amar Bhagat Challa

  • How to save image into blob:java.sql.SQLException: ORA-01465:nvalid hex

    hi all,
    I am trying to save an image (blob) into oracle. When i try this i am getting following error.
                      java.sql.SQLException: ORA-01465: invalid hex number
                             BLOB blob = BLOB.createTemporary(con , false, BLOB.DURATION_SESSION);
                       String dir = "C:\\opt\\temp";
                       File binaryFile = new File(dir+"/"+filename);
                                FileInputStream instream = new FileInputStream(binaryFile);
                          OutputStream outstream = blob.setBinaryStream(1L);
                          int size = blob.getBufferSize();
                          byte[] buffer = new byte[size];
                          int length = -1;
                          while ((length = instream.read(buffer)) != -1)
                            outstream.write(buffer, 0, length);
                          instream.close();
                          outstream.close();
                                  System.out.println("blob:>>>>>>"+blob);
                    String sqlText =
                              "INSERT INTO test_fileupload (filename, blobfile) " +
                              "   VALUES('" + filename + "','" + outstream  + "')";
                          st.executeUpdate(sqlText);
                          con.commit();In the above Insert statement i tried with "blob" insted of "outstream" still same but when i try with the string "3s34se"
    it is inserting into database..
    I am new to blob can any one explain me why is like that.
    Thanq in adv.
    Edited by: Ajayuppalapati on Nov 21, 2008 4:40 PM

    ORA-01465: invalid hex number
    [http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=3&t=012434]
    [http://forums.sun.com/thread.jspa?threadID=261091&forumID=31]

  • How To Add Flash Movie In Java Application

    Hello Every One
    I am facing a problem.
    I want to add a flash movie
    Plz Help Me

    Flash Player Java Edition is available - but only for old Flash V.2 :(

  • How to change heap memory size for general java applications (not applets)

    Hi. I made a java class which manipulates images and I sometimes get an out of memory error when the files are large. In these cases I can run it successfully from the command line using:
    java -Xms32m -Xmx128m myappbut as I run this class from a firefox extension, I can't use this technique.
    Could some one tell me how to set this globally? I've found a lot of info on setting it for applets in the control panel but that's it. I also tried editing my deployment.properties file by adding:
    deployment.javapi.jre.1.6.0_11-b03.args=-Xmx128mbut none of these options seem to work.
    Thanks for any help.

    Also you can use use [JAVA_TOOL_OPTIONS|http://java.sun.com/j2se/1.5.0/docs/guide/jvmti/jvmti.html#tooloptions], with more documentation here. JAVA_TOOLS_OPTIONS is available since 1.5. There is no direct documentation on [_JAVA_OPTIONS|http://java.sun.com/j2se/1.5.0/docs/guide/2d/flags.html] but was made available since at least 1.3.
    The recent existing forum thread [Java Programming - How to change the default memory limits for java.|http://forums.sun.com/thread.jspa?threadID=5368323&messageID=10615245&start=36] has a long discussion on the issue.
    You specified you are not using applets, but if you do, I would also suggest you use the next Generation Plug-in (since 1.6.0_10) that allows you specify the max memory in the applet without having to instruct the user how to make the change. See [JAVA_ARGUMENTS|https://jdk6.dev.java.net/plugin2/#JAVA_ARGUMENTS] for applets. Java Webstart uses resources.

  • Repaint in Java application (not Applet)

    Hi,
    I have server application with GUI containing TextArea. On any connection request from client, the application creates a thread which deals with the client request. Once the client request finishes, I want to show the detail of the client which connected to the server in the TextArea. Now I have extended the main server class for the thread class, which deals with the client, and have a method in the main server class which appends string on the textarea. BUT, when thread call that function nothing appears in the TextArea. I have checked that the thread is calling the method correctly, I think I need to repaint the TextArea. How? I am looking for your help. I have tried repaint, it does not work. Appreciate any help in this regard,
    Here is the code.
    Server side.......
    public synchronized void displayMessage( String thismessage) {
         System.out.println("-----> " + thismessage);
         editPan.setEditable(true);
         editPan.append(thismessage + "\n");
    public void run() {
    while(true) {
    try {
    server = new ServerSocket( 5000, 100 );
    while(listening) {
    connection = server.accept();
    if(connection != null) {
    alertDialog cThread = new alertDialog(connection, errorNumber);
    (new Thread(cThread)).start();
    errorNumber++;
    connection = null;
    catch( IOException e ) {
    System.out.println("Exception : " + e.toString() );
    Client Attender Thread class (alertDialog) which extends server class......
    public void run() {
    try {
    BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    String inputLine = in.readLine();
    System.out.println(" " + dateString + " " + inputLine );
    // super.editPan.append(" Test from alert.. \n");
         super.displayMessage(" " + dateString + " " + inputLine);
    JOptionPane.showMessageDialog(null, inputLine, "Alert....", JOptionPane.ERROR_MESSAGE);
    in.close();
    } catch (IOException e) {

    Be careful when calling Swing methods from outside the EventDispatch thread. If you call a swing method from a thread, you should use SwingUtilities.invokeAndWait() or SwingUtilities.invokeLater().
    See http://java.sun.com/docs/books/tutorial/uiswing/mini/threads.html for more details.

  • How to assign the Thems into Webdynpro Java Application

    HI All,
                   I Downloaded the plugin for them editor and i add those plugins and i created the Them. Please any one can help me how to add that Them into Webdynpro java application.

    Hi ep bhargav,
    You can check this link.....
    https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/docs/library/uuid/ccb6bcf4-0401-0010-e3bc-ec0ef03e13d1
    Re: webdynpro theme editor
    Thanks.
    Venkat.

  • How to import the image by using java application

    1.how to import the image by using java APPLICATION and display it on the textarea that you have been created.
    2.how to store the image into the file.
    3. what class should i used?
    4. how to create an object to keep track the image in java application.
    * important : not java applet.
    plzzzzzzz.
    regards fenny

    follow the link:
    http://java.sun.com/docs/books/tutorial/2d/images/index.html

  • How can I put an image/s in Java Application?

    Hi to all.
    How can I put an image/s in Java Application?
    I can put some images in Java applet but when i try to put it in Java application there was an error.

    hey u can easily do it with JLabels...i found a Code Snippet for u
    public class MyPanel extends JPanel
    {private JLabel imageLabel; public MyPanel() {     Image image = Toolkit.getDefaultToolkit().getImage("myimage.gif"); // Could be jpg or png too     imageLabel = new JLabel(new ImageIcon(image));     this.add(imageLabel);  }}
    hope it helps
    Cheers,
    Manja

  • How to add image to webdynpro screen . ?

    How to add image to webdynpro screen . ?

    hi,
    right click ur application and then click on create mime object.
    with Mime Objects u cn upload doc , jpeg, or giff files from our local system into the webdypnpro system .
    You can even try creating the MIME objects in webdynrpo abap .
    Right click on ur component->mime object->import
    after importing you can see that image into your component as MIME object .Now insert a UI element image into your view layout .
    Go to the source property of IMAGE element and select F4 option , u will find a window is opening with some tabs
    Select tab COMPONENT IMAGES and component name select your component .
    You will find the image which you have imported into this section just select the image and save it.
    In the transaction sicf/bc/webdynpro , u cn check your component name there you can view the mime objects created by you .
    also refer the SAP online help :
    http://help.sap.com/saphelp_crm50/helpdata/en/46/bb182fab4811d4968100a0c94260a5/content.htm
    to knw more abt mime repositories.
    http://help.sap.com/saphelp_nw04/helpdata/en/f3/1a61a9dc7f2e4199458e964e76b4ba/content.htm
    regards,
    Amit

  • How to use OLE Components in Java Application?

    Hi,
    I try to make program that is able to include OLE components( Excel sheet, MS Word document, and so on ), and that can control these components.
    I want to know that Java Application or Applet can control and show OLE components like JavaBeans components. If that, How to ?

    Try this to generate a Word file.....
    import com.jacob.activeX.*;
    import com.jacob.com.*;
    public class ExportDOC {
    public ExportDOC () {
    //Application type
    ActiveXComponent doc = new ActiveXComponent("Word.Application");
    //Application visible
    doc.setProperty("Visible", new Variant(true));
    Object documents = doc.getProperty("Documents").toDispatch();
    //Open a file already exits
    //Object document = Dispatch.call(documents, "Open", "c:/nameFile.doc").toDispatch();
    //Open a new file empty
    Object document = Dispatch.call(documents, "Add", "").toDispatch();
    Object selection = Dispatch.get(doc,"Selection").toDispatch();
    //Text Format
    Object font = Dispatch.get(selection, "Font").toDispatch();
    Object alignment = Dispatch.get(selection, "ParagraphFormat").toDispatch();
    Object image = Dispatch.get(selection, "InLineShapes").toDispatch();
    //I put the selection at the end of the file for if I want to add text
    //Dispatch.call(selection, "EndKey", "6");
    //Text format (bold & italic)
    Dispatch.put(font, "Bold", "1");
    Dispatch.put(font, "Italic", "1");
    //To jump line
    Dispatch.call(selection, "TypeParagraph");
    //Align center (0= left, 1=center, 2=right, 3=justify)
    Dispatch.put(alignment, "Alignment", "1");
    //Insert image
    Dispatch.call(image, "AddPicture", "c:/graf.jpg");
    Dispatch.call(selection, "TypeParagraph");
    Dispatch.put(alignment, "Alignment", "3");
    //Insert Text
    Dispatch.call(selection, "TypeText", "Text ....... ");
    //Original values
    Dispatch.call(selection, "TypeParagraph");
    Dispatch.put(alignment, "Alignment", "0");
    Dispatch.put(font, "Bold", "0");
    Dispatch.put(font, "Italic", "0");
    //To jump page
    //Dispatch.call(select, "InsertBreak");
    //Save the document
    //Dispatch.call(document, "SaveAs", "c:/test.doc");
    //Quit the application
    //dc.invoke("Quit", new Variant[] {});
    public static void main(String[] args) {
    ExportDOC e = new ExportDOC();
    but you must download 'jacob.jar' in....
    http://users.rcn.com/danadler/jacob/
    is a FREEWARE!!!
    regards!!

  • How to import image for display in APPLICATION

    1.how to import the image by using java APPLICATION and display it on the textarea that you have been created.
    2.how to store the image into the file.
    3. what class should i used?
    4. how to create an object to keep track the image in java application.
    * important : not java applet.
    plzzzzzzz.
    regards fenny

    You can use Graphics package for importing images in Java applications. But I am afraid you cannot display it on Text area, you can display it on JPanel, or JLabel or some container like this.
    Check this example.
    http://www.javaalmanac.com/egs/java.awt/DrawImage.html

Maybe you are looking for

  • Concatenate fields in hr payroll attendance report

    Hi all, I need to concatenate three fields PERNR, BEGDA and  ATTIND  into a string.after concatenating I need to count no. of records present for this combination into internal table and compare this count with other oracle database records count.if

  • Read a xml file from local directory, transform it and put in new local dir

    Hi, I have a simple requirement. I have one XML file in d://source folder. I want to read it, transform it and want to put the transformed xml file in new location d://target. I am new to OSB. Please help me out to complete the above requirement. Tha

  • Multiple SQL Statements in Data Template

    If I have more than 1 sql statements in a data template, how can I target them directly? I would like to link directly to a report and don't want to use the default sql statement.

  • How to install Forms Demos?

    Hi: I've downloaded the Forms Demos for NT but I cannot install it on my computer which I've installed Oracle 8i on it. The Installer said that "Oracle Forms Demos does not support multiple installations, and therefore cannot installed on 'DesDev6i'.

  • Determining web item parameters in JavaScript

    Hi there Is it possible to determine the parameters of a web item using JavaScript? I know there are commands to SET the parameters in JavaScript, but I need to determine a parameter first, then set it accordingly. Any ideas? Cheers, Andrew