当前位置: 代码迷 >> Java相关 >> [求助]如何将刚画到面板上的线条以稍慢的速度重放如何绘画?
  详细解决方案

[求助]如何将刚画到面板上的线条以稍慢的速度重放如何绘画?

热度:193   发布时间:2007-03-06 23:17:51.0
[求助]如何将刚画到面板上的线条以稍慢的速度重放如何绘画?

问题:如何将刚画到面板上的线条以稍慢的速度重放如何绘画?

搜索更多相关的解决方案: 线条  面板  速度  绘画  画到  

----------------解决方案--------------------------------------------------------
把它保存下来,然后起一个线程来重新画过一遍
----------------解决方案--------------------------------------------------------

我好像清楚不掉之前画在JPanel上的线条....能帮我看看吗?
----------------------------------------------------------
@FileName:ArtTest.java
些文件下载:
----------------------------------------------------------
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.border.*;
import javax.swing.*;

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;
}

}



----------------解决方案--------------------------------------------------------
建议再去看看java文档中关于repaint(),文档中说如果使用的是重量级repaint()会调用update(),
而如果使用轻量级repaint()会尽快调paint()而不调用update()。
祝你成功啊!!!
----------------解决方案--------------------------------------------------------
清除以前的线条,有两种方法
一种是调用父类的方法,super.paintComponent(g);
还有一种就是填充一个和JPanel一样大的矩形,
----------------解决方案--------------------------------------------------------
个人建议你可以在看看java文档关于repaint()的说明。那上面不说如果使用重量级容器,repaint()会调用update(),
如果使用轻量级容器,reapint()会尽快调用paint().
----------------解决方案--------------------------------------------------------
以下是引用千里冰封在2007-3-7 10:49:04的发言:
清除以前的线条,有两种方法
一种是调用父类的方法,super.paintComponent(g);
还有一种就是填充一个和JPanel一样大的矩形,

大概是这意思吗?这办法我哪里弄错了?还是清楚不掉以前画的
--------------------------------------------------------------------
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内部的代码!");
}
}
}
----------------解决方案--------------------------------------------------------
没必要生成一个新的JPanel去,只要你重绘的时候去一个线程去控制就可以了
----------------解决方案--------------------------------------------------------

我调用super.paintComponent(g); 都感觉不出以前的JPanle上的东西被清空了..好像没反应...

关键就是不会清空..


----------------解决方案--------------------------------------------------------
一定会清空的
你想动态加进去Jpanel?


----------------解决方案--------------------------------------------------------
  相关解决方案