title: How can I make my image appear in a random position?
body: <p>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...  </p>
  
  <p>Here's the code...</p>
  
  <pre><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 &gt;= right_wall)
      {
          x = right_wall;          
      }
      if (y &gt; up_wall)
      {
          y = up_wall;
      }
      if (x &lt;= left_wall)
      {
          x = left_wall;
      }
      if (y &lt; 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;
  }
  
  }
  </code></pre>
  
  <p>this is my chicken class</p>
  
  <pre><code>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));
  }
  </code></pre>
  
  <p>this is coming from my board class.</p>