画板是此次暑假课程第一个项目,让我学到了很多。当然此次画板程序还是有些缺陷。如:喷枪,橡皮之类的撤销键尚不十分完善。
1, 首先创建一个窗体,为其添加基本属性。采用边框布局,分为画板区,工具区,颜料区。并设置可见
public void init() {
setTitle("我的画板");
// 通过相对路径创建一个ImageIcon的对象
ImageIcon ii = new ImageIcon("img/icon.png");
// 从imageIcon对象中获取image,设置上去
setIconImage(ii.getImage());
// 设置大小
setSize(600, 600);
// 设置位置
setLocationRelativeTo(null);
// 设置默认关闭
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
颜料区
private void addColorPanel() {
colorPanel = new ColorPanel(this);
add(colorPanel, BorderLayout.SOUTH);
}
工具区
private void addToolsPanel() {
toolsPanel = new ToolsPanel(this);
add(toolsPanel, BorderLayout.WEST);
}
画板区
private void addDrawPanel() {
// 创建绘制面板
drawPanel = new DrawPanel(this);
add(drawPanel, BorderLayout.CENTER);
}
2, 给工具区添加选择按钮,颜料区添加颜料选择,画板分别添加添加监听。
工具区
public class ToolsPanel extends JPanel {
String[] allCommand = {"wj","xz","xp","pt","xg","fdj","qb","sz","pq","wz","xx","qx","jx","dbx","ty","yjjx"};
MainFrame mainFrame;
public ToolsPanel(MainFramemainFrame)
{
this.mainFrame =mainFrame;
setPreferredSize(new Dimension(60, 0));
setBackground(new Color(240,240,240));
setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
//创建一个往上凸起的边框
BevelBorder border = new BevelBorder(BevelBorder.RAISED,new Color(240,240,240),newColor(160,160,160));
setBorder(border);
//创建一个按钮组,使得所有按钮能够互斥
ButtonGroup gg = new ButtonGroup();
for (inti = 1; i <=allCommand.length;i++) {
ImageIcon i1 = new ImageIcon(getClass().getResource("/img/"+i+"-1.png"));
ImageIcon i2 = new ImageIcon(getClass().getResource("/img/"+i+"-2.png"));
ImageIcon i3 = new ImageIcon(getClass().getResource("/img/"+i+"-3.png"));
ImageIcon i4 = new ImageIcon(getClass().getResource("/img/"+i+"-4.png"));
//创建单选按钮
JRadioButton jrb = new JRadioButton();
//设置了一个ActionCommand
jrb.setActionCommand(allCommand[i-1]);
if(allCommand[i-1].equals("qb"))
{
jrb.setSelected(true);
}
//设置轻量级组建的大小
jrb.setPreferredSize(new Dimension(25, 25));
//设置按钮四种状态下的图标
jrb.setIcon(i1);
jrb.setRolloverIcon(i2);
jrb.setPressedIcon(i3);
jrb.setSelectedIcon(i4);
jrb.setMargin(new Insets(0, -2, 0, 0));
//给按钮增加监听
jrb.addActionListener(new ToolsActionListener(mainFrame));
add(jrb);
gg.add(jrb);
}
}
颜料区
public class ColorPanel extends JPanel {
添加颜料
Color[] colors = { new Color(0, 0, 0), new Color(128, 128, 128),
new Color(128, 0, 0),new Color(128, 128, 0), new Color(0, 128, 0),
new Color(0, 128, 128),new Color(0, 0, 128),
new Color(128, 0, 128),new Color(128, 128, 64),
new Color(0, 64, 64),new Color(0, 128, 255),
new Color(0, 64, 128),new Color(128, 0, 255),
new Color(128, 64, 0),new Color(255, 255, 255),
new Color(192, 192, 192),new Color(255, 0, 0),
new Color(255, 255, 0),new Color(0, 255, 0),
new Color(0, 255, 255),new Color(0, 0, 255),
new Color(255, 0, 255),new Color(255, 255, 128),
new Color(0, 255, 128),new Color(128, 255, 255),
new Color(128, 128, 255),new Color(255, 0, 128),
new Color(255, 128, 64) };
MainFrame mainFrame;
private JButtonjbt1;
private JButtonjbt2;
public ColorPanel(MainFramemainFrame)
{
this.mainFrame =mainFrame;
setPreferredSize(new Dimension(0, 50));
setBorder(new LineBorder(Color.GRAY));
setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
//创建底下的面板
JPanel back = createBack();
back.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
//创建左边
JPanel selectedColor = createSelected();
//创建右边
JPanel allColor = createAll();
back.add(selectedColor);
back.add(allColor);
add(back);
}
private JPanel createSelected() {
JPanel jp = new JPanel();
jp.setPreferredSize(new Dimension(30, 30));
jp.setBackground(Color.WHITE);
//设置布局为绝对布局,每一个加入到容器的组建必须setBonds()
jp.setLayout(null);
//设置边框
BevelBorder border = new BevelBorder(1,Color.WHITE,Color.GRAY);
jp.setBorder(border);
//增加按钮
BevelBorder border1 = new BevelBorder(0,Color.WHITE,Color.GRAY);
jbt1 = new JButton();
jbt1.setBackground(Color.BLACK);
jbt1.setBounds(5,5,15,15);
jbt1.setEnabled(false);
jbt1.setBorder(border1);
jbt2 = new JButton();
jbt2.setBackground(Color.WHITE);
jbt2.setBounds(10,10,15,15);
jbt2.setEnabled(false);
jbt2.setBorder(border1);
jp.add(jbt1);
jp.add(jbt2);
returnjp;
}
private JPanel createAll() {
JPanel jp = new JPanel();
jp.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
jp.setPreferredSize(new Dimension(210, 30));
jp.setBackground(Color.GREEN);
BevelBorder border1 = new BevelBorder(1,Color.WHITE,Color.GRAY);
for (inti = 0; i < colors.length; i++) {
JButton jbt = new JButton();
jbt.setPreferredSize(new Dimension(15, 15));
jbt.setBackground(colors[i]);
jbt.setBorder(border1);
jbt.addMouseListener(new ColorActionListener(mainFrame));
jp.add(jbt);
}
returnjp;
}
public JPanel createBack()
{
//创建一个面板
JPanel back = new JPanel();
//设置大小
back.setPreferredSize(new Dimension(250, 50));
//设置背景颜色
// back.setBackground(Color.RED);
//设置一个空的边框
back.setBorder(new EmptyBorder(10, 0, 10, 10));
returnback;
}
public void changeSelectedBKColor(Color color)
{
jbt1.setBackground(color);
}
public void changeSelectedTCColor(Color color)
{
jbt2.setBackground(color);
}
}
监听
颜料监听
public void mouseClicked(MouseEvent e) {
// TODO Auto-generatedmethod stub
//获取事件源==》按钮
//获取鼠标按钮1左键,3右键
int button = e.getButton();
System.out.println(button);
Object source = e.getSource();
//强制转换为JButton
if(sourceinstanceof JButton)
{
JButton jbt = (JButton)source;
System.out.println(jbt.getBackground());
// mainFrame.setCurrentSelectedColor(jbt.getBackground());
if(button == 1)
{
mainFrame.setBkColor(jbt.getBackground());
mainFrame.colorPanel.changeSelectedBKColor(jbt.getBackground());
}
else if(button == 3)
{
mainFrame.setTcColor(jbt.getBackground());
mainFrame.colorPanel.changeSelectedTCColor(jbt.getBackground());
}
工具监听
public void actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand());
String actionCommand = e.getActionCommand();
mainFrame.setsTool(actionCommand);
}
3, 为工具栏添加功能算法
比如直线
x2 = e.getX();
y2 = e.getY();
graphics.drawLine(x1, y1, x2, y2);
4, 进行封装,完善重绘功能
直线
x2 = e.getX();
y2 = e.getY();
//创建一个直线对象,调用直线对象的绘制方法,将直线画出来
DrawPanel.ls.add(new LineShape(x1,y1, x2, y2,bkColor));
mainFrame.repaint();
5, 添加撤销和恢复功能
撤销
public class UndoActionListener implements ActionListener{
static ShapeContainersc1=new ShapeContainer();
MainFrame mf;
public UndoActionListener(MainFramemf)
{
this.mf=mf;
}
public void actionPerformed(ActionEvent e){
ShapeContainer sc=DrawPanel.ls;
if(sc.getSize()==0){
return;
}
Shape shape=sc.get(sc.getSize()-1);
sc1.add(shape);
sc.remove(sc.getSize()-1);
mf.repaint();
}
}
恢复
public class RedoActionListener implements ActionListener{
MainFrame mf;
public RedoActionListener(MainFramemf)
{
this.mf=mf;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
ShapeContainer sc1=DrawPanel.ls;
ShapeContainer sc2=UndoActionListener.sc1;
Shape shape=sc2.get(sc2.getSize()-1);
sc2.remove(sc2.getSize()-1);
sc1.add(shape);
mf.repaint();
}
6, 调整布局,排除bug,完善代码。