Java实现简单的绘图软件
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
项目效果图:
项目源代码:
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
/* author: 蒋冰*/
public class ImageDraw {
private int typeFlag = 1;
private int endFlag = 1;
private int strokeTemp = 1;
private Color color = Color.black;
private BasicStroke stroke = new BasicStroke();
private float pre_x = 0;
private float pre_y = 0;
private float start_x = 0;
private float start_y = 0;
private float start_X = 0;
private float start_Y = 0;
private float end_x = 0;
private float end_y = 0;
private float width = 0;
private float height = 0;
private final int AREA_WIDTH = 700;
private final int AREA_HEIGHT = 400;
private Line2D.Float line;
private Rectangle2D.Float rect;
private Ellipse2D.Float ellipse;
private File imageFile;
BufferedImage image = new BufferedImage(AREA_WIDTH , AREA_HEIGHT , BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D) image.getGraphics();
private JFrame frame = new JFrame("简单手绘程序");
private DrawPanel drawArea = new DrawPanel();
JButton buttonShowColor = new JButton("");
public void init(){
g2.fillRect(0 , 0 ,AREA_WIDTH , AREA_HEIGHT);
g2.setColor(Color.black);
drawArea.setPreferredSize(new Dimension(AREA_WIDTH , AREA_HEIGHT));
drawArea.addMouseMotionListener(new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent e)
{
do_mouseDragged(e);
}
});
drawArea.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e1){
do_mousePressed(e1);
}
public void mouseReleased(MouseEvent e)
{
g2.setColor(color);
g2.setStroke(stroke);
endFlag = typeFlag;
drawArea.repaint();
}
public void mouseEntered(MouseEvent e){
frame.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
}
public void mouseExited(MouseEvent e){
frame.setCursor(null);
}
});
frame.add(drawArea);
JMenuBar mb = new JMenuBar();
JMenu menuFile = new JMenu("文件");
JMenuItem menuItemSave = new JMenuItem("保存"); menuItemSave.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
do_save_actionPerformed(arg0);
}
});
JMenuItem menuItemOpen = new JMenuItem("打开"); menuItemOpen.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
try {
do_open_actionPerformed(arg0);
} catch (IOException e) {
// TODO 自动生成的catch 块
e.printStackTrace();
}
}
});