图像暂停200-500毫秒

时间:2015-04-16 20:22:08

标签: java image graphics game-physics keylistener

我正在使用keylistener在屏幕上移动图像,并且它可以正常工作。但是,每次我使图像向不同方向移动时,图像会暂停200-500毫秒,然后再向那个方向移动。我想知道这是否正常,如果这只是将键盘输入发送到处理器需要多长时间,以及到达那个方向需要多长时间,或者我是否在代码中做错了。谢谢。

public class Fishy extends Panel implements KeyListener, MouseMotionListener {
int x1;// first x location of the shape.
int x2;// second x location of the shape
int y1;// first y location of the shape
int y2;// second y location of the shape
int shapeWidth;// width of the shape
int shapeHeight;// height of the shape
static final int left = 37;
static final int right = 39;
static final int down = 40;
static final int up = 38;
static boolean leftPress;
static boolean rightPress;
static boolean upPress;
static boolean downPress;
static int speed = 3;
static int x;
static int y;
static int size = 50;

static Image LimageFishy = new ImageIcon("E://Picture//Lfish.png").getImage();
static Image RimageFishy = new ImageIcon("E://Picture//Rfish.png").getImage();
static BufferedImage bufferedImageFishy;

// tatic int wolfWidth = wolf.getWidth(null);
// static int wolfHeight = wolf.getHeight(null);
// TREE method of extending classes[]

static Fishy fishy = new Fishy(500, 500, 9);

Fishy(int width, int length, int minusBy) {
    super(width, length, minusBy);
}

/* Graphics goes here */
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.BLACK);
    g.fillRect(0, 0, 500, 500);
    size++;
    g.drawImage(RimageFishy, x1, y1, size, 50, null);

    g.drawString(DraggingShapes.mousePointerLocation, 50, 50);

}

public static void main(String args[]) throws IOException {

    JFrame frame = new JFrame("Fishy");
    frame.addKeyListener(fishy);
    fishy.addMouseMotionListener(fishy);
    fishy.x1 = 50;
    fishy.y1 = 50;

    // bufferedImageFishy = toBufferedImage(imageFishy);
    // System.out.println(bufferedImageFishy.getHeight());
    // imageFishy = getScaledInstance(bufferedImageFishy, 50, 50, null, true);

    frame.add(fishy);
    Frame.showFrame(frame, false);

}

/**************************************************/
public static BufferedImage getScaledInstance(BufferedImage img, int targetWidth, int targetHeight, Object hint, boolean higherQuality) {
    int type = (img.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
    BufferedImage ret = (BufferedImage) img;
    int w, h;
    if (higherQuality) {
        // Use multi-step technique: start with original size, then
        // scale down in multiple passes with drawImage()
        // until the target size is reached
        w = img.getWidth();
        h = img.getHeight();
    } else {
        // Use one-step technique: scale directly from original
        // size to target size with a single drawImage() call
        w = targetWidth;
        h = targetHeight;
    }

    do {
        if (higherQuality && w > targetWidth) {
            w /= 2;
            if (w < targetWidth) {
                w = targetWidth;
            }
        }

        if (higherQuality && h > targetHeight) {
            h /= 2;
            if (h < targetHeight) {
                h = targetHeight;
            }
        }

        BufferedImage tmp = new BufferedImage(w, h, type);
        Graphics2D g2 = tmp.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
        g2.drawImage(ret, 0, 0, w, h, null);
        g2.dispose();

        ret = tmp;
    } while (w != targetWidth || h != targetHeight);

    return ret;
}

/**************************************************/
public static BufferedImage toBufferedImage(Image img) {
    if (img instanceof BufferedImage) {
        return (BufferedImage) img;
    }

    // Create a buffered image with transparency
    // img.getWidth(null)
    // img.getHeight(null);
    BufferedImage bimage = new BufferedImage(50, 50, BufferedImage.TYPE_INT_ARGB);

    // Draw the image on to the buffered image
    Graphics2D bGr = bimage.createGraphics();
    bGr.drawImage(img, 0, 0, null);
    bGr.dispose();

    // Return the buffered image
    return bimage;
}

/**************************************************/

@Override
public void keyPressed(KeyEvent e) {

    if (up == e.getKeyCode()) {

        upPress = true;
    }
    if (down == e.getKeyCode()) {

        downPress = true;
    }
    if (left == e.getKeyCode()) {

        leftPress = true;
    }
    if (right == e.getKeyCode()) {

        rightPress = true;
    }

    /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

    if (upPress == true && leftPress == false && rightPress == false) {
        fishy.y1 -= speed;
    }
    if (downPress == true && leftPress == false && rightPress == false) {
        fishy.y1 += speed;
    }
    if (leftPress == true && upPress == false && downPress == false) {
        fishy.x1 -= speed;
    }
    if (rightPress == true && upPress == false && downPress == false) {
        fishy.x1 += speed;
    }

    /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

    if (leftPress == true && upPress == true) {
        fishy.x1 -= speed;
        fishy.y1 -= speed;
    }
    if (leftPress == true && downPress == true) {
        fishy.x1 -= speed;
        fishy.y1 += speed;
    }
    if (rightPress == true && upPress == true) {
        fishy.x1 += speed;
        fishy.y1 -= speed;
    }
    if (rightPress == true && downPress == true) {
        fishy.x1 += speed;
        fishy.y1 += speed;
    }

    System.out.println("PRESSING (Left:" + leftPress + ") (Right:" + rightPress + ") (Up:" + upPress + ") (Down:" + downPress + ")");
    repaint();

}

@Override
public void keyReleased(KeyEvent e) {

    if (up == e.getKeyCode()) {
        upPress = false;
    }
    if (down == e.getKeyCode()) {
        downPress = false;
    }
    if (left == e.getKeyCode()) {
        leftPress = false;
    }
    if (right == e.getKeyCode()) {
        rightPress = false;
    }

}

@Override
public void mouseMoved(MouseEvent e) {
    DraggingShapes.showPointerLocation(e.getX(), e.getY());
    repaint();

}

/*** METHODS THAT WE'RE NOT GOING TO USE ***/
@Override
public void keyTyped(KeyEvent e) {

}

@Override
public void mouseDragged(MouseEvent e) {

}

}

0 个答案:

没有答案