2

Ill first start off with my code,

    package flappyBird;

    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Rectangle;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.image.BufferedImage;
    import java.awt.image.ImageObserver;
    import java.io.File;
    import java.io.IOException;
    import java.net.URL;
    import java.util.ArrayList;
    import java.util.Random;
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;

    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.Timer;

    public class FlappyBird extends JPanel implements ActionListener,    MouseListener, KeyListener
 {


public static FlappyBird flappyBird;

public final int WIDTH = 1600, HEIGHT = 800;

public Renderer renderer;

public Rectangle bird;

public ArrayList<Rectangle> columns;

public int ticks, yMotion, score;

public boolean gameOver, started;

public Random rand;

    Image Flappy = Toolkit.getDefaultToolkit().getImage("src/flappybird/flappy.png");
    Image Icon = Toolkit.getDefaultToolkit().getImage("src/flappybird/Icon.png");
    Image Background = Toolkit.getDefaultToolkit().getImage("src/flappybird/background.jpg");
    Image Log = Toolkit.getDefaultToolkit().getImage("src/flappybird/Log.png");
    Image Grass = Toolkit.getDefaultToolkit().getImage("src/flappybird/Grass.png");


public FlappyBird()
{
    JFrame jframe = new JFrame();
    Timer timer = new Timer(20, this);

    renderer = new Renderer();
    rand = new Random(); 

    jframe.add(renderer);
    jframe.setTitle("Missle Launch");
    jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jframe.setSize(WIDTH, HEIGHT);
    jframe.addMouseListener(this);
    jframe.addKeyListener(this);
    jframe.setResizable(false);
            jframe.setIconImage(Icon);
    jframe.setVisible(true);

    bird = new Rectangle(WIDTH / 2 - 10, HEIGHT / 2 - 10, 54, 54);
    columns = new ArrayList<Rectangle>();

    addColumn(true);
    addColumn(true);
    addColumn(true);
    addColumn(true);

    timer.start();
}

public void addColumn(boolean start)
{
    int space = 250;
    int width = 100;
    int height = 50 + rand.nextInt(300);

    if (start)
    {
        columns.add(new Rectangle(WIDTH + width + columns.size() * 300, HEIGHT - height - 120, width, height));
        columns.add(new Rectangle(WIDTH + width + (columns.size() - 1) * 300, 0, width, HEIGHT - height - space));
    }
    else
    {
        columns.add(new Rectangle(columns.get(columns.size() - 1).x + 600, HEIGHT - height - 120, width, height));
        columns.add(new Rectangle(columns.get(columns.size() - 1).x, 0, width, HEIGHT - height - space));
    }
}

public void paintColumn(Graphics g, Rectangle column)
{
    g.setColor(Color.orange.darker().darker());
    g.drawImage(Log, column.x, column.y, column.width, column.height, null);
}
public void jump()
{
    if (gameOver)
    {
        bird = new Rectangle(WIDTH / 2 - 10, HEIGHT / 2 - 10, 20, 20);
        columns.clear();
        yMotion = 0;
        score = 0;

        addColumn(true);
        addColumn(true);
        addColumn(true);
        addColumn(true);

        gameOver = false;
    }

    if (!started)
    {
        started = true;
    }
    else if (!gameOver)
    {
        if (yMotion > 0)
        {
            yMotion = 0;
        }

        yMotion -= 10;
    }
}


@Override
public void actionPerformed(ActionEvent e)
{
    int speed = 10;

    ticks++;

    if (started)
    {
        for (int i = 0; i < columns.size(); i++)
        {
            Rectangle column = columns.get(i);

            column.x -= speed;
        }

        if (ticks % 2 == 0 && yMotion < 15)
        {
            yMotion += 1.5 ;
        }

        for (int i = 0; i < columns.size(); i++)
        {
            Rectangle column = columns.get(i);

            if (column.x + column.width < 0)
            {
                columns.remove(column);

                if (column.y == 0)
                {
                    addColumn(false);
                }
            }
        }


        bird.y += yMotion;

        for (Rectangle column : columns)
        {
            if (column.y == 0 && bird.x + bird.width / 4 > column.x + column.width / 2 - 10 && bird.x + bird.width / 2 < column.x + column.width / 2 + 10)
            {
                score++;
            }

            if (column.intersects(bird))
            {
                gameOver = true;

                if (bird.x <= column.x)
                {
                    bird.x = column.x - bird.width;
                }
                else
                {
                    if (column.y != 0)
                    {
                        bird.y = column.y - bird.height;
                    }
                    else if (bird.y < column.height)
                    {
                        bird.y = column.height;
                    }
                }
            }
        }

        if (bird.y > HEIGHT - 120 || bird.y < 0)
        {
            gameOver = true;
        }

        if (bird.y + yMotion >= HEIGHT - 120)
        {
            bird.y = HEIGHT - 120 - bird.height;
            gameOver = true;
        }
    }

    renderer.repaint();
}

public void repaint(Graphics g)
{
    g.drawImage(Background, 0, 0, null);

            g.drawImage(Grass, 0, 680, null);

            g.drawImage(Flappy, bird.x, bird.y, null);


    for (Rectangle column : columns)
    {
        paintColumn(g, column);
    }

    g.setFont(new Font("Arial", 1, 100));

    if (!started)
    {
                g.setColor(Color.GREEN.darker());
        g.drawString("Click to start!", 475, HEIGHT / 2 - 50);
    }

    if (gameOver)
    {
                g.setColor(Color.RED.darker());
        g.drawString("Game Over!", 500, HEIGHT / 2 - 50);
                    g.drawString("Your score was: " + String.valueOf(score), WIDTH /2 -430 , 100);
    }

    if (!gameOver && started)
    {
                g.setColor(Color.ORANGE);
        g.drawString(String.valueOf(score), WIDTH / 2 - 25, 100);
    }
}

    public void getScore() {
        String.valueOf(score);
    }

public static void main(String[] args)
{
    flappyBird = new FlappyBird();
}

@Override
public void mouseClicked(MouseEvent e)
{
    jump();
}

@Override
public void keyReleased(KeyEvent e)
{
    if (((e.getKeyCode() == KeyEvent.VK_SPACE) || e.getKeyCode() == KeyEvent.VK_UP))
    {
        jump();
    }
}

@Override
public void mousePressed(MouseEvent e)
{
}

@Override
public void mouseReleased(MouseEvent e)
{
}

@Override
public void mouseEntered(MouseEvent e)
{
}

@Override
public void mouseExited(MouseEvent e)
{
}

@Override
public void keyTyped(KeyEvent e)
{

}

@Override
public void keyPressed(KeyEvent e)
{

}

}

Now, i have my src package, then my flappyBird package which contains my main class and my image files, when i test the game before compiling the images are there but afterwards the images are gone, any ideas?

prmottajr
  • 1,816
  • 1
  • 13
  • 22

2 Answers2

0

After compilation and packaging to JAR you have to reffer to your files as to resources. Load your images using ImageIO.read(InputStream). To get your resources as stream simply

FlappyBird.class.getRousourceAsStream(your/package/name/filename.extension)

While your files are "uncomplited" you can reffer to them as files because they can be read by the native filesystem. When they go to jar, filesystem is unable to get that files (in straightforward way).

Antoniossss
  • 31,590
  • 6
  • 57
  • 99
0

After further preview this is the code line i found to run the images:

        String Background = "background.jpg";
        URL imgURL = FlappyBird.class.getResource(Background);
        Toolkit tk = Toolkit.getDefaultToolkit();
        Image bg = tk.getImage(imgURL);

this code is used to replace:

    Image Background = Toolkit.getDefaultToolkit().getImage("src/flappybird/background.jpg");

i just did the same for each i hope this helps other people looking at the same problem.

Question repeat: How do you get an image stored in the same package of your main class to appear after building it into a .jar file. Via paintImages(Graphics, g)