Hello. I decided to practice co Swing, Java. I create a simple paint. So, if the position of the mouse is quickly updated, that is, passages in the drawn segment. In the main thread, I read the mouse position in the mouseDragged, and in the other I read from the queue and draw in the buffer (BufferedImage). What techniques can I use?

Draws.

private class DrawThread extends Thread{ public void run() { Point point; int bsize; Graphics buff = buffImage.getGraphics();//BufferedImage while (!closing) { try { if (!pointsList.isEmpty()) { buff.setColor(colorPanel.getCurrentColor()); bsize = brushSizePanel.getBrushSize(); point = pointsList.poll(); buff.fillRect((int) point.getX(), (int) point.getY(), bsize, bsize); } else drawPanel.repaint(); } catch (Exception ex) { ex.getStackTrace(); } } } } 

Saves the position.

  public void mouseDragged(MouseEvent e) { super.mouseMoved(e); if(instrumentPanel.getCurrInstrument()==1) { try { //pointsList: ArrayBlockingQueue<Point> pointsList.put(new Point(e.getX(),e.getY())); } catch (InterruptedException e1) { e1.printStackTrace(); } } } 

drawPanel.paint ()

 @Override public void paint(Graphics g) { super.paint(g); g.drawImage(buffImage,0,0,null); } 
  • one
    Save the position of the previous point and instead of fillRect call drawLine with drawing a line from the previous point to the current one. Then instead of breaks there will be just a straight line. - Morewind
  • Yes, but you need a brush of a different size. 1,2,5 points ... - Romeon0
  • 2
    docs.oracle.com/javase/7/docs/api/java/awt/BasicStroke.html BasicStroke - there you can specify the thickness and other parameters for graphic primitives. - Morewind

0