Results 1 to 19 of 19
  1. #1
    byuu is offline Member
    Join Date
    May 2008
    Posts
    15
    Rep Power
    0

    Default [SOLVED] Question about java.awt.image

    how do you simply put an image(gif format) to an Image datatype?

    example:

    import java.awt.image.*;

    Image i = ?????

    how do you do that?

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Something like this, add an image to GUI or something...

    Java Code:
    Image image = Toolkit.getDefaultToolkit().getImage(your_image_path);

  3. #3
    byuu is offline Member
    Join Date
    May 2008
    Posts
    15
    Rep Power
    0

    Default

    wow, thanks a lot. by the way, is there a simpler way to make an item in the system tray? by the way, i was the one who asked you last week regarding threads that run in an infinite loop. setting the timeout really does well. from 100% CPU it comes down to only 12-50%.

    we are making an IM and maybe you have an idea how to make a system tray for that? that is why i asked how to put an image to an Image datatype cause it is a parameter for a certain method creating a system tray. is there a simpler way on doing it? i find it quite difficult to understand all on the net. thanks for the reply anyways. :)

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    make an item, or make an image in the system tray? Normally an item mean a system tray pop-up menu item.

    How did you do it, because I don't know that ;).

  5. #5
    byuu is offline Member
    Join Date
    May 2008
    Posts
    15
    Rep Power
    0

    Default

    there is this class SystemTray i think in JAVA SE 6. located at java.awt. not quite sure though. here is a link to it on java forums:
    New System Tray Functionality in Java SE 6
    i dont know if its illegal here posting external links but if its illegal ill just edit it

    i'm not that smart to understand it, hehe

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    First thing is, yes you can post links here in our community. But in good way, don't directed people in wrong way. ;)

    Ok, you found a example here. This is a new feature of Java SE6. Before this it's not much easy. :) So now you can go ahead with that example. Where you stuck with.

  7. #7
    byuu is offline Member
    Join Date
    May 2008
    Posts
    15
    Rep Power
    0

    Default

    can you tell whats the problem with this code?


    import java.awt.event.*;
    import java.awt.*;
    import java.awt.Toolkit;
    import java.awt.TrayIcon.*;
    public class TrayIconDIMS
    {

    SystemTray systemtray;
    java.awt.Image image;
    TrayIcon trayIcon;
    PopupMenu popup = new PopupMenu();
    MenuItem defaultItem = new MenuItem("Exit");



    public void TrayIconDIMS()
    {
    image = Toolkit.getDefaultToolkit().getImage("724793.gif") ;
    }

    public void showTray()
    {
    try
    {
    systemtray = SystemTray.getSystemTray();
    System.out.println("System Tray Initialized");
    }
    catch(java.lang.UnsupportedOperationException e)
    {
    e.printStackTrace();
    System.out.println("Unable to initialize System tray");
    }

    MouseListener mouseListener = new MouseListener() {

    public void mouseClicked(MouseEvent e) {
    System.out.println("Tray Icon - Mouse clicked!");
    }

    public void mouseEntered(MouseEvent e) {
    System.out.println("Tray Icon - Mouse entered!");
    }

    public void mouseExited(MouseEvent e) {
    System.out.println("Tray Icon - Mouse exited!");
    }

    public void mousePressed(MouseEvent e) {
    System.out.println("Tray Icon - Mouse pressed!");
    }

    public void mouseReleased(MouseEvent e) {
    System.out.println("Tray Icon - Mouse released!");
    }
    };

    ActionListener exitListener = new ActionListener()
    {
    public void actionPerformed(ActionEvent e) {
    System.out.println("Exiting...");
    System.exit(0);
    }
    };

    defaultItem.addActionListener(exitListener);
    popup.add(defaultItem);

    trayIcon = new TrayIcon(image, "Tray Demo", popup);

    ActionListener actionListener = new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    trayIcon.displayMessage("Action Event",
    "An Action Event Has Been Performed!",
    TrayIcon.MessageType.INFO);
    }
    };


    }

    public static void main(String args[])
    {
    TrayIconDIMS A = new TrayIconDIMS();

    A.showTray();
    }

    }

    the error:

    init:
    deps-jar:
    Compiling 1 source file to /home/byuu/NetBeansProjects/testest/build/classes
    /home/byuu/NetBeansProjects/testest/src/TrayIconDIMS.java:78: cannot find symbol
    symbol : constructor TrayIcon(java.awt.Image,java.lang.String,java.awt. PopupMenu)
    location: class TrayIcon
    trayIcon = new TrayIcon(image, "Tray Demo", popup);
    /home/byuu/NetBeansProjects/testest/src/TrayIconDIMS.java:84: cannot find symbol
    symbol : variable MessageType
    location: class TrayIcon
    TrayIcon.MessageType.INFO);
    2 errors
    BUILD FAILED (total time: 0 seconds)

  8. #8
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    7

    Default

    Could you try this to your code. =) I think its the same error in handling arrays. In my experience with that, its the declaration. I hope this helps

    Java Code:
    SystemTray systemtray;
    java.awt.Image image = Toolkit.getDefaultToolkit().getImage("724793.gif") ;
    TrayIcon trayIcon = new TrayIcon(image);
    PopupMenu popup = new PopupMenu();
    MenuItem defaultItem = new MenuItem("Exit");

  9. #9
    byuu is offline Member
    Join Date
    May 2008
    Posts
    15
    Rep Power
    0

    Default

    it still get the same error.

    and get another error at your code:
    TrayIcon trayIcon = new TrayIcon(image);
    i think the problem is in the TrayIcon declaration or somethin'. thanks for the quick reply.. hope we could sort this one out hehe :)

    im gonna try to compile it on windows. im running on linux,

  10. #10
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    7

    Default

    ohhh. . . Im sorry, Maybe the senior members can help us. =P

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    You have done it in wrong way, I mean the order you have to take is incorrect. Here is the modified code. Try to get the difference between this and your code.

    Java Code:
    import java.awt.event.*;
    import java.awt.*;
    import java.awt.Toolkit;
    import java.awt.TrayIcon.*;
    
    public class TrayIconDIMS
    {
        SystemTray systemtray;
        Image image;    TrayIcon trayIcon;
        PopupMenu popup = new PopupMenu();
        MenuItem defaultItem = new MenuItem("Exit");
    
        public void TrayIconDIMS() {
            
        }
    
        public void showTray() {
            if (SystemTray.isSupported()) {
                try {
                    systemtray = SystemTray.getSystemTray();
                    System.out.println("System Tray Initialized");
    
                    MouseListener mouseListener = new MouseListener() {
    
                        public void mouseClicked(MouseEvent e) {
                            System.out.println("Tray Icon - Mouse clicked!");
                        }
    
                        public void mouseEntered(MouseEvent e) {
                            System.out.println("Tray Icon - Mouse entered!");
                        }
    
                        public void mouseExited(MouseEvent e) {
                            System.out.println("Tray Icon - Mouse exited!");
                        }
    
                        public void mousePressed(MouseEvent e) {
                            System.out.println("Tray Icon - Mouse pressed!");
                        }
    
                        public void mouseReleased(MouseEvent e) {
                            System.out.println("Tray Icon - Mouse released!");
                        }
                    };
    
                    ActionListener exitListener = new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            System.out.println("Exiting...");
                            System.exit(0);
                        }
                    };
    
                    //defaultItem.addActionListener(exitListener);
                    //popup.add(defaultItem);
                    image = Toolkit.getDefaultToolkit().getImage("RADAR.png");
                    trayIcon = new TrayIcon(image, "Tray Demo", popup);
    
                    ActionListener actionListener = new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            trayIcon.displayMessage("Action Event", "An Action Event Has Been Performed!", TrayIcon.MessageType.INFO);
                        }
                    };
    
                    trayIcon.setImageAutoSize(true);
                    trayIcon.addActionListener(exitListener);
                    systemtray.add(trayIcon);
                } 
                catch (AWTException ex) {
                    System.out.println("Error in icon loading.");
                }
            }
            else {
                System.out.println("System tray not supported.");
            }
    
    
        }
    
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new TrayIconDIMS().showTray();
                }
            });
        }
    
    }

  12. #12
    byuu is offline Member
    Join Date
    May 2008
    Posts
    15
    Rep Power
    0

    Default

    sorry for the late reply. i havent have quite time programming since there was a transport strike here and its hard riding a public transportation. hm, i tried the code, compiled it and says:

    init:
    deps-jar:
    Compiling 1 source file to /home/byuu/NetBeansProjects/testest/build/classes
    /home/byuu/NetBeansProjects/testest/src/TrayIconDIMS.java:56: cannot find symbol
    symbol : constructor TrayIcon(java.awt.Image,java.lang.String,java.awt. PopupMenu)
    location: class TrayIcon
    trayIcon = new TrayIcon(image, "Tray Demo", popup);
    /home/byuu/NetBeansProjects/testest/src/TrayIconDIMS.java:60: cannot find symbol
    symbol : variable MessageType
    location: class TrayIcon
    trayIcon.displayMessage("Action Event", "An Action Event Has Been Performed!", TrayIcon.MessageType.INFO);
    /home/byuu/NetBeansProjects/testest/src/TrayIconDIMS.java:64: cannot find symbol
    symbol : method setImageAutoSize(boolean)
    location: class TrayIcon
    trayIcon.setImageAutoSize(true);
    /home/byuu/NetBeansProjects/testest/src/TrayIconDIMS.java:65: cannot find symbol
    symbol : method addActionListener(java.awt.event.ActionListener)
    location: class TrayIcon
    trayIcon.addActionListener(exitListener);
    /home/byuu/NetBeansProjects/testest/src/TrayIconDIMS.java:66: add(java.awt.TrayIcon) in java.awt.SystemTray cannot be applied to (TrayIcon)
    systemtray.add(trayIcon);
    5 errors
    BUILD FAILED (total time: 0 seconds)
    the code seems no sign of error in syntax or usage but i dont know if we are missing anything..

  13. #13
    byuu is offline Member
    Join Date
    May 2008
    Posts
    15
    Rep Power
    0

    Default

    ney, i kinda fixed it. it displays a trayicon on the system tray although empty. the problem i son the imports. i took away the

    import java.awt.TrayIcon

    and just declared the whole thing on instances, method calls of class TrayIcon here is the code, just thinking i could share it here, i don't know what's the difference but it worked anyway, :)

    import java.awt.event.*;
    import java.awt.*;
    import java.awt.Toolkit;
    //import java.awt.TrayIcon.*;

    public class TrayIconDIMS
    {
    SystemTray systemtray;
    Image image;
    java.awt.TrayIcon trayIcon;
    PopupMenu popup = new PopupMenu();
    MenuItem defaultItem = new MenuItem("Exit");

    public void TrayIconDIMS() {

    }

    public void showTray() {
    if (SystemTray.isSupported()) {
    try {
    systemtray = SystemTray.getSystemTray();
    System.out.println("System Tray Initialized");

    MouseListener mouseListener = new MouseListener() {

    public void mouseClicked(MouseEvent e) {
    System.out.println("Tray Icon - Mouse clicked!");
    }

    public void mouseEntered(MouseEvent e) {
    System.out.println("Tray Icon - Mouse entered!");
    }

    public void mouseExited(MouseEvent e) {
    System.out.println("Tray Icon - Mouse exited!");
    }

    public void mousePressed(MouseEvent e) {
    System.out.println("Tray Icon - Mouse pressed!");
    }

    public void mouseReleased(MouseEvent e) {
    System.out.println("Tray Icon - Mouse released!");
    }
    };

    ActionListener exitListener = new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    System.out.println("Exiting...");
    System.exit(0);
    }
    };

    //defaultItem.addActionListener(exitListener);
    //popup.add(defaultItem);
    image = Toolkit.getDefaultToolkit().getImage("RADAR.png");
    trayIcon = new java.awt.TrayIcon(image, "Tray Demo", popup);

    ActionListener actionListener = new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    trayIcon.displayMessage("Action Event", "An Action Event Has Been Performed!", java.awt.TrayIcon.MessageType.INFO);
    }
    };

    trayIcon.setImageAutoSize(true);
    trayIcon.addActionListener(exitListener);
    systemtray.add(trayIcon);
    }
    catch (AWTException ex) {
    ex.printStackTrace();
    }
    }
    else {
    System.out.println("System tray not supported.");
    }


    }

    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new TrayIconDIMS().showTray();
    }
    });
    }

    }

  14. #14
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    7

    Default

    byuu, Are from the Phillipines? I didnt feel the Transport Strike Yesterday Morning. But later in the afternoon, there are lots of people in the streets.

  15. #15
    byuu is offline Member
    Join Date
    May 2008
    Posts
    15
    Rep Power
    0

    Default

    yes, i'm from davao city, people throw spikes on the roads here, :))

  16. #16
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by byuu View Post
    ney, i kinda fixed it. it displays a trayicon on the system tray although empty. the problem i son the imports. i took away the

    import java.awt.TrayIcon

    and just declared the whole thing on instances, method calls of class TrayIcon here is the code, just thinking i could share it here, i don't know what's the difference but it worked anyway, :)
    There is no difference between following two lines.

    Java Code:
    import java.awt.TrayIcon.*;
    
    java.awt.TrayIcon trayIcon;
    I think you get the empty try icon, because you don't change the image name. Is it?

    My code is working 100% perfectly. Which JDk version you used. This is support from 1.5 and seems to me you have older version than this.

    If you want try this, you can see four import statements in my original code. Remove all of them and add only these two.

    Java Code:
    import java.awt.event.*;
    import java.awt.*;
    Still my code working fine. Still you get the error, you used older version of JDK.


  17. #17
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    7

    Default

    The Strike wasnt succesful like before. Luckily i used the Train.

    Im from Makati. Nice to meet you sir byuu.

  18. #18
    byuu is offline Member
    Join Date
    May 2008
    Posts
    15
    Rep Power
    0

    Default

    yes mine is working on now. by the way i am using jdk 1.6 maybe java versions matter.. im not sure though






    ....
    The Strike wasnt succesful like before. Luckily i used the Train.

    Im from Makati. Nice to meet you sir byuu.
    yes, nice to meet you too. :)
    Last edited by byuu; 05-13-2008 at 04:07 AM. Reason: avoid double posting

  19. #19
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Ok, if you solved the question please mark thread solved.

    And also don't make chit-chats on a thread. You have separate forum for that, just do to the forum lobby. :)

Similar Threads

  1. creating image using java
    By mmahesh_mca in forum New To Java
    Replies: 1
    Last Post: 04-26-2008, 11:23 PM
  2. Replies: 0
    Last Post: 01-08-2008, 05:29 PM
  3. Label image Question
    By Soda in forum New To Java
    Replies: 3
    Last Post: 12-10-2007, 03:38 PM
  4. Background image in java
    By toby in forum AWT / Swing
    Replies: 1
    Last Post: 07-29-2007, 07:15 AM
  5. Java Image Album 1.1
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-04-2007, 07:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •