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?

I think what you are looking at doing is providing some type of smooth movement of your object across the screen. I would look into doing this with a thread. However before attempting this I would do some research on threading in Java applications (as you said you are new to it).

If you get some more code posted I will be more then happy to assist with problems you encounter.

what exactly do you mean by 'a variable' speeds?

I think he means speed is varying while it was supposed to be constant


Also as Java learner I'm lost with this code
Does it imply JFrame is contained in JPanel? Is that Possible with 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);
            

       }
}

A quick explanation of the code above.

the class board extends JPanel thefore it is a JPanel. In the constructor for board, it creates a JFrame (Window) sets the size of the frame, title, background and close operation (what happens when the window is exited). It then creates the "snake" which is a JComponent (Panel, Button, Label ...) and adds that to the frame. The frame is then made visible.

A quick explanation of the code above.

the class board extends JPanel thefore it is a JPanel. In the constructor for board, it creates a JFrame (Window) sets the size of the frame, title, background and close operation (what happens when the window is exited). It then creates the "snake" which is a JComponent (Panel, Button, Label ...) and adds that to the frame. The frame is then made visible.

Thanks. I didn't knew you cann add JFrame to JPanel. I only knew the opposite

Thanks. I didn't knew you cann add JFrame to JPanel. I only knew the opposite

Its not really doing that. The class is creating the JFrame. In this case the class also happens to be the JPanel :)

Its not really doing that. The class is creating the JFrame. In this case the class also happens to be the JPanel :)

Ooh! I understand now.

This question has already been answered. Start a new discussion instead.