Hello everyone,
this my real first OOP game, i used to do all my programs in one class, i followed some tutorials and then tried to remake a pong game that i already made but everything was in one class, but i don’t want to do that anymore, things becomes really complicated when i want to fix or re-do something with it, i always get lost in the code,
so,
here is all the codes for the new pong and the error is :
[quote]Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException
at Screen.paintComponent(Screen.java:28)
[/quote]
Frame.Java
import java.awt.Dimension;
import javax.swing.JFrame;
public class Frame extends JFrame {
public static Dimension size = new Dimension(800,600);
public static String title ="Pong2.0";
public Frame(){
this.setSize(size);
this.setTitle(title);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
init();
}
public void init(){
Screen screen = new Screen();
this.add(screen);
this.setVisible(true);
}
public static void main(String args[]){
Frame frame = new Frame();
}
}
Screen.java
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class Screen extends JPanel implements Runnable {
public static Thread thread = new Thread();
public static Ball ball;
public Screen(){
thread.start();
}
public void define(){
ball = new Ball(0, 0, 50, 50);
}
public void paintComponent(Graphics g){
g.setColor(new Color(0,0,0));
g.fillRect(0, 0, Frame.size.width, Frame.size.height);
g.setColor(new Color(0,50,0));
ball.draw(g);
}
@Override
public void run() {
while(true){
repaint();
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Ball.java
import java.awt.*;
public class Ball extends Rectangle {
public Ball(int x,int y,int width,int height){
this.setBounds(x, y, width, height);
}
public void draw (Graphics g){
g.drawRect( x, y, width,height);
}
}
thank you very much for your help