Note that there are some explanatory texts on larger screens.

plurals
  1. POFreeHand drawing Java
    primarykey
    data
    text
    <p>I'm trying to create a program that I could draw a freehand drawing. I created an array of points, and it saves the current point of the mouse. The problem is when I release the mouse and then I press it again in another location it draws line between the release point to the pressed point. What should I add or change?</p> <hr> <pre><code>import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Mouse extends JPanel implements MouseListener, MouseMotionListener { private int index = 0; private Point[] arr = new Point[100000]; public Mouse(String name) { super(); index = 0; this.addMouseListener(this); this.addMouseMotionListener(this); JFrame fr = new JFrame(name); fr.add(this); fr.setSize(500, 500); setBackground(Color.green); fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); fr.setVisible(true); } public void paintComponent(Graphics g) { super.paintComponents(g); for (int i = 0; i &lt; index - 1; i++) g.drawLine(arr[i].x, arr[i].y, arr[i + 1].x, arr[i + 1].y); } public void mouseDragged(MouseEvent e) { arr[index] = new Point(e.getX(), e.getY()); index++; System.out.println(index); repaint(); } public void mousePressed(MouseEvent e) { arr[index] = new Point(e.getX(), e.getY()); index++; System.out.println(index); repaint(); } public void mouseExited(MouseEvent e) {} public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mouseMoved(MouseEvent e) {} public static void main(String[] args) { Mouse mouse = new Mouse("Mouse"); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload