I am trying to make a "free-draw" program, like MS paint, with the following code:
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.awt.event.*;
public class Liner extends JComponent{
static int MouseX1, MouseX2, MouseX3, MouseY1, MouseY2, MouseY3, MouseX4, MouseY4, choose, fNumber, lineThickness, size;
static Color colorChoice;
static ArrayList<sPoint> freeDraw = new ArrayList<sPoint>();
static ArrayList<Point> test = new ArrayList<Point>();
static JFrame window = new JFrame();
static Liner shapes = new Liner();
static JButton black = new JButton("black");
static JButton blue = new JButton("blue");
static JButton red = new JButton("red");
static JButton delete = new JButton("clear screen");
static JButton undo = new JButton("undo");
static JSlider thickness = new JSlider(1,50,5);
public static void main(String[] args){
fNumber = 0;
test.add(new Point(1,1));
test.add(new Point(2,2));
test.add(new Point(3,3));
freeDraw.add(new sPoint(test,Color.BLACK,5));
window.add(black);
window.add(blue);
window.add(red);
window.add(delete);
window.add(thickness);
window.add(undo);
black.setVisible(true);
black.setSize(50,50);
black.setLocation(0,0);
black.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e) {
colorChoice = Color.BLACK;
}
});
blue.setVisible(true);
blue.setSize(50,50);
blue.setLocation(0,50);
blue.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e) {
colorChoice = Color.BLUE;;
}
});
red.setVisible(true);
red.setSize(50,50);
red.setLocation(0,100);
red.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e) {
colorChoice = Color.RED;
}
});
delete.setVisible(true);
delete.setSize(110,50);
delete.setLocation(0,150);
delete.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e) {
for(int i=0;i<=fNumber;i++){
freeDraw.get(i).pointers.clear();
}
shapes.repaint();
}
});
undo.setVisible(true);
undo.setSize(50,50);
undo.setLocation(0,200);
undo.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e) {
freeDraw.trimToSize();
size = freeDraw.size();
freeDraw.get(size-1).pointers.clear();
shapes.repaint();
}
});
thickness.setVisible(true);
thickness.setSize(100,20);
thickness.setLocation(0,250);
thickness.addChangeListener(new ChangeListener(){
public void stateChanged(ChangeEvent e){
lineThickness = thickness.getValue();
}
});
lineThickness = 2;
shapes.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e) {
freeDraw.add(new sPoint((new ArrayList<Point>()), colorChoice, lineThickness));
fNumber++;
}
});
shapes.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
freeDraw.get(fNumber).pointers.add(new Point(e.getX(), e.getY()));
shapes.repaint();
}
});
window.add(shapes);
window.setVisible(true);
window.pack();
window.setSize(1000,1000);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint(Graphics g){
Graphics2D g2 = (Graphics2D)g;
for(int a = 0;a<freeDraw.size();a++){
g2.setStroke(new BasicStroke(freeDraw.get(a).thn));
g.setColor(freeDraw.get(a).color);
for(int b = 1;b< freeDraw.get(a).pointers.size();b++){ \\HERE is where the error is- ---- -- ------ -- ------- ---
g.drawLine(freeDraw.get(a).pointers.get(b-1).x,freeDraw.get(a).pointers.get(b-1).y,freeDraw.get(a).pointers.get(b).x,freeDraw.get(a).pointers.get(b).y);
}
}
}
}
And here is the "sPoint" class:
import java.util.ArrayList;
public class sPoint{
ArrayList<Point> pointers;
Color color;
int thn;
public sPoint(ArrayList<Point> pointers, Color color, int thn){
}
}
I keep getting a Null Pointer Exception on the line with the nested for loop in in it; I put a comment in the code where the error is. Any suggestions? I have tried creating an "initial" sPoint object, but that does not fix the problem. I assume it has something to do with an empty array somewhere, but I honestly have no idea. Thanks!