Java程序设计绘图软件的开发

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。



clear=new JButton("清除");//清除按钮 clear.addActionListener(this); add(clear); save=new JButton("保存");// 保存按钮 save.addActionListener(this); add(save); load=new JButton("打开");//打开按钮 load.addActionListener(this); add(load); } public void mouseClicked(MouseEvent e) {//定义一个鼠标点击事件 JLabel l=(JLabel)e.getSource(); if(e.getClickCount()==2){// 鼠标双击 Color sc=JColorChooser.showDialog(null, "Color chooser", l.getBackground());//调用颜 色选框 l.setBackground(sc); mousePressed(e); } } public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mousePressed(MouseEvent e) { Color c=((JLabel)e.getSource()).getBackground(); if(fc.isSelected()) fc.setBackground(c); else if(bc.isSelected())


f.getContentPane().add(M.m,"South"); f.setVisible(true);//使可视化窗口组件显示 } } class CVS extends Component implements ComponentListener,MouseListener,MouseMotionListener/*用于接收组件上的鼠标移动事 件的侦听器接口*/ { public void componentHidden(ComponentEvent e) {}//组件变得不可见时调用 public void componentMoved(ComponentEvent e) {}//组件位置更改时调用 public void componentResized(ComponentEvent e) {resized();}//组件大小更改时调用 public void componentShown(ComponentEvent e) {}//组件变得可见时调用 private void resized() { int w=this.getWidth(); int h=this.getHeight(); tbuff=new BufferedImage(w,h,3);//描述具有可访问图像数据缓冲区的 Image makeBuff(w,h); } private void makeBuff(int w,int h) { Graphics g = tbuff.getGraphics(); g.drawImage(buff,0,0,null);//绘制指定图像中当前可用的图像 g.dispose(); buff=new BufferedImage(w,h,3); g=buff.getGraphics(); g.drawImage(tbuff,0,0,null); g.dispose(); }
四、源代码分析:


import java.awt.*; //包含用于创建用户界面和绘制图形图像的所有类 import java.awt.event.*; import java.awt.geom.*;//提供用于在与二维几何形状相关的对象上定义 和执行操作的 Java 2D 类 import java.awt.image.BufferedImage;//抽象类 Image 是表示图形图像 的所有类的超类 import java.io.*;//文件输入输出流 import java.util.ArrayList; import javax.swing.*; import javax.swing.filechooser.FileNameExtensionFilter; public class JDraw { public static void main(String[] args) { JFrame f=new JFrame(); //实例化一个窗体 f.setDefaultCloseOperation(3);//添加关闭按钮 f.setSize(880,600); f.setLocationRelativeTo(null);//此窗口位于屏幕的中央 f.getContentPane().add(M.c);//初始化一个容器

二、设计目的:
整个程序完成后,能实现简单的绘图功能。
能用鼠标拖动画出椭圆,正圆,直线,正方 形,长方形,弧线,还能设置颜色,具有擦 出、清除、保存等功能。
三、技术要点:
CVS已继承于Component,因为java中没有多重继承机制,
所以它不能再继承MouseAdapter类,而只能实现监听程序 接口,所以程序中需要有实现这两个接口的全部方法,包括: mouseClicked()、mouseEntered()、mouseExited()、 mousePressed()、mouseReleased()、mouseMoved ()、mouseDragged(),对于用不到的方法,可以令方 法为空。 对于Component,需使用addMouseListener(this)和 addMouseMotionListerner(this)来添加事件监听程序,并 在类中实现鼠标按下(mousepressed)和拖拉 (mouseDragged)方法。鼠标按下时得到画图区域的左上 角位置,拖拉过程中得到画图区域的右下角位置。因为是在 鼠标拖拉过程中得到右下角坐标,所以要重新实现public void mouseDragged()方法。

public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mousePressed(MouseEvent e) { M.mp(e.getPoint()); } public void mouseReleased(MouseEvent e) { M.mr(e.getPoint()); } public void mouseDragged(MouseEvent e) { M.md(e.getPoint()); } public void mouseMoved(MouseEvent e) {} } class Menu extends JComponent implements MouseListener,ActionListener{ JComboBox sbox,method; CLabel[] cl;//定义颜色选框 JCheckBox fillC,drawB; JRadioButton fc,bc;//定义两个多选框 ButtonGroup bg;//按钮组 JButton clear,save,load;//三个按钮 Menu(){ this.setLayout(new FlowLayout()); method=new JComboBox(new Object[]{"绘图","移动","擦除",}); add(method); sbox=new JComboBox(new Object[]{"椭圆","直线","长方形","正圆","弧线",});//两个下拉 菜单

BufferedImage buff,tbuff; CVS(){ this.addComponentListener(this); this.addMouseListener(this); this.addMouseMotionListener(this); buff=new BufferedImage(1,1,3); } public void paint(Graphics gr){ Graphics2D g = buff.createGraphics(); g.setBackground(new Color(0xffffffff,true)); g.clearRect(0,0,getWidth(),getHeight()); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, //类 java.awt.RenderingHints 中的静态变量 抗锯齿提示键。 RenderingHints.VALUE_ANTIALIAS_ON);//类 java.awt.RenderingHints 中的 静态变量 抗锯齿提示值——在不使用抗锯齿模式的情况下完成呈现 M.sa.drawAll(g); if(M.ts!=null) M.ts.draw(g); g.dispose(); gr.drawImage(buff,0,0,this); gr.dispose(); }

Leabharlann Baidu

add(sbox); cl=new CLabel[13];//出现了13个颜色选框 for(int i=0; i<cl.length; i++){ cl[i]=new CLabel(); cl[i].addMouseListener(this);//鼠标监听事件 add(cl[i]); } fc=new JRadioButton("填充颜色",true); bc=new JRadioButton("边框颜色");//两个多选按钮 bg=new ButtonGroup(); bg.add(fc);bg.add(bc);//在按钮组中添加两个按钮 add(fc);add(bc); fc.setOpaque(true);//默认为勾选 bc.setOpaque(true); fc.setBackground(Color.white);//将填充颜色初始化为白色 bc.setBackground(Color.blue);//将边框颜色初始化为蓝色 fillC=new JCheckBox("填充",true);//默认为选中 drawB=new JCheckBox("边框",true); fillC.addActionListener(this); drawB.addActionListener(this); add(fillC);add(drawB);
Java程序设计 绘图软件的开发
一、设计简介:
绘图软件的开发,顾名思义,就是要开发一
个能进行绘图的程序,为了能获取鼠标单击 点的位置信息,需要用鼠标事件监听程序 MouseListener和MouseMotionListener来获 取鼠标事件,为此说明一个类来说明和实现 这两个接口。还要调用调色板。
相关文档
最新文档