Draw lines on the canvas with the mouse: Java AWT
•
Java
The attempt is to draw a figure (now a line) with the mouse on the AWT canvas I tried java graphics for the first time So 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) { } } }
Problem: 1) when the window is minimized and restored, the drawn line disappears (due to redrawing) 2) what I want is that the line should follow the mouse (when it is dragged) The last line should extend from the pressing point to the mouse release point Rite now, when the mouse moves, the new line will be drawn I'm not sure how to clean the middle line in the canvas
Can someone help me solve these problems?
Solution
This is a simple example of this "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 ); }
It will remember all the shapes drawn, and it can be extended with little effort to draw any other shapes you like
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码