Java 小小绘图板,各种图形的绘制和文字的写入,也可以调整文字画笔的粗细 。还可以保存图像,非常值得学习的java 绘图板源代码下载地址:
具体下载目录在 /pub/2011/11/05/java 绘图板源代码/
用到的各种图片 请将图片放在icon文件夹下
circle.jpg
color.jpg
fcircle.jpg
foval.jpg
frect.jpg
froundrect.jpg
line.jpg
newfile.jpg
openfile.jpg
oval.jpg
pen.jpg
rect.jpg
roundrect.jpg
rubber.jpg
savefile.jpg
stroke.jpg
word.jpg
packageminidrawpad;
importjava.awt.*;
importjava.awt.event.*;
importjava.io.InputStreamReader;
importjava.io.Reader;
importjavax.swing.*;
// 主界面类
publicclassDrawPadextendsJFrameimplementsActionListener {
/**
* @param FileName DrawPad
* @author Liu Jun Guang s
* @param V 1.0.0
*/
privatestaticfinallongserialVersionUID = -2551980583852173918L;
privateJToolBar buttonpanel;//定义按钮面板
privateJMenuBar bar ;//定义菜单条
privateJMenu file,color,stroke,help;//定义菜单
privateJMenuItem newfile,openfile,savefile,exit;//file 菜单中的菜单项
privateJMenuItem helpin,helpmain,colorchoice,strokeitem;//help 菜单中的菜单项
privateIcon nf,sf,of;//文件菜单项的图标对象
privateJLabel startbar;//状态栏
privateDrawArea drawarea;//画布类的定义
privateHelp helpobject;//定义一个帮助类对象
privateFileClass fileclass ;//文件对象
String[] fontName;
//定义工具栏图标的名称
privateString names[] = {"newfile","openfile","savefile","pen","line"
,"rect","frect","oval","foval","circle","fcircle"
,"roundrect","froundrect","rubber","color"
,"stroke","word"};//定义工具栏图标的名称
privateIcon icons[];//定义图象数组
privateString tiptext[] = {//这里是鼠标移到相应的按钮上给出相应的提示
"新建一个图片","打开图片","保存图片","随笔画","画直线"
,"画空心的矩形","填充矩形","画空心的椭圆","填充椭圆"
,"画空心的圆","填充圆","画圆角矩形","填充圆角矩形"
,"橡皮擦","颜色","选择线条的粗细","文字的输入"};
JButton button[];//定义工具条中的按钮组
privateJCheckBox bold,italic;//工具条字体的风格(复选框)
privateJComboBox stytles ;//工具条中的字体的样式(下拉列表)
publicDrawPad(String string) {
// TODO 主界面的构造函数
super(string);
//菜单的初始化
file =newJMenu("文件");
color =newJMenu("颜色");
stroke =newJMenu("画笔");
help =newJMenu("帮助");
bar =newJMenuBar();//菜单条的初始化
//菜单条添加菜单
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 =newInputStreamReader(getClass().getResourceAsStream("/icon"));//读取文件以类路径为基准
}catch(Exception e) {
// TODO 文件读取错误
JOptionPane.showMessageDialog(this,"图片读取错误!","错误",JOptionPane.ERROR_MESSAGE);
}
nf =newImageIcon(getClass().getResource("/icon/newfile.jpg"));//创建图表
sf =newImageIcon(getClass().getResource("/icon/savefile.jpg"));
of =newImageIcon(getClass().getResource("/icon/openfile.jpg"));
newfile =newJMenuItem("新建",nf);
openfile =newJMenuItem("打开",of );
savefile =newJMenuItem("保存",sf);
exit =newJMenuItem("退出");
//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 =newJMenuItem("调色板");
colorchoice.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));
colorchoice.addActionListener(this);
color.add(colorchoice);
//Help 菜单项的初始化
helpmain =newJMenuItem("帮助主题");
helpin =newJMenuItem("关于小小绘图板");
//Help 菜单中添加菜单项
help.add(helpmain);
help.addSeparator();//添加分割线
help.add(helpin);
//Help 菜单项的注册监听
helpin.addActionListener(this);
helpmain.addActionListener(this);
//Stroke 菜单项的初始化
strokeitem =newJMenuItem("设置画笔");
strokeitem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,InputEvent.CTRL_MASK));
stroke.add(strokeitem);
strokeitem.addActionListener(this);
//工具栏的初始化
buttonpanel =newJToolBar( JToolBar.HORIZONTAL);
icons =newImageIcon[names.length];
button =newJButton[names.length];
for(inti =0;i
{
icons[i] =newImageIcon(getClass().getResource("/icon/"+names[i]+".jpg"));//获得图片(以类路径为基准)
button[i] =newJButton("",icons[i]);//创建工具条中的按钮
button[i].setToolTipText(tiptext[i]);//这里是鼠标移到相应的按钮上给出相应的提示
buttonpanel.add(button[i]);
button[i].setBackground(Color.red);
if(i<3)button[i].addActionListener(this);
elseif(i<=16) button[i].addActionListener(this);
}
CheckBoxHandler CHandler =newCheckBoxHandler();//字体样式处理类
bold =newJCheckBox("粗体");
bold.setFont(newFont(Font.DIALOG,Font.BOLD,30));//设置字体
bold.addItemListener(CHandler);//bold注册监听
italic =newJCheckBox("斜体");
italic.addItemListener(CHandler);//italic注册监听
italic.setFont(newFont(Font.DIALOG,Font.ITALIC,30));//设置字体
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();//计算机上字体可用的名称
fontName = ge.getAvailableFontFamilyNames();
stytles =newJComboBox(fontName);//下拉列表的初始化
stytles.addItemListener(CHandler);//stytles注册监听
stytles.setMaximumSize(newDimension(400,50));//设置下拉列表的最大尺寸
stytles.setMinimumSize(newDimension(250,40));
stytles.setFont(newFont(Font.DIALOG,Font.BOLD,20));//设置字体
//工具栏中添加字体式样
buttonpanel.add(bold);
buttonpanel.add(italic);
buttonpanel.add(stytles);
//状态栏的初始化
startbar =newJLabel("我的小小绘图板");
//绘画区的初始化
drawarea =newDrawArea(this);
helpobject =newHelp(this);
fileclass =newFileClass(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);
}
//设置状态栏显示的字符
publicvoidsetStratBar(String s) {
startbar.setText(s);
}
publicvoidactionPerformed(ActionEvent e) {
// TODO 事件的处理
for(inti =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();}
elseif(e.getSource() == openfile||e.getSource() == button[1])//打开
{fileclass.openFile();}
elseif(e.getSource() == savefile||e.getSource() == button[2])//保存
{fileclass.saveFile();}
elseif(e.getSource() == exit)//退出程序
{System.exit(0);}
elseif(e.getSource() == colorchoice||e.getSource() == button[14])//弹出颜色对话框
{
drawarea.chooseColor();//颜色的选择
}
elseif(e.getSource() == button[15]||e.getSource()==strokeitem)//画笔粗细
{
drawarea.setStroke();//画笔粗细的调整
}
elseif(e.getSource() == button[16])//添加文字
{ JOptionPane.showMessageDialog(null,"请单击画板以确定输入文字的位置!","提示"
,JOptionPane.INFORMATION_MESSAGE);
drawarea.setCurrentChoice(14);
drawarea.createNewitem();
drawarea.repaint();
}
elseif(e.getSource() == helpin)//帮助信息
{helpobject.AboutBook();}
elseif(e.getSource() == helpmain)//帮助主题
{helpobject.MainHeip();}
}
//字体样式处理类(粗体、斜体、字体名称)
publicclassCheckBoxHandlerimplementsItemListener
{
publicvoiditemStateChanged(ItemEvent ie) {
// TODO 字体样式处理类(粗体、斜体、字体名称)
if(ie.getSource() == bold)//字体粗体
{
if(ie.getStateChange() == ItemEvent.SELECTED)
drawarea.setFont(1, Font.BOLD);
else
drawarea.setFont(1, Font.PLAIN);
}
elseif(ie.getSource() == italic)//字体斜体
{
if(ie.getStateChange() == ItemEvent.SELECTED)
drawarea.setFont(2, Font.ITALIC);
elsedrawarea.setFont(2, Font.PLAIN);
}
elseif(ie.getSource() == stytles)//字体的名称
{
drawarea.stytle = fontName[stytles.getSelectedIndex()];
}
}
}
}