java 如何让以下代码画图流畅,而不是断断续续,因双缓冲没学,尽量在以下代码基础上该把,求大师指点

//设计如下GUI,在JFrame元件中鼠标拖拽的轨迹可以写文字打草稿importjava.awt.*;importjava.awt.event.*;importjava... //设计如下GUI,在JFrame元件中鼠标拖拽的轨迹可以写文字打草稿
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JFrame{
private Container c;
private JLabel label;
private MyPaint myPaint;
public Test(){
super("草稿面板");
label=new JLabel("草稿面板",JLabel.CENTER); //文本居中
c=getContentPane();
myPaint=new MyPaint();
label.setFont(new Font("Serif",Font.BOLD,22)); //设置组件文字大小
c.setLayout(new BorderLayout());
c.add(label,BorderLayout.NORTH);
c.add(myPaint,BorderLayout.CENTER);
setBounds(200,200,500,500);
setVisible(true);
}
public static void main(String args[]){
Test app=new Test();
app.addWindowListener(new MyWindowListener());
}
}
class MyPaint extends JPanel{
private int x=0,y=0;
private int nx=0,ny=0;
private Point startPoint=new Point(x,y);
private Point endPoint=new Point(nx,ny);
public MyPaint(){
addMouseMotionListener(new MyMouseMotionListener());
addMouseListener(new MyMouseListener());
}
public void paint(Graphics g){
//super.paint(g); 添加后画面会重绘
//g.drawLine((int)startPoint.getX(),(int)startPoint.getY(),(int)endPoint.getX(),(int)endPoint.getY()); 画线
g.fillRect((int)startPoint.getX(),(int)startPoint.getY(),5,5);
}
//拖拽侦听
class MyMouseMotionListener extends MouseMotionAdapter{
public void mouseDragged(MouseEvent e){
startPoint=e.getPoint();
repaint();
}
public void mouseMoved(MouseEvent e){
}
}
//鼠标单击侦听
class MyMouseListener extends MouseAdapter{
public void mousePressed(MouseEvent e){
startPoint=e.getPoint();
repaint();
}
public void mouseReleased(MouseEvent e){
endPoint=e.getPoint();
repaint();
}
}
}
左图为教材,右图为我的代码,画快了感觉侦听跟不上来,不知道什么原因。
展开
 我来答 举报
00gaojun
2014-07-27 · TA获得超过707个赞
知道小有建树答主
回答量:333
采纳率:0%
帮助的人:156万
展开全部
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class DrawDemo extends JFrame
{
    private Container c;
    private JLabel label;
    private MyPaint myPaint;
     
    public DrawDemo()
    {
        super("草稿面板");
        label = new JLabel("草稿面板", JLabel.CENTER); // 文本居中
        c = getContentPane();
        myPaint = new MyPaint();
        label.setFont(new Font("Serif", Font.BOLD, 22)); // 设置组件文字大小
        c.setLayout(new BorderLayout());
        c.add(label, BorderLayout.NORTH);
        c.add(myPaint, BorderLayout.CENTER);
        setBounds(200200500500);
        setVisible(true);
    }
     
    public static void main(String args[])
    {
        DrawDemo app = new DrawDemo();
    }
}
 
class MyPaint extends JPanel
{
    private int x = 0, y = 0;
    private int nx = 0, ny = 0;
    private Point startPoint = new Point(x, y);
    private Point endPoint = new Point(nx, ny);
     
    public MyPaint()
    {
        addMouseMotionListener(new MyMouseMotionListener());
        addMouseListener(new MyMouseListener());
    }
     
    public void paint(Graphics g)
    {
        g.drawLine((int) startPoint.getX(), (int) startPoint.getY(), (int) endPoint.getX(), (int) endPoint.getY());
        startPoint = endPoint;
    }
     
    // 拖拽侦听
    class MyMouseMotionListener extends MouseMotionAdapter
    {
        public void mouseDragged(MouseEvent e)
        {
            endPoint = e.getPoint();
            repaint();
        }
         
        public void mouseMoved(MouseEvent e)
        {
            endPoint = e.getPoint();
        }
    }
     
    // 鼠标单击侦听
    class MyMouseListener extends MouseAdapter
    {
        public void mousePressed(MouseEvent e)
        {
            startPoint = e.getPoint();
        }
    }
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式