-1

My friend and i are very new to java programming and were trying to make a simple game. were having problems making our images transparent. Also the images streak across when we move them. Any help would be verymuch.

    package keelbird;

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.util.TimerTask;
    import javax.imageio.ImageIO;

    public class KeelBird extends JApplet implements KeyListener, ActionListener {
        // establish all key variables
        private static final long serialVersionUID = 1L;
        int changey, changex, changez;
        int dump = 1500;
        int x = 20, y = 180, z = 250, r = 700, hx = 700, hy = 0;
        int gx = 720, gy = 0, ix = 740, iy = 0, jx = 760, jy = 0;
        int qx = dump, qy = dump;
        int j = dump, k = dump;
        int backx = dump, backy = dump;
        int rx = 160, ry = 0, grx = dump, gry = dump;
        int thealth = 5;
        int reload = 0;
        int rely = 0;
        javax.swing.Timer timer;
         BufferedImage yo, bird, explode, health, win, green, red, relImage;

         public void init() {
            // initialize applet processes, create applet
            start();
           setSize(800, 400);
           setFocusable(true);
            setFocusTraversalKeysEnabled(true);
           addKeyListener(this);
          repaint();
       }

public void start() {
    // start timer
    if (timer == null) {
        timer = new javax.swing.Timer(2, this);
        timer.start();
    } else {
        timer.restart();
    }
}
public void fire() {
    // fire missle method
    for(int j = 0; j < 100; j++) {
        changex = 4;
        repaint();
    }
}

public void refire() {
    // bring missle back
    x = 20;
    y = 180;
    backx = 0;
    backy = 0;
    changex = 0;
    System.out.println("RELOAD");
    repaint();
}

public void paint(Graphics g) {
    // paint graphics
    int width = 80;
    int height = 40;
    yo = null;
    bird = null;
    explode = null;
    health = null;
    win = null;
    BufferedImage background = null;
    try {
        File u = new File("the missel.png");
        yo = ImageIO.read(u);
        File w = new File("the dang bird.png");
        bird = ImageIO.read(w);
        File v = new File("explotions.png");
        explode = ImageIO.read(v);
        File t = new File("killdatbird.png");
        background = ImageIO.read(t);
        File h = new File("healthbar.png");
        health = ImageIO.read(h);
        File q = new File("winner.png");
        win = ImageIO.read(q);
        File re = new File("red.png");
        red = ImageIO.read(re);
        File gre = new File("green.png");
        green = ImageIO.read(gre);
        File rel = new File("reload.png");
        relImage = ImageIO.read(rel);
    } catch (IOException e) {
        System.err.println("You made a Boo-boo");
    }
    g.drawImage(relImage, 20, rely, 40, 16, null);
    g.drawImage(red, rx, ry, 80, 20, null);
    g.drawImage(green, grx, gry, 80, 20, null);
    g.drawImage(background, backx, backy, 800, 400, null);
    g.drawImage(win, qx, qy, 800, 400, null);
    g.drawImage(yo, x , y, width, height, null);
    g.drawImage(bird, r, z, 40, 40, null);
    g.drawImage(explode, j, k, 95, 95, null);
    g.drawImage(health, hx,hy , 20, 10, this);
    g.drawImage(health, gx,gy , 20, 10, this);
    g.drawImage(health, ix,iy , 20, 10, this);
    g.drawImage(health, jx,jy , 20, 10, this);
    repaint();
}

public void keyPressed(KeyEvent e) {
    // move missle up and down
    if (e.getKeyCode() == KeyEvent.VK_UP) {
        changey = -1;
    }
    if (e.getKeyCode() == KeyEvent.VK_DOWN) {
        changey = 1;
    }
    // fire the missle
    if (e.getKeyCode() == KeyEvent.VK_ENTER) {
        fire();
    }
    // allow reloading of missle
    if (reload == 0) {
        if(e.getKeyCode() == KeyEvent.VK_R) {
            // do nothing
        }
    }
    if (reload == 1) {
        if (e.getKeyCode() == KeyEvent.VK_R) {
        refire();
        java.util.Timer tim = new java.util.Timer();
        tim.schedule(new TimerTask() {
        public void run() {
        backx = dump;
        backy = dump;
        repaint();
        reload = 0;
        }
        }, 100);
        if (thealth == 0) {
            thealth = 5;
            qx = dump;
            qy = dump;
            hx = 700;
            hy = 0;
            gx = 720;
            gy = 0; 
            ix = 740; 
            iy = 0; 
            jx = 760; 
            jy = 0;
        }
    }
    }
    repaint(1);
}

public void keyTyped(KeyEvent e) {
    // do nothing
}

public void keyReleased(KeyEvent e) {
    // no change in missle position if key is released
    changey = 0;
}

public void actionPerformed(ActionEvent e) {
    changez = (int) (((Math.random() * 4) - 2) * 2);
    // set bounds for missle
    if (y > 400) {
        changey = 0;
        y = 400;
    }
    if (y < 0 ) {
        changey = 0;
        y = 0;
    }
    if (x > 700) {
        changex = 0;
    }
    // if max is hit
    if (x == 680) {
        j = x;
        k = y - 20;
        x = dump;
        y = dump;
        changex = 0;
        java.util.Timer time = new java.util.Timer();
            time.schedule(new TimerTask()
             {
                public void run() {
                j = dump;
                k = dump;
                repaint();
                reload = 1;
            }}, 1000);
    }
    // set bounds for bird
    if (z < 0) {
        z = 0;
        changez = 0;
    }
    if (z > 370) {
        z = 370;
        changez = 0;
    }
    if (reload == 0) {
        rx = 160;
        ry = 0;
        grx = dump;
        gry = dump;
        repaint();
    }
    if (reload == 1) {
        grx = 160;
        gry = 0;
        rx = dump;
        ry = dump;
        repaint();
    }
    // if bird is hit
    if (Math.abs(k - z) <= 30) {
        if(Math.abs(j - 700) <= 30) {
            // hit casts missle and bird away
            changez = 0;
            x = dump;
            y = dump;
            z = dump;
            r = dump;
            // initiate timer after hit
            java.util.Timer time = new java.util.Timer();
            time.schedule(new TimerTask()
             {
                public void run() {
                // throw explotion to side
                j = dump;
                k = dump;
                // bird respawns
                z = 250;
                r = 700;
                if (thealth == 4){
                    jx = dump;
                    jy = dump;
                }
                if (thealth == 3){
                    ix = dump;
                    iy = dump;
                }
                if (thealth == 2){
                    gx = dump;
                    gy = dump;
                }
                if (thealth == 1){
                    hx = dump;
                    hy = dump;
                }
                if (thealth == 0) {
                    qx = 0;
                    qy = 0;
                    grx = dump;
                    gry = dump;
                    rx = dump;
                    ry = dump;
                    rely = dump;
                }
                System.out.println("GOOD JOB!!!");
                repaint();
                reload = 1;
            }},
            1000);
            thealth = thealth - 1;
            repaint();
        } 
    }
    // constant changes in position
    x = changex + x;
    y = changey + y;
    z = changez + z;
    repaint(1);
}

}

1 Answers1

2

This is not an issue with your images but the fact that you have broken the paint chain by not calling super.paint

One of the jobs of paint is to clear the Graphics context ready for painting. This is important as the Graphics context is a shared resource, meaning that everything that was painted before you will still be on the Graphics.

Painting occurs for any number of reasons, repeatedly. You should avoid loading resources within any paint method, you should also avoid calling repaint or any method that might call repaint from within any paint method. This sets up a nasty recursive loop that will eventually consume your CPU cycles.

Preferably, you should avoid overriding paint, especially of top level containers. Instead, you should move you paint logic (and probably the rest of your program) to something like a JPanel and override it's paintComponent method instead and do your painting there.

You can then add the panel to whatever top level container you like, making it much more portable

Take a look at Performing Custom Painting for more details...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366