关于画线问题(新人请教)
import javax.swing.*;import java.awt.event.*;
import java.awt.*;
import java.util.*;
public class Draw extends JFrame
{
private ArrayList list = new ArrayList();
private Point startPos = null;
private JPanel p = null;
private JButton btn = null;
public static void main(String[] args)
{
new Draw();
}
public Draw()
{
initWindow();//把子窗口和按钮NEW出来
p.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
startPos = e.getPoint();
}
public void mouseReleased(MouseEvent e)
{
Point endPos = e.getPoint();
LineObject obj = new LineObject();
obj.start = startPos;
obj.end = endPos;
list.add(obj);
}
});
p.addMouseMotionListener(new MouseMotionListener(){
public void mouseDragged(MouseEvent e)
{
Point currPos = e.getPoint();
Graphics g = p.getGraphics();
p.paint(g);
drawAllList();
g.drawLine(startPos.x, startPos.y, currPos.x, currPos.y);
}
public void mouseMoved(MouseEvent e){}
});
showWindow();//设窗口大小,显示,关闭
}
public void drawAllList()
{
Graphics g = p.getGraphics();
LineObject obj;
for(int i=0; i<list.size(); i++)
{
obj = (LineObject)list.get(i);
g.drawLine(obj.start.x, obj.start.y, obj.end.x, obj.end.y);
}
}
public void initWindow()
{
if(null == p)
{
p = new JPanel();
}
}
public void showWindow()
{
this.setContentPane(p);
this.setSize(400, 400);
this.setVisible(true);
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}
class LineObject
{
Point start;
Point end;
}
我想问问画线的时候可不可以让每条线在画出来的时候就标明了顺序,就是我画第一条线就会在线那里标明是第一条,如此类推。像下图那样: