Drawing lines with the mouse on canvas: Java awt

Trying to enable figure drawing (line at the moment) with the mouse on awt canvas. First I test java graphics. Therefore, I’m not sure how to do it. This is my first attempt:

package def.grafi; import java.awt.Canvas; import java.awt.Frame; import java.awt.Graphics; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class Dra { Frame f = new Frame(); public void disp() { f.setBounds(100, 100, 200, 200); MosL ml = new MosL(); Can c = new Can(); f.add(c); c.addMouseListener(ml); c.addMouseMotionListener(ml); f.setVisible(true); } public static void main(String[] args) { Dra d = new Dra(); d.disp(); } public class MosL extends MouseAdapter { int sx = 0; int sy = 0; boolean onDrag = false; @Override public void mouseDragged(MouseEvent e) { if (onDrag) { int x = e.getX(); int y = e.getY(); Canvas comp = (Canvas) e.getSource(); Graphics g = comp.getGraphics(); // comp.repaint(); << for cleaning up the intermediate lines : doesnt work :( g.drawLine(sx, sy, x, y); return; } onDrag = true; sx = e.getX(); sy = e.getY(); System.out.println("Draggg"); } @Override public void mousePressed(MouseEvent e) { System.out.println("Pressed"); } @Override public void mouseReleased(MouseEvent e) { System.out.println("Released"); if (onDrag) onDrag = false; } } public class Can extends Canvas { @Override public void paint(Graphics g) { } } } 

Problems: 1) When the windows are minimized and restored, the drawn line is gone (due to repainting) 2) I want the line to follow the mouse (when it is being dragged). the end line should extend from the point of click to the point of release of the mouse. Rite now, when the mouse moves, new lines appear. I am not sure how to clear the intermediate lines from the canvas.

Can someone help me solve these problems?

+6
source share
3 answers

Here is a simple example of such a "painting":

 public static void main ( String[] args ) { JFrame paint = new JFrame (); paint.add ( new JComponent () { private List<Shape> shapes = new ArrayList<Shape> (); private Shape currentShape = null; { MouseAdapter mouseAdapter = new MouseAdapter () { public void mousePressed ( MouseEvent e ) { currentShape = new Line2D.Double ( e.getPoint (), e.getPoint () ); shapes.add ( currentShape ); repaint (); } public void mouseDragged ( MouseEvent e ) { Line2D shape = ( Line2D ) currentShape; shape.setLine ( shape.getP1 (), e.getPoint () ); repaint (); } public void mouseReleased ( MouseEvent e ) { currentShape = null; repaint (); } }; addMouseListener ( mouseAdapter ); addMouseMotionListener ( mouseAdapter ); } protected void paintComponent ( Graphics g ) { Graphics2D g2d = ( Graphics2D ) g; g2d.setPaint ( Color.BLACK ); for ( Shape shape : shapes ) { g2d.draw ( shape ); } } } ); paint.setSize ( 500, 500 ); paint.setLocationRelativeTo ( null ); paint.setVisible ( true ); } 

he will remember all the drawn shapes and with little effort you can expand it to draw any other shapes that you like.

+11
source

Use the Line2D object in the AWT package and complete the following steps:

  • Create mouse (X, Y) values ​​for first and second clicks
  • Create a boolean variable to check if the click is first or second
  • Create a List Container Containing Your Line2D
  • Draw them in a Can object
  • Assign values ​​before and after (X, Y) with the mouse listener event

Step 5 can be achieved with:

  • e.getX()
  • e.getY()

Where e is the mouse event and can be connected via the parameter of the mouse listener method.

+4
source

You should use the Line2D object in the awt package, create the x and y values ​​for the first click and second click and a logical determination of whether it is the first or second click. Then create an ArrayList Line2D and draw them in the Can object. That way, you can assign the values ​​before and after x and y to your event in the mouse listener using MouseEvent.getX () and getY ().

+2
source

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


All Articles