JAVA画图板04 —— MouseMotionListener

  • MouseMotionListener中发挥功能的函数是MouseDragged,我们在拖动鼠标时,可以实时地画出我们想画的图形。在实现时,需要配合MouseListener监听器中的部分代码。以画直线为例,当我们一开始按下鼠标时就会得起点的坐标(x1,y1),我们需要再得到拖动鼠标时实时的坐标(x,y),然后把第一个坐标和这个不断变化的坐标进行连线。当然,这样画出来的会是很多很多条起点相同的直线,所以还需要把除了最后一条直线以外的线用背景色擦除掉。我们用oldx,oldy来记录上一条线的坐标。代码如下:
public class ShapeListener implements MouseMotionListener
{
	public void mouseMoved(MouseEvent e){}

	int oldx, oldy;	//在MouseReleased中设为0
	public void mouseDragged(MouseEvent e)
	{
		int x = e.getX();
		int y = e.getY();
		
		if(action.equals("直线"))
		{
			if(oldx == oldy == 0)
			{
				oldx = x;
				oldy = y;
			}
			g.setColor(new Color(238, 238, 238));	//用背景色擦除所有的直线
			g.drawLine(x1, y1, oldx, oldy);

			g.setColor(color);
			g.drawLine(x1, y1, x, y);

			oldx = x;
			oldy = y;
		}
		if(action.equals("矩形"))
        {
            if(oldx == 0 && oldy == 0)
            {
                oldx = x;
                oldy = y;
            }
            g.setColor(new Color(238,238,238));
            g.drawRect(Math.min(oldx, x1), Math.min(oldy, y1), Math.abs(oldx - x1), Math.abs(oldy - y1));

            g.setColor(color);
            g.drawRect(Math.min(x, x1), Math.min(y, y1), Math.abs(x - x1), Math.abs(y - y1));

            oldx = x;
            oldy = y;
        }
        if(action.equals("圆"))
        {
            if(oldx == 0 && oldy == 0)
            {
                oldx = x;
                oldy = y;
            }
            g.setColor(new Color(238,238,238));
            g.drawOval(Math.min(oldx, x1), Math.min(oldy, y1), Math.abs(oldx - x1), Math.abs(oldy - y1));

            g.setColor(color);
            g.drawOval(Math.min(x, x1), Math.min(y, y1), Math.abs(x - x1), Math.abs(y - y1));

            oldx = x;
            oldy = y;
        }
		if(action.equals("铅笔"))
        {
           g.drawLine(x1, y1, x, y);
           x1 = x;
           y1 = y;
        }
	}
   
}

  • 最后在MyShapeUI中addMouseMotionListener即可。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java Swing是Java平台的一部分,是一组GUI类库,可以用于创建桌面应用程序。下面是一个简单的Java Swing画图的实现: ```java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DrawingBoard extends JFrame implements ActionListener, MouseListener, MouseMotionListener { private int x, y, x1, y1; private JButton clearButton, redButton, greenButton, blueButton; private JPanel buttonPanel, drawingPanel; private Graphics g; public DrawingBoard() { super("Drawing Board"); setSize(500, 500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); buttonPanel = new JPanel(); clearButton = new JButton("Clear"); redButton = new JButton("Red"); greenButton = new JButton("Green"); blueButton = new JButton("Blue"); buttonPanel.add(clearButton); buttonPanel.add(redButton); buttonPanel.add(greenButton); buttonPanel.add(blueButton); drawingPanel = new JPanel(); drawingPanel.setBackground(Color.WHITE); drawingPanel.addMouseListener(this); drawingPanel.addMouseMotionListener(this); Container contentPane = getContentPane(); contentPane.add(buttonPanel, BorderLayout.NORTH); contentPane.add(drawingPanel, BorderLayout.CENTER); setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource() == clearButton) { g.setColor(Color.WHITE); g.fillRect(0, 0, drawingPanel.getWidth(), drawingPanel.getHeight()); } else if (e.getSource() == redButton) { g.setColor(Color.RED); } else if (e.getSource() == greenButton) { g.setColor(Color.GREEN); } else if (e.getSource() == blueButton) { g.setColor(Color.BLUE); } } public void mousePressed(MouseEvent e) { x = e.getX(); y = e.getY(); } public void mouseDragged(MouseEvent e) { x1 = e.getX(); y1 = e.getY(); g.drawLine(x, y, x1, y1); x = x1; y = y1; } public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mouseMoved(MouseEvent e) {} public static void main(String[] args) { new DrawingBoard(); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值