Page 1 of 2 12 LastLast
Results 1 to 20 of 25
Like Tree1Likes

Thread: Adding image to GUI - Whats wrong with code?

  1. #1
    mindsword is offline Member
    Join Date
    Jul 2014
    Location
    Norway
    Posts
    16
    Rep Power
    0

    Default Adding image to GUI - Whats wrong with code?

    Code of file:
    Java Code:
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;
    import javax.swing.JPanel;
    
     public class Frank {
      static JFrame aWindow = new JFrame("This is Frank!");
     public static void main(String[] args) {
      int windowWidth = 444;
      int windowHeight = 444;
        aWindow.add(new JLabel("I am Frank. I am a Robot."));
        aWindow.setBounds(50, 100,windowWidth, windowHeight);
        aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        aWindow.setVisible(true);
        
    public class ImagePanel extends JPanel{
    
        private BufferedImage image;
    
        public ImagePanel() {
           try {                
              image = ImageIO.read(new File("/home/masjok/frank/frank.png"));
    } catch (IOException ex) {
    
    }
    }
    
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(image, 0, 0, null);
    }
    
    }
    }}
    Its messy i know.

    Errors in compiler:
    Java Code:
    gcj /home/masjok/frank/Frank.java --main=Frank
    /home/masjok/frank/Frank.java:22: error: Illegal modifier for the local class ImagePanel; only abstract or final is permitted
    	public class ImagePanel extends JPanel{
    	             ^^^^^^^^^^
    /home/masjok/frank/Frank.java:22: warning: The serializable class ImagePanel does not declare a static final serialVersionUID field of type long
    	public class ImagePanel extends JPanel{
    	             ^^^^^^^^^^
    2 problems (1 error, 1 warning)

    im basically just trying to put the image inside the GUI and centre the text underneath it. Which is hard to believe since the text is above the image in the code

    also do i need all those imports?

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    843
    Rep Power
    6

    Default Re: Adding image to GUI - Whats wrong with code?

    without even compiling you are posting your code in forum. Remove the public in ImagePanel class.
    Start reading the Suntutorial ...its not a sin to gothru ok?
    Ramya:cool:

  3. #3
    mindsword is offline Member
    Join Date
    Jul 2014
    Location
    Norway
    Posts
    16
    Rep Power
    0

    Default Re: Adding image to GUI - Whats wrong with code?

    Quote Originally Posted by RamyaSivakanth View Post
    without even compiling you are posting your code in forum. Remove the public in ImagePanel class.
    Start reading the Suntutorial ...its not a sin to gothru ok?
    You dont become a programmer by reading a book written in a language you dont understand.... (programming language in this case), i have no clue on coding whatsoever, since i made a html website in 6th grade on frontpage......

    I tried removing the public.... I got 5 warnings while compiling, and still no image in the GUI..

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,280
    Rep Power
    6

    Default Re: Adding image to GUI - Whats wrong with code?

    Quote Originally Posted by mindsword View Post
    You dont become a programmer by reading a book written in a language you dont understand....
    You seem to understand English just fine.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: Adding image to GUI - Whats wrong with code?

    The main problem (and you are not alone in this, it's really quite common) is you are trying to do fairly advanced stuff (eg Swing code) before you understand how Java fits together (eg classes, code structure etc).

    Java Code:
        public static void main(String[] args) {
            
            // Snipped its for clarity.
    
            public class ImagePanel extends JPanel{
    
                public ImagePanel() {
    // snip
                }
    
                @Override
                protected void paintComponent(Graphics g) {
    // snip
                }
    
            }
        }
    The above is the structure of your main() method (with fixed formatting).

    You should be able to spot the problem.
    If you cannot then you need to back away from Swing for a bit and do some code that doesn't involve the extra learning overhead of a complex framework.
    gimbal2 likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Matrice is offline Member
    Join Date
    Jul 2014
    Location
    Mililani
    Posts
    37
    Rep Power
    0

    Default Re: Adding image to GUI - Whats wrong with code?

    I would suggest:

    import java.util.*

    This imports everything for you, instead of having to rewrite import.java.whatever, so many times. It's a little redundant. :) But, learners like you and I aren't perfect at this hahaha.

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default Re: Adding image to GUI - Whats wrong with code?

    @Matrice: stop spewing your incorrect 'advice' (mind the quotes); an IDE is very well capable of generating the correct import statements and if you're too lazy to type, programming is not for you.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,280
    Rep Power
    6

    Default Re: Adding image to GUI - Whats wrong with code?

    Quote Originally Posted by JosAH View Post
    @Matrice: stop spewing your incorrect 'advice' (mind the quotes);

    Jos
    I would rather word it differently to better fit ones skill level. 'I suggest' -> 'This is what I do'. Making suggestions and passing out advice is better left to people who have the necessary experience.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    mindsword is offline Member
    Join Date
    Jul 2014
    Location
    Norway
    Posts
    16
    Rep Power
    0

    Default Re: Adding image to GUI - Whats wrong with code?

    Thanks Tolls. Ill take a look at this.

    Matrice: You mean to just replace all the import lines with just the one you provided? or do i add something?

    JosAH: IM on the super advanced Linux IDE called "Text Editor" lol. It even colors certain lines, just like crayons! Totally awesome

    Gimbal: Agreed..... I have no clue what im doing at this point, so advice and reading long books on coding is pretty useless...... My english is just fine, but my Java is beyond baby-stage.

  10. #10
    mindsword is offline Member
    Join Date
    Jul 2014
    Location
    Norway
    Posts
    16
    Rep Power
    0

    Default Re: Adding image to GUI - Whats wrong with code?

    Quote Originally Posted by Matrice View Post
    I would suggest:

    import java.util.*

    This imports everything for you, instead of having to rewrite import.java.whatever, so many times. It's a little redundant. :) But, learners like you and I aren't perfect at this hahaha.
    i dont understand how to get that working with all the utilities.... or whatever... i kept the confusing mass of imports as i got 16 errors with just the pure code u provided lol

    how exactly am i supposed to shorten it?
    Last edited by mindsword; 07-29-2014 at 04:36 PM.

  11. #11
    mindsword is offline Member
    Join Date
    Jul 2014
    Location
    Norway
    Posts
    16
    Rep Power
    0

    Default Re: Adding image to GUI - Whats wrong with code?

    my new code: still wierd, still no image :P

    Java Code:
    import java.util.*
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;
    import javax.swing.JPanel;
     
     public class Frank {
      static JFrame aWindow = new JFrame("This is Frank!");
     public static void main(String[] args) {
      int windowWidth = 444;
      int windowHeight = 444;
        aWindow.add(new JLabel("I am Frank. I am a Robot."));
        aWindow.setBounds(50, 100,windowWidth, windowHeight);
        aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        aWindow.setVisible(true);
         
    public class ImagePanel extends JPanel{
     
        public ImagePanel() {
           try {               
              image = ImageIO.read(new File("/home/masjok/frank/frank.png"));
    } catch (IOException ex) {
     
    }
    }
     
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
    }
     
    }
    }}
    this is frustrating lol......

  12. #12
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,280
    Rep Power
    6

    Default Re: Adding image to GUI - Whats wrong with code?

    That doesn't even compile, for multiple reasons.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  13. #13
    mindsword is offline Member
    Join Date
    Jul 2014
    Location
    Norway
    Posts
    16
    Rep Power
    0

    Default Re: Adding image to GUI - Whats wrong with code?

    Quote Originally Posted by gimbal2 View Post
    That doesn't even compile, for multiple reasons.
    yeah i figured out as much..... care to explain what im doing wrong? i seem to have f'd up the entire code.......

  14. #14
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,280
    Rep Power
    6

    Default Re: Adding image to GUI - Whats wrong with code?

    Yet you posted it claiming that "still nothing happens". I don't know what you are doing, but it isn't programming anymore at this point.

    Take a break perhaps and start over?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  15. #15
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default Re: Adding image to GUI - Whats wrong with code?

    You're not adding your panel to the JFrame and, above all, your panel doesn't draw that image, i.e. all that its paint method does is call the same method in its super class (and that one won't draw your image either).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    mindsword is offline Member
    Join Date
    Jul 2014
    Location
    Norway
    Posts
    16
    Rep Power
    0

    Default Re: Adding image to GUI - Whats wrong with code?

    oh god, im panicking (or maybe its the coffee reacting to the small dosage of benzos. dont worry i have prescription.)

    how do i add the panel to the JFrame, and how do i draw my image?

    Also, did i remove something important? Is there some way to clean up the code? It looks very chaotic, and i still dont know what each part does.

    I dont want to ask anyone to write the whole code for me either....... Even tho it would help alot.................... I need to make this simpler and easier to navigate
    Last edited by mindsword; 07-29-2014 at 06:06 PM.

  17. #17
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: Adding image to GUI - Whats wrong with code?

    1. Indent your code properly, ensuring your braces match up.
    2. Step back from the code and try something that does not involve Swing. Your code still has the same problem it had in your first post, which I highlight in my last post. If you do not understand why that structure is a problem, and a fundamental problem as far as Java is concerned, then you really, really, need to go back to basics.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  18. #18
    mindsword is offline Member
    Join Date
    Jul 2014
    Location
    Norway
    Posts
    16
    Rep Power
    0

    Default Re: Adding image to GUI - Whats wrong with code?

    i thought this was basics

    im just trying to put a picture in a grey box with a caption

    indent? braces? my teeth are in perfect order except for a little acid damage and a few holes... what does my dental record have to do with my java program?

    and i thought i needed swing for GUI?

  19. #19
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default Re: Adding image to GUI - Whats wrong with code?

    Your code has 'depth', i.e. it isn't a (fragment of a) novel: a JFrame needs to have a JLabel added as well as your panel; your panel needs to draw your image. Those are two separate (small) problems that you have to solve.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  20. #20
    mindsword is offline Member
    Join Date
    Jul 2014
    Location
    Norway
    Posts
    16
    Rep Power
    0

    Default Re: Adding image to GUI - Whats wrong with code?

    Quote Originally Posted by JosAH View Post
    Your code has 'depth', i.e. it isn't a (fragment of a) novel: a JFrame needs to have a JLabel added as well as your panel; your panel needs to draw your image. Those are two separate (small) problems that you have to solve.

    kind regards,

    Jos

    This is my progress so far:
    Adding image to GUI - Whats wrong with code?-797.jpg

    How do i fix this? I tried putting the image back in, and the code is a little cleaner... But it doesnt work or compile.........
    Java Code:
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
     
    public class Frank {
      static JFrame aWindow = new JFrame("This is Frank!");
    
    public static void main(String[] args) {
    
      int windowWidth = 444;
      int windowHeight = 444;
        aWindow.add(new JLabel("I am Frank. I am a Robot."));
    
        aWindow.setBounds(50, 100,windowWidth, windowHeight);
        aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        aWindow.setVisible(true);
    
    public class ImagePanel extends JPanel{
      
        public ImagePanel() {
           try {              
              image = ImageIO.read(new File("/home/masjok/frank/frank.png"));
         
    }}}}
    Error:
    /home/masjok/frank/Frank.java:25: error: Syntax error, insert "Finally" to complete BlockStatements
    }}}}
    ^
    /home/masjok/frank/Frank.java:25: error: Syntax error, insert "}" to complete ClassBody
    }}}}
    ^

    What th is it talking about?
    Last edited by mindsword; 07-29-2014 at 07:38 PM.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 3
    Last Post: 05-21-2013, 03:37 PM
  2. whats wrong with my code
    By naved in forum New To Java
    Replies: 5
    Last Post: 07-15-2011, 03:06 AM
  3. whats wrong with this code
    By hamzeeco in forum Java Applets
    Replies: 1
    Last Post: 12-31-2010, 03:19 PM
  4. Whats wrong with this code???
    By javanew in forum New To Java
    Replies: 4
    Last Post: 03-28-2010, 06:46 PM
  5. Whats wrong with my code???
    By Soda in forum New To Java
    Replies: 2
    Last Post: 12-06-2007, 01:54 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
  •