[CODE]/*
* Test.java
*
* Created on 2007年3月8日, 上午10:05
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package test1;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Iterator;
import java.util.LinkedList;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
/**
* 重放线条的绘画
* @author hadeslee
*/
public class Test extends JPanel implements Runnable,ActionListener,MouseListener,MouseMotionListener {
private JButton clear,draw,replay;//清除,开始画和重放的按钮
private LinkedList<MyShape> save;//保存起来的形状
private int flag=0;//定义一个标志,用它来传递当前是在干什么
private final int DRAW=1;//1代表当前正在画图
private final int REPLAY=2;//2代表当前正在重放
private LinkedList<Point> ps;//保存点
private volatile boolean go;
/** Creates a new instance of Test */
public Test() {
initOther();
initWindow();
}
private void initOther(){
clear=new JButton("清除");
draw=new JButton("开始画");
replay=new JButton("重放");
clear.addActionListener(this);
draw.addActionListener(this);
replay.addActionListener(this);
save=new LinkedList<MyShape>();
ps=new LinkedList<Point>();
clear.setEnabled(false);
replay.setEnabled(false);
}
private void initWindow(){
JFrame jf=new JFrame("画线板");
JPanel down=new JPanel();
down.add(clear);
down.add(draw);
down.add(replay);
this.setBorder(BorderFactory.createTitledBorder("绘画区"));
jf.add(this,BorderLayout.CENTER);
jf.add(down,BorderLayout.SOUTH);
jf.setSize(500,450);
jf.setLocationRelativeTo(null);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
if(flag==DRAW){
for(MyShape my:save){
my.drawMe(g);
}
Point previous=null;
for(Point p:ps){
if(previous!=null)
g.drawLine(previous.x,previous.y,p.x,p.y);
previous=p;
}
}else if(flag==REPLAY){
try{
for(MyShape my:save){
if(my.hasNext()){
my.drawNext(g);
return;
}else{
my.drawMe(g);
}
}
for(MyShape my:save){
my.reset();
}
go=false;
replay.setEnabled(true);
draw.setEnabled(true);
} catch(Exception exe){
exe.printStackTrace();
}
}
}
public void run(){
while(go){
try{
Thread.sleep(20);
repaint();
} catch(Exception exe){
exe.printStackTrace();
}
}
flag=DRAW;
repaint();
}
public void actionPerformed(ActionEvent e) {
Object source=e.getSource();
if(source==clear){
save.clear();
replay.setEnabled(false);
repaint();
}else if(source==draw){
flag=DRAW;
this.addMouseListener(this);
this.addMouseMotionListener(this);
draw.setEnabled(false);
clear.setEnabled(true);
}else if(source==replay){
flag=REPLAY;
this.removeMouseListener(this);
this.removeMouseMotionListener(this);
replay.setEnabled(false);
draw.setEnabled(false);
clear.setEnabled(false);
go=true;
new Thread(this).start();
}
}
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
Point p=e.getPoint();
replay.setEnabled(false);
ps.offer(p);
repaint();
}
public void mouseReleased(MouseEvent e) {
replay.setEnabled(true);
ps.offer(e.getPoint());
save.add(new MyLine(ps));
ps=new LinkedList<Point>();
repaint();
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseDragged(MouseEvent e) {
ps.offer(e.getPoint());
repaint();
}
public void mouseMoved(MouseEvent e) {
}
public static void main(String[] args) {
new Test();
}
}
//定义了一个接口,它规定了重绘自己的方法
interface MyShape{
void drawMe(Graphics g);//一下子画出自己
boolean hasNext();//是否还有下一个状态
void drawNext(Graphics g);//画出自己下一个样子
void reset();//重置一下
}
class MyLine implements MyShape{
private LinkedList<Point> ll;//用来保存点
private Iterator<Point> it;//一个迭代器
private LinkedList<Point> over;//保存已经画过的点
public MyLine(LinkedList<Point> ll){
this.ll=ll;
it=ll.iterator();
over=new LinkedList<Point>();
}
public void drawMe(Graphics g) {
Point previous=ll.getFirst();
for(Point p:ll){
g.drawLine(previous.x,previous.y,p.x,p.y);
previous=p;
}
}
public boolean hasNext() {
return it.hasNext();
}
public void drawNext(Graphics g) {
over.offer(it.next());
Point previous=over.getFirst();
for(Point p:over){
g.drawLine(previous.x,previous.y,p.x,p.y);
previous=p;
}
}
public void reset(){
it=ll.iterator();
over.clear();
}
}[/CODE]
刚刚写的,看看吧.你可以试着改进,再好,比如可以给线条不同的颜色,粗细等等
----------------解决方案--------------------------------------------------------
然后你还可以再定义几个类实现MyShape接口,以实现更多形状的重绘方法,我这里只实现了线条的重放
----------------解决方案--------------------------------------------------------
谢谢千里冰封~~~~
感激不尽
----------------解决方案--------------------------------------------------------
不要谢啊,你自己实现一下如何加颜色啊
----------------解决方案--------------------------------------------------------
public void run(){
while(go){
try{
Thread.sleep(20);
repaint(); //这句总这样隔20毫秒就repaint一下,怎么不会覆盖掉上一个20毫秒前repaint的内容?
//怎么不是从新开始而是接着上次的地方继续画呀?
} catch(Exception exe){
exe.printStackTrace();
}
}
flag=DRAW;
repaint();
}
千里大哥,你做的这个挺复杂的....我再学学估计能给加个颜色....能看懂7成就不错了..
----------------解决方案--------------------------------------------------------
public void run(){
while(go){
try{
Thread.sleep(20);
repaint(); //这句总这样隔20毫秒就repaint一下,怎么不会覆盖掉上一个20毫秒前repaint的内容?
//怎么不是从新开始而是接着上次的地方继续画呀?
} catch(Exception exe){
exe.printStackTrace();
}
}
flag=DRAW;
repaint();
}
千里大哥,你做的这个挺复杂的....我再学学估计能给加个颜色....能看懂7成就不错了..
为什么不会,你可以看看我画的时候的代码,也就是MyLine里面,它会把已经画了的地方保存起来
----------------解决方案--------------------------------------------------------
千里大哥,你搞java几年了?
我觉得你太太太..太历害了....
----------------解决方案--------------------------------------------------------
刚找到工作,还没开始
----------------解决方案--------------------------------------------------------
我晕~~~~
那你起薪多少钱?
怎么也得6000+吧?
----------------解决方案--------------------------------------------------------
10000+差不多
----------------解决方案--------------------------------------------------------