Search code examples
javaeclipseswingjpanelpaintcomponent

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:

Chemistry program when run from Eclipse

(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...

Chemistry program when run from outside of Eclipse

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:

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:

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);
    }
}

Solution

  • Don't try to read an image in the paintComponent() method!

    More to the point, by the time of deployment, those resources will likely become an . That being the case, the resource must be accessed by URL instead of File. See the info page for the tag, for a way to form an URL.

    Further tips:

    • g.drawImage(image, 45, -10, null); should best be g.drawImage(image, 45, -10, this);
    • Change code of the form catch (Exception e) { .. to catch (Exception e) { e.printStackTrace(); // very informative! ..
    • Don't set the size of top level containers. Instead layout the content & call pack().