开心绘画是一款用Java语言编写的模拟绘图工具的程序。该程序拥有设置画笔粗细、选择背景颜色、选择画笔颜色、绘制图形、清除图像、使用橡皮和展开简笔画等功能。
本程序开发细节设计如下:
1. 鼠标坐标的捕捉。
2. 鼠标拖动事件。
3. 鼠标点击事件。
4. 调用大量绘图工具类的方法。
5. 调用外部组件并实现其兼容接口。
6. 改变鼠标形状。
7. 按钮与菜单的点击事件。
开心绘画程序制作准备
1.系统开发环境要求
1.1 操作系统:Windows7以上
1.2 Java虚拟机:JDK2.1.7以上
1.3 开发环境:Eclipse
1.4 开发语言:Java SE
2. 创建新项目
2.1 找出包资源管理器界面
依次选择Eclipse菜单栏中的“Window(窗口)”→“Show View(视图)”→“Other(其他)”选项,打开显示视图对话框。
在显示视图对话框中展开Java这一项,选择Package Explorer(包资源管理器)选项,单击“OK”按钮。完成这些操作之后,包资源管理器就会显示到Eclipse界面中了。
2.2 创建项目
在Java EE版的Eclipse工具栏中,单击“New”按钮右侧的三角号,选择最后一项“Other”,会打开一个New窗口,这个窗口显示了Eclipse可以创建的所有项目种类。找到Java文件夹并展开,选中Java Project(Java项目)选项。
然后单击“Next >”按钮,在所示窗口中,输入要创建的项目名,单击“Finish”按钮完成项目的创建。
3 导入资源
3.1 创建文件夹
创建文件夹,用来放置以后会用到的jar文件。在创建好的项目上单击鼠标右键,在弹出的菜单中依次选择“New(新建)”→“Folder(文件夹)”选项。
完成以上操作,会弹出New Folder窗口,在此窗口中将文件夹命名为lib,其他选项都为默认,然后单击“Finish”按钮。
3.2 导入外部jar包
将编写好的jar文件复制到刚创建好的lib文件夹中。
在项目上单击鼠标右键,选择“Build Path(构建路径)”→“Configure Build Path...(配置构建路径)”选项。
打开Java Build Path窗口,选择Libraries(库)标签页,单击右侧的“Add JARs(添加Jar文件)”按钮。
打开JAR Selection窗口,选中DrawUtil.mr.jar”文件,单击“OK”按钮。
选择完jar文件后,jar文件会出现在Java Build Path窗口的Libraries标签页中,单击“OK”按钮完成导入操作。
Jar包导入成功后,成功导入的jar文件左下角出现特殊图标,且会自动生成引用jar包库。
Jar文件中共有五个类:
BackgroundPanel文件:可以添加背景图片的面板类
import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Image;import javax.swing.JPanel;public class BackgroundPanel extends JPanel { private static final long serialVersionUID = 1L; private Image image; public BackgroundPanel(Image image) { this.image = image; } public BackgroundPanel() {} protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; if (this.image != null) { int width = getWidth(); int height = getHeight(); g2.drawImage(this.image, 0, 0, width, height, this); } } public void setImage(Image image) { this.image = image; repaint(); }}
DrawImageUtil文件:绘图工具类,提供保存图片的方法
import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.Date;import javax.imageio.ImageIO;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JOptionPane;import javax.swing.filechooser.FileNameExtensionFilter;import javax.swing.filechooser.FileSystemView;public class DrawImageUtil { public static void saveImage(JFrame frame, BufferedImage saveImage) { JFileChooser jfc = new JFileChooser(); jfc.setDialogTitle("保存图片"); FileNameExtensionFilter filter=new FileNameExtensionFilter("JPG", new String[] { "jpg" }); jfc.setFileFilter(filter); SimpleDateFormat sdf = new SimpleDateFormat("yyyymmddHHmmss"); String fileName = sdf.format(new Date()); FileSystemView view = FileSystemView.getFileSystemView(); File filePath = view.getHomeDirectory(); File saveFile =new File(filePath, String.valueOf(fileName) + ".jpg"); jfc.setSelectedFile(saveFile); int flag = jfc.showSaveDialog(null); if (flag == 0) try {ImageIO.write(saveImage, "jpg", saveFile); } catch (IOException e) { e.printStackTrace(); JOptionPane.showMessageDialog(frame, "文件无法保存!", "错误", 64);}}}
FrameGetShape文件:兼容图形选择组件接口,实现该接口之后,可在getShape(Shapes shape)方法中获得组件返回的图形对象
public interface FrameGetShape { void getShape(Shapes paramShapes);}
Shapes文件:图形选择组件返回的图形类
Public class Shapes { public static final int YUAN = 25377; public static final int FANG = 25637; private int type; private int width; private int height; public Shapes() {} public Shapes(int type, int width, int height) { this.type = type; this.width = width; this.height = height; } public int getType() { return this.type; } public void setType(int type) { this.type = type; } public int getWidth() { return this.width; } public void setWidth(int width) { this.width = width; } public int getHeigth() { return this.height; }public void setHeigth(int height) { this.height = height; } public String toString() { return "Shapes [type=" + this.type + ", width=" + this.width + ", height=" + this.height + "]"; }}
ShapeWindow文件:图形选择组件类
import java.awt.BorderLayout;import java.awt.Color;import java.awt.Container;import java.awt.FlowLayout;import java.awt.Graphics2D;import java.awt.Shape;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.geom.Ellipse2D;import java.awt.geom.Rectangle2D;import java.awt.image.BufferedImage;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JSpinner;import javax.swing.JWindow;public class ShapeWindow extends JWindow { private Shapes shapes; private FrameGetShape frame; class ShapeButton extends JPanel { public ShapeButton(final Shape shape) {setSize(20, 20); setLayout(new BorderLayout()); BufferedImage img = new BufferedImage(15, 15,4);Graphics2D g = img.createGraphics(); g.setColor(Color.WHITE); g.fillRect(0, 0, img.getWidth(), img.getHeight()); g.setColor(Color.BLACK); g.draw(shape); JButton btnNewButton = new JButton(); btnNewButton.setIcon(new ImageIcon(img)); JPanel p = new JPanel(); p.add(btnNewButton); add(p, "Center"); JPanel south = new JPanel(); south.setLayout(new FlowLayout()); final JSpinner spinnerLeft = new JSpinner(); spinnerLeft.setValue(Integer.valueOf(50)); south.add(new JLabel("宽")); south.add(spinnerLeft); final JSpinner spinnerRigth = new JSpinner(); spinnerRigth.setValue(Integer.valueOf(50)); south.add(new JLabel("高")); south.add(spinnerRigth); add(south, "South"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(shapeinstanceofEllipse2D) (ShapeWindow.ShapeButton.access$0(ShapeWindow.ShapeButton.this)).shapes=new Shapes(25377,((Integer)spinnerLeft.getValue()).intValue(), ((Integer)spinnerRigth.getValue()).intValue()); if(shapeinstanceofRectangle2D)(ShapeWindow.ShapeButton.access$0(ShapeWindow.ShapeButton.this)).shapes = newShapes(25637, ((Integer)spinnerLeft.getValue()).intValue(), ((Integer)spinnerRigth.getValue()).intValue()); (ShapeWindow.ShapeButton.access$0(ShapeWindow.ShapeButton.this)).frame.getShape((ShapeWindow.ShapeButton.access$0(ShapeWindow.ShapeButton.this)).shapes); ShapeWindow.ShapeButton.access$0(ShapeWindow.ShapeButton.this).dispose(); } }); } } public ShapeWindow(FrameGetShape frame) { this.frame = frame; init(); } public ShapeWindow(int x, int y, FrameGetShape frame) { this.frame = frame; setLocation(x, y); init(); } private void init() { setSize(200, 100); Container c = getContentPane(); c.setLayout(new BorderLayout());JPanel centerPanel = new JPanel(); JPanel southPanel = new JPanel(); Ellipse2D e = new Ellipse2D.Double(0.0D, 0.0D, 14.0D, 14.0D); ShapeButton yuan = new ShapeButton(e); centerPanel.add(yuan); Rectangle2D r = new Rectangle2D.Double(0.0D, 0.0D, 14.0D, 14.0D); ShapeButton fang = new ShapeButton(r); centerPanel.add(fang); c.add(centerPanel, "Center"); FlowLayout flow = new FlowLayout(2); southPanel.setLayout(flow); JButton cancel = new JButton("取消"); southPanel.add(cancel); cancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ShapeWindow.this.dispose(); } }); c.add(southPanel, "South"); pack(); }}
本文作者:怀苏学长,热爱编程的计算机专业大学生,没有好看的皮囊,只有对编程热爱的心。