gpt4 book ai didi

java - java中的画笔效果

转载 作者:行者123 更新时间:2023-12-02 08:30:23 25 4
gpt4 key购买 nike

我想实现java中画笔的效果,鼠标回点,移动鼠标速度更快,线条更细。

最佳答案

这是一个程序,它演示了您想要实现的目标。

这个程序并不完美,但应该可以帮助您开始尝试构建。

import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;

public class PaintBrush {

JFrame f;
Canvas c;
int x=-1, y=-1;

public PaintBrush() {
f = new JFrame();
f.setSize(600, 400);
c = new Canvas() {
public void paint(Graphics g) {
// super.paint(g);
}
};
f.getContentPane().add(c);
c.addMouseMotionListener(new MouseMotionListener() {

@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
}

@Override
public void mouseDragged(MouseEvent e) {
if(x==-1){
x = e.getX();
y = e.getY();
}
int diffx = Math.abs(x - e.getX());
int diffy = Math.abs(y - e.getY());
System.out.println("diffx:"+diffx+"\t"+"diffy:"+diffy);
int speed = (int) Math.sqrt((diffx + diffy));
if(speed>1){
c.getGraphics().fillOval(x, y, 20-speed*2, 20-speed*2);
}else {
c.getGraphics().fillOval(x, y, 20, 20);
}
System.out.print("Speed:"+speed + "\t");
System.out.println("x:"+e.getX());
x = e.getX();
y = e.getY();
}
});
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}

public static void main(String[] args) {
new PaintBrush();

}

关于java - java中的画笔效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3572991/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com