chris edwards chris edwards - 7 months ago 29
Java Question

Java Snake game. Why is this image being drawn in completely the wrong location

I am making a simple Snake game in Java, and i want to use an image rather than a g.drawOval for a cherry on my game board. The cherry's location in randomly generated each time the game loads, or the cherry is eaten.

If i use the g.fillOval method, the cherry gets drawn in the correct location every time, but if i use an image the cherry is being drawn in the incorrect location every time.

Here is an image of the drawn cherry with the EXACT same coordinates (i am using a Point to represent the cherry) but you can see the clear differences in location.

The randomisation of the cherry's location is working as intended, it is a problem with drawing the image

Here is the outcome of my code:

enter image description here

The red dot is the g.fillOval and is the Correct location for the cherry
The Cherry graphic is where my g.drawImage puts the cherry.

The X/Y for the above image is

Cherry X/Y: 49 : 27


This is the code which renders the game:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;

import javax.swing.JPanel;


public class Render extends JPanel {


private static final Color BG = new Color(0);
private static final Color SNAKEBODY = new Color(3380564);
private static final Color SNAKEHEAD = new Color(16724736);

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(BG);
g.fillRect(0, 0, 800, 700);
Snake snake = Snake.snake;
g.setColor(SNAKEBODY);
for (Point point : snake.snakeParts) {
g.fillOval(point.x * Snake.SCALE, point.y * Snake.SCALE,
Snake.SCALE, Snake.SCALE);
}
g.setColor(SNAKEHEAD);
g.fillOval(snake.head.x * Snake.SCALE, snake.head.y * Snake.SCALE,
Snake.SCALE, Snake.SCALE);
g.setColor(Color.RED);

g.drawImage(snake.cherryImg, snake.cherry.x, snake.cherry.y, snake.cherryImg.getWidth(null), snake.cherryImg.getHeight(null), null);
g.fillOval(snake.cherry.x * 10, snake.cherry.y * 10,10, 10);
//debug line
System.out.println("Cherry X/Y: " + snake.cherry.x + " : " + snake.cherry.y);

String string = "Score: " + snake.score + ", Length: "
+ snake.snakeSize;
g.setColor(Color.white);
//simple math to center ish text.
g.drawString(string, (int) (getWidth() / 2 - string.length() * 2.5f),
10);
string = "Game Over!";
if (snake.over){
g.drawString(string,
(int) (getWidth() / 2 - string.length() * 2.5f),
(int) snake.dim.getHeight() / 4);
}
string = "Paused!";
{
if (snake.paused && !snake.over){
g.drawString(string,
(int) (getWidth() / 2 - string.length() * 2.5f),
(int) snake.dim.getHeight() / 4);
}
}
}
}


Can andy body see why there is differences in the drawing on these lines:

g.drawImage(snake.cherryImg, snake.cherry.x, snake.cherry.y,snake.cherryImg.getWidth(null), snake.cherryImg.getHeight(null), null);
g.fillOval(snake.cherry.x * 10, snake.cherry.y * 10, 10, 10);
//debug line
System.out.println("Cherry X/Y: " + snake.cherry.x + " : " + snake.cherry.y);

Answer

Look at your two positions.

Your fill oval is happening at snake.cherry.x*10 while the image is drawn at snake.cherry.x (SAME FOR Y)

Thats why the image is at 49,27 and the dot is at 490,270