非常值得学习的java 绘图板源代码
Java 小小绘图板,各种图形的绘制和文字的写入,也可以调整文字画笔的粗细 。还可以保存图像,非常值得学习的java 绘图板源代码下载地址:
免费下载地址在 http://linux.bkjia.com/
用户名与密码都是www.bkjia.com
具体下载目录在 /pub/2011/11/05/java 绘图板源代码/
用到的各种图片 请将图片放在icon文件夹下
circle.jpg color.jpgfcircle.jpgfoval.jpgfrect.jpgfroundrect.jpgline.jpgnewfile.jpgopenfile.jpgoval.jpgpen.jpgrect.jpgroundrect.jpgrubber.jpgsavefile.jpgstroke.jpgword.jpg
- 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 {
private static final long serialVersionUID = -2551980583852173918L;
private JToolBar buttonpanel;
private JMenuBar bar ;
private JMenu file,color,stroke,help;
private JMenuItem newfile,openfile,savefile,exit;
private JMenuItem helpin,helpmain,colorchoice,strokeitem;
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) {
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');
color.setMnemonic('C');
stroke.setMnemonic('S');
help.setMnemonic('H');
try {
Reader reader = new InputStreamReader(getClass().getResourceAsStream("/icon"));
} catch (Exception e) {
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.add(newfile);
file.add(openfile);
file.add(savefile);
file.add(exit);
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));
newfile.addActionListener(this);
openfile.addActionListener(this);
savefile.addActionListener(this);
exit.addActionListener(this);
colorchoice = new JMenuItem("调色板");
colorchoice.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));
colorchoice.addActionListener(this);
color.add(colorchoice);
helpmain = new JMenuItem("帮助主题");
helpin = new JMenuItem("关于小小绘图板");
help.add(helpmain);
help.addSeparator();
help.add(helpin);
helpin.addActionListener(this);
helpmain.addActionListener(this);
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);
italic = new JCheckBox("斜体");
italic.addItemListener(CHandler);
italic.setFont(new Font(Font.DIALOG,Font.ITALIC,30));
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
fontName = ge.getAvailableFontFamilyNames();
stytles = new JComboBox(fontName);
stytles.addItemListener(CHandler);
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();
Dimension dim = tool.getScreenSize();
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) {
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) {
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()];
}
}
}
}
评论暂时关闭