JAVA在鼠标点击位置绘制圆,如何在画布上的鼠标单击位置添加节点?

现在我是一个应用程序,让用户单击按钮浏览图片以用作画布背景 . 我想这样做,如果用户点击画布上的某个位置,那么就会放置一个节点 . 我假设我需要获得鼠标坐标 . 是否有一种简单的方法可以调用将节点放在鼠标单击位置,或者我是否必须在此链接中使用该路径:WPF - Drawing on canvas with mouse events?提前致谢 .

编辑:添加了我试图制作椭圆的代码 . 它不会工作,我不知道如何使用鼠标点击椭圆的坐标 . 我知道一行,它只是.x1 = currentPoint.x等 .

画布的XAML代码:

制作帆布背景的代码背后:

private void BrowseButton_Click(object sender, RoutedEventArgs e)

{

OpenFileDialog dlg = new OpenFileDialog();

dlg.InitialDirectory = "c:\\";

dlg.Filter = "Image files (*.jpg)|*.jpg|All Files (*.*)|*.*";

dlg.RestoreDirectory = true;

if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)

{

string selectedFileName = dlg.FileName;

ImageBrush brush = new ImageBrush();

brush.ImageSource = new BitmapImage(new Uri(selectedFileName));

canvas1.Background = brush;

BitmapImage bitmap = new BitmapImage();

}

}

//This is the method for adding the ellipse.

private void addNode_MouseDown(object sender, MouseButtonEventArgs e)

{

Point currentPoint = new Point();

if (e.ButtonState == MouseButtonState.Pressed)

currentPoint = e.GetPosition(this);

Ellipse ellipse = new Ellipse();

SolidColorBrush mySolidColorBrush = new SolidColorBrush();

mySolidColorBrush.Color = Color.FromArgb(255, 255, 255, 0);

ellipse.Fill = mySolidColorBrush;

ellipse.Width = 10;

ellipse.Height = 10;

canvas1.Children.Add(ellipse);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值