Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By Norm

Thread: Image not displaying in Jframe

  1. #1
    Umlaut is offline Member
    Join Date
    Sep 2011
    Posts
    3
    Rep Power
    0

    Default Image not displaying in Jframe

    Hey, im new to java and really bad a programming in general and im require to make a CAD for designing and testing digital circuits. Ive been given some of this code to follow by and my AND.gif is in the Digital.images package hwoever when i drag and drop my ANDGate.java onto my JFrame in an attempt to view it the program does nothing. Im using NetBeans IDE 7.0.1, cheers.
    Java Code:
    package Digital;
    
    import java.awt.Image;
    
    
    public class ANDGate extends javax.swing.Jpanel{
        private Image image;
    
        private void setSize(int width, int height) {
            throw new UnsupportedOperationException("Not yet implemented");
        }
    
        private static class graphics {
    
            public graphics() {
            }
        }
     
        
    
    
        
        private void paintComponent(graphics g) {
            this.setSize(image.getWidth(null), image.getHeight(null));
            java.net.URL url = getClass().getResource("http://www.java-forums.org/images/AND.gif");
            image = new javax.swing.ImageIcon(url).getImage();
            throw new UnsupportedOperationException("Not yet implemented");
            
        }
        
        public void paintComponent(java.awt.Graphics g)
        {
            g.drawImage(image, 0, 0, null);
        }
    
        
    }
    Last edited by Norm; 09-10-2011 at 04:57 AM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,873
    Rep Power
    25

    Default Re: Image not displaying in Jframe

    when i drag and drop my ANDGate.java onto my JFrame
    I don't understand what this has to do with displaying an image in a java program.

    What happens when you compile and execute your program?

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,873
    Rep Power
    25

    Default Re: Image not displaying in Jframe

    I suggest you attempt to learn java at the beginning.
    The code you posted is mostly useless bits and pieces that have been copied from somewhere and patched together here.
    Fubarable likes this.

  4. #4
    Umlaut is offline Member
    Join Date
    Sep 2011
    Posts
    3
    Rep Power
    0

    Default Re: Image not displaying in Jframe

    Yeah ive been going through Programming and object oriented design using java and its given me an idea of how it all works, just not to the extent that is required. This is the exact document outlining what i'm meant to be doing but i've looked through my text book and it doesnt give me any idea of how to address this.
    Implement a Java class to represent an AND gate. This Java class will have a visual interface, however we will not need to design it using the NetBeans WYSIWYG editors so, simply create a plain Java class named ANDGate and then change its definition so that it extends javax.swing.JPanel.

    To give the AND gate its physical appearance we will load and paint a suitable image file. Firstly create a new Java Package Digital.images and inside that folder add the following image (provided as “AND.gif” in Zip file provided with assignment specification):



    To load this image file at runtime we use the following code:

    java.net.URL url = getClass().getResource("images/AND.gif");
    image = new javax.swing.ImageIcon(url).getImage();

    This is best done just once in the AND gate constructor and stored in a private field. In addition to loading the image, we also want to resize the current component to match the size of the image:

    this.setSize(image.getWidth(null), image.getHeight(null));

    To paint the image to the screen we override the paintComponent method of our base class:

    @Override
    public void paintComponent(java.awt.Graphics g)
    {
    g.drawImage(image, 0, 0, null);
    }

    You can now test the visual properties of this class by creating a JFrame form and dragging ANDGate.java from the Projects window onto the design surface:

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Image not displaying in Jframe

    To understand what to do, you need to understand how to program with the Swing library, and this will take study and effort on your part. Fortunately there's a decent tutorial available that I know has helped me a great deal, and you can find it here: Intro to Swing

    Much luck!
    Last edited by Fubarable; 09-10-2011 at 05:44 AM.

  6. #6
    Umlaut is offline Member
    Join Date
    Sep 2011
    Posts
    3
    Rep Power
    0

    Default Re: Image not displaying in Jframe

    I think you forgot to link the swing tutorial, cheers for this btw.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Image not displaying in Jframe

    Quote Originally Posted by Umlaut View Post
    I think you forgot to link the swing tutorial, cheers for this btw.
    Indeed! Fixed, and thanks!

Similar Threads

  1. Components not displaying on JFrame
    By mDennis10 in forum New To Java
    Replies: 5
    Last Post: 07-27-2011, 08:48 PM
  2. Images not displaying in JFrame.
    By jlennards in forum AWT / Swing
    Replies: 3
    Last Post: 07-12-2011, 04:46 AM
  3. Displaying HashMap in a JFrame
    By ankit1801 in forum New To Java
    Replies: 1
    Last Post: 05-19-2011, 08:54 AM
  4. Displaying an image
    By Samari413 in forum New To Java
    Replies: 10
    Last Post: 11-06-2010, 09:56 PM
  5. JFrame(window) not displaying at all
    By mojisolaowoeye in forum AWT / Swing
    Replies: 1
    Last Post: 07-02-2010, 02:03 PM

Posting Permissions

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