简体   繁体   English

如何让我的图像以随机位置显示?

[英]How can I make my image appear in a random position?

I have a chicken image that only moves whenever I press my arrow keys, the image always appear at the lower left corner when I start to run the program. 我有一个只在我按下箭头键时移动的鸡图像,当我开始运行程序时,图像总是出现在左下角。 My problem is that how can I make the image appear at the top or anywhere on the screen(except on the lower left) and the image still moves when I press the arrow keys . 我的问题是如何让图像显示在屏幕的顶部或任何位置(左下方除外),当我按下箭头键时图像仍然移动。 I'm not really sure but I think this problem is coming from my translate(). 我不太确定,但我认为这个问题来自我的翻译()。 Is there something wrong with my calculation? 我的计算有问题吗? Thanks for sharing your ideas... 感谢您分享您的想法...

Here's the code... 这是代码......

public class Chicken extends Sprite implements ImageObserver
{
private java.awt.Image fishImage;
private final Board board;
private double x;
private double y;
private final double chickenHeight = 1.6;
private final double chickenWidth = 1.8;  
private double speed;
private boolean visible;
private double angle;
private double dx_m;
private double dy_m;
private boolean collision = false;

public Chicken(Board board, String name, double x, double y, double speed)
{
    super(name);

    this.board = board;
    this.x = x;
    this.y = y;
    this.speed = convertToMeterPerSecond(speed);
    visible = true;

    URL iU = this.getClass().getResource("chicken.jpg");
    ImageIcon icon = new ImageIcon(iU);
    chickenImage = icon.getImage();
}

public Image getImage()
{
    return chickenImage;
}

public void keyPressed(KeyEvent e)
{

    int key = e.getKeyCode();

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

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 right_wall = board.x1_world;
     double up_wall = board.y1_world;
     double down_wall = 0.0;
     double left_wall = 0.0;

    x += dx_m;
    y += dy_m;

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

@Override
public void render(Graphics2D g2d)
{
    AffineTransform t = g2d.getTransform();
    final double foot_position_y = chickenHeight;
    final double foot_position_x = chickenWidth / 2;

    double xx = board.convertToPixelX(x - foot_position_x); 
    double yy = board.convertToPixelY(y + foot_position_y);
    g2d.translate(xx, yy);
//        ratio for the actual size of the Image
    double x_expected_pixels = chickenHeight * board.meter;
    double y_expected_pixels = chickenWidth * board.meter;

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

    double x_s = x_expected_pixels / w;
    double y_s = y_expected_pixels / h;
    g2d.scale(x_s, y_s);
    g2d.drawImage(getImage(), (int) x, (int) y, this); 
    g2d.setTransform(t);
}

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

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

@Override
public RectangleX getBounds()
{
    return new RectangleX(x, y, chickenWidth, chickenHeight);
}

@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;
}

}

this is my chicken class 这是我的鸡类

public double meter;
int y1_pixel;

y1_pixel = getHeight(); 

public int convertToPixelX(double distance)
{
    return (int) (distance * meter);
}

public int convertToPixelY(double y_world)
{
    return (int) (y1_pixel - (y_world * meter));
}

this is coming from my board class. 这来自我的董事会。

Your render method is using the x and y coordinates of the class to determine where to draw the Chicken each time. 您的渲染方法使用类的x和y坐标来确定每次绘制鸡的位置。 A simple solution to your problem is to use random values for x and y when you create a Chicken instance. 解决问题的一个简单方法是在创建Chicken实例时使用x和y的随机值。

An alternate solution would be to create another constructor that do not take values for x or y and instead sets them to be default values anywhere between 0 and board.x1_world or board.y1_world, depending on the variable. 另一种解决方案是创建另一个不接受x或y值的构造函数,而是将它们设置为介于0和board.x1_world或board.y1_world之间的默认值,具体取决于变量。

public Chicken(Board board, String name, double speed)
{
    this( board, name,
          (int)( Math.random() * ( board.x1_world - chickenWidth ) ),
          (int)( Math.random() * ( board.y1_world - chickenHeight ) ),
          speed );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM