我觉得对学习Graphics类挺有用的....9楼更新了一下,加了一个自定义画笔粗细
文件名:Test.java
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
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.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test extends JPanel implements Runnable,ActionListener,MouseListener,MouseMotionListener {
private JButton clear,draw,replay,btnColor;//清除,开始画和重放和颜色的按钮
Color choosedColor = Color.BLACK;//选中的颜色
JDialog dialog = null;
private LinkedList<MyShape> save;//保存起来的形状
private int flag=0;//定义一个标志,用它来传递当前是在干什么
private final int DRAW=1;//1代表当前正在画图
private final int REPLAY=2;//2代表当前正在重放
private LinkedList<Pen> 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("重放");
btnColor = new JButton("选取颜色");//选颜色
clear.addActionListener(this);
draw.addActionListener(this);
replay.addActionListener(this);
save=new LinkedList<MyShape>();
ps=new LinkedList<Pen>();
clear.setEnabled(false);
replay.setEnabled(false);
}
private void initWindow(){
JFrame jf=new JFrame("画板 Dreamm ");
JPanel down=new JPanel();
down.add(clear);
down.add(draw);
down.add(replay);
down.add(btnColor);
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);
btnColor.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
final JColorChooser chooser = new JColorChooser();
dialog = JColorChooser.createDialog(null, "选取颜色", false, chooser, new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
choosedColor = chooser.getColor();
}
}, null);
dialog.setVisible(true);
}
});
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
if(flag==DRAW){
for(MyShape my:save){
my.drawMe(g);
}
Point previous=null;
for(Pen pp:ps){
Point p = pp.getP();
if(previous!=null)
{
Graphics2D g2 = (Graphics2D)g;
g2.setStroke(new BasicStroke(2,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL,2));
g.setColor(pp.getColor());
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();
Pen pp = new Pen(p,choosedColor);
replay.setEnabled(false);
ps.offer(pp);
repaint();
}
public void mouseReleased(MouseEvent e) {
replay.setEnabled(true);
Pen pp = new Pen(e.getPoint(),choosedColor);
save.add(new MyLine(ps));
ps=new LinkedList<Pen>();
repaint();
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseDragged(MouseEvent e) {
Pen pp = new Pen(e.getPoint(),choosedColor);
ps.offer(pp);
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 Pen
{
private Point p;
private Color color;
Pen(Point p,Color color)
{
this.p = p;
this.color = color;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public Point getP() {
return p;
}
public void setP(Point p) {
this.p = p;
}
}
class MyLine implements MyShape{
private LinkedList<Pen> ll;//用来保存点
private Iterator<Pen> it;//一个迭代器
private LinkedList<Pen> over;//保存已经画过的点
public MyLine(LinkedList<Pen> ll){
this.ll=ll;
it=ll.iterator();
over=new LinkedList<Pen>();
}
public void drawMe(Graphics g)
{
Point previous=ll.getFirst().getP();
for(Pen pp:ll){
Point p = pp.getP();
Graphics2D g2 = (Graphics2D)g;
g2.setStroke(new BasicStroke(2,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL,2));
g.setColor(pp.getColor());
g.drawLine(previous.x,previous.y,p.x,p.y);
previous=p;
}
}
public boolean hasNext() {
return it.hasNext();
}
public void drawNext(Graphics g)
{
over.add(it.next());
Point previous=over.getFirst().getP();
for(Pen pp:over){
Point p = pp.getP();
Graphics2D g2 = (Graphics2D)g;
g2.setStroke(new BasicStroke(2,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL,2));
g.setColor(pp.getColor());
g.drawLine(previous.x,previous.y,p.x,p.y);
previous=p;
}
}
public void reset(){
it=ll.iterator();
over.clear();
}
}
[此贴子已经被作者于2007-3-8 23:01:15编辑过]
----------------解决方案--------------------------------------------------------
千里我没让你失望吧..仔细看了几遍又试了半天才加上颜色的...后来加完了再一看,其实挺容易加的
----------------解决方案--------------------------------------------------------
其实你还可以再改进一下,让用户可以更新线条的粗细,也就是给你的Pen类,加一个属性
免得你画的时候都g2.setStroke(new BasicStroke(2,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL,2));
这样就更好了
----------------解决方案--------------------------------------------------------
千里我没让你失望吧..仔细看了几遍又试了半天才加上颜色的...后来加完了再一看,其实挺容易加的
不错,其实你没有必要写一个匿名内部类嘛,完全可以在我的actionPerfomred里面去实现你新加的按钮的监听
另外,试着加一个笔触的大小,
然后再试着加一个可以画矩形,画圆的重放,自己思考一下哦
----------------解决方案--------------------------------------------------------
支持原创
希望楼主继续完善程序
----------------解决方案--------------------------------------------------------
好的~~我试着再改下..
----------------解决方案--------------------------------------------------------
嗯,不错不错,继续努力!!
----------------解决方案--------------------------------------------------------
di ge
----------------解决方案--------------------------------------------------------
----------------解决方案--------------------------------------------------------
不错,你很有天分哦
再接再励,把画矩形和圆的都加上
----------------解决方案--------------------------------------------------------