用鼠标绘制在画布上线:Java的AWT(Drawing lines with mouse on ca

2019-06-23 12:53发布

的尝试,以实现与鼠标的AWT画布上的数字图(现在的线)。 IAM的尝试Java的图形首次。 所以不知道如何去做。 这是我第一次尝试:

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) {

    }
}
}

问题:1)当窗口最小化和恢复,描线消失(因为重绘)2)我想是该行应跟随鼠标(被拖动的时候)。 最后一行应该从按下鼠标的释放点的点延伸。 现在仪式,当鼠标移动,新的生产线越来越绘制。 我不知道如何清理从画布中间线。

有人可以帮助我在这些问题呢?

Answer 1:

这里是这样的“清明上河图”一个简单的例子:

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

它会记住所有的绘制的形状,并用一个小的努力,你可以扩展它来画你喜欢的任何其他形状。



Answer 2:

利用Line2D对象在AWT包并执行以下步骤:

  1. 创建小鼠(X,Y)为第一和第二值的点击次数
  2. 创建一个boolean variable来检查,如果点击是第一或第二
  3. 做一个List容器包含您Line2D对象
  4. 吸引他们的Can对象
  5. 通过鼠标监听的事件分配前后 (X,Y)

步数5可以实现通过:

  1. e.getX()
  2. e.getY()

其中e是鼠标事件,并可能通过鼠标侦听方法的参数被accesed。



Answer 3:

您应该使用Line2D对象在AWT包中,第一次点击和第二点击,和一个布尔值,确定是否是第一或第二点击创建x和y的值。 然后,让Line2D的一个ArrayList,并在你的对象能吸引他们。 所以,那么你可以通过使用MouseEvent.getX()和的getY()你的鼠标监听事件分配前后的X和Y值。



文章来源: Drawing lines with mouse on canvas : Java awt