Hello i am new to java and playing around with some scripts. I have three java files
Main.java
package snake;
import javax.swing.*;
import java.awt.*;
public class Main{
public static void main(String[] args)
{
new board();
}
}
board.java
package snake;
import javax.swing.*;
import java.awt.*;
public class board extends JPanel {
public board()
{
JFrame frame = new JFrame();
frame.setSize(300,400);
frame.setTitle("Snake");
frame.setBackground(Color.black);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
snake component = new snake();
frame.add(component);
frame.setVisible(true);
}
}
Snake.java
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import java.awt.Image;
public class snake extends JComponent {
Image body;
Image head;
int x,y;
public void paint(Graphics g)
{
ImageIcon head_image = new ImageIcon(this.getClass().getResource("reddot.png"));
head = head_image.getImage();
g.drawImage(head, x, y, this);
move();
}
public void move()
{
x +=1;
y +=1;
if (y > 300)
{
y = 0;
x = 0;
}
repaint();
}
}
i am trying to make my red image move across the screen. This happens but a variable speeds when it should be constant. What am i doing wrong?