0
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.imageio.event.IIOReadProgressListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class General implements Runnable, KeyListener{
    
    JFrame play_frame;
    JPanel panel;
    JButton start_button;
    Thread thread;
    boolean running = false;
    JFrame frame;
    Canvas canvas;
    Graphics g;
    BufferStrategy bs;
    int width;
    int height;
    int b_x = 80;
    int b_y = 300;
    int up_x;
    int up_y;
    int down_x = 1500;
    int down_y = 300;
    
    public void play() {
        play_frame = new JFrame("Start");
        panel = new JPanel();
        start_button = new JButton("Play!");
        start_button.setBounds(300/2 - 60, 300/2 - 20, 120, 40);
        start_button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                start();
                play_frame.setVisible(false);
                play_frame.dispose();
            }   
        });
        panel.setLayout(null);
        panel.add(start_button);
        play_frame.add(panel);      
        play_frame.setSize(300,300);
        play_frame.setResizable(false);
        play_frame.setLocationRelativeTo(null);
        play_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        play_frame.setVisible(true);
        
        
    }
    public void display(String title, int width, int height) {
        this.width = width;
        this.height = height;
        frame = new JFrame(title);
        frame.setSize(width, height);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setVisible(true);
        frame.addKeyListener(this);
        frame.setFocusable(true);
        canvas = new Canvas();
        canvas.setPreferredSize(new Dimension(width, height));
        canvas.setMaximumSize(new Dimension(width, height));
        canvas.setMinimumSize(new Dimension(width, height));
        frame.add(canvas);
        frame.pack();
    }
    int speed = 5;
    public void init() {
        display("Igrica", 800, 600);
    }
    public void death() {
        if(b_x + 80 > up_x && b_x < (up_x + 100)) {
            if(b_y + 57> down_y || b_y < (down_y - 180)) {
                frame.setVisible(false);
                stop();
            }
        }
        if(b_y + 57 >= 520) {
            frame.setVisible(false);
            stop();
        }
    }
    public void speed_up() {
        if(score == 10) {
            speed = 10;
        }
        if(score == 20) {
            speed = 15;
        }
        if(score == 30) {
            speed = 20;
        }
    }
    int score = 0;
    public void score() {
        g.setColor(Color.white);
        g.setFont(new Font("TimesRoman", Font.PLAIN, 20));
        g.drawString(String.valueOf(score), 700, 30);
    }
    public void moving() {
        if (down_x <= -100) {
            down_x = 800;
            down_y = (int) ((Math.random() * (500 - 160)) + 160);
            score += 1;
        }
        down_x -= speed;
        up_x = down_x;
        up_y = (down_y - 1000) - 180;
    }
    public synchronized void start() {
        if(running) {
            return;
        }
        thread = new Thread(this);
        thread.start();
        running = true;
    }
    public synchronized void stop() {
        if(!running) {
            return;
        }
        try {
            thread.join();
            running = false;
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public void bg() {
        try {
            BufferedImage ground = ImageIO.read(new File("textures/grass.png"));
            g.setColor(new Color(3, 165, 252));
            g.fillRect(0, 0, width, height);
            g.drawImage(ground, 0,520, null);
            g.drawImage(ground, 80, 520, null);
            g.drawImage(ground, 160, 520, null);
            g.drawImage(ground, 240, 520, null);
            g.drawImage(ground, 320, 520, null);
            g.drawImage(ground, 400, 520, null);
            g.drawImage(ground, 480, 520, null);
            g.drawImage(ground, 560, 520, null);
            g.drawImage(ground, 640, 520, null);
            g.drawImage(ground, 720, 520, null);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void ptica() {
        try {
            BufferedImage ptica = ImageIO.read(new File("textures/ptica.png"));
            g.drawImage(ptica, b_x, b_y, null);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void drawing() {
        bg();
        ptica();
        down();
        up();
        score();
    }
    public void down() {
        g.setColor(new Color(0, 133, 11));
        g.fillRect(down_x, down_y, 100, 800);
    }
    public void up() {
        g.setColor(new Color(0, 133, 11));
        g.fillRect(up_x, up_y, 100, 1000);
    }
    public void tick() {
        speed_up();  
        jump();
        g();
        moving();
        death();
    }
    public void g() {
        b_y += 3;
    }
    int jump_s = 0;
    boolean jumping = false;
    public void jump() {
        b_y -= jump_s;
        if(jump_s > 0) {
            jump_s -= 1;
        }
    }
    public void render() {
        bs = canvas.getBufferStrategy(); 
        if(bs == null) {
            canvas.createBufferStrategy(3);
            return;
        }
        g = bs.getDrawGraphics();
        //Draw here
        drawing();
        //To here
        bs.show();
        g.dispose();
    }
    
    public void run() {
        init();
        int fps = 60;
        double timePerTick = 1000000000 / fps;
        double delta = 0;
        long now;
        long lastTime = System.nanoTime();
        
        while(running) {
            
            now = System.nanoTime();
            delta += (now - lastTime) / timePerTick;
            lastTime = now;
            
            if(delta >= 1) {
                tick();
                render();
                delta --;
            }
        }
    }
    @Override
    public void keyTyped(KeyEvent e) {

    }
    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_SPACE) {
            if(jump_s == 0) {
                jump_s = 15;
            }
        }
    }
    @Override
    public void keyReleased(KeyEvent e) {
        
    }   
}

So, this is my code. It works perfectly fine when I run it in Eclipse, but when I export it, it doesn't load textures, but instead, I see flashing black and white. I know that only textures are not loaded, because other graphic methods, such as fillRect, are working, and are displaying over flashing black and white. What do I do?

gavrio55
  • 33
  • 6

0 Answers0