画图板小程序

上课老师让我们写的画板程序,写的实在是太难看了,功能很简单,保存着以后看看。

//测试类
public class Test {
     public static void main(String[] args) {
           //创建一个画图板对象
          DrawPanelDemo d = new DrawPanelDemo();
           //调用DrawPanelDemo类中的监听器方法
          d.Listener();
           //调用DrawPanelDemo类中的画图方法
          d.drawMetoh();
           //调用DrawPanelDemo类中的鼠标拖动监听方法
          d.myMouseMotionListener();
     }
}
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;

//画板类
public class DrawPanelDemo extends JFrame{
     
     //坐标
     private int x1;
     private int x;
     private int x2;
     private int y1;
     private int y;
     private int y2;
     private Color color = Color. black;
     private  int taye;
     //创建按钮
     JButton b1 = new JButton( "直线");
     JButton b2 = new JButton( "矩形");
     JButton b3 = new JButton( "圆");
     JButton b4 = new JButton();        //橙色按钮
     JButton b5 = new JButton();        //蓝色按钮
     JButton b6 = new JButton();        //黑色按钮
     JButton b7 = new JButton();        //红色按钮
     //创建Graphics对象
     Graphics g;
     //存取图像信息
     ArrayList<Line> line = new ArrayList<Line>();
     ArrayList<Rectangle> rect = new ArrayList<Rectangle>();
     ArrayList<Round> round = new ArrayList<Round>();
     //重写paint方法,图像重绘
     @Override
     public void paint(Graphics g) {
           super.paint(g);
           for(Line l : line){
              g = DrawPanelDemo. this.getGraphics();
              g.setColor(l.getColor());
              g.drawLine(l.getX1(), l.getY1(), l.getX2(), l.getY2());
          }
           for(Rectangle r: rect){
              g = DrawPanelDemo. this.getGraphics();
              g.setColor(r.getColor());
              g.drawRect(r.getX(), r.getY(), Math.abs(r.getX2()-r.getX1()), Math.abs(r.getY2()-r.getY1()));
          }
           for(Round ro : round){
              g = DrawPanelDemo. this.getGraphics();
              g.setColor(ro.getColor());
              g.drawOval(ro.getX(), ro.getY(), Math.abs(ro.getX2()-ro.getX1()), Math.abs(ro.getY2()-ro.getY1()));
          }
     }
     
     public DrawPanelDemo(){
           //设置布局
           super( "画图板");
           this.setSize(800, 490);
           this.setLayout( null);
           b1.setBounds(0, 0, 65, 65);
           b2.setBounds(0, 65, 65, 65);
           b3.setBounds(0, 130, 65, 65);
           b4.setBounds(0, 195, 65, 65);
           b5.setBounds(0, 260, 65, 65);
           b6.setBounds(0, 325, 65, 65);
           b7.setBounds(0, 390, 65, 65);
           b4.setBackground(Color. orange);
           b5.setBackground(Color. blue);
           b6.setBackground(Color. black);
           b7.setBackground(Color. red);
          add( b1);
          add( b2);
          add( b3);
          add( b4);
          add( b5);
          add( b6);
          add( b7);
           this.setVisible( true);
           this.setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE);
     }
     
     //按钮监听
     public void Listener(){
           b1.addActionListener( new ActionListener() {
              
               @Override
               public void actionPerformed(ActionEvent e) {
                    taye = 1;  //画直线
              }
          });
           b2.addActionListener( new ActionListener() {
              
               @Override
               public void actionPerformed(ActionEvent e) {
                    taye = 2;  //画矩形
              }
          });
           b3.addActionListener( new ActionListener() {
              
               @Override
               public void actionPerformed(ActionEvent e) {
                    taye = 3;  //画圆
              }
          });
           b4.addActionListener( new ActionListener() {
              
               @Override
               public void actionPerformed(ActionEvent e) {
                    color = Color. orange;
              }
          });
           b5.addActionListener( new ActionListener() {
              
               @Override
               public void actionPerformed(ActionEvent e) {
                    color = Color. blue;
              }
          });
           b6.addActionListener( new ActionListener() {
              
               @Override
               public void actionPerformed(ActionEvent e) {
                    color = Color. black;
              }
          });
           b7.addActionListener( new ActionListener() {
              
               @Override
               public void actionPerformed(ActionEvent e) {
                    color = Color. red;
              }
          });
     }
     //实现画图功能
     public void drawMetoh(){
          addMouseListener( new MouseListener() {
              
               @Override
               public void mouseReleased(MouseEvent e) {
                    x2 = e.getX();
                    y2 = e.getY();
                    //g.drawLine(x1, y1, x2, y2);
                    g = DrawPanelDemo. this.getGraphics();
                    if( taye == 1){
                         g.setColor( color);
                         g.drawLine( x1, y1, x2, y2);
                         line.add( new Line( x1, x2, y1, y2, color));
                   } else if( taye == 2){
                         if( x1< x2 && y1< y2){
                              x = x1;
                              y = y1;
                              g.setColor( color);
                              g.drawRect( x, y, Math. abs(x2 - x1), Math. abs(y2 - y1));
                              rect.add( new Rectangle(x,y ,x1 , x2 , y1 , y2 , color ));
                        } else if( x1> x2 && y1> y2){
                              x = x2;
                              y = y2;
                              g.setColor( color);
                              g.drawRect( x, y, Math. abs(x2 - x1), Math. abs(y2 - y1));
                              rect.add( new Rectangle(x,y ,x1 , x2 , y1 , y2 , color ));
                        } else if( x1< x2 && y1> y2){
                              x = x1;
                              y = y2;
                              g.setColor( color);
                              g.drawRect( x, y, Math. abs(x2 - x1), Math. abs(y2 - y1));
                              rect.add( new Rectangle(x,y ,x1 , x2 , y1 , y2 , color ));
                        } else if( x1> x2 && y2> y1){
                              x = x2;
                              y = y1;
                              g.setColor( color);
                              g.drawRect( x, y, Math. abs(x2 - x1), Math. abs(y2 - y1));
                              rect.add( new Rectangle(x,y ,x1 , x2 , y1 , y2 , color ));
                        }
                   } else if( taye == 3){
                         if( x1< x2 && y1< y2){
                              x = x1;
                              y = y1;
                              g.setColor( color);
                              g.drawOval( x, y, Math. abs(x2 - x1), Math. abs(y2 - y1));
                              round.add( new Round( x, y, x1, x2, y1, y2, color));
                        } else if( x1> x2 && y1> y2){
                              x = x2;
                              y = y2;
                              g.setColor( color);
                              g.drawOval( x, y, Math. abs(x2 - x1), Math. abs(y2 - y1));
                              round.add( new Round( x, y, x1, x2, y1, y2, color));
                        } else if( x1< x2 && y1> y2){
                              x = x1;
                              y = y2;
                              g.setColor( color);
                              g.drawOval( x, y, Math. abs(x2 - x1), Math. abs(y2 - y1));
                              round.add( new Round( x, y, x1, x2, y1, y2, color));
                        } else if( x1> x2 && y2> y1){
                              x = x2;
                              y = y1;
                              g.setColor( color);
                              g.drawOval( x, y, Math. abs(x2 - x1), Math. abs(y2 - y1));
                              round.add( new Round( x, y, x1, x2, y1, y2, color));
                        }
                   }
              }
              
               @Override
               public void mousePressed(MouseEvent e) {
                    x1 = e.getX();
                    y1 = e.getY();
              }
              
               @Override
               public void mouseExited(MouseEvent e) {}
              
               @Override
               public void mouseEntered(MouseEvent e) {}
              
               @Override
               public void mouseClicked(MouseEvent e) {}
          });
     }
     //拖动效果,监听器
     public void myMouseMotionListener(){
          addMouseMotionListener( new MouseMotionListener() {
              
               @Override
               public void mouseMoved(MouseEvent e) {
                    // TODO Auto-generated method stub
                   
              }
              
               @Override
               public void mouseDragged(MouseEvent e) {
                    x2 = e.getX();
                    y2 = e.getY();
                    g = DrawPanelDemo. this.getGraphics();
                    if( taye == 1){
                         g.setColor( color);
                         g.drawLine( x1, y1, x2, y2);
                        repaint();
                   } else if( taye == 2){
                         if( x1< x2 && y1< y2){
                              x = x1;
                              y = y1;
                              g.setColor( color);
                              g.drawRect( x, y, Math. abs(x2 - x1), Math. abs(y2 - y1));
                             repaint();
                        } else if( x1> x2 && y1> y2){
                              x = x2;
                              y = y2;
                              g.setColor( color);
                              g.drawRect( x, y, Math. abs(x2 - x1), Math. abs(y2 - y1));
                             repaint();
                        } else if( x1< x2 && y1> y2){
                              x = x1;
                              y = y2;
                              g.setColor( color);
                              g.drawRect( x, y, Math. abs(x2 - x1), Math. abs(y2 - y1));
                             repaint();
                        } else if( x1> x2 && y2> y1){
                              x = x2;
                              y = y1;
                              g.setColor( color);
                              g.drawRect( x, y, Math. abs(x2 - x1), Math. abs(y2 - y1));
                             repaint();
                        }
                   } else if( taye == 3){
                         if( x1< x2 && y1< y2){
                              x = x1;
                              y = y1;
                              g.setColor( color);
                              g.drawOval( x, y, Math. abs(x2 - x1), Math. abs(y2 - y1));
                             repaint();
                        } else if( x1> x2 && y1> y2){
                              x = x2;
                              y = y2;
                              g.setColor( color);
                              g.drawOval( x, y, Math. abs(x2 - x1), Math. abs(y2 - y1));
                             repaint();
                        } else if( x1< x2 && y1> y2){
                              x = x1;
                              y = y2;
                              g.setColor( color);
                              g.drawOval( x, y, Math. abs(x2 - x1), Math. abs(y2 - y1));
                             repaint();
                        } else if( x1> x2 && y2> y1){
                              x = x2;
                              y = y1;
                              g.setColor( color);
                              g.drawOval( x, y, Math. abs(x2 - x1), Math. abs(y2 - y1));
                             repaint();
                        }
                   }
              }
          });
     }

}
画板类
import java.awt.Color;
//直线类
public class Line {
     private int x1;
     private int x2;
     private int y1;
     private int y2;
     Color color;
     public Line( int x1, int x2, int y1, int y2, Color color) {
           super();
           this. x1 = x1;
           this. x2 = x2;
           this. y1 = y1;
           this. y2 = y2;
           this. color = color;
     }
     public int getX1() {
           return x1;
     }
     public void setX1( int x1) {
           this. x1 = x1;
     }
     public int getX2() {
           return x2;
     }
     public void setX2( int x2) {
           this. x2 = x2;
     }
     public int getY1() {
           return y1;
     }
     public void setY1( int y1) {
           this. y1 = y1;
     }
     public int getY2() {
           return y2;
     }
     public void setY2( int y2) {
           this. y2 = y2;
     }
     public Color getColor() {
           return color;
     }
     public void setColor(Color color) {
           this. color = color;
     }
     
}
直线类
import java.awt.Color;
//矩形类
public class Rectangle {
     private int x;
     private int y;
     private int x1;
     private int x2;
     private int y1;
     private int y2;
     Color color;
     public Rectangle( int x, int y, int x1, int x2, int y1, int y2, Color color) {
           super();
           this. x = x;
           this. y = y;
           this. x1 = x1;
           this. x2 = x2;
           this. y1 = y1;
           this. y2 = y2;
           this. color = color;
     }
     
     public int getX() {
           return x;
     }

     public void setX(int x) {
           this. x = x;
     }

     public int getY() {
           return y;
     }

     public void setY(int y) {
           this. y = y;
     }

     public int getX1() {
           return x1;
     }
     public void setX1( int x1) {
           this. x1 = x1;
     }
     public int getX2() {
           return x2;
     }
     public void setX2( int x2) {
           this. x2 = x2;
     }
     public int getY1() {
           return y1;
     }
     public void setY1( int y1) {
           this. y1 = y1;
     }
     public int getY2() {
           return y2;
     }
     public void setY2( int y2) {
           this. y2 = y2;
     }
     public Color getColor() {
           return color;
     }
     public void setColor(Color color) {
           this. color = color;
     }
     
}
矩形类
import java.awt.Color;
//椭圆类
public class Round {
     private int x;
     private int y;
     private int x1;
     private int x2;
     private int y1;
     private int y2;
     Color color;
     public Round( int x, int y, int x1, int x2, int y1, int y2, Color color) {
           super();
           this. x = x;
           this. y = y;
           this. x1 = x1;
           this. x2 = x2;
           this. y1 = y1;
           this. y2 = y2;
           this. color = color;
     }
     public int getX() {
           return x;
     }
     public void setX(int x) {
           this. x = x;
     }
     public int getY() {
           return y;
     }
     public void setY(int y) {
           this. y = y;
     }
     public int getX1() {
           return x1;
     }
     public void setX1( int x1) {
           this. x1 = x1;
     }
     public int getX2() {
           return x2;
     }
     public void setX2( int x2) {
           this. x2 = x2;
     }
     public int getY1() {
           return y1;
     }
     public void setY1( int y1) {
           this. y1 = y1;
     }
     public int getY2() {
           return y2;
     }
     public void setY2( int y2) {
           this. y2 = y2;
     }
     public Color getColor() {
           return color;
     }
     public void setColor(Color color) {
           this. color = color;
     }
     
}
椭圆类

界面效果:

转载于:https://www.cnblogs.com/junzhao/p/4852444.html

最近做了一个类似Windows画图板小程序,拿来和大家分享一下。功能和界面全部模仿于Windows自带的画图板,界面如截图。功能主要有: 手绘线、简单图形、文字输入、图块拖放、重复撤销、画面缩放、打开保存图片文件,另外为了直接从数据库或者XML中存取图片,另外还提供了从Base64编码存取图片的接口,其他还实现了和画图板同样效果的工具箱及颜料盒。 1、 绘图: 绘制功能主要在OnLButtonDown、OnMouseMove、OnLButtonUp中实现,根据当前选择的工具和工具的样式进行绘制。程序中每种工具的绘制都是通过一个图元对象实现,比如钢笔、画刷因为本质上都是手绘线所以都通过CDrawPen来实现、直线和矩形等都通过CDrawShape来实现,所有的图元对象都放在文件DrawObject.h中,具体的实现请参见代码。 另外为了避免屏幕的闪烁,程序中做了两个处理:一、把视图自己的重绘背景代码屏蔽掉,即响应视图的WM_ERASEBKGND消息,直接返回TRUE,并在Ondraw中程序自己绘制背景就可以了;二,在Ondraw中绘制的时候,先绘制到内存DC中,然后再贴回视图的DC。这样就基本上就可以避免屏幕的闪烁了。 2、 撤销重做: 由于整个程序较为简单,就没有采用把动作记录下来的方式,只是采取了一个较简单的方法,在每当一个图元绘制动作结束时就把画布的内容存到bmp中以备撤销,这些历史画面存放在一个bmp数组中,并定义了一个Stack类来管理该数组(此实现方式参考了在线杂志21期《类似画笔的绘图控件-卫琳》,在此表示感谢!),然后在屏幕上绘制的时候就是把当前位图贴到屏幕上,并把还没存取位图的图元绘上就行了。 3、 缩放: 一般视图缩放的实现是通过DC的两个函数来实现:SetViewportExt和SetWindowExt,但是这两个函数只在MM_ISOTROPIC和MM_ANISOTROPIC这两种映射模式下有效果,而常用的带滚动条的视图类CScrollView却不支持这两种模式(参见CScrollView::SetScrollSizes)的实现。所以只好把MFC的CScrollView的代码拿过来改了一下以支持MM_ISOTROPIC映射模式,如下所示,具体参见程序文件“ADMMScrollView.h”: ::SetMapMode(hdc,MM_ISOTROPIC); int XLogMm = ::GetDeviceCaps(hdc, HORZSIZE); int YLogMm = ::GetDeviceCaps(hdc, VERTSIZE); int XLogPix = ::GetDeviceCaps(hdc, HORZRES); int YLogPix = ::GetDeviceCaps(hdc, VERTRES); ::SetWindowExtEx(hdc,XLogMm*100,YLogMm*100,NULL); ::SetViewportExtEx(hdc,(int)(XLogPix*fZoomScale), (int)(YLogPix*fZoomScale),NULL); 4、 工具箱: 首先为了实现工具箱的按钮分两列显示的效果,需要设置一下按钮的TBBS_WRAPPED,参见程序中的CToolPaletteBar类;然后,选择不同工具时展现出工具的样式,比如选择“直线”时列出可用的直线宽度样式,该功能通过在ToolBar上放一个CListCtrl实现,当前的工具样式通过图标的方式展现,效果和画图板的类似。 5、 颜料盒: 从CDialogBar类继承了一个类,然后在WM_PAINT响应函数里面绘制一个个小颜色矩形,并重写其OnLButtonDown、OnRButtonDown、OnLButtonDblClk来和用户交互,实现出来的效果还不错,看上去和画图板的颜料盒一样。 6、 鼠标光标样式: 首先在资源文件中增加需要的光标资源,然后在视图的OnSetCursor消息函数中调用SetCursor函数来设置光标样式就可以了,注意要判断HitTest参数是否为HTCLIENT,不然的话鼠标移到了滚动条上的时候光标还是画笔的样式就让人觉得怪怪的了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值