⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 draw.java

📁 Java编写的画板程序
💻 JAVA
字号:
//导入包

package xupeipei;
import java.awt.Graphics; 
import java.awt.*;
import java.util.*;
import javax.swing.border.*;
import java.awt.Font.*;
import java.awt.image.*;
import java.awt.Frame;
import java.io.*;
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.awt.Toolkit;
import java.awt.Cursor;



public class Draw 
{//定义变量
   public static void main(String args[])
   {  
      DrawImage Painting = new DrawImage();
      Painting.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      Painting.show();
   }
}   
//**************************************************************************************************************
//定义框架
class DrawImage extends JFrame

{  
   public static Color color;
   public int x,y,x1,y1,x2,y2,width,height;
   public boolean isFirstPoint = true;
   public  int drawType = PaintingGround.LINE;
   
   
   Font f = new Font("幼圆",Font.BOLD,18)  ;
     
 //设置菜单   
        JMenuBar menuBar = new JMenuBar(); 
		JMenu fileMenu = new JMenu("文件");
    	JMenuItem newItem =new JMenuItem("新建");
    	JMenuItem openItem =new JMenuItem("打开");
        JMenuItem saveItem =new JMenuItem("保存");
        JMenuItem exitItem =new JMenuItem("退出");
		
	    JMenu editMenu = new JMenu("编辑");
		JMenuItem cutItem =new JMenuItem("剪切");
		JMenuItem copyItem =new JMenuItem("复制");
		JMenuItem pasteItem =new JMenuItem("粘贴");
		
		
		JMenu imageMenu = new JMenu("图象");
		JMenuItem cleanItem =new JMenuItem("清除");
		JMenuItem shxItem =new JMenuItem("属性");
		
     //定义组件 
      PaintingGround paintingGround = new PaintingGround(); 
      JPanel ButtonPanel = new JPanel();
      JLabel ColorLabel = new JLabel("color selected");
      JButton ColorButton = new JButton();
      JLabel TextLabel = new JLabel("button selected");
      JTextField Text = new JTextField("");
      
      JButton btLine = new JButton("line");
	  JButton btRectangle = new JButton("Rectangle");
      JButton btRound = new JButton("Round");
      JButton btEllipse = new JButton("Ellipse");
      JButton fillRetangle = new JButton("fillRetangle");
      JButton fillEllipse= new JButton("fillEllipse");
      JButton clean = new JButton("clean");
      JButton colorset = new JButton("colorset");
    
/***************构造方法*****************/ 
   public DrawImage()
   {
       //改变标题图像	
   	   Toolkit tk=Toolkit.getDefaultToolkit() ;
       Image image=tk.getImage("image.jpg"); /*image.jpg是你的图标*/ 
       this.setIconImage(image);
       //改变鼠标图像
       Image img=tk.getImage("mouse.jpg"); /*mouse.jpg是你的图标*/ 
       Cursor cu=tk.createCustomCursor(img,new Point(1,1),"stick"); 
       this.setCursor(cu);
       //设置标题和初始大小
       setTitle("PaintingGround");
       setSize(600,540);
      
    //添加菜单
    
        setJMenuBar(menuBar);
        menuBar.add(fileMenu);
        	fileMenu.setFont(f);	 
		fileMenu.add(newItem);
			newItem.setFont(f);
		fileMenu.add(openItem);
			openItem.setFont(f);
    /**************设置打开文件的监听器及实现的方法**************/
				openItem.addActionListener(new ActionListener()
				{
		 		public void actionPerformed(ActionEvent evn) 
		 		   {
		 			FileDialog d = new FileDialog(DrawImage.this,"打开文件",FileDialog.LOAD);
		 			d.show();
		 			}
		 		});
		fileMenu.add(saveItem);
			saveItem.setFont(f);
			
	/**************设置保存文件的监听器及实现的方法**************/	
			saveItem.addActionListener(new ActionListener(){
		 		public void actionPerformed(ActionEvent evn) 
		 		   {
		 			FileDialog d = new FileDialog(DrawImage.this,"保存文件",FileDialog.SAVE);
		 			d.show();
		 			
		 			}
		 	    });
		 			
		fileMenu.add(exitItem);
		 exitItem.setFont(f);
		 
	/**************设置退出文件的监听器及实现的方法**************/	 
		 exitItem.addActionListener(new ActionListener()
		       {
		 		public void actionPerformed(ActionEvent evn) 
		 		   {
		 		   	System.exit(0);
		           }
		        });
		 //子菜单添加如菜单条
		 menuBar.add(editMenu);
		 editMenu.setFont(f);
		 
		editMenu.add(cutItem);
		cutItem.setFont(f);
		editMenu.add(copyItem);
		copyItem.setFont(f);
		editMenu.add(pasteItem);
		pasteItem.setFont(f);
		
		 menuBar.add(imageMenu);
		 imageMenu.setFont(f);
		 imageMenu.add(cleanItem);
		 cleanItem.setFont(f);
         imageMenu.add(shxItem);
         shxItem.setFont(f);
         
         
     	/**************设置清除画板的监听器及实现的方法**************/	 
      cleanItem.addActionListener(new ActionListener(){
		 		public void actionPerformed(ActionEvent evn) {
		 			paintingGround.setBackground(Color.yellow);
         }
         });
      //面板加入框架,设置空布局   
      Container c = getContentPane();
      c.setLayout(null);
      c.add(paintingGround);
      paintingGround.setLocation(100,70);
      paintingGround.setSize(500,360) ;
   
      
    
      //将按钮加入面板,实现bt对按钮事件监听
        ButtonPanel.setLayout(new GridLayout(8,1));
        
        ButtonPanel.add(btLine);
            btLine.setToolTipText("线");
        
	    ButtonPanel.add(btRectangle);
	    	btRectangle.setToolTipText("矩形");
	    	
     	ButtonPanel.add(btRound);
     		btRound.setToolTipText("圆");
	
        ButtonPanel.add(btEllipse);
        	btEllipse.setToolTipText("椭圆");
	    ButtonPanel.add(fillRetangle);
	    	fillRetangle.setToolTipText("填充矩形");
	   
	    ButtonPanel.add(fillEllipse);
	    fillEllipse.setToolTipText("填充椭圆");
	    
	    ButtonPanel.add(clean);  
	     clean.setToolTipText("清除");
	     
	    ButtonPanel.add(colorset);
	     colorset.setToolTipText("设置颜色");
   
	  c.add(ButtonPanel);
      ButtonPanel.setLocation(0,70);
      ButtonPanel.setSize(98,400) ;
      
      c.add(ColorLabel) ;   
      ColorLabel.setLocation(230,40);
      ColorLabel.setSize(120,27);
     
      c.add(ColorButton);
      ColorButton.setLocation(360,40);
      ColorButton.setSize(80,27);
      
      c.add(TextLabel);
      TextLabel.setLocation(0,40);
      TextLabel.setSize(100,27);
      
      c.add(Text);
      Text.setLocation(102,40);
      Text.setSize(120,27);
     
     
     
      //设置绘图面板的监听器
  
      
      
   	paintingGround.addMouseListener(new MouseAdapter()
   	 {
				public void mouseReleased(MouseEvent evn) 
				{	
				  
					isFirstPoint = true;
				}
						
	 });
   	paintingGround.addMouseMotionListener(new MouseMotionAdapter()
		{
				public void mouseDragged(MouseEvent evn) 
				{
					if(isFirstPoint)//if-else语句循环执行得到点坐标 
					{
						x1 = evn.getX();
						y1 = evn.getY();
						isFirstPoint = false;	
						
					
					}
					else 
					{	
						 x2 = evn.getX();
						 y2 = evn.getY();
       
          switch(drawType)
						 {
						 //画线	
						case PaintingGround.LINE:
					  //  System.out.println("坐标1:"+x1+y1+"坐标2:"+x2+y2);//得到坐标
						paintingGround.drawLine(x1,y1,x2,y2);
					    break;
						
						//画矫形 
						    case PaintingGround.RECTANGLE:
						    paintingGround.drawRect(x2,y2,Math.abs(x2-x1),Math.abs(y2-y1));
					//	    System.out.println("坐标1:"+x1+y1+"坐标2:"+x2+y2);//得到坐标
						    break;
					    
					    //画圆
					        case PaintingGround.ROUND:
						    //两点距离公式
						    int size = Math.abs((int)Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)));
						    paintingGround.drawOval(x1,y1,size);
					//	   System.out.println("坐标1:"+x1+y1+"坐标2:"+x2+y2);//得到坐标
						    break;
						//画椭圆	
							case PaintingGround.ELLIPSE:
					//		System.out.println("坐标:"+x1+y1+x2+y2);//得到坐标	
						//	paintingGround.drawOval(x1,y1,Math.abs(x2-x1),Math.abs(y2-y1));
					  //  	System.out.println("坐标1:"+x1+y1+"坐标2:"+x2+y2);//得到坐标
						    break;
						//画填充矩形		
							case PaintingGround.FILLRECTANGLE:
							paintingGround.fillRect(x1,y1,Math.abs(x2-x1),Math.abs(y2-y1));
					 //   	System.out.println("坐标1:"+x1+y1+"坐标2:"+x2+y2);//得到坐标
							break;
						//画填充椭圆	    
							case PaintingGround.FILLELLIPSE:
							paintingGround.fillOval(x1,y1,Math.abs(x2-x1),Math.abs(y2-y1));
					 //   	System.out.println("坐标1:"+x1+y1+"坐标2:"+x2+y2);//得到坐标
							break;
						//清除	    
							case PaintingGround.CLEAN:
							paintingGround.clearRect(x1,y1,x2,y2);
							default:
							break;
						}
       
                 }
             } 
     });  
      
      
      
      
//设置按扭的监听器
      
         btLine.addActionListener(new ActionListener(){
		 		public void actionPerformed(ActionEvent evn) {
		 			drawType = PaintingGround.LINE;
		 			Text.setText("线");
		 		}
		 	});
		 
		 btRectangle.addActionListener(new ActionListener(){
		 		public void actionPerformed(ActionEvent evn) {
		 			drawType = PaintingGround.RECTANGLE;
		 			Text.setText("矩形");
		 		}
		 	});
		 
		 btRound.addActionListener(new ActionListener(){
		 		public void actionPerformed(ActionEvent evn) {
		 			drawType = PaintingGround.ROUND;
		 			Text.setText("圆");
		 		}
		 	});
		 
		 btEllipse.addActionListener(new ActionListener(){
		 		public void actionPerformed(ActionEvent evn) {
		 			drawType = PaintingGround.ELLIPSE;
		 			Text.setText("椭圆");
		 			
		 		}
		 	});
		 	
		 fillRetangle.addActionListener(new ActionListener(){
		 		public void actionPerformed(ActionEvent evn) {	
		 			drawType = PaintingGround.FILLRECTANGLE;
		 			Text.setText("填充矩形");
		 			}
		 	});
		 	
		 	
		 fillEllipse.addActionListener(new ActionListener(){
		 		public void actionPerformed(ActionEvent evn) {	
		 		drawType = PaintingGround.FILLELLIPSE;
		 		Text.setText("填充椭圆");
		 			}
		 	});
		 	
		 clean.addActionListener(new ActionListener(){
		 		public void actionPerformed(ActionEvent evn) {
		 			
		 			 drawType = PaintingGround.CLEAN;
		 			 Text.setText("橡皮");
		 			}
		 	});
		 	
		 	
		 colorset.addActionListener(new ActionListener(){
		 		public void actionPerformed(ActionEvent evn) {	
		 	 Text.setText("颜色设置");
		 color = JColorChooser.showDialog(DrawImage.this, "Choose a color", null);
		            ColorButton.setBackground(color) ;
		 			}
		 	});

 }    
 }
 
// **********************************************************************************************************
//定义绘图面板,设置参数
  class PaintingGround extends JPanel 
{  
    public static final int LINE = 1;
	public static final int RECTANGLE = 2;
	public static final int ROUND = 3;
	public static final int ELLIPSE = 4;
	public static final int FILLRECTANGLE = 5;
	public static final int FILLELLIPSE = 6;
	public static final int CLEAN = 7;
	public static final int COLORSET = 8;
    private int x1,y1;
	private int x2,y2;
	private int width, height,size;
	private int drawType = 0;


 public PaintingGround()
      {
      	   setBackground(Color.pink);
      	   }
           	
  public void paint(Graphics g)
	{
		super.paint(g);
		
		g.setColor(DrawImage.color);
			
	        switch(drawType)
            {  case LINE:
                            g.drawLine(x1,y1,x2,y2);break;
                            
               case RECTANGLE:
                            g.drawRect(x1,y1,width,height);  break;
                            
               case ROUND:   
                            g.drawOval(x1,y1,size,size);     break;
                            
               case ELLIPSE:  
                            g.drawOval(x1,y1,width,height);  break;
                            
               case FILLRECTANGLE:   
                            g.fillRect(x1,y1,width,height);  break;
                            
               case FILLELLIPSE:
                            g.fillOval(x1,y1,width,height);  break;
                         
               case CLEAN:   
               
                            g.setColor(Color.white);
                            g.clearRect(x1,y1,width,height);  break;
              default :  break;           
           } 
    }  
    
   	public void drawLine(int x1, int y1, int x2,int y2) {
		this.x1 = x1;
		this.y1 = y1;
		this.x2 = x2;
		this.y2 = y2;
		
		drawType = LINE;
		repaint();
	}
	//具体的实现方式
	public void drawRect(int x1,int y1,int width, int height) {
		this.x1 = x1;
		this.y1 = y1;
		this.width = width;
		this.height = height;
		drawType = RECTANGLE;
		repaint();
	}
	
	public void drawOval(int x1,int y1,int size) {
		this.x1 = x1;
		this.y1 = y1;
		this.size = size;
		drawType = ROUND;
		repaint();
	}

	
    public void fillRect(int x1,int y1,int width, int height) {
		this.x1 = x1;
		this.y1 = y1;
		this.width = width;
		this.height = height;
		drawType = FILLRECTANGLE;
		repaint();
	}
	
	
	public void fillOval(int x1,int y1,int width,int height) {
		this.x1 = x1;
		this.y1 = y1;
		this.width = width;
		this.height = height;
		drawType = FILLELLIPSE;
		repaint();
	}
	
	
	public void clearRect(int x1,int y1,int width, int height) {
		this.x1 = x1;
		this.y1 = y1;
		this.width = width;
		this.height = height;
		drawType = CLEAN;
		repaint();
	  }      
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -