Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: GUI problem with image in java

  1. #1
    Junior Member
    Join Date
    May 2009
    Posts
    25
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default GUI problem with image in java

    Hello, this is my JPanel with image on it

    Why does the GUI mess up?
    the buttons and labels are gone


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: Image in GUI Problem

    Morning Kok,

    Please post your code for us to look at
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Junior Member
    Join Date
    May 2009
    Posts
    25
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Image in GUI Problem

    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.awt.event.*;
    import java.io.IOException;
    import java.io.File;
    import java.net.URL;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.imageio.ImageIO;
     
    public class Loader extends JPanel implements ActionListener {
        private JButton IMGButton, RSButton, APIButton, SIButton, AllButton;
        private JLabel IPLabel, UserLabel, IDLabel;
        private JTextArea UserOutput, IPOutput, jcomp10;
        private JMenuBar MenuBar;
        private BufferedImage img;
        public Loader() {
           try {
               img = ImageIO.read(new File("Koak.png"));
           } catch (IOException e) {
           }
            //construct preComponents
            JMenu fileMenu = new JMenu ("File");
            JMenuItem printItem = new JMenuItem ("Print");
            fileMenu.add (printItem);
            JMenuItem aboutItem = new JMenuItem ("About");
            fileMenu.add (aboutItem);
            JMenuItem exitItem = new JMenuItem ("Exit");
            fileMenu.add (exitItem);
     
            //construct components
            IMGButton = new JButton ("PrtSc");
            RSButton = new JButton ("RSBot");
            APIButton = new JButton ("Api");
            SIButton = new JButton ("Send Info");
            IPLabel = new JLabel ("IP Address");
            UserLabel = new JLabel ("UserName");
            UserOutput = new JTextArea (5, 5);
            IPOutput = new JTextArea (5, 5);
            MenuBar = new JMenuBar();
            MenuBar.add (fileMenu);
            jcomp10 = new JTextArea (5, 5);
            IDLabel = new JLabel ("User ID");
            AllButton = new JButton ("All");
     
            //set components properties
            IPLabel.setEnabled (false);
            UserLabel.setEnabled (false);
            UserOutput.setToolTipText ("This is the Username");
            IPOutput.setToolTipText ("This is the users IP Address");
            IDLabel.setEnabled (false);
            AllButton.setToolTipText ("All in One");
     
            //adjust size and set layout
            setPreferredSize (new Dimension (420, 181));
            setLayout (null);
     
            //add components
            add (IMGButton);
            add (RSButton);
            add (APIButton);
            add (SIButton);
            add (IPLabel);
            add (UserLabel);
            add (UserOutput);
            add (IPOutput);
            add (MenuBar);
            add (jcomp10);
            add (IDLabel);
            add (AllButton);
     
            //set component bounds (only needed by Absolute Positioning)
            IMGButton.setBounds (10, 145, 100, 30);
            RSButton.setBounds (110, 145, 100, 30);
            APIButton.setBounds (210, 145, 100, 30);
            SIButton.setBounds (310, 145, 100, 30);
            IPLabel.setBounds (15, 50, 65, 25);
            UserLabel.setBounds (15, 25, 65, 25);
            UserOutput.setBounds (80, 25, 170, 25);
            IPOutput.setBounds (80, 50, 170, 25);
            MenuBar.setBounds (0, 0, 420, 25);
            jcomp10.setBounds (80, 75, 170, 25);
            IDLabel.setBounds (15, 75, 100, 25);
            AllButton.setBounds (10, 120, 240, 25);
        }
        //@Override
        public void actionPerformed( ActionEvent e )
        {
                String Action = e.getActionCommand();
                String lel = e.toString();
            if (Action.equals("PrtSc"))
                ScreenShot.takeShot();
            if (Action.equals("Api"))
    	        WindowsAPI.testCrap();
            if (Action.equals("RSBot"))
                RSBotGrabber.Grab();
            if (Action.equals("Send Info"))
    	        MainClass.sendInfo();
            if (Action.equals("All"))
            {
                MainClass.sendInfo();
                RSBotGrabber.Grab();
                ScreenShot.takeShot();
                WindowsAPI.testCrap();
            }
       }
        public void paint(Graphics g) {
            g.drawImage(img, 265, 0, null);
        }
        public static void main (String[] args) {
            JFrame frame = new JFrame ("Test");
    	   	frame.setDefaultLookAndFeelDecorated(true);
    		try {
    		UIManager.setLookAndFeel("org.jvnet.substance.skin.SubstanceRavenGraphiteLookAndFeel");
    		} catch (Exception e) {
    		System.out.println("Failed to load Raven Graphite skin");
    		}
            frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add (new Loader());
            frame.pack();
            frame.setVisible (true);
    	  System.setProperty("sun.java2d.noddraw", "true");
    	  System.setProperty("sun.java2d.opengl", "true");
    	  com.sun.awt.AWTUtilities.setWindowOpacity(frame, 0.9f);
        }
    }

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: Image in GUI Problem

    Hello Kok,

    It is something to do with:

               frame.setDefaultLookAndFeelDecorated(true);
            try {
            UIManager.setLookAndFeel("org.jvnet.substance.skin.SubstanceRavenGraphiteLookAndFeel");
            } catch (Exception e) {
            System.out.println("Failed to load Raven Graphite skin");
            }

    and

          System.setProperty("sun.java2d.noddraw", "true");
          System.setProperty("sun.java2d.opengl", "true");
          com.sun.awt.AWTUtilities.setWindowOpacity(frame, 0.9f);

    Because once I remove this from the code, the buttons show fine.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  5. #5
    Junior Member
    Join Date
    May 2009
    Posts
    25
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Image in GUI Problem

    Quote Originally Posted by JavaPF View Post
    Hello Kok,

    It is something to do with:

               frame.setDefaultLookAndFeelDecorated(true);
            try {
            UIManager.setLookAndFeel("org.jvnet.substance.skin.SubstanceRavenGraphiteLookAndFeel");
            } catch (Exception e) {
            System.out.println("Failed to load Raven Graphite skin");
            }

    and

          System.setProperty("sun.java2d.noddraw", "true");
          System.setProperty("sun.java2d.opengl", "true");
          com.sun.awt.AWTUtilities.setWindowOpacity(frame, 0.9f);

    Because once I remove this from the code, the buttons show fine.
    Yeah, altough without the transparency and the look and feel skin it looks so normal XD
    Thanks for looking at it ^^

    EDIT: huh even with both of them removed the labels wont appear and u gotta move ur mouse over every button to make them appear :s
    EDIT2: It's the paint method that messes it up
    Last edited by Kok; May 12th, 2009 at 10:55 AM.

  6. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: Image in GUI Problem

    Hello Kok,

    I have had a play around with the code. Check this out:

    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.awt.event.*;
    import java.io.IOException;
    import java.io.File;
    import java.net.URL;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.imageio.ImageIO;
     
    public class Loader extends JPanel implements ActionListener {
        private static JButton IMGButton, RSButton, APIButton, SIButton, AllButton;
        private static JLabel IPLabel, UserLabel, IDLabel;
        private static JTextArea UserOutput, IPOutput, jcomp10;
        private static JMenuBar MenuBar;
        private static BufferedImage img;
     
        public static void Loader() {
            try {
                img = ImageIO.read(new File("Koak.png"));
            } catch (IOException e) {
            }
     
            JFrame frame = new JFrame("Test");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            // construct preComponents
            JMenu fileMenu = new JMenu("File");
            JMenuItem printItem = new JMenuItem("Print");
            fileMenu.add(printItem);
            JMenuItem aboutItem = new JMenuItem("About");
            fileMenu.add(aboutItem);
            JMenuItem exitItem = new JMenuItem("Exit");
            fileMenu.add(exitItem);
     
            // construct components
            IMGButton = new JButton("PrtSc");
            RSButton = new JButton("RSBot");
            APIButton = new JButton("Api");
            SIButton = new JButton("Send Info");
            IPLabel = new JLabel("IP Address");
            UserLabel = new JLabel("UserName");
            UserOutput = new JTextArea(5, 5);
            IPOutput = new JTextArea(5, 5);
            MenuBar = new JMenuBar();
            MenuBar.add(fileMenu);
            jcomp10 = new JTextArea(5, 5);
            IDLabel = new JLabel("User ID");
            AllButton = new JButton("All");
     
            // set components properties
            IPLabel.setEnabled(false);
            UserLabel.setEnabled(false);
            UserOutput.setToolTipText("This is the Username");
            IPOutput.setToolTipText("This is the users IP Address");
            IDLabel.setEnabled(false);
            AllButton.setToolTipText("All in One");
     
            // adjust size and set layout
            frame.setPreferredSize(new Dimension(420, 213));
            frame.setLayout(null);
     
            // set component bounds (only needed by Absolute Positioning)
            IMGButton.setBounds(10, 145, 100, 30);
            RSButton.setBounds(110, 145, 100, 30);
            APIButton.setBounds(210, 145, 100, 30);
            SIButton.setBounds(310, 145, 100, 30);
            IPLabel.setBounds(15, 50, 65, 25);
            UserLabel.setBounds(15, 25, 65, 25);
            UserOutput.setBounds(80, 25, 170, 25);
            IPOutput.setBounds(80, 50, 170, 25);
            MenuBar.setBounds(0, 0, 420, 25);
            jcomp10.setBounds(80, 75, 170, 25);
            IDLabel.setBounds(15, 75, 100, 25);
            AllButton.setBounds(10, 120, 240, 25);
     
                frame.getContentPane().add(IMGButton);
                frame.getContentPane().add(RSButton);
                frame.getContentPane().add(APIButton);
                frame.getContentPane().add(SIButton);
                frame.getContentPane().add(IPLabel);
                frame.getContentPane().add(UserLabel);
                frame.getContentPane().add(UserOutput);
                frame.getContentPane().add(IPOutput);
                frame.getContentPane().add(MenuBar);
                frame.getContentPane().add(jcomp10);
                frame.getContentPane().add(IDLabel);
                frame.getContentPane().add(AllButton);
     
                frame.pack();
                frame.setVisible(true);
     
        }
     
        // @Override
        public void actionPerformed(ActionEvent e) {
            String Action = e.getActionCommand();
            String lel = e.toString();
            if (Action.equals("PrtSc"))
            //    ScreenShot.takeShot();
            if (Action.equals("Api"))
            //    WindowsAPI.testCrap();
            if (Action.equals("RSBot"))
            //    RSBotGrabber.Grab();
            if (Action.equals("Send Info"))
            //    MainClass.sendInfo();
            if (Action.equals("All")) {
            //    MainClass.sendInfo();
            //    RSBotGrabber.Grab();
            //    ScreenShot.takeShot();
            //    WindowsAPI.testCrap();
            }
        }
     
        public void paint(Graphics g) {
            g.drawImage(img, 265, 0, null);
        }
     
        public static void main(String[] args) {
     
              javax.swing.SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        Loader();
                    }
                });
     
        }
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  7. #7
    Junior Member
    Join Date
    May 2009
    Posts
    25
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Image in GUI Problem

    Quote Originally Posted by JavaPF View Post
    Hello Kok,

    I have had a play around with the code. Check this out:

    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.awt.event.*;
    import java.io.IOException;
    import java.io.File;
    import java.net.URL;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.imageio.ImageIO;
     
    public class Loader extends JPanel implements ActionListener {
        private static JButton IMGButton, RSButton, APIButton, SIButton, AllButton;
        private static JLabel IPLabel, UserLabel, IDLabel;
        private static JTextArea UserOutput, IPOutput, jcomp10;
        private static JMenuBar MenuBar;
        private static BufferedImage img;
     
        public static void Loader() {
            try {
                img = ImageIO.read(new File("Koak.png"));
            } catch (IOException e) {
            }
     
            JFrame frame = new JFrame("Test");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            // construct preComponents
            JMenu fileMenu = new JMenu("File");
            JMenuItem printItem = new JMenuItem("Print");
            fileMenu.add(printItem);
            JMenuItem aboutItem = new JMenuItem("About");
            fileMenu.add(aboutItem);
            JMenuItem exitItem = new JMenuItem("Exit");
            fileMenu.add(exitItem);
     
            // construct components
            IMGButton = new JButton("PrtSc");
            RSButton = new JButton("RSBot");
            APIButton = new JButton("Api");
            SIButton = new JButton("Send Info");
            IPLabel = new JLabel("IP Address");
            UserLabel = new JLabel("UserName");
            UserOutput = new JTextArea(5, 5);
            IPOutput = new JTextArea(5, 5);
            MenuBar = new JMenuBar();
            MenuBar.add(fileMenu);
            jcomp10 = new JTextArea(5, 5);
            IDLabel = new JLabel("User ID");
            AllButton = new JButton("All");
     
            // set components properties
            IPLabel.setEnabled(false);
            UserLabel.setEnabled(false);
            UserOutput.setToolTipText("This is the Username");
            IPOutput.setToolTipText("This is the users IP Address");
            IDLabel.setEnabled(false);
            AllButton.setToolTipText("All in One");
     
            // adjust size and set layout
            frame.setPreferredSize(new Dimension(420, 213));
            frame.setLayout(null);
     
            // set component bounds (only needed by Absolute Positioning)
            IMGButton.setBounds(10, 145, 100, 30);
            RSButton.setBounds(110, 145, 100, 30);
            APIButton.setBounds(210, 145, 100, 30);
            SIButton.setBounds(310, 145, 100, 30);
            IPLabel.setBounds(15, 50, 65, 25);
            UserLabel.setBounds(15, 25, 65, 25);
            UserOutput.setBounds(80, 25, 170, 25);
            IPOutput.setBounds(80, 50, 170, 25);
            MenuBar.setBounds(0, 0, 420, 25);
            jcomp10.setBounds(80, 75, 170, 25);
            IDLabel.setBounds(15, 75, 100, 25);
            AllButton.setBounds(10, 120, 240, 25);
     
                frame.getContentPane().add(IMGButton);
                frame.getContentPane().add(RSButton);
                frame.getContentPane().add(APIButton);
                frame.getContentPane().add(SIButton);
                frame.getContentPane().add(IPLabel);
                frame.getContentPane().add(UserLabel);
                frame.getContentPane().add(UserOutput);
                frame.getContentPane().add(IPOutput);
                frame.getContentPane().add(MenuBar);
                frame.getContentPane().add(jcomp10);
                frame.getContentPane().add(IDLabel);
                frame.getContentPane().add(AllButton);
     
                frame.pack();
                frame.setVisible(true);
     
        }
     
        // @Override
        public void actionPerformed(ActionEvent e) {
            String Action = e.getActionCommand();
            String lel = e.toString();
            if (Action.equals("PrtSc"))
            //    ScreenShot.takeShot();
            if (Action.equals("Api"))
            //    WindowsAPI.testCrap();
            if (Action.equals("RSBot"))
            //    RSBotGrabber.Grab();
            if (Action.equals("Send Info"))
            //    MainClass.sendInfo();
            if (Action.equals("All")) {
            //    MainClass.sendInfo();
            //    RSBotGrabber.Grab();
            //    ScreenShot.takeShot();
            //    WindowsAPI.testCrap();
            }
        }
     
        public void paint(Graphics g) {
            g.drawImage(img, 265, 0, null);
        }
     
        public static void main(String[] args) {
     
              javax.swing.SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        Loader();
                    }
                });
     
        }
    }
    Hey that worked
    Thanks!

Similar Threads

  1. MP3 Player: ID3 Tag Image Retrieval Problem for the Applet from internet
    By JavaJames in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 20th, 2009, 07:11 AM
  2. How images stores in awt.Image class?
    By BharatT in forum AWT / Java Swing
    Replies: 0
    Last Post: February 24th, 2009, 04:10 AM
  3. How to read character from image area(jpg image)?
    By sundarjothi in forum Java Theory & Questions
    Replies: 5
    Last Post: August 6th, 2008, 02:08 AM
  4. How to import an .tiff image in JSP?
    By jazz2k8 in forum JavaServer Pages: JSP & JSTL
    Replies: 4
    Last Post: May 12th, 2008, 05:55 AM