import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class TestMouseLine extends JFrame
{
int x,y;
int endX,endY;
ArrayList list,array;
public TestMouseLine()
{
Container c=getContentPane();
list=new ArrayList();
array=new ArrayList();
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
x=e.getX();
y=e.getY();
}
public void mouseReleased(MouseEvent e)
{
for(int i=0;i<list.size();i++)
array.add(list.get(i));
}
});
addMouseMotionListener(new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent e)
{
endX = e.getX();
endY = e.getY();
list.clear();
list.add(new MyLine(x,y,endX,endY));
repaint();
}
});
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setSize(400,300);
setLocation(400,300);
show();
}
public void paint(Graphics g)
{
super.paint(g);
Iterator it=array.iterator();
while(it.hasNext())
{
MyLine line=(MyLine)it.next();
line.drawMe(g);
}
}
public static void main(String[] args)
{
new TestMouseLine();
}
}
class MyLine
{
private int x,y;
private int endX,endY;
public MyLine(int x,int y,int endX,int endY)
{
this.x = x;
this.y = y;
this.endX = endX;
this.endY = endY;
}
public void drawMe(Graphics g)
{
g.setColor(Color.RED);
g.drawLine(x,y,endX,endY);
}
};
为什么非要画下一条线的时候,才出现上一条线呢?比如:在画第2条线的时候,才出现第一条线...请指教!!!
----------------解决方案--------------------------------------------------------
你这个应该利用边画边看的方法
增加一个鼠标拖动的事件就可以了
并且中间增加一个变量用于存储鼠标拖动时的座标值
在鼠标释放的时候就把它清0就可以了
这个时候就算画成了一条线,然后画别的线也是这个道理
----------------解决方案--------------------------------------------------------
应该是这样的.我利用一个ArrayList去保存画出来的线,我应该再创建一个ArrayList对象去保存已经画出来的线...
----------------解决方案--------------------------------------------------------
我在你的基础上改的
你看看
import java.awt.*;
import javax.swing.*;
import sun.security.krb5.internal.j;
import java.awt.event.*;
import java.util.*;
public class TestMouseLine extends JPanel
{
int x,y;
int endX,endY;
ArrayList list,array;
public TestMouseLine()
{
list=new ArrayList();
array=new ArrayList();
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
x=e.getX();
y=e.getY();
}
public void mouseReleased(MouseEvent e)
{
MyLine my=new MyLine(x,y,e.getX(),e.getY());
array.add(my);
repaint();
}
});
addMouseMotionListener(new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent e)
{
endX = e.getX();
endY = e.getY();
repaint();
}
});
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Iterator it=array.iterator();
while(it.hasNext())
{
MyLine line=(MyLine)it.next();
line.drawMe(g);
}
g.drawLine(x,y,endX,endY);
}
public static void main(String[] args)
{
JFrame jf=new JFrame();
jf.getContentPane().add(new TestMouseLine(),BorderLayout.CENTER);
jf.setBounds(300,300,400,400);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class MyLine
{
private int x,y;
private int endX,endY;
public MyLine(int x,int y,int endX,int endY)
{
this.x = x;
this.y = y;
this.endX = endX;
this.endY = endY;
}
public void drawMe(Graphics g)
{
g.setColor(Color.RED);
g.drawLine(x,y,endX,endY);
}
};
我只是改成把东西画在JPanel上,然后把JPanel加在JFrame中
并且在鼠标拖动的时候改了一下
这个程序也简单了
你试试,是不是你想要的效果
----------------解决方案--------------------------------------------------------