So, I'm a total Java newb and I wanted to make an animation program but I wanted to do it my own way so I understand how it works, I know there may be other questions like this but I wanted to know the answer using the code I made, so here is my code, the problem is when I run this the box doesn't show up on the window and looking at my prints it stops right before i set the color. Can anyone help?
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import javax.swing.JFrame;
final public class main extends JFrame{
private int x = 20;
private int y = 20;
private boolean right = true;
private boolean left = false;
private boolean down = true;
private boolean up = false;
public static void main(String[] args){
JFrame JFrame = new JFrame("On My Own (kinda)");
JFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JFrame.setVisible(true);
JFrame.setLocation(375, 55);
JFrame.setSize(300, 300);
new main().Animate();
}
public void draw(){
System.out.println("2");
Graphics g = getGraphics();
System.out.println("3");
g.setColor(Color.BLACK);
System.out.println("4");
g.fillRect(x, y, 6, 6);
System.out.println("5");
}
public void Animate(){
System.out.println("1");
while(true){
//Game loop
if(x >= 283){
right = false;
left = true;
}
if(x <= 7){
right = true;
left = false;
}
if(y >= 259){
up = true;
down = false;
}
if(y <= 7){
up = false;
down = true;
}
if(up){
y--;
}
if(down){
y++;
}
if(left){
x--;
}
if(right){
x++;
}
try{
Thread.sleep(10);
} catch (Exception exc){System.out.println("OOOPS");}
draw();
}
}
}
And this is what I get
1
Exception in thread "main" java.lang.NullPointerException
at main.draw(main.java:30)
at main.Animate(main.java:71)
at main.main(main.java:23)
2
3