ImageVerifierCode 换一换
格式:DOCX , 页数:14 ,大小:158.29KB ,
资源ID:12784994      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/12784994.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(Java实现简单的绘图软件.docx)为本站会员(b****1)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

Java实现简单的绘图软件.docx

1、Java实现简单的绘图软件Java实现简单的绘图软件项目效果图:项目源代码: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; private float end_y = 0; private float width

2、 = 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 , BufferedImag

3、e.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.set

4、PreferredSize(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 mouseRel

5、eased(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 JMe

6、nuBar(); 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 Acti

7、onListener() public void actionPerformed(ActionEvent arg0) try do_open_actionPerformed(arg0); catch (IOException e) / TODO 自动生成的 catch 块 e.printStackTrace(); ); JMenuItem menuItemExit = new JMenuItem(退出); menuItemExit.addActionListener(new ActionListener() public void actionPerformed(ActionEvent arg

8、0) System.exit(0); ); menuFile.add(menuItemSave); menuFile.add(menuItemOpen); menuFile.add(menuItemExit); mb.add(menuFile); JPanel buttonPanel = new JPanel(); FlowLayout layout = new FlowLayout(FlowLayout.CENTER, 20, 20); buttonPanel.setLayout(layout); buttonPanel.setBackground(Color.gray); JButton

9、buttonDraw = new JButton(绘图); buttonDraw.addActionListener(new ActionListener() public void actionPerformed(ActionEvent arg0) typeFlag = 1; ); buttonPanel.add(buttonDraw); JButton buttonLine = new JButton(直线); buttonLine.addActionListener(new ActionListener() public void actionPerformed(ActionEvent

10、arg0) typeFlag = 2; ); buttonPanel.add(buttonLine); JButton buttonRect = new JButton(矩形); buttonRect.addActionListener(new ActionListener() public void actionPerformed(ActionEvent arg0) typeFlag = 3; ); buttonPanel.add(buttonRect); JButton buttonEllipse = new JButton(椭圆); buttonEllipse.addActionList

11、ener(new ActionListener() public void actionPerformed(ActionEvent arg0) typeFlag = 4; ); buttonPanel.add(buttonEllipse); JButton buttonDrop = new JButton(清除); buttonDrop.addActionListener(new ActionListener() public void actionPerformed(ActionEvent arg0) typeFlag = 5; g2.setColor(Color.white); g2.fi

12、llRect(0 , 0 ,AREA_WIDTH , AREA_HEIGHT); drawArea.repaint(); ); buttonPanel.add(buttonDrop); JButton buttonColorChoose = new JButton(选择颜色); buttonColorChoose.addActionListener(new ActionListener() public void actionPerformed(ActionEvent arg0) do_buttonColorChoose_actionPerformed(arg0); ); buttonPane

13、l.add(buttonColorChoose); buttonShowColor = new JButton(); buttonPanel.add(buttonShowColor); JLabel label = new JLabel(边框:); buttonPanel.add(label); JComboBox combox = new JComboBox(); for(int i=1; i=36; i+) combox.addItem(i); combox.addItemListener(new ItemListener() public void itemStateChanged(It

14、emEvent e) strokeTemp = Integer.parseInt(e.getItem().toString() ; stroke = new BasicStroke(strokeTemp); ); buttonPanel.add(combox); frame.add(buttonPanel,BorderLayout.SOUTH); frame.add(mb,BorderLayout.NORTH); frame.setBackground(Color.gray); frame.pack(); frame.setVisible(true); public static void m

15、ain(String args) new ImageDraw().init(); class DrawPanel extends JPanel public void paint(Graphics g) Graphics2D g2 = (Graphics2D) g; g2.clearRect(0, 0, AREA_WIDTH, AREA_HEIGHT); g2.setColor(color); g2.setStroke(stroke); g2.drawImage(image , 0 , 0 , null); if(typeFlag = 2) line = new Line2D.Float(st

16、art_x, start_y, end_x, end_y); g2.draw(line); if(typeFlag = 3) rect = new Rectangle2D.Float(start_X, start_Y, width, height); g2.draw(rect); if(typeFlag = 4) ellipse = new Ellipse2D.Float(start_X, start_Y, width, height); g2.draw(ellipse); if(typeFlag = 5) g2.setColor(Color.white); g2.fillRect(0 , 0

17、 ,AREA_WIDTH , AREA_HEIGHT); private void do_save_actionPerformed(ActionEvent arg0) / TODO 自动生成的方法存根 JFileChooser chooser = new JFileChooser(); chooser.setSelectedFile(new File(imageFile.getName(); int option = chooser.showSaveDialog(null); if(option = JFileChooser.APPROVE_OPTION) File file = choose

18、r.getSelectedFile(); try ImageIO.write(image, jpg, file); catch (IOException e) / TODO 自动生成的 catch 块 e.printStackTrace(); private void do_open_actionPerformed(ActionEvent arg0) throws IOException / TODO 自动生成的方法存根 JFileChooser chooser = new JFileChooser(); chooser.setFileFilter(new FileNameExtensionF

19、ilter(图片文件,jpg,jpeg,jif); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); int option = chooser.showOpenDialog(null); if(option = JFileChooser.APPROVE_OPTION) imageFile = chooser.getSelectedFile(); Image sourceImage = ImageIO.read(imageFile); g2.drawImage(sourceImage, 0, 0, sourceImage.getWidt

20、h(null), sourceImage.getHeight(null), null); drawArea.repaint(); private void do_buttonColorChoose_actionPerformed(ActionEvent arg0) / TODO 自动生成的方法存根 JColorChooser chooser = new JColorChooser(); color = chooser.showDialog(null, 请选取颜色, Color.black); buttonShowColor.setBackground(color); private void

21、do_mouseDragged(MouseEvent e) end_x = e.getX(); end_y = e.getY(); width = Math.abs( end_x - start_x ); height = Math.abs( end_y - start_y ); if(end_x = start_x & end_y start_x & end_y start_y) start_Y = start_y - height; start_X = start_x; if(end_x = start_y) start_Y = start_y; start_X = start_x - w

22、idth; if(end_x = start_x & end_y =start_y) start_Y = start_y; start_X = start_x; if(typeFlag = 1) if (pre_x 0 & pre_y 0) line = new Line2D.Float(pre_x, pre_y, end_x, end_y); g2.setColor(color); g2.setStroke(stroke); g2.draw(line); pre_x = e.getX(); pre_y = e.getY(); drawArea.repaint(); else drawArea.repaint(); private void do_mousePressed(MouseEvent e1) / TODO 自动生成的方法存根 width = 0; height = 0; start_x = end_x = pre_x = e1.getX(); start_y = end_y = pre_y = e1.getY(); if(endFlag = 2) g2.draw(line); if(endFlag = 3) g2.draw(rect); if(endFlag = 4) g2.draw(ellipse); drawArea.repaint();

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1