唉,今天是一个繁忙而又充实的一天,因为今天的我第一次坐在电脑桌旁这么久,感觉手都有点酸,但是真的觉得收获还是蛮多的,,,,
好了,接下来就直接看代码吧 ,效果自己执行看一下吧:
第一步,新建一个主函数(我用记事本写的 不是很规范,还望多多包涵):
public class Test {
public static void main(String []args){
DraWing draWing=new DraWing();
draWing.ShowMainJFrame();
}
}
第二布,最主要的一步,建立一个加载窗口和一些属性 DraWing类 :
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.BevelBorder;
public class DraWing {
//------------------------------------------
public JButton btn2;
public JMenu menu ;
public JFrame frame;
public JPanel jp_left;
public JPanel jp_down;
public JPanel jp_ShowColor;//显示当前选择的颜色
public Graphics g;//画布
public JPanel jp_Center;
public ActionListener button_listener;
public ActionListener button_Color1;
public MyMouseListener button_mouseListener;
public JPanel jp_HistoryPanel;
public String command;//设置选择工具栏的按钮
public String function="10";
public Color button_color1;//负责按钮的颜色设置
//------------------------------------------
//=========Show JFrame======================
public void ShowMainJFrame(){
//=========Set JFrame=======================
frame=new JFrame();
frame.setSize(700,500);
frame.setLocation(300,50);
frame.setTitle("DraWing");
frame.setDefaultCloseOperation(3);
setMenu();
//=========Set JFrame Layout================
BorderLayout layout=new BorderLayout();
frame.setLayout(layout);
//=========To instance Linstener============
button_listener=new ButtonListener(this);
button_Color1=new Button_Color1(this);
button_mouseListener=new MyMouseListener(this);
//=========Call Method======================
setLeftPanel();//左边的面板
setDownPanel();//下面的面板
setCenterPanel();//中间的面板
ShowColor (); //显示当前先择的颜色的颜色面板
setHistoryPanel();//颜色板上的小板块用来放颜色按钮
ToInstanceButton();//功能面板的按钮
setSetPanel();//设置面板
ToInstanceColorButton();//颜色版上的颜色选择按钮
//==============Show JFrame=================
frame.setVisible(true);
//=========getGraphicd======================
g = jp_Center.getGraphics();
}
//=====================Method===========================================================
//=========Set LeftPanel====================
public void setLeftPanel(){
jp_left=new JPanel();
//---------Set LeftPanel Color--------------
Color color=new Color(216,216,216);
jp_left.setBackground(color);
//---------Establish Dimension-------------
Dimension dimen_left=new Dimension(70,0);
//---------Set Leftpanel Size---------------
jp_left.setPreferredSize(dimen_left);
//---------Add up JFrame--------------------
frame.add(jp_left,BorderLayout.WEST);
}
//========Set DownPanel=====================
public void setDownPanel(){
jp_down=new JPanel();
//--------Set DownPandel Color--------------
Color color=new Color(216,216,216);
jp_down.setBackground(color);
//--------Establish Dimension--------------
Dimension dimen_down = new Dimension(0, 50);
//--------Set DownPandelSize----------------
jp_down.setPreferredSize(dimen_down);
//--------Set Alignment---------------------
FlowLayout layout=new FlowLayout(0,0,0);
jp_down.setLayout(layout);
//jp_down.setAlignment(0);
//--------Add up JFrame---------------------
frame.add(jp_down,BorderLayout.SOUTH);
}
//========Set CenterPanel===================
public void setCenterPanel(){
jp_Center=new JPanel();
//--------Set CenterPandel Color------------
Color color=new Color(250,250,250);
jp_Center.setBackground(color);
//--------Add up JFrame---------------------
frame.add(jp_Center,BorderLayout.CENTER);
//---------add MouseListener----------------
jp_Center.addMouseListener(button_mouseListener);
jp_Center.addMouseMotionListener(button_mouseListener);
}
//=========To instance button===========================================================
public void ToInstanceButton(){
//=========Defubution StringArray=======================================================
String button_image[]={"images/1.png","images/2.png","images/3.png","images/4.png",
"images/5.png","images/6.png","images/7.png","images/8.png",
"images/9.png","images/10.png","images/11.png","images/12.png",
"images/13.png","images/14.png","images/15.png","images/16.png"};
//======================================================================================
//------------------------------------------
for(int i=0;i<button_image.length;i++){
JButton button=new JButton();
//---------add jp_left up-------------------
jp_left.add(button);
//---------set button Size------------------
Dimension dim=new Dimension(30,30);
button.setPreferredSize(dim);
//---------Set button command---------------
button.setActionCommand(i+"");
//---------Set button Picture---------------
ImageIcon image=new ImageIcon(button_image[i]);
button.setIcon(image);
//---------add Listener---------------------
button.addActionListener(button_listener);
}
}
//==========Show Corl ======================
public void ShowColor () {
jp_ShowColor = new JPanel();
//--------Set Color-------------------------
Color color=new Color(216,216,216);
jp_ShowColor.setBackground(color);
//--------Establish Dimension--------------
Dimension dimen_down = new Dimension(50, 50);
//--------Set Size--------------------------
jp_ShowColor.setPreferredSize(dimen_down);
//---------Add HistoryPanel-----------------
//设置绝对布局
jp_ShowColor.setLayout(null);
//==============================================
BevelBorder bevel = new BevelBorder(1, Color.white, Color.gray);
jp_ShowColor.setBorder(bevel);
jp_down.add(jp_ShowColor);
//========显示按钮===========================
btn2=new JButton();
JButton btn3=new JButton();
Color color1=new Color(250,250,250);
btn2.setBackground(Color.black);
btn3.setBackground(color1);
btn2.setBorder(bevel);
btn3.setBorder(bevel);
btn2.setBounds(10,10,20,20);
btn3.setBounds(20,20,20,20);
btn2.setEnabled(false);
btn3.setEnabled(false);
jp_ShowColor.add(btn2);
jp_ShowColor.add(btn3);
}
//=========To instance Color Button==================================================
public void ToInstanceColorButton(){
//===================================================================
Color button_Color[]={Color.black,Color.blue,Color.gray,Color.red,Color.cyan ,Color.darkGray ,Color.green,Color.magenta ,Color. orange ,Color. pink,Color.white,Color. yellow ,Color.red,Color.blue,Color.blue,Color.blue};
for(int i=0;i<button_Color.length;i++){
JButton btn1=new JButton();
//---------Set button Color-----------------
btn1.setBackground(button_Color[i]);
//---------Set biankuang liangdu yingyin----
BevelBorder bevel=new BevelBorder(1, Color.white, Color.gray);
btn1.setBorder(bevel);
//---------Set ColorButton Size-------------
Dimension dim=new Dimension(25,25);
btn1.setPreferredSize(dim);
//---------Set button command---------------
btn1.setActionCommand(i+"");
//---------button_Color Add jp_down up------
jp_HistoryPanel.add(btn1);
//---------Add Listener---------------------
btn1.addActionListener(button_Color1);
}
}
//==============Set SetPanel================
public void setSetPanel(){
//--------------To instance JPanel----------
JPanel jp_setPanel=new JPanel();
//--------------To instance Dimension-------
Dimension dim=new Dimension(50,85);
//--------------Set jp_setPanelSize---------
jp_setPanel.setPreferredSize(dim);
//--------------jp_setPanelAddjp_leftUp-----
jp_left.add(jp_setPanel);
}
//==============Set gongjuPanel============
public void setHistoryPanel(){
jp_HistoryPanel=new JPanel();
//--------------To instance Dimension-------
Dimension dim1=new Dimension(200,50);
//--------------Set jp_HistoryPanel Size----
jp_HistoryPanel.setPreferredSize(dim1);
//--------------Set Layout------------------
FlowLayout layout=new FlowLayout(0,0,0);
jp_HistoryPanel.setLayout(layout);
//--------------jp_HistoryPanelADDjp_downUp-
jp_down.add(jp_HistoryPanel);
}
public void setMenu(){
//创建菜单条
JMenuBar menuBar=new JMenuBar();
//添加菜单
JMenu menu1=new JMenu("文件(F)");
JMenu menu2=new JMenu("编辑(E)");
JMenu menu3=new JMenu("查看(V)");
JMenu menu4=new JMenu("图像(I)");
JMenu menu5=new JMenu("颜色(C)");
JMenu menu6=new JMenu("帮助(H)");
//设置菜单项
menuBar.add(menu1);
menuBar.add(menu2);
menuBar.add(menu3);
menuBar.add(menu4);
menuBar.add(menu5);
menuBar.add(menu6);
//-------------menu1-----------------------
JMenuItem item1 = new JMenuItem("新建(N)");
JMenuItem item2 = new JMenuItem("打开(O)");
JMenuItem item3 = new JMenuItem("保存(S)");
JMenuItem item4 = new JMenuItem("另存为(A)...");
JMenuItem item5 = new JMenuItem("退出(X)");
menu1.add(item1);
menu1.add(item2);
menu1.add(item3);
menu1.add(item4);
menu1.add(item5);
//-------------menu2-----------------------
JMenuItem item6 = new JMenuItem("撤销(U)");
JMenuItem item7 = new JMenuItem("重复(R)");
JMenuItem item8 = new JMenuItem("剪切(T)");
JMenuItem item9 = new JMenuItem("复制(C)");
JMenuItem item10 =new JMenuItem("黏贴(P)");
JMenuItem item11 =new JMenuItem("清楚选择内容(L)");
JMenuItem item12 =new JMenuItem("全选(A)");
JMenuItem item13 =new JMenuItem("复制到(O)");
JMenuItem item14 =new JMenuItem("黏贴来源(F)");
menu2.add(item6);
menu2.add(item7);
menu2.add(item8);
menu2.add(item9);
menu2.add(item10);
menu2.add(item11);
menu2.add(item12);
menu2.add(item13);
menu2.add(item14);
//-------------menu3-----------------------
JMenuItem item15 = new JMenuItem("工具箱(T)");
JMenuItem item16 = new JMenuItem("颜色盒(C)");
JMenuItem item17 = new JMenuItem("状态栏(S)");
JMenuItem item18 = new JMenuItem("文字工具栏(E)");
JMenuItem item19 = new JMenuItem("缩放(Z)");
JMenuItem item20 = new JMenuItem("查看位图(V)");
menu3.add(item15);
menu3.add(item16);
menu3.add(item17);
menu3.add(item18);
menu3.add(item19);
menu3.add(item20);
//-------------menu4-----------------------
JMenuItem item21 = new JMenuItem("翻转/旋转(F)");
JMenuItem item22 = new JMenuItem("拉伸/扭曲(S)");
JMenuItem item23 = new JMenuItem("反色(I)");
JMenuItem item24 = new JMenuItem("属性(A)");
JMenuItem item25 = new JMenuItem("清楚图像(C)");
JMenuItem item26 = new JMenuItem("不透明处理(D)");
menu4.add(item21);
menu4.add(item22);
menu4.add(item23);
menu4.add(item24);
menu4.add(item25);
menu4.add(item26);
//-------------menu5------------------------
JMenuItem item27 = new JMenuItem("编辑颜色(E)");
menu5.add(item27);
//-------------menu6------------------------
JMenuItem item28 = new JMenuItem("帮助主题(H)");
JMenuItem item29 = new JMenuItem("关于画布(A)");
menu6.add(item28);
menu6.add(item29);
//把菜单条添加到窗体上
frame.setJMenuBar(menuBar);
//把菜单条添加到窗体上
}
}
第三步,在创建一个 Button_Color 类(也就是按钮颜色选择框类):
import javax.swing.JButton;
import java.awt.event.*;
public class Button_Color implements ActionListener{
public DraWing command=null;
//===============Constructors(构造函数)==========================
public Button_Color(DraWing command1){
command=command1;
}
//===================Monitor control(监控 控制)======================
public void actionPerformed(){
Object obj= e.getSource(); //-----------获取事件源返回对象
//----------强制把object类型转型为JButton类型----------------------
JButton button=(JButton)obj;
//-------------获取背景颜色复制接btn1 显示颜色面板---------------------
command.btn2.setBackground(button.getBackground());
}
}
第四步,在往里面加上一个 MouseListener(鼠标监听事件)类:
import java.awt.event.*;
public class MouseListener implements MouseListener,MouseMotionListener{
public int x1,x2,x3,x4,y1,y2,y3,y4;
public int first=0; //
public DraWing command=null;
//=================Constructors(构造函数)===================================
public MouseListener(){
command= command1;
}
//=================Set Color===============================================
public void mouseClicked(MouseEvent arg0){}
public void mouseEntered(MouseEvent arg0){}
public void mouseExited(MouseEvent arg0){}
//-------------------------------------------------------------------------
public void mousePressed(MouseEvent e){ //按下
x1=e.getX();
y1=e.getY();
command.g.setColor(command.btn2.getBackground());
}
//-----------------------------------------------------------------------------
public void mouseReleased(){
x2=e.getX();
y2=e.getY();
if(command.function.equals("13")){ //矩形直线
if(first==1){ //如果没有画下第一根线就不继续画
command.g.drawLine(x4,y4,x1,y1);
x4=x1;
y4=y1;
}else if(first==0){ //如果是第一次画就画直线
x3=x1;y3=y1;x4=x2;y4=y2;
command.g.drawLine(x3,y3,x4,y4);
frist=1;
}
int n=e.getClickCount(); //获取单机次数
if(n==2){
command.g.drawLine(x4,y4,x3,y3);
frist=0;
}
}
if(command.function.equals("10")){ //直线
command.g.drawLine(x1,y1,x2,y2);
}else if(command.function.equals("12")){ //矩形
command.g.drawRect(Math.min(x1,y1),Main.min(y1,y2),Math.abs(x2-x1),Math.abs(y2-y1));
}else if(command.function.equals("14")) //椭圆
command.g.drawOval(Math.min(x1,y1),Main.min(y1,y2),Math.abs(x2-x1),Math.abs(y2-y1));
}else if(command.function.equals("15")) //圆角矩形
command.g.drawRoundRect(Math.min(x1,y1),Main.min(y1,y2),Math.abs(x2-x1),Math.abs(y2-y1),20,20);
}
//--------------------------------------------------------------------------------------------------
public void mouseDragged(MouseEvent e){
if(command.function.equals("2")){ //橡皮擦
command.g.setColor(command.jp_Center.getBackground());
x1=e.getX();
y1=e.getY();
command.g.fillRect(x1-5,y1-5,10,10);
//--------------------------------------------------------
}else if(command.function.equals("6")){ //铅笔
x2=e.getX();
y2=e.getY();
command.g.drawLine(x1,y1,x2,y2);
x1=x2;
y1=y2;
}
}
//====================================================================================
public void mouseMoved(){
}
}
第五步,也是最后一步,在加一个Button_Listener (按钮监听事件)类,:
import java.awt.event.*;
public class ButtonListener implements ActionListener{
public DraWing command=null;
//===============Constructors====================
public ButtonListener(DraWing command1){
command=command1;
}
//===============Monitor control=================
public void actionPerformed(ActionEvent e){
String cm1=e.getActionCommand();
command.function=cm1;
}
}
好了,代码我已经全部发给你们了,感兴趣的游客可以自己去试运行一下,由于这是我第一个版本,所以功能有些不全,所以还希望别喷啊。。。。。
今天就先写到这里,接下来就继续每天的自我提醒吧:
1、每天锻炼指法一小时;
2、每天坚持写一篇博客或者日志;
3、每天上课提前15分钟进教室;
4、给每天的自己打打气-------你也许不是全班最好的,但是我相信你不是最差劲的,相信自己我能成功。