Java Swing redraws when dragging mouse

I find it difficult to get a custom panel to redraw it while I click on it with the mouse.

Basically, I draw free-form strings on the user panel with the following handlers attached:

MouseInputAdapter mia = new MouseInputAdapter() { @Override public void mousePressed(MouseEvent e) { if(_app_split_right_buttons_radioInkBtn.isSelected()) { _app_split_right_journal.StartLine(); _app_split_right_journal.AddLineSegment(e.getX(), e.getY()); } _app_split_right_journal.repaint(); } @Override public void mouseReleased(MouseEvent e) { if(_app_split_right_buttons_radioInkBtn.isSelected()) { _app_split_right_journal.AddLineSegment(e.getX(), e.getY()); _app_split_right_journal.EndLine(); } _app_split_right_journal.repaint(); } @Override public void mouseDragged(MouseEvent e) { if(_app_split_right_buttons_radioInkBtn.isSelected()) { _app_split_right_journal.AddLineSegment(e.getX(), e.getY()); } _app_split_right_journal.repaint(); } }; _app_split_right_journal.addMouseListener(mia); _app_split_right_journal.addMouseMotionListener(mia); 

However, I cannot force it to redraw while I drag the mouse, only after I release it (the line is drawn correctly). Oddly enough, if in the middle of my drag and drop, I right-clicked, the line will start to draw when I drag.

Any help would be appreciated.

Edit: for clarity, the material StartLine () / AddLineSegment () / EndLine () simply adds points to the ArrayList Point objects, and the repaint () function is redefined to cycle through these points and draw line segments between each. Code below:

 public void DrawShapes(Graphics g) { g.setColor(Color.black); for(Geometry.Shape shape : _shapeList) { if(shape instanceof Geometry.Line) { ArrayList<Point> points = ((Line) shape).GetPointList(); Point p1 = points.get(0); for(int i=1; i<points.size(); i++) { Point p = points.get(i); g.drawLine(p1.x, p1.y, px, py); p1 = p; } } } 

Edit: Figured this out. This is because I cycled through the list of lines, but I forgot that I did not add the line that I am currently drawing on this list until I release the mouse and call EndLine (). I had to put separate code in the repaint () method to draw the form, which I am now in the middle of drawing. Code added in repaint ():

 if(_currentShape instanceof Geometry.Line) { ArrayList<Point> points = ((Line)_currentShape).GetPointList(); Point p1 = points.get(0); for(int i=1; i<points.size(); i++) { Point p = points.get(i); g.drawLine(p1.x, p1.y, px, py); p1 = p; } } 
+4
source share
1 answer

This is an example that I made for another question, but it uses drag and drop to draw lines on the component ...

 public class PaintingExample { private BufferedImage bImage; private ImageIcon image; private JLabel imageLabel; private int xClicked = 0; private int yClicked = 0; private int xDragged = 0; private int yDragged = 0; private MouseAdapter mouseListener = new MouseAdapter() { private boolean paint = false; @Override public void mousePressed(MouseEvent me) { xClicked = me.getX(); yClicked = me.getY(); xDragged = xClicked; yDragged = yClicked; paint = true; } @Override public void mouseReleased(MouseEvent e) { xClicked = -1; xClicked = -1; xDragged = -1; yDragged = -1; paint = false; } @Override public void mouseMoved(MouseEvent me) { } @Override public void mouseDragged(MouseEvent me) { if (paint) { xClicked = xDragged; yClicked = yDragged; xDragged = me.getX(); yDragged = me.getY(); xDragged = me.getX(); yDragged = me.getY(); Graphics2D g2 = bImage.createGraphics(); g2.setColor(Color.WHITE); g2.drawLine(xClicked, yClicked, xDragged, yDragged); g2.dispose(); imageLabel.setIcon(new ImageIcon(bImage)); me.getComponent().repaint(); } } }; public PaintingExample() { try { bImage = ImageIO.read(new URL( "http://gagandeepbali.uk.to/" + "gaganisonline/images/planetbackground.jpg")); image = new ImageIcon(bImage); } catch (Exception e) { e.printStackTrace(); } } private void displayGUI() { JFrame frame = new JFrame("Painting on Image"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel contentPane = new JPanel(); imageLabel = new JLabel(image); imageLabel.addMouseListener(mouseListener); imageLabel.addMouseMotionListener(mouseListener); contentPane.add(imageLabel); frame.setContentPane(contentPane); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); } public static void main(String... args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new PaintingExample().displayGUI(); } }); } } 
+5
source

Source: https://habr.com/ru/post/1437877/


All Articles