如何让我的图像不正方形?

时间:2014-02-10 02:14:54

标签: java image japplet

我的朋友和我是java编程的新手,并且正在尝试制作一个简单的游戏。我们的图像透明化了。当我们移动图像时,图像也会划过。任何帮助都会非常多。

    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 个答案:

答案 0 :(得分:2)

这不是您的图片的问题,但是您通过不调用paint来打破super.paint

paint的一项工作是清除准备绘画的Graphics背景。这很重要,因为Graphics上下文是一个共享资源,这意味着在您之前绘制的所有内容仍然位于Graphics上。

绘画的出现有很多原因,反复出现。您应避免在任何paint方法中加载资源,还应避免在任何repaint方法中调用repaint或任何可能调用paint的方法。这会设置一个讨厌的递归循环,最终会消耗你的CPU周期。

最好避免覆盖paint,尤其是顶级容器。相反,你应该将绘制逻辑(可能是你的程序的其余部分)转移到像JPanel这样的东西,然后覆盖它的paintComponent方法,然后在那里进行绘画。

然后,您可以将面板添加到您喜欢的任何顶级容器中,使其更加便携

请查看Performing Custom Painting了解更多详情......