第四天

package 画图板二;
import javax.swing.*;


import java.awt.*;


import java.awt.event.*;
/**
 * 
 * @author wensimiao
 *
 */


public class Draw {
String shape;
int x1,y1,x2,y2;
Graphics g;
public void showUI(){
JFrame frame  =  new JFrame();
frame.setSize(800,800);
frame.setTitle("easydraw");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(3);
//JFrame 默认布局 BorderLayout JPanel 默认布局 FlowLayout
   //设置面板 JPanel
JPanel tool_panel = new JPanel();
JPanel color_panel = new JPanel();
JPanel draw_panel = new JPanel();
//装饰面板
setToolPanel(tool_panel);
setColorPanel(color_panel);
setDrawPanel(draw_panel);
//添加到窗体  有五个位置 添加到指定位置
frame.add(tool_panel,BorderLayout.WEST);
frame.add(color_panel,BorderLayout.SOUTH);
frame.add(draw_panel,BorderLayout.CENTER);
frame.setVisible(true);
//在中间获取画布
g = draw_panel.getGraphics();
draw_panel.addMouseListener(mouse_listener);
}
MouseListener mouse_listener = new MouseListener(){
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
public void mouseClicked(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("oval")){
if(x2>x1&&y2>=y1)g.drawRect(x1,y1,x2-x1,y2-y1);
if(x2<x1&&y2<=y1) g.drawRect(x2,y2,x1-x2,y1-y2);
if(x2>x1&&y2<y1) g.drawRect(x1,y2,x2-x1,y1-y2);
if(x2<x1&&y2>y1) g.drawRect(x2,y1,x1-x2,y2-y1);
}
if(shape.equals("rect")){
if(x2>x1&&y2>=y1)g.drawOval(x1,y1,x2-x1,y2-y1);
if(x2<x1&&y2<=y1) g.drawOval(x2,y2,x1-x2,y1-y2);
if(x2>x1&&y2<y1) g.drawOval(x1,y2,x2-x1,y1-y2);
if(x2<x1&&y2>y1) g.drawOval(x2,y1,x1-x2,y2-y1);
}
}
};
//装饰工具面板的方法
public void setToolPanel(JPanel tool_panel){
//setsize 试用于重量级容器
tool_panel.setPreferredSize(new Dimension(50,500));
tool_panel.setBackground(Color.lightGray);
//监听按钮
ActionListener shape_listener = new ActionListener(){
public void actionPerformed(ActionEvent e){
//获取按钮上的标记
shape = e.getActionCommand();
}
};
String[] imagename = {"airbrush","brush","color_picker","curve","dot_rect","earser","fill",
"line","magnifier","oval","pencil","polygon","rect","round_rect","star","thumbs","word"
};
for(int i=0;i<imagename.length;i++){
JButton shape_button = new JButton();
ImageIcon icon = new ImageIcon("images//"+imagename[i]+".jpg");
//设置按钮图标 按钮大小 做标记 添加动作监听
shape_button.setIcon(icon);
shape_button.setPreferredSize(new Dimension(25,25));
shape_button.setActionCommand(imagename[i]);
shape_button.addActionListener(shape_listener);
tool_panel.add(shape_button);
}
}
//装饰颜色面板
public void setColorPanel(JPanel color_panel){
color_panel.setPreferredSize(new Dimension(500,100));
color_panel.setBackground(Color.blue);
ActionListener color_listener = new ActionListener(){
public void actionPerformed(ActionEvent e){
JButton button = (JButton)e.getSource();
Color color = button.getBackground();
g.setColor(color);
}
};
Color[] color = {Color.red,Color.blue,Color.gray,Color.green,
Color.white,Color.black,Color.yellow,Color.orange,
Color.pink,Color.lightGray,new Color(100,0,200)};
//创建颜色按钮对象 循环
for(int i=0;i<color.length;i++){
JButton color_button = new JButton();
color_button.setPreferredSize(new Dimension(25,25));
color_button.setBackground(color[i]);
color_button.addActionListener(color_listener);
color_panel.add(color_button);
}
}
//装饰画图面板
public void setDrawPanel(JPanel draw_panel){
draw_panel.setBackground(Color.white);
}
public static void main(String[] arga){
Draw draw = new Draw();
draw.showUI();
}
}

贴了一波代码,,然后作业还没写,学的东西可以听懂,只是记忆力不行,多打几遍会好些,今天又开始写博客,说实话是因为需要整理一下思路。


从第一步开始,java面向对象,显示showUi方法,创建窗体对象JFrame (默认BorderLayout布局),大小,标题,位置,设置关闭窗体时默认退出,创建三个面板JPanel(默认FL布局),面板方法分开写,装饰面板setTool,将面板添加到窗体,设置窗体可见bool值true,在draw_panel获取画布,draw_panel添加鼠标监听

第二步,鼠标监听器

第三步,装饰tool_panel,局部变量需在定义,参数。

tool_panel背景颜色,大小PreformedSize(new Dimension),创建按钮对象,用for,按钮的大小,用数组记录图片名;添加动作监听器,获取按钮上的标记;

关联图片路径ImageIcon,设置按钮图标setIcon,给按钮做标记setActionCommand,按钮添加到tool_panel

第四步,装饰color-panel 大小,尺寸,动作监听器,监听颜色按钮,获取被点击的按钮对象,Color color保存获取按钮的背景颜色,改变画布颜色

,创建颜色数组,创建颜色按钮对象,setBackground(color[i]),,大小,添加

第五步,装饰画图面板,,颜色

最后主函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值