非常值得学习的java 绘图板源代码下载地址:http://download.csdn.net/source/2371150
- package minidrawpad;
- import java.awt.*;
- import java.awt.event.*;
- import java.io.InputStreamReader;
- import java.io.Reader;
- import javax.swing.*;
- // 主界面类
- public class DrawPad extends JFrame implements ActionListener {
- /**
- * @param FileName DrawPad
- * @author Liu Jun Guang s
- * @param V 1.0.0
- */
- private static final long serialVersionUID = -2551980583852173918L;
- private JToolBar buttonpanel;//定义按钮面板
- private JMenuBar bar ;//定义菜单条
- private JMenu file,color,stroke,help;//定义菜单
- private JMenuItem newfile,openfile,savefile,exit;//file 菜单中的菜单项
- private JMenuItem helpin,helpmain,colorchoice,strokeitem;//help 菜单中的菜单项
- private Icon nf,sf,of;//文件菜单项的图标对象
- private JLabel startbar;//状态栏
- private DrawArea drawarea;//画布类的定义
- private Help helpobject; //定义一个帮助类对象
- private FileClass fileclass ;//文件对象
- String[] fontName;
- //定义工具栏图标的名称
- private String names[] = {"newfile","openfile","savefile","pen","line"
- ,"rect","frect","oval","foval","circle","fcircle"
- ,"roundrect","froundrect","rubber","color"
- ,"stroke","word"};//定义工具栏图标的名称
- private Icon icons[];//定义图象数组
- private String tiptext[] = {//这里是鼠标移到相应的按钮上给出相应的提示
- "新建一个图片","打开图片","保存图片","随笔画","画直线"
- ,"画空心的矩形","填充矩形","画空心的椭圆","填充椭圆"
- ,"画空心的圆","填充圆","画圆角矩形","填充圆角矩形"
- ,"橡皮擦","颜色","选择线条的粗细","文字的输入"};
- JButton button[];//定义工具条中的按钮组
- private JCheckBox bold,italic;//工具条字体的风格(复选框)
- private JComboBox stytles ;//工具条中的字体的样式(下拉列表)
- public DrawPad(String string) {
- // TODO 主界面的构造函数
- super(string);
- //菜单的初始化
- file = new JMenu("文件");
- color = new JMenu("颜色");
- stroke = new JMenu("画笔");
- help = new JMenu("帮助");
- bar = new JMenuBar();//菜单条的初始化
- //菜单条添加菜单
- bar.add(file);
- bar.add(color);
- bar.add(stroke);
- bar.add(help);
- //界面中添加菜单条
- setJMenuBar(bar);
- //菜单中添加快捷键
- file.setMnemonic('F');//既是ALT+“F”
- color.setMnemonic('C');//既是ALT+“C”
- stroke.setMnemonic('S');//既是ALT+“S”
- help.setMnemonic('H');//既是ALT+“H”
- //File 菜单项的初始化
- try {
- Reader reader = new InputStreamReader(getClass().getResourceAsStream("/icon"));//读取文件以类路径为基准
- } catch (Exception e) {
- // TODO 文件读取错误
- JOptionPane.showMessageDialog(this,"图片读取错误!","错误",JOptionPane.ERROR_MESSAGE);
- }
- nf = new ImageIcon(getClass().getResource("/icon/newfile.jpg"));//创建图表
- sf = new ImageIcon(getClass().getResource("/icon/savefile.jpg"));
- of = new ImageIcon(getClass().getResource("/icon/openfile.jpg"));
- newfile = new JMenuItem("新建",nf);
- openfile = new JMenuItem("打开",of );
- savefile = new JMenuItem("保存",sf);
- exit = new JMenuItem("退出");
- //File 菜单中添加菜单项
- file.add(newfile);
- file.add(openfile);
- file.add(savefile);
- file.add(exit);
- //File 菜单项添加快捷键
- newfile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK));
- openfile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));
- savefile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
- exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,InputEvent.CTRL_MASK));
- //File 菜单项的注册监听
- newfile.addActionListener(this);
- openfile.addActionListener(this);
- savefile.addActionListener(this);
- exit.addActionListener(this);
- //Color 菜单项的初始化
- colorchoice = new JMenuItem("调色板");
- colorchoice.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));
- colorchoice.addActionListener(this);
- color.add(colorchoice);
- //Help 菜单项的初始化
- helpmain = new JMenuItem("帮助主题");
- helpin = new JMenuItem("关于小小绘图板");
- //Help 菜单中添加菜单项
- help.add(helpmain);
- help.addSeparator();//添加分割线
- help.add(helpin);
- //Help 菜单项的注册监听
- helpin.addActionListener(this);
- helpmain.addActionListener(this);
- //Stroke 菜单项的初始化
- strokeitem = new JMenuItem("设置画笔");
- strokeitem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,InputEvent.CTRL_MASK));
- stroke.add(strokeitem);
- strokeitem.addActionListener(this);
- //工具栏的初始化
- buttonpanel = new JToolBar( JToolBar.HORIZONTAL);
- icons = new ImageIcon[names.length];
- button = new JButton[names.length];
- for(int i = 0 ;i<names.length;i++)
- {
- icons[i] = new ImageIcon(getClass().getResource("/icon/"+names[i]+".jpg"));//获得图片(以类路径为基准)
- button[i] = new JButton("",icons[i]);//创建工具条中的按钮
- button[i].setToolTipText(tiptext[i]);//这里是鼠标移到相应的按钮上给出相应的提示
- buttonpanel.add(button[i]);
- button[i].setBackground(Color.red);
- if(i<3)button[i].addActionListener(this);
- else if(i<=16) button[i].addActionListener(this);
- }
- CheckBoxHandler CHandler = new CheckBoxHandler();//字体样式处理类
- bold = new JCheckBox("粗体");
- bold.setFont(new Font(Font.DIALOG,Font.BOLD,30));//设置字体
- bold.addItemListener(CHandler);//bold注册监听
- italic = new JCheckBox("斜体");
- italic.addItemListener(CHandler);//italic注册监听
- italic.setFont(new Font(Font.DIALOG,Font.ITALIC,30));//设置字体
- GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();//计算机上字体可用的名称
- fontName = ge.getAvailableFontFamilyNames();
- stytles = new JComboBox(fontName);//下拉列表的初始化
- stytles.addItemListener(CHandler);//stytles注册监听
- stytles.setMaximumSize(new Dimension(400,50));//设置下拉列表的最大尺寸
- stytles.setMinimumSize(new Dimension(250,40));
- stytles.setFont(new Font(Font.DIALOG,Font.BOLD,20));//设置字体
- //工具栏中添加字体式样
- buttonpanel.add(bold);
- buttonpanel.add(italic);
- buttonpanel.add(stytles);
- //状态栏的初始化
- startbar = new JLabel("我的小小绘图板");
- //绘画区的初始化
- drawarea = new DrawArea(this);
- helpobject = new Help(this);
- fileclass = new FileClass(this,drawarea);
- Container con = getContentPane();//得到内容面板
- con.add(buttonpanel, BorderLayout.NORTH);
- con.add(drawarea,BorderLayout.CENTER);
- con.add(startbar,BorderLayout.SOUTH);
- Toolkit tool = getToolkit();//得到一个Tolkit类的对象(主要用于得到屏幕的大小)
- Dimension dim = tool.getScreenSize();//得到屏幕的大小 (返回Dimension对象)
- setBounds(40,40,dim.width-70,dim.height-100);
- setVisible(true);
- validate();
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- //设置状态栏显示的字符
- public void setStratBar(String s) {
- startbar.setText(s);
- }
- public void actionPerformed(ActionEvent e) {
- // TODO 事件的处理
- for(int i = 3; i<=13;i++)
- {
- if(e.getSource() ==button[i])
- {
- drawarea.setCurrentChoice(i);
- drawarea.createNewitem();
- drawarea.repaint();
- }
- }
- if(e.getSource() == newfile||e.getSource() == button[0])//新建
- {fileclass.newFile();}
- else if(e.getSource() == openfile||e.getSource() == button[1])//打开
- {fileclass.openFile();}
- else if(e.getSource() == savefile||e.getSource() == button[2])//保存
- {fileclass.saveFile();}
- else if(e.getSource() == exit)//退出程序
- {System.exit(0);}
- else if(e.getSource() == colorchoice||e.getSource() == button[14])//弹出颜色对话框
- {
- drawarea.chooseColor();//颜色的选择
- }
- else if(e.getSource() == button[15]||e.getSource()==strokeitem)//画笔粗细
- {
- drawarea.setStroke();//画笔粗细的调整
- }
- else if(e.getSource() == button[16])//添加文字
- { JOptionPane.showMessageDialog(null, "请单击画板以确定输入文字的位置!","提示"
- ,JOptionPane.INFORMATION_MESSAGE);
- drawarea.setCurrentChoice(14);
- drawarea.createNewitem();
- drawarea.repaint();
- }
- else if(e.getSource() == helpin)//帮助信息
- {helpobject.AboutBook();}
- else if(e.getSource() == helpmain)//帮助主题
- {helpobject.MainHeip();}
- }
- //字体样式处理类(粗体、斜体、字体名称)
- public class CheckBoxHandler implements ItemListener
- {
- public void itemStateChanged(ItemEvent ie) {
- // TODO 字体样式处理类(粗体、斜体、字体名称)
- if(ie.getSource() == bold)//字体粗体
- {
- if(ie.getStateChange() == ItemEvent.SELECTED)
- drawarea.setFont(1, Font.BOLD);
- else
- drawarea.setFont(1, Font.PLAIN);
- }
- else if(ie.getSource() == italic)//字体斜体
- {
- if(ie.getStateChange() == ItemEvent.SELECTED)
- drawarea.setFont(2, Font.ITALIC);
- else drawarea.setFont(2, Font.PLAIN);
- }
- else if(ie.getSource() == stytles)//字体的名称
- {
- drawarea.stytle = fontName[stytles.getSelectedIndex()];
- }
- }
- }
- }
java 绘图板源代码下载地址:http://download.csdn.net/source/2371150
- package minidrawpad;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import java.awt.event.MouseMotionAdapter;
- //绘图区类(各种图形的绘制和鼠标事件)
- public class DrawArea extends JPanel{
- DrawPad drawpad =null;
- Drawing[] itemList =new Drawing[5000];; //绘制图形类
- private int currentChoice = 3;//设置默认基本图形状态为随笔画
- int index = 0;//当前已经绘制的图形数目
- private Color color = Color.black;//当前画笔的颜色
- int R,G,B;//用来存放当前颜色的彩值
- int f1,f2;//用来存放当前字体的风格
- String stytle ;//存放当前字体
- float stroke = 1.0f;//设置画笔的粗细 ,默认的是 1.0
- DrawArea(DrawPad dp) {
- drawpad = dp;
- // 把鼠标设置成十字形
- setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
- // setCursor 设置鼠标的形状 ,getPredefinedCursor()返回一个具有指定类型的光标的对象
- setBackground(Color.white);// 设置绘制区的背景是白色
- addMouseListener(new MouseA());// 添加鼠标事件
- addMouseMotionListener(new MouseB());
- createNewitem();
- }
- public void paintComponent(Graphics g){
- super.paintComponent(g);
- Graphics2D g2d = (Graphics2D)g;//定义随笔画
- int j = 0;
- while(j<=index)
- {
- draw(g2d,itemList[j]);
- j++;
- }
- }
- void draw(Graphics2D g2d , Drawing i)
- {
- i.draw(g2d);//将画笔传到个各类的子类中,用来完成各自的绘图
- }
- //新建一个图形的基本单元对象的程序段
- void createNewitem(){
- if(currentChoice == 14)//字体的输入光标相应的设置为文本输入格式
- setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
- else setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
- switch(currentChoice){
- case 3: itemList[index] = new Pencil();break;
- case 4: itemList[index] = new Line();break;
- case 5: itemList[index] = new Rect();break;
- case 6: itemList[index] = new fillRect();break;
- case 7: itemList[index] = new Oval();break;
- case 8: itemList[index] = new fillOval();break;
- case 9: itemList[index] = new Circle();break;
- case 10: itemList[index] = new fillCircle();break;
- case 11: itemList[index] = new RoundRect();break;
- case 12: itemList[index] = new fillRoundRect();break;
- case 13: itemList[index] = new Rubber();break;
- case 14: itemList[index] = new Word();break;
- }
- itemList[index].type = currentChoice;
- itemList[index].R = R;
- itemList[index].G = G;
- itemList[index].B = B;
- itemList[index].stroke = stroke ;
- }
- public void setIndex(int x){//设置index的接口
- index = x;
- }
- public int getIndex(){//设置index的接口
- return index ;
- }
- public void setColor(Color color)//设置颜色的值
- {
- this.color = color;
- }
- public void setStroke(float f)//设置画笔粗细的接口
- {
- stroke = f;
- }
- public void chooseColor()//选择当前颜色
- {
- color = JColorChooser.showDialog(drawpad, "请选择颜色", color);
- try {
- R = color.getRed();
- G = color.getGreen();
- B = color.getBlue();
- } catch (Exception e) {
- R = 0;
- G = 0;
- B = 0;
- }
- itemList[index].R = R;
- itemList[index].G = G;
- itemList[index].B = B;
- }
- public void setStroke()//画笔粗细的调整
- {
- String input ;
- input = JOptionPane.showInputDialog("请输入画笔的粗细( >0 )");
- try {
- stroke = Float.parseFloat(input);
- } catch (Exception e) {
- stroke = 1.0f;
- }itemList[index].stroke = stroke;
- }
- public void setCurrentChoice(int i )//文字的输入
- {
- currentChoice = i;
- }
- public void setFont(int i,int font)//设置字体
- {
- if(i == 1)
- {
- f1 = font;
- }
- else
- f2 = font;
- }
- // TODO 鼠标事件MouseA类继承了MouseAdapter
- //用来完成鼠标的响应事件的操作(鼠标的按下、释放、单击、移动、拖动、何时进入一个组件、何时退出、何时滚动鼠标滚轮 )
- class MouseA extends MouseAdapter
- {
- @Override
- public void mouseEntered(MouseEvent me) {
- // TODO 鼠标进入
- drawpad.setStratBar("鼠标进入在:["+me.getX()+" ,"+me.getY()+"]");
- }
- @Override
- public void mouseExited(MouseEvent me) {
- // TODO 鼠标退出
- drawpad.setStratBar("鼠标退出在:["+me.getX()+" ,"+me.getY()+"]");
- }
- @Override
- public void mousePressed(MouseEvent me) {
- // TODO 鼠标按下
- drawpad.setStratBar("鼠标按下在:["+me.getX()+" ,"+me.getY()+"]");//设置状态栏提示
- itemList[index].x1 = itemList[index].x2 = me.getX();
- itemList[index].y1 = itemList[index].y2 = me.getY();
- //如果当前选择为随笔画或橡皮擦 ,则进行下面的操作
- if(currentChoice == 3||currentChoice ==13){
- itemList[index].x1 = itemList[index].x2 = me.getX();
- itemList[index].y1 = itemList[index].y2 = me.getY();
- index++;
- createNewitem();//创建新的图形的基本单元对象
- }
- //如果选择图形的文字输入,则进行下面的操作
- if(currentChoice == 14){
- itemList[index].x1 = me.getX();
- itemList[index].y1 = me.getY();
- String input ;
- input = JOptionPane.showInputDialog("请输入你要写入的文字!");
- itemList[index].s1 = input;
- itemList[index].x2 = f1;
- itemList[index].y2 = f2;
- itemList[index].s2 = stytle;
- index++;
- currentChoice = 14;
- createNewitem();//创建新的图形的基本单元对象
- repaint();
- }
- }
- @Override
- public void mouseReleased(MouseEvent me) {
- // TODO 鼠标松开
- drawpad.setStratBar("鼠标松开在:["+me.getX()+" ,"+me.getY()+"]");
- if(currentChoice == 3||currentChoice ==13){
- itemList[index].x1 = me.getX();
- itemList[index].y1 = me.getY();
- }
- itemList[index].x2 = me.getX();
- itemList[index].y2 = me.getY();
- repaint();
- index++;
- createNewitem();//创建新的图形的基本单元对象
- }
- }
- // 鼠标事件MouseB继承了MouseMotionAdapter
- // 用来处理鼠标的滚动与拖动
- class MouseB extends MouseMotionAdapter {
- public void mouseDragged(MouseEvent me)//鼠标的拖动
- {
- drawpad.setStratBar("鼠标拖动在:["+me.getX()+" ,"+me.getY()+"]");
- if(currentChoice == 3||currentChoice ==13){
- itemList[index-1].x1 = itemList[index].x2 = itemList[index].x1 =me.getX();
- itemList[index-1].y1 = itemList[index].y2 = itemList[index].y1 = me.getY();
- index++;
- createNewitem();//创建新的图形的基本单元对象
- }
- else
- {
- itemList[index].x2 = me.getX();
- itemList[index].y2 = me.getY();
- }
- repaint();
- }
- public void mouseMoved(MouseEvent me)//鼠标的移动
- {
- drawpad.setStratBar("鼠标移动在:["+me.getX()+" ,"+me.getY()+"]");
- }
- }
- }
- package minidrawpad;
- import javax.swing.UIManager;
- //主函数类
- public class MiniDrawPad {
- /**
- * @param FileName DrawPad
- * @author Liu Jun Guang
- * @param V 1.0.0
- */
- public static void main(String[] args) {
- // TODO 主函数
- /*try {//将界面设置为当前windows界面风格
- UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
- } catch (Exception e) {}*/
- DrawPad drawpad = new DrawPad("小小绘图板");
- }
- }
java 绘图板源代码下载地址:http://download.csdn.net/source/2371150
- package minidrawpad;
- import java.awt.Color;
- import java.io.*;
- import javax.swing.*;
- import javax.swing.filechooser.*;
- //文件类 (文件的打开、新建、保存)
- public class FileClass {
- private DrawPad drawpad;
- DrawArea drawarea = null;
- FileClass(DrawPad dp,DrawArea da) {
- drawpad = dp;
- drawarea = da;
- }
- public void newFile() {
- // TODO 新建图像
- drawarea.setIndex(0);
- drawarea.setCurrentChoice(3);//设置默认为随笔画
- drawarea.setColor(Color.black);//设置颜色
- drawarea.setStroke(1.0f);//设置画笔的粗细
- drawarea.createNewitem();
- drawarea.repaint();
- }
- public void openFile() {
- // TODO 打开图像
- //JFileChooser 为用户选择文件提供了一种简单的机制
- JFileChooser filechooser = new JFileChooser();
- filechooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
- /* FileNameExtensionFilter filter = new FileNameExtensionFilter(
- "JPG & GIF Images", "jpg", "gif");//其中只显示 .jpg 和 .gif 图像
- filechooser.setFileFilter(filter);*/
- int returnVal = filechooser.showOpenDialog(drawpad);
- if(returnVal == JFileChooser.CANCEL_OPTION) {//如果单击确定按钮就执行下面得程序
- return;
- }
- File fileName = filechooser.getSelectedFile();//getSelectedFile()返回选中的文件
- fileName.canRead();
- if(fileName == null || fileName.getName().equals(""))//文件名不存在时
- {
- JOptionPane.showMessageDialog(filechooser,"文件名","请输入文件名!",JOptionPane.ERROR_MESSAGE);
- }
- else {
- try {
- FileInputStream ifs = new FileInputStream(fileName);
- ObjectInputStream input = new ObjectInputStream(ifs);
- int countNumber = 0;
- Drawing inputRecord;
- countNumber = input.readInt();
- for(int i =0;i<=countNumber;i++)
- {
- drawarea.setIndex(i);
- inputRecord = (Drawing)input.readObject();
- drawarea.itemList[i] = inputRecord;
- }
- drawarea.createNewitem();
- input.close();
- drawarea.repaint();
- } catch (FileNotFoundException e) {
- JOptionPane.showMessageDialog(drawpad,"没有找到源文件!","没有找到源文件",JOptionPane.ERROR_MESSAGE);
- } catch (IOException e) {
- JOptionPane.showMessageDialog(drawpad,"读文件是发生错误!","读取错误",JOptionPane.ERROR_MESSAGE);
- } catch (ClassNotFoundException e) {
- JOptionPane.showMessageDialog(drawpad,"不能创建对象!","已到文件末尾",JOptionPane.ERROR_MESSAGE);
- }
- }
- }
- //保存图像文件程序段,用到文件对(FileOupputSream)象流
- public void saveFile() {
- // TODO 保存图像
- //JFileChooser 为用户选择文件提供了一种简单的机制
- JFileChooser filechooser = new JFileChooser();
- filechooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
- //setFileSelectionMode()设置 JFileChooser,以允许用户只选择文件、只选择目录,或者可选择文件和目录。
- int result = filechooser.showSaveDialog(drawpad);
- if(result == JFileChooser.CANCEL_OPTION){
- return ;
- }
- File fileName = filechooser.getSelectedFile();//getSelectedFile()返回选中的文件
- fileName.canWrite();//测试应用程序是否可以修改此抽象路径名表示的文件
- if(fileName == null || fileName.getName().equals(""))//文件名不存在时
- {
- JOptionPane.showMessageDialog(filechooser,"文件名","请输入文件名!",JOptionPane.ERROR_MESSAGE);
- }
- else {
- try {
- fileName.delete();//删除此抽象路径名表示的文件或目录
- FileOutputStream fos = new FileOutputStream(fileName+".xxh");//文件输出流以字节的方式输出
- //对象输出流
- ObjectOutputStream output = new ObjectOutputStream(fos);
- //Drawing record;
- output.writeInt(drawarea.getIndex());
- for(int i = 0;i<=drawarea.getIndex() ;i++)
- {
- Drawing p = drawarea.itemList[i];
- output.writeObject(p);
- output.flush();//刷新该流的缓冲。此操作将写入所有已缓冲的输出字节,并将它们刷新到底层流中。
- //将所有的图形信息强制的转换成父类线性化存储到文件中
- }
- output.close();
- fos.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
- package minidrawpad;
- import java.awt.BasicStroke;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Graphics2D;
- import java.io.Serializable;
- //图形绘制类 用于绘制各种图形
- //父类,基本图形单元,用到串行的接口,保存使用到
- //公共的属性放到超类中,子类可以避免重复定义
- /*类通过实现 java.io.Serializable 接口以启用其序列化功能。
- 未实现此接口的类将无法使其任何状态序列化或反序列化。
- 可序列化类的所有子类型本身都是可序列化的。序列化接口没有方法或字段,
- 仅用于标识可序列化的语义。*/
- public class Drawing implements Serializable {
- int x1,x2,y1,y2; //定义坐标属性
- int R,G,B; //定义色彩属性
- float stroke ; //定义线条粗细的属性
- int type; //定义字体属性
- String s1; //定义字体的风格
- String s2; //定义字体的风格
- void draw(Graphics2D g2d ){}//定义绘图函数
- }
- class Line extends Drawing//直线类
- {
- void draw(Graphics2D g2d) {
- g2d.setPaint(new Color(R, G, B));// 为 Graphics2D 上下文设置 Paint 属性。
- // 使用为 null 的 Paint 对象调用此方法对此 Graphics2D 的当前 Paint 属性没有任何影响。
- g2d.setStroke(new BasicStroke(stroke, BasicStroke.CAP_ROUND,
- BasicStroke.JOIN_BEVEL));
- // setStroke(Stroke s)为 Graphics2D 上下文设置 Stroke
- // BasicStroke 类定义针对图形图元轮廓呈现属性的一个基本集合
- // BasicStroke.CAP_ROUND使用半径等于画笔宽度一半的圆形装饰结束未封闭的子路径和虚线线段
- // BasicStroke.JOIN_BEVEL通过直线连接宽体轮廓的外角,将路径线段连接在一起。
- g2d.drawLine(x1, y1, x2, y2);// 画直线
- }
- }
- class Rect extends Drawing{//矩形类
- void draw(Graphics2D g2d ){
- g2d.setPaint(new Color(R,G,B));
- g2d.setStroke(new BasicStroke(stroke));
- g2d.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2));
- }
- }
- class fillRect extends Drawing{//实心矩形类
- void draw(Graphics2D g2d ){
- g2d.setPaint(new Color(R,G,B));
- g2d.setStroke(new BasicStroke(stroke));
- g2d.fillRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2));
- }
- }
- class Oval extends Drawing{//椭圆类
- void draw(Graphics2D g2d ){
- g2d.setPaint(new Color(R,G,B));
- g2d.setStroke(new BasicStroke(stroke));
- g2d.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2));
- }
- }
- class fillOval extends Drawing{//实心椭圆类
- void draw(Graphics2D g2d ){
- g2d.setPaint(new Color(R,G,B));
- g2d.setStroke(new BasicStroke(stroke));
- g2d.fillOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2));
- }
- }
- class Circle extends Drawing{//圆类
- void draw(Graphics2D g2d ){
- g2d.setPaint(new Color(R,G,B));
- g2d.setStroke(new BasicStroke(stroke));
- g2d.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.max(Math.abs(x1-x2),
- Math.abs(y1-y2)), Math.max(Math.abs(x1-x2), Math.abs(y1-y2)));
- }
- }
- class fillCircle extends Drawing{//实心圆类
- void draw(Graphics2D g2d ){
- g2d.setPaint(new Color(R,G,B));
- g2d.setStroke(new BasicStroke(stroke));
- g2d.fillOval(Math.min(x1, x2), Math.min(y1, y2), Math.max(Math.abs(x1-x2),
- Math.abs(y1-y2)), Math.max(Math.abs(x1-x2), Math.abs(y1-y2)));
- }
- }
- class RoundRect extends Drawing{//圆角矩形类
- void draw(Graphics2D g2d ){
- g2d.setPaint(new Color(R,G,B));
- g2d.setStroke(new BasicStroke(stroke));
- g2d.drawRoundRect(Math.min(x1, x2), Math.min(y1, y2),Math.abs(x1-x2), Math.abs(y1-y2),50,35);
- }
- }
- class fillRoundRect extends Drawing{//实心圆角矩形类
- void draw(Graphics2D g2d ){
- g2d.setPaint(new Color(R,G,B));
- g2d.setStroke(new BasicStroke(stroke));
- g2d.fillRoundRect(Math.min(x1, x2), Math.min(y1, y2),Math.abs(x1-x2), Math.abs(y1-y2),50,35);
- }
- }
- class Pencil extends Drawing{//随笔画类
- void draw(Graphics2D g2d ){
- g2d.setPaint(new Color(R,G,B));
- g2d.setStroke(new BasicStroke(stroke,BasicStroke.CAP_ROUND,BasicStroke.JOIN_BEVEL));
- g2d.drawLine(x1, y1,x2, y2);
- }
- }
- class Rubber extends Drawing{//橡皮擦类
- void draw(Graphics2D g2d ){
- g2d.setPaint(new Color(255,255,255));//白色
- g2d.setStroke(new BasicStroke(stroke+4,BasicStroke.CAP_ROUND,BasicStroke.JOIN_BEVEL));
- g2d.drawLine(x1, y1,x2, y2);
- }
- }
- class Word extends Drawing{//输入文字类
- void draw(Graphics2D g2d ){
- g2d.setPaint(new Color(R,G,B));
- g2d.setFont(new Font(s2,x2+y2,((int)stroke)*18));//设置字体
- if(s1 != null)
- g2d.drawString( s1, x1,y1);
- }
- }
java 绘图板源代码下载地址:http://download.csdn.net/source/2371150
- package minidrawpad;
- import javax.swing.JFrame;
- import javax.swing.JOptionPane;
- //帮助菜单功能的事项类
- public class Help extends JFrame {
- private DrawPad drawpad = null;
- Help(DrawPad dp)
- {
- drawpad = dp;
- }
- public void MainHeip()
- {
- JOptionPane.showMessageDialog(this,"小小绘图板帮助文档!","小小绘图板",JOptionPane.WARNING_MESSAGE);
- }
- public void AboutBook()
- {
- JOptionPane.showMessageDialog(drawpad,"小小绘图板"+"/n"+" 版本: 1.1.2"+"/n"
- +" 作者: 刘 军 光"+"/n"
- +" 时间: 2009/12/13","小小绘图板",JOptionPane.WARNING_MESSAGE);
- }
- }
用到的各种图片 请将图片放在icon文件夹下
java 绘图板源代码下载地址:http://download.csdn.net/source/2371150
circle.jpg color.jpgfcircle.jpgfoval.jpgfrect.jpgfroundrect.jpgline.jpgnewfile.jpgopenfile.jpgoval.jpgpen.jpgrect.jpgroundrect.jpgrubber.jpgsavefile.jpgstroke.jpgword.jpg