问题:如何将刚画到面板上的线条以稍慢的速度重放如何绘画?
问题:如何将刚画到面板上的线条以稍慢的速度重放如何绘画?
我好像清楚不掉之前画在JPanel上的线条....能帮我看看吗?
----------------------------------------------------------
@FileName:ArtTest.java
些文件下载:
public class ArtTest
{
public static void main(String[] args)
{
MFrame frm = new MFrame();
}
}
class MFrame extends JFrame
{
MFrame()
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Dreamm");
MPanel pnl = new MPanel();
setContentPane(pnl);
setSize(600,400);
setVisible(true);
}
}
class MPanel extends JPanel
{
ArrayList<Pan> lstPoint = new ArrayList<Pan>(); //存放鼠标按下移动的点的坐标集合
JLabel lblInfo = new JLabel(); //显示坐标用
JButton btnStart = new JButton("开始画画");//开始画
JButton btnColor = new JButton("选取颜色");//选颜色
JButton btmPrint = new JButton("展示绘画过程");//展示绘画过程
Color choosedColor = Color.BLACK;//选中的颜色
JDialog dialog = null;
Point lastPoint = new Point();//最后一个点
boolean enterLeft = false; //判断鼠标左键是否按下
boolean isRePoint = false;//是否重绘
ControlPanel conPanel = new ControlPanel();//放所有按钮的面板
class ControlPanel extends JPanel
{
ControlPanel()
{
this.setBorder(new LineBorder(Color.RED,1));
setPreferredSize(new Dimension(120,400));
add(lblInfo);
add(btnStart);
add(btnColor);
add(btmPrint);
btnStart.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("开始画画"))
{
btnStart.setText("停止画画");
}
else
{
btnStart.setText("开始画画");
}
}
});
btnColor.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
final JColorChooser chooser = new JColorChooser();
dialog = JColorChooser.createDialog(MPanel.this, "选取颜色", false, chooser, new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
choosedColor = chooser.getColor();
}
}, null);
dialog.setVisible(true);
}
});
btmPrint.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("展示绘画过程"))
{
isRePoint = true;
btmPrint.setText("停止展示");
}
else
{
isRePoint = false;
btmPrint.setText("展示绘画过程");
//需要再写出停止展示的代码
}
repaint();
}
});
}
}
MPanel()
{
this.setBackground(Color.WHITE);
setLayout(new BorderLayout());
lblInfo.setPreferredSize(new Dimension(130,20));
//add(lblInfo,BorderLayout.NORTH);
add(conPanel,BorderLayout.WEST);
this.setFocusable(true); //设置Panel可以接收focus
this.addMouseListener(new MouseAdapter()
{
@Override
public void mouseReleased(MouseEvent e) {
super.mouseReleased(e);
enterLeft = false;
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
super.mousePressed(e);
lastPoint = e.getPoint();
enterLeft = true;
}
});
this.addMouseMotionListener(new MouseMotionAdapter()
{
@Override
public void mouseDragged(MouseEvent e)
{
super.mouseDragged(e);
Point p = e.getPoint();
if(btnStart.getText().equals("开始画画"))
{}
else
{
if(enterLeft)
{
Line2D.Float line = new Line2D.Float(lastPoint,p);
Pan pan = new Pan(line,choosedColor);
lstPoint.add(pan);
lastPoint = p;
repaint();
}
}
}
@Override
public void mouseMoved(MouseEvent e)
{
super.mouseMoved(e);
Point p = e.getPoint();
lblInfo.setText(" 坐标: "+p.getX()+" , "+p.getY());
}
});
}
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
if(!isRePoint)//利用点击重绘来确定是否以下面代码中有延时的方式进行执行,还是按上面的方式正确执行
{
//System.out.println("paintComponent内部的代码!");
for(Pan pan: lstPoint) //正常绘图
{
g2.setColor(pan.getColor());
g2.draw(pan.getLine());
}
}
else
{
//g2.clearRect(0, 60, 400, 400);
//System.out.println("清除完成!");
for(Pan pan: lstPoint)
{
g2.setPaint(pan.getColor());
g2.draw(pan.getLine());
try{Thread.sleep(100);}catch(Exception e){e.printStackTrace();}//睡0.1秒画一次
//System.out.println("paint内部的代码!");
}
}
}
}
class Pan
{
Line2D.Float line;
Color color;
Pan(Line2D.Float line,Color color)
{
this.line = line;
this.color = color;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public Line2D.Float getLine() {
return line;
}
public void setLine(Line2D.Float line) {
this.line = line;
}
}
大概是这意思吗?这办法我哪里弄错了?还是清楚不掉以前画的
--------------------------------------------------------------------
public void addPaintPanel() //慢动作重绘时直接调用这个方法
{
PaintPanel pp = new PaintPanel();
pp.setLocation(120, 0); //位置放到
pp.setBackground(Color.WHITE);
add(pp);
}
//重绘时生成一个一样大的面板盖往原先的面板
class PaintPanel extends JPanel
{
PaintPanel()
{
setLayout(null);
this.setPreferredSize(new Dimension(480,400)); //和以前JPanle一样大小
System.out.println("pp");
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
for(Pan pan: lstPoint) //慢动作重绘
{
g2.setPaint(pan.getColor());
g2.draw(pan.getLine());
try{Thread.sleep(100);}catch(Exception e){e.printStackTrace();}//睡0.1秒画一次
System.out.println("paint内部的代码!");
}
}
}
我调用super.paintComponent(g); 都感觉不出以前的JPanle上的东西被清空了..好像没反应...
关键就是不会清空..