Pregunta

In my main class, I call a class that, upon instantiation, should display its JFrame window. However, that is not the case. I've had this class work before, when I ran the project through Eclipse. Now, running it through command line, it does not work :(.

From my main method:

PaintTitleMovie q = new PaintTitleMovie();

The Jframe class:

public class PaintTitleMovie extends JPanel implements MouseListener {

    Image image;
    Font ConfettiFinal = new Font("Analog", 1, 20); // fail safe
    static JFrame frame = new JFrame();

    public PaintTitleMovie() {
        image = Toolkit.getDefaultToolkit().createImage("src/Title2.gif");
        try {
            Font Confetti = Font.createFont(Font.TRUETYPE_FONT, new File(
                    "src/Fonts/Confetti.ttf"));
            ConfettiFinal = Confetti.deriveFont(1, 50);
        } catch (FontFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        addMouseListener(this);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (image != null) {
            g.drawImage(image, 0, 0, this);
        }
        // draw exit button
        g.setColor(Color.BLUE);
        g.fillRect(990, 50, 210, 100);
        g.setColor(Color.BLACK);
        g.fillRect(1000, 60, 190, 80);
        g.setColor(Color.WHITE);
        g.setFont(ConfettiFinal);
        g.drawString("Continue", 1000, 120);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {

                frame.add(new PaintTitleMovie());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setSize(1200, 800);
                frame.setUndecorated(true);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
                SongTitle s = new SongTitle();
            }
        });
    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
        // TODO Auto-generated method stub
        int x = arg0.getX();
        int y = arg0.getY();

        if (x >= 990 && y >= 50 && y <= 150) {
            this.setVisible(false);
            frame.dispose();
            PaintMenu load = new PaintMenu(); // load paint menu
        }
    }
}
¿Fue útil?

Solución

This src/Title2.gif is going to be problem, the src directory will not exist when the program is built.

Toolkit.getDefaultToolkit().createImage(String) also assumes that the resource is file on the file system, but anything that is contained within the application context (or jar file) is consider an embedded resource and can not be treated as a file.

Instead, you could need to use something more like

image = ImageIO.read(getClass().getResource("/Title2.gif"));

This will return a BufferedImage but will also throw an IOException if the image can not be loaded. If the gif is an animated gif you will need to use something more like

image = new ImageIcon(getClass().getResource("/Title2.gif"));

The same will go for your font, but in that case you will likely need to use

Font Confetti = Font.createFont(Font.TRUETYPE_FONT, getClass().getResourceAsStream(
                "/Fonts/Confetti.ttf"));

If you're using Eclipse, you may need to move these resources out the the src directory and in a "resources" directory at the same level as the src directory in order for them to be included in the final build.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top