java - 用鼠标在 Canvas 上画线 : Java awt

标签 java swing graphics awt

尝试使用鼠标在 awt Canvas 上绘制图形(现在是一条线)。我是第一次尝试 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)我想要的是线应该跟随鼠标(拖动时)。最后一行应该从按下点延伸到释放鼠标点。现在,当鼠标移动时,将绘制新的线条。我不确定如何从 canvas 中清除中间线。

有人可以帮我解决这些问题吗?

最佳答案

下面是这种“绘画”的一个简单例子:

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

它会记住所有绘制的形状,只需稍加努力,您就可以扩展它来绘制您喜欢的任何其他形状。

关于java - 用鼠标在 Canvas 上画线 : Java awt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10101673/

相关文章:

java - 时区、日历和 SimpleDateFormat 的奇怪问题

java - SynchronizedMap 并发修改异常

java - 如何移动屏幕上的对象

java - 如何在同一 servlet 请求中使用 getOutputStream() 和 getWriter()?

java - 更改面板的大小

Java JFrame在不可见后使用.setVisible(true)后不会显示

java - getRootPane() 默认按钮 - 这是一个错误吗?

Java 2D 图形 BufferedImage FillRect 问题

graphics - 如何用三角函数而不是向量来处理球与球的碰撞?

graphics - 能否在不损失质量的情况下旋转 JPEG 压缩图像?