关于仿XP画图板的实现以及内部类与外部类的优劣

       前几天看了一些大牛的博客,顿时觉得自己的博客写的不好,不够详细,不够深刻,今天我就要写一些有力量的东西,顺便说一句胡哥说:“只要博客写的好,就能找到妹子”,我先试一下。言归正传,先传一下我的仿XP画板的图片


关于仿XP画图板的实现以及内部类与外部类的优劣_第1张图片
        至于整个画图板的功能,实现了一些选颜色与工具的功能,没有涉及到文件的部分功能,这篇文章主要就是通过画图板这个实例讲一下当一个项目的类的数量过多的时候怎样传值,以及在那种情况下使用内部类和外部类。

        写画图板的时候,刚开始必须要先将画图板的布局,以及组件加上。请看下面的代码

package Draw;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSeparator;

/**
 * 此类主要是显示面板,并向其上面添加组件
 * @author 李伟 
 *
 */
public class DrawBord extends JFrame{
	private JMenuBar jmenubar;
	private JPanel panelWest;
	private JPanel panelSouth;
	private JPanel panelCentral;
	private Graphics g;
	private JButton but01;
	private ButColorListener bl;
	private MenuItemListener ml;
	//添加标志
	public static String biaozhi="huabi";
	//定义按钮
	private JButton but;
	//定义显示的方法
	public void showUI(){
		panelCentral = creatCentral();
		//添加菜单条 
		jmenubar = creatMenuBar();
		this.setJMenuBar(jmenubar);
		//添加工具栏
		panelWest = creatWest();
		//添加颜色选择栏
		panelSouth = creatSouth();
	
		//设置画板标题
		this.setTitle("仿XP画板");
		//设置画板大小尺寸
		this.setSize(800,600);
		//设置画板显示在窗体中央
		this.setLocationRelativeTo(null);
		this.add(panelCentral,BorderLayout.CENTER);
		this.add(panelWest,BorderLayout.WEST);
		this.add(panelSouth,BorderLayout.SOUTH);
	
		this.setDefaultCloseOperation(3);
		//设置窗体为不可改变大小的
		this.setResizable(false);
		//设置窗体可见
		this.setVisible(true);
		g = panelCentral.getGraphics();
		bl.setGraphics(g);
		ml.setGraphics(g);
		ml.setPanel(panelCentral);
		FrameListener fl = new FrameListener(g);
		//将画布及鼠标监听器传过去
		panelCentral.addMouseListener(fl);
		FrameMouseMotion mm= new FrameMouseMotion(g);
		panelCentral.addMouseMotionListener(mm);		
	}
	/*********设置画板上的菜单栏***********/
	private JMenuBar creatMenuBar(){
		 ml = new MenuItemListener(this);
		//创建菜单栏
		JMenuBar menubar = new JMenuBar();
		//设置菜单
		JMenu  menu01 = new JMenu("文件(F)");
		JMenu  menu02 = new JMenu("编辑(E)");
		JMenu  menu03 = new JMenu("查看(V)");
		JMenu  menu04 = new JMenu("图像(I)");
		JMenu  menu05 = new JMenu("颜色(C)");
		JMenu  menu06 = new JMenu("帮助(H)");
		JMenu  menu07 = new JMenu("自选图形");
		//创建文件菜单项
		JMenuItem FItem01 = new JMenuItem("新建(N)");
		JMenuItem FItem02 = new JMenuItem("打开(O)");
		JMenuItem FItem03 = new JMenuItem("保存(S)");
		JMenuItem FItem04 = new JMenuItem("另存为(A)");
		JSeparator Separator01 = new JSeparator();
		JMenuItem FItem05 = new JMenuItem("退出(X)");
		FItem05.addActionListener(ml);
		menu01.add(FItem01);
		menu01.add(FItem02);
		menu01.add(FItem03);
		menu01.add(FItem04);
		menu01.add(Separator01);
		menu01.add(FItem05);
		//创建编辑菜单项
		JMenuItem  EItem01 = new JMenuItem("撤销(U)");
		JMenuItem  EItem02 = new JMenuItem("重复(R)");
		JSeparator Separator02 = new JSeparator();
		JMenuItem  EItem03 = new JMenuItem("剪切(X)");
		JMenuItem  EItem04 = new JMenuItem("复制(C)");
		JMenuItem  EItem05= new JMenuItem("粘贴(V)");
		menu02.add(EItem01);
		menu02.add(EItem02);
		menu02.add(Separator02);
		menu02.add(EItem03);
		menu02.add(EItem04);
		menu02.add(EItem05);
		//创建查看菜单项
		JMenuItem VItem01 = new JMenuItem("没有东西");
		menu03.add(VItem01);
		//创建图像菜单项
		JMenuItem IItem01 = new JMenuItem("清除图像");
		IItem01.addActionListener(ml);
		menu04.add(IItem01);
		//创建颜色菜单项
		JMenuItem CItem01 = new JMenuItem("颜色选择");
		CItem01.addActionListener(ml);
		menu05.add(CItem01);
		//创建帮助菜单项
		JMenuItem HItem01 = new JMenuItem("关于画图");
		HItem01.addActionListener(ml);
		menu06.add(HItem01);
		//创建自选图形菜单项
		JMenuItem ZItem01 = new JMenuItem("科赫曲线");
		JMenuItem ZItem02 = new JMenuItem("科赫雪花");
		JMenuItem ZItem03 = new JMenuItem("毕达哥拉斯树");
		JMenuItem ZItem04 = new JMenuItem("树");
		JMenuItem ZItem05= new JMenuItem("科赫曲线");
		JMenuItem ZItem06= new JMenuItem("科赫曲线");
		menu07.add(ZItem01);
		menu07.add(ZItem02);
		menu07.add(ZItem03);
		menu07.add(ZItem04);
		menu07.add(ZItem05);
		menu07.add(ZItem06);
		//想菜单条上加上菜单
		menubar.add(menu01);
		menubar.add(menu02);
		menubar.add(menu03);
		menubar.add(menu04);
		menubar.add(menu05);
		menubar.add(menu07);
		menubar.add(menu06);		
		return menubar;
	}
/***********创建左边工具选择面板***********/
	private JPanel creatWest(){
		JPanel panel = new JPanel();
		panel.setPreferredSize(new Dimension(70,500));
		//创建一个监听器类
		ToolButListener tb = new ToolButListener(biaozhi);
		String[] arr = { "duobianxing", "Xjuxing", "ca", "tianchong", "dianbi",
				"fangdajing", "huabi", "shuazi", "youqitong", "bianji",
				"zhixian", "quxian", "juxing", "shouwei", "tuoyuan", "yuanji" };
		for (int i = 0; i < arr.length; i++) {
			ImageIcon img = new ImageIcon("DrawImages/" + arr[i] + ".jpg");
			but = new JButton(img);
			but.setActionCommand(arr[i]);	
			but.setPreferredSize(new Dimension(24, 22));
			but.addActionListener(tb);
			panel.add(but);
		}
		return panel;
	}
	
/***********创建南面的颜色框*****************/
	
	private JPanel creatSouth(){
		JPanel panel = new JPanel();
		
		panel.setPreferredSize(new Dimension(800,60));
		JPanel panel01 = new JPanel();
		panel.setLayout(null);
		but01 = new JButton();
		but01.setBackground(Color.BLACK);
		but01.setBounds(70, 5, 50, 50);
		panel.add(but01);
		panel01.setPreferredSize(new Dimension(160,60));
		//添加颜色按钮监听器
		bl = new ButColorListener(but01);
		String[] arr01 = {"Color.black","Color.gray","Color.darkGray","Color.green","Color.LIGHT_GRAY","Color.magenta",
				"Color.orange","Color.pink","Color.red","Color.white"};
		Color [] arr02={Color.black,Color.gray,Color.darkGray,Color.green,Color.LIGHT_GRAY,Color.magenta,
				Color.orange,Color.pink,Color.red,Color.white};
		for(int i=0;i<arr01.length;i++){
			JButton but = new JButton();		
			but.setBackground(arr02[i]);
			but.setActionCommand(arr01[i]);
			but.setPreferredSize(new Dimension(22, 22));
			but.addActionListener(bl);
			
			panel01.add(but);
		}
		panel01.setBounds(110, 0, 160, 60);
		panel.add(panel01);
		return panel;
	}
	
/*********创建中间的画布****************/
	
	private JPanel creatCentral(){
		JPanel panel = new JPanel();
		panel.setBackground(Color.WHITE);
		g = panel.getGraphics();
		return panel;
	}
	

}

 当你看到这些代码,可能已经晕了,不用急,让我一步一步解释一下,在刚开始的时候就是添加一些组件的代码,应该没有什么难以理解,如果无法理解就看注释吧,应该很清晰的,我想讲一下就是我将添加按钮写在一个方法里,如下部分代码:

/***********创建左边工具选择面板***********/
	private JPanel creatWest(){
		JPanel panel = new JPanel();
		panel.setPreferredSize(new Dimension(70,500));
		//创建一个监听器类
		ToolButListener tb = new ToolButListener(biaozhi);
		String[] arr = { "duobianxing", "Xjuxing", "ca", "tianchong", "dianbi",
				"fangdajing", "huabi", "shuazi", "youqitong", "bianji",
				"zhixian", "quxian", "juxing", "shouwei", "tuoyuan", "yuanji" };
		for (int i = 0; i < arr.length; i++) {
			ImageIcon img = new ImageIcon("DrawImages/" + arr[i] + ".jpg");
			but = new JButton(img);
			but.setActionCommand(arr[i]);	
			but.setPreferredSize(new Dimension(24, 22));
			but.addActionListener(tb);
			panel.add(but);
		}
		return panel;
	}

 通过for循环添加按钮,那么此时也必须实例化按钮监听器类,这样问题就来了,我们画图的时候,必须要在窗体可见后才能获画布对象 Graphics ,这样就会出现怎样将Graphics对象传到监听器类中,可以看如下代码:

		//设置窗体可见
		this.setVisible(true);
		g = panelCentral.getGraphics();
		bl.setGraphics(g);
		ml.setGraphics(g);
		ml.setPanel(panelCentral);

 通过在监听器类里设置set和get方法就能解决问题了,当然还能在监听器类实例化是传入,这种方法在这里不适合。接下类看一下监听器中的具体代码:

package Draw;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;

public class ButColorListener implements ActionListener{
	private JButton but01;
	private Graphics g;

	public ButColorListener(JButton but01) {
		// TODO Auto-generated constructor stub
		this.but01=but01;
	}
	
	public void setGraphics(Graphics g){
		this.g = g;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
//		Graphics g = this.panelCentral.getGraphics();
		if(e.getActionCommand().equals("Color.black")){
			this.but01.setBackground(Color.BLACK);
			g.setColor(Color.BLACK);
			
		}if(e.getActionCommand().equals("Color.gray")){
			this.but01.setBackground(Color.gray);
			g.setColor(Color.gray);
		}if(e.getActionCommand().equals("Color.darkGray")){
			this.but01.setBackground(Color.darkGray);
			g.setColor(Color.darkGray);
			
		}if(e.getActionCommand().equals("Color.green")){
			this.but01.setBackground(Color.green);
			g.setColor(Color.green);
			
		}if(e.getActionCommand().equals("Color.LIGHT_GRAY")){
			this.but01.setBackground(Color.LIGHT_GRAY);
			g.setColor(Color.LIGHT_GRAY);
			
		}if(e.getActionCommand().equals("Color.magenta")){
			this.but01.setBackground(Color.magenta);
			g.setColor(Color.magenta);
			
		}if(e.getActionCommand().equals("Color.orange")){
			this.but01.setBackground(Color.orange);
			g.setColor(Color.orange);
			
		}if(e.getActionCommand().equals("Color.pink")){
			this.but01.setBackground(Color.pink);
			g.setColor(Color.pink);
			
		}if(e.getActionCommand().equals("Color.red")){
			this.but01.setBackground(Color.red);
			g.setColor(Color.red);
			
		}if(e.getActionCommand().equals("Color.white")){
			this.but01.setBackground(Color.white);
			g.setColor(Color.white);
			
		}
		
	}
	

}

 从以上代码可以看看到如下:

	public void setGraphics(Graphics g){
		this.g = g;
	}

 这样我们就获取了画布对象。那么我们就可调用这个g了;

以上就是通过set和get方法将对象传过去的,我们再将一下通过构造方法将对象传过去的方法,看如下代码:

		FrameListener fl = new FrameListener(g);
		//将画布及鼠标监听器传过去
		panelCentral.addMouseListener(fl);
		FrameMouseMotion mm= new FrameMouseMotion(g);
		panelCentral.addMouseMotionListener(mm);	

 我们直接通过构造函数将对象传过去了,那我们在看看实例化的对象的那边的代码:

package Draw;

import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class FrameListener implements MouseListener {
	private Graphics g;
	//定义鼠标的获取的点的坐标
//	private int x1;
//	private int y1;
	public static int x1;
	public static int y1;
	private int x2;
	private int y2;

	public FrameListener(Graphics g) {
		
		this.g = g;
	}

//	public int getX1(){
//		return this.x1;
//	}
//	public int gety1(){
//		return this.y1;
//	}
	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		x1 = e.getX();
		y1 = e.getY();
		
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		x2 = e.getX();
		y2 = e.getY();
		//具体画的方法
		if(DrawBord.biaozhi.equals("zhixian")){
			g.drawLine(x1, y1, x2, y2);
			
		}else if(DrawBord.biaozhi.equals("juxing")){
			
			g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2));
		}else if(DrawBord.biaozhi.equals("yuanji")){
			g.drawRoundRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2),25,25);
			
		}else if(DrawBord.biaozhi.equals("tuoyuan")){
			g.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2));
			
		}
		FrameMouseMotion.state=1;
		
		
	}

}

 就这样。我们也将画布对象传过去了。至此我们已将传对象的方法说了,下面继续发一下画图板的其他代码,一下的是画图板鼠标监听器类的代码:

package Draw;

import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class FrameListener implements MouseListener {
	private Graphics g;
	//定义鼠标的获取的点的坐标
//	private int x1;
//	private int y1;
	public static int x1;
	public static int y1;
	private int x2;
	private int y2;

	public FrameListener(Graphics g) {
		
		this.g = g;
	}

//	public int getX1(){
//		return this.x1;
//	}
//	public int gety1(){
//		return this.y1;
//	}
	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		x1 = e.getX();
		y1 = e.getY();
		
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		x2 = e.getX();
		y2 = e.getY();
		//具体画的方法
		if(DrawBord.biaozhi.equals("zhixian")){
			g.drawLine(x1, y1, x2, y2);
			
		}else if(DrawBord.biaozhi.equals("juxing")){
			
			g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2));
		}else if(DrawBord.biaozhi.equals("yuanji")){
			g.drawRoundRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2),25,25);
			
		}else if(DrawBord.biaozhi.equals("tuoyuan")){
			g.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2));
			
		}
		FrameMouseMotion.state=1;
		
		
	}

}

 接下来是鼠标移动时的鼠标监听器类的代码:

package Draw;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

public class FrameMouseMotion implements MouseMotionListener {
	
	//将画布对象传过来
	private Graphics g;
	private int x1;
	private int y1;
	private int x2;
	private int y2;
	public static int state =1;
	//构造方法

	public FrameMouseMotion(Graphics g) {
		// TODO Auto-generated constructor stub
		this.g = g;
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub

		x2 = e.getX();
		y2 = e.getY();
		if(DrawBord.biaozhi.equals("huabi")){
			if(state==1){
				g.drawLine(x2, y2, x2, y2);
				state =2;
			}else if(state==2){
				g.drawLine(x2, y2, x1, y1);
			}
			
		

			
		}else if (DrawBord.biaozhi.equals("ca")) {
			g.setColor(Color.white);
			g.fillRect(x1, y1, 10, 20);
			g.setColor(Color.black);
		} else if (DrawBord.biaozhi.equals("shuazi")) {

			g.fillRect(x1, y1, 5, 5);

		} else if (DrawBord.biaozhi.equals("youqitong")) {
			for (int i = 0; i < 20; i++) {
				int j = (int) (Math.random() * 10);
				int k = (int) (Math.random() * 10);
				g.drawLine(x2+ j, y2 + k, x2 + j, y2 + k);
			}

		}
		x1 = x2;
		y1 = y2;

		

		
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

}

 然后就是菜单栏的监听器类了:

package Draw;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class MenuItemListener implements ActionListener {
	
	private Graphics g;
	private DrawBord drawBord;
	private JPanel panel;
	
	public MenuItemListener(DrawBord drawBord) {
		this.drawBord=drawBord;
		
	}

	public void setPanel(JPanel panel){
		this.panel = panel;
	}
	public void setGraphics(Graphics g){
		this.g =g;
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getActionCommand().equals("退出(X)")){
			
			this.drawBord.setVisible(false);
			
		}else if(e.getActionCommand().equals("清除图像")){
			
			this.panel.paint(g);
			
		}else if(e.getActionCommand().equals("颜色选择")){
			JFrame frame = new JFrame();
			frame.setTitle("颜色选择");
			frame.setSize(300,300);
			JColorChooser color = new JColorChooser();
			frame.add(color);
			frame.setDefaultCloseOperation(3);
			JButton but = new JButton("确定");
			ColorConfirm cf = new ColorConfirm(frame,g,color);
			but.addActionListener(cf);
			but.setPreferredSize(new Dimension(100,20));
			frame.add(but,BorderLayout.SOUTH);
			frame.setVisible(true);
			
		}else if(e.getActionCommand().equals("关于画图")){
			JFrame frame = new JFrame();
			frame.setTitle("声明");
			AnounceConfirm ac = new AnounceConfirm(frame);
			String  str= "haha";
			JLabel label = new JLabel(str);
			JButton but = new JButton("确定");
			but.addActionListener(ac);
			label.setPreferredSize(new Dimension(300,300));
			frame.add(but,BorderLayout.SOUTH);
			frame.setSize(400,300);
			frame.setDefaultCloseOperation(3);
			frame.setVisible(true);
			
		}
		
	}

}

 接下来是菜单项的颜色选择器的代码

package Draw;

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JColorChooser;
import javax.swing.JFrame;

public class ColorConfirm implements ActionListener {
	private JFrame frame;
	private Graphics g;
	private JColorChooser color;
	public ColorConfirm(JFrame frame, Graphics g, JColorChooser color) {
		// TODO Auto-generated constructor stub
		this.frame = frame;
		this.g = g;
		this.color  = color;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		g.setColor(this.color.getColor());
		this.frame.setVisible(false);
		
		
	}

}

 接下来是工具栏的监听器:

package Draw;

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ToolButListener implements ActionListener {
	
	//添加一个标志
	private String biaozhi;

	public ToolButListener(String biaozhi) {
		// TODO Auto-generated constructor stub
		this.biaozhi =biaozhi;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("duobianxing")){
			
			DrawBord.biaozhi = "duobianxing";
			
		}else if(e.getActionCommand().equals("Xjuxing")){
			
			DrawBord.biaozhi = "Xjuxing";
			
		}else if(e.getActionCommand().equals("ca")){
			
			DrawBord.biaozhi = "ca";
			
		}else if(e.getActionCommand().equals("tianchong")){
			
			DrawBord.biaozhi = "tianchong";
			
		}else if(e.getActionCommand().equals("dianbi")){
			
			DrawBord.biaozhi = "dianbi";
			
		}else if(e.getActionCommand().equals("fangdajing")){
			
			DrawBord.biaozhi = "fangdajing";
			
		}else if(e.getActionCommand().equals("huabi")){
			
			DrawBord.biaozhi = "huabi";
			
		}else if(e.getActionCommand().equals("shuazi")){
			
			DrawBord.biaozhi = "shuazi";
			
		}else if(e.getActionCommand().equals("youqitong")){
			
			DrawBord.biaozhi = "youqitong";
			
		}else if(e.getActionCommand().equals("bianji")){
			
			DrawBord.biaozhi = "bianji";
			
		}else if(e.getActionCommand().equals("zhixian")){
			
			DrawBord.biaozhi = "zhixian";
			
		}else if(e.getActionCommand().equals("quxian")){
			
			DrawBord.biaozhi = "quxian";
			
		}else if(e.getActionCommand().equals("juxing")){
			
			DrawBord.biaozhi = "juxing";
			
		}else if(e.getActionCommand().equals("shouwei")){
			
			DrawBord.biaozhi = "shouwei";
			
		}else if(e.getActionCommand().equals("tuoyuan")){
			
			DrawBord.biaozhi = "tuoyuan";
			
		}else if(e.getActionCommand().equals("yuanji")){
			
			DrawBord.biaozhi = "yuanji";
			
		}
		
	}

}

 最后就是整个程序运行的manager类了:

package Draw;
//设置主程序入口类
public class Manager {
	
	public static void main(String[]args){
		
		DrawBord draw = new DrawBord();
		draw.showUI();
		
	}

}

 通过以上代码,我们能写出一个画图板,但是你可能回觉的太过麻烦了,接下来请看一下我通过内部类写的仿XP画图板,功能一样,,但是就用了一个类就搞定了:

package netjava0626;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;

//定义一个画板类
public class DrawingBoard {
	private String biaozhi = "huabi";
	private String   color;
	private Graphics g;
	private int x1, y1, x2, y2, x3, y3,  w, h;
	private JLabel lab;
	// 初始化面板并实现组建
	public void show() {
		JFrame frame = new JFrame();
		// 创建一个工具栏
		JButton but01 = new JButton("文件(F)");
		JButton but02 = new JButton("编辑(E)");
		JButton but03 = new JButton("查看(V)");
		JButton but04 = new JButton("图像(I)");
		JButton but05 = new JButton("颜色(C)");
		JButton but06 = new JButton("帮助(H)");
		JToolBar bar = new JToolBar();
		bar.add(but01);
		bar.add(but02);
		bar.add(but03);
		bar.add(but04);
		bar.add(but05);
		bar.add(but06);
		frame.add(bar, BorderLayout.NORTH);
		//创建颜色框的监听器
		ActionListener a3 = new ActionListener(){

			public void actionPerformed(ActionEvent e) {
//设置颜色
				color = e.getActionCommand();
				if(color.equals("Color.black") ){
				
					g.setColor(Color.black);
					lab.setBackground(Color.black);
				}else if(color.equals("Color.gray")){
					
					g.setColor(Color.gray);
					lab.setBackground(Color.gray);
				}else if(color.equals("Color.darkGray")){
					g.setColor(Color.darkGray);
					lab.setBackground(Color.darkGray);
				}else if(color.equals("Color.green")){
					
					g.setColor(Color.green);
					lab.setBackground(Color.green);
				}else if(color.equals("Color.LIGHT_GRAY")){
					
					g.setColor(Color.LIGHT_GRAY);
					lab.setBackground(Color.LIGHT_GRAY);
				}else if(color.equals("Color.magenta")){
					g.setColor(Color.magenta);
					lab.setBackground(Color.magenta);
				}else if(color.equals("Color.orange")){
					g.setColor(Color.orange);
					lab.setBackground(Color.orange);
				}else if(color.equals("Color.pink")){
					
					g.setColor(Color.pink);
					lab.setBackground(Color.pink);
				}else if(color.equals("Color.red")){
					g.setColor(Color.red);
					lab.setBackground(Color.red);
					
				}else if(color.equals("Color.white")){
					
					g.setColor(Color.white);
					lab.setBackground(Color.white);
				}	
			}
		};
		// 创建监听器
		ActionListener al = new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				biaozhi = e.getActionCommand();

			}

		};
		// 创建工具面板
		JPanel panel01 = new JPanel();
		panel01.setPreferredSize(new Dimension(60, 104));
		String[] arr = { "duobianxing", "Xjuxing", "ca", "tianchong", "dianbi",
				"fangdajing", "huabi", "shuazi", "youqitong", "bianji",
				"zhixian", "quxian", "juxing", "shouwei", "tuoyuan", "yuanji" };
		for (int i = 0; i < arr.length; i++) {
			ImageIcon img = new ImageIcon("DrawImages/" + arr[i] + ".jpg");
			JButton but = new JButton(img);
			but.setActionCommand(arr[i]);
			but.addActionListener(al);
			but.setPreferredSize(new Dimension(24, 22));
			panel01.add(but);
		}
		frame.add(panel01, BorderLayout.WEST);

		// 颜色选项栏
		JPanel panel02 = new JPanel();
		panel02.setPreferredSize(new Dimension(100, 50));
		panel02.setLayout(null);
		JPanel panel04 = new JPanel();
		panel04.setPreferredSize(new Dimension(100, 40));
//		panel04.setBackground(Color.white );
		panel04.setLayout(new GridLayout(2, 5));

		Color [] arr02={Color.black,Color.gray,Color.darkGray,Color.green,Color.LIGHT_GRAY,Color.magenta,
				Color.orange,Color.pink,Color.red,Color.white};
		String[] arr01 = {"Color.black","Color.gray","Color.darkGray","Color.green","Color.LIGHT_GRAY","Color.magenta",
				"Color.orange","Color.pink","Color.red","Color.white"};
		lab = new JLabel();
		lab.setPreferredSize(new Dimension());
		lab.setOpaque(true);
		lab.setBackground(Color.black);
		lab.setBounds(60, 0, 47, 50);
		for (int i = 0; i < arr02.length; i++) {
			
			
			JButton but = new JButton();		
			but.setBackground(arr02[i]);
			but.setActionCommand(arr01[i]);
			but.setPreferredSize(new Dimension(16, 16));
			but.addActionListener(a3);
			panel04.add(but);

		}
		panel02.add(lab);
		panel04.setBounds(107, 0, 100, 50);
		panel02.add(panel04);
		
		frame.add(panel02, BorderLayout.SOUTH);

		// 画布
		JPanel panel03 = new JPanel();
		panel03.setPreferredSize(new Dimension(694, 400));
		panel03.setBackground(Color.WHITE);
		frame.add(panel03, BorderLayout.CENTER);
		frame.setTitle("画图板");
		frame.setSize(754, 508);
		frame.setResizable(false);
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(3);

		MouseListener a1 = new MouseAdapter() {
			// 鼠標按下時獲得坐标
			public void mousePressed(MouseEvent e) {
				x1 = e.getX();
				y1 = e.getY();
			}
			// 鼠标松开时获取坐标
			public void mouseReleased(MouseEvent e) {
				x2 = e.getX();
				y2 = e.getY();
				if (biaozhi.equals("shouwei")) {
					g.drawLine(x1, y1, x1, y1);

				} else if (biaozhi.equals("dianbi")) {

				} else if (biaozhi.equals("zhixian")) {

					g.drawLine(x1, y1, x2, y2);
				} else if (biaozhi.equals("juxing")) {
					if (x2 > x1 && y2 > y1) {
						x3 = x1;
						y3 = y1;
						w = Math.abs(x2 - x1);
						h = Math.abs(y2 - y1);
						g.drawRect(x3, y3, w, h);
					} else if (x2 > x1 && y2 < y1) {
						x3 = x1;
						y3 = y2;
						w = Math.abs(x2 - x1);
						h = Math.abs(y2 - y1);
						g.drawRect(x3, y3, w, h);
					} else if (x2 < x1 && y2 < y1) {
						x3 = x2;
						y3 = y2;
						w = Math.abs(x2 - x1);
						h = Math.abs(y2 - y1);
						g.drawRect(x3, y3, w, h);
					} else {
						x3 = x2;
						y3 = y1;
						w = Math.abs(x2 - x1);
						h = Math.abs(y2 - y1);
						g.drawRect(x3, y3, w, h);
					}
				} else if (biaozhi.equals("tuoyuan")) {
					if (x2 > x1 && y2 > y1) {
						x3 = x1;
						y3 = y1;
						w = Math.abs(x2 - x1);
						h = Math.abs(y2 - y1);
						g.drawOval(x3, y3, w, h);
					} else if (x2 > x1 && y2 < y1) {
						x3 = x1;
						y3 = y2;
						w = Math.abs(x2 - x1);
						h = Math.abs(y2 - y1);
						g.drawOval(x3, y3, w, h);
					} else if (x2 < x1 && y2 < y1) {
						x3 = x2;
						y3 = y2;
						w = Math.abs(x2 - x1);
						h = Math.abs(y2 - y1);
						g.drawOval(x3, y3, w, h);
					} else {
						x3 = x2;
						y3 = y1;
						w = Math.abs(x2 - x1);
						h = Math.abs(y2 - y1);
						g.drawOval(x3, y3, w, h);
					}
				} else if (biaozhi.equals("yuanji")) {
					if (x2 > x1 && y2 > y1) {
						x3 = x1;
						y3 = y1;
						w = Math.abs(x2 - x1);
						h = Math.abs(y2 - y1);
						g.drawRoundRect(x3, y3, w, h, 20, 20);
					} else if (x2 > x1 && y2 < y1) {
						x3 = x1;
						y3 = y2;
						w = Math.abs(x2 - x1);
						h = Math.abs(y2 - y1);
						g.drawRoundRect(x3, y3, w, h, 20, 20);
					} else if (x2 < x1 && y2 < y1) {
						x3 = x2;
						y3 = y2;
						w = Math.abs(x2 - x1);
						h = Math.abs(y2 - y1);
						g.drawRoundRect(x3, y3, w, h, 20, 20);
					} else {
						x3 = x2;
						y3 = y1;
						w = Math.abs(x2 - x1);
						h = Math.abs(y2 - y1);
						g.drawRoundRect(x3, y3, w, h, 20, 20);
					}

				}
			}
		};
		// 添加拖動時的方法
		MouseMotionListener a2 = new MouseMotionAdapter() {

			public void mouseDragged(MouseEvent e) {
				x3 = e.getX();
				y3 = e.getY();
				// 当鼠标在拖动时也要注意对坐标的赋值
				if (biaozhi.equals("huabi")) {
					g.drawLine(x1, y1, x3, y3);
					x1 = x3;
					y1 = y3;
				} else if (biaozhi.equals("ca")) {
					g.setColor(Color.white);
					g.fillRect(x1, y1, 10, 20);
					x1 = x3;
					y1 = y3;
					g.setColor(Color.black);
				} else if (biaozhi.equals("shuazi")) {

					g.fillRect(x1, y1, 5, 5);
					x1 = x3;
					y1 = y3;

				} else if (biaozhi.equals("youqitong")) {
					for (int i = 0; i < 20; i++) {
						int j = (int) (Math.random() * 10);
						int k = (int) (Math.random() * 10);
						g.drawLine(x3 + j, y3 + k, x3 + j, y3 + k);
					}

				}
			}
		};
		panel03.addMouseListener(a1);
		panel03.addMouseMotionListener(a2);
		frame.setVisible(true);
		g = panel03.getGraphics();
	}

	public static void main(String[] args) {

		DrawingBoard draw = new DrawingBoard();
		draw.show();

	}

}

 

 至此我们总结一下:

传值方法:1.通过set和get方法传值;

                 2.通过构造函数传值;

内部类和外部类的优劣:

1.使用内部类可以很轻松的写,但是这个框架是很不清晰的,我们要知道以后做项目的时候并不是一个人完成的,而是整个团队合作写的。

2.使用外部类可能会很清晰,但是传值问题真的很是蛋痛,但是我们看可以很清晰,以后写代码是给别人看的,不是只有你自己的。

你可能会问,那么我们是否就不适用内部类了呢,这个问题我将在下一篇博客写到,到时候会结合一下贪吃蛇项目具体讲解一下的。

 

 




















 

你可能感兴趣的:(java)