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); }