I want to try to put a rectangle on my screen by using JPanel and JFrame. But I get the java.lang.NullPointerException and I don't know why. My code:
Class to create a rectangle:
package com.game.main;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class Rect extends JPanel {
Graphics a;
public Rect(){
a.drawRect(100, 100, 200, 250);
a.setColor(Color.RED);
a.fillRect(100, 100, 200, 250);
}
}
Class to create a JPanel:
package com.game.main;
import javax.swing.JPanel;
public class Inside extends JPanel {
public Inside(){
JPanel content = new JPanel();
Rect rect = new Rect();
content.add(rect);
}
}
Class to create a JFrame:
package com.game.main;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Window extends Main{
public static final int Width = 1020, Height = 860;
public Window(JPanel content){
JFrame frame = new JFrame("The Game");
frame.setContentPane(content);
frame.setSize(Width, Height);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(100, 100);
}
}
Class to put everything together:
package com.game.main;
public class Main {
public static void main(String[] args) {
Inside inside = new Inside();
Window window = new Window(inside);
}
}
I am also not very familiar with the Graphics class, so a little bit of explanation would be wonderful.
Thank you