Results 1 to 10 of 10
  1. #1
    CaptainForge is offline Member
    Join Date
    May 2013
    Posts
    5
    Rep Power
    0

    Default Paint Component not working outside of Eclipse

    I really don't understand this. When I run my program in Eclipse, it looks perfectly fine. It appears below:

    Paint Component not working outside of Eclipse-screenshot2013-05-12at125510pm_zpsb138ef9f.png
    (note that I dragged something over to cover up my full name, since this program is written for a school project. Please ignore that).
    Note that everything shows up

    However, when I run the program outside of eclipse...

    Paint Component not working outside of Eclipse-screenshot2013-05-12at125838pm_zps6de0e5f5.png

    As you can see, any PaintComponent related objects do not show up, but everything else JText and JButton) does show up. JOptionPane message boxes also show up. It might be worth noting that everything that doesn't appear all comes from one JPanel, which doesn't contain anything that does show up.

    Here is the code for the Panel that doesn't appear:

    Java Code:
    package gui;
        import java.awt.FlowLayout;
        import java.awt.Graphics;
        import java.awt.Image;
        import java.io.File;
        import javax.imageio.ImageIO;
        import javax.swing.JPanel;
        
        public class TopPane extends JPanel {
        	public TopPane(){
        		setLayout(new FlowLayout());
        	}
        	
        	public void paintComponent(Graphics g){
        		try{
        		String filename = "logo.jpg";
        		Image image = ImageIO.read(new File(filename));
        		g.drawImage(image, 45, -10, null);
        		
        		String intro = "Program by XXXXXXX XXXX\n";
        		String intro2 =	"The purpose of this program is to make the process of creating a solution";
        		String intro3 = "less painful by performing the calculations for how much solute needs to be\n";
        		String intro4 = "added to the solvent.  Miscellanious additional information will also be provided.\n";
        		String intro5 = "\n";
        		String intro6 = "Please enter infromation in the following format:\n";
        		String intro7 = "<volume> <molarity> <compound>\n";
        		String intro8 = "For example:";
        		String intro9 = "5mL 5M H2SO4";
        		g.drawString(intro, 30, 70);
        		g.drawString(intro2, 30, 95);
        		g.drawString(intro3, 30, 110);
        		g.drawString(intro4, 30, 125);
        		g.drawString(intro5, 30, 140);
        		g.drawString(intro6, 30, 155);
        		g.drawString(intro7, 30, 170);
        		g.drawString(intro8, 30, 195);
        		g.drawString(intro9, 30, 210);
        		}catch(Exception ex){System.out.println("Failed");}
        	}
        }
    Here is the code sample for the class that runs the class that doesn't work when run outside of eclipse:

    Java Code:
     package gui;
        import java.awt.BorderLayout;
        import javax.swing.JFrame;
        
        public class MainFrame extends JFrame{
        	public MainFrame(){
        		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        		setTitle("Chemistry Lab Assistant");
        		setSize(550, 300);
        		
        		//Top Frame
        		TopPane topPane = new TopPane();
        		add(topPane);
        		
        		//Input Pane
        		InputPane inputPane = new InputPane();
        		add(inputPane, BorderLayout.SOUTH);
        	}
        }
    Last edited by CaptainForge; 05-12-2013 at 07:40 PM.

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Paint Component not working outside of Eclipse

    when you say outside eclpise do you mean as a .jar? did you create this jar from the command line?

    Make sure you include all the Class files for the application

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,003
    Blog Entries
    7
    Rep Power
    20

    Default Re: Paint Component not working outside of Eclipse

    Did you forget to mask this line out?

    Java Code:
    String intro = "Program by XXXXXXX XXXX\n";
    kind regards,

    Jos
    Last edited by JosAH; 05-12-2013 at 07:57 PM.
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    CaptainForge is offline Member
    Join Date
    May 2013
    Posts
    5
    Rep Power
    0

    Default Re: Paint Component not working outside of Eclipse

    Quote Originally Posted by JosAH View Post
    Did you forget to mask this line out?

    Java Code:
    String intro = "Program by XXXXXXX XXXX\n";
    kind regards,

    Jos
    Indeed I did, thank you for that. If you wouldn't mind also removing my name from your post, that would be most appreciated :)
    Last edited by JosAH; 05-12-2013 at 07:58 PM.

  5. #5
    CaptainForge is offline Member
    Join Date
    May 2013
    Posts
    5
    Rep Power
    0

    Default Re: Paint Component not working outside of Eclipse

    Quote Originally Posted by monkeyjr97 View Post
    when you say outside eclpise do you mean as a .jar? did you create this jar from the command line?

    Make sure you include all the Class files for the application
    I just ran the class file that contained the main method.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,003
    Blog Entries
    7
    Rep Power
    20

    Default Re: Paint Component not working outside of Eclipse

    Quote Originally Posted by CaptainForge View Post
    I just ran the class file that contained the main method.
    Did you put that image in your .jar file as well?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    CaptainForge is offline Member
    Join Date
    May 2013
    Posts
    5
    Rep Power
    0

    Default Re: Paint Component not working outside of Eclipse

    Quote Originally Posted by JosAH View Post
    Did you put that image in your .jar file as well?

    kind regards,

    Jos


    There is no .jar file. Like I said, all I did was ran the class that contained the main method that was in the bin folder.
    Last edited by CaptainForge; 05-12-2013 at 08:15 PM. Reason: quoting

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,003
    Blog Entries
    7
    Rep Power
    20

    Default Re: Paint Component not working outside of Eclipse

    Quote Originally Posted by CaptainForge View Post
    There is no .jar file. Like I said, all I did was ran the class that contained the main method that was in the bin folder.
    Still then; I highly suspect that your image file can't be found and a NullPointerException is thrown from with the paintComponent( ... ) method before anything is drawn. Stick in a bit of debug code that tests whether or not the image is null.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    CaptainForge is offline Member
    Join Date
    May 2013
    Posts
    5
    Rep Power
    0

    Default Re: Paint Component not working outside of Eclipse

    Quote Originally Posted by JosAH View Post
    Still then; I highly suspect that your image file can't be found and a NullPointerException is thrown from with the paintComponent( ... ) method before anything is drawn. Stick in a bit of debug code that tests whether or not the image is null.

    kind regards,

    Jos
    I did that, and it appears that the line
    Java Code:
    	Image image = ImageIO.read(new File(filename));
    causes an error. Presumably null pointer then? So... how do I go about fixing this?

  10. #10
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Paint Component not working outside of Eclipse

    Make sure it is a null pointer, print the stack trace of your error message:
    Java Code:
    catch (Exception ex) {
    
    ex.printStackTrace();
    
    }
    Also if you think it is a null pointer, make sure your image is in the root directory of the project.

Similar Threads

  1. Replies: 1
    Last Post: 11-05-2012, 07:09 PM
  2. Can't paint my Buffered image on my component?
    By Bram in forum New To Java
    Replies: 2
    Last Post: 06-05-2012, 04:22 PM
  3. Replies: 1
    Last Post: 05-20-2011, 01:59 PM
  4. Replies: 10
    Last Post: 04-11-2011, 09:34 PM
  5. Custom component and paint outside of bounds
    By happy_hippie in forum AWT / Swing
    Replies: 2
    Last Post: 06-30-2010, 05:41 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
  •