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