0

I'm using the keylistener to move an image around the screen, and it works. BUT, every time I make the image move in a different direction, the image pauses for maybe 200-500 milliseconds before moving in that direction again. I'm wondering if that's suppose to be normal, if that's just how long it takes for the keyboard input to be sent to the processor, and how long it takes to get to that direction, or whether if I did something wrong in my code. Thanks.

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) {

}

}
Ski
  • 111
  • 2
  • 3
  • 17
  • I think that is normal but I do not know why it does that. I think it is to limit unwanted key presses but I doubt it's the processor. I got an AMD fx-8350 and it pauses so it's not that. – JGerulskis Apr 16 '15 at 20:25
  • check this out http://stackoverflow.com/questions/16328946/java-keylistener-stutters – JGerulskis Apr 16 '15 at 20:28
  • I see. So it's normal. Okay. – Ski Apr 16 '15 at 20:30

0 Answers0