I am writing simple Paint program, where you paint whatever you want by dragging the mouse. You can change the color and the size of the brush, but in this version, when I change the color or the size of the brush, everything painted before is changed too when I start to draw again by dragging the mouse. I have tried with getGraphics method in the paintComponent method, but I probably did it wrong way because it did not help me.. Any ideas how to deal with this issue? Thank you.
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JPanel;
public class PaintPanel extends JPanel{
private int pointCount = 0;
private Point points[] = new Point[10000];
private Color currentColor;
private int pointSize;
public PaintPanel(){
setBackground(Color.WHITE);
setDefaultColor();
setDefaultPointSize();
addMouseMotionListener(
new MouseMotionAdapter() {
public void mouseDragged(MouseEvent event){
if(pointCount < points.length){
points[pointCount] = event.getPoint();
pointCount++;
repaint();
}
}
}
);
}
public void setColor(Color newColor){
currentColor = newColor;
}
public void setDefaultColor(){
currentColor = Color.BLACK;
}
public void setPointSize(int size){
pointSize = size;
}
public void setDefaultPointSize(){
pointSize = 6;
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(currentColor);
for(int i = 0; i < pointCount; i++)
g.fillOval(points[i].x,points[i].y,pointSize,pointSize);
}
}
Any option to do it without Collections?