如何将图像放在屏幕的左上角?

时间:2011-11-01 03:13:28

标签: java image position coordinates drawimage

我有一个图像,它出现在左下角,我希望它出现在左上角。我已经尝试了不同的x,y坐标,但仍然相同,它没有改变。我知道这不是很难,但我只是感到困惑。请帮我解决这个问题。 huhu ... T_T,提前谢谢。

这是我的代码:

public class DonPaking extends Sprite implements ImageObserver
{
private java.awt.Image donImage;
private final Sea sea;
private double x;
private double y;
private final double donHeight = 1.6;
private final double donWidth = 1.8; 
private double speed;
private boolean visible;
private double angle;
private double dx_m;
private double dy_m;

private boolean collision = false;

public DonPaking(Sea sea, String name, double x, double y, double speed)
{
    super(name);
    this.sea = sea;
    this.x = x;
    this.y = y;
    this.speed = convertToMeterPerSecond(speed);
    visible = true;

    URL iU = this.getClass().getResource("don.jpg");
    ImageIcon icon = new ImageIcon(iU);
    donImage = icon.getImage();

}

public Image getImage()
{
    return donImage;
}

public void keyPressed(KeyEvent e)
{

    int key = e.getKeyCode();

    if (key == KeyEvent.VK_LEFT)
    {
        dx_m = -0.3;
    }
    if (key == KeyEvent.VK_RIGHT)
    {
        dx_m = 0.3;
    }
    if (key == KeyEvent.VK_UP)
    {
        dy_m = 0.3;
    }
    if (key == KeyEvent.VK_DOWN)
    {
        dy_m = -0.3;
    }
}

public void keyReleased(KeyEvent e)
{
    int key = e.getKeyCode();

    if (key == KeyEvent.VK_LEFT)
    {
        dx_m = 0;
    }

    if (key == KeyEvent.VK_RIGHT)
    {
        dx_m = 0;
    }

    if (key == KeyEvent.VK_UP)
    {
        dy_m = 0;
    }

    if (key == KeyEvent.VK_DOWN)
    {
        dy_m = 0;
    }
}

@Override
public void move(long dt)
{
    double dt_s = dt / 1e9;

    final double right_wall = sea.x1_world;
    final double up_wall = sea.y1_world;
    final double down_wall = 0.0;
    final double left_wall = 0.0;

    x += dx_m;
    y += dy_m;


    if (x >= right_wall)
    {
        x = right_wall;
    }
    if (x <= left_wall)
    {
        x = left_wall;
    }

    if (y <= down_wall)
    {
        y = down_wall;
    }

    if (y >= up_wall)
    {
        y = up_wall;
    }

}

@Override
public void render(Graphics2D g2d)
{
    AffineTransform t = g2d.getTransform();

    final double foot_position_y = donHeight;
    final double foot_position_x = donWidth / 2;

    int xx = sea.convertToPixelX(x - foot_position_x); 
    int yy = sea.convertToPixelY(y + foot_position_y); 

    g2d.translate(xx, yy);

    double x_expected_pixels = donHeight * sea.meter;
    double y_expected_pixels = donWidth * sea.meter;

    double w = ((ToolkitImage) donImage).getWidth();
    double h = ((ToolkitImage) donImage).getHeight();

    double x_s = x_expected_pixels / w;
    double y_s = y_expected_pixels / h;

    g2d.scale(x_s, y_s);
    g2d.drawImage(getImage(), 0, 0, this); 

    g2d.setTransform(t);
}

@Override
public void moveAt(double distance_x, double distance_y)
{
    this.x = distance_x;
    this.y = distance_y;
}

public void setAngle(double angle)
{
    this.angle = angle;
}


@Override
public RectangleX getBounds()
{
    return new RectangleX(x, y, donWidth, donHeight);
}

@Override
public double getWidth()
{
    return WIDTH;
}

@Override
public double getHeight()
{
    return HEIGHT;
}

@Override
public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height)
{
    return true;
}


}

1 个答案:

答案 0 :(得分:1)

顶部/左侧的坐标是(0,0)。因此,摆脱translate(..)方法,图像将在您指定的坐标处绘制。