Reputation: 1432
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:
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);
Upvotes: 1
Views: 594
Reputation: 426
I'm assuming that this is occurring because there is what I call: "Invisible Feather", in the image. What this is, is extra invisible pixels which have not been cropped on the outside of the image. Go into an image editing software, and crop the outside of the cherry to fit the image.
You can test this, by drawing the cherry at 0, 0
. If it is not directly in the corner, there is some invisible feather on the outside of your cherry.
EDIT:
I see by the answer above, that I was incorrect about what your problem was. I am going to leave this answer up in case anyone has the problem I have listed.
Upvotes: 2
Reputation: 3625
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
Upvotes: 3