1. 项目目标:
2. 程序流程:
显示画图板的方法(){
创建窗体;
设置窗体属性;
设置布局;
创建三块面板对象
设置面板1(工具面板)
设置面板2(颜色面板)
…………
//最后
设置窗体可见
获取画图面板的画布对象
创建鼠标动作监听器对象
给画图面板添加鼠标监听
}
设置面板1的方法(){
设置面板大小、背景
创建按钮
创建按钮图标
创建按钮的动作监听对象
设置按钮图标
设置按钮标记
给按钮添加监听
……
}
设置面板2的方法(){
//类似设置面板1
}
注意:
//设置面板们的大小,注意:setSize只适合于JFrame重量级容器设置大小
//轻量级组件(面板、按钮等)使用setPreferredSize(尺寸Dimension)设置其大小
3. 写程序:
package paint_2;
导入类import
/**
* 画图板(第二版)
* @author Administrator
*/
public class Paint {
//画布
private Graphics g;
//形状标记
private String shape;
//显示画图板的方法
public void showFrame(){
//创建窗体
JFrame frame = new JFrame();
//设置窗体的属性
frame.setTitle("画图板");
frame.setSize(500,500);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(3);
//使用BorderLayout边框布局来规范面板(JFrame默认布局就是BorderLayout)
//创建三块面板对象 JPanel
JPanel toolPanel = new JPanel();
JPanel colorPanel = new JPanel();
JPanel paintPanel = new JPanel();
//装饰面板对象们
setToolPanel(toolPanel);
setColorPanel(colorPanel);
//设置画图面板的背景颜色
paintPanel.setBackground(Color.white);
//将面板添加到窗体指定的位置
frame.add(toolPanel,BorderLayout.WEST);
frame.add(colorPanel,BorderLayout.SOUTH);
frame.add(paintPanel,BorderLayout.CENTER);
//可见
frame.setVisible(true);
//获取画图面板的画布对象
g = paintPanel.getGraphics();
//创建鼠标监听器对象
MouseListener mouseListener = new MouseListener(){
int x1,y1,x2,y2;
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
//按下
public void mousePressed(MouseEvent e){
x1 = e.getX();
y1 = e.getY();
}
//释放
public void mouseReleased(MouseEvent e){
x2 = e.getX();
y2 = e.getY();
if(shape.equals("line")){
g.drawLine(x1,y1,x2,y2);
}
if(shape.equals("rect")){
g.drawRect(Math.min(x1,x2),Math.min(y1,y2),Math.abs(x2-x1),Math.abs(y2-y1));
}
if(shape.equals("oval")){
g.drawOval(Math.min(x1,x2),Math.min(y1,y2),Math.abs(x2-x1),Math.abs(y2-y1));
}
}
};
//给画图面板添加鼠标监听
paintPanel.addMouseListener(mouseListener);
}
//装饰工具面板的方法
public void setToolPanel(JPanel toolPanel){
toolPanel.setBackground(Color.lightGray);
toolPanel.setPreferredSize(new Dimension(80,500));
//字符串数组(保存图片名称)
String[] imageNames = {"airbrush","brush","color_picker","curve","dot_rect","eraser","fill","line","magnifier","oval","pencil","polygon","rect","round_rect","star","word"};
//创建动作监听器对象
ActionListener shapeListener = new ActionListener(){
public void actionPerformed(ActionEvent e){
//获取按钮上的文本
shape = e.getActionCommand();
}
};
//循环创建16个形状按钮对象
for(int i=0;i<16;i++){
JButton shapeButton = new JButton();
//设置按钮上的标记
shapeButton.setActionCommand(imageNames[i]);
shapeButton.setPreferredSize(new Dimension(25,25));
//创建关联指定图片文件的图标对象
ImageIcon icon = new ImageIcon("images//"+imageNames[i]+".jpg");
shapeButton.setIcon(icon);//设置按钮上的图标
shapeButton.addActionListener(shapeListener);
toolPanel.add(shapeButton);
}
}
//装饰颜色面板的方法
public void setColorPanel(JPanel colorPanel){
colorPanel.setBackground(Color.lightGray);
colorPanel.setPreferredSize(new Dimension(500,80));
//创建颜色数组(保存按钮的背景颜色)
Color[] colors = {Color.red,Color.gray,Color.blue,Color.green,Color.white,Color.black,Color.orange,Color.pink,Color.yellow,Color.lightGray};
//创建动作监听对象
ActionListener colorListener = new ActionListener(){
public void actionPerformed(ActionEvent e){
//获取到被点击的按钮对象
Object object = e.getSource();
JButton button = (JButton)object;
//获取按钮上的颜色标记
Color color = button.getBackground();
//设置画布的颜色
g.setColor(color);
}
};
//循环创建颜色按钮
for(int i=0;i<10;i++){
JButton colorButton = new JButton();
colorButton.setPreferredSize(new Dimension(25,25));
colorButton.setBackground(colors[i]);
colorPanel.add(colorButton);
//给按钮添加监听器
colorButton.addActionListener(colorListener);
}
}
}
4. 测试结果:(只能画直线、矩形、椭圆)