java模拟画板

package Project01;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;

public class Code01 extends JFrame{

    int x1,x2,y1,y2;
    int selectMode=0;
    Color selectColor;
    int cnt;
    public static void main(String[] args)  {
         new Code01();

    }
    
 public Code01()
    {  
      //初始化,添加控件
        this.setTitle("我的画板");
        this.setBounds(100, 100, 400, 400);
        this.setLayout(null);
        this.setBackground(Color.white);
        MyPanel jpanel=new MyPanel();
        
        JButton Line=new JButton("线");
        JButton Rect=new JButton("矩形");
        JButton Cir=new JButton("圆");
        JButton Colorbut=new JButton();
        JButton Save=new JButton("保存");
        JButton Exit=new JButton("退出");
        JButton Clear=new JButton("清屏");
        
         Line.setBounds(0,0, 60,30);
         Rect.setBounds(0,30, 60,30);
         Cir.setBounds(0,60, 60,30);
         Colorbut.setBounds(0,90, 60,30);
         Save.setBounds(0,120, 60,30);
         Exit.setBounds(0,150, 60,30);
         Clear.setBounds(0,180, 60,30);
         this.add(Line);this.add(Rect);this.add(Cir);this.add(Colorbut);
         this.add(Save);this.add(Exit);this.add(Clear);this.add(jpanel);
         this.setVisible(true);
    //添加监听
           
        Line.addMouseListener(new MouseAdapter(){    
            public void mouseClicked(MouseEvent arg0) {
                selectMode=1;
       }});
        Rect.addMouseListener(new MouseAdapter(){    
               public void mouseClicked(MouseEvent arg0) {
                    selectMode=2;
           }});

        Cir.addMouseListener(new MouseAdapter(){
            public void mouseClicked(MouseEvent arg0) {
                selectMode=3;
       }});
        
        //选择颜色
        Colorbut.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent arg0) {
            JColorChooser color=new JColorChooser();           
           selectColor=color.showDialog(null,"颜色选择",Color.white);
           Colorbut.setBackground(selectColor);
        }});
        //保存
         Save.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent arg0) {
             SavePic(jpanel);
         }});
        //退出
         Exit.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent arg0) {
             int b=arg0.getButton();  
             System.exit(b);
         }});
         //清屏
         Clear.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent arg0) {
             Graphics2D g=(Graphics2D)getGraphics();
             g.clearRect(75,10, 380, 390);
         }});
         //主窗口监听器
        jpanel.addMouseListener(new MouseAdapter(){
             public void mousePressed(MouseEvent arg0) {                                 
           x1=arg0.getX()+70;y1=arg0.getY()+30;}              
         public void mouseReleased(MouseEvent arg0) {
             x2=arg0.getX()+70;y2=arg0.getY()+30;
             Graphics2D g=(Graphics2D)getGraphics();
             g.setColor(selectColor);           
             switch(selectMode){
                 case 1: g.drawLine(x1, y1, x2, y2);break;
                 case 2: g.drawRect(x1, y1, x2-x1, y2-y1);break;
                 case 3 : g.drawOval(x1,y1,x2-x1,y2-y1);break;
                 }
             jpanel.select(x1, y1, x2, y2,selectMode,selectColor);
             }
          });
    }
  void SavePic(JPanel jpanel){    //保存画板内容类
            
    Container content=jpanel; //得到画板             
      BufferedImage img=new BufferedImage(jpanel.getWidth(),jpanel.getHeight(),BufferedImage.TYPE_INT_RGB);  //创建图片缓冲区          
     Graphics2D g= img.createGraphics();   //创建可以绘制到图片缓冲区的对象           
       content.printAll(g);    //将画板打印到图形对象中        
    File f=new File("src/image/"+"画板截图"+cnt+".jpg");  //保存为图片
    try {
                
      ImageIO.write(img, "jpg", f);
     } catch (IOException e) {
       e.printStackTrace();
    }          
        g.dispose();//释放图形对象
        cnt++;
        }  
    
}
 class MyPanel extends JPanel{     //画板类
     int[] x1=new int[1000],y1=new int[1000],x2=new int[1000],y2=new int[1000];
     int[] selectMode=new int[1000];
     Color[] selectColor=new Color[1000];
     int i=0;                 //i用来记录第几个图形
    
    public void select(int x1,int y1,int x2,int y2,int selectMode,Color selectColor) {
        this.x1[i]=x1;this.y1[i]=y1;
        this.x2[i]=x2;this.y2[i]=y2;
        this.selectMode[i]=selectMode;
        this.selectColor[i]=selectColor;
        this.i++;
        
    }
    public void paint(Graphics g) {     //画板paint重写
        // TODO Auto-generated method stub
         super.paint(g);
         if((i-1)!=-1) {            
         for(int j = 0;j<=i;j++) {
         g.setColor(selectColor[j]);
         switch(selectMode[j]){
         case 1: g.drawLine(x1[j]-70, y1[j]-30, x2[j]-70, y2[j]-30);break;
         case 2: g.drawRect(x1[j]-70, y1[j]-30,x2[j]-x1[j], y2[j]-y1[j]);break;
         case 3: g.drawOval(x1[j]-70, y1[j]-30,x2[j]-x1[j], y2[j]-y1[j]);break;
           }        
         }        
       }
    }

 public MyPanel(){
    this.setBounds(65, 0, 380,390);
    this.setBackground(Color.white);
     }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值