标题:[分享]90%代码是千里冰封写的作品,一个涂鸦的画板[更新]
取消只看楼主
limaoyuan
Rank: 1
等 级:新手上路
帖 子:493
专家分:0
注 册:2006-6-25
 问题点数:0 回复次数:6 
[分享]90%代码是千里冰封写的作品,一个涂鸦的画板[更新]

我觉得对学习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编辑过]

搜索更多相关主题的帖子: awt import java 涂鸦 千里冰封 
2007-03-08 17:36
limaoyuan
Rank: 1
等 级:新手上路
帖 子:493
专家分:0
注 册:2006-6-25
得分:0 
千里我没让你失望吧..仔细看了几遍又试了半天才加上颜色的...后来加完了再一看,其实挺容易加的

我在说's blog   http://www..cn
2007-03-08 17:47
limaoyuan
Rank: 1
等 级:新手上路
帖 子:493
专家分:0
注 册:2006-6-25
得分:0 
好的~~我试着再改下..

我在说's blog   http://www..cn
2007-03-08 18:11
limaoyuan
Rank: 1
等 级:新手上路
帖 子:493
专家分:0
注 册:2006-6-25
得分:0 
加了一个自定义画笔粗细
增加了一个自定义画笔粗细,画矩形暂没弄出来
GEYYqQyF.rar (13.08 KB) [分享]90%代码是千里冰封写的作品,一个涂鸦的画板




hLaDHBKn.jpg (40.19 KB)

我在说's blog   http://www..cn
2007-03-08 22:59
limaoyuan
Rank: 1
等 级:新手上路
帖 子:493
专家分:0
注 册:2006-6-25
得分:0 

谢谢老大夸奖....我现在试着加下矩形~~


我在说's blog   http://www..cn
2007-03-09 09:51
limaoyuan
Rank: 1
等 级:新手上路
帖 子:493
专家分:0
注 册:2006-6-25
得分:0 
回复:(千里冰封)不错,像你这样努力,一定会有成就的...
W7KnCIOP.rar (13.02 KB) [分享]90%代码是千里冰封写的作品,一个涂鸦的画板[更新]




0K5GTsfd.jpg (29.32 KB)

我在说's blog   http://www..cn
2007-03-09 11:12
limaoyuan
Rank: 1
等 级:新手上路
帖 子:493
专家分:0
注 册:2006-6-25
得分:0 
嘿嘿,在千里眼里这些肯定是小菜....在我这个菜鸟眼了还是挺困难的....这个半成品先放这里吧...我往下面继续看SWING了...先总体了解过后,以后我再完善这个作品.....谢谢千里冰封的一路帮忙........

我在说's blog   http://www..cn
2007-03-09 19:24



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-123124-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.286679 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved