小弟菜鸟一枚java鼠标跟随问题,我想让下面代码在鼠标“快速”移动时画完整的线,不是断断续续的哟!
packagecom.jlchaoshi.test;importjava.awt.Color;importjava.awt.Graphics;importjava.awt...
package com.jlchaoshi.test;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JFrame;
public class MouseDrawLine extends JFrame {
int x, y;
int endX, endY;
public MouseDrawLine() {
addMouseListener(new MouseAdapter() {
//鼠标下压事件
public void mousePressed(MouseEvent e) {
x = e.getX();
y = e.getY();
}
//鼠标弹起事件
public void mouseReleased(MouseEvent e) {
System.out.println("鼠标弹起事件");
}
});
addMouseMotionListener(new MouseMotionAdapter() {
//鼠标下压移动事件
public void mouseDragged(MouseEvent e) {
endX = e.getX();
endY = e.getY();
repaint();
x = endX+1;
y = endY+1;
}
});
setSize(600, 500);
setLocation(200,100);
setVisible(true);
}
public void paint(Graphics g) {
g.setColor(Color.black);
g.drawLine(x, y, endX, endY);
}
public static void main(String[] args) {
new MouseDrawLine();
}
} 展开
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JFrame;
public class MouseDrawLine extends JFrame {
int x, y;
int endX, endY;
public MouseDrawLine() {
addMouseListener(new MouseAdapter() {
//鼠标下压事件
public void mousePressed(MouseEvent e) {
x = e.getX();
y = e.getY();
}
//鼠标弹起事件
public void mouseReleased(MouseEvent e) {
System.out.println("鼠标弹起事件");
}
});
addMouseMotionListener(new MouseMotionAdapter() {
//鼠标下压移动事件
public void mouseDragged(MouseEvent e) {
endX = e.getX();
endY = e.getY();
repaint();
x = endX+1;
y = endY+1;
}
});
setSize(600, 500);
setLocation(200,100);
setVisible(true);
}
public void paint(Graphics g) {
g.setColor(Color.black);
g.drawLine(x, y, endX, endY);
}
public static void main(String[] args) {
new MouseDrawLine();
}
} 展开
展开全部
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JFrame;
public class MouseDrawLine extends JFrame {
int x, y;
int endX, endY;
public MouseDrawLine() {
addMouseListener(new MouseAdapter() {
//鼠标下压事件
@Override
public void mousePressed(MouseEvent e) {
x = e.getX();
y = e.getY();
}
//鼠标弹起事件
@Override
public void mouseReleased(MouseEvent e) {
System.out.println("鼠标弹起事件");
}
});
addMouseMotionListener(new MouseMotionAdapter() {
//鼠标下压移动事件
@Override
public void mouseDragged(MouseEvent e) {
Graphics g = getGraphics();
g.setColor(Color.BLACK);
endX = e.getX();
endY = e.getY();
g.drawLine(x, y, endX, endY);
x = endX;
y = endY;
}
});
setSize(600, 500);
setLocation(200,100);
setVisible(true);
setDefaultCloseOperation(3);
}
public static void main(String[] args) {
new MouseDrawLine();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询