In my app, I have a JPanel where I can paint. This is done by recording three points from the mouse clicked and mouse dragged events and then drawing a polyline between those three points. But if I use my tablet to draw, the lines aren't very smooth. The biggest problem I see is that if I quickly stroke my pen, the program won't even draw the line. If I go a bit slow, then it draws the line, but it lags behind so I can't see the line as it's being drawn. How can I make this smoother, like in one note where it will draw my lines no matter how fast I stroke my pen?
Here is the code I've been using, incase someone needs its
public class ScratchPad extends JPanel {
private Image image;
private Graphics2D graphics;
private ArrayList<Integer[]> coordinates = new ArrayList<>();
public ScratchPad() {
setPreferredSize(new Dimension(1000, 500));
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent me) {
Integer[] coord = {me.getX(), me.getY()};
coordinates.add(coord);
}
@Override
public void mouseReleased(MouseEvent me) {
coordinates.clear();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent me) {
Integer[] coord = {me.getX(), me.getY()};
coordinates.add(coord);
if (coordinates.size() >= 3) {
if (graphics != null) {
drawLine();
repaint();
}
}
}
});
}
private void drawLine() {
int[] x = {coordinates.get(0)[0], coordinates.get(1)[0], coordinates.get(2)[0]};
int[] y = {coordinates.get(0)[1], coordinates.get(1)[1], coordinates.get(2)[1]};
graphics.drawPolyline(x, y, 3);
Integer[] temp = {coordinates.get(2)[0], coordinates.get(2)[1]};
coordinates.clear();
coordinates.add(temp);
}
@Override
protected void paintComponent(Graphics g) {
if (image == null) {
// image to draw null ==> we create
image = createImage(getSize().width, getSize().height);
graphics = (Graphics2D) image.getGraphics();
// enable antialiasing
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// clear draw area
clear();
}
g.drawImage(image, 0, 0, null);
}
public void clear() {
graphics.setPaint(Color.white);
// draw white on entire draw area to clear
graphics.fillRect(0, 0, getSize().width, getSize().height);
graphics.setPaint(Color.black);
repaint();
}
}
EDIT: Image added to show what's happening. Code for this image is taken MadProgrammer's second example in the comments below. The main issue here is that Java isn't detecting a portion of mouse drag. I drew the vertical line first, then from the top of vertical line, I started to draw the horizontal line. As you can see in the image, the horizontal line did not start immediately from the vertical line; it left a gap. This gap only shows up when I try to draw with my Wacom tablet, not with the mouse. This is the main issue I am trying to resolve at this point. I will be writing Japanese Kanji in this draw tool, which has small strokes that do not show up at all! I have to make them unnecessarily big so Java can draw them.
