1、Java版本简单的手绘程序,点击右键可以选择颜色,对初学Java的同学还是很有帮助!学会做这个,下一步就可以做出一个简单的Windows版本的画图!


package project;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.p_w_picpath.BufferedImage;
/**
 *
 *@author huyongjian Oracle(Compus Solution Group)
 * @Date  2013-8-18
 * @version 2.0
 * @since JDK1.6(建议)
   Copy Right Information    COMPUS SOLUTION GROUP
   IDE:Eclipse
   class:
   绘制图像01
 */
public class HandDraw {
    //画图去的宽度
    private final int AREA_WIDTH=500;
    //画图去的高度
    private final int AREA_HEIGHT=400;
    //下面的prex、prey保存了上一次鼠标拖动事件的鼠标坐标
    private int preX=-1;
    private int preY=-1;
    //定义一个右键菜单用于设置画笔颜色
    PopupMenu pop=new PopupMenu();
    MenuItem redItem=new MenuItem("红色");
    MenuItem greenItem=new MenuItem("绿色");
    MenuItem blueItem=new MenuItem("蓝色");
    MenuItem blackItem=new MenuItem("黑色");
    //定义一个BufferedImage对象
    BufferedImage p_w_picpath=new BufferedImage(AREA_WIDTH,AREA_HEIGHT,
            BufferedImage.TYPE_INT_RGB
            );
    //获取p_w_picpath对象的Graphics
    Graphics g=p_w_picpath.getGraphics();
    private Frame f=new Frame("简单手绘程序");
    private DrawCanvas drawArea=new DrawCanvas();
    //用于保存需要绘制什么图形的字符串属性
    private String shape="";
    //用于保存画笔颜色
    private Color foreColor=new Color(255,0,0);
    public void init(){
        ActionListener menuListener=new ActionListener(){
            public void actionPerformed(ActionEvent e){
                if(e.getActionCommand().equals("绿色")){
                    foreColor=new Color(0,255,0);
                }
                if(e.getActionCommand().equals("红色")){
                    foreColor=new Color(255,0,0);
                }
                if(e.getActionCommand().equals("蓝色")){
                    foreColor=new Color(0,0,255);
                }
                if(e.getActionCommand().equals("黑色")){
                    foreColor=new Color(0,0,0);
                }
            }
        };
        //为三个菜单添加事件监听器
        redItem.addActionListener(menuListener);
        greenItem.addActionListener(menuListener);
        blueItem.addActionListener(menuListener);
        blackItem.addActionListener(menuListener);
        //将菜单项组合成右键菜单
        pop.add(redItem);
        pop.add(greenItem);
        pop.add(blueItem);
        pop.add(blackItem);
        //将右键菜单添加到drawArea对象中
        drawArea.add(pop);
        //将p_w_picpath对象的背景色填充成白色
        g.fillRect(0, 0, AREA_WIDTH, AREA_HEIGHT);
        drawArea.setPreferredSize(new Dimension(AREA_WIDTH,AREA_HEIGHT));
        //监听鼠标移动动作
        drawArea.addMouseMotionListener(new MouseMotionAdapter(){
            //实现按下鼠标键并拖动的事件处理器
            public void mouseDragged(MouseEvent e) {
                //如果preX和preY大于0
                if(preX>0&&preY>0){
                    //设置当前颜色
                    g.setColor(foreColor);
                    //绘制从上一次鼠标拖动事件点到本次鼠标拖动事件点的线段
                    g.drawLine(preX, preY, e.getX(), e.getY());
                }
                //将当前鼠标事件点的x、y坐标保存起来
                preX=e.getX();
                preY=e.getY();
                //重绘drawArea对象
                drawArea.repaint();
            }
        });
        drawArea.addMouseListener(new MouseAdapter(){
            public void mouseReleased(MouseEvent e) {
                //弹出右键菜单
                if(e.isPopupTrigger()){
                    pop.show(drawArea, e.getX(), e.getY());
                }
                //松开鼠标键,把上一次鼠标拖动事件的 x y坐标设为-1;
                preX=-1;
                preY=-1;
            }
        });
        f.add(drawArea);
        f.pack();
        f.setVisible(true);
        f.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }
        });
        f.setLocationRelativeTo(null);
    }
                      
    public static void main(String[] args) {
        new HandDraw().init();
    }
    class DrawCanvas extends Canvas{
        public void paint(Graphics g) {
         g.drawImage(p_w_picpath, 0, 0, null);
        }
    }
}


群号:160243674--Java程序员联盟 欢迎大家的加入!



171324180.jpg