[java]代码库
1.主类
package s1221画图板重绘撤销;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.ArrayList;
import javax.swing.*;
public class Main1221 extends JFrame{
ImageIcon image[]={
new ImageIcon("E:/Michael/javaImage/扫雷图片/2.jpg"),//曲线
new ImageIcon("E:/Michael/javaImage/扫雷图片/2.jpg"),//直线
new ImageIcon("E:/Michael/javaImage/扫雷图片/2.jpg"), //矩形
new ImageIcon("E:/Michael/javaImage/扫雷图片/2.jpg"),//椭圆
};
String st[]={"曲线","直线","矩形","椭圆","橡皮","颜色笔","填色","放大镜","刷子","圆角矩形","涂鸦","文字","多边形","填充矩形","文字","多边形","填充矩形"};
Color b[] ={ Color.blue,Color.green,Color.lightGray,Color.orange,Color.black,
Color.cyan,Color.darkGray,Color.gray,Color.magenta,Color.pink,Color.red,
Color.yellow,Color.white,new Color(0,100,200),new Color(0,200,200)};
static ArrayList<Shape> list = new ArrayList<Shape>();
Mymouselisten l1=new Mymouselisten(); //创建监听对象
public Main1221()
{
//设置窗体属性*********************************************
this.setDefaultCloseOperation(3);//设置关闭的方式
this.setLayout( new BorderLayout());//设置窗体为边框布局
this.setSize(800, 600);//设置窗体的大小
this.setTitle("画板"); //设置窗体的标题
this.setResizable(true);//设置窗体不可改变大小
this.setLocationRelativeTo(null);//设置窗体出现在屏幕中间
//设置窗体属性*********************************************
//创建按钮**************************************
JMenuBar jbar=new JMenuBar();
JMenuItem t1=new JMenuItem("撤销");
JMenu m1=new JMenu("选项");
t1.addActionListener(l1);
m1.add(t1);
jbar.add(m1);
this.setJMenuBar(jbar);
//创建按钮***************************************
//左边的面板************************************
JPanel west=new JPanel();
west.setPreferredSize(new Dimension(80,0));
west.setBackground(Color.lightGray);
this.add(west,BorderLayout.WEST);
//左边的面板************************************
//下方的面板********************************************
JPanel south =new JPanel();
south.setPreferredSize(new Dimension(0,100));
south.setBackground(Color.pink);
this.add(south,BorderLayout.SOUTH);
//下方的面板*********************************************
//绘画的面板**************************************************************
JPanel drawpanel =new JPanel()
{
public void paint(Graphics g) //重写JPanel中的方法
{
super.paint(g);
list.forEach((shape)->shape.draw(g)); //lambda表达式
}
};
//绘画的面板**************************************************************
//中间的面板********************************************
JPanel center =new JPanel();
center.setBackground(Color.GRAY);
center.setLayout(new BorderLayout());
center.add(drawpanel);
this.add(center,BorderLayout.CENTER);
//中间的面板*********************************************
drawpanel.setBackground(Color.WHITE);
drawpanel.addMouseListener(l1); //给监听方法传递监听的对象
drawpanel.addMouseMotionListener(l1); //虽然l1实现了多个接口,但是面板要监听鼠标拖动事件,要分别添加方法
//**********************************窗体左边的选项栏***************************************//
for(int i=0;i<image.length;i++)
{
JButton jb=new JButton(image[i]);
jb.setActionCommand(st[i]); //给这个图标添加action command 方便在事件中获取
jb.setPreferredSize(new Dimension(image[i].getIconWidth(),image[i].getIconHeight()));
jb.setToolTipText(st[i]);
jb.addActionListener(l1);
west.add(jb);
}
//*****************************************************************************************//
//**********************************窗体下方颜色选择框***************************************//
for(Color i:b)
{
JButton jb1=new JButton("");
jb1.setBackground(i);
jb1.setPreferredSize(new Dimension(30,30));
jb1.addActionListener(l1);
south.add(jb1);
}
//*****************************************************************************************//
this.setVisible(true); //要先设置窗体可见,然后再传画笔
Graphics g=drawpanel.getGraphics(); //传递画笔 ,我一开始放在最后,结果不能显示!!!传画笔之前一定要先设置可见
l1.setGraphics(g,list,drawpanel);
}
public static void main(String[] args)
{ Main1221 k=new Main1221();}
}
[源代码打包下载]
初级程序员
by: 云代码会员 发表于:2016-06-09 23:05:51 顶(0) | 踩(0) 回复
我要积分啊!
回复评论